return NULL;
/* Register a callback for certain types of requests */
- evhttp_set_cb(myhttp, "/test", http_basic_cb, NULL);
- evhttp_set_cb(myhttp, "/chunked", http_chunked_cb, NULL);
- evhttp_set_cb(myhttp, "/streamed", http_chunked_cb, NULL);
- evhttp_set_cb(myhttp, "/postit", http_post_cb, NULL);
- evhttp_set_cb(myhttp, "/putit", http_put_cb, NULL);
- evhttp_set_cb(myhttp, "/deleteit", http_delete_cb, NULL);
- evhttp_set_cb(myhttp, "/delay", http_delay_cb, NULL);
- evhttp_set_cb(myhttp, "/largedelay", http_large_delay_cb, NULL);
- evhttp_set_cb(myhttp, "/badrequest", http_badreq_cb, NULL);
- evhttp_set_cb(myhttp, "/", http_dispatcher_cb, NULL);
+ evhttp_set_cb(myhttp, "/test", http_basic_cb, base);
+ evhttp_set_cb(myhttp, "/chunked", http_chunked_cb, base);
+ evhttp_set_cb(myhttp, "/streamed", http_chunked_cb, base);
+ evhttp_set_cb(myhttp, "/postit", http_post_cb, base);
+ evhttp_set_cb(myhttp, "/putit", http_put_cb, base);
+ evhttp_set_cb(myhttp, "/deleteit", http_delete_cb, base);
+ evhttp_set_cb(myhttp, "/delay", http_delay_cb, base);
+ evhttp_set_cb(myhttp, "/largedelay", http_large_delay_cb, base);
+ evhttp_set_cb(myhttp, "/badrequest", http_badreq_cb, base);
+ evhttp_set_cb(myhttp, "/", http_dispatcher_cb, base);
return (myhttp);
}
http_readcb(struct bufferevent *bev, void *arg)
{
const char *what = BASIC_REQUEST_BODY;
+ struct event_base *my_base = arg;
if (evbuffer_contains(bufferevent_get_input(bev), what)) {
struct evhttp_request *req = evhttp_request_new(NULL, NULL);
bufferevent_disable(bev, EV_READ);
if (base)
event_base_loopexit(base, NULL);
+ else if (my_base)
+ event_base_loopexit(my_base, NULL);
else
event_loopexit(NULL);
}
/* but trickle it across several iterations to ensure we're not
* assuming it comes all at once */
- event_base_once(NULL, -1, EV_TIMEOUT, http_chunked_trickle_cb, state, &when);
+ event_base_once(arg, -1, EV_TIMEOUT, http_chunked_trickle_cb, state, &when);
}
static void
}
static void
-http_basic_test(void)
+http_basic_test(void *arg)
{
+ struct basic_test_data *data = arg;
struct timeval tv;
struct bufferevent *bev;
evutil_socket_t fd;
test_ok = 0;
- http = http_setup(&port, NULL);
+ http = http_setup(&port, data->base);
/* bind to a second socket */
if (http_bind(http, &port2) == -1) {
fd = http_connect("127.0.0.1", port);
/* Stupid thing to send a request */
- bev = bufferevent_socket_new(NULL, fd, 0);
+ bev = bufferevent_socket_new(data->base, fd, 0);
bufferevent_setcb(bev, http_readcb, http_writecb,
- http_errorcb, NULL);
+ http_errorcb, data->base);
/* first half of the http request */
http_request =
bufferevent_write(bev, http_request, strlen(http_request));
evutil_timerclear(&tv);
tv.tv_usec = 10000;
- event_base_once(NULL, -1, EV_TIMEOUT, http_complete_write, bev, &tv);
+ event_base_once(data->base,
+ -1, EV_TIMEOUT, http_complete_write, bev, &tv);
- event_dispatch();
+ event_base_dispatch(data->base);
tt_assert(test_ok == 3);
fd = http_connect("127.0.0.1", port2);
/* Stupid thing to send a request */
- bev = bufferevent_socket_new(NULL, fd, 0);
+ bev = bufferevent_socket_new(data->base, fd, 0);
bufferevent_setcb(bev, http_readcb, http_writecb,
- http_errorcb, NULL);
+ http_errorcb, data->base);
http_request =
"GET /test HTTP/1.1\r\n"
bufferevent_write(bev, http_request, strlen(http_request));
- event_dispatch();
+ event_base_dispatch(data->base);
bufferevent_free(bev);
evutil_closesocket(fd);
tv.tv_sec = 0;
tv.tv_usec = 200 * 1000;
- event_base_once(NULL, -1, EV_TIMEOUT, http_delay_reply, req, &tv);
+ event_base_once(arg, -1, EV_TIMEOUT, http_delay_reply, req, &tv);
}
static void
evutil_timerclear(&tv);
tv.tv_sec = 3;
- event_base_once(NULL, -1, EV_TIMEOUT, http_delay_reply, req, &tv);
+ event_base_once(arg, -1, EV_TIMEOUT, http_delay_reply, req, &tv);
evhttp_connection_fail(delayed_client, EVCON_HTTP_EOF);
}
}
static void
-http_delete_test(void)
+http_delete_test(void *arg)
{
+ struct basic_test_data *data = arg;
struct bufferevent *bev;
evutil_socket_t fd;
const char *http_request;
test_ok = 0;
- http = http_setup(&port, NULL);
+ http = http_setup(&port, data->base);
fd = http_connect("127.0.0.1", port);
/* Stupid thing to send a request */
- bev = bufferevent_socket_new(NULL, fd, 0);
+ bev = bufferevent_socket_new(data->base, fd, 0);
bufferevent_setcb(bev, http_readcb, http_writecb,
- http_errorcb, NULL);
+ http_errorcb, data->base);
http_request =
"DELETE /deleteit HTTP/1.1\r\n"
bufferevent_write(bev, http_request, strlen(http_request));
- event_dispatch();
+ event_base_dispatch(data->base);
bufferevent_free(bev);
evutil_closesocket(fd);
static void http_request_empty_done(struct evhttp_request *, void *);
static void
-_http_connection_test(int persistent)
+_http_connection_test(struct basic_test_data *data, int persistent)
{
ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
test_ok = 0;
- http = http_setup(&port, NULL);
+ http = http_setup(&port, data->base);
- evcon = evhttp_connection_new("127.0.0.1", port);
+ evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon);
- tt_assert(evhttp_connection_get_base(evcon) == NULL);
+ tt_assert(evhttp_connection_get_base(evcon) == data->base);
+ base = data->base;
/*
* At this point, we want to schedule a request to the HTTP
* server using our make request method.
exit(1);
}
- event_dispatch();
+ event_base_dispatch(data->base);
tt_assert(test_ok);
tt_abort_msg("couldn't make request");
}
- event_dispatch();
+ event_base_dispatch(data->base);
/* make another request: request empty reply */
test_ok = 0;
- req = evhttp_request_new(http_request_empty_done, NULL);
+ req = evhttp_request_new(http_request_empty_done, data->base);
/* Add the information that we care about */
evhttp_add_header(req->output_headers, "Empty", "itis");
exit(1);
}
- event_dispatch();
+ event_base_dispatch(data->base);
end:
if (evcon)
}
static void
-http_connection_test(void)
+http_connection_test(void *arg)
{
- _http_connection_test(0);
+ _http_connection_test(arg, 0);
}
static void
-http_persist_connection_test(void)
+http_persist_connection_test(void *arg)
{
- _http_connection_test(1);
+ _http_connection_test(arg, 1);
}
static struct regress_dns_server_table search_table[] = {
};
static void
-http_connection_async_test(void)
+http_connection_async_test(void *arg)
{
+ struct basic_test_data *data = arg;
ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
ev_uint16_t portnum = 0;
char address[64];
- tt_assert(regress_dnsserver(base, &portnum, search_table));
+ base = data->base;
+ tt_assert(regress_dnsserver(data->base, &portnum, search_table));
- dns_base = evdns_base_new(base, 0/* init name servers */);
+ dns_base = evdns_base_new(data->base, 0/* init name servers */);
tt_assert(dns_base);
/* Add ourself as the only nameserver, and make sure we really are
test_ok = 0;
- http = http_setup(&port, NULL);
+ http = http_setup(&port, data->base);
- evcon = evhttp_connection_base_new(base, dns_base, "127.0.0.1", port);
+ evcon = evhttp_connection_base_new(data->base, dns_base, "127.0.0.1", port);
tt_assert(evcon);
/*
exit(1);
}
- event_dispatch();
+ event_base_dispatch(data->base);
tt_assert(test_ok);
tt_abort_msg("couldn't make request");
}
- event_dispatch();
+ event_base_dispatch(data->base);
/* make another request: request empty reply */
test_ok = 0;
- req = evhttp_request_new(http_request_empty_done, NULL);
+ req = evhttp_request_new(http_request_empty_done, data->base);
/* Add the information that we care about */
evhttp_add_header(req->output_headers, "Empty", "itis");
exit(1);
}
- event_dispatch();
+ event_base_dispatch(data->base);
end:
if (evcon)
{
struct evhttp_request *req = arg;
struct timeval tv;
+ struct event_base *base;
evutil_timerclear(&tv);
tv.tv_sec = 0;
tv.tv_usec = 500 * 1000;
+ base = evhttp_connection_get_base(evhttp_request_get_connection(req));
evhttp_cancel_request(req);
- event_loopexit(&tv);
+ event_base_loopexit(base, &tv);
++test_ok;
}
static void
-http_cancel_test(void)
+http_cancel_test(void *arg)
{
+ struct basic_test_data *data = arg;
ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
struct timeval tv;
+ base = data->base;
+
test_ok = 0;
- http = http_setup(&port, NULL);
+ http = http_setup(&port, data->base);
- evcon = evhttp_connection_new("127.0.0.1", port);
+ evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon);
/*
tv.tv_sec = 0;
tv.tv_usec = 100 * 1000;
- event_base_once(NULL, -1, EV_TIMEOUT, http_do_cancel, req, &tv);
+ event_base_once(data->base, -1, EV_TIMEOUT, http_do_cancel, req, &tv);
- event_dispatch();
+ event_base_dispatch(data->base);
tt_int_op(test_ok, ==, 2);
tt_int_op(evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test"),
!=, -1);
- event_dispatch();
+ event_base_dispatch(data->base);
/* make another request: request empty reply */
test_ok = 0;
- req = evhttp_request_new(http_request_empty_done, NULL);
+ req = evhttp_request_new(http_request_empty_done, data->base);
/* Add the information that we care about */
evhttp_add_header(req->output_headers, "Empty", "itis");
tt_int_op(evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test"),
!=, -1);
- event_dispatch();
+ event_base_dispatch(data->base);
end:
if (evcon)
}
test_ok = 1;
- event_loopexit(NULL);
+ if (base)
+ event_base_loopexit(base, NULL);
+ else
+ event_loopexit(NULL);
}
static void
}
test_ok = 1;
- event_loopexit(NULL);
+ if (arg)
+ event_base_loopexit(arg, NULL);
+ else
+ event_loopexit(NULL);
}
/* test virtual hosts */
static void
-http_virtual_host_test(void)
+http_virtual_host_test(void *arg)
{
+ struct basic_test_data *data = arg;
ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
struct evhttp *second = NULL, *third = NULL;
- http = http_setup(&port, NULL);
+ base = data->base;
+
+ http = http_setup(&port, data->base);
/* virtual host */
second = evhttp_new(NULL);
tt_abort_msg("Couldn't add wildcarded vhost");
}
- evcon = evhttp_connection_new("127.0.0.1", port);
+ evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon);
/* make a request with a different host and expect an error */
- req = evhttp_request_new(http_request_expect_error, NULL);
+ req = evhttp_request_new(http_request_expect_error, data->base);
/* Add the information that we care about */
evhttp_add_header(req->output_headers, "Host", "somehost");
tt_abort_msg("Couldn't make request");
}
- event_dispatch();
+ event_base_dispatch(data->base);
tt_assert(test_ok == 1);
exit(1);
}
- event_dispatch();
+ event_base_dispatch(data->base);
tt_assert(test_ok == 1);
tt_abort_msg("Couldn't make request");
}
- event_dispatch();
+ event_base_dispatch(data->base);
tt_assert(test_ok == 1)
}
test_ok = 1;
- event_loopexit(NULL);
+ if (arg)
+ event_base_loopexit(arg, NULL);
+ else
+ event_loopexit(NULL);
}
/*
#define POST_DATA "Okay. Not really printf"
static void
-http_post_test(void)
+http_post_test(void *arg)
{
+ struct basic_test_data *data = arg;
ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
test_ok = 0;
- http = http_setup(&port, NULL);
+ http = http_setup(&port, data->base);
- evcon = evhttp_connection_new("127.0.0.1", port);
+ evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon);
/*
* server using our make request method.
*/
- req = evhttp_request_new(http_postrequest_done, NULL);
+ req = evhttp_request_new(http_postrequest_done, data->base);
tt_assert(req);
/* Add the information that we care about */
tt_abort_msg("Couldn't make request");
}
- event_dispatch();
+ event_base_dispatch(data->base);
evhttp_connection_free(evcon);
evhttp_free(http);
http_postrequest_done(struct evhttp_request *req, void *arg)
{
const char *what = BASIC_REQUEST_BODY;
+ struct event_base *base = arg;
if (req == NULL) {
fprintf(stderr, "FAILED (timeout)\n");
}
test_ok = 1;
- event_loopexit(NULL);
+ event_base_loopexit(base, NULL);
}
/*
#define PUT_DATA "Hi, I'm some PUT data"
static void
-http_put_test(void)
+http_put_test(void *arg)
{
+ struct basic_test_data *data = arg;
ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
test_ok = 0;
- http = http_setup(&port, NULL);
+ http = http_setup(&port, data->base);
- evcon = evhttp_connection_new("127.0.0.1", port);
+ evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon);
/*
* Schedule the HTTP PUT request
*/
- req = evhttp_request_new(http_putrequest_done, NULL);
+ req = evhttp_request_new(http_putrequest_done, data->base);
tt_assert(req);
/* Add the information that we care about */
tt_abort_msg("Couldn't make request");
}
- event_dispatch();
+ event_base_dispatch(data->base);
evhttp_connection_free(evcon);
evhttp_free(http);
void
http_putrequest_done(struct evhttp_request *req, void *arg)
{
+ struct event_base *base = arg;
const char *what = "That ain't funny";
if (req == NULL) {
}
test_ok = 1;
- event_loopexit(NULL);
+ event_base_loopexit(base, NULL);
}
static void
if (evbuffer_contains(bufferevent_get_input(bev), what)) {
test_ok = 2;
bufferevent_disable(bev, EV_READ);
- event_loopexit(NULL);
+ event_base_loopexit(arg, NULL);
}
}
* Testing that the HTTP server can deal with a malformed request.
*/
static void
-http_failure_test(void)
+http_failure_test(void *arg)
{
+ struct basic_test_data *data = arg;
struct bufferevent *bev;
evutil_socket_t fd;
const char *http_request;
test_ok = 0;
- http = http_setup(&port, NULL);
+ http = http_setup(&port, data->base);
fd = http_connect("127.0.0.1", port);
/* Stupid thing to send a request */
- bev = bufferevent_socket_new(NULL, fd, 0);
+ bev = bufferevent_socket_new(data->base, fd, 0);
bufferevent_setcb(bev, http_failure_readcb, http_writecb,
- http_errorcb, NULL);
+ http_errorcb, data->base);
http_request = "illegal request\r\n";
bufferevent_write(bev, http_request, strlen(http_request));
- event_dispatch();
+ event_base_dispatch(data->base);
bufferevent_free(bev);
evutil_closesocket(fd);
end:
evutil_timerclear(&tv);
tv.tv_sec = 3;
- event_loopexit(&tv);
+ event_base_loopexit(arg, &tv);
}
static void
close_detect_launch(evutil_socket_t fd, short what, void *arg)
{
struct evhttp_connection *evcon = arg;
+ struct event_base *base = evhttp_connection_get_base(evcon);
struct evhttp_request *req;
- req = evhttp_request_new(close_detect_done, NULL);
+ req = evhttp_request_new(close_detect_done, base);
/* Add the information that we care about */
evhttp_add_header(req->output_headers, "Host", "somehost");
close_detect_cb(struct evhttp_request *req, void *arg)
{
struct evhttp_connection *evcon = arg;
+ struct event_base *base = evhttp_connection_get_base(evcon);
struct timeval tv;
if (req != NULL && req->response_code != HTTP_OK) {
tv.tv_sec = 3; /* longer than the http time out */
/* launch a new request on the persistent connection in 3 seconds */
- event_base_once(NULL, -1, EV_TIMEOUT, close_detect_launch, evcon, &tv);
+ event_base_once(base, -1, EV_TIMEOUT, close_detect_launch, evcon, &tv);
end:
;
}
static void
-_http_close_detection(int with_delay)
+_http_close_detection(struct basic_test_data *data, int with_delay)
{
ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
test_ok = 0;
- http = http_setup(&port, NULL);
+ http = http_setup(&port, data->base);
/* 2 second timeout */
evhttp_set_timeout(http, 1);
- evcon = evhttp_connection_new("127.0.0.1", port);
+ evcon = evhttp_connection_base_new(data->base, NULL,
+ "127.0.0.1", port);
tt_assert(evcon);
delayed_client = evcon;
exit(1);
}
- event_dispatch();
+ event_base_dispatch(data->base);
/* at this point, the http server should have no connection */
tt_assert(TAILQ_FIRST(&http->connections) == NULL);
evhttp_free(http);
}
static void
-http_close_detection_test(void)
+http_close_detection_test(void *arg)
{
- _http_close_detection(0);
+ _http_close_detection(arg, 0);
}
static void
-http_close_detection_delay_test(void)
+http_close_detection_delay_test(void *arg)
{
- _http_close_detection(1);
+ _http_close_detection(arg, 1);
}
static void
http_incomplete_readcb(struct bufferevent *bev, void *arg)
{
test_ok = -1;
- event_loopexit(NULL);
+ event_base_loopexit(base,NULL);
}
static void
test_ok++;
else
test_ok = -2;
- event_loopexit(NULL);
+ event_base_loopexit(base,NULL);
}
static void
}
static void
-_http_incomplete_test(int use_timeout)
+_http_incomplete_test(struct basic_test_data *data, int use_timeout)
{
struct bufferevent *bev;
evutil_socket_t fd;
ev_uint16_t port = 0;
struct timeval tv_start, tv_end;
+ base = data->base;
+
test_ok = 0;
- http = http_setup(&port, NULL);
+ http = http_setup(&port, data->base);
evhttp_set_timeout(http, 1);
fd = http_connect("127.0.0.1", port);
/* Stupid thing to send a request */
- bev = bufferevent_socket_new(NULL, fd, 0);
+ bev = bufferevent_socket_new(data->base, fd, 0);
bufferevent_setcb(bev,
http_incomplete_readcb, http_incomplete_writecb,
http_incomplete_errorcb, use_timeout ? NULL : &fd);
evutil_gettimeofday(&tv_start, NULL);
- event_dispatch();
+ event_base_dispatch(data->base);
evutil_gettimeofday(&tv_end, NULL);
evutil_timersub(&tv_end, &tv_start, &tv_end);
;
}
static void
-http_incomplete_test(void)
+http_incomplete_test(void *arg)
{
- _http_incomplete_test(0);
+ _http_incomplete_test(arg, 0);
}
static void
-http_incomplete_timeout_test(void)
+http_incomplete_timeout_test(void *arg)
{
- _http_incomplete_test(1);
+ _http_incomplete_test(arg, 1);
}
/*
{ #name, run_legacy_test_fn, TT_ISOLATED|TT_LEGACY, &legacy_setup, \
http_##name##_test }
+#define HTTP(name) \
+ { #name, http_##name##_test, TT_ISOLATED, &basic_setup, NULL }
+
struct testcase_t http_testcases[] = {
{ "primitives", http_primitives, 0, NULL, NULL },
{ "base", http_base_test, TT_FORK|TT_NEED_BASE, NULL, NULL },
{ "parse_query", http_parse_query_test, 0, NULL, NULL },
{ "parse_uri", http_parse_uri_test, 0, NULL, NULL },
{ "uriencode", http_uriencode_test, 0, NULL, NULL },
- HTTP_LEGACY(basic),
- HTTP_LEGACY(cancel),
- HTTP_LEGACY(virtual_host),
- HTTP_LEGACY(post),
- HTTP_LEGACY(put),
- HTTP_LEGACY(delete),
- HTTP_LEGACY(failure),
- HTTP_LEGACY(connection),
- HTTP_LEGACY(persist_connection),
- HTTP_LEGACY(connection_async),
- HTTP_LEGACY(close_detection),
- HTTP_LEGACY(close_detection_delay),
+ HTTP(basic),
+ HTTP(cancel),
+ HTTP(virtual_host),
+ HTTP(post),
+ HTTP(put),
+ HTTP(delete),
+ HTTP(failure),
+ HTTP(connection),
+ HTTP(persist_connection),
+ HTTP(connection_async),
+ HTTP(close_detection),
+ HTTP(close_detection_delay),
HTTP_LEGACY(bad_request),
- HTTP_LEGACY(incomplete),
- HTTP_LEGACY(incomplete_timeout),
+ HTTP(incomplete),
+ HTTP(incomplete_timeout),
{ "terminate_chunked", run_legacy_test_fn,
TT_ISOLATED|TT_LEGACY, &legacy_setup,
http_terminate_chunked_test },