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>
This commit is contained in:
Lukas Fleischer 2016-09-29 08:38:45 +02:00 committed by Jason A. Donenfeld
parent e18a85b6a2
commit 927b0ae30c
2 changed files with 11 additions and 57 deletions

66
html.c
View File

@ -8,6 +8,7 @@
#include "cgit.h" #include "cgit.h"
#include "html.h" #include "html.h"
#include "url.h"
/* Percent-encoding of each character, except: a-zA-Z0-9!$()*,./:;@- */ /* Percent-encoding of each character, except: a-zA-Z0-9!$()*,./:;@- */
static const char* url_escape_table[256] = { static const char* url_escape_table[256] = {
@ -337,64 +338,17 @@ int html_include(const char *filename)
return 0; return 0;
} }
static int hextoint(char c) void http_parse_querystring(const char *txt, void (*fn)(const char *name, const char *value))
{ {
if (c >= 'a' && c <= 'f') const char *t = txt;
return 10 + c - 'a';
else if (c >= 'A' && c <= 'F')
return 10 + c - 'A';
else if (c >= '0' && c <= '9')
return c - '0';
else
return -1;
}
static char *convert_query_hexchar(char *txt) while (t && *t) {
{ char *name = url_decode_parameter_name(&t);
int d1, d2, n; if (*name) {
n = strlen(txt); char *value = url_decode_parameter_value(&t);
if (n < 3) { fn(name, value);
*txt = '\0'; free(value);
return txt-1;
}
d1 = hextoint(*(txt + 1));
d2 = hextoint(*(txt + 2));
if (d1 < 0 || d2 < 0) {
memmove(txt, txt + 3, n - 2);
return txt-1;
} else {
*txt = d1 * 16 + d2;
memmove(txt + 1, txt + 3, n - 2);
return txt;
}
}
int http_parse_querystring(const char *txt_, void (*fn)(const char *name, const char *value))
{
char *o, *t, *txt, *value = NULL, c;
if (!txt_)
return 0;
o = t = txt = xstrdup(txt_);
while ((c=*t) != '\0') {
if (c == '=') {
*t = '\0';
value = t + 1;
} else if (c == '+') {
*t = ' ';
} else if (c == '%') {
t = convert_query_hexchar(t);
} else if (c == '&') {
*t = '\0';
(*fn)(txt, value);
txt = t + 1;
value = NULL;
} }
t++; free(name);
} }
if (t != txt)
(*fn)(txt, value);
free(o);
return 0;
} }

2
html.h
View File

@ -32,6 +32,6 @@ extern void html_link_close(void);
extern void html_fileperm(unsigned short mode); extern void html_fileperm(unsigned short mode);
extern int html_include(const char *filename); extern int html_include(const char *filename);
extern int http_parse_querystring(const char *txt, void (*fn)(const char *name, const char *value)); extern void http_parse_querystring(const char *txt, void (*fn)(const char *name, const char *value));
#endif /* HTML_H */ #endif /* HTML_H */