aboutsummaryrefslogtreecommitdiff
path: root/lib/req2url.c
blob: c97d3210087c33d096adfb978ad50cd7911e0f87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* This file is part of Eclat.
   Copyright (C) 2012-2018 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 <http://www.gnu.org/licenses/>. */

#include <config.h>
#include <string.h>
#include "libeclat.h"
#include "grecs.h"

struct param_closure {
	struct grecs_txtacc *acc;
	int delim;
};

static int
add_param(void *sym, void *data)
{	
	struct ec2_param *p = sym;
	struct param_closure *pc = data;

	if (pc->delim)
		grecs_txtacc_grow_char(pc->acc, pc->delim);
	pc->delim = '&';
	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)
{
	struct grecs_txtacc *acc;
	char *url = NULL;
	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));

	/* Add parameters */
	pc.acc = acc;
	pc.delim = '?';
	grecs_symtab_foreach(req->params, add_param, &pc);

	grecs_txtacc_grow_char(acc, 0);
	url = grecs_txtacc_finish(acc, 1);
	grecs_txtacc_free(acc);

	return url;
}

void
eclat_request_finalize(struct ec2_request *req)
{
	struct param_closure pc;

	if (!(req->flags & EC2_RF_POST) || req->postdata)
		return;

	eclat_request_encode(req);
	pc.acc = grecs_txtacc_create();
	pc.delim = 0;
	grecs_symtab_foreach(req->params, add_param, &pc);
	grecs_txtacc_grow_char(pc.acc, 0);
	req->postdata = grecs_txtacc_finish(pc.acc, 1);
	grecs_txtacc_free(pc.acc);
	grecs_symtab_clear(req->params);
}

Return to:

Send suggestions and report system problems to the System administrator.