dispatch the callbacks against the decoded uri to be more http compliant

svn:r806
This commit is contained in:
Niels Provos 2008-05-10 07:32:05 +00:00
parent 7bbe185b0e
commit 687be1241b

52
http.c
View File

@ -168,6 +168,8 @@ static void evhttp_read_cb(struct bufferevent *, void *);
static void evhttp_read_header_cb(struct bufferevent *bufev, void *arg); static void evhttp_read_header_cb(struct bufferevent *bufev, void *arg);
static void evhttp_write_cb(struct bufferevent *, void *); static void evhttp_write_cb(struct bufferevent *, void *);
static void evhttp_error_cb(struct bufferevent *bufev, short what, void *arg); 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);
#ifndef HAVE_STRSEP #ifndef HAVE_STRSEP
/* strsep replacement for platforms that lack it. Only works if /* strsep replacement for platforms that lack it. Only works if
@ -1799,18 +1801,13 @@ evhttp_encode_uri(const char *uri)
return (p); return (p);
} }
char * static int
evhttp_decode_uri(const char *uri) evhttp_decode_uri_internal(const char *uri, size_t length, char *ret)
{ {
char c, *ret; char c;
int i, j, in_query = 0; int i, j, in_query = 0;
ret = mm_malloc(strlen(uri) + 1); for (i = j = 0; i < length; i++) {
if (ret == NULL)
event_err(1, "%s: malloc(%lu)", __func__,
(unsigned long)(strlen(uri) + 1));
for (i = j = 0; uri[i] != '\0'; i++) {
c = uri[i]; c = uri[i];
if (c == '?') { if (c == '?') {
in_query = 1; in_query = 1;
@ -1825,6 +1822,20 @@ evhttp_decode_uri(const char *uri)
ret[j++] = c; ret[j++] = c;
} }
ret[j] = '\0'; ret[j] = '\0';
return (j);
}
char *
evhttp_decode_uri(const char *uri)
{
char *ret;
if ((ret = mm_malloc(strlen(uri) + 1)) == NULL)
event_err(1, "%s: malloc(%lu)", __func__,
(unsigned long)(strlen(uri) + 1));
evhttp_decode_uri_internal(uri, strlen(uri), ret);
return (ret); return (ret);
} }
@ -1882,20 +1893,23 @@ evhttp_dispatch_callback(struct httpcbq *callbacks, struct evhttp_request *req)
{ {
struct evhttp_cb *cb; struct evhttp_cb *cb;
size_t offset = 0; size_t offset = 0;
char *translated;
/* Test for different URLs */ /* Test for different URLs */
char *p = strchr(req->uri, '?'); char *p = req->uri;
if (p != NULL) while (*p != '\0' && *p != '?')
offset = (size_t)(p - req->uri); ++p;
offset = (size_t)(p - req->uri);
/* allocate the rewritten version on the stack */
if ((translated = alloca(offset + 1)) == NULL)
return (NULL);
offset = evhttp_decode_uri_internal(req->uri, offset, translated);
TAILQ_FOREACH(cb, callbacks, next) { TAILQ_FOREACH(cb, callbacks, next) {
int res = 0; int res = 0;
if (p == NULL) { res = ((strncmp(cb->what, translated, offset) == 0) &&
res = strcmp(cb->what, req->uri) == 0; (cb->what[offset] == '\0'));
} else {
res = ((strncmp(cb->what, req->uri, offset) == 0) &&
(cb->what[offset] == '\0'));
}
if (res) if (res)
return (cb); return (cb);