2 * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2010 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "event2/event-config.h"
35 #include <sys/types.h>
37 #ifdef _EVENT_HAVE_SYS_TIME_H
40 #include <sys/queue.h>
42 #include <sys/socket.h>
53 #include "event2/dns.h"
55 #include "event2/event.h"
56 #include "event2/http.h"
57 #include "event2/buffer.h"
58 #include "event2/bufferevent.h"
59 #include "log-internal.h"
60 #include "util-internal.h"
61 #include "http-internal.h"
63 #include "regress_testutils.h"
65 static struct evhttp *http;
66 /* set if a test needs to call loopexit on a base */
67 static struct event_base *exit_base;
69 static char const BASIC_REQUEST_BODY[] = "This is funny";
71 static void http_basic_cb(struct evhttp_request *req, void *arg);
72 static void http_chunked_cb(struct evhttp_request *req, void *arg);
73 static void http_post_cb(struct evhttp_request *req, void *arg);
74 static void http_put_cb(struct evhttp_request *req, void *arg);
75 static void http_delete_cb(struct evhttp_request *req, void *arg);
76 static void http_delay_cb(struct evhttp_request *req, void *arg);
77 static void http_large_delay_cb(struct evhttp_request *req, void *arg);
78 static void http_badreq_cb(struct evhttp_request *req, void *arg);
79 static void http_dispatcher_cb(struct evhttp_request *req, void *arg);
81 http_bind(struct evhttp *myhttp, ev_uint16_t *pport)
84 struct evhttp_bound_socket *sock;
86 sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", *pport);
88 event_errx(1, "Could not start web server");
90 port = regress_get_socket_port(evhttp_bound_socket_get_fd(sock));
93 *pport = (ev_uint16_t) port;
98 static struct evhttp *
99 http_setup(ev_uint16_t *pport, struct event_base *base)
101 struct evhttp *myhttp;
103 /* Try a few different ports */
104 myhttp = evhttp_new(base);
106 if (http_bind(myhttp, pport) < 0)
109 /* Register a callback for certain types of requests */
110 evhttp_set_cb(myhttp, "/test", http_basic_cb, base);
111 evhttp_set_cb(myhttp, "/chunked", http_chunked_cb, base);
112 evhttp_set_cb(myhttp, "/streamed", http_chunked_cb, base);
113 evhttp_set_cb(myhttp, "/postit", http_post_cb, base);
114 evhttp_set_cb(myhttp, "/putit", http_put_cb, base);
115 evhttp_set_cb(myhttp, "/deleteit", http_delete_cb, base);
116 evhttp_set_cb(myhttp, "/delay", http_delay_cb, base);
117 evhttp_set_cb(myhttp, "/largedelay", http_large_delay_cb, base);
118 evhttp_set_cb(myhttp, "/badrequest", http_badreq_cb, base);
119 evhttp_set_cb(myhttp, "/", http_dispatcher_cb, base);
124 #define NI_MAXSERV 1024
127 static evutil_socket_t
128 http_connect(const char *address, u_short port)
130 /* Stupid code for connecting */
133 struct sockaddr_in sin;
135 struct addrinfo ai, *aitop;
136 char strport[NI_MAXSERV];
143 if (!(he = gethostbyname(address))) {
144 event_warn("gethostbyname");
146 memcpy(&sin.sin_addr, he->h_addr_list[0], he->h_length);
147 sin.sin_family = AF_INET;
148 sin.sin_port = htons(port);
149 slen = sizeof(struct sockaddr_in);
150 sa = (struct sockaddr*)&sin;
152 memset(&ai, 0, sizeof(ai));
153 ai.ai_family = AF_INET;
154 ai.ai_socktype = SOCK_STREAM;
155 evutil_snprintf(strport, sizeof(strport), "%d", port);
156 if (getaddrinfo(address, strport, &ai, &aitop) != 0) {
157 event_warn("getaddrinfo");
161 slen = aitop->ai_addrlen;
164 fd = socket(AF_INET, SOCK_STREAM, 0);
166 event_err(1, "socket failed");
168 evutil_make_socket_nonblocking(fd);
169 if (connect(fd, sa, slen) == -1) {
171 int tmp_err = WSAGetLastError();
172 if (tmp_err != WSAEINPROGRESS && tmp_err != WSAEINVAL &&
173 tmp_err != WSAEWOULDBLOCK)
174 event_err(1, "connect failed");
176 if (errno != EINPROGRESS)
177 event_err(1, "connect failed");
188 /* Helper: do a strcmp on the contents of buf and the string s. */
190 evbuffer_datacmp(struct evbuffer *buf, const char *s)
192 size_t b_sz = evbuffer_get_length(buf);
193 size_t s_sz = strlen(s);
200 d = evbuffer_pullup(buf, s_sz);
201 if ((r = memcmp(d, s, s_sz)))
210 /* Helper: Return true iff buf contains s */
212 evbuffer_contains(struct evbuffer *buf, const char *s)
214 struct evbuffer_ptr ptr;
215 ptr = evbuffer_search(buf, s, strlen(s), NULL);
216 return ptr.pos != -1;
220 http_readcb(struct bufferevent *bev, void *arg)
222 const char *what = BASIC_REQUEST_BODY;
223 struct event_base *my_base = arg;
225 if (evbuffer_contains(bufferevent_get_input(bev), what)) {
226 struct evhttp_request *req = evhttp_request_new(NULL, NULL);
227 enum message_read_status done;
229 /* req->kind = EVHTTP_RESPONSE; */
230 done = evhttp_parse_firstline(req, bufferevent_get_input(bev));
231 if (done != ALL_DATA_READ)
234 done = evhttp_parse_headers(req, bufferevent_get_input(bev));
235 if (done != ALL_DATA_READ)
239 evhttp_find_header(evhttp_request_get_input_headers(req),
240 "Content-Type") != NULL)
244 evhttp_request_free(req);
245 bufferevent_disable(bev, EV_READ);
247 event_base_loopexit(exit_base, NULL);
249 event_base_loopexit(my_base, NULL);
251 fprintf(stderr, "No way to exit loop!\n");
258 http_writecb(struct bufferevent *bev, void *arg)
260 if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
261 /* enable reading of the reply */
262 bufferevent_enable(bev, EV_READ);
268 http_errorcb(struct bufferevent *bev, short what, void *arg)
271 event_base_loopexit(arg, NULL);
275 http_basic_cb(struct evhttp_request *req, void *arg)
277 struct evbuffer *evb = evbuffer_new();
278 int empty = evhttp_find_header(evhttp_request_get_input_headers(req), "Empty") != NULL;
279 event_debug(("%s: called\n", __func__));
280 evbuffer_add_printf(evb, BASIC_REQUEST_BODY);
282 /* For multi-line headers test */
285 evhttp_find_header(evhttp_request_get_input_headers(req),"X-multi");
287 if (strcmp("END", multi + strlen(multi) - 3) == 0)
289 if (evhttp_find_header(evhttp_request_get_input_headers(req), "X-Last"))
294 /* injecting a bad content-length */
295 if (evhttp_find_header(evhttp_request_get_input_headers(req), "X-Negative"))
296 evhttp_add_header(evhttp_request_get_output_headers(req),
297 "Content-Length", "-100");
299 /* allow sending of an empty reply */
300 evhttp_send_reply(req, HTTP_OK, "Everything is fine",
301 !empty ? evb : NULL);
306 static char const* const CHUNKS[] = {
308 "but not hilarious.",
312 struct chunk_req_state {
313 struct event_base *base;
314 struct evhttp_request *req;
319 http_chunked_trickle_cb(evutil_socket_t fd, short events, void *arg)
321 struct evbuffer *evb = evbuffer_new();
322 struct chunk_req_state *state = arg;
323 struct timeval when = { 0, 0 };
325 evbuffer_add_printf(evb, "%s", CHUNKS[state->i]);
326 evhttp_send_reply_chunk(state->req, evb);
329 if (++state->i < (int) (sizeof(CHUNKS)/sizeof(CHUNKS[0]))) {
330 event_base_once(state->base, -1, EV_TIMEOUT,
331 http_chunked_trickle_cb, state, &when);
333 evhttp_send_reply_end(state->req);
339 http_chunked_cb(struct evhttp_request *req, void *arg)
341 struct timeval when = { 0, 0 };
342 struct chunk_req_state *state = malloc(sizeof(struct chunk_req_state));
343 event_debug(("%s: called\n", __func__));
345 memset(state, 0, sizeof(struct chunk_req_state));
349 if (strcmp(evhttp_request_get_uri(req), "/streamed") == 0) {
350 evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Length", "39");
353 /* generate a chunked/streamed reply */
354 evhttp_send_reply_start(req, HTTP_OK, "Everything is fine");
356 /* but trickle it across several iterations to ensure we're not
357 * assuming it comes all at once */
358 event_base_once(arg, -1, EV_TIMEOUT, http_chunked_trickle_cb, state, &when);
362 http_complete_write(evutil_socket_t fd, short what, void *arg)
364 struct bufferevent *bev = arg;
365 const char *http_request = "host\r\n"
366 "Connection: close\r\n"
368 bufferevent_write(bev, http_request, strlen(http_request));
372 http_basic_test(void *arg)
374 struct basic_test_data *data = arg;
376 struct bufferevent *bev;
378 const char *http_request;
379 ev_uint16_t port = 0, port2 = 0;
383 http = http_setup(&port, data->base);
385 /* bind to a second socket */
386 if (http_bind(http, &port2) == -1) {
387 fprintf(stdout, "FAILED (bind)\n");
391 fd = http_connect("127.0.0.1", port);
393 /* Stupid thing to send a request */
394 bev = bufferevent_socket_new(data->base, fd, 0);
395 bufferevent_setcb(bev, http_readcb, http_writecb,
396 http_errorcb, data->base);
398 /* first half of the http request */
400 "GET /test HTTP/1.1\r\n"
403 bufferevent_write(bev, http_request, strlen(http_request));
404 evutil_timerclear(&tv);
406 event_base_once(data->base,
407 -1, EV_TIMEOUT, http_complete_write, bev, &tv);
409 event_base_dispatch(data->base);
411 tt_assert(test_ok == 3);
413 /* connect to the second port */
414 bufferevent_free(bev);
415 evutil_closesocket(fd);
417 fd = http_connect("127.0.0.1", port2);
419 /* Stupid thing to send a request */
420 bev = bufferevent_socket_new(data->base, fd, 0);
421 bufferevent_setcb(bev, http_readcb, http_writecb,
422 http_errorcb, data->base);
425 "GET /test HTTP/1.1\r\n"
427 "Connection: close\r\n"
430 bufferevent_write(bev, http_request, strlen(http_request));
432 event_base_dispatch(data->base);
434 tt_assert(test_ok == 5);
436 /* Connect to the second port again. This time, send an absolute uri. */
437 bufferevent_free(bev);
438 evutil_closesocket(fd);
440 fd = http_connect("127.0.0.1", port2);
442 /* Stupid thing to send a request */
443 bev = bufferevent_socket_new(data->base, fd, 0);
444 bufferevent_setcb(bev, http_readcb, http_writecb,
445 http_errorcb, data->base);
448 "GET http://somehost.net/test HTTP/1.1\r\n"
450 "Connection: close\r\n"
453 bufferevent_write(bev, http_request, strlen(http_request));
455 event_base_dispatch(data->base);
457 tt_assert(test_ok == 7);
465 http_delay_reply(evutil_socket_t fd, short what, void *arg)
467 struct evhttp_request *req = arg;
469 evhttp_send_reply(req, HTTP_OK, "Everything is fine", NULL);
475 http_delay_cb(struct evhttp_request *req, void *arg)
478 evutil_timerclear(&tv);
480 tv.tv_usec = 200 * 1000;
482 event_base_once(arg, -1, EV_TIMEOUT, http_delay_reply, req, &tv);
486 http_badreq_cb(struct evhttp_request *req, void *arg)
488 struct evbuffer *buf = evbuffer_new();
490 evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", "text/xml; charset=UTF-8");
491 evbuffer_add_printf(buf, "Hello, %s!", "127.0.0.1");
493 evhttp_send_reply(req, HTTP_OK, "OK", buf);
498 http_badreq_errorcb(struct bufferevent *bev, short what, void *arg)
500 event_debug(("%s: called (what=%04x, arg=%p)", __func__, what, arg));
506 #define SHUT_WR SD_SEND
513 http_badreq_readcb(struct bufferevent *bev, void *arg)
515 const char *what = "Hello, 127.0.0.1";
516 const char *bad_request = "400 Bad Request";
518 if (evbuffer_contains(bufferevent_get_input(bev), bad_request)) {
519 TT_FAIL(("%s:bad request detected", __func__));
520 bufferevent_disable(bev, EV_READ);
521 event_base_loopexit(arg, NULL);
525 if (evbuffer_contains(bufferevent_get_input(bev), what)) {
526 struct evhttp_request *req = evhttp_request_new(NULL, NULL);
527 enum message_read_status done;
529 /* req->kind = EVHTTP_RESPONSE; */
530 done = evhttp_parse_firstline(req, bufferevent_get_input(bev));
531 if (done != ALL_DATA_READ)
534 done = evhttp_parse_headers(req, bufferevent_get_input(bev));
535 if (done != ALL_DATA_READ)
539 evhttp_find_header(evhttp_request_get_input_headers(req),
540 "Content-Type") != NULL)
544 evhttp_request_free(req);
545 evbuffer_drain(bufferevent_get_input(bev), evbuffer_get_length(bufferevent_get_input(bev)));
548 shutdown(bufferevent_getfd(bev), SHUT_WR);
552 http_badreq_successcb(evutil_socket_t fd, short what, void *arg)
554 event_debug(("%s: called (what=%04x, arg=%p)", __func__, what, arg));
555 event_base_loopexit(exit_base, NULL);
559 http_bad_request_test(void *arg)
561 struct basic_test_data *data = arg;
563 struct bufferevent *bev = NULL;
565 const char *http_request;
566 ev_uint16_t port=0, port2=0;
569 exit_base = data->base;
571 http = http_setup(&port, data->base);
573 /* bind to a second socket */
574 if (http_bind(http, &port2) == -1)
575 TT_DIE(("Bind socket failed"));
577 /* NULL request test */
578 fd = http_connect("127.0.0.1", port);
580 /* Stupid thing to send a request */
581 bev = bufferevent_socket_new(data->base, fd, 0);
582 bufferevent_setcb(bev, http_badreq_readcb, http_writecb,
583 http_badreq_errorcb, data->base);
584 bufferevent_enable(bev, EV_READ);
586 /* real NULL request */
589 shutdown(fd, SHUT_WR);
592 event_base_once(data->base, -1, EV_TIMEOUT, http_badreq_successcb, bev, &tv);
594 event_base_dispatch(data->base);
596 bufferevent_free(bev);
597 evutil_closesocket(fd);
600 fprintf(stdout, "FAILED\n");
604 /* Second answer (BAD REQUEST) on connection close */
606 /* connect to the second port */
607 fd = http_connect("127.0.0.1", port2);
609 /* Stupid thing to send a request */
610 bev = bufferevent_socket_new(data->base, fd, 0);
611 bufferevent_setcb(bev, http_badreq_readcb, http_writecb,
612 http_badreq_errorcb, data->base);
613 bufferevent_enable(bev, EV_READ);
615 /* first half of the http request */
617 "GET /badrequest HTTP/1.0\r\n" \
618 "Connection: Keep-Alive\r\n" \
621 bufferevent_write(bev, http_request, strlen(http_request));
625 event_base_once(data->base, -1, EV_TIMEOUT, http_badreq_successcb, bev, &tv);
627 event_base_dispatch(data->base);
629 tt_int_op(test_ok, ==, 2);
634 bufferevent_free(bev);
637 static struct evhttp_connection *delayed_client;
640 http_large_delay_cb(struct evhttp_request *req, void *arg)
643 evutil_timerclear(&tv);
646 event_base_once(arg, -1, EV_TIMEOUT, http_delay_reply, req, &tv);
647 evhttp_connection_fail(delayed_client, EVCON_HTTP_EOF);
651 * HTTP DELETE test, just piggyback on the basic test
655 http_delete_cb(struct evhttp_request *req, void *arg)
657 struct evbuffer *evb = evbuffer_new();
658 int empty = evhttp_find_header(evhttp_request_get_input_headers(req), "Empty") != NULL;
660 /* Expecting a DELETE request */
661 if (evhttp_request_get_command(req) != EVHTTP_REQ_DELETE) {
662 fprintf(stdout, "FAILED (delete type)\n");
666 event_debug(("%s: called\n", __func__));
667 evbuffer_add_printf(evb, BASIC_REQUEST_BODY);
669 /* allow sending of an empty reply */
670 evhttp_send_reply(req, HTTP_OK, "Everything is fine",
671 !empty ? evb : NULL);
677 http_delete_test(void *arg)
679 struct basic_test_data *data = arg;
680 struct bufferevent *bev;
682 const char *http_request;
683 ev_uint16_t port = 0;
687 http = http_setup(&port, data->base);
689 fd = http_connect("127.0.0.1", port);
691 /* Stupid thing to send a request */
692 bev = bufferevent_socket_new(data->base, fd, 0);
693 bufferevent_setcb(bev, http_readcb, http_writecb,
694 http_errorcb, data->base);
697 "DELETE /deleteit HTTP/1.1\r\n"
699 "Connection: close\r\n"
702 bufferevent_write(bev, http_request, strlen(http_request));
704 event_base_dispatch(data->base);
706 bufferevent_free(bev);
707 evutil_closesocket(fd);
711 tt_int_op(test_ok, ==, 2);
717 http_allowed_methods_eventcb(struct bufferevent *bev, short what, void *arg)
720 if ((what & (BEV_EVENT_ERROR|BEV_EVENT_EOF))) {
723 n = evbuffer_remove(bufferevent_get_input(bev), buf,
729 *output = strdup(buf);
731 event_base_loopexit(exit_base, NULL);
736 http_allowed_methods_test(void *arg)
738 struct basic_test_data *data = arg;
739 struct bufferevent *bev1, *bev2, *bev3;
740 evutil_socket_t fd1, fd2, fd3;
741 const char *http_request;
742 char *result1=NULL, *result2=NULL, *result3=NULL;
743 ev_uint16_t port = 0;
745 exit_base = data->base;
748 http = http_setup(&port, data->base);
750 fd1 = http_connect("127.0.0.1", port);
752 /* GET is out; PATCH is in. */
753 evhttp_set_allowed_methods(http, EVHTTP_REQ_PATCH);
755 /* Stupid thing to send a request */
756 bev1 = bufferevent_socket_new(data->base, fd1, 0);
757 bufferevent_enable(bev1, EV_READ|EV_WRITE);
758 bufferevent_setcb(bev1, NULL, NULL,
759 http_allowed_methods_eventcb, &result1);
762 "GET /index.html HTTP/1.1\r\n"
764 "Connection: close\r\n"
767 bufferevent_write(bev1, http_request, strlen(http_request));
769 event_base_dispatch(data->base);
771 fd2 = http_connect("127.0.0.1", port);
773 bev2 = bufferevent_socket_new(data->base, fd2, 0);
774 bufferevent_enable(bev2, EV_READ|EV_WRITE);
775 bufferevent_setcb(bev2, NULL, NULL,
776 http_allowed_methods_eventcb, &result2);
779 "PATCH /test HTTP/1.1\r\n"
781 "Connection: close\r\n"
784 bufferevent_write(bev2, http_request, strlen(http_request));
786 event_base_dispatch(data->base);
788 fd3 = http_connect("127.0.0.1", port);
790 bev3 = bufferevent_socket_new(data->base, fd3, 0);
791 bufferevent_enable(bev3, EV_READ|EV_WRITE);
792 bufferevent_setcb(bev3, NULL, NULL,
793 http_allowed_methods_eventcb, &result3);
796 "FLOOP /test HTTP/1.1\r\n"
798 "Connection: close\r\n"
801 bufferevent_write(bev3, http_request, strlen(http_request));
803 event_base_dispatch(data->base);
805 bufferevent_free(bev1);
806 bufferevent_free(bev2);
807 bufferevent_free(bev3);
808 evutil_closesocket(fd1);
809 evutil_closesocket(fd2);
810 evutil_closesocket(fd3);
814 /* Method known but disallowed */
816 tt_assert(!strncmp(result1, "HTTP/1.1 501 ", strlen("HTTP/1.1 501 ")));
818 /* Method known and allowed */
820 tt_assert(!strncmp(result2, "HTTP/1.1 200 ", strlen("HTTP/1.1 200 ")));
824 tt_assert(!strncmp(result3, "HTTP/1.1 501 ", strlen("HTTP/1.1 501 ")));
835 static void http_request_done(struct evhttp_request *, void *);
836 static void http_request_empty_done(struct evhttp_request *, void *);
839 _http_connection_test(struct basic_test_data *data, int persistent)
841 ev_uint16_t port = 0;
842 struct evhttp_connection *evcon = NULL;
843 struct evhttp_request *req = NULL;
847 http = http_setup(&port, data->base);
849 evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
852 tt_assert(evhttp_connection_get_base(evcon) == data->base);
854 exit_base = data->base;
856 * At this point, we want to schedule a request to the HTTP
857 * server using our make request method.
860 req = evhttp_request_new(http_request_done, (void*) BASIC_REQUEST_BODY);
862 /* Add the information that we care about */
863 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
865 /* We give ownership of the request to the connection */
866 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
867 fprintf(stdout, "FAILED\n");
871 event_base_dispatch(data->base);
875 /* try to make another request over the same connection */
878 req = evhttp_request_new(http_request_done, (void*) BASIC_REQUEST_BODY);
880 /* Add the information that we care about */
881 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
884 * if our connections are not supposed to be persistent; request
885 * a close from the server.
888 evhttp_add_header(evhttp_request_get_output_headers(req), "Connection", "close");
890 /* We give ownership of the request to the connection */
891 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
892 tt_abort_msg("couldn't make request");
895 event_base_dispatch(data->base);
897 /* make another request: request empty reply */
900 req = evhttp_request_new(http_request_empty_done, data->base);
902 /* Add the information that we care about */
903 evhttp_add_header(evhttp_request_get_output_headers(req), "Empty", "itis");
905 /* We give ownership of the request to the connection */
906 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
907 tt_abort_msg("Couldn't make request");
911 event_base_dispatch(data->base);
915 evhttp_connection_free(evcon);
921 http_connection_test(void *arg)
923 _http_connection_test(arg, 0);
926 http_persist_connection_test(void *arg)
928 _http_connection_test(arg, 1);
931 static struct regress_dns_server_table search_table[] = {
932 { "localhost", "A", "127.0.0.1", 0 },
933 { NULL, NULL, NULL, 0 }
937 http_connection_async_test(void *arg)
939 struct basic_test_data *data = arg;
940 ev_uint16_t port = 0;
941 struct evhttp_connection *evcon = NULL;
942 struct evhttp_request *req = NULL;
943 struct evdns_base *dns_base = NULL;
944 ev_uint16_t portnum = 0;
947 exit_base = data->base;
948 tt_assert(regress_dnsserver(data->base, &portnum, search_table));
950 dns_base = evdns_base_new(data->base, 0/* init name servers */);
953 /* Add ourself as the only nameserver, and make sure we really are
954 * the only nameserver. */
955 evutil_snprintf(address, sizeof(address), "127.0.0.1:%d", portnum);
956 evdns_base_nameserver_ip_add(dns_base, address);
960 http = http_setup(&port, data->base);
962 evcon = evhttp_connection_base_new(data->base, dns_base, "127.0.0.1", port);
966 * At this point, we want to schedule a request to the HTTP
967 * server using our make request method.
970 req = evhttp_request_new(http_request_done, (void*) BASIC_REQUEST_BODY);
972 /* Add the information that we care about */
973 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
975 /* We give ownership of the request to the connection */
976 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
977 fprintf(stdout, "FAILED\n");
981 event_base_dispatch(data->base);
985 /* try to make another request over the same connection */
988 req = evhttp_request_new(http_request_done, (void*) BASIC_REQUEST_BODY);
990 /* Add the information that we care about */
991 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
994 * if our connections are not supposed to be persistent; request
995 * a close from the server.
997 evhttp_add_header(evhttp_request_get_output_headers(req), "Connection", "close");
999 /* We give ownership of the request to the connection */
1000 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
1001 tt_abort_msg("couldn't make request");
1004 event_base_dispatch(data->base);
1006 /* make another request: request empty reply */
1009 req = evhttp_request_new(http_request_empty_done, data->base);
1011 /* Add the information that we care about */
1012 evhttp_add_header(evhttp_request_get_output_headers(req), "Empty", "itis");
1014 /* We give ownership of the request to the connection */
1015 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
1016 tt_abort_msg("Couldn't make request");
1020 event_base_dispatch(data->base);
1024 evhttp_connection_free(evcon);
1028 evdns_base_free(dns_base, 0);
1029 regress_clean_dnsserver();
1033 http_request_never_call(struct evhttp_request *req, void *arg)
1035 fprintf(stdout, "FAILED\n");
1040 http_do_cancel(evutil_socket_t fd, short what, void *arg)
1042 struct evhttp_request *req = arg;
1044 struct event_base *base;
1045 evutil_timerclear(&tv);
1047 tv.tv_usec = 500 * 1000;
1049 base = evhttp_connection_get_base(evhttp_request_get_connection(req));
1050 evhttp_cancel_request(req);
1052 event_base_loopexit(base, &tv);
1058 http_cancel_test(void *arg)
1060 struct basic_test_data *data = arg;
1061 ev_uint16_t port = 0;
1062 struct evhttp_connection *evcon = NULL;
1063 struct evhttp_request *req = NULL;
1066 exit_base = data->base;
1070 http = http_setup(&port, data->base);
1072 evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
1076 * At this point, we want to schedule a request to the HTTP
1077 * server using our make request method.
1080 req = evhttp_request_new(http_request_never_call, NULL);
1082 /* Add the information that we care about */
1083 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1085 /* We give ownership of the request to the connection */
1086 tt_int_op(evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/delay"),
1089 evutil_timerclear(&tv);
1091 tv.tv_usec = 100 * 1000;
1093 event_base_once(data->base, -1, EV_TIMEOUT, http_do_cancel, req, &tv);
1095 event_base_dispatch(data->base);
1097 tt_int_op(test_ok, ==, 2);
1099 /* try to make another request over the same connection */
1102 req = evhttp_request_new(http_request_done, (void*) BASIC_REQUEST_BODY);
1104 /* Add the information that we care about */
1105 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1107 /* We give ownership of the request to the connection */
1108 tt_int_op(evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test"),
1111 event_base_dispatch(data->base);
1113 /* make another request: request empty reply */
1116 req = evhttp_request_new(http_request_empty_done, data->base);
1118 /* Add the information that we care about */
1119 evhttp_add_header(evhttp_request_get_output_headers(req), "Empty", "itis");
1121 /* We give ownership of the request to the connection */
1122 tt_int_op(evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test"),
1125 event_base_dispatch(data->base);
1129 evhttp_connection_free(evcon);
1135 http_request_done(struct evhttp_request *req, void *arg)
1137 const char *what = arg;
1139 if (evhttp_request_get_response_code(req) != HTTP_OK) {
1140 fprintf(stderr, "FAILED\n");
1144 if (evhttp_find_header(evhttp_request_get_input_headers(req), "Content-Type") == NULL) {
1145 fprintf(stderr, "FAILED\n");
1149 if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != strlen(what)) {
1150 fprintf(stderr, "FAILED\n");
1154 if (evbuffer_datacmp(evhttp_request_get_input_buffer(req), what) != 0) {
1155 fprintf(stderr, "FAILED\n");
1160 EVUTIL_ASSERT(exit_base);
1161 event_base_loopexit(exit_base, NULL);
1165 http_request_expect_error(struct evhttp_request *req, void *arg)
1167 if (evhttp_request_get_response_code(req) == HTTP_OK) {
1168 fprintf(stderr, "FAILED\n");
1174 event_base_loopexit(arg, NULL);
1177 /* test virtual hosts */
1179 http_virtual_host_test(void *arg)
1181 struct basic_test_data *data = arg;
1182 ev_uint16_t port = 0;
1183 struct evhttp_connection *evcon = NULL;
1184 struct evhttp_request *req = NULL;
1185 struct evhttp *second = NULL, *third = NULL;
1187 struct bufferevent *bev;
1188 const char *http_request;
1190 exit_base = data->base;
1192 http = http_setup(&port, data->base);
1195 second = evhttp_new(NULL);
1196 evhttp_set_cb(second, "/funnybunny", http_basic_cb, NULL);
1197 third = evhttp_new(NULL);
1198 evhttp_set_cb(third, "/blackcoffee", http_basic_cb, NULL);
1200 if (evhttp_add_virtual_host(http, "foo.com", second) == -1) {
1201 tt_abort_msg("Couldn't add vhost");
1204 if (evhttp_add_virtual_host(http, "bar.*.foo.com", third) == -1) {
1205 tt_abort_msg("Couldn't add wildcarded vhost");
1208 /* add some aliases to the vhosts */
1209 tt_assert(evhttp_add_server_alias(second, "manolito.info") == 0);
1210 tt_assert(evhttp_add_server_alias(third, "bonkers.org") == 0);
1212 evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
1215 /* make a request with a different host and expect an error */
1216 req = evhttp_request_new(http_request_expect_error, data->base);
1218 /* Add the information that we care about */
1219 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1221 /* We give ownership of the request to the connection */
1222 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
1223 "/funnybunny") == -1) {
1224 tt_abort_msg("Couldn't make request");
1227 event_base_dispatch(data->base);
1229 tt_assert(test_ok == 1);
1233 /* make a request with the right host and expect a response */
1234 req = evhttp_request_new(http_request_done, (void*) BASIC_REQUEST_BODY);
1236 /* Add the information that we care about */
1237 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "foo.com");
1239 /* We give ownership of the request to the connection */
1240 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
1241 "/funnybunny") == -1) {
1242 fprintf(stdout, "FAILED\n");
1246 event_base_dispatch(data->base);
1248 tt_assert(test_ok == 1);
1252 /* make a request with the right host and expect a response */
1253 req = evhttp_request_new(http_request_done, (void*) BASIC_REQUEST_BODY);
1255 /* Add the information that we care about */
1256 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "bar.magic.foo.com");
1258 /* We give ownership of the request to the connection */
1259 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
1260 "/blackcoffee") == -1) {
1261 tt_abort_msg("Couldn't make request");
1264 event_base_dispatch(data->base);
1266 tt_assert(test_ok == 1)
1270 /* make a request with the right host and expect a response */
1271 req = evhttp_request_new(http_request_done, (void*) BASIC_REQUEST_BODY);
1273 /* Add the information that we care about */
1274 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "manolito.info");
1276 /* We give ownership of the request to the connection */
1277 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
1278 "/funnybunny") == -1) {
1279 tt_abort_msg("Couldn't make request");
1282 event_base_dispatch(data->base);
1284 tt_assert(test_ok == 1)
1288 /* make a request with the right host and expect a response */
1289 req = evhttp_request_new(http_request_done, (void*) BASIC_REQUEST_BODY);
1291 /* Add the Host header. This time with the optional port. */
1292 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "bonkers.org:8000");
1294 /* We give ownership of the request to the connection */
1295 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
1296 "/blackcoffee") == -1) {
1297 tt_abort_msg("Couldn't make request");
1300 event_base_dispatch(data->base);
1302 tt_assert(test_ok == 1)
1306 /* Now make a raw request with an absolute URI. */
1307 fd = http_connect("127.0.0.1", port);
1309 /* Stupid thing to send a request */
1310 bev = bufferevent_socket_new(data->base, fd, 0);
1311 bufferevent_setcb(bev, http_readcb, http_writecb,
1312 http_errorcb, NULL);
1314 /* The host in the URI should override the Host: header */
1316 "GET http://manolito.info/funnybunny HTTP/1.1\r\n"
1317 "Host: somehost\r\n"
1318 "Connection: close\r\n"
1321 bufferevent_write(bev, http_request, strlen(http_request));
1323 event_base_dispatch(data->base);
1325 tt_int_op(test_ok, ==, 2);
1327 bufferevent_free(bev);
1328 evutil_closesocket(fd);
1332 evhttp_connection_free(evcon);
1338 /* test date header and content length */
1341 http_request_empty_done(struct evhttp_request *req, void *arg)
1343 if (evhttp_request_get_response_code(req) != HTTP_OK) {
1344 fprintf(stderr, "FAILED\n");
1348 if (evhttp_find_header(evhttp_request_get_input_headers(req), "Date") == NULL) {
1349 fprintf(stderr, "FAILED\n");
1354 if (evhttp_find_header(evhttp_request_get_input_headers(req), "Content-Length") == NULL) {
1355 fprintf(stderr, "FAILED\n");
1359 if (strcmp(evhttp_find_header(evhttp_request_get_input_headers(req), "Content-Length"),
1361 fprintf(stderr, "FAILED\n");
1365 if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != 0) {
1366 fprintf(stderr, "FAILED\n");
1372 event_base_loopexit(arg, NULL);
1376 * HTTP DISPATCHER test
1380 http_dispatcher_cb(struct evhttp_request *req, void *arg)
1383 struct evbuffer *evb = evbuffer_new();
1384 event_debug(("%s: called\n", __func__));
1385 evbuffer_add_printf(evb, "DISPATCHER_TEST");
1387 evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
1393 http_dispatcher_test_done(struct evhttp_request *req, void *arg)
1395 struct event_base *base = arg;
1396 const char *what = "DISPATCHER_TEST";
1398 if (evhttp_request_get_response_code(req) != HTTP_OK) {
1399 fprintf(stderr, "FAILED\n");
1403 if (evhttp_find_header(evhttp_request_get_input_headers(req), "Content-Type") == NULL) {
1404 fprintf(stderr, "FAILED (content type)\n");
1408 if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != strlen(what)) {
1409 fprintf(stderr, "FAILED (length %lu vs %lu)\n",
1410 (unsigned long)evbuffer_get_length(evhttp_request_get_input_buffer(req)), (unsigned long)strlen(what));
1414 if (evbuffer_datacmp(evhttp_request_get_input_buffer(req), what) != 0) {
1415 fprintf(stderr, "FAILED (data)\n");
1420 event_base_loopexit(base, NULL);
1424 http_dispatcher_test(void *arg)
1426 struct basic_test_data *data = arg;
1427 ev_uint16_t port = 0;
1428 struct evhttp_connection *evcon = NULL;
1429 struct evhttp_request *req = NULL;
1433 http = http_setup(&port, data->base);
1435 evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
1438 /* also bind to local host */
1439 evhttp_connection_set_local_address(evcon, "127.0.0.1");
1442 * At this point, we want to schedule an HTTP GET request
1443 * server using our make request method.
1446 req = evhttp_request_new(http_dispatcher_test_done, data->base);
1449 /* Add the information that we care about */
1450 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1452 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/?arg=val") == -1) {
1453 tt_abort_msg("Couldn't make request");
1456 event_base_dispatch(data->base);
1460 evhttp_connection_free(evcon);
1469 void http_postrequest_done(struct evhttp_request *, void *);
1471 #define POST_DATA "Okay. Not really printf"
1474 http_post_test(void *arg)
1476 struct basic_test_data *data = arg;
1477 ev_uint16_t port = 0;
1478 struct evhttp_connection *evcon = NULL;
1479 struct evhttp_request *req = NULL;
1483 http = http_setup(&port, data->base);
1485 evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
1489 * At this point, we want to schedule an HTTP POST request
1490 * server using our make request method.
1493 req = evhttp_request_new(http_postrequest_done, data->base);
1496 /* Add the information that we care about */
1497 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1498 evbuffer_add_printf(evhttp_request_get_output_buffer(req), POST_DATA);
1500 if (evhttp_make_request(evcon, req, EVHTTP_REQ_POST, "/postit") == -1) {
1501 tt_abort_msg("Couldn't make request");
1504 event_base_dispatch(data->base);
1506 tt_int_op(test_ok, ==, 1);
1510 req = evhttp_request_new(http_postrequest_done, data->base);
1513 /* Now try with 100-continue. */
1515 /* Add the information that we care about */
1516 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1517 evhttp_add_header(evhttp_request_get_output_headers(req), "Expect", "100-continue");
1518 evbuffer_add_printf(evhttp_request_get_output_buffer(req), POST_DATA);
1520 if (evhttp_make_request(evcon, req, EVHTTP_REQ_POST, "/postit") == -1) {
1521 tt_abort_msg("Couldn't make request");
1524 event_base_dispatch(data->base);
1526 tt_int_op(test_ok, ==, 1);
1528 evhttp_connection_free(evcon);
1536 http_post_cb(struct evhttp_request *req, void *arg)
1538 struct evbuffer *evb;
1539 event_debug(("%s: called\n", __func__));
1541 /* Yes, we are expecting a post request */
1542 if (evhttp_request_get_command(req) != EVHTTP_REQ_POST) {
1543 fprintf(stdout, "FAILED (post type)\n");
1547 if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != strlen(POST_DATA)) {
1548 fprintf(stdout, "FAILED (length: %lu vs %lu)\n",
1549 (unsigned long) evbuffer_get_length(evhttp_request_get_input_buffer(req)), (unsigned long) strlen(POST_DATA));
1553 if (evbuffer_datacmp(evhttp_request_get_input_buffer(req), POST_DATA) != 0) {
1554 fprintf(stdout, "FAILED (data)\n");
1555 fprintf(stdout, "Got :%s\n", evbuffer_pullup(evhttp_request_get_input_buffer(req),-1));
1556 fprintf(stdout, "Want:%s\n", POST_DATA);
1560 evb = evbuffer_new();
1561 evbuffer_add_printf(evb, BASIC_REQUEST_BODY);
1563 evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
1569 http_postrequest_done(struct evhttp_request *req, void *arg)
1571 const char *what = BASIC_REQUEST_BODY;
1572 struct event_base *base = arg;
1575 fprintf(stderr, "FAILED (timeout)\n");
1579 if (evhttp_request_get_response_code(req) != HTTP_OK) {
1581 fprintf(stderr, "FAILED (response code)\n");
1585 if (evhttp_find_header(evhttp_request_get_input_headers(req), "Content-Type") == NULL) {
1586 fprintf(stderr, "FAILED (content type)\n");
1590 if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != strlen(what)) {
1591 fprintf(stderr, "FAILED (length %lu vs %lu)\n",
1592 (unsigned long)evbuffer_get_length(evhttp_request_get_input_buffer(req)), (unsigned long)strlen(what));
1596 if (evbuffer_datacmp(evhttp_request_get_input_buffer(req), what) != 0) {
1597 fprintf(stderr, "FAILED (data)\n");
1602 event_base_loopexit(base, NULL);
1606 * HTTP PUT test, basically just like POST, but ...
1609 void http_putrequest_done(struct evhttp_request *, void *);
1611 #define PUT_DATA "Hi, I'm some PUT data"
1614 http_put_test(void *arg)
1616 struct basic_test_data *data = arg;
1617 ev_uint16_t port = 0;
1618 struct evhttp_connection *evcon = NULL;
1619 struct evhttp_request *req = NULL;
1623 http = http_setup(&port, data->base);
1625 evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
1629 * Schedule the HTTP PUT request
1632 req = evhttp_request_new(http_putrequest_done, data->base);
1635 /* Add the information that we care about */
1636 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "someotherhost");
1637 evbuffer_add_printf(evhttp_request_get_output_buffer(req), PUT_DATA);
1639 if (evhttp_make_request(evcon, req, EVHTTP_REQ_PUT, "/putit") == -1) {
1640 tt_abort_msg("Couldn't make request");
1643 event_base_dispatch(data->base);
1645 evhttp_connection_free(evcon);
1648 tt_int_op(test_ok, ==, 1);
1654 http_put_cb(struct evhttp_request *req, void *arg)
1656 struct evbuffer *evb;
1657 event_debug(("%s: called\n", __func__));
1659 /* Expecting a PUT request */
1660 if (evhttp_request_get_command(req) != EVHTTP_REQ_PUT) {
1661 fprintf(stdout, "FAILED (put type)\n");
1665 if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != strlen(PUT_DATA)) {
1666 fprintf(stdout, "FAILED (length: %lu vs %lu)\n",
1667 (unsigned long)evbuffer_get_length(evhttp_request_get_input_buffer(req)), (unsigned long)strlen(PUT_DATA));
1671 if (evbuffer_datacmp(evhttp_request_get_input_buffer(req), PUT_DATA) != 0) {
1672 fprintf(stdout, "FAILED (data)\n");
1673 fprintf(stdout, "Got :%s\n", evbuffer_pullup(evhttp_request_get_input_buffer(req),-1));
1674 fprintf(stdout, "Want:%s\n", PUT_DATA);
1678 evb = evbuffer_new();
1679 evbuffer_add_printf(evb, "That ain't funny");
1681 evhttp_send_reply(req, HTTP_OK, "Everything is great", evb);
1687 http_putrequest_done(struct evhttp_request *req, void *arg)
1689 struct event_base *base = arg;
1690 const char *what = "That ain't funny";
1693 fprintf(stderr, "FAILED (timeout)\n");
1697 if (evhttp_request_get_response_code(req) != HTTP_OK) {
1699 fprintf(stderr, "FAILED (response code)\n");
1703 if (evhttp_find_header(evhttp_request_get_input_headers(req), "Content-Type") == NULL) {
1704 fprintf(stderr, "FAILED (content type)\n");
1708 if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != strlen(what)) {
1709 fprintf(stderr, "FAILED (length %lu vs %lu)\n",
1710 (unsigned long)evbuffer_get_length(evhttp_request_get_input_buffer(req)), (unsigned long)strlen(what));
1715 if (evbuffer_datacmp(evhttp_request_get_input_buffer(req), what) != 0) {
1716 fprintf(stderr, "FAILED (data)\n");
1721 event_base_loopexit(base, NULL);
1725 http_failure_readcb(struct bufferevent *bev, void *arg)
1727 const char *what = "400 Bad Request";
1728 if (evbuffer_contains(bufferevent_get_input(bev), what)) {
1730 bufferevent_disable(bev, EV_READ);
1731 event_base_loopexit(arg, NULL);
1736 * Testing that the HTTP server can deal with a malformed request.
1739 http_failure_test(void *arg)
1741 struct basic_test_data *data = arg;
1742 struct bufferevent *bev;
1744 const char *http_request;
1745 ev_uint16_t port = 0;
1749 http = http_setup(&port, data->base);
1751 fd = http_connect("127.0.0.1", port);
1753 /* Stupid thing to send a request */
1754 bev = bufferevent_socket_new(data->base, fd, 0);
1755 bufferevent_setcb(bev, http_failure_readcb, http_writecb,
1756 http_errorcb, data->base);
1758 http_request = "illegal request\r\n";
1760 bufferevent_write(bev, http_request, strlen(http_request));
1762 event_base_dispatch(data->base);
1764 bufferevent_free(bev);
1765 evutil_closesocket(fd);
1769 tt_int_op(test_ok, ==, 2);
1775 close_detect_done(struct evhttp_request *req, void *arg)
1779 tt_assert(evhttp_request_get_response_code(req) == HTTP_OK);
1784 evutil_timerclear(&tv);
1786 event_base_loopexit(arg, &tv);
1790 close_detect_launch(evutil_socket_t fd, short what, void *arg)
1792 struct evhttp_connection *evcon = arg;
1793 struct event_base *base = evhttp_connection_get_base(evcon);
1794 struct evhttp_request *req;
1796 req = evhttp_request_new(close_detect_done, base);
1798 /* Add the information that we care about */
1799 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1801 /* We give ownership of the request to the connection */
1802 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
1803 tt_fail_msg("Couldn't make request");
1808 close_detect_cb(struct evhttp_request *req, void *arg)
1810 struct evhttp_connection *evcon = arg;
1811 struct event_base *base = evhttp_connection_get_base(evcon);
1814 if (req != NULL && evhttp_request_get_response_code(req) != HTTP_OK) {
1815 tt_abort_msg("Failed");
1818 evutil_timerclear(&tv);
1819 tv.tv_sec = 3; /* longer than the http time out */
1821 /* launch a new request on the persistent connection in 3 seconds */
1822 event_base_once(base, -1, EV_TIMEOUT, close_detect_launch, evcon, &tv);
1829 _http_close_detection(struct basic_test_data *data, int with_delay)
1831 ev_uint16_t port = 0;
1832 struct evhttp_connection *evcon = NULL;
1833 struct evhttp_request *req = NULL;
1836 http = http_setup(&port, data->base);
1838 /* 2 second timeout */
1839 evhttp_set_timeout(http, 1);
1841 evcon = evhttp_connection_base_new(data->base, NULL,
1844 delayed_client = evcon;
1847 * At this point, we want to schedule a request to the HTTP
1848 * server using our make request method.
1851 req = evhttp_request_new(close_detect_cb, evcon);
1853 /* Add the information that we care about */
1854 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1856 /* We give ownership of the request to the connection */
1857 if (evhttp_make_request(evcon,
1858 req, EVHTTP_REQ_GET, with_delay ? "/largedelay" : "/test") == -1) {
1859 tt_abort_msg("couldn't make request");
1863 event_base_dispatch(data->base);
1865 /* at this point, the http server should have no connection */
1866 tt_assert(TAILQ_FIRST(&http->connections) == NULL);
1870 evhttp_connection_free(evcon);
1875 http_close_detection_test(void *arg)
1877 _http_close_detection(arg, 0);
1880 http_close_detection_delay_test(void *arg)
1882 _http_close_detection(arg, 1);
1886 http_highport_test(void *arg)
1888 struct basic_test_data *data = arg;
1890 struct evhttp *myhttp = NULL;
1892 /* Try a few different ports */
1893 for (i = 0; i < 50; ++i) {
1894 myhttp = evhttp_new(data->base);
1895 if (evhttp_bind_socket(myhttp, "127.0.0.1", 65535 - i) == 0) {
1897 evhttp_free(myhttp);
1900 evhttp_free(myhttp);
1903 tt_fail_msg("Couldn't get a high port");
1907 http_bad_header_test(void *ptr)
1909 struct evkeyvalq headers;
1911 TAILQ_INIT(&headers);
1913 tt_want(evhttp_add_header(&headers, "One", "Two") == 0);
1914 tt_want(evhttp_add_header(&headers, "One", "Two\r\n Three") == 0);
1915 tt_want(evhttp_add_header(&headers, "One\r", "Two") == -1);
1916 tt_want(evhttp_add_header(&headers, "One\n", "Two") == -1);
1917 tt_want(evhttp_add_header(&headers, "One", "Two\r") == -1);
1918 tt_want(evhttp_add_header(&headers, "One", "Two\n") == -1);
1920 evhttp_clear_headers(&headers);
1923 static int validate_header(
1924 const struct evkeyvalq* headers,
1925 const char *key, const char *value)
1927 const char *real_val = evhttp_find_header(headers, key);
1928 tt_assert(real_val != NULL);
1929 tt_want(strcmp(real_val, value) == 0);
1935 http_parse_query_test(void *ptr)
1937 struct evkeyvalq headers;
1940 TAILQ_INIT(&headers);
1942 r = evhttp_parse_query("http://www.test.com/?q=test", &headers);
1943 tt_want(validate_header(&headers, "q", "test") == 0);
1944 tt_int_op(r, ==, 0);
1945 evhttp_clear_headers(&headers);
1947 r = evhttp_parse_query("http://www.test.com/?q=test&foo=bar", &headers);
1948 tt_want(validate_header(&headers, "q", "test") == 0);
1949 tt_want(validate_header(&headers, "foo", "bar") == 0);
1950 tt_int_op(r, ==, 0);
1951 evhttp_clear_headers(&headers);
1953 r = evhttp_parse_query("http://www.test.com/?q=test+foo", &headers);
1954 tt_want(validate_header(&headers, "q", "test foo") == 0);
1955 tt_int_op(r, ==, 0);
1956 evhttp_clear_headers(&headers);
1958 r = evhttp_parse_query("http://www.test.com/?q=test%0Afoo", &headers);
1959 tt_want(validate_header(&headers, "q", "test\nfoo") == 0);
1960 tt_int_op(r, ==, 0);
1961 evhttp_clear_headers(&headers);
1963 r = evhttp_parse_query("http://www.test.com/?q=test%0Dfoo", &headers);
1964 tt_want(validate_header(&headers, "q", "test\rfoo") == 0);
1965 tt_int_op(r, ==, 0);
1966 evhttp_clear_headers(&headers);
1968 r = evhttp_parse_query("http://www.test.com/?q=test&&q2", &headers);
1969 tt_int_op(r, ==, -1);
1970 evhttp_clear_headers(&headers);
1972 r = evhttp_parse_query("http://www.test.com/?q=test+this", &headers);
1973 tt_want(validate_header(&headers, "q", "test this") == 0);
1974 tt_int_op(r, ==, 0);
1975 evhttp_clear_headers(&headers);
1977 r = evhttp_parse_query("http://www.test.com/?q=test&q2=foo", &headers);
1978 tt_int_op(r, ==, 0);
1979 tt_want(validate_header(&headers, "q", "test") == 0);
1980 tt_want(validate_header(&headers, "q2", "foo") == 0);
1981 evhttp_clear_headers(&headers);
1983 r = evhttp_parse_query("http://www.test.com/?q&q2=foo", &headers);
1984 tt_int_op(r, ==, -1);
1985 evhttp_clear_headers(&headers);
1987 r = evhttp_parse_query("http://www.test.com/?q=foo&q2", &headers);
1988 tt_int_op(r, ==, -1);
1989 evhttp_clear_headers(&headers);
1991 r = evhttp_parse_query("http://www.test.com/?q=foo&q2&q3=x", &headers);
1992 tt_int_op(r, ==, -1);
1993 evhttp_clear_headers(&headers);
1995 r = evhttp_parse_query("http://www.test.com/?q=&q2=&q3=", &headers);
1996 tt_int_op(r, ==, 0);
1997 tt_want(validate_header(&headers, "q", "") == 0);
1998 tt_want(validate_header(&headers, "q2", "") == 0);
1999 tt_want(validate_header(&headers, "q3", "") == 0);
2000 evhttp_clear_headers(&headers);
2003 evhttp_clear_headers(&headers);
2007 http_parse_uri_test(void *ptr)
2009 struct evhttp_uri *uri = NULL;
2012 #define TT_URI(want) do { \
2013 char *ret = evhttp_uri_join(uri, url_tmp, sizeof(url_tmp)); \
2014 tt_want(ret != NULL); \
2015 tt_want(ret == url_tmp); \
2016 if (strcmp(ret,want) != 0) \
2017 TT_FAIL(("\"%s\" != \"%s\"",ret,want)); \
2020 tt_want(evhttp_uri_join(NULL, 0, 0) == NULL);
2021 tt_want(evhttp_uri_join(NULL, url_tmp, 0) == NULL);
2022 tt_want(evhttp_uri_join(NULL, url_tmp, sizeof(url_tmp)) == NULL);
2024 /* bad URIs: parsing */
2025 #define BAD(s) do { \
2026 if (evhttp_uri_parse(s) != NULL) \
2027 TT_FAIL(("Expected error parsing \"%s\"",s)); \
2029 BAD("http://www.test.com/ why hello");
2030 BAD("http://www.test.com/why-hello\x01");
2031 BAD("http://www.test.com/why-hello?\x01");
2032 BAD("http://www.test.com/why-hello#\x01");
2033 BAD("http://www.\x01.test.com/why-hello");
2034 BAD("http://www.%7test.com/why-hello");
2035 BAD("http://www.test.com/why-hell%7o");
2036 BAD("h%3ttp://www.test.com/why-hello");
2037 BAD("http://www.test.com/why-hello%7");
2038 BAD("http://www.test.com/why-hell%7o");
2039 BAD("http://www.test.com/foo?ba%r");
2040 BAD("http://www.test.com/foo#ba%r");
2042 BAD("http://www.test.com:999x/");
2043 BAD("http://www.test.com:x/");
2044 BAD("http://[hello-there]/");
2045 BAD("http://[::1]]/");
2046 BAD("http://[::1/");
2047 BAD("http://[foob/");
2049 BAD("http://[ffff:ffff:ffff:ffff:Ffff:ffff:ffff:"
2050 "ffff:ffff:ffff:ffff:ffff:ffff:ffff]/");
2051 BAD("http://[vX.foo]/");
2052 BAD("http://[vX.foo]/");
2053 BAD("http://[v.foo]/");
2054 BAD("http://[v5.fo%o]/");
2055 BAD("http://[v5X]/");
2056 BAD("http://[v5]/");
2058 BAD("http://f\x01red@www.example.com/");
2059 BAD("http://f%0red@www.example.com/");
2060 BAD("http://www.example.com:9999999999999999999999999999999999999/");
2061 BAD("http://www.example.com:hihi/");
2062 BAD("://www.example.com/");
2064 /* bad URIs: joining */
2065 uri = evhttp_uri_new();
2066 tt_want(0==evhttp_uri_set_host(uri, "www.example.com"));
2067 tt_want(evhttp_uri_join(uri, url_tmp, sizeof(url_tmp)) != NULL);
2068 /* not enough space: */
2069 tt_want(evhttp_uri_join(uri, url_tmp, 3) == NULL);
2070 /* host is set, but path doesn't start with "/": */
2071 tt_want(0==evhttp_uri_set_path(uri, "hi_mom"));
2072 tt_want(evhttp_uri_join(uri, url_tmp, sizeof(url_tmp)) == NULL);
2073 tt_want(evhttp_uri_join(uri, NULL, sizeof(url_tmp))==NULL);
2074 tt_want(evhttp_uri_join(uri, url_tmp, 0)==NULL);
2075 evhttp_uri_free(uri);
2076 uri = evhttp_uri_parse("mailto:foo@bar");
2077 tt_want(uri != NULL);
2078 tt_want(evhttp_uri_get_host(uri) == NULL);
2079 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2080 tt_want(evhttp_uri_get_port(uri) == -1);
2081 tt_want(!strcmp(evhttp_uri_get_scheme(uri), "mailto"));
2082 tt_want(!strcmp(evhttp_uri_get_path(uri), "foo@bar"));
2083 tt_want(evhttp_uri_get_query(uri) == NULL);
2084 tt_want(evhttp_uri_get_fragment(uri) == NULL);
2085 TT_URI("mailto:foo@bar");
2086 evhttp_uri_free(uri);
2088 uri = evhttp_uri_new();
2089 /* Bad URI usage: setting invalid values */
2090 tt_want(-1 == evhttp_uri_set_scheme(uri,""));
2091 tt_want(-1 == evhttp_uri_set_scheme(uri,"33"));
2092 tt_want(-1 == evhttp_uri_set_scheme(uri,"hi!"));
2093 tt_want(-1 == evhttp_uri_set_userinfo(uri,"hello@"));
2094 tt_want(-1 == evhttp_uri_set_host(uri,"[1.2.3.4]"));
2095 tt_want(-1 == evhttp_uri_set_host(uri,"["));
2096 tt_want(-1 == evhttp_uri_set_host(uri,"www.[foo].com"));
2097 tt_want(-1 == evhttp_uri_set_port(uri,-3));
2098 tt_want(-1 == evhttp_uri_set_path(uri,"hello?world"));
2099 tt_want(-1 == evhttp_uri_set_query(uri,"hello#world"));
2100 tt_want(-1 == evhttp_uri_set_fragment(uri,"hello#world"));
2101 /* Valid URI usage: setting valid values */
2102 tt_want(0 == evhttp_uri_set_scheme(uri,"http"));
2103 tt_want(0 == evhttp_uri_set_scheme(uri,NULL));
2104 tt_want(0 == evhttp_uri_set_userinfo(uri,"username:pass"));
2105 tt_want(0 == evhttp_uri_set_userinfo(uri,NULL));
2106 tt_want(0 == evhttp_uri_set_host(uri,"www.example.com"));
2107 tt_want(0 == evhttp_uri_set_host(uri,"1.2.3.4"));
2108 tt_want(0 == evhttp_uri_set_host(uri,"[1:2:3:4::]"));
2109 tt_want(0 == evhttp_uri_set_host(uri,"[v7.wobblewobble]"));
2110 tt_want(0 == evhttp_uri_set_host(uri,NULL));
2111 tt_want(0 == evhttp_uri_set_host(uri,""));
2112 tt_want(0 == evhttp_uri_set_port(uri, -1));
2113 tt_want(0 == evhttp_uri_set_port(uri, 80));
2114 tt_want(0 == evhttp_uri_set_port(uri, 65535));
2115 tt_want(0 == evhttp_uri_set_path(uri, ""));
2116 tt_want(0 == evhttp_uri_set_path(uri, "/documents/public/index.html"));
2117 tt_want(0 == evhttp_uri_set_path(uri, NULL));
2118 tt_want(0 == evhttp_uri_set_query(uri, "key=val&key2=val2"));
2119 tt_want(0 == evhttp_uri_set_query(uri, "keyvalblarg"));
2120 tt_want(0 == evhttp_uri_set_query(uri, ""));
2121 tt_want(0 == evhttp_uri_set_query(uri, NULL));
2122 tt_want(0 == evhttp_uri_set_fragment(uri, ""));
2123 tt_want(0 == evhttp_uri_set_fragment(uri, "here?i?am"));
2124 tt_want(0 == evhttp_uri_set_fragment(uri, NULL));
2125 evhttp_uri_free(uri);
2128 uri = evhttp_uri_parse("http://www.test.com/?q=t%33est");
2129 tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2130 tt_want(strcmp(evhttp_uri_get_host(uri), "www.test.com") == 0);
2131 tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2132 tt_want(strcmp(evhttp_uri_get_query(uri), "q=t%33est") == 0);
2133 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2134 tt_want(evhttp_uri_get_port(uri) == -1);
2135 tt_want(evhttp_uri_get_fragment(uri) == NULL);
2136 TT_URI("http://www.test.com/?q=t%33est");
2137 evhttp_uri_free(uri);
2139 uri = evhttp_uri_parse("http://%77ww.test.com");
2140 tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2141 tt_want(strcmp(evhttp_uri_get_host(uri), "%77ww.test.com") == 0);
2142 tt_want(strcmp(evhttp_uri_get_path(uri), "") == 0);
2143 tt_want(evhttp_uri_get_query(uri) == NULL);
2144 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2145 tt_want(evhttp_uri_get_port(uri) == -1);
2146 tt_want(evhttp_uri_get_fragment(uri) == NULL);
2147 TT_URI("http://%77ww.test.com");
2148 evhttp_uri_free(uri);
2150 uri = evhttp_uri_parse("http://www.test.com?q=test");
2151 tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2152 tt_want(strcmp(evhttp_uri_get_host(uri), "www.test.com") == 0);
2153 tt_want(strcmp(evhttp_uri_get_path(uri), "") == 0);
2154 tt_want(strcmp(evhttp_uri_get_query(uri), "q=test") == 0);
2155 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2156 tt_want(evhttp_uri_get_port(uri) == -1);
2157 tt_want(evhttp_uri_get_fragment(uri) == NULL);
2158 TT_URI("http://www.test.com?q=test");
2159 evhttp_uri_free(uri);
2161 uri = evhttp_uri_parse("http://www.test.com#fragment");
2162 tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2163 tt_want(strcmp(evhttp_uri_get_host(uri), "www.test.com") == 0);
2164 tt_want(strcmp(evhttp_uri_get_path(uri), "") == 0);
2165 tt_want(evhttp_uri_get_query(uri) == NULL);
2166 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2167 tt_want(evhttp_uri_get_port(uri) == -1);
2168 tt_want_str_op(evhttp_uri_get_fragment(uri), ==, "fragment");
2169 TT_URI("http://www.test.com#fragment");
2170 evhttp_uri_free(uri);
2172 uri = evhttp_uri_parse("http://8000/");
2173 tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2174 tt_want(strcmp(evhttp_uri_get_host(uri), "8000") == 0);
2175 tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2176 tt_want(evhttp_uri_get_query(uri) == NULL);
2177 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2178 tt_want(evhttp_uri_get_port(uri) == -1);
2179 tt_want(evhttp_uri_get_fragment(uri) == NULL);
2180 TT_URI("http://8000/");
2181 evhttp_uri_free(uri);
2183 uri = evhttp_uri_parse("http://:8000/");
2184 tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2185 tt_want(strcmp(evhttp_uri_get_host(uri), "") == 0);
2186 tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2187 tt_want(evhttp_uri_get_query(uri) == NULL);
2188 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2189 tt_want(evhttp_uri_get_port(uri) == 8000);
2190 tt_want(evhttp_uri_get_fragment(uri) == NULL);
2191 TT_URI("http://:8000/");
2192 evhttp_uri_free(uri);
2194 uri = evhttp_uri_parse("http://www.test.com:/"); /* empty port */
2195 tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2196 tt_want(strcmp(evhttp_uri_get_host(uri), "www.test.com") == 0);
2197 tt_want_str_op(evhttp_uri_get_path(uri), ==, "/");
2198 tt_want(evhttp_uri_get_query(uri) == NULL);
2199 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2200 tt_want(evhttp_uri_get_port(uri) == -1);
2201 tt_want(evhttp_uri_get_fragment(uri) == NULL);
2202 TT_URI("http://www.test.com/");
2203 evhttp_uri_free(uri);
2205 uri = evhttp_uri_parse("http://www.test.com:"); /* empty port 2 */
2206 tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2207 tt_want(strcmp(evhttp_uri_get_host(uri), "www.test.com") == 0);
2208 tt_want(strcmp(evhttp_uri_get_path(uri), "") == 0);
2209 tt_want(evhttp_uri_get_query(uri) == NULL);
2210 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2211 tt_want(evhttp_uri_get_port(uri) == -1);
2212 tt_want(evhttp_uri_get_fragment(uri) == NULL);
2213 TT_URI("http://www.test.com");
2214 evhttp_uri_free(uri);
2216 uri = evhttp_uri_parse("ftp://www.test.com/?q=test");
2217 tt_want(strcmp(evhttp_uri_get_scheme(uri), "ftp") == 0);
2218 tt_want(strcmp(evhttp_uri_get_host(uri), "www.test.com") == 0);
2219 tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2220 tt_want(strcmp(evhttp_uri_get_query(uri), "q=test") == 0);
2221 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2222 tt_want(evhttp_uri_get_port(uri) == -1);
2223 tt_want(evhttp_uri_get_fragment(uri) == NULL);
2224 TT_URI("ftp://www.test.com/?q=test");
2225 evhttp_uri_free(uri);
2227 uri = evhttp_uri_parse("ftp://[::1]:999/?q=test");
2228 tt_want(strcmp(evhttp_uri_get_scheme(uri), "ftp") == 0);
2229 tt_want(strcmp(evhttp_uri_get_host(uri), "[::1]") == 0);
2230 tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2231 tt_want(strcmp(evhttp_uri_get_query(uri), "q=test") == 0);
2232 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2233 tt_want(evhttp_uri_get_port(uri) == 999);
2234 tt_want(evhttp_uri_get_fragment(uri) == NULL);
2235 TT_URI("ftp://[::1]:999/?q=test");
2236 evhttp_uri_free(uri);
2238 uri = evhttp_uri_parse("ftp://[ff00::127.0.0.1]/?q=test");
2239 tt_want(strcmp(evhttp_uri_get_scheme(uri), "ftp") == 0);
2240 tt_want(strcmp(evhttp_uri_get_host(uri), "[ff00::127.0.0.1]") == 0);
2241 tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2242 tt_want(strcmp(evhttp_uri_get_query(uri), "q=test") == 0);
2243 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2244 tt_want(evhttp_uri_get_port(uri) == -1);
2245 tt_want(evhttp_uri_get_fragment(uri) == NULL);
2246 TT_URI("ftp://[ff00::127.0.0.1]/?q=test");
2247 evhttp_uri_free(uri);
2249 uri = evhttp_uri_parse("ftp://[v99.not_(any:time)_soon]/?q=test");
2250 tt_want(strcmp(evhttp_uri_get_scheme(uri), "ftp") == 0);
2251 tt_want(strcmp(evhttp_uri_get_host(uri), "[v99.not_(any:time)_soon]") == 0);
2252 tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2253 tt_want(strcmp(evhttp_uri_get_query(uri), "q=test") == 0);
2254 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2255 tt_want(evhttp_uri_get_port(uri) == -1);
2256 tt_want(evhttp_uri_get_fragment(uri) == NULL);
2257 TT_URI("ftp://[v99.not_(any:time)_soon]/?q=test");
2258 evhttp_uri_free(uri);
2260 uri = evhttp_uri_parse("scheme://user:pass@foo.com:42/?q=test&s=some+thing#fragment");
2261 tt_want(strcmp(evhttp_uri_get_scheme(uri), "scheme") == 0);
2262 tt_want(strcmp(evhttp_uri_get_userinfo(uri), "user:pass") == 0);
2263 tt_want(strcmp(evhttp_uri_get_host(uri), "foo.com") == 0);
2264 tt_want(evhttp_uri_get_port(uri) == 42);
2265 tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2266 tt_want(strcmp(evhttp_uri_get_query(uri), "q=test&s=some+thing") == 0);
2267 tt_want(strcmp(evhttp_uri_get_fragment(uri), "fragment") == 0);
2268 TT_URI("scheme://user:pass@foo.com:42/?q=test&s=some+thing#fragment");
2269 evhttp_uri_free(uri);
2271 uri = evhttp_uri_parse("scheme://user@foo.com/#fragment");
2272 tt_want(strcmp(evhttp_uri_get_scheme(uri), "scheme") == 0);
2273 tt_want(strcmp(evhttp_uri_get_userinfo(uri), "user") == 0);
2274 tt_want(strcmp(evhttp_uri_get_host(uri), "foo.com") == 0);
2275 tt_want(evhttp_uri_get_port(uri) == -1);
2276 tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2277 tt_want(evhttp_uri_get_query(uri) == NULL);
2278 tt_want(strcmp(evhttp_uri_get_fragment(uri), "fragment") == 0);
2279 TT_URI("scheme://user@foo.com/#fragment");
2280 evhttp_uri_free(uri);
2282 uri = evhttp_uri_parse("scheme://%75ser@foo.com/#frag@ment");
2283 tt_want(strcmp(evhttp_uri_get_scheme(uri), "scheme") == 0);
2284 tt_want(strcmp(evhttp_uri_get_userinfo(uri), "%75ser") == 0);
2285 tt_want(strcmp(evhttp_uri_get_host(uri), "foo.com") == 0);
2286 tt_want(evhttp_uri_get_port(uri) == -1);
2287 tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2288 tt_want(evhttp_uri_get_query(uri) == NULL);
2289 tt_want(strcmp(evhttp_uri_get_fragment(uri), "frag@ment") == 0);
2290 TT_URI("scheme://%75ser@foo.com/#frag@ment");
2291 evhttp_uri_free(uri);
2293 uri = evhttp_uri_parse("file:///some/path/to/the/file");
2294 tt_want(strcmp(evhttp_uri_get_scheme(uri), "file") == 0);
2295 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2296 tt_want(strcmp(evhttp_uri_get_host(uri), "") == 0);
2297 tt_want(evhttp_uri_get_port(uri) == -1);
2298 tt_want(strcmp(evhttp_uri_get_path(uri), "/some/path/to/the/file") == 0);
2299 tt_want(evhttp_uri_get_query(uri) == NULL);
2300 tt_want(evhttp_uri_get_fragment(uri) == NULL);
2301 TT_URI("file:///some/path/to/the/file");
2302 evhttp_uri_free(uri);
2304 uri = evhttp_uri_parse("///some/path/to/the-file");
2305 tt_want(uri != NULL);
2306 tt_want(evhttp_uri_get_scheme(uri) == NULL);
2307 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2308 tt_want(strcmp(evhttp_uri_get_host(uri), "") == 0);
2309 tt_want(evhttp_uri_get_port(uri) == -1);
2310 tt_want(strcmp(evhttp_uri_get_path(uri), "/some/path/to/the-file") == 0);
2311 tt_want(evhttp_uri_get_query(uri) == NULL);
2312 tt_want(evhttp_uri_get_fragment(uri) == NULL);
2313 TT_URI("///some/path/to/the-file");
2314 evhttp_uri_free(uri);
2316 uri = evhttp_uri_parse("/s:ome/path/to/the-file?q=99#fred");
2317 tt_want(uri != NULL);
2318 tt_want(evhttp_uri_get_scheme(uri) == NULL);
2319 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2320 tt_want(evhttp_uri_get_host(uri) == NULL);
2321 tt_want(evhttp_uri_get_port(uri) == -1);
2322 tt_want(strcmp(evhttp_uri_get_path(uri), "/s:ome/path/to/the-file") == 0);
2323 tt_want(strcmp(evhttp_uri_get_query(uri), "q=99") == 0);
2324 tt_want(strcmp(evhttp_uri_get_fragment(uri), "fred") == 0);
2325 TT_URI("/s:ome/path/to/the-file?q=99#fred");
2326 evhttp_uri_free(uri);
2328 uri = evhttp_uri_parse("relative/path/with/co:lon");
2329 tt_want(uri != NULL);
2330 tt_want(evhttp_uri_get_scheme(uri) == NULL);
2331 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2332 tt_want(evhttp_uri_get_host(uri) == NULL);
2333 tt_want(evhttp_uri_get_port(uri) == -1);
2334 tt_want(strcmp(evhttp_uri_get_path(uri), "relative/path/with/co:lon") == 0);
2335 tt_want(evhttp_uri_get_query(uri) == NULL);
2336 tt_want(evhttp_uri_get_fragment(uri) == NULL);
2337 TT_URI("relative/path/with/co:lon");
2338 evhttp_uri_free(uri);
2340 uri = evhttp_uri_parse("bob?q=99&q2=q?33#fr?ed");
2341 tt_want(uri != NULL);
2342 tt_want(evhttp_uri_get_scheme(uri) == NULL);
2343 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2344 tt_want(evhttp_uri_get_host(uri) == NULL);
2345 tt_want(evhttp_uri_get_port(uri) == -1);
2346 tt_want(strcmp(evhttp_uri_get_path(uri), "bob") == 0);
2347 tt_want(strcmp(evhttp_uri_get_query(uri), "q=99&q2=q?33") == 0);
2348 tt_want(strcmp(evhttp_uri_get_fragment(uri), "fr?ed") == 0);
2349 TT_URI("bob?q=99&q2=q?33#fr?ed");
2350 evhttp_uri_free(uri);
2352 uri = evhttp_uri_parse("#fr?ed");
2353 tt_want(uri != NULL);
2354 tt_want(evhttp_uri_get_scheme(uri) == NULL);
2355 tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2356 tt_want(evhttp_uri_get_host(uri) == NULL);
2357 tt_want(evhttp_uri_get_port(uri) == -1);
2358 tt_want(strcmp(evhttp_uri_get_path(uri), "") == 0);
2359 tt_want(evhttp_uri_get_query(uri) == NULL);
2360 tt_want(strcmp(evhttp_uri_get_fragment(uri), "fr?ed") == 0);
2362 evhttp_uri_free(uri);
2366 http_uriencode_test(void *ptr)
2368 char *s=NULL, *s2=NULL;
2371 #define ENC(from,want,plus) do { \
2372 s = evhttp_uriencode((from), -1, (plus)); \
2374 tt_str_op(s,==,(want)); \
2376 s2 = evhttp_uridecode((s), (plus), &sz); \
2378 tt_str_op(s2,==,(from)); \
2379 tt_int_op(sz,==,strlen(from)); \
2385 #define DEC(from,want,dp) do { \
2386 s = evhttp_uridecode((from),(dp),&sz); \
2388 tt_str_op(s,==,(want)); \
2389 tt_int_op(sz,==,strlen(want)); \
2394 #define OLD_DEC(from,want) do { \
2395 s = evhttp_decode_uri((from)); \
2397 tt_str_op(s,==,(want)); \
2403 ENC("Hello", "Hello",0);
2407 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-.~_",
2408 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-.~_",0);
2411 ENC("\xff\xf0\xe0", "%FF%F0%E0",0);
2412 ENC("\x01\x19", "%01%19",1);
2413 ENC("http://www.ietf.org/rfc/rfc3986.txt",
2414 "http%3A%2F%2Fwww.ietf.org%2Frfc%2Frfc3986.txt",1);
2416 ENC("1+2=3", "1%2B2%3D3",1);
2417 ENC("1+2=3", "1%2B2%3D3",0);
2419 /* Now try encoding with internal NULs. */
2420 s = evhttp_uriencode("hello\0world", 11, 0);
2422 tt_str_op(s,==,"hello%00world");
2426 /* Now try out some decoding cases that we don't generate with
2427 * encode_uri: Make sure that malformed stuff doesn't crash... */
2428 DEC("%%xhello th+ere \xff",
2429 "%%xhello th+ere \xff", 0);
2430 /* Make sure plus decoding works */
2431 DEC("plus+should%20work+", "plus should work ",1);
2432 /* Try some lowercase hex */
2433 DEC("%f0%a0%b0", "\xf0\xa0\xb0",1);
2435 /* Try an internal NUL. */
2437 s = evhttp_uridecode("%00%00x%00%00", 1, &sz);
2439 tt_assert(!memcmp(s, "\0\0x\0\0", 5));
2443 /* Try with size == NULL */
2445 s = evhttp_uridecode("%00%00x%00%00", 1, NULL);
2446 tt_assert(!memcmp(s, "\0\0x\0\0", 5));
2450 /* Test out the crazy old behavior of the deprecated
2451 * evhttp_decode_uri */
2452 OLD_DEC("http://example.com/normal+path/?key=val+with+spaces",
2453 "http://example.com/normal+path/?key=val with spaces");
2466 http_base_test(void *ptr)
2468 struct event_base *base = NULL;
2469 struct bufferevent *bev;
2471 const char *http_request;
2472 ev_uint16_t port = 0;
2475 base = event_base_new();
2476 http = http_setup(&port, base);
2478 fd = http_connect("127.0.0.1", port);
2480 /* Stupid thing to send a request */
2481 bev = bufferevent_socket_new(base, fd, 0);
2482 bufferevent_setcb(bev, http_readcb, http_writecb,
2483 http_errorcb, base);
2484 bufferevent_base_set(base, bev);
2487 "GET /test HTTP/1.1\r\n"
2488 "Host: somehost\r\n"
2489 "Connection: close\r\n"
2492 bufferevent_write(bev, http_request, strlen(http_request));
2494 event_base_dispatch(base);
2496 bufferevent_free(bev);
2497 evutil_closesocket(fd);
2501 tt_int_op(test_ok, ==, 2);
2505 event_base_free(base);
2509 * the server is just going to close the connection if it times out during
2510 * reading the headers.
2514 http_incomplete_readcb(struct bufferevent *bev, void *arg)
2517 event_base_loopexit(exit_base,NULL);
2521 http_incomplete_errorcb(struct bufferevent *bev, short what, void *arg)
2523 if (what == (BEV_EVENT_READING|BEV_EVENT_EOF))
2527 event_base_loopexit(exit_base,NULL);
2531 http_incomplete_writecb(struct bufferevent *bev, void *arg)
2534 evutil_socket_t fd = *(evutil_socket_t *)arg;
2535 /* terminate the write side to simulate EOF */
2536 shutdown(fd, SHUT_WR);
2538 if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
2539 /* enable reading of the reply */
2540 bufferevent_enable(bev, EV_READ);
2546 _http_incomplete_test(struct basic_test_data *data, int use_timeout)
2548 struct bufferevent *bev;
2550 const char *http_request;
2551 ev_uint16_t port = 0;
2552 struct timeval tv_start, tv_end;
2554 exit_base = data->base;
2558 http = http_setup(&port, data->base);
2559 evhttp_set_timeout(http, 1);
2561 fd = http_connect("127.0.0.1", port);
2563 /* Stupid thing to send a request */
2564 bev = bufferevent_socket_new(data->base, fd, 0);
2565 bufferevent_setcb(bev,
2566 http_incomplete_readcb, http_incomplete_writecb,
2567 http_incomplete_errorcb, use_timeout ? NULL : &fd);
2570 "GET /test HTTP/1.1\r\n"
2571 "Host: somehost\r\n";
2573 bufferevent_write(bev, http_request, strlen(http_request));
2575 evutil_gettimeofday(&tv_start, NULL);
2577 event_base_dispatch(data->base);
2579 evutil_gettimeofday(&tv_end, NULL);
2580 evutil_timersub(&tv_end, &tv_start, &tv_end);
2582 bufferevent_free(bev);
2584 evutil_closesocket(fd);
2589 if (use_timeout && tv_end.tv_sec >= 3) {
2590 tt_abort_msg("time");
2591 } else if (!use_timeout && tv_end.tv_sec >= 1) {
2592 /* we should be done immediately */
2593 tt_abort_msg("time");
2596 tt_int_op(test_ok, ==, 2);
2601 http_incomplete_test(void *arg)
2603 _http_incomplete_test(arg, 0);
2606 http_incomplete_timeout_test(void *arg)
2608 _http_incomplete_test(arg, 1);
2612 * the server is going to reply with chunked data.
2616 http_chunked_readcb(struct bufferevent *bev, void *arg)
2622 http_chunked_errorcb(struct bufferevent *bev, short what, void *arg)
2629 if ((what & BEV_EVENT_EOF) != 0) {
2630 struct evhttp_request *req = evhttp_request_new(NULL, NULL);
2632 enum message_read_status done;
2634 /* req->kind = EVHTTP_RESPONSE; */
2635 done = evhttp_parse_firstline(req, bufferevent_get_input(bev));
2636 if (done != ALL_DATA_READ)
2639 done = evhttp_parse_headers(req, bufferevent_get_input(bev));
2640 if (done != ALL_DATA_READ)
2643 header = evhttp_find_header(evhttp_request_get_input_headers(req), "Transfer-Encoding");
2644 if (header == NULL || strcmp(header, "chunked"))
2647 header = evhttp_find_header(evhttp_request_get_input_headers(req), "Connection");
2648 if (header == NULL || strcmp(header, "close"))
2651 header = evbuffer_readln(bufferevent_get_input(bev), NULL, EVBUFFER_EOL_CRLF);
2655 if (strcmp(header, "d"))
2657 free((char*)header);
2659 if (strncmp((char *)evbuffer_pullup(bufferevent_get_input(bev), 13),
2660 "This is funny", 13))
2663 evbuffer_drain(bufferevent_get_input(bev), 13 + 2);
2665 header = evbuffer_readln(bufferevent_get_input(bev), NULL, EVBUFFER_EOL_CRLF);
2669 if (strcmp(header, "12"))
2671 free((char *)header);
2673 if (strncmp((char *)evbuffer_pullup(bufferevent_get_input(bev), 18),
2674 "but not hilarious.", 18))
2677 evbuffer_drain(bufferevent_get_input(bev), 18 + 2);
2679 header = evbuffer_readln(bufferevent_get_input(bev), NULL, EVBUFFER_EOL_CRLF);
2683 if (strcmp(header, "8"))
2685 free((char *)header);
2687 if (strncmp((char *)evbuffer_pullup(bufferevent_get_input(bev), 8),
2691 evbuffer_drain(bufferevent_get_input(bev), 8 + 2);
2693 header = evbuffer_readln(bufferevent_get_input(bev), NULL, EVBUFFER_EOL_CRLF);
2697 if (strcmp(header, "0"))
2699 free((char *)header);
2703 evhttp_request_free(req);
2707 event_base_loopexit(arg, NULL);
2711 http_chunked_writecb(struct bufferevent *bev, void *arg)
2713 if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
2714 /* enable reading of the reply */
2715 bufferevent_enable(bev, EV_READ);
2721 http_chunked_request_done(struct evhttp_request *req, void *arg)
2723 if (evhttp_request_get_response_code(req) != HTTP_OK) {
2724 fprintf(stderr, "FAILED\n");
2728 if (evhttp_find_header(evhttp_request_get_input_headers(req),
2729 "Transfer-Encoding") == NULL) {
2730 fprintf(stderr, "FAILED\n");
2734 if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != 13 + 18 + 8) {
2735 fprintf(stderr, "FAILED\n");
2739 if (strncmp((char *)evbuffer_pullup(evhttp_request_get_input_buffer(req), 13 + 18 + 8),
2740 "This is funnybut not hilarious.bwv 1052",
2742 fprintf(stderr, "FAILED\n");
2747 event_base_loopexit(arg, NULL);
2751 http_chunk_out_test(void *arg)
2753 struct basic_test_data *data = arg;
2754 struct bufferevent *bev;
2756 const char *http_request;
2757 ev_uint16_t port = 0;
2758 struct timeval tv_start, tv_end;
2759 struct evhttp_connection *evcon = NULL;
2760 struct evhttp_request *req = NULL;
2763 exit_base = data->base;
2766 http = http_setup(&port, data->base);
2768 fd = http_connect("127.0.0.1", port);
2770 /* Stupid thing to send a request */
2771 bev = bufferevent_socket_new(data->base, fd, 0);
2772 bufferevent_setcb(bev,
2773 http_chunked_readcb, http_chunked_writecb,
2774 http_chunked_errorcb, data->base);
2777 "GET /chunked HTTP/1.1\r\n"
2778 "Host: somehost\r\n"
2779 "Connection: close\r\n"
2782 bufferevent_write(bev, http_request, strlen(http_request));
2784 evutil_gettimeofday(&tv_start, NULL);
2786 event_base_dispatch(data->base);
2788 bufferevent_free(bev);
2790 evutil_gettimeofday(&tv_end, NULL);
2791 evutil_timersub(&tv_end, &tv_start, &tv_end);
2793 tt_int_op(tv_end.tv_sec, <, 1);
2795 tt_int_op(test_ok, ==, 2);
2797 /* now try again with the regular connection object */
2798 evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
2801 /* make two requests to check the keepalive behavior */
2802 for (i = 0; i < 2; i++) {
2804 req = evhttp_request_new(http_chunked_request_done,data->base);
2806 /* Add the information that we care about */
2807 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
2809 /* We give ownership of the request to the connection */
2810 if (evhttp_make_request(evcon, req,
2811 EVHTTP_REQ_GET, "/chunked") == -1) {
2812 tt_abort_msg("Couldn't make request");
2815 event_base_dispatch(data->base);
2817 tt_assert(test_ok == 1);
2822 evhttp_connection_free(evcon);
2828 http_stream_out_test(void *arg)
2830 struct basic_test_data *data = arg;
2831 ev_uint16_t port = 0;
2832 struct evhttp_connection *evcon = NULL;
2833 struct evhttp_request *req = NULL;
2836 exit_base = data->base;
2838 http = http_setup(&port, data->base);
2840 evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
2844 * At this point, we want to schedule a request to the HTTP
2845 * server using our make request method.
2848 req = evhttp_request_new(http_request_done,
2849 (void *)"This is funnybut not hilarious.bwv 1052");
2851 /* Add the information that we care about */
2852 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
2854 /* We give ownership of the request to the connection */
2855 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/streamed")
2857 tt_abort_msg("Couldn't make request");
2860 event_base_dispatch(data->base);
2864 evhttp_connection_free(evcon);
2870 http_stream_in_chunk(struct evhttp_request *req, void *arg)
2872 struct evbuffer *reply = arg;
2874 if (evhttp_request_get_response_code(req) != HTTP_OK) {
2875 fprintf(stderr, "FAILED\n");
2879 evbuffer_add_buffer(reply, evhttp_request_get_input_buffer(req));
2883 http_stream_in_done(struct evhttp_request *req, void *arg)
2885 if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != 0) {
2886 fprintf(stderr, "FAILED\n");
2890 event_base_loopexit(exit_base, NULL);
2894 * Makes a request and reads the response in chunks.
2897 _http_stream_in_test(struct basic_test_data *data, char const *url,
2898 size_t expected_len, char const *expected)
2900 struct evhttp_connection *evcon;
2901 struct evbuffer *reply = evbuffer_new();
2902 struct evhttp_request *req = NULL;
2903 ev_uint16_t port = 0;
2905 exit_base = data->base;
2906 http = http_setup(&port, data->base);
2908 evcon = evhttp_connection_base_new(data->base, NULL,"127.0.0.1", port);
2911 req = evhttp_request_new(http_stream_in_done, reply);
2912 evhttp_request_set_chunked_cb(req, http_stream_in_chunk);
2914 /* We give ownership of the request to the connection */
2915 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, url) == -1) {
2916 tt_abort_msg("Couldn't make request");
2919 event_base_dispatch(data->base);
2921 if (evbuffer_get_length(reply) != expected_len) {
2922 TT_DIE(("reply length %lu; expected %lu; FAILED (%s)\n",
2923 (unsigned long)evbuffer_get_length(reply),
2924 (unsigned long)expected_len,
2925 (char*)evbuffer_pullup(reply, -1)));
2928 if (memcmp(evbuffer_pullup(reply, -1), expected, expected_len) != 0) {
2929 tt_abort_msg("Memory mismatch");
2935 evbuffer_free(reply);
2937 evhttp_connection_free(evcon);
2943 http_stream_in_test(void *arg)
2945 _http_stream_in_test(arg, "/chunked", 13 + 18 + 8,
2946 "This is funnybut not hilarious.bwv 1052");
2948 _http_stream_in_test(arg, "/test", strlen(BASIC_REQUEST_BODY),
2949 BASIC_REQUEST_BODY);
2953 http_stream_in_cancel_chunk(struct evhttp_request *req, void *arg)
2955 tt_int_op(evhttp_request_get_response_code(req), ==, HTTP_OK);
2958 evhttp_cancel_request(req);
2959 event_base_loopexit(arg, NULL);
2963 http_stream_in_cancel_done(struct evhttp_request *req, void *arg)
2965 /* should never be called */
2966 tt_fail_msg("In cancel done");
2970 http_stream_in_cancel_test(void *arg)
2972 struct basic_test_data *data = arg;
2973 struct evhttp_connection *evcon;
2974 struct evhttp_request *req = NULL;
2975 ev_uint16_t port = 0;
2977 http = http_setup(&port, data->base);
2979 evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
2982 req = evhttp_request_new(http_stream_in_cancel_done, data->base);
2983 evhttp_request_set_chunked_cb(req, http_stream_in_cancel_chunk);
2985 /* We give ownership of the request to the connection */
2986 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/chunked") == -1) {
2987 tt_abort_msg("Couldn't make request");
2990 event_base_dispatch(data->base);
2994 evhttp_connection_free(evcon);
3000 http_connection_retry_done(struct evhttp_request *req, void *arg)
3003 tt_int_op(evhttp_request_get_response_code(req), !=, HTTP_OK);
3004 if (evhttp_find_header(evhttp_request_get_input_headers(req), "Content-Type") != NULL) {
3005 tt_abort_msg("(content type)\n");
3008 tt_uint_op(evbuffer_get_length(evhttp_request_get_input_buffer(req)), ==, 0);
3012 event_base_loopexit(arg,NULL);
3015 static struct event_base *http_make_web_server_base=NULL;
3017 http_make_web_server(evutil_socket_t fd, short what, void *arg)
3019 ev_uint16_t port = *(ev_uint16_t*)arg;
3020 http = http_setup(&port, http_make_web_server_base);
3024 http_connection_retry_test(void *arg)
3026 struct basic_test_data *data = arg;
3027 ev_uint16_t port = 0;
3028 struct evhttp_connection *evcon = NULL;
3029 struct evhttp_request *req = NULL;
3030 struct timeval tv, tv_start, tv_end;
3032 exit_base = data->base;
3035 /* auto detect a port */
3036 http = http_setup(&port, data->base);
3040 evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
3043 evhttp_connection_set_timeout(evcon, 1);
3044 /* also bind to local host */
3045 evhttp_connection_set_local_address(evcon, "127.0.0.1");
3048 * At this point, we want to schedule an HTTP GET request
3049 * server using our make request method.
3052 req = evhttp_request_new(http_connection_retry_done, data->base);
3055 /* Add the information that we care about */
3056 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
3058 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
3059 "/?arg=val") == -1) {
3060 tt_abort_msg("Couldn't make request");
3063 evutil_gettimeofday(&tv_start, NULL);
3064 event_base_dispatch(data->base);
3065 evutil_gettimeofday(&tv_end, NULL);
3066 evutil_timersub(&tv_end, &tv_start, &tv_end);
3067 tt_int_op(tv_end.tv_sec, <, 1);
3069 tt_int_op(test_ok, ==, 1);
3072 * now test the same but with retries
3076 evhttp_connection_set_timeout(evcon, 1);
3077 evhttp_connection_set_retries(evcon, 1);
3079 req = evhttp_request_new(http_connection_retry_done, data->base);
3082 /* Add the information that we care about */
3083 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
3085 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
3086 "/?arg=val") == -1) {
3087 tt_abort_msg("Couldn't make request");
3090 evutil_gettimeofday(&tv_start, NULL);
3091 event_base_dispatch(data->base);
3092 evutil_gettimeofday(&tv_end, NULL);
3093 evutil_timersub(&tv_end, &tv_start, &tv_end);
3094 tt_int_op(tv_end.tv_sec, >, 1);
3095 tt_int_op(tv_end.tv_sec, <, 6);
3097 tt_assert(test_ok == 1);
3100 * now test the same but with retries and give it a web server
3105 evhttp_connection_set_timeout(evcon, 1);
3106 evhttp_connection_set_retries(evcon, 3);
3108 req = evhttp_request_new(http_dispatcher_test_done, data->base);
3111 /* Add the information that we care about */
3112 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
3114 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
3115 "/?arg=val") == -1) {
3116 tt_abort_msg("Couldn't make request");
3119 /* start up a web server one second after the connection tried
3122 evutil_timerclear(&tv);
3124 http_make_web_server_base = data->base;
3125 event_base_once(data->base, -1, EV_TIMEOUT, http_make_web_server, &port, &tv);
3127 evutil_gettimeofday(&tv_start, NULL);
3128 event_base_dispatch(data->base);
3129 evutil_gettimeofday(&tv_end, NULL);
3131 evutil_timersub(&tv_end, &tv_start, &tv_end);
3133 tt_int_op(tv_end.tv_sec, >, 1);
3134 tt_int_op(tv_end.tv_sec, <, 6);
3136 tt_int_op(test_ok, ==, 1);
3140 evhttp_connection_free(evcon);
3146 http_primitives(void *ptr)
3148 char *escaped = NULL;
3149 struct evhttp *http;
3151 escaped = evhttp_htmlescape("<script>");
3152 tt_str_op(escaped, ==, "<script>");
3155 escaped = evhttp_htmlescape("\"\'&");
3156 tt_str_op(escaped, ==, ""'&");
3158 http = evhttp_new(NULL);
3159 tt_int_op(evhttp_set_cb(http, "/test", http_basic_cb, NULL), ==, 0);
3160 tt_int_op(evhttp_set_cb(http, "/test", http_basic_cb, NULL), ==, -1);
3161 tt_int_op(evhttp_del_cb(http, "/test"), ==, 0);
3162 tt_int_op(evhttp_del_cb(http, "/test"), ==, -1);
3163 tt_int_op(evhttp_set_cb(http, "/test", http_basic_cb, NULL), ==, 0);
3172 http_multi_line_header_test(void *arg)
3174 struct basic_test_data *data = arg;
3175 struct bufferevent *bev= NULL;
3176 evutil_socket_t fd = -1;
3177 const char *http_start_request;
3178 ev_uint16_t port = 0;
3182 http = http_setup(&port, data->base);
3184 fd = http_connect("127.0.0.1", port);
3186 /* Stupid thing to send a request */
3187 bev = bufferevent_socket_new(data->base, fd, 0);
3188 bufferevent_setcb(bev, http_readcb, http_writecb,
3189 http_errorcb, data->base);
3191 http_start_request =
3192 "GET /test HTTP/1.1\r\n"
3193 "Host: somehost\r\n"
3194 "Connection: close\r\n"
3195 "X-Multi: aaaaaaaa\r\n"
3201 bufferevent_write(bev, http_start_request, strlen(http_start_request));
3203 event_base_dispatch(data->base);
3205 tt_int_op(test_ok, ==, 4);
3208 bufferevent_free(bev);
3210 evutil_closesocket(fd);
3216 http_request_bad(struct evhttp_request *req, void *arg)
3219 fprintf(stderr, "FAILED\n");
3224 event_base_loopexit(arg, NULL);
3228 http_negative_content_length_test(void *arg)
3230 struct basic_test_data *data = arg;
3231 ev_uint16_t port = 0;
3232 struct evhttp_connection *evcon = NULL;
3233 struct evhttp_request *req = NULL;
3237 http = http_setup(&port, data->base);
3239 evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
3243 * At this point, we want to schedule a request to the HTTP
3244 * server using our make request method.
3247 req = evhttp_request_new(http_request_bad, data->base);
3249 /* Cause the response to have a negative content-length */
3250 evhttp_add_header(evhttp_request_get_output_headers(req), "X-Negative", "makeitso");
3252 /* We give ownership of the request to the connection */
3253 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
3254 tt_abort_msg("Couldn't make request");
3257 event_base_dispatch(data->base);
3261 evhttp_connection_free(evcon);
3268 http_data_length_constraints_test_done(struct evhttp_request *req, void *arg)
3271 tt_int_op(evhttp_request_get_response_code(req), ==, HTTP_BADREQUEST);
3273 event_base_loopexit(arg, NULL);
3277 http_large_entity_test_done(struct evhttp_request *req, void *arg)
3280 tt_int_op(evhttp_request_get_response_code(req), ==, HTTP_ENTITYTOOLARGE);
3282 event_base_loopexit(arg, NULL);
3286 http_data_length_constraints_test(void *arg)
3288 struct basic_test_data *data = arg;
3289 ev_uint16_t port = 0;
3290 struct evhttp_connection *evcon = NULL;
3291 struct evhttp_request *req = NULL;
3292 char long_str[8192];
3296 http = http_setup(&port, data->base);
3298 evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
3301 /* also bind to local host */
3302 evhttp_connection_set_local_address(evcon, "127.0.0.1");
3305 * At this point, we want to schedule an HTTP GET request
3306 * server using our make request method.
3309 req = evhttp_request_new(http_data_length_constraints_test_done, data->base);
3312 memset(long_str, 'a', 8192);
3313 long_str[8191] = '\0';
3314 /* Add the information that we care about */
3315 evhttp_set_max_headers_size(http, 8191);
3316 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
3317 evhttp_add_header(evhttp_request_get_output_headers(req), "Longheader", long_str);
3319 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/?arg=val") == -1) {
3320 tt_abort_msg("Couldn't make request");
3322 event_base_dispatch(data->base);
3324 req = evhttp_request_new(http_data_length_constraints_test_done, data->base);
3326 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
3328 /* GET /?arg=verylongvalue HTTP/1.1 */
3329 if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, long_str) == -1) {
3330 tt_abort_msg("Couldn't make request");
3332 event_base_dispatch(data->base);
3334 evhttp_set_max_body_size(http, 8190);
3335 req = evhttp_request_new(http_data_length_constraints_test_done, data->base);
3336 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
3337 evbuffer_add_printf(evhttp_request_get_output_buffer(req), "%s", long_str);
3338 if (evhttp_make_request(evcon, req, EVHTTP_REQ_POST, "/") == -1) {
3339 tt_abort_msg("Couldn't make request");
3341 event_base_dispatch(data->base);
3343 req = evhttp_request_new(http_large_entity_test_done, data->base);
3344 evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
3345 evhttp_add_header(evhttp_request_get_output_headers(req), "Expect", "100-continue");
3346 evbuffer_add_printf(evhttp_request_get_output_buffer(req), "%s", long_str);
3347 if (evhttp_make_request(evcon, req, EVHTTP_REQ_POST, "/") == -1) {
3348 tt_abort_msg("Couldn't make request");
3350 event_base_dispatch(data->base);
3355 evhttp_connection_free(evcon);
3361 * Testing client reset of server chunked connections
3364 struct terminate_state {
3365 struct event_base *base;
3366 struct evhttp_request *req;
3367 struct bufferevent *bev;
3373 terminate_chunked_trickle_cb(evutil_socket_t fd, short events, void *arg)
3375 struct terminate_state *state = arg;
3376 struct evbuffer *evb;
3379 if (evhttp_request_get_connection(state->req) == NULL) {
3381 evhttp_request_free(state->req);
3382 event_base_loopexit(state->base,NULL);
3386 evb = evbuffer_new();
3387 evbuffer_add_printf(evb, "%p", evb);
3388 evhttp_send_reply_chunk(state->req, evb);
3393 EVUTIL_ASSERT(state);
3394 EVUTIL_ASSERT(state->base);
3395 event_base_once(state->base, -1, EV_TIMEOUT, terminate_chunked_trickle_cb, arg, &tv);
3399 terminate_chunked_close_cb(struct evhttp_connection *evcon, void *arg)
3401 struct terminate_state *state = arg;
3402 state->gotclosecb = 1;
3406 terminate_chunked_cb(struct evhttp_request *req, void *arg)
3408 struct terminate_state *state = arg;
3411 /* we want to know if this connection closes on us */
3412 evhttp_connection_set_closecb(
3413 evhttp_request_get_connection(req),
3414 terminate_chunked_close_cb, arg);
3418 evhttp_send_reply_start(req, HTTP_OK, "OK");
3422 event_base_once(state->base, -1, EV_TIMEOUT, terminate_chunked_trickle_cb, arg, &tv);
3426 terminate_chunked_client(evutil_socket_t fd, short event, void *arg)
3428 struct terminate_state *state = arg;
3429 bufferevent_free(state->bev);
3430 evutil_closesocket(state->fd);
3434 terminate_readcb(struct bufferevent *bev, void *arg)
3436 /* just drop the data */
3437 evbuffer_drain(bufferevent_get_input(bev), -1);
3442 http_terminate_chunked_test(void *arg)
3444 struct basic_test_data *data = arg;
3445 struct bufferevent *bev = NULL;
3447 const char *http_request;
3448 ev_uint16_t port = 0;
3449 evutil_socket_t fd = -1;
3450 struct terminate_state terminate_state;
3454 http = http_setup(&port, data->base);
3455 evhttp_del_cb(http, "/test");
3456 tt_assert(evhttp_set_cb(http, "/test",
3457 terminate_chunked_cb, &terminate_state) == 0);
3459 fd = http_connect("127.0.0.1", port);
3461 /* Stupid thing to send a request */
3462 bev = bufferevent_socket_new(data->base, fd, 0);
3463 bufferevent_setcb(bev, terminate_readcb, http_writecb,
3464 http_errorcb, data->base);
3466 memset(&terminate_state, 0, sizeof(terminate_state));
3467 terminate_state.base = data->base;
3468 terminate_state.fd = fd;
3469 terminate_state.bev = bev;
3470 terminate_state.gotclosecb = 0;
3472 /* first half of the http request */
3474 "GET /test HTTP/1.1\r\n"
3475 "Host: some\r\n\r\n";
3477 bufferevent_write(bev, http_request, strlen(http_request));
3478 evutil_timerclear(&tv);
3480 event_base_once(data->base, -1, EV_TIMEOUT, terminate_chunked_client, &terminate_state,
3483 event_base_dispatch(data->base);
3485 if (terminate_state.gotclosecb == 0)
3490 evutil_closesocket(fd);
3495 #define HTTP_LEGACY(name) \
3496 { #name, run_legacy_test_fn, TT_ISOLATED|TT_LEGACY, &legacy_setup, \
3497 http_##name##_test }
3499 #define HTTP(name) \
3500 { #name, http_##name##_test, TT_ISOLATED, &basic_setup, NULL }
3502 struct testcase_t http_testcases[] = {
3503 { "primitives", http_primitives, 0, NULL, NULL },
3504 { "base", http_base_test, TT_FORK, NULL, NULL },
3505 { "bad_headers", http_bad_header_test, 0, NULL, NULL },
3506 { "parse_query", http_parse_query_test, 0, NULL, NULL },
3507 { "parse_uri", http_parse_uri_test, 0, NULL, NULL },
3508 { "uriencode", http_uriencode_test, 0, NULL, NULL },
3515 HTTP(allowed_methods),
3518 HTTP(persist_connection),
3519 HTTP(connection_async),
3520 HTTP(close_detection),
3521 HTTP(close_detection_delay),
3524 HTTP(incomplete_timeout),
3525 HTTP(terminate_chunked),
3529 HTTP(multi_line_header),
3530 HTTP(negative_content_length),
3535 HTTP(stream_in_cancel),
3537 HTTP(connection_retry),
3538 HTTP(data_length_constraints),