aboutsummaryrefslogtreecommitdiffstats
path: root/html.c
diff options
context:
space:
mode:
authorGravatar Lars Hjemli <hjemli@gmail.com>2008-04-09 04:11:36 (JST)
committerGravatar Lars Hjemli <hjemli@gmail.com>2008-04-09 04:11:36 (JST)
commite87e89633383b8b75c68c98be3e0c14212109de2 (patch)
treef57e131ab854b58023387aee8efc0e4ee54653b5 /html.c
parent20a33548b9a87a6eb23162ee5d137daa46d78613 (diff)
downloadcgit-e87e89633383b8b75c68c98be3e0c14212109de2.zip
cgit-e87e89633383b8b75c68c98be3e0c14212109de2.tar.gz
Move cgit_parse_query() from parsing.c to html.c as http_parse_querystring()
This is a generic http-function. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (limited to 'html.c')
-rw-r--r--html.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/html.c b/html.c
index 0962e71..98ffaf9 100644
--- a/html.c
+++ b/html.c
@@ -185,3 +185,67 @@ int html_include(const char *filename)
185 fclose(f); 185 fclose(f);
186 return 0; 186 return 0;
187} 187}
188
189int hextoint(char c)
190{
191 if (c >= 'a' && c <= 'f')
192 return 10 + c - 'a';
193 else if (c >= 'A' && c <= 'F')
194 return 10 + c - 'A';
195 else if (c >= '0' && c <= '9')
196 return c - '0';
197 else
198 return -1;
199}
200
201char *convert_query_hexchar(char *txt)
202{
203 int d1, d2;
204 if (strlen(txt) < 3) {
205 *txt = '\0';
206 return txt-1;
207 }
208 d1 = hextoint(*(txt+1));
209 d2 = hextoint(*(txt+2));
210 if (d1<0 || d2<0) {
211 strcpy(txt, txt+3);
212 return txt-1;
213 } else {
214 *txt = d1 * 16 + d2;
215 strcpy(txt+1, txt+3);
216 return txt;
217 }
218}
219
220int http_parse_querystring(char *txt, void (*fn)(const char *name, const char *value))
221{
222 char *t, *value = NULL, c;
223
224 if (!txt)
225 return 0;
226
227 t = txt = strdup(txt);
228 if (t == NULL) {
229 printf("Out of memory\n");
230 exit(1);
231 }
232 while((c=*t) != '\0') {
233 if (c=='=') {
234 *t = '\0';
235 value = t+1;
236 } else if (c=='+') {
237 *t = ' ';
238 } else if (c=='%') {
239 t = convert_query_hexchar(t);
240 } else if (c=='&') {
241 *t = '\0';
242 (*fn)(txt, value);
243 txt = t+1;
244 value = NULL;
245 }
246 t++;
247 }
248 if (t!=txt)
249 (*fn)(txt, value);
250 return 0;
251}