diff options
| author | Lukas Fleischer | 2016-09-29 08:38:45 +0200 |
|---|---|---|
| committer | Jason A. Donenfeld | 2016-10-01 21:19:38 +0200 |
| commit | 927b0ae30c84fbfce877e35415681dce6eba0229 (patch) | |
| tree | be0b56a1b2ecf4bcc767bd927c779c62e38fed2b /html.c | |
| parent | e18a85b6a298feef88da8085ef16fd20ce971071 (diff) | |
| download | cgit-927b0ae30c84fbfce877e35415681dce6eba0229.tar.gz cgit-927b0ae30c84fbfce877e35415681dce6eba0229.tar.bz2 cgit-927b0ae30c84fbfce877e35415681dce6eba0229.zip | |
Simplify http_parse_querystring()
Instead of reimplementing URL parameter parsing from scratch, use
url_decode_parameter_name() and url_decode_parameter_value() which are
already provided by Git.
Also, change the return type of http_parse_querystring() to void since
its only caller already ignores the return value.
Signed-off-by: Lukas Fleischer <lfleischer@lfos.de>
Diffstat (limited to 'html.c')
| -rw-r--r-- | html.c | 66 |
1 files changed, 10 insertions, 56 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 | } |
