* The arguments are separated by key and value.
*/
-void
+int
evhttp_parse_query(const char *uri, struct evkeyvalq *headers)
{
char *line;
char *argument;
char *p;
+ int result = -1;
TAILQ_INIT(headers);
/* No arguments - we are done */
if (strchr(uri, '?') == NULL)
- return;
+ return 0;
if ((line = mm_strdup(uri)) == NULL) {
- /* TODO(niels): does this function need to return -1 */
event_warn("%s: strdup", __func__);
- return;
+ return -1;
}
-
argument = line;
/* We already know that there has to be a ? */
value = argument;
key = strsep(&value, "=");
- if (value == NULL || *key == '\0')
+ if (value == NULL || *key == '\0') {
goto error;
+ }
if ((decoded_value = mm_malloc(strlen(value) + 1)) == NULL) {
- /* TODO(niels): do we need to return -1 here? */
event_warn("%s: mm_malloc", __func__);
- break;
+ goto error;
}
evhttp_decode_uri_internal(value, strlen(value),
decoded_value, 1 /*always_decode_plus*/);
mm_free(decoded_value);
}
- error:
+ result = 0;
+ goto done;
+error:
+ evhttp_clear_headers(headers);
+done:
mm_free(line);
+ return result;
}
static struct evhttp_cb *
http_parse_query_test(void *ptr)
{
struct evkeyvalq headers;
+ int r;
TAILQ_INIT(&headers);
- evhttp_parse_query("http://www.test.com/?q=test", &headers);
+ r = evhttp_parse_query("http://www.test.com/?q=test", &headers);
tt_want(validate_header(&headers, "q", "test") == 0);
+ tt_int_op(r, ==, 0);
evhttp_clear_headers(&headers);
- evhttp_parse_query("http://www.test.com/?q=test&foo=bar", &headers);
+ r = evhttp_parse_query("http://www.test.com/?q=test&foo=bar", &headers);
tt_want(validate_header(&headers, "q", "test") == 0);
tt_want(validate_header(&headers, "foo", "bar") == 0);
+ tt_int_op(r, ==, 0);
evhttp_clear_headers(&headers);
- evhttp_parse_query("http://www.test.com/?q=test+foo", &headers);
+ r = evhttp_parse_query("http://www.test.com/?q=test+foo", &headers);
tt_want(validate_header(&headers, "q", "test foo") == 0);
+ tt_int_op(r, ==, 0);
evhttp_clear_headers(&headers);
- evhttp_parse_query("http://www.test.com/?q=test%0Afoo", &headers);
+ r = evhttp_parse_query("http://www.test.com/?q=test%0Afoo", &headers);
tt_want(validate_header(&headers, "q", "test\nfoo") == 0);
+ tt_int_op(r, ==, 0);
evhttp_clear_headers(&headers);
- evhttp_parse_query("http://www.test.com/?q=test%0Dfoo", &headers);
+ r = evhttp_parse_query("http://www.test.com/?q=test%0Dfoo", &headers);
tt_want(validate_header(&headers, "q", "test\rfoo") == 0);
+ tt_int_op(r, ==, 0);
+ evhttp_clear_headers(&headers);
+
+ r = evhttp_parse_query("http://www.test.com/?q=test&&q2", &headers);
+ tt_int_op(r, ==, -1);
+ evhttp_clear_headers(&headers);
+
+ r = evhttp_parse_query("http://www.test.com/?q=test+this", &headers);
+ tt_want(validate_header(&headers, "q", "test this") == 0);
+ tt_int_op(r, ==, 0);
+ evhttp_clear_headers(&headers);
+
+ r = evhttp_parse_query("http://www.test.com/?q=test&q2=foo", &headers);
+ tt_int_op(r, ==, 0);
+ tt_want(validate_header(&headers, "q", "test") == 0);
+ tt_want(validate_header(&headers, "q2", "foo") == 0);
+ evhttp_clear_headers(&headers);
+
+ r = evhttp_parse_query("http://www.test.com/?q&q2=foo", &headers);
+ tt_int_op(r, ==, -1);
+ evhttp_clear_headers(&headers);
+
+ r = evhttp_parse_query("http://www.test.com/?q=foo&q2", &headers);
+ tt_int_op(r, ==, -1);
+ evhttp_clear_headers(&headers);
+
+ r = evhttp_parse_query("http://www.test.com/?q=foo&q2&q3=x", &headers);
+ tt_int_op(r, ==, -1);
+ evhttp_clear_headers(&headers);
+
+ r = evhttp_parse_query("http://www.test.com/?q=&q2=&q3=", &headers);
+ tt_int_op(r, ==, 0);
+ tt_want(validate_header(&headers, "q", "") == 0);
+ tt_want(validate_header(&headers, "q2", "") == 0);
+ tt_want(validate_header(&headers, "q3", "") == 0);
+ evhttp_clear_headers(&headers);
+
+end:
evhttp_clear_headers(&headers);
}