/* This file is part of Eclat.
Copyright (C) 2012-2015 Sergey Poznyakoff.
Eclat is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
Eclat is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Eclat. If not, see . */
#include
#include
#include "libeclat.h"
#include "grecs.h"
struct param_closure {
struct grecs_txtacc *acc;
int count;
};
static int
add_param(void *sym, void *data)
{
struct ec2_param *p = sym;
struct param_closure *pc = data;
if (pc->count)
grecs_txtacc_grow_char(pc->acc, '&');
++pc->count;
grecs_txtacc_grow_string(pc->acc, p->name);
if (p->value) {
grecs_txtacc_grow_char(pc->acc, '=');
grecs_txtacc_grow_string(pc->acc, p->value);
}
return 0;
}
char *
eclat_request_to_url(struct ec2_request *req, char **post_params)
{
struct grecs_txtacc *acc;
char *ret = NULL, *p;
struct param_closure pc;
acc = grecs_txtacc_create();
grecs_txtacc_grow(acc, "http", 4);
if (req->flags & EC2_RF_HTTPS)
grecs_txtacc_grow_char(acc, 's');
grecs_txtacc_grow(acc, "://", 3);
grecs_txtacc_grow(acc, req->endpoint, strlen(req->endpoint));
grecs_txtacc_grow(acc, req->uri, strlen(req->uri));
if (req->flags & EC2_RF_POST) {
grecs_txtacc_grow_char(acc, 0);
ret = grecs_txtacc_finish(acc, 1);
} else {
grecs_txtacc_grow_char(acc, '?');
}
/* Add other parameters */
pc.acc = acc;
pc.count = 0;
grecs_symtab_enumerate(req->params, add_param, &pc);
grecs_txtacc_grow_char(acc, 0);
if (!ret)
ret = grecs_txtacc_finish(acc, 1);
else
*post_params = grecs_txtacc_finish(acc, 1);
grecs_txtacc_free(acc);
return ret;
}