diff options
| -rw-r--r-- | html.c | 66 | ||||
| -rw-r--r-- | html.h | 2 | 
2 files changed, 11 insertions, 57 deletions
| @@ -8,6 +8,7 @@ | |||
| 8 | 8 | ||
| 9 | #include "cgit.h" | 9 | #include "cgit.h" | 
| 10 | #include "html.h" | 10 | #include "html.h" | 
| 11 | #include "url.h" | ||
| 11 | 12 | ||
| 12 | /* Percent-encoding of each character, except: a-zA-Z0-9!$()*,./:;@- */ | 13 | /* Percent-encoding of each character, except: a-zA-Z0-9!$()*,./:;@- */ | 
| 13 | static const char* url_escape_table[256] = { | 14 | static const char* url_escape_table[256] = { | 
| @@ -337,64 +338,17 @@ int html_include(const char *filename) | |||
| 337 | return 0; | 338 | return 0; | 
| 338 | } | 339 | } | 
| 339 | 340 | ||
| 340 | static int hextoint(char c) | 341 | void http_parse_querystring(const char *txt, void (*fn)(const char *name, const char *value)) | 
| 341 | { | 342 | { | 
| 342 | if (c >= 'a' && c <= 'f') | 343 | const char *t = txt; | 
| 343 | return 10 + c - 'a'; | ||
| 344 | else if (c >= 'A' && c <= 'F') | ||
| 345 | return 10 + c - 'A'; | ||
| 346 | else if (c >= '0' && c <= '9') | ||
| 347 | return c - '0'; | ||
| 348 | else | ||
| 349 | return -1; | ||
| 350 | } | ||
| 351 | |||
| 352 | static char *convert_query_hexchar(char *txt) | ||
| 353 | { | ||
| 354 | int d1, d2, n; | ||
| 355 | n = strlen(txt); | ||
| 356 | if (n < 3) { | ||
| 357 | *txt = '\0'; | ||
| 358 | return txt-1; | ||
| 359 | } | ||
| 360 | d1 = hextoint(*(txt + 1)); | ||
| 361 | d2 = hextoint(*(txt + 2)); | ||
| 362 | if (d1 < 0 || d2 < 0) { | ||
| 363 | memmove(txt, txt + 3, n - 2); | ||
| 364 | return txt-1; | ||
| 365 | } else { | ||
| 366 | *txt = d1 * 16 + d2; | ||
| 367 | memmove(txt + 1, txt + 3, n - 2); | ||
| 368 | return txt; | ||
| 369 | } | ||
| 370 | } | ||
| 371 | 344 | ||
| 372 | int http_parse_querystring(const char *txt_, void (*fn)(const char *name, const char *value)) | 345 | while (t && *t) { | 
| 373 | { | 346 | char *name = url_decode_parameter_name(&t); | 
| 374 | char *o, *t, *txt, *value = NULL, c; | 347 | if (*name) { | 
| 375 | 348 | char *value = url_decode_parameter_value(&t); | |
| 376 | if (!txt_) | 349 | fn(name, value); | 
| 377 | return 0; | 350 | free(value); | 
| 378 | |||
| 379 | o = t = txt = xstrdup(txt_); | ||
| 380 | while ((c=*t) != '\0') { | ||
| 381 | if (c == '=') { | ||
| 382 | *t = '\0'; | ||
| 383 | value = t + 1; | ||
| 384 | } else if (c == '+') { | ||
| 385 | *t = ' '; | ||
| 386 | } else if (c == '%') { | ||
| 387 | t = convert_query_hexchar(t); | ||
| 388 | } else if (c == '&') { | ||
| 389 | *t = '\0'; | ||
| 390 | (*fn)(txt, value); | ||
| 391 | txt = t + 1; | ||
| 392 | value = NULL; | ||
| 393 | } | 351 | } | 
| 394 | t++; | 352 | free(name); | 
| 395 | } | 353 | } | 
| 396 | if (t != txt) | ||
| 397 | (*fn)(txt, value); | ||
| 398 | free(o); | ||
| 399 | return 0; | ||
| 400 | } | 354 | } | 
| @@ -32,6 +32,6 @@ extern void html_link_close(void); | |||
| 32 | extern void html_fileperm(unsigned short mode); | 32 | extern void html_fileperm(unsigned short mode); | 
| 33 | extern int html_include(const char *filename); | 33 | extern int html_include(const char *filename); | 
| 34 | 34 | ||
| 35 | extern int http_parse_querystring(const char *txt, void (*fn)(const char *name, const char *value)); | 35 | extern void http_parse_querystring(const char *txt, void (*fn)(const char *name, const char *value)); | 
| 36 | 36 | ||
| 37 | #endif /* HTML_H */ | 37 | #endif /* HTML_H */ | 
