From: Niels Provos Date: Sat, 9 Dec 2006 01:33:03 +0000 (+0000) Subject: decode uri when sending a request; from dug song X-Git-Tag: release-2.0.1-alpha~676 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2225eec22b1e62c6b24113e7e8915de3ef26a209;p=libevent decode uri when sending a request; from dug song svn:r296 --- diff --git a/evhttp.h b/evhttp.h index b1796d04..c26fbb8d 100644 --- a/evhttp.h +++ b/evhttp.h @@ -175,6 +175,7 @@ int evhttp_add_header(struct evkeyvalq *, const char *, const char *); void evhttp_clear_headers(struct evkeyvalq *); /* Miscellaneous utility functions */ +char *evhttp_decode_uri(const char *path); void evhttp_parse_query(const char *uri, struct evkeyvalq *); char *evhttp_htmlescape(const char *html); #ifdef __cplusplus diff --git a/http.c b/http.c index 032ff8de..3ef22641 100644 --- a/http.c +++ b/http.c @@ -55,6 +55,7 @@ #endif #include +#include #include #include #include @@ -888,8 +889,8 @@ evhttp_parse_request_line(struct evhttp_request *req, char *line) return (-1); } - if ((req->uri = strdup(uri)) == NULL) { - event_warn("%s: strdup", __func__); + if ((req->uri = evhttp_decode_uri(uri)) == NULL) { + event_warn("%s: evhttp_decode_uri", __func__); return (-1); } @@ -1372,8 +1373,9 @@ evhttp_send(struct evhttp_request *req, struct evbuffer *databuf) assert(TAILQ_FIRST(&evcon->requests) == req); /* xxx: not sure if we really should expose the data buffer this way */ - evbuffer_add_buffer(req->output_buffer, databuf); - + if (databuf != NULL) + evbuffer_add_buffer(req->output_buffer, databuf); + /* Adds headers to the response */ evhttp_make_header(evcon, req); @@ -1417,9 +1419,39 @@ evhttp_send_page(struct evhttp_request *req, struct evbuffer *databuf) evhttp_send(req, databuf); } +char * +evhttp_decode_uri(const char *path) +{ + char c, *ret; + int i, j, in_query = 0; + + ret = malloc(strlen(path) + 1); + if (ret == NULL) + event_err(1, "%s: malloc(%d)", __func__, strlen(path) + 1); + + for (i = j = 0; path[i] != '\0'; i++) { + c = path[i]; + if (c == '?') { + in_query = 1; + } else if (c == '+' && in_query) { + c = ' '; + } else if (c == '%' && isxdigit(path[i+1]) && + isxdigit(path[i+2])) { + char tmp[] = { path[i+1], path[i+2], '\0' }; + c = (char)strtol(tmp, NULL, 16); + i += 2; + } + ret[j++] = c; + } + ret[j] = '\0'; + + return (ret); +} + /* * Helper function to parse out arguments in a query. * The arguments are separated by key and value. + * URI should already be decoded. */ void