summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels Provos <provos@gmail.com>2007-08-16 02:20:35 +0000
committerNiels Provos <provos@gmail.com>2007-08-16 02:20:35 +0000
commit8bdc2b664b6071d131f4a0c7f24f165edc34c776 (patch)
treea7e9ee988076a080d3bd1faa5fe25aba68611e26
parentf4efca0942d3b06b64664aea60403cb380451427 (diff)
downloadlibevent-8bdc2b664b6071d131f4a0c7f24f165edc34c776.tar.gz
[port to 1.3 branch] request dispatching fix from Elliot Foster
svn:r391
-rw-r--r--http.c16
-rw-r--r--test/regress_http.c100
2 files changed, 111 insertions, 5 deletions
diff --git a/http.c b/http.c
index d9d4cdd4..3a1ab511 100644
--- a/http.c
+++ b/http.c
@@ -1808,16 +1808,22 @@ static struct evhttp_cb *
evhttp_dispatch_callback(struct httpcbq *callbacks, struct evhttp_request *req)
{
struct evhttp_cb *cb;
+ size_t offset = 0;
/* Test for different URLs */
char *p = strchr(req->uri, '?');
+ if (p != NULL)
+ offset = (size_t)(p - req->uri);
+
TAILQ_FOREACH(cb, callbacks, next) {
- int res;
- if (p == NULL)
+ int res = 0;
+ if (p == NULL) {
res = strcmp(cb->what, req->uri) == 0;
- else
- res = strncmp(cb->what, req->uri,
- (size_t)(p - req->uri)) == 0;
+ } else {
+ res = ((strncmp(cb->what, req->uri, offset) == 0) &&
+ (cb->what[offset] == '\0'));
+ }
+
if (res)
return (cb);
}
diff --git a/test/regress_http.c b/test/regress_http.c
index a7956a04..40bae84e 100644
--- a/test/regress_http.c
+++ b/test/regress_http.c
@@ -64,6 +64,7 @@ static struct evhttp *http;
void http_basic_cb(struct evhttp_request *req, void *arg);
void http_post_cb(struct evhttp_request *req, void *arg);
+void http_dispatcher_cb(struct evhttp_request *req, void *arg);
struct evhttp *
http_setup(short *pport)
@@ -87,6 +88,7 @@ http_setup(short *pport)
/* Register a callback for certain types of requests */
evhttp_set_cb(myhttp, "/test", http_basic_cb, NULL);
evhttp_set_cb(myhttp, "/postit", http_post_cb, NULL);
+ evhttp_set_cb(myhttp, "/", http_dispatcher_cb, NULL);
*pport = port;
return (myhttp);
@@ -321,6 +323,103 @@ http_request_done(struct evhttp_request *req, void *arg)
}
/*
+ * HTTP DISPATCHER test
+ */
+
+void
+http_dispatcher_cb(struct evhttp_request *req, void *arg)
+{
+
+ struct evbuffer *evb = evbuffer_new();
+ event_debug(("%s: called\n", __func__));
+ evbuffer_add_printf(evb, "DISPATCHER_TEST");
+
+ evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
+
+ evbuffer_free(evb);
+}
+
+void
+http_dispatcher_test_done(struct evhttp_request *req, void *arg)
+{
+ const char *what = "DISPATCHER_TEST";
+
+ if (req->response_code != HTTP_OK) {
+ fprintf(stderr, "FAILED\n");
+ exit(1);
+ }
+
+ if (evhttp_find_header(req->input_headers, "Content-Type") == NULL) {
+ fprintf(stderr, "FAILED (content type)\n");
+ exit(1);
+ }
+
+ if (EVBUFFER_LENGTH(req->input_buffer) != strlen(what)) {
+ fprintf(stderr, "FAILED (length %zu vs %zu)\n",
+ EVBUFFER_LENGTH(req->input_buffer), strlen(what));
+ exit(1);
+ }
+
+ if (memcmp(EVBUFFER_DATA(req->input_buffer), what, strlen(what)) != 0) {
+ fprintf(stderr, "FAILED (data)\n");
+ exit(1);
+ }
+
+ test_ok = 1;
+ event_loopexit(NULL);
+}
+
+void
+http_dispatcher_test(void)
+{
+ short port = -1;
+ struct evhttp_connection *evcon = NULL;
+ struct evhttp_request *req = NULL;
+
+ test_ok = 0;
+ fprintf(stdout, "Testing HTTP Dispatcher: ");
+
+ http = http_setup(&port);
+
+ evcon = evhttp_connection_new("127.0.0.1", port);
+ if (evcon == NULL) {
+ fprintf(stdout, "FAILED\n");
+ exit(1);
+ }
+
+ /*
+ * At this point, we want to schedule an HTTP GET request
+ * server using our make request method.
+ */
+
+ req = evhttp_request_new(http_dispatcher_test_done, NULL);
+ if (req == NULL) {
+ fprintf(stdout, "FAILED\n");
+ exit(1);
+ }
+
+ /* Add the information that we care about */
+ evhttp_add_header(req->output_headers, "Host", "somehost");
+
+ if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/?arg=val") == -1) {
+ fprintf(stdout, "FAILED\n");
+ exit(1);
+ }
+
+ event_dispatch();
+
+ evhttp_connection_free(evcon);
+ evhttp_free(http);
+
+ if (test_ok != 1) {
+ fprintf(stdout, "FAILED: %d\n", test_ok);
+ exit(1);
+ }
+
+ fprintf(stdout, "OK\n");
+}
+
+/*
* HTTP POST test.
*/
@@ -629,4 +728,5 @@ http_suite(void)
http_post_test();
http_failure_test();
http_highport_test();
+ http_dispatcher_test();
}