static void evhttp_write_cb(struct bufferevent *, void *);
static void evhttp_error_cb(struct bufferevent *bufev, short what, void *arg);
static int evhttp_decode_uri_internal(const char *uri, size_t length,
- char *ret);
+ char *ret, int optional_decode_plus);
#ifndef _EVENT_HAVE_STRSEP
/* strsep replacement for platforms that lack it. Only works if
return (p);
}
+/*
+ * @param optional_decode_plus: when true we transform plus to space only
+ * if we have seen a ?.
+ */
static int
-evhttp_decode_uri_internal(const char *uri, size_t length, char *ret)
+evhttp_decode_uri_internal(
+ const char *uri, size_t length, char *ret, int optional_decode_plus)
{
char c;
- int i, j, in_query = 0;
+ int i, j, in_query = !optional_decode_plus;
for (i = j = 0; i < length; i++) {
c = uri[i];
event_err(1, "%s: malloc(%lu)", __func__,
(unsigned long)(strlen(uri) + 1));
- evhttp_decode_uri_internal(uri, strlen(uri), ret);
+ evhttp_decode_uri_internal(uri, strlen(uri),
+ ret, 0 /*optional_decode_plus*/);
return (ret);
}
if ((translated = mm_malloc(offset + 1)) == NULL)
return (NULL);
- offset = evhttp_decode_uri_internal(req->uri, offset, translated);
+ offset = evhttp_decode_uri_internal(req->uri, offset,
+ translated, 1 /* optional_decode_plus */);
TAILQ_FOREACH(cb, callbacks, next) {
int res = 0;
evhttp_clear_headers(&headers);
}
+static int validate_header(
+ const struct evkeyvalq* headers,
+ const char *key, const char *value)
+{
+ const char *real_val = evhttp_find_header(headers, key);
+ tt_assert(real_val != NULL);
+ tt_want(strcmp(real_val, value) == 0);
+end:
+ return (0);
+}
+
+static void
+http_parse_query_test(void *ptr)
+{
+ struct evkeyvalq headers;
+
+ TAILQ_INIT(&headers);
+
+ evhttp_parse_query("http://www.test.com/?q=test", &headers);
+ tt_want(validate_header(&headers, "q", "test") == 0);
+ evhttp_clear_headers(&headers);
+
+ 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);
+ evhttp_clear_headers(&headers);
+
+ evhttp_parse_query("http://www.test.com/?q=test+foo", &headers);
+ tt_want(validate_header(&headers, "q", "test foo") == 0);
+ evhttp_clear_headers(&headers);
+}
+
static void
http_base_test(void)
{
{ "primitives", http_primitives, 0, NULL, NULL },
HTTP_LEGACY(base),
{ "bad_headers", http_bad_header_test, 0, NULL, NULL },
+ { "parse_query", http_parse_query_test, 0, NULL, NULL },
HTTP_LEGACY(basic),
HTTP_LEGACY(cancel),
HTTP_LEGACY(virtual_host),