1 /* $NetBSD: regress_http.c,v 1.4 2015/01/29 07:26:02 spz Exp $ */
3 * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
4 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "event2/event-config.h"
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: regress_http.c,v 1.4 2015/01/29 07:26:02 spz Exp $");
39 #include <sys/types.h>
41 #ifdef _EVENT_HAVE_SYS_TIME_H
44 #include <sys/queue.h>
46 #include <sys/socket.h>
57 #include "event2/dns.h"
59 #include "event2/event.h"
60 #include "event2/http.h"
61 #include "event2/buffer.h"
62 #include "event2/bufferevent.h"
63 #include "event2/util.h"
64 #include "log-internal.h"
65 #include "util-internal.h"
66 #include "http-internal.h"
68 #include "regress_testutils.h"
70 static struct evhttp
*http
;
71 /* set if a test needs to call loopexit on a base */
72 static struct event_base
*exit_base
;
74 static char const BASIC_REQUEST_BODY
[] = "This is funny";
75 static void *basic_request_body
= __UNCONST(BASIC_REQUEST_BODY
);
77 static void http_basic_cb(struct evhttp_request
*req
, void *arg
);
78 static void http_chunked_cb(struct evhttp_request
*req
, void *arg
);
79 static void http_post_cb(struct evhttp_request
*req
, void *arg
);
80 static void http_put_cb(struct evhttp_request
*req
, void *arg
);
81 static void http_delete_cb(struct evhttp_request
*req
, void *arg
);
82 static void http_delay_cb(struct evhttp_request
*req
, void *arg
);
83 static void http_large_delay_cb(struct evhttp_request
*req
, void *arg
);
84 static void http_badreq_cb(struct evhttp_request
*req
, void *arg
);
85 static void http_dispatcher_cb(struct evhttp_request
*req
, void *arg
);
87 http_bind(struct evhttp
*myhttp
, ev_uint16_t
*pport
)
90 struct evhttp_bound_socket
*sock
;
92 sock
= evhttp_bind_socket_with_handle(myhttp
, "127.0.0.1", *pport
);
94 event_errx(1, "Could not start web server");
96 port
= regress_get_socket_port(evhttp_bound_socket_get_fd(sock
));
99 *pport
= (ev_uint16_t
) port
;
104 static struct evhttp
*
105 http_setup(ev_uint16_t
*pport
, struct event_base
*base
)
107 struct evhttp
*myhttp
;
109 /* Try a few different ports */
110 myhttp
= evhttp_new(base
);
112 if (http_bind(myhttp
, pport
) < 0)
115 /* Register a callback for certain types of requests */
116 evhttp_set_cb(myhttp
, "/test", http_basic_cb
, base
);
117 evhttp_set_cb(myhttp
, "/chunked", http_chunked_cb
, base
);
118 evhttp_set_cb(myhttp
, "/streamed", http_chunked_cb
, base
);
119 evhttp_set_cb(myhttp
, "/postit", http_post_cb
, base
);
120 evhttp_set_cb(myhttp
, "/putit", http_put_cb
, base
);
121 evhttp_set_cb(myhttp
, "/deleteit", http_delete_cb
, base
);
122 evhttp_set_cb(myhttp
, "/delay", http_delay_cb
, base
);
123 evhttp_set_cb(myhttp
, "/largedelay", http_large_delay_cb
, base
);
124 evhttp_set_cb(myhttp
, "/badrequest", http_badreq_cb
, base
);
125 evhttp_set_cb(myhttp
, "/", http_dispatcher_cb
, base
);
130 #define NI_MAXSERV 1024
133 static evutil_socket_t
134 http_connect(const char *address
, u_short port
)
136 /* Stupid code for connecting */
137 struct evutil_addrinfo ai
, *aitop
;
138 char strport
[NI_MAXSERV
];
144 memset(&ai
, 0, sizeof(ai
));
145 ai
.ai_family
= AF_INET
;
146 ai
.ai_socktype
= SOCK_STREAM
;
147 evutil_snprintf(strport
, sizeof(strport
), "%d", port
);
148 if (evutil_getaddrinfo(address
, strport
, &ai
, &aitop
) != 0) {
149 event_warn("getaddrinfo");
153 slen
= aitop
->ai_addrlen
;
155 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
157 event_err(1, "socket failed");
159 evutil_make_socket_nonblocking(fd
);
160 if (connect(fd
, sa
, slen
) == -1) {
162 int tmp_err
= WSAGetLastError();
163 if (tmp_err
!= WSAEINPROGRESS
&& tmp_err
!= WSAEINVAL
&&
164 tmp_err
!= WSAEWOULDBLOCK
)
165 event_err(1, "connect failed");
167 if (errno
!= EINPROGRESS
)
168 event_err(1, "connect failed");
172 evutil_freeaddrinfo(aitop
);
177 /* Helper: do a strcmp on the contents of buf and the string s. */
179 evbuffer_datacmp(struct evbuffer
*buf
, const char *s
)
181 size_t b_sz
= evbuffer_get_length(buf
);
182 size_t s_sz
= strlen(s
);
189 d
= evbuffer_pullup(buf
, s_sz
);
190 if ((r
= memcmp(d
, s
, s_sz
)))
199 /* Helper: Return true iff buf contains s */
201 evbuffer_contains(struct evbuffer
*buf
, const char *s
)
203 struct evbuffer_ptr ptr
;
204 ptr
= evbuffer_search(buf
, s
, strlen(s
), NULL
);
205 return ptr
.pos
!= -1;
209 http_readcb(struct bufferevent
*bev
, void *arg
)
211 const char *what
= BASIC_REQUEST_BODY
;
212 struct event_base
*my_base
= arg
;
214 if (evbuffer_contains(bufferevent_get_input(bev
), what
)) {
215 struct evhttp_request
*req
= evhttp_request_new(NULL
, NULL
);
216 enum message_read_status done
;
218 /* req->kind = EVHTTP_RESPONSE; */
219 done
= evhttp_parse_firstline(req
, bufferevent_get_input(bev
));
220 if (done
!= ALL_DATA_READ
)
223 done
= evhttp_parse_headers(req
, bufferevent_get_input(bev
));
224 if (done
!= ALL_DATA_READ
)
228 evhttp_find_header(evhttp_request_get_input_headers(req
),
229 "Content-Type") != NULL
)
233 evhttp_request_free(req
);
234 bufferevent_disable(bev
, EV_READ
);
236 event_base_loopexit(exit_base
, NULL
);
238 event_base_loopexit(my_base
, NULL
);
240 fprintf(stderr
, "No way to exit loop!\n");
247 http_writecb(struct bufferevent
*bev
, void *arg
)
249 if (evbuffer_get_length(bufferevent_get_output(bev
)) == 0) {
250 /* enable reading of the reply */
251 bufferevent_enable(bev
, EV_READ
);
257 http_errorcb(struct bufferevent
*bev
, short what
, void *arg
)
260 event_base_loopexit(arg
, NULL
);
264 http_basic_cb(struct evhttp_request
*req
, void *arg
)
266 struct evbuffer
*evb
= evbuffer_new();
267 int empty
= evhttp_find_header(evhttp_request_get_input_headers(req
), "Empty") != NULL
;
268 event_debug(("%s: called\n", __func__
));
269 evbuffer_add_printf(evb
, BASIC_REQUEST_BODY
);
271 /* For multi-line headers test */
274 evhttp_find_header(evhttp_request_get_input_headers(req
),"X-multi");
276 if (strcmp("END", multi
+ strlen(multi
) - 3) == 0)
278 if (evhttp_find_header(evhttp_request_get_input_headers(req
), "X-Last"))
283 /* injecting a bad content-length */
284 if (evhttp_find_header(evhttp_request_get_input_headers(req
), "X-Negative"))
285 evhttp_add_header(evhttp_request_get_output_headers(req
),
286 "Content-Length", "-100");
288 /* allow sending of an empty reply */
289 evhttp_send_reply(req
, HTTP_OK
, "Everything is fine",
290 !empty
? evb
: NULL
);
295 static char const* const CHUNKS
[] = {
297 "but not hilarious.",
301 struct chunk_req_state
{
302 struct event_base
*base
;
303 struct evhttp_request
*req
;
308 http_chunked_trickle_cb(evutil_socket_t fd
, short events
, void *arg
)
310 struct evbuffer
*evb
= evbuffer_new();
311 struct chunk_req_state
*state
= arg
;
312 struct timeval when
= { 0, 0 };
314 evbuffer_add_printf(evb
, "%s", CHUNKS
[state
->i
]);
315 evhttp_send_reply_chunk(state
->req
, evb
);
318 if (++state
->i
< (int) (sizeof(CHUNKS
)/sizeof(CHUNKS
[0]))) {
319 event_base_once(state
->base
, -1, EV_TIMEOUT
,
320 http_chunked_trickle_cb
, state
, &when
);
322 evhttp_send_reply_end(state
->req
);
328 http_chunked_cb(struct evhttp_request
*req
, void *arg
)
330 struct timeval when
= { 0, 0 };
331 struct chunk_req_state
*state
= malloc(sizeof(struct chunk_req_state
));
332 event_debug(("%s: called\n", __func__
));
334 memset(state
, 0, sizeof(struct chunk_req_state
));
338 if (strcmp(evhttp_request_get_uri(req
), "/streamed") == 0) {
339 evhttp_add_header(evhttp_request_get_output_headers(req
), "Content-Length", "39");
342 /* generate a chunked/streamed reply */
343 evhttp_send_reply_start(req
, HTTP_OK
, "Everything is fine");
345 /* but trickle it across several iterations to ensure we're not
346 * assuming it comes all at once */
347 event_base_once(arg
, -1, EV_TIMEOUT
, http_chunked_trickle_cb
, state
, &when
);
351 http_complete_write(evutil_socket_t fd
, short what
, void *arg
)
353 struct bufferevent
*bev
= arg
;
354 const char *http_request
= "host\r\n"
355 "Connection: close\r\n"
357 bufferevent_write(bev
, http_request
, strlen(http_request
));
361 http_basic_test(void *arg
)
363 struct basic_test_data
*data
= arg
;
365 struct bufferevent
*bev
;
367 const char *http_request
;
368 ev_uint16_t port
= 0, port2
= 0;
372 http
= http_setup(&port
, data
->base
);
374 /* bind to a second socket */
375 if (http_bind(http
, &port2
) == -1) {
376 fprintf(stdout
, "FAILED (bind)\n");
380 fd
= http_connect("127.0.0.1", port
);
382 /* Stupid thing to send a request */
383 bev
= bufferevent_socket_new(data
->base
, fd
, 0);
384 bufferevent_setcb(bev
, http_readcb
, http_writecb
,
385 http_errorcb
, data
->base
);
387 /* first half of the http request */
389 "GET /test HTTP/1.1\r\n"
392 bufferevent_write(bev
, http_request
, strlen(http_request
));
393 evutil_timerclear(&tv
);
395 event_base_once(data
->base
,
396 -1, EV_TIMEOUT
, http_complete_write
, bev
, &tv
);
398 event_base_dispatch(data
->base
);
400 tt_assert(test_ok
== 3);
402 /* connect to the second port */
403 bufferevent_free(bev
);
404 evutil_closesocket(fd
);
406 fd
= http_connect("127.0.0.1", port2
);
408 /* Stupid thing to send a request */
409 bev
= bufferevent_socket_new(data
->base
, fd
, 0);
410 bufferevent_setcb(bev
, http_readcb
, http_writecb
,
411 http_errorcb
, data
->base
);
414 "GET /test HTTP/1.1\r\n"
416 "Connection: close\r\n"
419 bufferevent_write(bev
, http_request
, strlen(http_request
));
421 event_base_dispatch(data
->base
);
423 tt_assert(test_ok
== 5);
425 /* Connect to the second port again. This time, send an absolute uri. */
426 bufferevent_free(bev
);
427 evutil_closesocket(fd
);
429 fd
= http_connect("127.0.0.1", port2
);
431 /* Stupid thing to send a request */
432 bev
= bufferevent_socket_new(data
->base
, fd
, 0);
433 bufferevent_setcb(bev
, http_readcb
, http_writecb
,
434 http_errorcb
, data
->base
);
437 "GET http://somehost.net/test HTTP/1.1\r\n"
439 "Connection: close\r\n"
442 bufferevent_write(bev
, http_request
, strlen(http_request
));
444 event_base_dispatch(data
->base
);
446 tt_assert(test_ok
== 7);
454 http_delay_reply(evutil_socket_t fd
, short what
, void *arg
)
456 struct evhttp_request
*req
= arg
;
458 evhttp_send_reply(req
, HTTP_OK
, "Everything is fine", NULL
);
464 http_delay_cb(struct evhttp_request
*req
, void *arg
)
467 evutil_timerclear(&tv
);
469 tv
.tv_usec
= 200 * 1000;
471 event_base_once(arg
, -1, EV_TIMEOUT
, http_delay_reply
, req
, &tv
);
475 http_badreq_cb(struct evhttp_request
*req
, void *arg
)
477 struct evbuffer
*buf
= evbuffer_new();
479 evhttp_add_header(evhttp_request_get_output_headers(req
), "Content-Type", "text/xml; charset=UTF-8");
480 evbuffer_add_printf(buf
, "Hello, %s!", "127.0.0.1");
482 evhttp_send_reply(req
, HTTP_OK
, "OK", buf
);
487 http_badreq_errorcb(struct bufferevent
*bev
, short what
, void *arg
)
489 event_debug(("%s: called (what=%04x, arg=%p)", __func__
, what
, arg
));
495 #define SHUT_WR SD_SEND
502 http_badreq_readcb(struct bufferevent
*bev
, void *arg
)
504 const char *what
= "Hello, 127.0.0.1";
505 const char *bad_request
= "400 Bad Request";
507 if (evbuffer_contains(bufferevent_get_input(bev
), bad_request
)) {
508 TT_FAIL(("%s:bad request detected", __func__
));
509 bufferevent_disable(bev
, EV_READ
);
510 event_base_loopexit(arg
, NULL
);
514 if (evbuffer_contains(bufferevent_get_input(bev
), what
)) {
515 struct evhttp_request
*req
= evhttp_request_new(NULL
, NULL
);
516 enum message_read_status done
;
518 /* req->kind = EVHTTP_RESPONSE; */
519 done
= evhttp_parse_firstline(req
, bufferevent_get_input(bev
));
520 if (done
!= ALL_DATA_READ
)
523 done
= evhttp_parse_headers(req
, bufferevent_get_input(bev
));
524 if (done
!= ALL_DATA_READ
)
528 evhttp_find_header(evhttp_request_get_input_headers(req
),
529 "Content-Type") != NULL
)
533 evhttp_request_free(req
);
534 evbuffer_drain(bufferevent_get_input(bev
), evbuffer_get_length(bufferevent_get_input(bev
)));
537 shutdown(bufferevent_getfd(bev
), SHUT_WR
);
541 http_badreq_successcb(evutil_socket_t fd
, short what
, void *arg
)
543 event_debug(("%s: called (what=%04x, arg=%p)", __func__
, what
, arg
));
544 event_base_loopexit(exit_base
, NULL
);
548 http_bad_request_test(void *arg
)
550 struct basic_test_data
*data
= arg
;
552 struct bufferevent
*bev
= NULL
;
554 const char *http_request
;
555 ev_uint16_t port
=0, port2
=0;
558 exit_base
= data
->base
;
560 http
= http_setup(&port
, data
->base
);
562 /* bind to a second socket */
563 if (http_bind(http
, &port2
) == -1)
564 TT_DIE(("Bind socket failed"));
566 /* NULL request test */
567 fd
= http_connect("127.0.0.1", port
);
569 /* Stupid thing to send a request */
570 bev
= bufferevent_socket_new(data
->base
, fd
, 0);
571 bufferevent_setcb(bev
, http_badreq_readcb
, http_writecb
,
572 http_badreq_errorcb
, data
->base
);
573 bufferevent_enable(bev
, EV_READ
);
575 /* real NULL request */
578 bufferevent_write(bev
, http_request
, strlen(http_request
));
580 shutdown(fd
, SHUT_WR
);
583 event_base_once(data
->base
, -1, EV_TIMEOUT
, http_badreq_successcb
, bev
, &tv
);
585 event_base_dispatch(data
->base
);
587 bufferevent_free(bev
);
588 evutil_closesocket(fd
);
591 fprintf(stdout
, "FAILED\n");
595 /* Second answer (BAD REQUEST) on connection close */
597 /* connect to the second port */
598 fd
= http_connect("127.0.0.1", port2
);
600 /* Stupid thing to send a request */
601 bev
= bufferevent_socket_new(data
->base
, fd
, 0);
602 bufferevent_setcb(bev
, http_badreq_readcb
, http_writecb
,
603 http_badreq_errorcb
, data
->base
);
604 bufferevent_enable(bev
, EV_READ
);
606 /* first half of the http request */
608 "GET /badrequest HTTP/1.0\r\n" \
609 "Connection: Keep-Alive\r\n" \
612 bufferevent_write(bev
, http_request
, strlen(http_request
));
616 event_base_once(data
->base
, -1, EV_TIMEOUT
, http_badreq_successcb
, bev
, &tv
);
618 event_base_dispatch(data
->base
);
620 tt_int_op(test_ok
, ==, 2);
625 bufferevent_free(bev
);
628 static struct evhttp_connection
*delayed_client
;
631 http_large_delay_cb(struct evhttp_request
*req
, void *arg
)
634 evutil_timerclear(&tv
);
637 event_base_once(arg
, -1, EV_TIMEOUT
, http_delay_reply
, req
, &tv
);
638 evhttp_connection_fail(delayed_client
, EVCON_HTTP_EOF
);
642 * HTTP DELETE test, just piggyback on the basic test
646 http_delete_cb(struct evhttp_request
*req
, void *arg
)
648 struct evbuffer
*evb
= evbuffer_new();
649 int empty
= evhttp_find_header(evhttp_request_get_input_headers(req
), "Empty") != NULL
;
651 /* Expecting a DELETE request */
652 if (evhttp_request_get_command(req
) != EVHTTP_REQ_DELETE
) {
653 fprintf(stdout
, "FAILED (delete type)\n");
657 event_debug(("%s: called\n", __func__
));
658 evbuffer_add_printf(evb
, BASIC_REQUEST_BODY
);
660 /* allow sending of an empty reply */
661 evhttp_send_reply(req
, HTTP_OK
, "Everything is fine",
662 !empty
? evb
: NULL
);
668 http_delete_test(void *arg
)
670 struct basic_test_data
*data
= arg
;
671 struct bufferevent
*bev
;
673 const char *http_request
;
674 ev_uint16_t port
= 0;
678 http
= http_setup(&port
, data
->base
);
680 fd
= http_connect("127.0.0.1", port
);
682 /* Stupid thing to send a request */
683 bev
= bufferevent_socket_new(data
->base
, fd
, 0);
684 bufferevent_setcb(bev
, http_readcb
, http_writecb
,
685 http_errorcb
, data
->base
);
688 "DELETE /deleteit HTTP/1.1\r\n"
690 "Connection: close\r\n"
693 bufferevent_write(bev
, http_request
, strlen(http_request
));
695 event_base_dispatch(data
->base
);
697 bufferevent_free(bev
);
698 evutil_closesocket(fd
);
702 tt_int_op(test_ok
, ==, 2);
708 http_allowed_methods_eventcb(struct bufferevent
*bev
, short what
, void *arg
)
711 if ((what
& (BEV_EVENT_ERROR
|BEV_EVENT_EOF
))) {
714 n
= evbuffer_remove(bufferevent_get_input(bev
), buf
,
720 *output
= strdup(buf
);
722 event_base_loopexit(exit_base
, NULL
);
727 http_allowed_methods_test(void *arg
)
729 struct basic_test_data
*data
= arg
;
730 struct bufferevent
*bev1
, *bev2
, *bev3
;
731 evutil_socket_t fd1
, fd2
, fd3
;
732 const char *http_request
;
733 char *result1
=NULL
, *result2
=NULL
, *result3
=NULL
;
734 ev_uint16_t port
= 0;
736 exit_base
= data
->base
;
739 http
= http_setup(&port
, data
->base
);
741 fd1
= http_connect("127.0.0.1", port
);
743 /* GET is out; PATCH is in. */
744 evhttp_set_allowed_methods(http
, EVHTTP_REQ_PATCH
);
746 /* Stupid thing to send a request */
747 bev1
= bufferevent_socket_new(data
->base
, fd1
, 0);
748 bufferevent_enable(bev1
, EV_READ
|EV_WRITE
);
749 bufferevent_setcb(bev1
, NULL
, NULL
,
750 http_allowed_methods_eventcb
, &result1
);
753 "GET /index.html HTTP/1.1\r\n"
755 "Connection: close\r\n"
758 bufferevent_write(bev1
, http_request
, strlen(http_request
));
760 event_base_dispatch(data
->base
);
762 fd2
= http_connect("127.0.0.1", port
);
764 bev2
= bufferevent_socket_new(data
->base
, fd2
, 0);
765 bufferevent_enable(bev2
, EV_READ
|EV_WRITE
);
766 bufferevent_setcb(bev2
, NULL
, NULL
,
767 http_allowed_methods_eventcb
, &result2
);
770 "PATCH /test HTTP/1.1\r\n"
772 "Connection: close\r\n"
775 bufferevent_write(bev2
, http_request
, strlen(http_request
));
777 event_base_dispatch(data
->base
);
779 fd3
= http_connect("127.0.0.1", port
);
781 bev3
= bufferevent_socket_new(data
->base
, fd3
, 0);
782 bufferevent_enable(bev3
, EV_READ
|EV_WRITE
);
783 bufferevent_setcb(bev3
, NULL
, NULL
,
784 http_allowed_methods_eventcb
, &result3
);
787 "FLOOP /test HTTP/1.1\r\n"
789 "Connection: close\r\n"
792 bufferevent_write(bev3
, http_request
, strlen(http_request
));
794 event_base_dispatch(data
->base
);
796 bufferevent_free(bev1
);
797 bufferevent_free(bev2
);
798 bufferevent_free(bev3
);
799 evutil_closesocket(fd1
);
800 evutil_closesocket(fd2
);
801 evutil_closesocket(fd3
);
805 /* Method known but disallowed */
807 tt_assert(!strncmp(result1
, "HTTP/1.1 501 ", strlen("HTTP/1.1 501 ")));
809 /* Method known and allowed */
811 tt_assert(!strncmp(result2
, "HTTP/1.1 200 ", strlen("HTTP/1.1 200 ")));
815 tt_assert(!strncmp(result3
, "HTTP/1.1 501 ", strlen("HTTP/1.1 501 ")));
826 static void http_request_done(struct evhttp_request
*, void *);
827 static void http_request_empty_done(struct evhttp_request
*, void *);
830 _http_connection_test(struct basic_test_data
*data
, int persistent
)
832 ev_uint16_t port
= 0;
833 struct evhttp_connection
*evcon
= NULL
;
834 struct evhttp_request
*req
= NULL
;
838 http
= http_setup(&port
, data
->base
);
840 evcon
= evhttp_connection_base_new(data
->base
, NULL
, "127.0.0.1", port
);
843 tt_assert(evhttp_connection_get_base(evcon
) == data
->base
);
845 exit_base
= data
->base
;
847 * At this point, we want to schedule a request to the HTTP
848 * server using our make request method.
851 req
= evhttp_request_new(http_request_done
, basic_request_body
);
853 /* Add the information that we care about */
854 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
856 /* We give ownership of the request to the connection */
857 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/test") == -1) {
858 fprintf(stdout
, "FAILED\n");
862 event_base_dispatch(data
->base
);
866 /* try to make another request over the same connection */
869 req
= evhttp_request_new(http_request_done
, basic_request_body
);
871 /* Add the information that we care about */
872 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
875 * if our connections are not supposed to be persistent; request
876 * a close from the server.
879 evhttp_add_header(evhttp_request_get_output_headers(req
), "Connection", "close");
881 /* We give ownership of the request to the connection */
882 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/test") == -1) {
883 tt_abort_msg("couldn't make request");
886 event_base_dispatch(data
->base
);
888 /* make another request: request empty reply */
891 req
= evhttp_request_new(http_request_empty_done
, data
->base
);
893 /* Add the information that we care about */
894 evhttp_add_header(evhttp_request_get_output_headers(req
), "Empty", "itis");
896 /* We give ownership of the request to the connection */
897 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/test") == -1) {
898 tt_abort_msg("Couldn't make request");
901 event_base_dispatch(data
->base
);
905 evhttp_connection_free(evcon
);
911 http_connection_test(void *arg
)
913 _http_connection_test(arg
, 0);
916 http_persist_connection_test(void *arg
)
918 _http_connection_test(arg
, 1);
921 static struct regress_dns_server_table search_table
[] = {
922 { "localhost", "A", "127.0.0.1", 0 },
923 { NULL
, NULL
, NULL
, 0 }
927 http_connection_async_test(void *arg
)
929 struct basic_test_data
*data
= arg
;
930 ev_uint16_t port
= 0;
931 struct evhttp_connection
*evcon
= NULL
;
932 struct evhttp_request
*req
= NULL
;
933 struct evdns_base
*dns_base
= NULL
;
934 ev_uint16_t portnum
= 0;
937 exit_base
= data
->base
;
938 tt_assert(regress_dnsserver(data
->base
, &portnum
, search_table
));
940 dns_base
= evdns_base_new(data
->base
, 0/* init name servers */);
943 /* Add ourself as the only nameserver, and make sure we really are
944 * the only nameserver. */
945 evutil_snprintf(address
, sizeof(address
), "127.0.0.1:%d", portnum
);
946 evdns_base_nameserver_ip_add(dns_base
, address
);
950 http
= http_setup(&port
, data
->base
);
952 evcon
= evhttp_connection_base_new(data
->base
, dns_base
, "127.0.0.1", port
);
956 * At this point, we want to schedule a request to the HTTP
957 * server using our make request method.
960 req
= evhttp_request_new(http_request_done
, basic_request_body
);
962 /* Add the information that we care about */
963 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
965 /* We give ownership of the request to the connection */
966 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/test") == -1) {
967 fprintf(stdout
, "FAILED\n");
971 event_base_dispatch(data
->base
);
975 /* try to make another request over the same connection */
978 req
= evhttp_request_new(http_request_done
, basic_request_body
);
980 /* Add the information that we care about */
981 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
984 * if our connections are not supposed to be persistent; request
985 * a close from the server.
987 evhttp_add_header(evhttp_request_get_output_headers(req
), "Connection", "close");
989 /* We give ownership of the request to the connection */
990 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/test") == -1) {
991 tt_abort_msg("couldn't make request");
994 event_base_dispatch(data
->base
);
996 /* make another request: request empty reply */
999 req
= evhttp_request_new(http_request_empty_done
, data
->base
);
1001 /* Add the information that we care about */
1002 evhttp_add_header(evhttp_request_get_output_headers(req
), "Empty", "itis");
1004 /* We give ownership of the request to the connection */
1005 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/test") == -1) {
1006 tt_abort_msg("Couldn't make request");
1009 event_base_dispatch(data
->base
);
1013 evhttp_connection_free(evcon
);
1017 evdns_base_free(dns_base
, 0);
1018 regress_clean_dnsserver();
1022 http_request_never_call(struct evhttp_request
*req
, void *arg
)
1024 fprintf(stdout
, "FAILED\n");
1029 http_do_cancel(evutil_socket_t fd
, short what
, void *arg
)
1031 struct evhttp_request
*req
= arg
;
1033 struct event_base
*base
;
1034 evutil_timerclear(&tv
);
1036 tv
.tv_usec
= 500 * 1000;
1038 base
= evhttp_connection_get_base(evhttp_request_get_connection(req
));
1039 evhttp_cancel_request(req
);
1041 event_base_loopexit(base
, &tv
);
1047 http_cancel_test(void *arg
)
1049 struct basic_test_data
*data
= arg
;
1050 ev_uint16_t port
= 0;
1051 struct evhttp_connection
*evcon
= NULL
;
1052 struct evhttp_request
*req
= NULL
;
1055 exit_base
= data
->base
;
1059 http
= http_setup(&port
, data
->base
);
1061 evcon
= evhttp_connection_base_new(data
->base
, NULL
, "127.0.0.1", port
);
1065 * At this point, we want to schedule a request to the HTTP
1066 * server using our make request method.
1069 req
= evhttp_request_new(http_request_never_call
, NULL
);
1071 /* Add the information that we care about */
1072 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
1074 /* We give ownership of the request to the connection */
1075 tt_int_op(evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/delay"),
1078 evutil_timerclear(&tv
);
1080 tv
.tv_usec
= 100 * 1000;
1082 event_base_once(data
->base
, -1, EV_TIMEOUT
, http_do_cancel
, req
, &tv
);
1084 event_base_dispatch(data
->base
);
1086 tt_int_op(test_ok
, ==, 2);
1088 /* try to make another request over the same connection */
1091 req
= evhttp_request_new(http_request_done
, basic_request_body
);
1093 /* Add the information that we care about */
1094 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
1096 /* We give ownership of the request to the connection */
1097 tt_int_op(evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/test"),
1100 event_base_dispatch(data
->base
);
1102 /* make another request: request empty reply */
1105 req
= evhttp_request_new(http_request_empty_done
, data
->base
);
1107 /* Add the information that we care about */
1108 evhttp_add_header(evhttp_request_get_output_headers(req
), "Empty", "itis");
1110 /* We give ownership of the request to the connection */
1111 tt_int_op(evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/test"),
1114 event_base_dispatch(data
->base
);
1118 evhttp_connection_free(evcon
);
1124 http_request_done(struct evhttp_request
*req
, void *arg
)
1126 const char *what
= arg
;
1128 if (evhttp_request_get_response_code(req
) != HTTP_OK
) {
1129 fprintf(stderr
, "FAILED\n");
1133 if (evhttp_find_header(evhttp_request_get_input_headers(req
), "Content-Type") == NULL
) {
1134 fprintf(stderr
, "FAILED\n");
1138 if (evbuffer_get_length(evhttp_request_get_input_buffer(req
)) != strlen(what
)) {
1139 fprintf(stderr
, "FAILED\n");
1143 if (evbuffer_datacmp(evhttp_request_get_input_buffer(req
), what
) != 0) {
1144 fprintf(stderr
, "FAILED\n");
1149 EVUTIL_ASSERT(exit_base
);
1150 event_base_loopexit(exit_base
, NULL
);
1154 http_request_expect_error(struct evhttp_request
*req
, void *arg
)
1156 if (evhttp_request_get_response_code(req
) == HTTP_OK
) {
1157 fprintf(stderr
, "FAILED\n");
1163 event_base_loopexit(arg
, NULL
);
1166 /* test virtual hosts */
1168 http_virtual_host_test(void *arg
)
1170 struct basic_test_data
*data
= arg
;
1171 ev_uint16_t port
= 0;
1172 struct evhttp_connection
*evcon
= NULL
;
1173 struct evhttp_request
*req
= NULL
;
1174 struct evhttp
*second
= NULL
, *third
= NULL
;
1176 struct bufferevent
*bev
;
1177 const char *http_request
;
1179 exit_base
= data
->base
;
1181 http
= http_setup(&port
, data
->base
);
1184 second
= evhttp_new(NULL
);
1185 evhttp_set_cb(second
, "/funnybunny", http_basic_cb
, NULL
);
1186 third
= evhttp_new(NULL
);
1187 evhttp_set_cb(third
, "/blackcoffee", http_basic_cb
, NULL
);
1189 if (evhttp_add_virtual_host(http
, "foo.com", second
) == -1) {
1190 tt_abort_msg("Couldn't add vhost");
1193 if (evhttp_add_virtual_host(http
, "bar.*.foo.com", third
) == -1) {
1194 tt_abort_msg("Couldn't add wildcarded vhost");
1197 /* add some aliases to the vhosts */
1198 tt_assert(evhttp_add_server_alias(second
, "manolito.info") == 0);
1199 tt_assert(evhttp_add_server_alias(third
, "bonkers.org") == 0);
1201 evcon
= evhttp_connection_base_new(data
->base
, NULL
, "127.0.0.1", port
);
1204 /* make a request with a different host and expect an error */
1205 req
= evhttp_request_new(http_request_expect_error
, data
->base
);
1207 /* Add the information that we care about */
1208 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
1210 /* We give ownership of the request to the connection */
1211 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
,
1212 "/funnybunny") == -1) {
1213 tt_abort_msg("Couldn't make request");
1216 event_base_dispatch(data
->base
);
1218 tt_assert(test_ok
== 1);
1222 /* make a request with the right host and expect a response */
1223 req
= evhttp_request_new(http_request_done
, basic_request_body
);
1225 /* Add the information that we care about */
1226 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "foo.com");
1228 /* We give ownership of the request to the connection */
1229 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
,
1230 "/funnybunny") == -1) {
1231 fprintf(stdout
, "FAILED\n");
1235 event_base_dispatch(data
->base
);
1237 tt_assert(test_ok
== 1);
1241 /* make a request with the right host and expect a response */
1242 req
= evhttp_request_new(http_request_done
, basic_request_body
);
1244 /* Add the information that we care about */
1245 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "bar.magic.foo.com");
1247 /* We give ownership of the request to the connection */
1248 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
,
1249 "/blackcoffee") == -1) {
1250 tt_abort_msg("Couldn't make request");
1253 event_base_dispatch(data
->base
);
1255 tt_assert(test_ok
== 1)
1259 /* make a request with the right host and expect a response */
1260 req
= evhttp_request_new(http_request_done
, basic_request_body
);
1262 /* Add the information that we care about */
1263 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "manolito.info");
1265 /* We give ownership of the request to the connection */
1266 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
,
1267 "/funnybunny") == -1) {
1268 tt_abort_msg("Couldn't make request");
1271 event_base_dispatch(data
->base
);
1273 tt_assert(test_ok
== 1)
1277 /* make a request with the right host and expect a response */
1278 req
= evhttp_request_new(http_request_done
, basic_request_body
);
1280 /* Add the Host header. This time with the optional port. */
1281 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "bonkers.org:8000");
1283 /* We give ownership of the request to the connection */
1284 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
,
1285 "/blackcoffee") == -1) {
1286 tt_abort_msg("Couldn't make request");
1289 event_base_dispatch(data
->base
);
1291 tt_assert(test_ok
== 1)
1295 /* Now make a raw request with an absolute URI. */
1296 fd
= http_connect("127.0.0.1", port
);
1298 /* Stupid thing to send a request */
1299 bev
= bufferevent_socket_new(data
->base
, fd
, 0);
1300 bufferevent_setcb(bev
, http_readcb
, http_writecb
,
1301 http_errorcb
, NULL
);
1303 /* The host in the URI should override the Host: header */
1305 "GET http://manolito.info/funnybunny HTTP/1.1\r\n"
1306 "Host: somehost\r\n"
1307 "Connection: close\r\n"
1310 bufferevent_write(bev
, http_request
, strlen(http_request
));
1312 event_base_dispatch(data
->base
);
1314 tt_int_op(test_ok
, ==, 2);
1316 bufferevent_free(bev
);
1317 evutil_closesocket(fd
);
1321 evhttp_connection_free(evcon
);
1327 /* test date header and content length */
1330 http_request_empty_done(struct evhttp_request
*req
, void *arg
)
1332 if (evhttp_request_get_response_code(req
) != HTTP_OK
) {
1333 fprintf(stderr
, "FAILED\n");
1337 if (evhttp_find_header(evhttp_request_get_input_headers(req
), "Date") == NULL
) {
1338 fprintf(stderr
, "FAILED\n");
1343 if (evhttp_find_header(evhttp_request_get_input_headers(req
), "Content-Length") == NULL
) {
1344 fprintf(stderr
, "FAILED\n");
1348 if (strcmp(evhttp_find_header(evhttp_request_get_input_headers(req
), "Content-Length"),
1350 fprintf(stderr
, "FAILED\n");
1354 if (evbuffer_get_length(evhttp_request_get_input_buffer(req
)) != 0) {
1355 fprintf(stderr
, "FAILED\n");
1361 event_base_loopexit(arg
, NULL
);
1365 * HTTP DISPATCHER test
1369 http_dispatcher_cb(struct evhttp_request
*req
, void *arg
)
1372 struct evbuffer
*evb
= evbuffer_new();
1373 event_debug(("%s: called\n", __func__
));
1374 evbuffer_add_printf(evb
, "DISPATCHER_TEST");
1376 evhttp_send_reply(req
, HTTP_OK
, "Everything is fine", evb
);
1382 http_dispatcher_test_done(struct evhttp_request
*req
, void *arg
)
1384 struct event_base
*base
= arg
;
1385 const char *what
= "DISPATCHER_TEST";
1387 if (evhttp_request_get_response_code(req
) != HTTP_OK
) {
1388 fprintf(stderr
, "FAILED\n");
1392 if (evhttp_find_header(evhttp_request_get_input_headers(req
), "Content-Type") == NULL
) {
1393 fprintf(stderr
, "FAILED (content type)\n");
1397 if (evbuffer_get_length(evhttp_request_get_input_buffer(req
)) != strlen(what
)) {
1398 fprintf(stderr
, "FAILED (length %lu vs %lu)\n",
1399 (unsigned long)evbuffer_get_length(evhttp_request_get_input_buffer(req
)), (unsigned long)strlen(what
));
1403 if (evbuffer_datacmp(evhttp_request_get_input_buffer(req
), what
) != 0) {
1404 fprintf(stderr
, "FAILED (data)\n");
1409 event_base_loopexit(base
, NULL
);
1413 http_dispatcher_test(void *arg
)
1415 struct basic_test_data
*data
= arg
;
1416 ev_uint16_t port
= 0;
1417 struct evhttp_connection
*evcon
= NULL
;
1418 struct evhttp_request
*req
= NULL
;
1422 http
= http_setup(&port
, data
->base
);
1424 evcon
= evhttp_connection_base_new(data
->base
, NULL
, "127.0.0.1", port
);
1427 /* also bind to local host */
1428 evhttp_connection_set_local_address(evcon
, "127.0.0.1");
1431 * At this point, we want to schedule an HTTP GET request
1432 * server using our make request method.
1435 req
= evhttp_request_new(http_dispatcher_test_done
, data
->base
);
1438 /* Add the information that we care about */
1439 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
1441 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/?arg=val") == -1) {
1442 tt_abort_msg("Couldn't make request");
1445 event_base_dispatch(data
->base
);
1449 evhttp_connection_free(evcon
);
1458 void http_postrequest_done(struct evhttp_request
*, void *);
1460 #define POST_DATA "Okay. Not really printf"
1463 http_post_test(void *arg
)
1465 struct basic_test_data
*data
= arg
;
1466 ev_uint16_t port
= 0;
1467 struct evhttp_connection
*evcon
= NULL
;
1468 struct evhttp_request
*req
= NULL
;
1472 http
= http_setup(&port
, data
->base
);
1474 evcon
= evhttp_connection_base_new(data
->base
, NULL
, "127.0.0.1", port
);
1478 * At this point, we want to schedule an HTTP POST request
1479 * server using our make request method.
1482 req
= evhttp_request_new(http_postrequest_done
, data
->base
);
1485 /* Add the information that we care about */
1486 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
1487 evbuffer_add_printf(evhttp_request_get_output_buffer(req
), POST_DATA
);
1489 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_POST
, "/postit") == -1) {
1490 tt_abort_msg("Couldn't make request");
1493 event_base_dispatch(data
->base
);
1495 tt_int_op(test_ok
, ==, 1);
1499 req
= evhttp_request_new(http_postrequest_done
, data
->base
);
1502 /* Now try with 100-continue. */
1504 /* Add the information that we care about */
1505 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
1506 evhttp_add_header(evhttp_request_get_output_headers(req
), "Expect", "100-continue");
1507 evbuffer_add_printf(evhttp_request_get_output_buffer(req
), POST_DATA
);
1509 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_POST
, "/postit") == -1) {
1510 tt_abort_msg("Couldn't make request");
1513 event_base_dispatch(data
->base
);
1515 tt_int_op(test_ok
, ==, 1);
1517 evhttp_connection_free(evcon
);
1525 http_post_cb(struct evhttp_request
*req
, void *arg
)
1527 struct evbuffer
*evb
;
1528 event_debug(("%s: called\n", __func__
));
1530 /* Yes, we are expecting a post request */
1531 if (evhttp_request_get_command(req
) != EVHTTP_REQ_POST
) {
1532 fprintf(stdout
, "FAILED (post type)\n");
1536 if (evbuffer_get_length(evhttp_request_get_input_buffer(req
)) != strlen(POST_DATA
)) {
1537 fprintf(stdout
, "FAILED (length: %lu vs %lu)\n",
1538 (unsigned long) evbuffer_get_length(evhttp_request_get_input_buffer(req
)), (unsigned long) strlen(POST_DATA
));
1542 if (evbuffer_datacmp(evhttp_request_get_input_buffer(req
), POST_DATA
) != 0) {
1543 fprintf(stdout
, "FAILED (data)\n");
1544 fprintf(stdout
, "Got :%s\n", evbuffer_pullup(evhttp_request_get_input_buffer(req
),-1));
1545 fprintf(stdout
, "Want:%s\n", POST_DATA
);
1549 evb
= evbuffer_new();
1550 evbuffer_add_printf(evb
, BASIC_REQUEST_BODY
);
1552 evhttp_send_reply(req
, HTTP_OK
, "Everything is fine", evb
);
1558 http_postrequest_done(struct evhttp_request
*req
, void *arg
)
1560 const char *what
= BASIC_REQUEST_BODY
;
1561 struct event_base
*base
= arg
;
1564 fprintf(stderr
, "FAILED (timeout)\n");
1568 if (evhttp_request_get_response_code(req
) != HTTP_OK
) {
1570 fprintf(stderr
, "FAILED (response code)\n");
1574 if (evhttp_find_header(evhttp_request_get_input_headers(req
), "Content-Type") == NULL
) {
1575 fprintf(stderr
, "FAILED (content type)\n");
1579 if (evbuffer_get_length(evhttp_request_get_input_buffer(req
)) != strlen(what
)) {
1580 fprintf(stderr
, "FAILED (length %lu vs %lu)\n",
1581 (unsigned long)evbuffer_get_length(evhttp_request_get_input_buffer(req
)), (unsigned long)strlen(what
));
1585 if (evbuffer_datacmp(evhttp_request_get_input_buffer(req
), what
) != 0) {
1586 fprintf(stderr
, "FAILED (data)\n");
1591 event_base_loopexit(base
, NULL
);
1595 * HTTP PUT test, basically just like POST, but ...
1598 void http_putrequest_done(struct evhttp_request
*, void *);
1600 #define PUT_DATA "Hi, I'm some PUT data"
1603 http_put_test(void *arg
)
1605 struct basic_test_data
*data
= arg
;
1606 ev_uint16_t port
= 0;
1607 struct evhttp_connection
*evcon
= NULL
;
1608 struct evhttp_request
*req
= NULL
;
1612 http
= http_setup(&port
, data
->base
);
1614 evcon
= evhttp_connection_base_new(data
->base
, NULL
, "127.0.0.1", port
);
1618 * Schedule the HTTP PUT request
1621 req
= evhttp_request_new(http_putrequest_done
, data
->base
);
1624 /* Add the information that we care about */
1625 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "someotherhost");
1626 evbuffer_add_printf(evhttp_request_get_output_buffer(req
), PUT_DATA
);
1628 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_PUT
, "/putit") == -1) {
1629 tt_abort_msg("Couldn't make request");
1632 event_base_dispatch(data
->base
);
1634 evhttp_connection_free(evcon
);
1637 tt_int_op(test_ok
, ==, 1);
1643 http_put_cb(struct evhttp_request
*req
, void *arg
)
1645 struct evbuffer
*evb
;
1646 event_debug(("%s: called\n", __func__
));
1648 /* Expecting a PUT request */
1649 if (evhttp_request_get_command(req
) != EVHTTP_REQ_PUT
) {
1650 fprintf(stdout
, "FAILED (put type)\n");
1654 if (evbuffer_get_length(evhttp_request_get_input_buffer(req
)) != strlen(PUT_DATA
)) {
1655 fprintf(stdout
, "FAILED (length: %lu vs %lu)\n",
1656 (unsigned long)evbuffer_get_length(evhttp_request_get_input_buffer(req
)), (unsigned long)strlen(PUT_DATA
));
1660 if (evbuffer_datacmp(evhttp_request_get_input_buffer(req
), PUT_DATA
) != 0) {
1661 fprintf(stdout
, "FAILED (data)\n");
1662 fprintf(stdout
, "Got :%s\n", evbuffer_pullup(evhttp_request_get_input_buffer(req
),-1));
1663 fprintf(stdout
, "Want:%s\n", PUT_DATA
);
1667 evb
= evbuffer_new();
1668 evbuffer_add_printf(evb
, "That ain't funny");
1670 evhttp_send_reply(req
, HTTP_OK
, "Everything is great", evb
);
1676 http_putrequest_done(struct evhttp_request
*req
, void *arg
)
1678 struct event_base
*base
= arg
;
1679 const char *what
= "That ain't funny";
1682 fprintf(stderr
, "FAILED (timeout)\n");
1686 if (evhttp_request_get_response_code(req
) != HTTP_OK
) {
1688 fprintf(stderr
, "FAILED (response code)\n");
1692 if (evhttp_find_header(evhttp_request_get_input_headers(req
), "Content-Type") == NULL
) {
1693 fprintf(stderr
, "FAILED (content type)\n");
1697 if (evbuffer_get_length(evhttp_request_get_input_buffer(req
)) != strlen(what
)) {
1698 fprintf(stderr
, "FAILED (length %lu vs %lu)\n",
1699 (unsigned long)evbuffer_get_length(evhttp_request_get_input_buffer(req
)), (unsigned long)strlen(what
));
1704 if (evbuffer_datacmp(evhttp_request_get_input_buffer(req
), what
) != 0) {
1705 fprintf(stderr
, "FAILED (data)\n");
1710 event_base_loopexit(base
, NULL
);
1714 http_failure_readcb(struct bufferevent
*bev
, void *arg
)
1716 const char *what
= "400 Bad Request";
1717 if (evbuffer_contains(bufferevent_get_input(bev
), what
)) {
1719 bufferevent_disable(bev
, EV_READ
);
1720 event_base_loopexit(arg
, NULL
);
1725 * Testing that the HTTP server can deal with a malformed request.
1728 http_failure_test(void *arg
)
1730 struct basic_test_data
*data
= arg
;
1731 struct bufferevent
*bev
;
1733 const char *http_request
;
1734 ev_uint16_t port
= 0;
1738 http
= http_setup(&port
, data
->base
);
1740 fd
= http_connect("127.0.0.1", port
);
1742 /* Stupid thing to send a request */
1743 bev
= bufferevent_socket_new(data
->base
, fd
, 0);
1744 bufferevent_setcb(bev
, http_failure_readcb
, http_writecb
,
1745 http_errorcb
, data
->base
);
1747 http_request
= "illegal request\r\n";
1749 bufferevent_write(bev
, http_request
, strlen(http_request
));
1751 event_base_dispatch(data
->base
);
1753 bufferevent_free(bev
);
1754 evutil_closesocket(fd
);
1758 tt_int_op(test_ok
, ==, 2);
1764 close_detect_done(struct evhttp_request
*req
, void *arg
)
1768 tt_assert(evhttp_request_get_response_code(req
) == HTTP_OK
);
1773 evutil_timerclear(&tv
);
1775 event_base_loopexit(arg
, &tv
);
1779 close_detect_launch(evutil_socket_t fd
, short what
, void *arg
)
1781 struct evhttp_connection
*evcon
= arg
;
1782 struct event_base
*base
= evhttp_connection_get_base(evcon
);
1783 struct evhttp_request
*req
;
1785 req
= evhttp_request_new(close_detect_done
, base
);
1787 /* Add the information that we care about */
1788 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
1790 /* We give ownership of the request to the connection */
1791 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/test") == -1) {
1792 tt_fail_msg("Couldn't make request");
1797 close_detect_cb(struct evhttp_request
*req
, void *arg
)
1799 struct evhttp_connection
*evcon
= arg
;
1800 struct event_base
*base
= evhttp_connection_get_base(evcon
);
1803 if (req
!= NULL
&& evhttp_request_get_response_code(req
) != HTTP_OK
) {
1804 tt_abort_msg("Failed");
1807 evutil_timerclear(&tv
);
1808 tv
.tv_sec
= 3; /* longer than the http time out */
1810 /* launch a new request on the persistent connection in 3 seconds */
1811 event_base_once(base
, -1, EV_TIMEOUT
, close_detect_launch
, evcon
, &tv
);
1818 _http_close_detection(struct basic_test_data
*data
, int with_delay
)
1820 ev_uint16_t port
= 0;
1821 struct evhttp_connection
*evcon
= NULL
;
1822 struct evhttp_request
*req
= NULL
;
1825 http
= http_setup(&port
, data
->base
);
1827 /* 2 second timeout */
1828 evhttp_set_timeout(http
, 1);
1830 evcon
= evhttp_connection_base_new(data
->base
, NULL
,
1833 delayed_client
= evcon
;
1836 * At this point, we want to schedule a request to the HTTP
1837 * server using our make request method.
1840 req
= evhttp_request_new(close_detect_cb
, evcon
);
1842 /* Add the information that we care about */
1843 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
1845 /* We give ownership of the request to the connection */
1846 if (evhttp_make_request(evcon
,
1847 req
, EVHTTP_REQ_GET
, with_delay
? "/largedelay" : "/test") == -1) {
1848 tt_abort_msg("couldn't make request");
1851 event_base_dispatch(data
->base
);
1853 /* at this point, the http server should have no connection */
1854 tt_assert(TAILQ_FIRST(&http
->connections
) == NULL
);
1858 evhttp_connection_free(evcon
);
1863 http_close_detection_test(void *arg
)
1865 _http_close_detection(arg
, 0);
1868 http_close_detection_delay_test(void *arg
)
1870 _http_close_detection(arg
, 1);
1874 http_highport_test(void *arg
)
1876 struct basic_test_data
*data
= arg
;
1878 struct evhttp
*myhttp
= NULL
;
1880 /* Try a few different ports */
1881 for (i
= 0; i
< 50; ++i
) {
1882 myhttp
= evhttp_new(data
->base
);
1883 if (evhttp_bind_socket(myhttp
, "127.0.0.1", 65535 - i
) == 0) {
1885 evhttp_free(myhttp
);
1888 evhttp_free(myhttp
);
1891 tt_fail_msg("Couldn't get a high port");
1895 http_bad_header_test(void *ptr
)
1897 struct evkeyvalq headers
;
1899 TAILQ_INIT(&headers
);
1901 tt_want(evhttp_add_header(&headers
, "One", "Two") == 0);
1902 tt_want(evhttp_add_header(&headers
, "One", "Two\r\n Three") == 0);
1903 tt_want(evhttp_add_header(&headers
, "One\r", "Two") == -1);
1904 tt_want(evhttp_add_header(&headers
, "One\n", "Two") == -1);
1905 tt_want(evhttp_add_header(&headers
, "One", "Two\r") == -1);
1906 tt_want(evhttp_add_header(&headers
, "One", "Two\n") == -1);
1908 evhttp_clear_headers(&headers
);
1911 static int validate_header(
1912 const struct evkeyvalq
* headers
,
1913 const char *key
, const char *value
)
1915 const char *real_val
= evhttp_find_header(headers
, key
);
1916 tt_assert(real_val
!= NULL
);
1917 tt_want(strcmp(real_val
, value
) == 0);
1923 http_parse_query_test(void *ptr
)
1925 struct evkeyvalq headers
;
1928 TAILQ_INIT(&headers
);
1930 r
= evhttp_parse_query("http://www.test.com/?q=test", &headers
);
1931 tt_want(validate_header(&headers
, "q", "test") == 0);
1932 tt_int_op(r
, ==, 0);
1933 evhttp_clear_headers(&headers
);
1935 r
= evhttp_parse_query("http://www.test.com/?q=test&foo=bar", &headers
);
1936 tt_want(validate_header(&headers
, "q", "test") == 0);
1937 tt_want(validate_header(&headers
, "foo", "bar") == 0);
1938 tt_int_op(r
, ==, 0);
1939 evhttp_clear_headers(&headers
);
1941 r
= evhttp_parse_query("http://www.test.com/?q=test+foo", &headers
);
1942 tt_want(validate_header(&headers
, "q", "test foo") == 0);
1943 tt_int_op(r
, ==, 0);
1944 evhttp_clear_headers(&headers
);
1946 r
= evhttp_parse_query("http://www.test.com/?q=test%0Afoo", &headers
);
1947 tt_want(validate_header(&headers
, "q", "test\nfoo") == 0);
1948 tt_int_op(r
, ==, 0);
1949 evhttp_clear_headers(&headers
);
1951 r
= evhttp_parse_query("http://www.test.com/?q=test%0Dfoo", &headers
);
1952 tt_want(validate_header(&headers
, "q", "test\rfoo") == 0);
1953 tt_int_op(r
, ==, 0);
1954 evhttp_clear_headers(&headers
);
1956 r
= evhttp_parse_query("http://www.test.com/?q=test&&q2", &headers
);
1957 tt_int_op(r
, ==, -1);
1958 evhttp_clear_headers(&headers
);
1960 r
= evhttp_parse_query("http://www.test.com/?q=test+this", &headers
);
1961 tt_want(validate_header(&headers
, "q", "test this") == 0);
1962 tt_int_op(r
, ==, 0);
1963 evhttp_clear_headers(&headers
);
1965 r
= evhttp_parse_query("http://www.test.com/?q=test&q2=foo", &headers
);
1966 tt_int_op(r
, ==, 0);
1967 tt_want(validate_header(&headers
, "q", "test") == 0);
1968 tt_want(validate_header(&headers
, "q2", "foo") == 0);
1969 evhttp_clear_headers(&headers
);
1971 r
= evhttp_parse_query("http://www.test.com/?q&q2=foo", &headers
);
1972 tt_int_op(r
, ==, -1);
1973 evhttp_clear_headers(&headers
);
1975 r
= evhttp_parse_query("http://www.test.com/?q=foo&q2", &headers
);
1976 tt_int_op(r
, ==, -1);
1977 evhttp_clear_headers(&headers
);
1979 r
= evhttp_parse_query("http://www.test.com/?q=foo&q2&q3=x", &headers
);
1980 tt_int_op(r
, ==, -1);
1981 evhttp_clear_headers(&headers
);
1983 r
= evhttp_parse_query("http://www.test.com/?q=&q2=&q3=", &headers
);
1984 tt_int_op(r
, ==, 0);
1985 tt_want(validate_header(&headers
, "q", "") == 0);
1986 tt_want(validate_header(&headers
, "q2", "") == 0);
1987 tt_want(validate_header(&headers
, "q3", "") == 0);
1988 evhttp_clear_headers(&headers
);
1991 evhttp_clear_headers(&headers
);
1995 http_parse_uri_test(void *ptr
)
1997 const int nonconform
= (ptr
!= NULL
);
1998 const unsigned parse_flags
=
1999 nonconform
? EVHTTP_URI_NONCONFORMANT
: 0;
2000 struct evhttp_uri
*uri
= NULL
;
2002 #define URI_PARSE(uri) \
2003 evhttp_uri_parse_with_flags((uri), parse_flags)
2005 #define TT_URI(want) do { \
2006 char *ret = evhttp_uri_join(uri, url_tmp, sizeof(url_tmp)); \
2007 tt_want(ret != NULL); \
2008 tt_want(ret == url_tmp); \
2009 if (strcmp(ret,want) != 0) \
2010 TT_FAIL(("\"%s\" != \"%s\"",ret,want)); \
2011 } while(/*CONSTCOND*/0)
2013 tt_want(evhttp_uri_join(NULL
, 0, 0) == NULL
);
2014 tt_want(evhttp_uri_join(NULL
, url_tmp
, 0) == NULL
);
2015 tt_want(evhttp_uri_join(NULL
, url_tmp
, sizeof(url_tmp
)) == NULL
);
2017 /* bad URIs: parsing */
2018 #define BAD(s) do { \
2019 if (URI_PARSE(s) != NULL) \
2020 TT_FAIL(("Expected error parsing \"%s\"",s)); \
2021 } while(/*CONSTCOND*/0)
2022 /* Nonconformant URIs we can parse: parsing */
2023 #define NCF(s) do { \
2024 uri = URI_PARSE(s); \
2025 if (uri != NULL && !nonconform) { \
2026 TT_FAIL(("Expected error parsing \"%s\"",s)); \
2027 } else if (uri == NULL && nonconform) { \
2028 TT_FAIL(("Couldn't parse nonconformant URI \"%s\"", \
2032 tt_want(evhttp_uri_join(uri, url_tmp, \
2033 sizeof(url_tmp))); \
2034 evhttp_uri_free(uri); \
2036 } while(/*CONSTCOND*/0)
2038 NCF("http://www.test.com/ why hello");
2039 NCF("http://www.test.com/why-hello\x01");
2040 NCF("http://www.test.com/why-hello?\x01");
2041 NCF("http://www.test.com/why-hello#\x01");
2042 BAD("http://www.\x01.test.com/why-hello");
2043 BAD("http://www.%7test.com/why-hello");
2044 NCF("http://www.test.com/why-hell%7o");
2045 BAD("h%3ttp://www.test.com/why-hello");
2046 NCF("http://www.test.com/why-hello%7");
2047 NCF("http://www.test.com/why-hell%7o");
2048 NCF("http://www.test.com/foo?ba%r");
2049 NCF("http://www.test.com/foo#ba%r");
2051 BAD("http://www.test.com:999x/");
2052 BAD("http://www.test.com:x/");
2053 BAD("http://[hello-there]/");
2054 BAD("http://[::1]]/");
2055 BAD("http://[::1/");
2056 BAD("http://[foob/");
2058 BAD("http://[ffff:ffff:ffff:ffff:Ffff:ffff:ffff:"
2059 "ffff:ffff:ffff:ffff:ffff:ffff:ffff]/");
2060 BAD("http://[vX.foo]/");
2061 BAD("http://[vX.foo]/");
2062 BAD("http://[v.foo]/");
2063 BAD("http://[v5.fo%o]/");
2064 BAD("http://[v5X]/");
2065 BAD("http://[v5]/");
2067 BAD("http://f\x01red@www.example.com/");
2068 BAD("http://f%0red@www.example.com/");
2069 BAD("http://www.example.com:9999999999999999999999999999999999999/");
2070 BAD("http://www.example.com:hihi/");
2071 BAD("://www.example.com/");
2073 /* bad URIs: joining */
2074 uri
= evhttp_uri_new();
2075 tt_want(0==evhttp_uri_set_host(uri
, "www.example.com"));
2076 tt_want(evhttp_uri_join(uri
, url_tmp
, sizeof(url_tmp
)) != NULL
);
2077 /* not enough space: */
2078 tt_want(evhttp_uri_join(uri
, url_tmp
, 3) == NULL
);
2079 /* host is set, but path doesn't start with "/": */
2080 tt_want(0==evhttp_uri_set_path(uri
, "hi_mom"));
2081 tt_want(evhttp_uri_join(uri
, url_tmp
, sizeof(url_tmp
)) == NULL
);
2082 tt_want(evhttp_uri_join(uri
, NULL
, sizeof(url_tmp
))==NULL
);
2083 tt_want(evhttp_uri_join(uri
, url_tmp
, 0)==NULL
);
2084 evhttp_uri_free(uri
);
2085 uri
= URI_PARSE("mailto:foo@bar");
2086 tt_want(uri
!= NULL
);
2087 tt_want(evhttp_uri_get_host(uri
) == NULL
);
2088 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2089 tt_want(evhttp_uri_get_port(uri
) == -1);
2090 tt_want(!strcmp(evhttp_uri_get_scheme(uri
), "mailto"));
2091 tt_want(!strcmp(evhttp_uri_get_path(uri
), "foo@bar"));
2092 tt_want(evhttp_uri_get_query(uri
) == NULL
);
2093 tt_want(evhttp_uri_get_fragment(uri
) == NULL
);
2094 TT_URI("mailto:foo@bar");
2095 evhttp_uri_free(uri
);
2097 uri
= evhttp_uri_new();
2098 /* Bad URI usage: setting invalid values */
2099 tt_want(-1 == evhttp_uri_set_scheme(uri
,""));
2100 tt_want(-1 == evhttp_uri_set_scheme(uri
,"33"));
2101 tt_want(-1 == evhttp_uri_set_scheme(uri
,"hi!"));
2102 tt_want(-1 == evhttp_uri_set_userinfo(uri
,"hello@"));
2103 tt_want(-1 == evhttp_uri_set_host(uri
,"[1.2.3.4]"));
2104 tt_want(-1 == evhttp_uri_set_host(uri
,"["));
2105 tt_want(-1 == evhttp_uri_set_host(uri
,"www.[foo].com"));
2106 tt_want(-1 == evhttp_uri_set_port(uri
,-3));
2107 tt_want(-1 == evhttp_uri_set_path(uri
,"hello?world"));
2108 tt_want(-1 == evhttp_uri_set_query(uri
,"hello#world"));
2109 tt_want(-1 == evhttp_uri_set_fragment(uri
,"hello#world"));
2110 /* Valid URI usage: setting valid values */
2111 tt_want(0 == evhttp_uri_set_scheme(uri
,"http"));
2112 tt_want(0 == evhttp_uri_set_scheme(uri
,NULL
));
2113 tt_want(0 == evhttp_uri_set_userinfo(uri
,"username:pass"));
2114 tt_want(0 == evhttp_uri_set_userinfo(uri
,NULL
));
2115 tt_want(0 == evhttp_uri_set_host(uri
,"www.example.com"));
2116 tt_want(0 == evhttp_uri_set_host(uri
,"1.2.3.4"));
2117 tt_want(0 == evhttp_uri_set_host(uri
,"[1:2:3:4::]"));
2118 tt_want(0 == evhttp_uri_set_host(uri
,"[v7.wobblewobble]"));
2119 tt_want(0 == evhttp_uri_set_host(uri
,NULL
));
2120 tt_want(0 == evhttp_uri_set_host(uri
,""));
2121 tt_want(0 == evhttp_uri_set_port(uri
, -1));
2122 tt_want(0 == evhttp_uri_set_port(uri
, 80));
2123 tt_want(0 == evhttp_uri_set_port(uri
, 65535));
2124 tt_want(0 == evhttp_uri_set_path(uri
, ""));
2125 tt_want(0 == evhttp_uri_set_path(uri
, "/documents/public/index.html"));
2126 tt_want(0 == evhttp_uri_set_path(uri
, NULL
));
2127 tt_want(0 == evhttp_uri_set_query(uri
, "key=val&key2=val2"));
2128 tt_want(0 == evhttp_uri_set_query(uri
, "keyvalblarg"));
2129 tt_want(0 == evhttp_uri_set_query(uri
, ""));
2130 tt_want(0 == evhttp_uri_set_query(uri
, NULL
));
2131 tt_want(0 == evhttp_uri_set_fragment(uri
, ""));
2132 tt_want(0 == evhttp_uri_set_fragment(uri
, "here?i?am"));
2133 tt_want(0 == evhttp_uri_set_fragment(uri
, NULL
));
2134 evhttp_uri_free(uri
);
2137 uri
= URI_PARSE("http://www.test.com/?q=t%33est");
2138 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "http") == 0);
2139 tt_want(strcmp(evhttp_uri_get_host(uri
), "www.test.com") == 0);
2140 tt_want(strcmp(evhttp_uri_get_path(uri
), "/") == 0);
2141 tt_want(strcmp(evhttp_uri_get_query(uri
), "q=t%33est") == 0);
2142 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2143 tt_want(evhttp_uri_get_port(uri
) == -1);
2144 tt_want(evhttp_uri_get_fragment(uri
) == NULL
);
2145 TT_URI("http://www.test.com/?q=t%33est");
2146 evhttp_uri_free(uri
);
2148 uri
= URI_PARSE("http://%77ww.test.com");
2149 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "http") == 0);
2150 tt_want(strcmp(evhttp_uri_get_host(uri
), "%77ww.test.com") == 0);
2151 tt_want(strcmp(evhttp_uri_get_path(uri
), "") == 0);
2152 tt_want(evhttp_uri_get_query(uri
) == NULL
);
2153 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2154 tt_want(evhttp_uri_get_port(uri
) == -1);
2155 tt_want(evhttp_uri_get_fragment(uri
) == NULL
);
2156 TT_URI("http://%77ww.test.com");
2157 evhttp_uri_free(uri
);
2159 uri
= URI_PARSE("http://www.test.com?q=test");
2160 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "http") == 0);
2161 tt_want(strcmp(evhttp_uri_get_host(uri
), "www.test.com") == 0);
2162 tt_want(strcmp(evhttp_uri_get_path(uri
), "") == 0);
2163 tt_want(strcmp(evhttp_uri_get_query(uri
), "q=test") == 0);
2164 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2165 tt_want(evhttp_uri_get_port(uri
) == -1);
2166 tt_want(evhttp_uri_get_fragment(uri
) == NULL
);
2167 TT_URI("http://www.test.com?q=test");
2168 evhttp_uri_free(uri
);
2170 uri
= URI_PARSE("http://www.test.com#fragment");
2171 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "http") == 0);
2172 tt_want(strcmp(evhttp_uri_get_host(uri
), "www.test.com") == 0);
2173 tt_want(strcmp(evhttp_uri_get_path(uri
), "") == 0);
2174 tt_want(evhttp_uri_get_query(uri
) == NULL
);
2175 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2176 tt_want(evhttp_uri_get_port(uri
) == -1);
2177 tt_want_str_op(evhttp_uri_get_fragment(uri
), ==, "fragment");
2178 TT_URI("http://www.test.com#fragment");
2179 evhttp_uri_free(uri
);
2181 uri
= URI_PARSE("http://8000/");
2182 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "http") == 0);
2183 tt_want(strcmp(evhttp_uri_get_host(uri
), "8000") == 0);
2184 tt_want(strcmp(evhttp_uri_get_path(uri
), "/") == 0);
2185 tt_want(evhttp_uri_get_query(uri
) == NULL
);
2186 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2187 tt_want(evhttp_uri_get_port(uri
) == -1);
2188 tt_want(evhttp_uri_get_fragment(uri
) == NULL
);
2189 TT_URI("http://8000/");
2190 evhttp_uri_free(uri
);
2192 uri
= URI_PARSE("http://:8000/");
2193 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "http") == 0);
2194 tt_want(strcmp(evhttp_uri_get_host(uri
), "") == 0);
2195 tt_want(strcmp(evhttp_uri_get_path(uri
), "/") == 0);
2196 tt_want(evhttp_uri_get_query(uri
) == NULL
);
2197 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2198 tt_want(evhttp_uri_get_port(uri
) == 8000);
2199 tt_want(evhttp_uri_get_fragment(uri
) == NULL
);
2200 TT_URI("http://:8000/");
2201 evhttp_uri_free(uri
);
2203 uri
= URI_PARSE("http://www.test.com:/"); /* empty port */
2204 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "http") == 0);
2205 tt_want(strcmp(evhttp_uri_get_host(uri
), "www.test.com") == 0);
2206 tt_want_str_op(evhttp_uri_get_path(uri
), ==, "/");
2207 tt_want(evhttp_uri_get_query(uri
) == NULL
);
2208 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2209 tt_want(evhttp_uri_get_port(uri
) == -1);
2210 tt_want(evhttp_uri_get_fragment(uri
) == NULL
);
2211 TT_URI("http://www.test.com/");
2212 evhttp_uri_free(uri
);
2214 uri
= URI_PARSE("http://www.test.com:"); /* empty port 2 */
2215 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "http") == 0);
2216 tt_want(strcmp(evhttp_uri_get_host(uri
), "www.test.com") == 0);
2217 tt_want(strcmp(evhttp_uri_get_path(uri
), "") == 0);
2218 tt_want(evhttp_uri_get_query(uri
) == NULL
);
2219 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2220 tt_want(evhttp_uri_get_port(uri
) == -1);
2221 tt_want(evhttp_uri_get_fragment(uri
) == NULL
);
2222 TT_URI("http://www.test.com");
2223 evhttp_uri_free(uri
);
2225 uri
= URI_PARSE("ftp://www.test.com/?q=test");
2226 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "ftp") == 0);
2227 tt_want(strcmp(evhttp_uri_get_host(uri
), "www.test.com") == 0);
2228 tt_want(strcmp(evhttp_uri_get_path(uri
), "/") == 0);
2229 tt_want(strcmp(evhttp_uri_get_query(uri
), "q=test") == 0);
2230 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2231 tt_want(evhttp_uri_get_port(uri
) == -1);
2232 tt_want(evhttp_uri_get_fragment(uri
) == NULL
);
2233 TT_URI("ftp://www.test.com/?q=test");
2234 evhttp_uri_free(uri
);
2236 uri
= URI_PARSE("ftp://[::1]:999/?q=test");
2237 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "ftp") == 0);
2238 tt_want(strcmp(evhttp_uri_get_host(uri
), "[::1]") == 0);
2239 tt_want(strcmp(evhttp_uri_get_path(uri
), "/") == 0);
2240 tt_want(strcmp(evhttp_uri_get_query(uri
), "q=test") == 0);
2241 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2242 tt_want(evhttp_uri_get_port(uri
) == 999);
2243 tt_want(evhttp_uri_get_fragment(uri
) == NULL
);
2244 TT_URI("ftp://[::1]:999/?q=test");
2245 evhttp_uri_free(uri
);
2247 uri
= URI_PARSE("ftp://[ff00::127.0.0.1]/?q=test");
2248 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "ftp") == 0);
2249 tt_want(strcmp(evhttp_uri_get_host(uri
), "[ff00::127.0.0.1]") == 0);
2250 tt_want(strcmp(evhttp_uri_get_path(uri
), "/") == 0);
2251 tt_want(strcmp(evhttp_uri_get_query(uri
), "q=test") == 0);
2252 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2253 tt_want(evhttp_uri_get_port(uri
) == -1);
2254 tt_want(evhttp_uri_get_fragment(uri
) == NULL
);
2255 TT_URI("ftp://[ff00::127.0.0.1]/?q=test");
2256 evhttp_uri_free(uri
);
2258 uri
= URI_PARSE("ftp://[v99.not_(any:time)_soon]/?q=test");
2259 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "ftp") == 0);
2260 tt_want(strcmp(evhttp_uri_get_host(uri
), "[v99.not_(any:time)_soon]") == 0);
2261 tt_want(strcmp(evhttp_uri_get_path(uri
), "/") == 0);
2262 tt_want(strcmp(evhttp_uri_get_query(uri
), "q=test") == 0);
2263 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2264 tt_want(evhttp_uri_get_port(uri
) == -1);
2265 tt_want(evhttp_uri_get_fragment(uri
) == NULL
);
2266 TT_URI("ftp://[v99.not_(any:time)_soon]/?q=test");
2267 evhttp_uri_free(uri
);
2269 uri
= URI_PARSE("scheme://user:pass@foo.com:42/?q=test&s=some+thing#fragment");
2270 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "scheme") == 0);
2271 tt_want(strcmp(evhttp_uri_get_userinfo(uri
), "user:pass") == 0);
2272 tt_want(strcmp(evhttp_uri_get_host(uri
), "foo.com") == 0);
2273 tt_want(evhttp_uri_get_port(uri
) == 42);
2274 tt_want(strcmp(evhttp_uri_get_path(uri
), "/") == 0);
2275 tt_want(strcmp(evhttp_uri_get_query(uri
), "q=test&s=some+thing") == 0);
2276 tt_want(strcmp(evhttp_uri_get_fragment(uri
), "fragment") == 0);
2277 TT_URI("scheme://user:pass@foo.com:42/?q=test&s=some+thing#fragment");
2278 evhttp_uri_free(uri
);
2280 uri
= URI_PARSE("scheme://user@foo.com/#fragment");
2281 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "scheme") == 0);
2282 tt_want(strcmp(evhttp_uri_get_userinfo(uri
), "user") == 0);
2283 tt_want(strcmp(evhttp_uri_get_host(uri
), "foo.com") == 0);
2284 tt_want(evhttp_uri_get_port(uri
) == -1);
2285 tt_want(strcmp(evhttp_uri_get_path(uri
), "/") == 0);
2286 tt_want(evhttp_uri_get_query(uri
) == NULL
);
2287 tt_want(strcmp(evhttp_uri_get_fragment(uri
), "fragment") == 0);
2288 TT_URI("scheme://user@foo.com/#fragment");
2289 evhttp_uri_free(uri
);
2291 uri
= URI_PARSE("scheme://%75ser@foo.com/#frag@ment");
2292 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "scheme") == 0);
2293 tt_want(strcmp(evhttp_uri_get_userinfo(uri
), "%75ser") == 0);
2294 tt_want(strcmp(evhttp_uri_get_host(uri
), "foo.com") == 0);
2295 tt_want(evhttp_uri_get_port(uri
) == -1);
2296 tt_want(strcmp(evhttp_uri_get_path(uri
), "/") == 0);
2297 tt_want(evhttp_uri_get_query(uri
) == NULL
);
2298 tt_want(strcmp(evhttp_uri_get_fragment(uri
), "frag@ment") == 0);
2299 TT_URI("scheme://%75ser@foo.com/#frag@ment");
2300 evhttp_uri_free(uri
);
2302 uri
= URI_PARSE("file:///some/path/to/the/file");
2303 tt_want(strcmp(evhttp_uri_get_scheme(uri
), "file") == 0);
2304 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2305 tt_want(strcmp(evhttp_uri_get_host(uri
), "") == 0);
2306 tt_want(evhttp_uri_get_port(uri
) == -1);
2307 tt_want(strcmp(evhttp_uri_get_path(uri
), "/some/path/to/the/file") == 0);
2308 tt_want(evhttp_uri_get_query(uri
) == NULL
);
2309 tt_want(evhttp_uri_get_fragment(uri
) == NULL
);
2310 TT_URI("file:///some/path/to/the/file");
2311 evhttp_uri_free(uri
);
2313 uri
= URI_PARSE("///some/path/to/the-file");
2314 tt_want(uri
!= NULL
);
2315 tt_want(evhttp_uri_get_scheme(uri
) == NULL
);
2316 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2317 tt_want(strcmp(evhttp_uri_get_host(uri
), "") == 0);
2318 tt_want(evhttp_uri_get_port(uri
) == -1);
2319 tt_want(strcmp(evhttp_uri_get_path(uri
), "/some/path/to/the-file") == 0);
2320 tt_want(evhttp_uri_get_query(uri
) == NULL
);
2321 tt_want(evhttp_uri_get_fragment(uri
) == NULL
);
2322 TT_URI("///some/path/to/the-file");
2323 evhttp_uri_free(uri
);
2325 uri
= URI_PARSE("/s:ome/path/to/the-file?q=99#fred");
2326 tt_want(uri
!= NULL
);
2327 tt_want(evhttp_uri_get_scheme(uri
) == NULL
);
2328 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2329 tt_want(evhttp_uri_get_host(uri
) == NULL
);
2330 tt_want(evhttp_uri_get_port(uri
) == -1);
2331 tt_want(strcmp(evhttp_uri_get_path(uri
), "/s:ome/path/to/the-file") == 0);
2332 tt_want(strcmp(evhttp_uri_get_query(uri
), "q=99") == 0);
2333 tt_want(strcmp(evhttp_uri_get_fragment(uri
), "fred") == 0);
2334 TT_URI("/s:ome/path/to/the-file?q=99#fred");
2335 evhttp_uri_free(uri
);
2337 uri
= URI_PARSE("relative/path/with/co:lon");
2338 tt_want(uri
!= NULL
);
2339 tt_want(evhttp_uri_get_scheme(uri
) == NULL
);
2340 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2341 tt_want(evhttp_uri_get_host(uri
) == NULL
);
2342 tt_want(evhttp_uri_get_port(uri
) == -1);
2343 tt_want(strcmp(evhttp_uri_get_path(uri
), "relative/path/with/co:lon") == 0);
2344 tt_want(evhttp_uri_get_query(uri
) == NULL
);
2345 tt_want(evhttp_uri_get_fragment(uri
) == NULL
);
2346 TT_URI("relative/path/with/co:lon");
2347 evhttp_uri_free(uri
);
2349 uri
= URI_PARSE("bob?q=99&q2=q?33#fr?ed");
2350 tt_want(uri
!= NULL
);
2351 tt_want(evhttp_uri_get_scheme(uri
) == NULL
);
2352 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2353 tt_want(evhttp_uri_get_host(uri
) == NULL
);
2354 tt_want(evhttp_uri_get_port(uri
) == -1);
2355 tt_want(strcmp(evhttp_uri_get_path(uri
), "bob") == 0);
2356 tt_want(strcmp(evhttp_uri_get_query(uri
), "q=99&q2=q?33") == 0);
2357 tt_want(strcmp(evhttp_uri_get_fragment(uri
), "fr?ed") == 0);
2358 TT_URI("bob?q=99&q2=q?33#fr?ed");
2359 evhttp_uri_free(uri
);
2361 uri
= URI_PARSE("#fr?ed");
2362 tt_want(uri
!= NULL
);
2363 tt_want(evhttp_uri_get_scheme(uri
) == NULL
);
2364 tt_want(evhttp_uri_get_userinfo(uri
) == NULL
);
2365 tt_want(evhttp_uri_get_host(uri
) == NULL
);
2366 tt_want(evhttp_uri_get_port(uri
) == -1);
2367 tt_want(strcmp(evhttp_uri_get_path(uri
), "") == 0);
2368 tt_want(evhttp_uri_get_query(uri
) == NULL
);
2369 tt_want(strcmp(evhttp_uri_get_fragment(uri
), "fr?ed") == 0);
2371 evhttp_uri_free(uri
);
2378 http_uriencode_test(void *ptr
)
2380 char *s
=NULL
, *s2
=NULL
;
2383 #define ENC(from,want,plus) do { \
2384 s = evhttp_uriencode((from), -1, (plus)); \
2386 tt_str_op(s,==,(want)); \
2388 s2 = evhttp_uridecode((s), (plus), &sz); \
2390 tt_str_op(s2,==,(from)); \
2391 tt_int_op(sz,==,strlen(from)); \
2395 } while (/*CONSTCOND*/0)
2397 #define DEC(from,want,dp) do { \
2398 s = evhttp_uridecode((from),(dp),&sz); \
2400 tt_str_op(s,==,(want)); \
2401 tt_int_op(sz,==,strlen(want)); \
2404 } while (/*CONSTCOND*/0)
2406 #define OLD_DEC(from,want) do { \
2407 s = evhttp_decode_uri((from)); \
2409 tt_str_op(s,==,(want)); \
2412 } while (/*CONSTCOND*/0)
2415 ENC("Hello", "Hello",0);
2419 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-.~_",
2420 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-.~_",0);
2423 ENC("\xff\xf0\xe0", "%FF%F0%E0",0);
2424 ENC("\x01\x19", "%01%19",1);
2425 ENC("http://www.ietf.org/rfc/rfc3986.txt",
2426 "http%3A%2F%2Fwww.ietf.org%2Frfc%2Frfc3986.txt",1);
2428 ENC("1+2=3", "1%2B2%3D3",1);
2429 ENC("1+2=3", "1%2B2%3D3",0);
2431 /* Now try encoding with internal NULs. */
2432 s
= evhttp_uriencode("hello\0world", 11, 0);
2434 tt_str_op(s
,==,"hello%00world");
2438 /* Now try out some decoding cases that we don't generate with
2439 * encode_uri: Make sure that malformed stuff doesn't crash... */
2440 DEC("%%xhello th+ere \xff",
2441 "%%xhello th+ere \xff", 0);
2442 /* Make sure plus decoding works */
2443 DEC("plus+should%20work+", "plus should work ",1);
2444 /* Try some lowercase hex */
2445 DEC("%f0%a0%b0", "\xf0\xa0\xb0",1);
2447 /* Try an internal NUL. */
2449 s
= evhttp_uridecode("%00%00x%00%00", 1, &sz
);
2451 tt_assert(!memcmp(s
, "\0\0x\0\0", 5));
2455 /* Try with size == NULL */
2457 s
= evhttp_uridecode("%00%00x%00%00", 1, NULL
);
2458 tt_assert(!memcmp(s
, "\0\0x\0\0", 5));
2462 /* Test out the crazy old behavior of the deprecated
2463 * evhttp_decode_uri */
2464 OLD_DEC("http://example.com/normal+path/?key=val+with+spaces",
2465 "http://example.com/normal+path/?key=val with spaces");
2478 http_base_test(void *ptr
)
2480 struct event_base
*base
= NULL
;
2481 struct bufferevent
*bev
;
2483 const char *http_request
;
2484 ev_uint16_t port
= 0;
2487 base
= event_base_new();
2488 http
= http_setup(&port
, base
);
2490 fd
= http_connect("127.0.0.1", port
);
2492 /* Stupid thing to send a request */
2493 bev
= bufferevent_socket_new(base
, fd
, 0);
2494 bufferevent_setcb(bev
, http_readcb
, http_writecb
,
2495 http_errorcb
, base
);
2496 bufferevent_base_set(base
, bev
);
2499 "GET /test HTTP/1.1\r\n"
2500 "Host: somehost\r\n"
2501 "Connection: close\r\n"
2504 bufferevent_write(bev
, http_request
, strlen(http_request
));
2506 event_base_dispatch(base
);
2508 bufferevent_free(bev
);
2509 evutil_closesocket(fd
);
2513 tt_int_op(test_ok
, ==, 2);
2517 event_base_free(base
);
2521 * the server is just going to close the connection if it times out during
2522 * reading the headers.
2526 http_incomplete_readcb(struct bufferevent
*bev
, void *arg
)
2529 event_base_loopexit(exit_base
,NULL
);
2533 http_incomplete_errorcb(struct bufferevent
*bev
, short what
, void *arg
)
2535 if (what
== (BEV_EVENT_READING
|BEV_EVENT_EOF
))
2539 event_base_loopexit(exit_base
,NULL
);
2543 http_incomplete_writecb(struct bufferevent
*bev
, void *arg
)
2546 evutil_socket_t fd
= *(evutil_socket_t
*)arg
;
2547 /* terminate the write side to simulate EOF */
2548 shutdown(fd
, SHUT_WR
);
2550 if (evbuffer_get_length(bufferevent_get_output(bev
)) == 0) {
2551 /* enable reading of the reply */
2552 bufferevent_enable(bev
, EV_READ
);
2558 _http_incomplete_test(struct basic_test_data
*data
, int use_timeout
)
2560 struct bufferevent
*bev
;
2562 const char *http_request
;
2563 ev_uint16_t port
= 0;
2564 struct timeval tv_start
, tv_end
;
2566 exit_base
= data
->base
;
2570 http
= http_setup(&port
, data
->base
);
2571 evhttp_set_timeout(http
, 1);
2573 fd
= http_connect("127.0.0.1", port
);
2575 /* Stupid thing to send a request */
2576 bev
= bufferevent_socket_new(data
->base
, fd
, 0);
2577 bufferevent_setcb(bev
,
2578 http_incomplete_readcb
, http_incomplete_writecb
,
2579 http_incomplete_errorcb
, use_timeout
? NULL
: &fd
);
2582 "GET /test HTTP/1.1\r\n"
2583 "Host: somehost\r\n";
2585 bufferevent_write(bev
, http_request
, strlen(http_request
));
2587 evutil_gettimeofday(&tv_start
, NULL
);
2589 event_base_dispatch(data
->base
);
2591 evutil_gettimeofday(&tv_end
, NULL
);
2592 evutil_timersub(&tv_end
, &tv_start
, &tv_end
);
2594 bufferevent_free(bev
);
2596 evutil_closesocket(fd
);
2601 if (use_timeout
&& tv_end
.tv_sec
>= 3) {
2602 tt_abort_msg("time");
2603 } else if (!use_timeout
&& tv_end
.tv_sec
>= 1) {
2604 /* we should be done immediately */
2605 tt_abort_msg("time");
2608 tt_int_op(test_ok
, ==, 2);
2613 http_incomplete_test(void *arg
)
2615 _http_incomplete_test(arg
, 0);
2618 http_incomplete_timeout_test(void *arg
)
2620 _http_incomplete_test(arg
, 1);
2624 * the server is going to reply with chunked data.
2628 http_chunked_readcb(struct bufferevent
*bev
, void *arg
)
2634 http_chunked_errorcb(struct bufferevent
*bev
, short what
, void *arg
)
2641 if ((what
& BEV_EVENT_EOF
) != 0) {
2642 struct evhttp_request
*req
= evhttp_request_new(NULL
, NULL
);
2644 enum message_read_status done
;
2646 /* req->kind = EVHTTP_RESPONSE; */
2647 done
= evhttp_parse_firstline(req
, bufferevent_get_input(bev
));
2648 if (done
!= ALL_DATA_READ
)
2651 done
= evhttp_parse_headers(req
, bufferevent_get_input(bev
));
2652 if (done
!= ALL_DATA_READ
)
2655 header
= evhttp_find_header(evhttp_request_get_input_headers(req
), "Transfer-Encoding");
2656 if (header
== NULL
|| strcmp(header
, "chunked"))
2659 header
= evhttp_find_header(evhttp_request_get_input_headers(req
), "Connection");
2660 if (header
== NULL
|| strcmp(header
, "close"))
2663 header
= evbuffer_readln(bufferevent_get_input(bev
), NULL
, EVBUFFER_EOL_CRLF
);
2667 if (strcmp(header
, "d")) {
2668 free(__UNCONST(header
));
2671 free(__UNCONST(header
));
2673 if (strncmp((char *)evbuffer_pullup(bufferevent_get_input(bev
), 13),
2674 "This is funny", 13))
2677 evbuffer_drain(bufferevent_get_input(bev
), 13 + 2);
2679 header
= evbuffer_readln(bufferevent_get_input(bev
), NULL
, EVBUFFER_EOL_CRLF
);
2683 if (strcmp(header
, "12"))
2685 free(__UNCONST(header
));
2687 if (strncmp((char *)evbuffer_pullup(bufferevent_get_input(bev
), 18),
2688 "but not hilarious.", 18))
2691 evbuffer_drain(bufferevent_get_input(bev
), 18 + 2);
2693 header
= evbuffer_readln(bufferevent_get_input(bev
), NULL
, EVBUFFER_EOL_CRLF
);
2697 if (strcmp(header
, "8")) {
2698 free(__UNCONST(header
));
2701 free(__UNCONST(header
));
2703 if (strncmp((char *)evbuffer_pullup(bufferevent_get_input(bev
), 8),
2707 evbuffer_drain(bufferevent_get_input(bev
), 8 + 2);
2709 header
= evbuffer_readln(bufferevent_get_input(bev
), NULL
, EVBUFFER_EOL_CRLF
);
2713 if (strcmp(header
, "0")) {
2714 free(__UNCONST(header
));
2717 free(__UNCONST(header
));
2721 evhttp_request_free(req
);
2725 event_base_loopexit(arg
, NULL
);
2729 http_chunked_writecb(struct bufferevent
*bev
, void *arg
)
2731 if (evbuffer_get_length(bufferevent_get_output(bev
)) == 0) {
2732 /* enable reading of the reply */
2733 bufferevent_enable(bev
, EV_READ
);
2739 http_chunked_request_done(struct evhttp_request
*req
, void *arg
)
2741 if (evhttp_request_get_response_code(req
) != HTTP_OK
) {
2742 fprintf(stderr
, "FAILED\n");
2746 if (evhttp_find_header(evhttp_request_get_input_headers(req
),
2747 "Transfer-Encoding") == NULL
) {
2748 fprintf(stderr
, "FAILED\n");
2752 if (evbuffer_get_length(evhttp_request_get_input_buffer(req
)) != 13 + 18 + 8) {
2753 fprintf(stderr
, "FAILED\n");
2757 if (strncmp((char *)evbuffer_pullup(evhttp_request_get_input_buffer(req
), 13 + 18 + 8),
2758 "This is funnybut not hilarious.bwv 1052",
2760 fprintf(stderr
, "FAILED\n");
2765 event_base_loopexit(arg
, NULL
);
2769 http_chunk_out_test(void *arg
)
2771 struct basic_test_data
*data
= arg
;
2772 struct bufferevent
*bev
;
2774 const char *http_request
;
2775 ev_uint16_t port
= 0;
2776 struct timeval tv_start
, tv_end
;
2777 struct evhttp_connection
*evcon
= NULL
;
2778 struct evhttp_request
*req
= NULL
;
2781 exit_base
= data
->base
;
2784 http
= http_setup(&port
, data
->base
);
2786 fd
= http_connect("127.0.0.1", port
);
2788 /* Stupid thing to send a request */
2789 bev
= bufferevent_socket_new(data
->base
, fd
, 0);
2790 bufferevent_setcb(bev
,
2791 http_chunked_readcb
, http_chunked_writecb
,
2792 http_chunked_errorcb
, data
->base
);
2795 "GET /chunked HTTP/1.1\r\n"
2796 "Host: somehost\r\n"
2797 "Connection: close\r\n"
2800 bufferevent_write(bev
, http_request
, strlen(http_request
));
2802 evutil_gettimeofday(&tv_start
, NULL
);
2804 event_base_dispatch(data
->base
);
2806 bufferevent_free(bev
);
2808 evutil_gettimeofday(&tv_end
, NULL
);
2809 evutil_timersub(&tv_end
, &tv_start
, &tv_end
);
2811 tt_int_op(tv_end
.tv_sec
, <, 1);
2813 tt_int_op(test_ok
, ==, 2);
2815 /* now try again with the regular connection object */
2816 evcon
= evhttp_connection_base_new(data
->base
, NULL
, "127.0.0.1", port
);
2819 /* make two requests to check the keepalive behavior */
2820 for (i
= 0; i
< 2; i
++) {
2822 req
= evhttp_request_new(http_chunked_request_done
,data
->base
);
2824 /* Add the information that we care about */
2825 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
2827 /* We give ownership of the request to the connection */
2828 if (evhttp_make_request(evcon
, req
,
2829 EVHTTP_REQ_GET
, "/chunked") == -1) {
2830 tt_abort_msg("Couldn't make request");
2833 event_base_dispatch(data
->base
);
2835 tt_assert(test_ok
== 1);
2840 evhttp_connection_free(evcon
);
2846 http_stream_out_test(void *arg
)
2848 struct basic_test_data
*data
= arg
;
2849 ev_uint16_t port
= 0;
2850 struct evhttp_connection
*evcon
= NULL
;
2851 struct evhttp_request
*req
= NULL
;
2854 exit_base
= data
->base
;
2856 http
= http_setup(&port
, data
->base
);
2858 evcon
= evhttp_connection_base_new(data
->base
, NULL
, "127.0.0.1", port
);
2862 * At this point, we want to schedule a request to the HTTP
2863 * server using our make request method.
2866 req
= evhttp_request_new(http_request_done
,
2867 __UNCONST("This is funnybut not hilarious.bwv 1052"));
2869 /* Add the information that we care about */
2870 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
2872 /* We give ownership of the request to the connection */
2873 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/streamed")
2875 tt_abort_msg("Couldn't make request");
2878 event_base_dispatch(data
->base
);
2882 evhttp_connection_free(evcon
);
2888 http_stream_in_chunk(struct evhttp_request
*req
, void *arg
)
2890 struct evbuffer
*reply
= arg
;
2892 if (evhttp_request_get_response_code(req
) != HTTP_OK
) {
2893 fprintf(stderr
, "FAILED\n");
2897 evbuffer_add_buffer(reply
, evhttp_request_get_input_buffer(req
));
2901 http_stream_in_done(struct evhttp_request
*req
, void *arg
)
2903 if (evbuffer_get_length(evhttp_request_get_input_buffer(req
)) != 0) {
2904 fprintf(stderr
, "FAILED\n");
2908 event_base_loopexit(exit_base
, NULL
);
2912 * Makes a request and reads the response in chunks.
2915 _http_stream_in_test(struct basic_test_data
*data
, char const *url
,
2916 size_t expected_len
, char const *expected
)
2918 struct evhttp_connection
*evcon
;
2919 struct evbuffer
*reply
= evbuffer_new();
2920 struct evhttp_request
*req
= NULL
;
2921 ev_uint16_t port
= 0;
2923 exit_base
= data
->base
;
2924 http
= http_setup(&port
, data
->base
);
2926 evcon
= evhttp_connection_base_new(data
->base
, NULL
,"127.0.0.1", port
);
2929 req
= evhttp_request_new(http_stream_in_done
, reply
);
2930 evhttp_request_set_chunked_cb(req
, http_stream_in_chunk
);
2932 /* We give ownership of the request to the connection */
2933 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, url
) == -1) {
2934 tt_abort_msg("Couldn't make request");
2937 event_base_dispatch(data
->base
);
2939 if (evbuffer_get_length(reply
) != expected_len
) {
2940 TT_DIE(("reply length %lu; expected %lu; FAILED (%s)\n",
2941 (unsigned long)evbuffer_get_length(reply
),
2942 (unsigned long)expected_len
,
2943 (char*)evbuffer_pullup(reply
, -1)));
2946 if (memcmp(evbuffer_pullup(reply
, -1), expected
, expected_len
) != 0) {
2947 tt_abort_msg("Memory mismatch");
2953 evbuffer_free(reply
);
2955 evhttp_connection_free(evcon
);
2961 http_stream_in_test(void *arg
)
2963 _http_stream_in_test(arg
, "/chunked", 13 + 18 + 8,
2964 "This is funnybut not hilarious.bwv 1052");
2966 _http_stream_in_test(arg
, "/test", strlen(BASIC_REQUEST_BODY
),
2967 BASIC_REQUEST_BODY
);
2971 http_stream_in_cancel_chunk(struct evhttp_request
*req
, void *arg
)
2973 tt_int_op(evhttp_request_get_response_code(req
), ==, HTTP_OK
);
2976 evhttp_cancel_request(req
);
2977 event_base_loopexit(arg
, NULL
);
2981 http_stream_in_cancel_done(struct evhttp_request
*req
, void *arg
)
2983 /* should never be called */
2984 tt_fail_msg("In cancel done");
2988 http_stream_in_cancel_test(void *arg
)
2990 struct basic_test_data
*data
= arg
;
2991 struct evhttp_connection
*evcon
;
2992 struct evhttp_request
*req
= NULL
;
2993 ev_uint16_t port
= 0;
2995 http
= http_setup(&port
, data
->base
);
2997 evcon
= evhttp_connection_base_new(data
->base
, NULL
, "127.0.0.1", port
);
3000 req
= evhttp_request_new(http_stream_in_cancel_done
, data
->base
);
3001 evhttp_request_set_chunked_cb(req
, http_stream_in_cancel_chunk
);
3003 /* We give ownership of the request to the connection */
3004 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/chunked") == -1) {
3005 tt_abort_msg("Couldn't make request");
3008 event_base_dispatch(data
->base
);
3012 evhttp_connection_free(evcon
);
3018 http_connection_fail_done(struct evhttp_request
*req
, void *arg
)
3020 struct evhttp_connection
*evcon
= arg
;
3021 struct event_base
*base
= evhttp_connection_get_base(evcon
);
3023 /* An ENETUNREACH error results in an unrecoverable
3024 * evhttp_connection error (see evhttp_connection_fail()). The
3025 * connection will be reset, and the user will be notified with a NULL
3029 evhttp_connection_free(evcon
);
3034 event_base_loopexit(base
, NULL
);
3037 /* Test unrecoverable evhttp_connection errors by generating an ENETUNREACH
3038 * error on connection. */
3040 http_connection_fail_test(void *arg
)
3042 struct basic_test_data
*data
= arg
;
3043 ev_uint16_t port
= 0;
3044 struct evhttp_connection
*evcon
= NULL
;
3045 struct evhttp_request
*req
= NULL
;
3047 exit_base
= data
->base
;
3050 /* auto detect a port */
3051 http
= http_setup(&port
, data
->base
);
3055 /* Pick an unroutable address. This administratively scoped multicast
3056 * address should do when working with TCP. */
3057 evcon
= evhttp_connection_base_new(data
->base
, NULL
, "239.10.20.30", 80);
3061 * At this point, we want to schedule an HTTP GET request
3062 * server using our make request method.
3065 req
= evhttp_request_new(http_connection_fail_done
, evcon
);
3068 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/") == -1) {
3069 tt_abort_msg("Couldn't make request");
3072 event_base_dispatch(data
->base
);
3074 tt_int_op(test_ok
, ==, 1);
3081 http_connection_retry_done(struct evhttp_request
*req
, void *arg
)
3084 tt_int_op(evhttp_request_get_response_code(req
), !=, HTTP_OK
);
3085 if (evhttp_find_header(evhttp_request_get_input_headers(req
), "Content-Type") != NULL
) {
3086 tt_abort_msg("(content type)\n");
3089 tt_uint_op(evbuffer_get_length(evhttp_request_get_input_buffer(req
)), ==, 0);
3093 event_base_loopexit(arg
,NULL
);
3096 static struct event_base
*http_make_web_server_base
=NULL
;
3098 http_make_web_server(evutil_socket_t fd
, short what
, void *arg
)
3100 ev_uint16_t port
= *(ev_uint16_t
*)arg
;
3101 http
= http_setup(&port
, http_make_web_server_base
);
3105 http_connection_retry_test(void *arg
)
3107 struct basic_test_data
*data
= arg
;
3108 ev_uint16_t port
= 0;
3109 struct evhttp_connection
*evcon
= NULL
;
3110 struct evhttp_request
*req
= NULL
;
3111 struct timeval tv
, tv_start
, tv_end
;
3113 exit_base
= data
->base
;
3116 /* auto detect a port */
3117 http
= http_setup(&port
, data
->base
);
3121 evcon
= evhttp_connection_base_new(data
->base
, NULL
, "127.0.0.1", port
);
3124 evhttp_connection_set_timeout(evcon
, 1);
3125 /* also bind to local host */
3126 evhttp_connection_set_local_address(evcon
, "127.0.0.1");
3129 * At this point, we want to schedule an HTTP GET request
3130 * server using our make request method.
3133 req
= evhttp_request_new(http_connection_retry_done
, data
->base
);
3136 /* Add the information that we care about */
3137 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
3139 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
,
3140 "/?arg=val") == -1) {
3141 tt_abort_msg("Couldn't make request");
3144 evutil_gettimeofday(&tv_start
, NULL
);
3145 event_base_dispatch(data
->base
);
3146 evutil_gettimeofday(&tv_end
, NULL
);
3147 evutil_timersub(&tv_end
, &tv_start
, &tv_end
);
3148 tt_int_op(tv_end
.tv_sec
, <, 1);
3150 tt_int_op(test_ok
, ==, 1);
3153 * now test the same but with retries
3157 evhttp_connection_set_timeout(evcon
, 1);
3158 evhttp_connection_set_retries(evcon
, 1);
3160 req
= evhttp_request_new(http_connection_retry_done
, data
->base
);
3163 /* Add the information that we care about */
3164 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
3166 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
,
3167 "/?arg=val") == -1) {
3168 tt_abort_msg("Couldn't make request");
3171 evutil_gettimeofday(&tv_start
, NULL
);
3172 event_base_dispatch(data
->base
);
3173 evutil_gettimeofday(&tv_end
, NULL
);
3174 evutil_timersub(&tv_end
, &tv_start
, &tv_end
);
3175 tt_int_op(tv_end
.tv_sec
, >, 1);
3176 tt_int_op(tv_end
.tv_sec
, <, 6);
3178 tt_assert(test_ok
== 1);
3181 * now test the same but with retries and give it a web server
3186 evhttp_connection_set_timeout(evcon
, 1);
3187 evhttp_connection_set_retries(evcon
, 3);
3189 req
= evhttp_request_new(http_dispatcher_test_done
, data
->base
);
3192 /* Add the information that we care about */
3193 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
3195 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
,
3196 "/?arg=val") == -1) {
3197 tt_abort_msg("Couldn't make request");
3200 /* start up a web server one second after the connection tried
3203 evutil_timerclear(&tv
);
3205 http_make_web_server_base
= data
->base
;
3206 event_base_once(data
->base
, -1, EV_TIMEOUT
, http_make_web_server
, &port
, &tv
);
3208 evutil_gettimeofday(&tv_start
, NULL
);
3209 event_base_dispatch(data
->base
);
3210 evutil_gettimeofday(&tv_end
, NULL
);
3212 evutil_timersub(&tv_end
, &tv_start
, &tv_end
);
3214 tt_int_op(tv_end
.tv_sec
, >, 1);
3215 tt_int_op(tv_end
.tv_sec
, <, 6);
3217 tt_int_op(test_ok
, ==, 1);
3221 evhttp_connection_free(evcon
);
3227 http_primitives(void *ptr
)
3229 char *escaped
= NULL
;
3230 struct evhttp
*xhttp
= NULL
;
3232 escaped
= evhttp_htmlescape("<script>");
3234 tt_str_op(escaped
, ==, "<script>");
3237 escaped
= evhttp_htmlescape("\"\'&");
3239 tt_str_op(escaped
, ==, ""'&");
3241 xhttp
= evhttp_new(NULL
);
3243 tt_int_op(evhttp_set_cb(xhttp
, "/test", http_basic_cb
, NULL
), ==, 0);
3244 tt_int_op(evhttp_set_cb(xhttp
, "/test", http_basic_cb
, NULL
), ==, -1);
3245 tt_int_op(evhttp_del_cb(xhttp
, "/test"), ==, 0);
3246 tt_int_op(evhttp_del_cb(xhttp
, "/test"), ==, -1);
3247 tt_int_op(evhttp_set_cb(xhttp
, "/test", http_basic_cb
, NULL
), ==, 0);
3257 http_multi_line_header_test(void *arg
)
3259 struct basic_test_data
*data
= arg
;
3260 struct bufferevent
*bev
= NULL
;
3261 evutil_socket_t fd
= -1;
3262 const char *http_start_request
;
3263 ev_uint16_t port
= 0;
3267 http
= http_setup(&port
, data
->base
);
3269 fd
= http_connect("127.0.0.1", port
);
3271 /* Stupid thing to send a request */
3272 bev
= bufferevent_socket_new(data
->base
, fd
, 0);
3273 bufferevent_setcb(bev
, http_readcb
, http_writecb
,
3274 http_errorcb
, data
->base
);
3276 http_start_request
=
3277 "GET /test HTTP/1.1\r\n"
3278 "Host: somehost\r\n"
3279 "Connection: close\r\n"
3280 "X-Multi: aaaaaaaa\r\n"
3286 bufferevent_write(bev
, http_start_request
, strlen(http_start_request
));
3288 event_base_dispatch(data
->base
);
3290 tt_int_op(test_ok
, ==, 4);
3293 bufferevent_free(bev
);
3295 evutil_closesocket(fd
);
3301 http_request_bad(struct evhttp_request
*req
, void *arg
)
3304 fprintf(stderr
, "FAILED\n");
3309 event_base_loopexit(arg
, NULL
);
3313 http_negative_content_length_test(void *arg
)
3315 struct basic_test_data
*data
= arg
;
3316 ev_uint16_t port
= 0;
3317 struct evhttp_connection
*evcon
= NULL
;
3318 struct evhttp_request
*req
= NULL
;
3322 http
= http_setup(&port
, data
->base
);
3324 evcon
= evhttp_connection_base_new(data
->base
, NULL
, "127.0.0.1", port
);
3328 * At this point, we want to schedule a request to the HTTP
3329 * server using our make request method.
3332 req
= evhttp_request_new(http_request_bad
, data
->base
);
3334 /* Cause the response to have a negative content-length */
3335 evhttp_add_header(evhttp_request_get_output_headers(req
), "X-Negative", "makeitso");
3337 /* We give ownership of the request to the connection */
3338 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/test") == -1) {
3339 tt_abort_msg("Couldn't make request");
3342 event_base_dispatch(data
->base
);
3346 evhttp_connection_free(evcon
);
3353 http_data_length_constraints_test_done(struct evhttp_request
*req
, void *arg
)
3356 tt_int_op(evhttp_request_get_response_code(req
), ==, HTTP_BADREQUEST
);
3358 event_base_loopexit(arg
, NULL
);
3362 http_large_entity_test_done(struct evhttp_request
*req
, void *arg
)
3365 tt_int_op(evhttp_request_get_response_code(req
), ==, HTTP_ENTITYTOOLARGE
);
3367 event_base_loopexit(arg
, NULL
);
3371 http_data_length_constraints_test(void *arg
)
3373 struct basic_test_data
*data
= arg
;
3374 ev_uint16_t port
= 0;
3375 struct evhttp_connection
*evcon
= NULL
;
3376 struct evhttp_request
*req
= NULL
;
3377 char long_str
[8192];
3381 http
= http_setup(&port
, data
->base
);
3383 evcon
= evhttp_connection_base_new(data
->base
, NULL
, "127.0.0.1", port
);
3386 /* also bind to local host */
3387 evhttp_connection_set_local_address(evcon
, "127.0.0.1");
3390 * At this point, we want to schedule an HTTP GET request
3391 * server using our make request method.
3394 req
= evhttp_request_new(http_data_length_constraints_test_done
, data
->base
);
3397 memset(long_str
, 'a', 8192);
3398 long_str
[8191] = '\0';
3399 /* Add the information that we care about */
3400 evhttp_set_max_headers_size(http
, 8191);
3401 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
3402 evhttp_add_header(evhttp_request_get_output_headers(req
), "Longheader", long_str
);
3404 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, "/?arg=val") == -1) {
3405 tt_abort_msg("Couldn't make request");
3407 event_base_dispatch(data
->base
);
3409 req
= evhttp_request_new(http_data_length_constraints_test_done
, data
->base
);
3411 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
3413 /* GET /?arg=verylongvalue HTTP/1.1 */
3414 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_GET
, long_str
) == -1) {
3415 tt_abort_msg("Couldn't make request");
3417 event_base_dispatch(data
->base
);
3419 evhttp_set_max_body_size(http
, 8190);
3420 req
= evhttp_request_new(http_data_length_constraints_test_done
, data
->base
);
3421 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
3422 evbuffer_add_printf(evhttp_request_get_output_buffer(req
), "%s", long_str
);
3423 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_POST
, "/") == -1) {
3424 tt_abort_msg("Couldn't make request");
3426 event_base_dispatch(data
->base
);
3428 req
= evhttp_request_new(http_large_entity_test_done
, data
->base
);
3429 evhttp_add_header(evhttp_request_get_output_headers(req
), "Host", "somehost");
3430 evhttp_add_header(evhttp_request_get_output_headers(req
), "Expect", "100-continue");
3431 evbuffer_add_printf(evhttp_request_get_output_buffer(req
), "%s", long_str
);
3432 if (evhttp_make_request(evcon
, req
, EVHTTP_REQ_POST
, "/") == -1) {
3433 tt_abort_msg("Couldn't make request");
3435 event_base_dispatch(data
->base
);
3440 evhttp_connection_free(evcon
);
3446 * Testing client reset of server chunked connections
3449 struct terminate_state
{
3450 struct event_base
*base
;
3451 struct evhttp_request
*req
;
3452 struct bufferevent
*bev
;
3458 terminate_chunked_trickle_cb(evutil_socket_t fd
, short events
, void *arg
)
3460 struct terminate_state
*state
= arg
;
3461 struct evbuffer
*evb
;
3464 if (evhttp_request_get_connection(state
->req
) == NULL
) {
3466 evhttp_request_free(state
->req
);
3467 event_base_loopexit(state
->base
,NULL
);
3471 evb
= evbuffer_new();
3472 evbuffer_add_printf(evb
, "%p", evb
);
3473 evhttp_send_reply_chunk(state
->req
, evb
);
3478 EVUTIL_ASSERT(state
);
3479 EVUTIL_ASSERT(state
->base
);
3480 event_base_once(state
->base
, -1, EV_TIMEOUT
, terminate_chunked_trickle_cb
, arg
, &tv
);
3484 terminate_chunked_close_cb(struct evhttp_connection
*evcon
, void *arg
)
3486 struct terminate_state
*state
= arg
;
3487 state
->gotclosecb
= 1;
3491 terminate_chunked_cb(struct evhttp_request
*req
, void *arg
)
3493 struct terminate_state
*state
= arg
;
3496 /* we want to know if this connection closes on us */
3497 evhttp_connection_set_closecb(
3498 evhttp_request_get_connection(req
),
3499 terminate_chunked_close_cb
, arg
);
3503 evhttp_send_reply_start(req
, HTTP_OK
, "OK");
3507 event_base_once(state
->base
, -1, EV_TIMEOUT
, terminate_chunked_trickle_cb
, arg
, &tv
);
3511 terminate_chunked_client(evutil_socket_t fd
, short event
, void *arg
)
3513 struct terminate_state
*state
= arg
;
3514 bufferevent_free(state
->bev
);
3515 evutil_closesocket(state
->fd
);
3519 terminate_readcb(struct bufferevent
*bev
, void *arg
)
3521 /* just drop the data */
3522 evbuffer_drain(bufferevent_get_input(bev
), -1);
3527 http_terminate_chunked_test(void *arg
)
3529 struct basic_test_data
*data
= arg
;
3530 struct bufferevent
*bev
= NULL
;
3532 const char *http_request
;
3533 ev_uint16_t port
= 0;
3534 evutil_socket_t fd
= -1;
3535 struct terminate_state terminate_state
;
3539 http
= http_setup(&port
, data
->base
);
3540 evhttp_del_cb(http
, "/test");
3541 tt_assert(evhttp_set_cb(http
, "/test",
3542 terminate_chunked_cb
, &terminate_state
) == 0);
3544 fd
= http_connect("127.0.0.1", port
);
3546 /* Stupid thing to send a request */
3547 bev
= bufferevent_socket_new(data
->base
, fd
, 0);
3548 bufferevent_setcb(bev
, terminate_readcb
, http_writecb
,
3549 http_errorcb
, data
->base
);
3551 memset(&terminate_state
, 0, sizeof(terminate_state
));
3552 terminate_state
.base
= data
->base
;
3553 terminate_state
.fd
= fd
;
3554 terminate_state
.bev
= bev
;
3555 terminate_state
.gotclosecb
= 0;
3557 /* first half of the http request */
3559 "GET /test HTTP/1.1\r\n"
3560 "Host: some\r\n\r\n";
3562 bufferevent_write(bev
, http_request
, strlen(http_request
));
3563 evutil_timerclear(&tv
);
3565 event_base_once(data
->base
, -1, EV_TIMEOUT
, terminate_chunked_client
, &terminate_state
,
3568 event_base_dispatch(data
->base
);
3570 if (terminate_state
.gotclosecb
== 0)
3575 evutil_closesocket(fd
);
3580 #define HTTP_LEGACY(name) \
3581 { #name, run_legacy_test_fn, TT_ISOLATED|TT_LEGACY, &legacy_setup, \
3582 http_##name##_test }
3584 #define HTTP(name) \
3585 { #name, http_##name##_test, TT_ISOLATED, &basic_setup, NULL }
3587 struct testcase_t http_testcases
[] = {
3588 { "primitives", http_primitives
, 0, NULL
, NULL
},
3589 { "base", http_base_test
, TT_FORK
, NULL
, NULL
},
3590 { "bad_headers", http_bad_header_test
, 0, NULL
, NULL
},
3591 { "parse_query", http_parse_query_test
, 0, NULL
, NULL
},
3592 { "parse_uri", http_parse_uri_test
, 0, NULL
, NULL
},
3593 { "parse_uri_nc", http_parse_uri_test
, 0, &basic_setup
, __UNCONST("nc") },
3594 { "uriencode", http_uriencode_test
, 0, NULL
, NULL
},
3601 HTTP(allowed_methods
),
3604 HTTP(persist_connection
),
3605 HTTP(connection_async
),
3606 HTTP(close_detection
),
3607 HTTP(close_detection_delay
),
3610 HTTP(incomplete_timeout
),
3611 HTTP(terminate_chunked
),
3615 HTTP(multi_line_header
),
3616 HTTP(negative_content_length
),
3621 HTTP(stream_in_cancel
),
3623 HTTP(connection_fail
),
3624 HTTP(connection_retry
),
3625 HTTP(data_length_constraints
),