regress_http.c revision 1.3 1 1.3 christos /* $NetBSD: regress_http.c,v 1.3 2013/04/11 16:56:42 christos Exp $ */
2 1.1 plunky /*
3 1.3 christos * Copyright (c) 2003-2007 Niels Provos <provos (at) citi.umich.edu>
4 1.3 christos * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
5 1.1 plunky *
6 1.1 plunky * Redistribution and use in source and binary forms, with or without
7 1.1 plunky * modification, are permitted provided that the following conditions
8 1.1 plunky * are met:
9 1.1 plunky * 1. Redistributions of source code must retain the above copyright
10 1.1 plunky * notice, this list of conditions and the following disclaimer.
11 1.1 plunky * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 plunky * notice, this list of conditions and the following disclaimer in the
13 1.1 plunky * documentation and/or other materials provided with the distribution.
14 1.1 plunky * 3. The name of the author may not be used to endorse or promote products
15 1.1 plunky * derived from this software without specific prior written permission.
16 1.1 plunky *
17 1.1 plunky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1 plunky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1 plunky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 plunky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1 plunky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.1 plunky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.1 plunky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.1 plunky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.1 plunky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 1.1 plunky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 plunky */
28 1.1 plunky
29 1.1 plunky #ifdef WIN32
30 1.1 plunky #include <winsock2.h>
31 1.3 christos #include <ws2tcpip.h>
32 1.1 plunky #include <windows.h>
33 1.1 plunky #endif
34 1.1 plunky
35 1.3 christos #include "event2/event-config.h"
36 1.3 christos #include <sys/cdefs.h>
37 1.3 christos __RCSID("$NetBSD: regress_http.c,v 1.3 2013/04/11 16:56:42 christos Exp $");
38 1.1 plunky
39 1.1 plunky #include <sys/types.h>
40 1.1 plunky #include <sys/stat.h>
41 1.3 christos #ifdef _EVENT_HAVE_SYS_TIME_H
42 1.1 plunky #include <sys/time.h>
43 1.1 plunky #endif
44 1.1 plunky #include <sys/queue.h>
45 1.1 plunky #ifndef WIN32
46 1.1 plunky #include <sys/socket.h>
47 1.1 plunky #include <signal.h>
48 1.1 plunky #include <unistd.h>
49 1.1 plunky #include <netdb.h>
50 1.1 plunky #endif
51 1.1 plunky #include <fcntl.h>
52 1.1 plunky #include <stdlib.h>
53 1.1 plunky #include <stdio.h>
54 1.1 plunky #include <string.h>
55 1.1 plunky #include <errno.h>
56 1.1 plunky
57 1.3 christos #include "event2/dns.h"
58 1.3 christos
59 1.3 christos #include "event2/event.h"
60 1.3 christos #include "event2/http.h"
61 1.3 christos #include "event2/buffer.h"
62 1.3 christos #include "event2/bufferevent.h"
63 1.3 christos #include "event2/util.h"
64 1.3 christos #include "log-internal.h"
65 1.3 christos #include "util-internal.h"
66 1.1 plunky #include "http-internal.h"
67 1.3 christos #include "regress.h"
68 1.3 christos #include "regress_testutils.h"
69 1.1 plunky
70 1.1 plunky static struct evhttp *http;
71 1.1 plunky /* set if a test needs to call loopexit on a base */
72 1.3 christos static struct event_base *exit_base;
73 1.1 plunky
74 1.3 christos static char const BASIC_REQUEST_BODY[] = "This is funny";
75 1.3 christos static void *basic_request_body = __UNCONST(BASIC_REQUEST_BODY);
76 1.1 plunky
77 1.3 christos static void http_basic_cb(struct evhttp_request *req, void *arg);
78 1.1 plunky static void http_chunked_cb(struct evhttp_request *req, void *arg);
79 1.3 christos static void http_post_cb(struct evhttp_request *req, void *arg);
80 1.3 christos static void http_put_cb(struct evhttp_request *req, void *arg);
81 1.3 christos static void http_delete_cb(struct evhttp_request *req, void *arg);
82 1.3 christos static void http_delay_cb(struct evhttp_request *req, void *arg);
83 1.1 plunky static void http_large_delay_cb(struct evhttp_request *req, void *arg);
84 1.3 christos static void http_badreq_cb(struct evhttp_request *req, void *arg);
85 1.3 christos static void http_dispatcher_cb(struct evhttp_request *req, void *arg);
86 1.3 christos static int
87 1.3 christos http_bind(struct evhttp *myhttp, ev_uint16_t *pport)
88 1.3 christos {
89 1.3 christos int port;
90 1.3 christos struct evhttp_bound_socket *sock;
91 1.3 christos
92 1.3 christos sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", *pport);
93 1.3 christos if (sock == NULL)
94 1.3 christos event_errx(1, "Could not start web server");
95 1.3 christos
96 1.3 christos port = regress_get_socket_port(evhttp_bound_socket_get_fd(sock));
97 1.3 christos if (port < 0)
98 1.3 christos return -1;
99 1.3 christos *pport = (ev_uint16_t) port;
100 1.3 christos
101 1.3 christos return 0;
102 1.3 christos }
103 1.1 plunky
104 1.1 plunky static struct evhttp *
105 1.3 christos http_setup(ev_uint16_t *pport, struct event_base *base)
106 1.1 plunky {
107 1.1 plunky struct evhttp *myhttp;
108 1.1 plunky
109 1.1 plunky /* Try a few different ports */
110 1.1 plunky myhttp = evhttp_new(base);
111 1.1 plunky
112 1.3 christos if (http_bind(myhttp, pport) < 0)
113 1.3 christos return NULL;
114 1.1 plunky
115 1.1 plunky /* Register a callback for certain types of requests */
116 1.3 christos evhttp_set_cb(myhttp, "/test", http_basic_cb, base);
117 1.3 christos evhttp_set_cb(myhttp, "/chunked", http_chunked_cb, base);
118 1.3 christos evhttp_set_cb(myhttp, "/streamed", http_chunked_cb, base);
119 1.3 christos evhttp_set_cb(myhttp, "/postit", http_post_cb, base);
120 1.3 christos evhttp_set_cb(myhttp, "/putit", http_put_cb, base);
121 1.3 christos evhttp_set_cb(myhttp, "/deleteit", http_delete_cb, base);
122 1.3 christos evhttp_set_cb(myhttp, "/delay", http_delay_cb, base);
123 1.3 christos evhttp_set_cb(myhttp, "/largedelay", http_large_delay_cb, base);
124 1.3 christos evhttp_set_cb(myhttp, "/badrequest", http_badreq_cb, base);
125 1.3 christos evhttp_set_cb(myhttp, "/", http_dispatcher_cb, base);
126 1.1 plunky return (myhttp);
127 1.1 plunky }
128 1.1 plunky
129 1.1 plunky #ifndef NI_MAXSERV
130 1.1 plunky #define NI_MAXSERV 1024
131 1.1 plunky #endif
132 1.1 plunky
133 1.3 christos static evutil_socket_t
134 1.1 plunky http_connect(const char *address, u_short port)
135 1.1 plunky {
136 1.1 plunky /* Stupid code for connecting */
137 1.3 christos struct evutil_addrinfo ai, *aitop;
138 1.1 plunky char strport[NI_MAXSERV];
139 1.3 christos
140 1.1 plunky struct sockaddr *sa;
141 1.1 plunky int slen;
142 1.3 christos evutil_socket_t fd;
143 1.3 christos
144 1.3 christos memset(&ai, 0, sizeof(ai));
145 1.1 plunky ai.ai_family = AF_INET;
146 1.1 plunky ai.ai_socktype = SOCK_STREAM;
147 1.3 christos evutil_snprintf(strport, sizeof(strport), "%d", port);
148 1.3 christos if (evutil_getaddrinfo(address, strport, &ai, &aitop) != 0) {
149 1.1 plunky event_warn("getaddrinfo");
150 1.1 plunky return (-1);
151 1.1 plunky }
152 1.1 plunky sa = aitop->ai_addr;
153 1.1 plunky slen = aitop->ai_addrlen;
154 1.3 christos
155 1.1 plunky fd = socket(AF_INET, SOCK_STREAM, 0);
156 1.1 plunky if (fd == -1)
157 1.1 plunky event_err(1, "socket failed");
158 1.1 plunky
159 1.3 christos evutil_make_socket_nonblocking(fd);
160 1.3 christos if (connect(fd, sa, slen) == -1) {
161 1.3 christos #ifdef WIN32
162 1.3 christos int tmp_err = WSAGetLastError();
163 1.3 christos if (tmp_err != WSAEINPROGRESS && tmp_err != WSAEINVAL &&
164 1.3 christos tmp_err != WSAEWOULDBLOCK)
165 1.3 christos event_err(1, "connect failed");
166 1.3 christos #else
167 1.3 christos if (errno != EINPROGRESS)
168 1.3 christos event_err(1, "connect failed");
169 1.3 christos #endif
170 1.3 christos }
171 1.1 plunky
172 1.3 christos evutil_freeaddrinfo(aitop);
173 1.1 plunky
174 1.1 plunky return (fd);
175 1.1 plunky }
176 1.1 plunky
177 1.3 christos /* Helper: do a strcmp on the contents of buf and the string s. */
178 1.3 christos static int
179 1.3 christos evbuffer_datacmp(struct evbuffer *buf, const char *s)
180 1.3 christos {
181 1.3 christos size_t b_sz = evbuffer_get_length(buf);
182 1.3 christos size_t s_sz = strlen(s);
183 1.3 christos unsigned char *d;
184 1.3 christos int r;
185 1.3 christos
186 1.3 christos if (b_sz < s_sz)
187 1.3 christos return -1;
188 1.3 christos
189 1.3 christos d = evbuffer_pullup(buf, s_sz);
190 1.3 christos if ((r = memcmp(d, s, s_sz)))
191 1.3 christos return r;
192 1.3 christos
193 1.3 christos if (b_sz > s_sz)
194 1.3 christos return 1;
195 1.3 christos else
196 1.3 christos return 0;
197 1.3 christos }
198 1.3 christos
199 1.3 christos /* Helper: Return true iff buf contains s */
200 1.3 christos static int
201 1.3 christos evbuffer_contains(struct evbuffer *buf, const char *s)
202 1.3 christos {
203 1.3 christos struct evbuffer_ptr ptr;
204 1.3 christos ptr = evbuffer_search(buf, s, strlen(s), NULL);
205 1.3 christos return ptr.pos != -1;
206 1.3 christos }
207 1.3 christos
208 1.1 plunky static void
209 1.1 plunky http_readcb(struct bufferevent *bev, void *arg)
210 1.1 plunky {
211 1.3 christos const char *what = BASIC_REQUEST_BODY;
212 1.3 christos struct event_base *my_base = arg;
213 1.1 plunky
214 1.3 christos if (evbuffer_contains(bufferevent_get_input(bev), what)) {
215 1.1 plunky struct evhttp_request *req = evhttp_request_new(NULL, NULL);
216 1.1 plunky enum message_read_status done;
217 1.1 plunky
218 1.3 christos /* req->kind = EVHTTP_RESPONSE; */
219 1.3 christos done = evhttp_parse_firstline(req, bufferevent_get_input(bev));
220 1.1 plunky if (done != ALL_DATA_READ)
221 1.1 plunky goto out;
222 1.1 plunky
223 1.3 christos done = evhttp_parse_headers(req, bufferevent_get_input(bev));
224 1.1 plunky if (done != ALL_DATA_READ)
225 1.1 plunky goto out;
226 1.1 plunky
227 1.1 plunky if (done == 1 &&
228 1.3 christos evhttp_find_header(evhttp_request_get_input_headers(req),
229 1.1 plunky "Content-Type") != NULL)
230 1.1 plunky test_ok++;
231 1.1 plunky
232 1.3 christos out:
233 1.1 plunky evhttp_request_free(req);
234 1.1 plunky bufferevent_disable(bev, EV_READ);
235 1.3 christos if (exit_base)
236 1.3 christos event_base_loopexit(exit_base, NULL);
237 1.3 christos else if (my_base)
238 1.3 christos event_base_loopexit(my_base, NULL);
239 1.3 christos else {
240 1.3 christos fprintf(stderr, "No way to exit loop!\n");
241 1.3 christos exit(1);
242 1.3 christos }
243 1.1 plunky }
244 1.1 plunky }
245 1.1 plunky
246 1.1 plunky static void
247 1.1 plunky http_writecb(struct bufferevent *bev, void *arg)
248 1.1 plunky {
249 1.3 christos if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
250 1.1 plunky /* enable reading of the reply */
251 1.1 plunky bufferevent_enable(bev, EV_READ);
252 1.1 plunky test_ok++;
253 1.1 plunky }
254 1.1 plunky }
255 1.1 plunky
256 1.1 plunky static void
257 1.1 plunky http_errorcb(struct bufferevent *bev, short what, void *arg)
258 1.1 plunky {
259 1.1 plunky test_ok = -2;
260 1.3 christos event_base_loopexit(arg, NULL);
261 1.1 plunky }
262 1.1 plunky
263 1.3 christos static void
264 1.1 plunky http_basic_cb(struct evhttp_request *req, void *arg)
265 1.1 plunky {
266 1.1 plunky struct evbuffer *evb = evbuffer_new();
267 1.3 christos int empty = evhttp_find_header(evhttp_request_get_input_headers(req), "Empty") != NULL;
268 1.1 plunky event_debug(("%s: called\n", __func__));
269 1.3 christos evbuffer_add_printf(evb, BASIC_REQUEST_BODY);
270 1.3 christos
271 1.1 plunky /* For multi-line headers test */
272 1.1 plunky {
273 1.1 plunky const char *multi =
274 1.3 christos evhttp_find_header(evhttp_request_get_input_headers(req),"X-multi");
275 1.1 plunky if (multi) {
276 1.1 plunky if (strcmp("END", multi + strlen(multi) - 3) == 0)
277 1.1 plunky test_ok++;
278 1.3 christos if (evhttp_find_header(evhttp_request_get_input_headers(req), "X-Last"))
279 1.1 plunky test_ok++;
280 1.1 plunky }
281 1.1 plunky }
282 1.1 plunky
283 1.1 plunky /* injecting a bad content-length */
284 1.3 christos if (evhttp_find_header(evhttp_request_get_input_headers(req), "X-Negative"))
285 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req),
286 1.1 plunky "Content-Length", "-100");
287 1.1 plunky
288 1.1 plunky /* allow sending of an empty reply */
289 1.1 plunky evhttp_send_reply(req, HTTP_OK, "Everything is fine",
290 1.1 plunky !empty ? evb : NULL);
291 1.1 plunky
292 1.1 plunky evbuffer_free(evb);
293 1.1 plunky }
294 1.1 plunky
295 1.1 plunky static char const* const CHUNKS[] = {
296 1.1 plunky "This is funny",
297 1.1 plunky "but not hilarious.",
298 1.1 plunky "bwv 1052"
299 1.1 plunky };
300 1.1 plunky
301 1.1 plunky struct chunk_req_state {
302 1.3 christos struct event_base *base;
303 1.1 plunky struct evhttp_request *req;
304 1.1 plunky int i;
305 1.1 plunky };
306 1.1 plunky
307 1.1 plunky static void
308 1.3 christos http_chunked_trickle_cb(evutil_socket_t fd, short events, void *arg)
309 1.1 plunky {
310 1.1 plunky struct evbuffer *evb = evbuffer_new();
311 1.1 plunky struct chunk_req_state *state = arg;
312 1.1 plunky struct timeval when = { 0, 0 };
313 1.1 plunky
314 1.1 plunky evbuffer_add_printf(evb, "%s", CHUNKS[state->i]);
315 1.1 plunky evhttp_send_reply_chunk(state->req, evb);
316 1.1 plunky evbuffer_free(evb);
317 1.1 plunky
318 1.3 christos if (++state->i < (int) (sizeof(CHUNKS)/sizeof(CHUNKS[0]))) {
319 1.3 christos event_base_once(state->base, -1, EV_TIMEOUT,
320 1.1 plunky http_chunked_trickle_cb, state, &when);
321 1.1 plunky } else {
322 1.1 plunky evhttp_send_reply_end(state->req);
323 1.1 plunky free(state);
324 1.1 plunky }
325 1.1 plunky }
326 1.1 plunky
327 1.1 plunky static void
328 1.1 plunky http_chunked_cb(struct evhttp_request *req, void *arg)
329 1.1 plunky {
330 1.1 plunky struct timeval when = { 0, 0 };
331 1.1 plunky struct chunk_req_state *state = malloc(sizeof(struct chunk_req_state));
332 1.1 plunky event_debug(("%s: called\n", __func__));
333 1.1 plunky
334 1.1 plunky memset(state, 0, sizeof(struct chunk_req_state));
335 1.1 plunky state->req = req;
336 1.3 christos state->base = arg;
337 1.3 christos
338 1.3 christos if (strcmp(evhttp_request_get_uri(req), "/streamed") == 0) {
339 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Length", "39");
340 1.3 christos }
341 1.1 plunky
342 1.3 christos /* generate a chunked/streamed reply */
343 1.1 plunky evhttp_send_reply_start(req, HTTP_OK, "Everything is fine");
344 1.1 plunky
345 1.1 plunky /* but trickle it across several iterations to ensure we're not
346 1.1 plunky * assuming it comes all at once */
347 1.3 christos event_base_once(arg, -1, EV_TIMEOUT, http_chunked_trickle_cb, state, &when);
348 1.1 plunky }
349 1.1 plunky
350 1.1 plunky static void
351 1.3 christos http_complete_write(evutil_socket_t fd, short what, void *arg)
352 1.1 plunky {
353 1.1 plunky struct bufferevent *bev = arg;
354 1.1 plunky const char *http_request = "host\r\n"
355 1.1 plunky "Connection: close\r\n"
356 1.1 plunky "\r\n";
357 1.1 plunky bufferevent_write(bev, http_request, strlen(http_request));
358 1.1 plunky }
359 1.1 plunky
360 1.1 plunky static void
361 1.3 christos http_basic_test(void *arg)
362 1.1 plunky {
363 1.3 christos struct basic_test_data *data = arg;
364 1.1 plunky struct timeval tv;
365 1.1 plunky struct bufferevent *bev;
366 1.3 christos evutil_socket_t fd;
367 1.1 plunky const char *http_request;
368 1.3 christos ev_uint16_t port = 0, port2 = 0;
369 1.1 plunky
370 1.1 plunky test_ok = 0;
371 1.1 plunky
372 1.3 christos http = http_setup(&port, data->base);
373 1.1 plunky
374 1.1 plunky /* bind to a second socket */
375 1.3 christos if (http_bind(http, &port2) == -1) {
376 1.1 plunky fprintf(stdout, "FAILED (bind)\n");
377 1.1 plunky exit(1);
378 1.1 plunky }
379 1.3 christos
380 1.1 plunky fd = http_connect("127.0.0.1", port);
381 1.1 plunky
382 1.1 plunky /* Stupid thing to send a request */
383 1.3 christos bev = bufferevent_socket_new(data->base, fd, 0);
384 1.3 christos bufferevent_setcb(bev, http_readcb, http_writecb,
385 1.3 christos http_errorcb, data->base);
386 1.1 plunky
387 1.1 plunky /* first half of the http request */
388 1.1 plunky http_request =
389 1.1 plunky "GET /test HTTP/1.1\r\n"
390 1.1 plunky "Host: some";
391 1.1 plunky
392 1.1 plunky bufferevent_write(bev, http_request, strlen(http_request));
393 1.3 christos evutil_timerclear(&tv);
394 1.1 plunky tv.tv_usec = 10000;
395 1.3 christos event_base_once(data->base,
396 1.3 christos -1, EV_TIMEOUT, http_complete_write, bev, &tv);
397 1.3 christos
398 1.3 christos event_base_dispatch(data->base);
399 1.1 plunky
400 1.3 christos tt_assert(test_ok == 3);
401 1.1 plunky
402 1.1 plunky /* connect to the second port */
403 1.1 plunky bufferevent_free(bev);
404 1.3 christos evutil_closesocket(fd);
405 1.1 plunky
406 1.3 christos fd = http_connect("127.0.0.1", port2);
407 1.1 plunky
408 1.1 plunky /* Stupid thing to send a request */
409 1.3 christos bev = bufferevent_socket_new(data->base, fd, 0);
410 1.3 christos bufferevent_setcb(bev, http_readcb, http_writecb,
411 1.3 christos http_errorcb, data->base);
412 1.1 plunky
413 1.1 plunky http_request =
414 1.1 plunky "GET /test HTTP/1.1\r\n"
415 1.1 plunky "Host: somehost\r\n"
416 1.1 plunky "Connection: close\r\n"
417 1.1 plunky "\r\n";
418 1.1 plunky
419 1.1 plunky bufferevent_write(bev, http_request, strlen(http_request));
420 1.1 plunky
421 1.3 christos event_base_dispatch(data->base);
422 1.3 christos
423 1.3 christos tt_assert(test_ok == 5);
424 1.3 christos
425 1.3 christos /* Connect to the second port again. This time, send an absolute uri. */
426 1.1 plunky bufferevent_free(bev);
427 1.3 christos evutil_closesocket(fd);
428 1.3 christos
429 1.3 christos fd = http_connect("127.0.0.1", port2);
430 1.3 christos
431 1.3 christos /* Stupid thing to send a request */
432 1.3 christos bev = bufferevent_socket_new(data->base, fd, 0);
433 1.3 christos bufferevent_setcb(bev, http_readcb, http_writecb,
434 1.3 christos http_errorcb, data->base);
435 1.3 christos
436 1.3 christos http_request =
437 1.3 christos "GET http://somehost.net/test HTTP/1.1\r\n"
438 1.3 christos "Host: somehost\r\n"
439 1.3 christos "Connection: close\r\n"
440 1.3 christos "\r\n";
441 1.3 christos
442 1.3 christos bufferevent_write(bev, http_request, strlen(http_request));
443 1.3 christos
444 1.3 christos event_base_dispatch(data->base);
445 1.3 christos
446 1.3 christos tt_assert(test_ok == 7);
447 1.1 plunky
448 1.1 plunky evhttp_free(http);
449 1.3 christos end:
450 1.3 christos ;
451 1.1 plunky }
452 1.1 plunky
453 1.1 plunky static void
454 1.3 christos http_delay_reply(evutil_socket_t fd, short what, void *arg)
455 1.1 plunky {
456 1.1 plunky struct evhttp_request *req = arg;
457 1.1 plunky
458 1.1 plunky evhttp_send_reply(req, HTTP_OK, "Everything is fine", NULL);
459 1.1 plunky
460 1.1 plunky ++test_ok;
461 1.1 plunky }
462 1.1 plunky
463 1.1 plunky static void
464 1.3 christos http_delay_cb(struct evhttp_request *req, void *arg)
465 1.1 plunky {
466 1.1 plunky struct timeval tv;
467 1.3 christos evutil_timerclear(&tv);
468 1.3 christos tv.tv_sec = 0;
469 1.3 christos tv.tv_usec = 200 * 1000;
470 1.3 christos
471 1.3 christos event_base_once(arg, -1, EV_TIMEOUT, http_delay_reply, req, &tv);
472 1.3 christos }
473 1.3 christos
474 1.3 christos static void
475 1.3 christos http_badreq_cb(struct evhttp_request *req, void *arg)
476 1.3 christos {
477 1.3 christos struct evbuffer *buf = evbuffer_new();
478 1.3 christos
479 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", "text/xml; charset=UTF-8");
480 1.3 christos evbuffer_add_printf(buf, "Hello, %s!", "127.0.0.1");
481 1.1 plunky
482 1.3 christos evhttp_send_reply(req, HTTP_OK, "OK", buf);
483 1.3 christos evbuffer_free(buf);
484 1.3 christos }
485 1.1 plunky
486 1.3 christos static void
487 1.3 christos http_badreq_errorcb(struct bufferevent *bev, short what, void *arg)
488 1.3 christos {
489 1.3 christos event_debug(("%s: called (what=%04x, arg=%p)", __func__, what, arg));
490 1.3 christos /* ignore */
491 1.1 plunky }
492 1.1 plunky
493 1.3 christos #ifndef SHUT_WR
494 1.3 christos #ifdef WIN32
495 1.3 christos #define SHUT_WR SD_SEND
496 1.3 christos #else
497 1.3 christos #define SHUT_WR 1
498 1.3 christos #endif
499 1.3 christos #endif
500 1.1 plunky
501 1.1 plunky static void
502 1.3 christos http_badreq_readcb(struct bufferevent *bev, void *arg)
503 1.1 plunky {
504 1.3 christos const char *what = "Hello, 127.0.0.1";
505 1.3 christos const char *bad_request = "400 Bad Request";
506 1.1 plunky
507 1.3 christos if (evbuffer_contains(bufferevent_get_input(bev), bad_request)) {
508 1.3 christos TT_FAIL(("%s:bad request detected", __func__));
509 1.3 christos bufferevent_disable(bev, EV_READ);
510 1.3 christos event_base_loopexit(arg, NULL);
511 1.3 christos return;
512 1.3 christos }
513 1.1 plunky
514 1.3 christos if (evbuffer_contains(bufferevent_get_input(bev), what)) {
515 1.3 christos struct evhttp_request *req = evhttp_request_new(NULL, NULL);
516 1.3 christos enum message_read_status done;
517 1.1 plunky
518 1.3 christos /* req->kind = EVHTTP_RESPONSE; */
519 1.3 christos done = evhttp_parse_firstline(req, bufferevent_get_input(bev));
520 1.3 christos if (done != ALL_DATA_READ)
521 1.3 christos goto out;
522 1.1 plunky
523 1.3 christos done = evhttp_parse_headers(req, bufferevent_get_input(bev));
524 1.3 christos if (done != ALL_DATA_READ)
525 1.3 christos goto out;
526 1.1 plunky
527 1.3 christos if (done == 1 &&
528 1.3 christos evhttp_find_header(evhttp_request_get_input_headers(req),
529 1.3 christos "Content-Type") != NULL)
530 1.3 christos test_ok++;
531 1.1 plunky
532 1.3 christos out:
533 1.3 christos evhttp_request_free(req);
534 1.3 christos evbuffer_drain(bufferevent_get_input(bev), evbuffer_get_length(bufferevent_get_input(bev)));
535 1.1 plunky }
536 1.1 plunky
537 1.3 christos shutdown(bufferevent_getfd(bev), SHUT_WR);
538 1.3 christos }
539 1.3 christos
540 1.3 christos static void
541 1.3 christos http_badreq_successcb(evutil_socket_t fd, short what, void *arg)
542 1.3 christos {
543 1.3 christos event_debug(("%s: called (what=%04x, arg=%p)", __func__, what, arg));
544 1.3 christos event_base_loopexit(exit_base, NULL);
545 1.3 christos }
546 1.1 plunky
547 1.3 christos static void
548 1.3 christos http_bad_request_test(void *arg)
549 1.3 christos {
550 1.3 christos struct basic_test_data *data = arg;
551 1.3 christos struct timeval tv;
552 1.3 christos struct bufferevent *bev = NULL;
553 1.3 christos evutil_socket_t fd;
554 1.3 christos const char *http_request;
555 1.3 christos ev_uint16_t port=0, port2=0;
556 1.1 plunky
557 1.1 plunky test_ok = 0;
558 1.3 christos exit_base = data->base;
559 1.1 plunky
560 1.3 christos http = http_setup(&port, data->base);
561 1.3 christos
562 1.3 christos /* bind to a second socket */
563 1.3 christos if (http_bind(http, &port2) == -1)
564 1.3 christos TT_DIE(("Bind socket failed"));
565 1.1 plunky
566 1.3 christos /* NULL request test */
567 1.3 christos fd = http_connect("127.0.0.1", port);
568 1.1 plunky
569 1.3 christos /* Stupid thing to send a request */
570 1.3 christos bev = bufferevent_socket_new(data->base, fd, 0);
571 1.3 christos bufferevent_setcb(bev, http_badreq_readcb, http_writecb,
572 1.3 christos http_badreq_errorcb, data->base);
573 1.3 christos bufferevent_enable(bev, EV_READ);
574 1.1 plunky
575 1.3 christos /* real NULL request */
576 1.3 christos http_request = "";
577 1.1 plunky
578 1.3 christos bufferevent_write(bev, http_request, strlen(http_request));
579 1.1 plunky
580 1.3 christos shutdown(fd, SHUT_WR);
581 1.3 christos timerclear(&tv);
582 1.3 christos tv.tv_usec = 10000;
583 1.3 christos event_base_once(data->base, -1, EV_TIMEOUT, http_badreq_successcb, bev, &tv);
584 1.1 plunky
585 1.3 christos event_base_dispatch(data->base);
586 1.1 plunky
587 1.3 christos bufferevent_free(bev);
588 1.3 christos evutil_closesocket(fd);
589 1.1 plunky
590 1.3 christos if (test_ok != 0) {
591 1.1 plunky fprintf(stdout, "FAILED\n");
592 1.1 plunky exit(1);
593 1.1 plunky }
594 1.1 plunky
595 1.3 christos /* Second answer (BAD REQUEST) on connection close */
596 1.1 plunky
597 1.3 christos /* connect to the second port */
598 1.3 christos fd = http_connect("127.0.0.1", port2);
599 1.1 plunky
600 1.3 christos /* Stupid thing to send a request */
601 1.3 christos bev = bufferevent_socket_new(data->base, fd, 0);
602 1.3 christos bufferevent_setcb(bev, http_badreq_readcb, http_writecb,
603 1.3 christos http_badreq_errorcb, data->base);
604 1.3 christos bufferevent_enable(bev, EV_READ);
605 1.1 plunky
606 1.3 christos /* first half of the http request */
607 1.3 christos http_request =
608 1.3 christos "GET /badrequest HTTP/1.0\r\n" \
609 1.3 christos "Connection: Keep-Alive\r\n" \
610 1.3 christos "\r\n";
611 1.1 plunky
612 1.3 christos bufferevent_write(bev, http_request, strlen(http_request));
613 1.1 plunky
614 1.3 christos timerclear(&tv);
615 1.3 christos tv.tv_usec = 10000;
616 1.3 christos event_base_once(data->base, -1, EV_TIMEOUT, http_badreq_successcb, bev, &tv);
617 1.1 plunky
618 1.3 christos event_base_dispatch(data->base);
619 1.1 plunky
620 1.3 christos tt_int_op(test_ok, ==, 2);
621 1.1 plunky
622 1.3 christos end:
623 1.3 christos evhttp_free(http);
624 1.3 christos if (bev)
625 1.3 christos bufferevent_free(bev);
626 1.3 christos }
627 1.1 plunky
628 1.3 christos static struct evhttp_connection *delayed_client;
629 1.1 plunky
630 1.3 christos static void
631 1.3 christos http_large_delay_cb(struct evhttp_request *req, void *arg)
632 1.3 christos {
633 1.3 christos struct timeval tv;
634 1.3 christos evutil_timerclear(&tv);
635 1.3 christos tv.tv_sec = 3;
636 1.1 plunky
637 1.3 christos event_base_once(arg, -1, EV_TIMEOUT, http_delay_reply, req, &tv);
638 1.3 christos evhttp_connection_fail(delayed_client, EVCON_HTTP_EOF);
639 1.1 plunky }
640 1.1 plunky
641 1.1 plunky /*
642 1.3 christos * HTTP DELETE test, just piggyback on the basic test
643 1.1 plunky */
644 1.1 plunky
645 1.3 christos static void
646 1.3 christos http_delete_cb(struct evhttp_request *req, void *arg)
647 1.1 plunky {
648 1.3 christos struct evbuffer *evb = evbuffer_new();
649 1.3 christos int empty = evhttp_find_header(evhttp_request_get_input_headers(req), "Empty") != NULL;
650 1.3 christos
651 1.3 christos /* Expecting a DELETE request */
652 1.3 christos if (evhttp_request_get_command(req) != EVHTTP_REQ_DELETE) {
653 1.3 christos fprintf(stdout, "FAILED (delete type)\n");
654 1.3 christos exit(1);
655 1.3 christos }
656 1.1 plunky
657 1.1 plunky event_debug(("%s: called\n", __func__));
658 1.3 christos evbuffer_add_printf(evb, BASIC_REQUEST_BODY);
659 1.1 plunky
660 1.3 christos /* allow sending of an empty reply */
661 1.3 christos evhttp_send_reply(req, HTTP_OK, "Everything is fine",
662 1.3 christos !empty ? evb : NULL);
663 1.1 plunky
664 1.1 plunky evbuffer_free(evb);
665 1.1 plunky }
666 1.1 plunky
667 1.1 plunky static void
668 1.3 christos http_delete_test(void *arg)
669 1.1 plunky {
670 1.3 christos struct basic_test_data *data = arg;
671 1.3 christos struct bufferevent *bev;
672 1.3 christos evutil_socket_t fd;
673 1.3 christos const char *http_request;
674 1.3 christos ev_uint16_t port = 0;
675 1.1 plunky
676 1.3 christos test_ok = 0;
677 1.1 plunky
678 1.3 christos http = http_setup(&port, data->base);
679 1.1 plunky
680 1.3 christos fd = http_connect("127.0.0.1", port);
681 1.1 plunky
682 1.3 christos /* Stupid thing to send a request */
683 1.3 christos bev = bufferevent_socket_new(data->base, fd, 0);
684 1.3 christos bufferevent_setcb(bev, http_readcb, http_writecb,
685 1.3 christos http_errorcb, data->base);
686 1.3 christos
687 1.3 christos http_request =
688 1.3 christos "DELETE /deleteit HTTP/1.1\r\n"
689 1.3 christos "Host: somehost\r\n"
690 1.3 christos "Connection: close\r\n"
691 1.3 christos "\r\n";
692 1.3 christos
693 1.3 christos bufferevent_write(bev, http_request, strlen(http_request));
694 1.3 christos
695 1.3 christos event_base_dispatch(data->base);
696 1.3 christos
697 1.3 christos bufferevent_free(bev);
698 1.3 christos evutil_closesocket(fd);
699 1.3 christos
700 1.3 christos evhttp_free(http);
701 1.3 christos
702 1.3 christos tt_int_op(test_ok, ==, 2);
703 1.3 christos end:
704 1.3 christos ;
705 1.3 christos }
706 1.3 christos
707 1.3 christos static void
708 1.3 christos http_allowed_methods_eventcb(struct bufferevent *bev, short what, void *arg)
709 1.3 christos {
710 1.3 christos char **output = arg;
711 1.3 christos if ((what & (BEV_EVENT_ERROR|BEV_EVENT_EOF))) {
712 1.3 christos char buf[4096];
713 1.3 christos int n;
714 1.3 christos n = evbuffer_remove(bufferevent_get_input(bev), buf,
715 1.3 christos sizeof(buf)-1);
716 1.3 christos if (n >= 0) {
717 1.3 christos buf[n]='\0';
718 1.3 christos if (*output)
719 1.3 christos free(*output);
720 1.3 christos *output = strdup(buf);
721 1.3 christos }
722 1.3 christos event_base_loopexit(exit_base, NULL);
723 1.3 christos }
724 1.3 christos }
725 1.3 christos
726 1.3 christos static void
727 1.3 christos http_allowed_methods_test(void *arg)
728 1.3 christos {
729 1.3 christos struct basic_test_data *data = arg;
730 1.3 christos struct bufferevent *bev1, *bev2, *bev3;
731 1.3 christos evutil_socket_t fd1, fd2, fd3;
732 1.3 christos const char *http_request;
733 1.3 christos char *result1=NULL, *result2=NULL, *result3=NULL;
734 1.3 christos ev_uint16_t port = 0;
735 1.3 christos
736 1.3 christos exit_base = data->base;
737 1.3 christos test_ok = 0;
738 1.3 christos
739 1.3 christos http = http_setup(&port, data->base);
740 1.3 christos
741 1.3 christos fd1 = http_connect("127.0.0.1", port);
742 1.3 christos
743 1.3 christos /* GET is out; PATCH is in. */
744 1.3 christos evhttp_set_allowed_methods(http, EVHTTP_REQ_PATCH);
745 1.3 christos
746 1.3 christos /* Stupid thing to send a request */
747 1.3 christos bev1 = bufferevent_socket_new(data->base, fd1, 0);
748 1.3 christos bufferevent_enable(bev1, EV_READ|EV_WRITE);
749 1.3 christos bufferevent_setcb(bev1, NULL, NULL,
750 1.3 christos http_allowed_methods_eventcb, &result1);
751 1.3 christos
752 1.3 christos http_request =
753 1.3 christos "GET /index.html HTTP/1.1\r\n"
754 1.3 christos "Host: somehost\r\n"
755 1.3 christos "Connection: close\r\n"
756 1.3 christos "\r\n";
757 1.3 christos
758 1.3 christos bufferevent_write(bev1, http_request, strlen(http_request));
759 1.3 christos
760 1.3 christos event_base_dispatch(data->base);
761 1.3 christos
762 1.3 christos fd2 = http_connect("127.0.0.1", port);
763 1.3 christos
764 1.3 christos bev2 = bufferevent_socket_new(data->base, fd2, 0);
765 1.3 christos bufferevent_enable(bev2, EV_READ|EV_WRITE);
766 1.3 christos bufferevent_setcb(bev2, NULL, NULL,
767 1.3 christos http_allowed_methods_eventcb, &result2);
768 1.3 christos
769 1.3 christos http_request =
770 1.3 christos "PATCH /test HTTP/1.1\r\n"
771 1.3 christos "Host: somehost\r\n"
772 1.3 christos "Connection: close\r\n"
773 1.3 christos "\r\n";
774 1.3 christos
775 1.3 christos bufferevent_write(bev2, http_request, strlen(http_request));
776 1.3 christos
777 1.3 christos event_base_dispatch(data->base);
778 1.3 christos
779 1.3 christos fd3 = http_connect("127.0.0.1", port);
780 1.3 christos
781 1.3 christos bev3 = bufferevent_socket_new(data->base, fd3, 0);
782 1.3 christos bufferevent_enable(bev3, EV_READ|EV_WRITE);
783 1.3 christos bufferevent_setcb(bev3, NULL, NULL,
784 1.3 christos http_allowed_methods_eventcb, &result3);
785 1.3 christos
786 1.3 christos http_request =
787 1.3 christos "FLOOP /test HTTP/1.1\r\n"
788 1.3 christos "Host: somehost\r\n"
789 1.3 christos "Connection: close\r\n"
790 1.3 christos "\r\n";
791 1.3 christos
792 1.3 christos bufferevent_write(bev3, http_request, strlen(http_request));
793 1.3 christos
794 1.3 christos event_base_dispatch(data->base);
795 1.3 christos
796 1.3 christos bufferevent_free(bev1);
797 1.3 christos bufferevent_free(bev2);
798 1.3 christos bufferevent_free(bev3);
799 1.3 christos evutil_closesocket(fd1);
800 1.3 christos evutil_closesocket(fd2);
801 1.3 christos evutil_closesocket(fd3);
802 1.3 christos
803 1.3 christos evhttp_free(http);
804 1.3 christos
805 1.3 christos /* Method known but disallowed */
806 1.3 christos tt_assert(result1);
807 1.3 christos tt_assert(!strncmp(result1, "HTTP/1.1 501 ", strlen("HTTP/1.1 501 ")));
808 1.3 christos
809 1.3 christos /* Method known and allowed */
810 1.3 christos tt_assert(result2);
811 1.3 christos tt_assert(!strncmp(result2, "HTTP/1.1 200 ", strlen("HTTP/1.1 200 ")));
812 1.3 christos
813 1.3 christos /* Method unknown */
814 1.3 christos tt_assert(result3);
815 1.3 christos tt_assert(!strncmp(result3, "HTTP/1.1 501 ", strlen("HTTP/1.1 501 ")));
816 1.3 christos
817 1.3 christos end:
818 1.3 christos if (result1)
819 1.3 christos free(result1);
820 1.3 christos if (result2)
821 1.3 christos free(result2);
822 1.3 christos if (result3)
823 1.3 christos free(result3);
824 1.3 christos }
825 1.3 christos
826 1.3 christos static void http_request_done(struct evhttp_request *, void *);
827 1.3 christos static void http_request_empty_done(struct evhttp_request *, void *);
828 1.1 plunky
829 1.1 plunky static void
830 1.3 christos _http_connection_test(struct basic_test_data *data, int persistent)
831 1.1 plunky {
832 1.3 christos ev_uint16_t port = 0;
833 1.1 plunky struct evhttp_connection *evcon = NULL;
834 1.1 plunky struct evhttp_request *req = NULL;
835 1.1 plunky
836 1.1 plunky test_ok = 0;
837 1.1 plunky
838 1.3 christos http = http_setup(&port, data->base);
839 1.1 plunky
840 1.3 christos evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
841 1.3 christos tt_assert(evcon);
842 1.3 christos
843 1.3 christos tt_assert(evhttp_connection_get_base(evcon) == data->base);
844 1.3 christos
845 1.3 christos exit_base = data->base;
846 1.3 christos /*
847 1.3 christos * At this point, we want to schedule a request to the HTTP
848 1.3 christos * server using our make request method.
849 1.3 christos */
850 1.3 christos
851 1.3 christos req = evhttp_request_new(http_request_done, basic_request_body);
852 1.3 christos
853 1.3 christos /* Add the information that we care about */
854 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
855 1.3 christos
856 1.3 christos /* We give ownership of the request to the connection */
857 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
858 1.1 plunky fprintf(stdout, "FAILED\n");
859 1.1 plunky exit(1);
860 1.1 plunky }
861 1.1 plunky
862 1.3 christos event_base_dispatch(data->base);
863 1.3 christos
864 1.3 christos tt_assert(test_ok);
865 1.3 christos
866 1.3 christos /* try to make another request over the same connection */
867 1.3 christos test_ok = 0;
868 1.3 christos
869 1.3 christos req = evhttp_request_new(http_request_done, basic_request_body);
870 1.3 christos
871 1.3 christos /* Add the information that we care about */
872 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
873 1.3 christos
874 1.3 christos /*
875 1.3 christos * if our connections are not supposed to be persistent; request
876 1.3 christos * a close from the server.
877 1.3 christos */
878 1.3 christos if (!persistent)
879 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Connection", "close");
880 1.3 christos
881 1.3 christos /* We give ownership of the request to the connection */
882 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
883 1.3 christos tt_abort_msg("couldn't make request");
884 1.3 christos }
885 1.3 christos
886 1.3 christos event_base_dispatch(data->base);
887 1.3 christos
888 1.3 christos /* make another request: request empty reply */
889 1.3 christos test_ok = 0;
890 1.3 christos
891 1.3 christos req = evhttp_request_new(http_request_empty_done, data->base);
892 1.3 christos
893 1.3 christos /* Add the information that we care about */
894 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Empty", "itis");
895 1.3 christos
896 1.3 christos /* We give ownership of the request to the connection */
897 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
898 1.3 christos tt_abort_msg("Couldn't make request");
899 1.3 christos }
900 1.3 christos
901 1.3 christos event_base_dispatch(data->base);
902 1.3 christos
903 1.3 christos end:
904 1.3 christos if (evcon)
905 1.3 christos evhttp_connection_free(evcon);
906 1.3 christos if (http)
907 1.3 christos evhttp_free(http);
908 1.3 christos }
909 1.3 christos
910 1.3 christos static void
911 1.3 christos http_connection_test(void *arg)
912 1.3 christos {
913 1.3 christos _http_connection_test(arg, 0);
914 1.3 christos }
915 1.3 christos static void
916 1.3 christos http_persist_connection_test(void *arg)
917 1.3 christos {
918 1.3 christos _http_connection_test(arg, 1);
919 1.3 christos }
920 1.3 christos
921 1.3 christos static struct regress_dns_server_table search_table[] = {
922 1.3 christos { "localhost", "A", "127.0.0.1", 0 },
923 1.3 christos { NULL, NULL, NULL, 0 }
924 1.3 christos };
925 1.3 christos
926 1.3 christos static void
927 1.3 christos http_connection_async_test(void *arg)
928 1.3 christos {
929 1.3 christos struct basic_test_data *data = arg;
930 1.3 christos ev_uint16_t port = 0;
931 1.3 christos struct evhttp_connection *evcon = NULL;
932 1.3 christos struct evhttp_request *req = NULL;
933 1.3 christos struct evdns_base *dns_base = NULL;
934 1.3 christos ev_uint16_t portnum = 0;
935 1.3 christos char address[64];
936 1.3 christos
937 1.3 christos exit_base = data->base;
938 1.3 christos tt_assert(regress_dnsserver(data->base, &portnum, search_table));
939 1.3 christos
940 1.3 christos dns_base = evdns_base_new(data->base, 0/* init name servers */);
941 1.3 christos tt_assert(dns_base);
942 1.3 christos
943 1.3 christos /* Add ourself as the only nameserver, and make sure we really are
944 1.3 christos * the only nameserver. */
945 1.3 christos evutil_snprintf(address, sizeof(address), "127.0.0.1:%d", portnum);
946 1.3 christos evdns_base_nameserver_ip_add(dns_base, address);
947 1.3 christos
948 1.3 christos test_ok = 0;
949 1.3 christos
950 1.3 christos http = http_setup(&port, data->base);
951 1.3 christos
952 1.3 christos evcon = evhttp_connection_base_new(data->base, dns_base, "127.0.0.1", port);
953 1.3 christos tt_assert(evcon);
954 1.1 plunky
955 1.1 plunky /*
956 1.3 christos * At this point, we want to schedule a request to the HTTP
957 1.1 plunky * server using our make request method.
958 1.1 plunky */
959 1.1 plunky
960 1.3 christos req = evhttp_request_new(http_request_done, basic_request_body);
961 1.3 christos
962 1.3 christos /* Add the information that we care about */
963 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
964 1.3 christos
965 1.3 christos /* We give ownership of the request to the connection */
966 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
967 1.1 plunky fprintf(stdout, "FAILED\n");
968 1.1 plunky exit(1);
969 1.1 plunky }
970 1.1 plunky
971 1.3 christos event_base_dispatch(data->base);
972 1.3 christos
973 1.3 christos tt_assert(test_ok);
974 1.3 christos
975 1.3 christos /* try to make another request over the same connection */
976 1.3 christos test_ok = 0;
977 1.3 christos
978 1.3 christos req = evhttp_request_new(http_request_done, basic_request_body);
979 1.3 christos
980 1.1 plunky /* Add the information that we care about */
981 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
982 1.3 christos
983 1.3 christos /*
984 1.3 christos * if our connections are not supposed to be persistent; request
985 1.3 christos * a close from the server.
986 1.3 christos */
987 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Connection", "close");
988 1.3 christos
989 1.3 christos /* We give ownership of the request to the connection */
990 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
991 1.3 christos tt_abort_msg("couldn't make request");
992 1.1 plunky }
993 1.1 plunky
994 1.3 christos event_base_dispatch(data->base);
995 1.1 plunky
996 1.3 christos /* make another request: request empty reply */
997 1.3 christos test_ok = 0;
998 1.3 christos
999 1.3 christos req = evhttp_request_new(http_request_empty_done, data->base);
1000 1.3 christos
1001 1.3 christos /* Add the information that we care about */
1002 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Empty", "itis");
1003 1.3 christos
1004 1.3 christos /* We give ownership of the request to the connection */
1005 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
1006 1.3 christos tt_abort_msg("Couldn't make request");
1007 1.1 plunky }
1008 1.3 christos
1009 1.3 christos event_base_dispatch(data->base);
1010 1.3 christos
1011 1.3 christos end:
1012 1.3 christos if (evcon)
1013 1.3 christos evhttp_connection_free(evcon);
1014 1.3 christos if (http)
1015 1.3 christos evhttp_free(http);
1016 1.3 christos if (dns_base)
1017 1.3 christos evdns_base_free(dns_base, 0);
1018 1.3 christos regress_clean_dnsserver();
1019 1.3 christos }
1020 1.3 christos
1021 1.3 christos static void
1022 1.3 christos http_request_never_call(struct evhttp_request *req, void *arg)
1023 1.3 christos {
1024 1.3 christos fprintf(stdout, "FAILED\n");
1025 1.3 christos exit(1);
1026 1.1 plunky }
1027 1.1 plunky
1028 1.3 christos static void
1029 1.3 christos http_do_cancel(evutil_socket_t fd, short what, void *arg)
1030 1.3 christos {
1031 1.3 christos struct evhttp_request *req = arg;
1032 1.3 christos struct timeval tv;
1033 1.3 christos struct event_base *base;
1034 1.3 christos evutil_timerclear(&tv);
1035 1.3 christos tv.tv_sec = 0;
1036 1.3 christos tv.tv_usec = 500 * 1000;
1037 1.3 christos
1038 1.3 christos base = evhttp_connection_get_base(evhttp_request_get_connection(req));
1039 1.3 christos evhttp_cancel_request(req);
1040 1.1 plunky
1041 1.3 christos event_base_loopexit(base, &tv);
1042 1.1 plunky
1043 1.3 christos ++test_ok;
1044 1.3 christos }
1045 1.1 plunky
1046 1.1 plunky static void
1047 1.3 christos http_cancel_test(void *arg)
1048 1.1 plunky {
1049 1.3 christos struct basic_test_data *data = arg;
1050 1.3 christos ev_uint16_t port = 0;
1051 1.1 plunky struct evhttp_connection *evcon = NULL;
1052 1.1 plunky struct evhttp_request *req = NULL;
1053 1.3 christos struct timeval tv;
1054 1.3 christos
1055 1.3 christos exit_base = data->base;
1056 1.1 plunky
1057 1.1 plunky test_ok = 0;
1058 1.1 plunky
1059 1.3 christos http = http_setup(&port, data->base);
1060 1.1 plunky
1061 1.3 christos evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
1062 1.3 christos tt_assert(evcon);
1063 1.1 plunky
1064 1.1 plunky /*
1065 1.3 christos * At this point, we want to schedule a request to the HTTP
1066 1.1 plunky * server using our make request method.
1067 1.1 plunky */
1068 1.1 plunky
1069 1.3 christos req = evhttp_request_new(http_request_never_call, NULL);
1070 1.3 christos
1071 1.3 christos /* Add the information that we care about */
1072 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1073 1.3 christos
1074 1.3 christos /* We give ownership of the request to the connection */
1075 1.3 christos tt_int_op(evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/delay"),
1076 1.3 christos !=, -1);
1077 1.3 christos
1078 1.3 christos evutil_timerclear(&tv);
1079 1.3 christos tv.tv_sec = 0;
1080 1.3 christos tv.tv_usec = 100 * 1000;
1081 1.3 christos
1082 1.3 christos event_base_once(data->base, -1, EV_TIMEOUT, http_do_cancel, req, &tv);
1083 1.3 christos
1084 1.3 christos event_base_dispatch(data->base);
1085 1.3 christos
1086 1.3 christos tt_int_op(test_ok, ==, 2);
1087 1.3 christos
1088 1.3 christos /* try to make another request over the same connection */
1089 1.3 christos test_ok = 0;
1090 1.3 christos
1091 1.3 christos req = evhttp_request_new(http_request_done, basic_request_body);
1092 1.3 christos
1093 1.3 christos /* Add the information that we care about */
1094 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1095 1.3 christos
1096 1.3 christos /* We give ownership of the request to the connection */
1097 1.3 christos tt_int_op(evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test"),
1098 1.3 christos !=, -1);
1099 1.3 christos
1100 1.3 christos event_base_dispatch(data->base);
1101 1.3 christos
1102 1.3 christos /* make another request: request empty reply */
1103 1.3 christos test_ok = 0;
1104 1.3 christos
1105 1.3 christos req = evhttp_request_new(http_request_empty_done, data->base);
1106 1.3 christos
1107 1.3 christos /* Add the information that we care about */
1108 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Empty", "itis");
1109 1.3 christos
1110 1.3 christos /* We give ownership of the request to the connection */
1111 1.3 christos tt_int_op(evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test"),
1112 1.3 christos !=, -1);
1113 1.3 christos
1114 1.3 christos event_base_dispatch(data->base);
1115 1.3 christos
1116 1.3 christos end:
1117 1.3 christos if (evcon)
1118 1.3 christos evhttp_connection_free(evcon);
1119 1.3 christos if (http)
1120 1.3 christos evhttp_free(http);
1121 1.3 christos }
1122 1.3 christos
1123 1.3 christos static void
1124 1.3 christos http_request_done(struct evhttp_request *req, void *arg)
1125 1.3 christos {
1126 1.3 christos const char *what = arg;
1127 1.3 christos
1128 1.3 christos if (evhttp_request_get_response_code(req) != HTTP_OK) {
1129 1.3 christos fprintf(stderr, "FAILED\n");
1130 1.3 christos exit(1);
1131 1.3 christos }
1132 1.3 christos
1133 1.3 christos if (evhttp_find_header(evhttp_request_get_input_headers(req), "Content-Type") == NULL) {
1134 1.3 christos fprintf(stderr, "FAILED\n");
1135 1.3 christos exit(1);
1136 1.3 christos }
1137 1.3 christos
1138 1.3 christos if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != strlen(what)) {
1139 1.3 christos fprintf(stderr, "FAILED\n");
1140 1.1 plunky exit(1);
1141 1.1 plunky }
1142 1.1 plunky
1143 1.3 christos if (evbuffer_datacmp(evhttp_request_get_input_buffer(req), what) != 0) {
1144 1.3 christos fprintf(stderr, "FAILED\n");
1145 1.1 plunky exit(1);
1146 1.1 plunky }
1147 1.1 plunky
1148 1.3 christos test_ok = 1;
1149 1.3 christos EVUTIL_ASSERT(exit_base);
1150 1.3 christos event_base_loopexit(exit_base, NULL);
1151 1.3 christos }
1152 1.1 plunky
1153 1.3 christos static void
1154 1.3 christos http_request_expect_error(struct evhttp_request *req, void *arg)
1155 1.3 christos {
1156 1.3 christos if (evhttp_request_get_response_code(req) == HTTP_OK) {
1157 1.3 christos fprintf(stderr, "FAILED\n");
1158 1.1 plunky exit(1);
1159 1.1 plunky }
1160 1.3 christos
1161 1.3 christos test_ok = 1;
1162 1.3 christos EVUTIL_ASSERT(arg);
1163 1.3 christos event_base_loopexit(arg, NULL);
1164 1.1 plunky }
1165 1.1 plunky
1166 1.3 christos /* test virtual hosts */
1167 1.3 christos static void
1168 1.3 christos http_virtual_host_test(void *arg)
1169 1.3 christos {
1170 1.3 christos struct basic_test_data *data = arg;
1171 1.3 christos ev_uint16_t port = 0;
1172 1.3 christos struct evhttp_connection *evcon = NULL;
1173 1.3 christos struct evhttp_request *req = NULL;
1174 1.3 christos struct evhttp *second = NULL, *third = NULL;
1175 1.3 christos evutil_socket_t fd;
1176 1.3 christos struct bufferevent *bev;
1177 1.3 christos const char *http_request;
1178 1.3 christos
1179 1.3 christos exit_base = data->base;
1180 1.3 christos
1181 1.3 christos http = http_setup(&port, data->base);
1182 1.3 christos
1183 1.3 christos /* virtual host */
1184 1.3 christos second = evhttp_new(NULL);
1185 1.3 christos evhttp_set_cb(second, "/funnybunny", http_basic_cb, NULL);
1186 1.3 christos third = evhttp_new(NULL);
1187 1.3 christos evhttp_set_cb(third, "/blackcoffee", http_basic_cb, NULL);
1188 1.3 christos
1189 1.3 christos if (evhttp_add_virtual_host(http, "foo.com", second) == -1) {
1190 1.3 christos tt_abort_msg("Couldn't add vhost");
1191 1.3 christos }
1192 1.3 christos
1193 1.3 christos if (evhttp_add_virtual_host(http, "bar.*.foo.com", third) == -1) {
1194 1.3 christos tt_abort_msg("Couldn't add wildcarded vhost");
1195 1.3 christos }
1196 1.3 christos
1197 1.3 christos /* add some aliases to the vhosts */
1198 1.3 christos tt_assert(evhttp_add_server_alias(second, "manolito.info") == 0);
1199 1.3 christos tt_assert(evhttp_add_server_alias(third, "bonkers.org") == 0);
1200 1.3 christos
1201 1.3 christos evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
1202 1.3 christos tt_assert(evcon);
1203 1.3 christos
1204 1.3 christos /* make a request with a different host and expect an error */
1205 1.3 christos req = evhttp_request_new(http_request_expect_error, data->base);
1206 1.3 christos
1207 1.3 christos /* Add the information that we care about */
1208 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1209 1.3 christos
1210 1.3 christos /* We give ownership of the request to the connection */
1211 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
1212 1.3 christos "/funnybunny") == -1) {
1213 1.3 christos tt_abort_msg("Couldn't make request");
1214 1.3 christos }
1215 1.3 christos
1216 1.3 christos event_base_dispatch(data->base);
1217 1.3 christos
1218 1.3 christos tt_assert(test_ok == 1);
1219 1.3 christos
1220 1.3 christos test_ok = 0;
1221 1.3 christos
1222 1.3 christos /* make a request with the right host and expect a response */
1223 1.3 christos req = evhttp_request_new(http_request_done, basic_request_body);
1224 1.3 christos
1225 1.3 christos /* Add the information that we care about */
1226 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "foo.com");
1227 1.3 christos
1228 1.3 christos /* We give ownership of the request to the connection */
1229 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
1230 1.3 christos "/funnybunny") == -1) {
1231 1.3 christos fprintf(stdout, "FAILED\n");
1232 1.3 christos exit(1);
1233 1.3 christos }
1234 1.3 christos
1235 1.3 christos event_base_dispatch(data->base);
1236 1.3 christos
1237 1.3 christos tt_assert(test_ok == 1);
1238 1.3 christos
1239 1.3 christos test_ok = 0;
1240 1.3 christos
1241 1.3 christos /* make a request with the right host and expect a response */
1242 1.3 christos req = evhttp_request_new(http_request_done, basic_request_body);
1243 1.3 christos
1244 1.3 christos /* Add the information that we care about */
1245 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "bar.magic.foo.com");
1246 1.3 christos
1247 1.3 christos /* We give ownership of the request to the connection */
1248 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
1249 1.3 christos "/blackcoffee") == -1) {
1250 1.3 christos tt_abort_msg("Couldn't make request");
1251 1.3 christos }
1252 1.3 christos
1253 1.3 christos event_base_dispatch(data->base);
1254 1.3 christos
1255 1.3 christos tt_assert(test_ok == 1)
1256 1.3 christos
1257 1.3 christos test_ok = 0;
1258 1.3 christos
1259 1.3 christos /* make a request with the right host and expect a response */
1260 1.3 christos req = evhttp_request_new(http_request_done, basic_request_body);
1261 1.3 christos
1262 1.3 christos /* Add the information that we care about */
1263 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "manolito.info");
1264 1.3 christos
1265 1.3 christos /* We give ownership of the request to the connection */
1266 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
1267 1.3 christos "/funnybunny") == -1) {
1268 1.3 christos tt_abort_msg("Couldn't make request");
1269 1.3 christos }
1270 1.3 christos
1271 1.3 christos event_base_dispatch(data->base);
1272 1.3 christos
1273 1.3 christos tt_assert(test_ok == 1)
1274 1.3 christos
1275 1.3 christos test_ok = 0;
1276 1.3 christos
1277 1.3 christos /* make a request with the right host and expect a response */
1278 1.3 christos req = evhttp_request_new(http_request_done, basic_request_body);
1279 1.3 christos
1280 1.3 christos /* Add the Host header. This time with the optional port. */
1281 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "bonkers.org:8000");
1282 1.3 christos
1283 1.3 christos /* We give ownership of the request to the connection */
1284 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
1285 1.3 christos "/blackcoffee") == -1) {
1286 1.3 christos tt_abort_msg("Couldn't make request");
1287 1.3 christos }
1288 1.3 christos
1289 1.3 christos event_base_dispatch(data->base);
1290 1.3 christos
1291 1.3 christos tt_assert(test_ok == 1)
1292 1.3 christos
1293 1.3 christos test_ok = 0;
1294 1.3 christos
1295 1.3 christos /* Now make a raw request with an absolute URI. */
1296 1.3 christos fd = http_connect("127.0.0.1", port);
1297 1.3 christos
1298 1.3 christos /* Stupid thing to send a request */
1299 1.3 christos bev = bufferevent_socket_new(data->base, fd, 0);
1300 1.3 christos bufferevent_setcb(bev, http_readcb, http_writecb,
1301 1.3 christos http_errorcb, NULL);
1302 1.3 christos
1303 1.3 christos /* The host in the URI should override the Host: header */
1304 1.3 christos http_request =
1305 1.3 christos "GET http://manolito.info/funnybunny HTTP/1.1\r\n"
1306 1.3 christos "Host: somehost\r\n"
1307 1.3 christos "Connection: close\r\n"
1308 1.3 christos "\r\n";
1309 1.3 christos
1310 1.3 christos bufferevent_write(bev, http_request, strlen(http_request));
1311 1.3 christos
1312 1.3 christos event_base_dispatch(data->base);
1313 1.3 christos
1314 1.3 christos tt_int_op(test_ok, ==, 2);
1315 1.3 christos
1316 1.3 christos bufferevent_free(bev);
1317 1.3 christos evutil_closesocket(fd);
1318 1.3 christos
1319 1.3 christos end:
1320 1.3 christos if (evcon)
1321 1.3 christos evhttp_connection_free(evcon);
1322 1.3 christos if (http)
1323 1.3 christos evhttp_free(http);
1324 1.3 christos }
1325 1.3 christos
1326 1.3 christos
1327 1.3 christos /* test date header and content length */
1328 1.3 christos
1329 1.3 christos static void
1330 1.3 christos http_request_empty_done(struct evhttp_request *req, void *arg)
1331 1.3 christos {
1332 1.3 christos if (evhttp_request_get_response_code(req) != HTTP_OK) {
1333 1.3 christos fprintf(stderr, "FAILED\n");
1334 1.3 christos exit(1);
1335 1.3 christos }
1336 1.3 christos
1337 1.3 christos if (evhttp_find_header(evhttp_request_get_input_headers(req), "Date") == NULL) {
1338 1.3 christos fprintf(stderr, "FAILED\n");
1339 1.3 christos exit(1);
1340 1.3 christos }
1341 1.3 christos
1342 1.3 christos
1343 1.3 christos if (evhttp_find_header(evhttp_request_get_input_headers(req), "Content-Length") == NULL) {
1344 1.3 christos fprintf(stderr, "FAILED\n");
1345 1.3 christos exit(1);
1346 1.3 christos }
1347 1.3 christos
1348 1.3 christos if (strcmp(evhttp_find_header(evhttp_request_get_input_headers(req), "Content-Length"),
1349 1.3 christos "0")) {
1350 1.3 christos fprintf(stderr, "FAILED\n");
1351 1.3 christos exit(1);
1352 1.3 christos }
1353 1.3 christos
1354 1.3 christos if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != 0) {
1355 1.3 christos fprintf(stderr, "FAILED\n");
1356 1.3 christos exit(1);
1357 1.3 christos }
1358 1.3 christos
1359 1.3 christos test_ok = 1;
1360 1.3 christos EVUTIL_ASSERT(arg);
1361 1.3 christos event_base_loopexit(arg, NULL);
1362 1.3 christos }
1363 1.3 christos
1364 1.3 christos /*
1365 1.3 christos * HTTP DISPATCHER test
1366 1.3 christos */
1367 1.3 christos
1368 1.3 christos void
1369 1.3 christos http_dispatcher_cb(struct evhttp_request *req, void *arg)
1370 1.3 christos {
1371 1.3 christos
1372 1.3 christos struct evbuffer *evb = evbuffer_new();
1373 1.3 christos event_debug(("%s: called\n", __func__));
1374 1.3 christos evbuffer_add_printf(evb, "DISPATCHER_TEST");
1375 1.3 christos
1376 1.3 christos evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
1377 1.3 christos
1378 1.3 christos evbuffer_free(evb);
1379 1.3 christos }
1380 1.3 christos
1381 1.3 christos static void
1382 1.3 christos http_dispatcher_test_done(struct evhttp_request *req, void *arg)
1383 1.3 christos {
1384 1.3 christos struct event_base *base = arg;
1385 1.3 christos const char *what = "DISPATCHER_TEST";
1386 1.3 christos
1387 1.3 christos if (evhttp_request_get_response_code(req) != HTTP_OK) {
1388 1.3 christos fprintf(stderr, "FAILED\n");
1389 1.3 christos exit(1);
1390 1.3 christos }
1391 1.3 christos
1392 1.3 christos if (evhttp_find_header(evhttp_request_get_input_headers(req), "Content-Type") == NULL) {
1393 1.3 christos fprintf(stderr, "FAILED (content type)\n");
1394 1.3 christos exit(1);
1395 1.3 christos }
1396 1.3 christos
1397 1.3 christos if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != strlen(what)) {
1398 1.3 christos fprintf(stderr, "FAILED (length %lu vs %lu)\n",
1399 1.3 christos (unsigned long)evbuffer_get_length(evhttp_request_get_input_buffer(req)), (unsigned long)strlen(what));
1400 1.3 christos exit(1);
1401 1.3 christos }
1402 1.3 christos
1403 1.3 christos if (evbuffer_datacmp(evhttp_request_get_input_buffer(req), what) != 0) {
1404 1.3 christos fprintf(stderr, "FAILED (data)\n");
1405 1.3 christos exit(1);
1406 1.3 christos }
1407 1.3 christos
1408 1.3 christos test_ok = 1;
1409 1.3 christos event_base_loopexit(base, NULL);
1410 1.3 christos }
1411 1.3 christos
1412 1.3 christos static void
1413 1.3 christos http_dispatcher_test(void *arg)
1414 1.3 christos {
1415 1.3 christos struct basic_test_data *data = arg;
1416 1.3 christos ev_uint16_t port = 0;
1417 1.3 christos struct evhttp_connection *evcon = NULL;
1418 1.3 christos struct evhttp_request *req = NULL;
1419 1.3 christos
1420 1.3 christos test_ok = 0;
1421 1.3 christos
1422 1.3 christos http = http_setup(&port, data->base);
1423 1.3 christos
1424 1.3 christos evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
1425 1.3 christos tt_assert(evcon);
1426 1.3 christos
1427 1.3 christos /* also bind to local host */
1428 1.3 christos evhttp_connection_set_local_address(evcon, "127.0.0.1");
1429 1.3 christos
1430 1.3 christos /*
1431 1.3 christos * At this point, we want to schedule an HTTP GET request
1432 1.3 christos * server using our make request method.
1433 1.3 christos */
1434 1.3 christos
1435 1.3 christos req = evhttp_request_new(http_dispatcher_test_done, data->base);
1436 1.3 christos tt_assert(req);
1437 1.3 christos
1438 1.3 christos /* Add the information that we care about */
1439 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1440 1.3 christos
1441 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/?arg=val") == -1) {
1442 1.3 christos tt_abort_msg("Couldn't make request");
1443 1.3 christos }
1444 1.3 christos
1445 1.3 christos event_base_dispatch(data->base);
1446 1.3 christos
1447 1.3 christos end:
1448 1.3 christos if (evcon)
1449 1.3 christos evhttp_connection_free(evcon);
1450 1.3 christos if (http)
1451 1.3 christos evhttp_free(http);
1452 1.3 christos }
1453 1.3 christos
1454 1.3 christos /*
1455 1.3 christos * HTTP POST test.
1456 1.3 christos */
1457 1.3 christos
1458 1.3 christos void http_postrequest_done(struct evhttp_request *, void *);
1459 1.3 christos
1460 1.3 christos #define POST_DATA "Okay. Not really printf"
1461 1.3 christos
1462 1.3 christos static void
1463 1.3 christos http_post_test(void *arg)
1464 1.3 christos {
1465 1.3 christos struct basic_test_data *data = arg;
1466 1.3 christos ev_uint16_t port = 0;
1467 1.3 christos struct evhttp_connection *evcon = NULL;
1468 1.3 christos struct evhttp_request *req = NULL;
1469 1.3 christos
1470 1.3 christos test_ok = 0;
1471 1.3 christos
1472 1.3 christos http = http_setup(&port, data->base);
1473 1.3 christos
1474 1.3 christos evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
1475 1.3 christos tt_assert(evcon);
1476 1.3 christos
1477 1.3 christos /*
1478 1.3 christos * At this point, we want to schedule an HTTP POST request
1479 1.3 christos * server using our make request method.
1480 1.3 christos */
1481 1.3 christos
1482 1.3 christos req = evhttp_request_new(http_postrequest_done, data->base);
1483 1.3 christos tt_assert(req);
1484 1.3 christos
1485 1.3 christos /* Add the information that we care about */
1486 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1487 1.3 christos evbuffer_add_printf(evhttp_request_get_output_buffer(req), POST_DATA);
1488 1.3 christos
1489 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_POST, "/postit") == -1) {
1490 1.3 christos tt_abort_msg("Couldn't make request");
1491 1.3 christos }
1492 1.3 christos
1493 1.3 christos event_base_dispatch(data->base);
1494 1.3 christos
1495 1.3 christos tt_int_op(test_ok, ==, 1);
1496 1.3 christos
1497 1.3 christos test_ok = 0;
1498 1.3 christos
1499 1.3 christos req = evhttp_request_new(http_postrequest_done, data->base);
1500 1.3 christos tt_assert(req);
1501 1.3 christos
1502 1.3 christos /* Now try with 100-continue. */
1503 1.3 christos
1504 1.3 christos /* Add the information that we care about */
1505 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1506 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Expect", "100-continue");
1507 1.3 christos evbuffer_add_printf(evhttp_request_get_output_buffer(req), POST_DATA);
1508 1.3 christos
1509 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_POST, "/postit") == -1) {
1510 1.3 christos tt_abort_msg("Couldn't make request");
1511 1.3 christos }
1512 1.3 christos
1513 1.3 christos event_base_dispatch(data->base);
1514 1.3 christos
1515 1.3 christos tt_int_op(test_ok, ==, 1);
1516 1.3 christos
1517 1.3 christos evhttp_connection_free(evcon);
1518 1.3 christos evhttp_free(http);
1519 1.3 christos
1520 1.3 christos end:
1521 1.3 christos ;
1522 1.3 christos }
1523 1.3 christos
1524 1.3 christos void
1525 1.3 christos http_post_cb(struct evhttp_request *req, void *arg)
1526 1.3 christos {
1527 1.3 christos struct evbuffer *evb;
1528 1.3 christos event_debug(("%s: called\n", __func__));
1529 1.3 christos
1530 1.3 christos /* Yes, we are expecting a post request */
1531 1.3 christos if (evhttp_request_get_command(req) != EVHTTP_REQ_POST) {
1532 1.3 christos fprintf(stdout, "FAILED (post type)\n");
1533 1.3 christos exit(1);
1534 1.3 christos }
1535 1.3 christos
1536 1.3 christos if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != strlen(POST_DATA)) {
1537 1.3 christos fprintf(stdout, "FAILED (length: %lu vs %lu)\n",
1538 1.3 christos (unsigned long) evbuffer_get_length(evhttp_request_get_input_buffer(req)), (unsigned long) strlen(POST_DATA));
1539 1.3 christos exit(1);
1540 1.3 christos }
1541 1.3 christos
1542 1.3 christos if (evbuffer_datacmp(evhttp_request_get_input_buffer(req), POST_DATA) != 0) {
1543 1.3 christos fprintf(stdout, "FAILED (data)\n");
1544 1.3 christos fprintf(stdout, "Got :%s\n", evbuffer_pullup(evhttp_request_get_input_buffer(req),-1));
1545 1.3 christos fprintf(stdout, "Want:%s\n", POST_DATA);
1546 1.3 christos exit(1);
1547 1.3 christos }
1548 1.3 christos
1549 1.3 christos evb = evbuffer_new();
1550 1.3 christos evbuffer_add_printf(evb, BASIC_REQUEST_BODY);
1551 1.3 christos
1552 1.3 christos evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);
1553 1.3 christos
1554 1.3 christos evbuffer_free(evb);
1555 1.3 christos }
1556 1.3 christos
1557 1.3 christos void
1558 1.3 christos http_postrequest_done(struct evhttp_request *req, void *arg)
1559 1.3 christos {
1560 1.3 christos const char *what = BASIC_REQUEST_BODY;
1561 1.3 christos struct event_base *base = arg;
1562 1.3 christos
1563 1.3 christos if (req == NULL) {
1564 1.3 christos fprintf(stderr, "FAILED (timeout)\n");
1565 1.3 christos exit(1);
1566 1.3 christos }
1567 1.3 christos
1568 1.3 christos if (evhttp_request_get_response_code(req) != HTTP_OK) {
1569 1.3 christos
1570 1.3 christos fprintf(stderr, "FAILED (response code)\n");
1571 1.3 christos exit(1);
1572 1.3 christos }
1573 1.3 christos
1574 1.3 christos if (evhttp_find_header(evhttp_request_get_input_headers(req), "Content-Type") == NULL) {
1575 1.3 christos fprintf(stderr, "FAILED (content type)\n");
1576 1.3 christos exit(1);
1577 1.3 christos }
1578 1.3 christos
1579 1.3 christos if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != strlen(what)) {
1580 1.3 christos fprintf(stderr, "FAILED (length %lu vs %lu)\n",
1581 1.3 christos (unsigned long)evbuffer_get_length(evhttp_request_get_input_buffer(req)), (unsigned long)strlen(what));
1582 1.3 christos exit(1);
1583 1.3 christos }
1584 1.3 christos
1585 1.3 christos if (evbuffer_datacmp(evhttp_request_get_input_buffer(req), what) != 0) {
1586 1.3 christos fprintf(stderr, "FAILED (data)\n");
1587 1.3 christos exit(1);
1588 1.3 christos }
1589 1.3 christos
1590 1.3 christos test_ok = 1;
1591 1.3 christos event_base_loopexit(base, NULL);
1592 1.3 christos }
1593 1.3 christos
1594 1.3 christos /*
1595 1.3 christos * HTTP PUT test, basically just like POST, but ...
1596 1.3 christos */
1597 1.3 christos
1598 1.3 christos void http_putrequest_done(struct evhttp_request *, void *);
1599 1.3 christos
1600 1.3 christos #define PUT_DATA "Hi, I'm some PUT data"
1601 1.3 christos
1602 1.3 christos static void
1603 1.3 christos http_put_test(void *arg)
1604 1.3 christos {
1605 1.3 christos struct basic_test_data *data = arg;
1606 1.3 christos ev_uint16_t port = 0;
1607 1.3 christos struct evhttp_connection *evcon = NULL;
1608 1.3 christos struct evhttp_request *req = NULL;
1609 1.3 christos
1610 1.3 christos test_ok = 0;
1611 1.3 christos
1612 1.3 christos http = http_setup(&port, data->base);
1613 1.3 christos
1614 1.3 christos evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
1615 1.3 christos tt_assert(evcon);
1616 1.3 christos
1617 1.3 christos /*
1618 1.3 christos * Schedule the HTTP PUT request
1619 1.3 christos */
1620 1.3 christos
1621 1.3 christos req = evhttp_request_new(http_putrequest_done, data->base);
1622 1.3 christos tt_assert(req);
1623 1.3 christos
1624 1.3 christos /* Add the information that we care about */
1625 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "someotherhost");
1626 1.3 christos evbuffer_add_printf(evhttp_request_get_output_buffer(req), PUT_DATA);
1627 1.3 christos
1628 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_PUT, "/putit") == -1) {
1629 1.3 christos tt_abort_msg("Couldn't make request");
1630 1.3 christos }
1631 1.3 christos
1632 1.3 christos event_base_dispatch(data->base);
1633 1.3 christos
1634 1.3 christos evhttp_connection_free(evcon);
1635 1.3 christos evhttp_free(http);
1636 1.3 christos
1637 1.3 christos tt_int_op(test_ok, ==, 1);
1638 1.3 christos end:
1639 1.3 christos ;
1640 1.3 christos }
1641 1.3 christos
1642 1.3 christos void
1643 1.3 christos http_put_cb(struct evhttp_request *req, void *arg)
1644 1.3 christos {
1645 1.3 christos struct evbuffer *evb;
1646 1.3 christos event_debug(("%s: called\n", __func__));
1647 1.3 christos
1648 1.3 christos /* Expecting a PUT request */
1649 1.3 christos if (evhttp_request_get_command(req) != EVHTTP_REQ_PUT) {
1650 1.3 christos fprintf(stdout, "FAILED (put type)\n");
1651 1.3 christos exit(1);
1652 1.3 christos }
1653 1.3 christos
1654 1.3 christos if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != strlen(PUT_DATA)) {
1655 1.3 christos fprintf(stdout, "FAILED (length: %lu vs %lu)\n",
1656 1.3 christos (unsigned long)evbuffer_get_length(evhttp_request_get_input_buffer(req)), (unsigned long)strlen(PUT_DATA));
1657 1.3 christos exit(1);
1658 1.3 christos }
1659 1.3 christos
1660 1.3 christos if (evbuffer_datacmp(evhttp_request_get_input_buffer(req), PUT_DATA) != 0) {
1661 1.3 christos fprintf(stdout, "FAILED (data)\n");
1662 1.3 christos fprintf(stdout, "Got :%s\n", evbuffer_pullup(evhttp_request_get_input_buffer(req),-1));
1663 1.3 christos fprintf(stdout, "Want:%s\n", PUT_DATA);
1664 1.3 christos exit(1);
1665 1.3 christos }
1666 1.3 christos
1667 1.3 christos evb = evbuffer_new();
1668 1.3 christos evbuffer_add_printf(evb, "That ain't funny");
1669 1.3 christos
1670 1.3 christos evhttp_send_reply(req, HTTP_OK, "Everything is great", evb);
1671 1.3 christos
1672 1.3 christos evbuffer_free(evb);
1673 1.3 christos }
1674 1.3 christos
1675 1.3 christos void
1676 1.3 christos http_putrequest_done(struct evhttp_request *req, void *arg)
1677 1.3 christos {
1678 1.3 christos struct event_base *base = arg;
1679 1.3 christos const char *what = "That ain't funny";
1680 1.3 christos
1681 1.3 christos if (req == NULL) {
1682 1.3 christos fprintf(stderr, "FAILED (timeout)\n");
1683 1.3 christos exit(1);
1684 1.3 christos }
1685 1.3 christos
1686 1.3 christos if (evhttp_request_get_response_code(req) != HTTP_OK) {
1687 1.3 christos
1688 1.3 christos fprintf(stderr, "FAILED (response code)\n");
1689 1.3 christos exit(1);
1690 1.3 christos }
1691 1.3 christos
1692 1.3 christos if (evhttp_find_header(evhttp_request_get_input_headers(req), "Content-Type") == NULL) {
1693 1.3 christos fprintf(stderr, "FAILED (content type)\n");
1694 1.3 christos exit(1);
1695 1.3 christos }
1696 1.3 christos
1697 1.3 christos if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != strlen(what)) {
1698 1.3 christos fprintf(stderr, "FAILED (length %lu vs %lu)\n",
1699 1.3 christos (unsigned long)evbuffer_get_length(evhttp_request_get_input_buffer(req)), (unsigned long)strlen(what));
1700 1.3 christos exit(1);
1701 1.3 christos }
1702 1.3 christos
1703 1.3 christos
1704 1.3 christos if (evbuffer_datacmp(evhttp_request_get_input_buffer(req), what) != 0) {
1705 1.3 christos fprintf(stderr, "FAILED (data)\n");
1706 1.3 christos exit(1);
1707 1.3 christos }
1708 1.3 christos
1709 1.3 christos test_ok = 1;
1710 1.3 christos event_base_loopexit(base, NULL);
1711 1.3 christos }
1712 1.3 christos
1713 1.3 christos static void
1714 1.3 christos http_failure_readcb(struct bufferevent *bev, void *arg)
1715 1.3 christos {
1716 1.3 christos const char *what = "400 Bad Request";
1717 1.3 christos if (evbuffer_contains(bufferevent_get_input(bev), what)) {
1718 1.3 christos test_ok = 2;
1719 1.3 christos bufferevent_disable(bev, EV_READ);
1720 1.3 christos event_base_loopexit(arg, NULL);
1721 1.3 christos }
1722 1.3 christos }
1723 1.3 christos
1724 1.3 christos /*
1725 1.3 christos * Testing that the HTTP server can deal with a malformed request.
1726 1.3 christos */
1727 1.3 christos static void
1728 1.3 christos http_failure_test(void *arg)
1729 1.3 christos {
1730 1.3 christos struct basic_test_data *data = arg;
1731 1.3 christos struct bufferevent *bev;
1732 1.3 christos evutil_socket_t fd;
1733 1.3 christos const char *http_request;
1734 1.3 christos ev_uint16_t port = 0;
1735 1.3 christos
1736 1.3 christos test_ok = 0;
1737 1.3 christos
1738 1.3 christos http = http_setup(&port, data->base);
1739 1.3 christos
1740 1.3 christos fd = http_connect("127.0.0.1", port);
1741 1.3 christos
1742 1.3 christos /* Stupid thing to send a request */
1743 1.3 christos bev = bufferevent_socket_new(data->base, fd, 0);
1744 1.3 christos bufferevent_setcb(bev, http_failure_readcb, http_writecb,
1745 1.3 christos http_errorcb, data->base);
1746 1.3 christos
1747 1.3 christos http_request = "illegal request\r\n";
1748 1.3 christos
1749 1.3 christos bufferevent_write(bev, http_request, strlen(http_request));
1750 1.3 christos
1751 1.3 christos event_base_dispatch(data->base);
1752 1.3 christos
1753 1.3 christos bufferevent_free(bev);
1754 1.3 christos evutil_closesocket(fd);
1755 1.3 christos
1756 1.3 christos evhttp_free(http);
1757 1.3 christos
1758 1.3 christos tt_int_op(test_ok, ==, 2);
1759 1.3 christos end:
1760 1.3 christos ;
1761 1.3 christos }
1762 1.3 christos
1763 1.3 christos static void
1764 1.3 christos close_detect_done(struct evhttp_request *req, void *arg)
1765 1.3 christos {
1766 1.3 christos struct timeval tv;
1767 1.3 christos tt_assert(req);
1768 1.3 christos tt_assert(evhttp_request_get_response_code(req) == HTTP_OK);
1769 1.3 christos
1770 1.3 christos test_ok = 1;
1771 1.3 christos
1772 1.3 christos end:
1773 1.3 christos evutil_timerclear(&tv);
1774 1.3 christos tv.tv_sec = 3;
1775 1.3 christos event_base_loopexit(arg, &tv);
1776 1.3 christos }
1777 1.3 christos
1778 1.3 christos static void
1779 1.3 christos close_detect_launch(evutil_socket_t fd, short what, void *arg)
1780 1.3 christos {
1781 1.3 christos struct evhttp_connection *evcon = arg;
1782 1.3 christos struct event_base *base = evhttp_connection_get_base(evcon);
1783 1.3 christos struct evhttp_request *req;
1784 1.3 christos
1785 1.3 christos req = evhttp_request_new(close_detect_done, base);
1786 1.3 christos
1787 1.3 christos /* Add the information that we care about */
1788 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1789 1.3 christos
1790 1.3 christos /* We give ownership of the request to the connection */
1791 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
1792 1.3 christos tt_fail_msg("Couldn't make request");
1793 1.3 christos }
1794 1.3 christos }
1795 1.3 christos
1796 1.3 christos static void
1797 1.3 christos close_detect_cb(struct evhttp_request *req, void *arg)
1798 1.3 christos {
1799 1.3 christos struct evhttp_connection *evcon = arg;
1800 1.3 christos struct event_base *base = evhttp_connection_get_base(evcon);
1801 1.3 christos struct timeval tv;
1802 1.3 christos
1803 1.3 christos if (req != NULL && evhttp_request_get_response_code(req) != HTTP_OK) {
1804 1.3 christos tt_abort_msg("Failed");
1805 1.3 christos }
1806 1.3 christos
1807 1.3 christos evutil_timerclear(&tv);
1808 1.3 christos tv.tv_sec = 3; /* longer than the http time out */
1809 1.3 christos
1810 1.3 christos /* launch a new request on the persistent connection in 3 seconds */
1811 1.3 christos event_base_once(base, -1, EV_TIMEOUT, close_detect_launch, evcon, &tv);
1812 1.3 christos end:
1813 1.3 christos ;
1814 1.3 christos }
1815 1.3 christos
1816 1.3 christos
1817 1.3 christos static void
1818 1.3 christos _http_close_detection(struct basic_test_data *data, int with_delay)
1819 1.3 christos {
1820 1.3 christos ev_uint16_t port = 0;
1821 1.3 christos struct evhttp_connection *evcon = NULL;
1822 1.3 christos struct evhttp_request *req = NULL;
1823 1.3 christos
1824 1.3 christos test_ok = 0;
1825 1.3 christos http = http_setup(&port, data->base);
1826 1.3 christos
1827 1.3 christos /* 2 second timeout */
1828 1.3 christos evhttp_set_timeout(http, 1);
1829 1.3 christos
1830 1.3 christos evcon = evhttp_connection_base_new(data->base, NULL,
1831 1.3 christos "127.0.0.1", port);
1832 1.3 christos tt_assert(evcon);
1833 1.3 christos delayed_client = evcon;
1834 1.3 christos
1835 1.3 christos /*
1836 1.3 christos * At this point, we want to schedule a request to the HTTP
1837 1.3 christos * server using our make request method.
1838 1.3 christos */
1839 1.3 christos
1840 1.3 christos req = evhttp_request_new(close_detect_cb, evcon);
1841 1.3 christos
1842 1.3 christos /* Add the information that we care about */
1843 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
1844 1.3 christos
1845 1.3 christos /* We give ownership of the request to the connection */
1846 1.3 christos if (evhttp_make_request(evcon,
1847 1.3 christos req, EVHTTP_REQ_GET, with_delay ? "/largedelay" : "/test") == -1) {
1848 1.3 christos tt_abort_msg("couldn't make request");
1849 1.3 christos }
1850 1.3 christos
1851 1.3 christos event_base_dispatch(data->base);
1852 1.3 christos
1853 1.3 christos /* at this point, the http server should have no connection */
1854 1.3 christos tt_assert(TAILQ_FIRST(&http->connections) == NULL);
1855 1.3 christos
1856 1.3 christos end:
1857 1.3 christos if (evcon)
1858 1.3 christos evhttp_connection_free(evcon);
1859 1.3 christos if (http)
1860 1.3 christos evhttp_free(http);
1861 1.3 christos }
1862 1.3 christos static void
1863 1.3 christos http_close_detection_test(void *arg)
1864 1.3 christos {
1865 1.3 christos _http_close_detection(arg, 0);
1866 1.3 christos }
1867 1.3 christos static void
1868 1.3 christos http_close_detection_delay_test(void *arg)
1869 1.3 christos {
1870 1.3 christos _http_close_detection(arg, 1);
1871 1.3 christos }
1872 1.3 christos
1873 1.3 christos static void
1874 1.3 christos http_highport_test(void *arg)
1875 1.3 christos {
1876 1.3 christos struct basic_test_data *data = arg;
1877 1.3 christos int i = -1;
1878 1.3 christos struct evhttp *myhttp = NULL;
1879 1.3 christos
1880 1.3 christos /* Try a few different ports */
1881 1.3 christos for (i = 0; i < 50; ++i) {
1882 1.3 christos myhttp = evhttp_new(data->base);
1883 1.3 christos if (evhttp_bind_socket(myhttp, "127.0.0.1", 65535 - i) == 0) {
1884 1.3 christos test_ok = 1;
1885 1.3 christos evhttp_free(myhttp);
1886 1.3 christos return;
1887 1.3 christos }
1888 1.3 christos evhttp_free(myhttp);
1889 1.3 christos }
1890 1.3 christos
1891 1.3 christos tt_fail_msg("Couldn't get a high port");
1892 1.3 christos }
1893 1.3 christos
1894 1.3 christos static void
1895 1.3 christos http_bad_header_test(void *ptr)
1896 1.3 christos {
1897 1.3 christos struct evkeyvalq headers;
1898 1.3 christos
1899 1.3 christos TAILQ_INIT(&headers);
1900 1.3 christos
1901 1.3 christos tt_want(evhttp_add_header(&headers, "One", "Two") == 0);
1902 1.3 christos tt_want(evhttp_add_header(&headers, "One", "Two\r\n Three") == 0);
1903 1.3 christos tt_want(evhttp_add_header(&headers, "One\r", "Two") == -1);
1904 1.3 christos tt_want(evhttp_add_header(&headers, "One\n", "Two") == -1);
1905 1.3 christos tt_want(evhttp_add_header(&headers, "One", "Two\r") == -1);
1906 1.3 christos tt_want(evhttp_add_header(&headers, "One", "Two\n") == -1);
1907 1.3 christos
1908 1.3 christos evhttp_clear_headers(&headers);
1909 1.3 christos }
1910 1.3 christos
1911 1.3 christos static int validate_header(
1912 1.3 christos const struct evkeyvalq* headers,
1913 1.3 christos const char *key, const char *value)
1914 1.3 christos {
1915 1.3 christos const char *real_val = evhttp_find_header(headers, key);
1916 1.3 christos tt_assert(real_val != NULL);
1917 1.3 christos tt_want(strcmp(real_val, value) == 0);
1918 1.3 christos end:
1919 1.3 christos return (0);
1920 1.3 christos }
1921 1.3 christos
1922 1.3 christos static void
1923 1.3 christos http_parse_query_test(void *ptr)
1924 1.3 christos {
1925 1.3 christos struct evkeyvalq headers;
1926 1.3 christos int r;
1927 1.3 christos
1928 1.3 christos TAILQ_INIT(&headers);
1929 1.3 christos
1930 1.3 christos r = evhttp_parse_query("http://www.test.com/?q=test", &headers);
1931 1.3 christos tt_want(validate_header(&headers, "q", "test") == 0);
1932 1.3 christos tt_int_op(r, ==, 0);
1933 1.3 christos evhttp_clear_headers(&headers);
1934 1.3 christos
1935 1.3 christos r = evhttp_parse_query("http://www.test.com/?q=test&foo=bar", &headers);
1936 1.3 christos tt_want(validate_header(&headers, "q", "test") == 0);
1937 1.3 christos tt_want(validate_header(&headers, "foo", "bar") == 0);
1938 1.3 christos tt_int_op(r, ==, 0);
1939 1.3 christos evhttp_clear_headers(&headers);
1940 1.3 christos
1941 1.3 christos r = evhttp_parse_query("http://www.test.com/?q=test+foo", &headers);
1942 1.3 christos tt_want(validate_header(&headers, "q", "test foo") == 0);
1943 1.3 christos tt_int_op(r, ==, 0);
1944 1.3 christos evhttp_clear_headers(&headers);
1945 1.3 christos
1946 1.3 christos r = evhttp_parse_query("http://www.test.com/?q=test%0Afoo", &headers);
1947 1.3 christos tt_want(validate_header(&headers, "q", "test\nfoo") == 0);
1948 1.3 christos tt_int_op(r, ==, 0);
1949 1.3 christos evhttp_clear_headers(&headers);
1950 1.3 christos
1951 1.3 christos r = evhttp_parse_query("http://www.test.com/?q=test%0Dfoo", &headers);
1952 1.3 christos tt_want(validate_header(&headers, "q", "test\rfoo") == 0);
1953 1.3 christos tt_int_op(r, ==, 0);
1954 1.3 christos evhttp_clear_headers(&headers);
1955 1.3 christos
1956 1.3 christos r = evhttp_parse_query("http://www.test.com/?q=test&&q2", &headers);
1957 1.3 christos tt_int_op(r, ==, -1);
1958 1.3 christos evhttp_clear_headers(&headers);
1959 1.3 christos
1960 1.3 christos r = evhttp_parse_query("http://www.test.com/?q=test+this", &headers);
1961 1.3 christos tt_want(validate_header(&headers, "q", "test this") == 0);
1962 1.3 christos tt_int_op(r, ==, 0);
1963 1.3 christos evhttp_clear_headers(&headers);
1964 1.3 christos
1965 1.3 christos r = evhttp_parse_query("http://www.test.com/?q=test&q2=foo", &headers);
1966 1.3 christos tt_int_op(r, ==, 0);
1967 1.3 christos tt_want(validate_header(&headers, "q", "test") == 0);
1968 1.3 christos tt_want(validate_header(&headers, "q2", "foo") == 0);
1969 1.3 christos evhttp_clear_headers(&headers);
1970 1.3 christos
1971 1.3 christos r = evhttp_parse_query("http://www.test.com/?q&q2=foo", &headers);
1972 1.3 christos tt_int_op(r, ==, -1);
1973 1.3 christos evhttp_clear_headers(&headers);
1974 1.3 christos
1975 1.3 christos r = evhttp_parse_query("http://www.test.com/?q=foo&q2", &headers);
1976 1.3 christos tt_int_op(r, ==, -1);
1977 1.3 christos evhttp_clear_headers(&headers);
1978 1.3 christos
1979 1.3 christos r = evhttp_parse_query("http://www.test.com/?q=foo&q2&q3=x", &headers);
1980 1.3 christos tt_int_op(r, ==, -1);
1981 1.3 christos evhttp_clear_headers(&headers);
1982 1.3 christos
1983 1.3 christos r = evhttp_parse_query("http://www.test.com/?q=&q2=&q3=", &headers);
1984 1.3 christos tt_int_op(r, ==, 0);
1985 1.3 christos tt_want(validate_header(&headers, "q", "") == 0);
1986 1.3 christos tt_want(validate_header(&headers, "q2", "") == 0);
1987 1.3 christos tt_want(validate_header(&headers, "q3", "") == 0);
1988 1.3 christos evhttp_clear_headers(&headers);
1989 1.3 christos
1990 1.3 christos end:
1991 1.3 christos evhttp_clear_headers(&headers);
1992 1.3 christos }
1993 1.3 christos
1994 1.3 christos static void
1995 1.3 christos http_parse_uri_test(void *ptr)
1996 1.3 christos {
1997 1.3 christos const int nonconform = (ptr != NULL);
1998 1.3 christos const unsigned parse_flags =
1999 1.3 christos nonconform ? EVHTTP_URI_NONCONFORMANT : 0;
2000 1.3 christos struct evhttp_uri *uri = NULL;
2001 1.3 christos char url_tmp[4096];
2002 1.3 christos #define URI_PARSE(uri) \
2003 1.3 christos evhttp_uri_parse_with_flags((uri), parse_flags)
2004 1.3 christos
2005 1.3 christos #define TT_URI(want) do { \
2006 1.3 christos char *ret = evhttp_uri_join(uri, url_tmp, sizeof(url_tmp)); \
2007 1.3 christos tt_want(ret != NULL); \
2008 1.3 christos tt_want(ret == url_tmp); \
2009 1.3 christos if (strcmp(ret,want) != 0) \
2010 1.3 christos TT_FAIL(("\"%s\" != \"%s\"",ret,want)); \
2011 1.3 christos } while (/*CONSTCOND*/0)
2012 1.3 christos
2013 1.3 christos tt_want(evhttp_uri_join(NULL, 0, 0) == NULL);
2014 1.3 christos tt_want(evhttp_uri_join(NULL, url_tmp, 0) == NULL);
2015 1.3 christos tt_want(evhttp_uri_join(NULL, url_tmp, sizeof(url_tmp)) == NULL);
2016 1.3 christos
2017 1.3 christos /* bad URIs: parsing */
2018 1.3 christos #define BAD(s) do { \
2019 1.3 christos if (URI_PARSE(s) != NULL) \
2020 1.3 christos TT_FAIL(("Expected error parsing \"%s\"",s)); \
2021 1.3 christos } while (/*CONSTCOND*/0)
2022 1.3 christos /* Nonconformant URIs we can parse: parsing */
2023 1.3 christos #define NCF(s) do { \
2024 1.3 christos uri = URI_PARSE(s); \
2025 1.3 christos if (uri != NULL && !nonconform) { \
2026 1.3 christos TT_FAIL(("Expected error parsing \"%s\"",s)); \
2027 1.3 christos } else if (uri == NULL && nonconform) { \
2028 1.3 christos TT_FAIL(("Couldn't parse nonconformant URI \"%s\"", \
2029 1.3 christos s)); \
2030 1.3 christos } \
2031 1.3 christos if (uri) { \
2032 1.3 christos tt_want(evhttp_uri_join(uri, url_tmp, \
2033 1.3 christos sizeof(url_tmp))); \
2034 1.3 christos evhttp_uri_free(uri); \
2035 1.3 christos } \
2036 1.3 christos } while (/*CONSTCOND*/0)
2037 1.3 christos
2038 1.3 christos NCF("http://www.test.com/ why hello");
2039 1.3 christos NCF("http://www.test.com/why-hello\x01");
2040 1.3 christos NCF("http://www.test.com/why-hello?\x01");
2041 1.3 christos NCF("http://www.test.com/why-hello#\x01");
2042 1.3 christos BAD("http://www.\x01.test.com/why-hello");
2043 1.3 christos BAD("http://www.%7test.com/why-hello");
2044 1.3 christos NCF("http://www.test.com/why-hell%7o");
2045 1.3 christos BAD("h%3ttp://www.test.com/why-hello");
2046 1.3 christos NCF("http://www.test.com/why-hello%7");
2047 1.3 christos NCF("http://www.test.com/why-hell%7o");
2048 1.3 christos NCF("http://www.test.com/foo?ba%r");
2049 1.3 christos NCF("http://www.test.com/foo#ba%r");
2050 1.3 christos BAD("99:99/foo");
2051 1.3 christos BAD("http://www.test.com:999x/");
2052 1.3 christos BAD("http://www.test.com:x/");
2053 1.3 christos BAD("http://[hello-there]/");
2054 1.3 christos BAD("http://[::1]]/");
2055 1.3 christos BAD("http://[::1/");
2056 1.3 christos BAD("http://[foob/");
2057 1.3 christos BAD("http://[/");
2058 1.3 christos BAD("http://[ffff:ffff:ffff:ffff:Ffff:ffff:ffff:"
2059 1.3 christos "ffff:ffff:ffff:ffff:ffff:ffff:ffff]/");
2060 1.3 christos BAD("http://[vX.foo]/");
2061 1.3 christos BAD("http://[vX.foo]/");
2062 1.3 christos BAD("http://[v.foo]/");
2063 1.3 christos BAD("http://[v5.fo%o]/");
2064 1.3 christos BAD("http://[v5X]/");
2065 1.3 christos BAD("http://[v5]/");
2066 1.3 christos BAD("http://[]/");
2067 1.3 christos BAD("http://f\x01red@www.example.com/");
2068 1.3 christos BAD("http://f%0red@www.example.com/");
2069 1.3 christos BAD("http://www.example.com:9999999999999999999999999999999999999/");
2070 1.3 christos BAD("http://www.example.com:hihi/");
2071 1.3 christos BAD("://www.example.com/");
2072 1.3 christos
2073 1.3 christos /* bad URIs: joining */
2074 1.3 christos uri = evhttp_uri_new();
2075 1.3 christos tt_want(0==evhttp_uri_set_host(uri, "www.example.com"));
2076 1.3 christos tt_want(evhttp_uri_join(uri, url_tmp, sizeof(url_tmp)) != NULL);
2077 1.3 christos /* not enough space: */
2078 1.3 christos tt_want(evhttp_uri_join(uri, url_tmp, 3) == NULL);
2079 1.3 christos /* host is set, but path doesn't start with "/": */
2080 1.3 christos tt_want(0==evhttp_uri_set_path(uri, "hi_mom"));
2081 1.3 christos tt_want(evhttp_uri_join(uri, url_tmp, sizeof(url_tmp)) == NULL);
2082 1.3 christos tt_want(evhttp_uri_join(uri, NULL, sizeof(url_tmp))==NULL);
2083 1.3 christos tt_want(evhttp_uri_join(uri, url_tmp, 0)==NULL);
2084 1.3 christos evhttp_uri_free(uri);
2085 1.3 christos uri = URI_PARSE("mailto:foo@bar");
2086 1.3 christos tt_want(uri != NULL);
2087 1.3 christos tt_want(evhttp_uri_get_host(uri) == NULL);
2088 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2089 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2090 1.3 christos tt_want(!strcmp(evhttp_uri_get_scheme(uri), "mailto"));
2091 1.3 christos tt_want(!strcmp(evhttp_uri_get_path(uri), "foo@bar"));
2092 1.3 christos tt_want(evhttp_uri_get_query(uri) == NULL);
2093 1.3 christos tt_want(evhttp_uri_get_fragment(uri) == NULL);
2094 1.3 christos TT_URI("mailto:foo@bar");
2095 1.3 christos evhttp_uri_free(uri);
2096 1.3 christos
2097 1.3 christos uri = evhttp_uri_new();
2098 1.3 christos /* Bad URI usage: setting invalid values */
2099 1.3 christos tt_want(-1 == evhttp_uri_set_scheme(uri,""));
2100 1.3 christos tt_want(-1 == evhttp_uri_set_scheme(uri,"33"));
2101 1.3 christos tt_want(-1 == evhttp_uri_set_scheme(uri,"hi!"));
2102 1.3 christos tt_want(-1 == evhttp_uri_set_userinfo(uri,"hello@"));
2103 1.3 christos tt_want(-1 == evhttp_uri_set_host(uri,"[1.2.3.4]"));
2104 1.3 christos tt_want(-1 == evhttp_uri_set_host(uri,"["));
2105 1.3 christos tt_want(-1 == evhttp_uri_set_host(uri,"www.[foo].com"));
2106 1.3 christos tt_want(-1 == evhttp_uri_set_port(uri,-3));
2107 1.3 christos tt_want(-1 == evhttp_uri_set_path(uri,"hello?world"));
2108 1.3 christos tt_want(-1 == evhttp_uri_set_query(uri,"hello#world"));
2109 1.3 christos tt_want(-1 == evhttp_uri_set_fragment(uri,"hello#world"));
2110 1.3 christos /* Valid URI usage: setting valid values */
2111 1.3 christos tt_want(0 == evhttp_uri_set_scheme(uri,"http"));
2112 1.3 christos tt_want(0 == evhttp_uri_set_scheme(uri,NULL));
2113 1.3 christos tt_want(0 == evhttp_uri_set_userinfo(uri,"username:pass"));
2114 1.3 christos tt_want(0 == evhttp_uri_set_userinfo(uri,NULL));
2115 1.3 christos tt_want(0 == evhttp_uri_set_host(uri,"www.example.com"));
2116 1.3 christos tt_want(0 == evhttp_uri_set_host(uri,"1.2.3.4"));
2117 1.3 christos tt_want(0 == evhttp_uri_set_host(uri,"[1:2:3:4::]"));
2118 1.3 christos tt_want(0 == evhttp_uri_set_host(uri,"[v7.wobblewobble]"));
2119 1.3 christos tt_want(0 == evhttp_uri_set_host(uri,NULL));
2120 1.3 christos tt_want(0 == evhttp_uri_set_host(uri,""));
2121 1.3 christos tt_want(0 == evhttp_uri_set_port(uri, -1));
2122 1.3 christos tt_want(0 == evhttp_uri_set_port(uri, 80));
2123 1.3 christos tt_want(0 == evhttp_uri_set_port(uri, 65535));
2124 1.3 christos tt_want(0 == evhttp_uri_set_path(uri, ""));
2125 1.3 christos tt_want(0 == evhttp_uri_set_path(uri, "/documents/public/index.html"));
2126 1.3 christos tt_want(0 == evhttp_uri_set_path(uri, NULL));
2127 1.3 christos tt_want(0 == evhttp_uri_set_query(uri, "key=val&key2=val2"));
2128 1.3 christos tt_want(0 == evhttp_uri_set_query(uri, "keyvalblarg"));
2129 1.3 christos tt_want(0 == evhttp_uri_set_query(uri, ""));
2130 1.3 christos tt_want(0 == evhttp_uri_set_query(uri, NULL));
2131 1.3 christos tt_want(0 == evhttp_uri_set_fragment(uri, ""));
2132 1.3 christos tt_want(0 == evhttp_uri_set_fragment(uri, "here?i?am"));
2133 1.3 christos tt_want(0 == evhttp_uri_set_fragment(uri, NULL));
2134 1.3 christos evhttp_uri_free(uri);
2135 1.3 christos
2136 1.3 christos /* Valid parsing */
2137 1.3 christos uri = URI_PARSE("http://www.test.com/?q=t%33est");
2138 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2139 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "www.test.com") == 0);
2140 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2141 1.3 christos tt_want(strcmp(evhttp_uri_get_query(uri), "q=t%33est") == 0);
2142 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2143 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2144 1.3 christos tt_want(evhttp_uri_get_fragment(uri) == NULL);
2145 1.3 christos TT_URI("http://www.test.com/?q=t%33est");
2146 1.3 christos evhttp_uri_free(uri);
2147 1.3 christos
2148 1.3 christos uri = URI_PARSE("http://%77ww.test.com");
2149 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2150 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "%77ww.test.com") == 0);
2151 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "") == 0);
2152 1.3 christos tt_want(evhttp_uri_get_query(uri) == NULL);
2153 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2154 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2155 1.3 christos tt_want(evhttp_uri_get_fragment(uri) == NULL);
2156 1.3 christos TT_URI("http://%77ww.test.com");
2157 1.3 christos evhttp_uri_free(uri);
2158 1.3 christos
2159 1.3 christos uri = URI_PARSE("http://www.test.com?q=test");
2160 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2161 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "www.test.com") == 0);
2162 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "") == 0);
2163 1.3 christos tt_want(strcmp(evhttp_uri_get_query(uri), "q=test") == 0);
2164 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2165 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2166 1.3 christos tt_want(evhttp_uri_get_fragment(uri) == NULL);
2167 1.3 christos TT_URI("http://www.test.com?q=test");
2168 1.3 christos evhttp_uri_free(uri);
2169 1.3 christos
2170 1.3 christos uri = URI_PARSE("http://www.test.com#fragment");
2171 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2172 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "www.test.com") == 0);
2173 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "") == 0);
2174 1.3 christos tt_want(evhttp_uri_get_query(uri) == NULL);
2175 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2176 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2177 1.3 christos tt_want_str_op(evhttp_uri_get_fragment(uri), ==, "fragment");
2178 1.3 christos TT_URI("http://www.test.com#fragment");
2179 1.3 christos evhttp_uri_free(uri);
2180 1.3 christos
2181 1.3 christos uri = URI_PARSE("http://8000/");
2182 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2183 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "8000") == 0);
2184 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2185 1.3 christos tt_want(evhttp_uri_get_query(uri) == NULL);
2186 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2187 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2188 1.3 christos tt_want(evhttp_uri_get_fragment(uri) == NULL);
2189 1.3 christos TT_URI("http://8000/");
2190 1.3 christos evhttp_uri_free(uri);
2191 1.3 christos
2192 1.3 christos uri = URI_PARSE("http://:8000/");
2193 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2194 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "") == 0);
2195 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2196 1.3 christos tt_want(evhttp_uri_get_query(uri) == NULL);
2197 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2198 1.3 christos tt_want(evhttp_uri_get_port(uri) == 8000);
2199 1.3 christos tt_want(evhttp_uri_get_fragment(uri) == NULL);
2200 1.3 christos TT_URI("http://:8000/");
2201 1.3 christos evhttp_uri_free(uri);
2202 1.3 christos
2203 1.3 christos uri = URI_PARSE("http://www.test.com:/"); /* empty port */
2204 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2205 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "www.test.com") == 0);
2206 1.3 christos tt_want_str_op(evhttp_uri_get_path(uri), ==, "/");
2207 1.3 christos tt_want(evhttp_uri_get_query(uri) == NULL);
2208 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2209 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2210 1.3 christos tt_want(evhttp_uri_get_fragment(uri) == NULL);
2211 1.3 christos TT_URI("http://www.test.com/");
2212 1.3 christos evhttp_uri_free(uri);
2213 1.3 christos
2214 1.3 christos uri = URI_PARSE("http://www.test.com:"); /* empty port 2 */
2215 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "http") == 0);
2216 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "www.test.com") == 0);
2217 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "") == 0);
2218 1.3 christos tt_want(evhttp_uri_get_query(uri) == NULL);
2219 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2220 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2221 1.3 christos tt_want(evhttp_uri_get_fragment(uri) == NULL);
2222 1.3 christos TT_URI("http://www.test.com");
2223 1.3 christos evhttp_uri_free(uri);
2224 1.3 christos
2225 1.3 christos uri = URI_PARSE("ftp://www.test.com/?q=test");
2226 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "ftp") == 0);
2227 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "www.test.com") == 0);
2228 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2229 1.3 christos tt_want(strcmp(evhttp_uri_get_query(uri), "q=test") == 0);
2230 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2231 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2232 1.3 christos tt_want(evhttp_uri_get_fragment(uri) == NULL);
2233 1.3 christos TT_URI("ftp://www.test.com/?q=test");
2234 1.3 christos evhttp_uri_free(uri);
2235 1.3 christos
2236 1.3 christos uri = URI_PARSE("ftp://[::1]:999/?q=test");
2237 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "ftp") == 0);
2238 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "[::1]") == 0);
2239 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2240 1.3 christos tt_want(strcmp(evhttp_uri_get_query(uri), "q=test") == 0);
2241 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2242 1.3 christos tt_want(evhttp_uri_get_port(uri) == 999);
2243 1.3 christos tt_want(evhttp_uri_get_fragment(uri) == NULL);
2244 1.3 christos TT_URI("ftp://[::1]:999/?q=test");
2245 1.3 christos evhttp_uri_free(uri);
2246 1.3 christos
2247 1.3 christos uri = URI_PARSE("ftp://[ff00::127.0.0.1]/?q=test");
2248 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "ftp") == 0);
2249 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "[ff00::127.0.0.1]") == 0);
2250 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2251 1.3 christos tt_want(strcmp(evhttp_uri_get_query(uri), "q=test") == 0);
2252 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2253 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2254 1.3 christos tt_want(evhttp_uri_get_fragment(uri) == NULL);
2255 1.3 christos TT_URI("ftp://[ff00::127.0.0.1]/?q=test");
2256 1.3 christos evhttp_uri_free(uri);
2257 1.3 christos
2258 1.3 christos uri = URI_PARSE("ftp://[v99.not_(any:time)_soon]/?q=test");
2259 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "ftp") == 0);
2260 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "[v99.not_(any:time)_soon]") == 0);
2261 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2262 1.3 christos tt_want(strcmp(evhttp_uri_get_query(uri), "q=test") == 0);
2263 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2264 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2265 1.3 christos tt_want(evhttp_uri_get_fragment(uri) == NULL);
2266 1.3 christos TT_URI("ftp://[v99.not_(any:time)_soon]/?q=test");
2267 1.3 christos evhttp_uri_free(uri);
2268 1.3 christos
2269 1.3 christos uri = URI_PARSE("scheme://user:pass (at) foo.com:42/?q=test&s=some+thing#fragment");
2270 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "scheme") == 0);
2271 1.3 christos tt_want(strcmp(evhttp_uri_get_userinfo(uri), "user:pass") == 0);
2272 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "foo.com") == 0);
2273 1.3 christos tt_want(evhttp_uri_get_port(uri) == 42);
2274 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2275 1.3 christos tt_want(strcmp(evhttp_uri_get_query(uri), "q=test&s=some+thing") == 0);
2276 1.3 christos tt_want(strcmp(evhttp_uri_get_fragment(uri), "fragment") == 0);
2277 1.3 christos TT_URI("scheme://user:pass (at) foo.com:42/?q=test&s=some+thing#fragment");
2278 1.3 christos evhttp_uri_free(uri);
2279 1.3 christos
2280 1.3 christos uri = URI_PARSE("scheme://user (at) foo.com/#fragment");
2281 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "scheme") == 0);
2282 1.3 christos tt_want(strcmp(evhttp_uri_get_userinfo(uri), "user") == 0);
2283 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "foo.com") == 0);
2284 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2285 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2286 1.3 christos tt_want(evhttp_uri_get_query(uri) == NULL);
2287 1.3 christos tt_want(strcmp(evhttp_uri_get_fragment(uri), "fragment") == 0);
2288 1.3 christos TT_URI("scheme://user (at) foo.com/#fragment");
2289 1.3 christos evhttp_uri_free(uri);
2290 1.3 christos
2291 1.3 christos uri = URI_PARSE("scheme://%75ser (at) foo.com/#frag@ment");
2292 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "scheme") == 0);
2293 1.3 christos tt_want(strcmp(evhttp_uri_get_userinfo(uri), "%75ser") == 0);
2294 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "foo.com") == 0);
2295 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2296 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "/") == 0);
2297 1.3 christos tt_want(evhttp_uri_get_query(uri) == NULL);
2298 1.3 christos tt_want(strcmp(evhttp_uri_get_fragment(uri), "frag@ment") == 0);
2299 1.3 christos TT_URI("scheme://%75ser (at) foo.com/#frag@ment");
2300 1.3 christos evhttp_uri_free(uri);
2301 1.3 christos
2302 1.3 christos uri = URI_PARSE("file:///some/path/to/the/file");
2303 1.3 christos tt_want(strcmp(evhttp_uri_get_scheme(uri), "file") == 0);
2304 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2305 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "") == 0);
2306 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2307 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "/some/path/to/the/file") == 0);
2308 1.3 christos tt_want(evhttp_uri_get_query(uri) == NULL);
2309 1.3 christos tt_want(evhttp_uri_get_fragment(uri) == NULL);
2310 1.3 christos TT_URI("file:///some/path/to/the/file");
2311 1.3 christos evhttp_uri_free(uri);
2312 1.3 christos
2313 1.3 christos uri = URI_PARSE("///some/path/to/the-file");
2314 1.3 christos tt_want(uri != NULL);
2315 1.3 christos tt_want(evhttp_uri_get_scheme(uri) == NULL);
2316 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2317 1.3 christos tt_want(strcmp(evhttp_uri_get_host(uri), "") == 0);
2318 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2319 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "/some/path/to/the-file") == 0);
2320 1.3 christos tt_want(evhttp_uri_get_query(uri) == NULL);
2321 1.3 christos tt_want(evhttp_uri_get_fragment(uri) == NULL);
2322 1.3 christos TT_URI("///some/path/to/the-file");
2323 1.3 christos evhttp_uri_free(uri);
2324 1.3 christos
2325 1.3 christos uri = URI_PARSE("/s:ome/path/to/the-file?q=99#fred");
2326 1.3 christos tt_want(uri != NULL);
2327 1.3 christos tt_want(evhttp_uri_get_scheme(uri) == NULL);
2328 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2329 1.3 christos tt_want(evhttp_uri_get_host(uri) == NULL);
2330 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2331 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "/s:ome/path/to/the-file") == 0);
2332 1.3 christos tt_want(strcmp(evhttp_uri_get_query(uri), "q=99") == 0);
2333 1.3 christos tt_want(strcmp(evhttp_uri_get_fragment(uri), "fred") == 0);
2334 1.3 christos TT_URI("/s:ome/path/to/the-file?q=99#fred");
2335 1.3 christos evhttp_uri_free(uri);
2336 1.3 christos
2337 1.3 christos uri = URI_PARSE("relative/path/with/co:lon");
2338 1.3 christos tt_want(uri != NULL);
2339 1.3 christos tt_want(evhttp_uri_get_scheme(uri) == NULL);
2340 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2341 1.3 christos tt_want(evhttp_uri_get_host(uri) == NULL);
2342 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2343 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "relative/path/with/co:lon") == 0);
2344 1.3 christos tt_want(evhttp_uri_get_query(uri) == NULL);
2345 1.3 christos tt_want(evhttp_uri_get_fragment(uri) == NULL);
2346 1.3 christos TT_URI("relative/path/with/co:lon");
2347 1.3 christos evhttp_uri_free(uri);
2348 1.3 christos
2349 1.3 christos uri = URI_PARSE("bob?q=99&q2=q?33#fr?ed");
2350 1.3 christos tt_want(uri != NULL);
2351 1.3 christos tt_want(evhttp_uri_get_scheme(uri) == NULL);
2352 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2353 1.3 christos tt_want(evhttp_uri_get_host(uri) == NULL);
2354 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2355 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "bob") == 0);
2356 1.3 christos tt_want(strcmp(evhttp_uri_get_query(uri), "q=99&q2=q?33") == 0);
2357 1.3 christos tt_want(strcmp(evhttp_uri_get_fragment(uri), "fr?ed") == 0);
2358 1.3 christos TT_URI("bob?q=99&q2=q?33#fr?ed");
2359 1.3 christos evhttp_uri_free(uri);
2360 1.3 christos
2361 1.3 christos uri = URI_PARSE("#fr?ed");
2362 1.3 christos tt_want(uri != NULL);
2363 1.3 christos tt_want(evhttp_uri_get_scheme(uri) == NULL);
2364 1.3 christos tt_want(evhttp_uri_get_userinfo(uri) == NULL);
2365 1.3 christos tt_want(evhttp_uri_get_host(uri) == NULL);
2366 1.3 christos tt_want(evhttp_uri_get_port(uri) == -1);
2367 1.3 christos tt_want(strcmp(evhttp_uri_get_path(uri), "") == 0);
2368 1.3 christos tt_want(evhttp_uri_get_query(uri) == NULL);
2369 1.3 christos tt_want(strcmp(evhttp_uri_get_fragment(uri), "fr?ed") == 0);
2370 1.3 christos TT_URI("#fr?ed");
2371 1.3 christos evhttp_uri_free(uri);
2372 1.3 christos #undef URI_PARSE
2373 1.3 christos #undef TT_URI
2374 1.3 christos #undef BAD
2375 1.3 christos }
2376 1.3 christos
2377 1.3 christos static void
2378 1.3 christos http_uriencode_test(void *ptr)
2379 1.3 christos {
2380 1.3 christos char *s=NULL, *s2=NULL;
2381 1.3 christos size_t sz;
2382 1.3 christos
2383 1.3 christos #define ENC(from,want,plus) do { \
2384 1.3 christos s = evhttp_uriencode((from), -1, (plus)); \
2385 1.3 christos tt_assert(s); \
2386 1.3 christos tt_str_op(s,==,(want)); \
2387 1.3 christos sz = -1; \
2388 1.3 christos s2 = evhttp_uridecode((s), (plus), &sz); \
2389 1.3 christos tt_assert(s2); \
2390 1.3 christos tt_str_op(s2,==,(from)); \
2391 1.3 christos tt_int_op(sz,==,strlen(from)); \
2392 1.3 christos free(s); \
2393 1.3 christos free(s2); \
2394 1.3 christos s = s2 = NULL; \
2395 1.3 christos } while (/*CONSTCOND*/0)
2396 1.3 christos
2397 1.3 christos #define DEC(from,want,dp) do { \
2398 1.3 christos s = evhttp_uridecode((from),(dp),&sz); \
2399 1.3 christos tt_assert(s); \
2400 1.3 christos tt_str_op(s,==,(want)); \
2401 1.3 christos tt_int_op(sz,==,strlen(want)); \
2402 1.3 christos free(s); \
2403 1.3 christos s = NULL; \
2404 1.3 christos } while (/*CONSTCOND*/0)
2405 1.3 christos
2406 1.3 christos #define OLD_DEC(from,want) do { \
2407 1.3 christos s = evhttp_decode_uri((from)); \
2408 1.3 christos tt_assert(s); \
2409 1.3 christos tt_str_op(s,==,(want)); \
2410 1.3 christos free(s); \
2411 1.3 christos s = NULL; \
2412 1.3 christos } while (/*CONSTCOND*/0)
2413 1.3 christos
2414 1.3 christos
2415 1.3 christos ENC("Hello", "Hello",0);
2416 1.3 christos ENC("99", "99",0);
2417 1.3 christos ENC("", "",0);
2418 1.3 christos ENC(
2419 1.3 christos "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-.~_",
2420 1.3 christos "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-.~_",0);
2421 1.3 christos ENC(" ", "%20",0);
2422 1.3 christos ENC(" ", "+",1);
2423 1.3 christos ENC("\xff\xf0\xe0", "%FF%F0%E0",0);
2424 1.3 christos ENC("\x01\x19", "%01%19",1);
2425 1.3 christos ENC("http://www.ietf.org/rfc/rfc3986.txt",
2426 1.3 christos "http%3A%2F%2Fwww.ietf.org%2Frfc%2Frfc3986.txt",1);
2427 1.3 christos
2428 1.3 christos ENC("1+2=3", "1%2B2%3D3",1);
2429 1.3 christos ENC("1+2=3", "1%2B2%3D3",0);
2430 1.3 christos
2431 1.3 christos /* Now try encoding with internal NULs. */
2432 1.3 christos s = evhttp_uriencode("hello\0world", 11, 0);
2433 1.3 christos tt_assert(s);
2434 1.3 christos tt_str_op(s,==,"hello%00world");
2435 1.3 christos free(s);
2436 1.3 christos s = NULL;
2437 1.3 christos
2438 1.3 christos /* Now try out some decoding cases that we don't generate with
2439 1.3 christos * encode_uri: Make sure that malformed stuff doesn't crash... */
2440 1.3 christos DEC("%%xhello th+ere \xff",
2441 1.3 christos "%%xhello th+ere \xff", 0);
2442 1.3 christos /* Make sure plus decoding works */
2443 1.3 christos DEC("plus+should%20work+", "plus should work ",1);
2444 1.3 christos /* Try some lowercase hex */
2445 1.3 christos DEC("%f0%a0%b0", "\xf0\xa0\xb0",1);
2446 1.3 christos
2447 1.3 christos /* Try an internal NUL. */
2448 1.3 christos sz = 0;
2449 1.3 christos s = evhttp_uridecode("%00%00x%00%00", 1, &sz);
2450 1.3 christos tt_int_op(sz,==,5);
2451 1.3 christos tt_assert(!memcmp(s, "\0\0x\0\0", 5));
2452 1.3 christos free(s);
2453 1.3 christos s = NULL;
2454 1.3 christos
2455 1.3 christos /* Try with size == NULL */
2456 1.3 christos sz = 0;
2457 1.3 christos s = evhttp_uridecode("%00%00x%00%00", 1, NULL);
2458 1.3 christos tt_assert(!memcmp(s, "\0\0x\0\0", 5));
2459 1.3 christos free(s);
2460 1.3 christos s = NULL;
2461 1.3 christos
2462 1.3 christos /* Test out the crazy old behavior of the deprecated
2463 1.3 christos * evhttp_decode_uri */
2464 1.3 christos OLD_DEC("http://example.com/normal+path/?key=val+with+spaces",
2465 1.3 christos "http://example.com/normal+path/?key=val with spaces");
2466 1.3 christos
2467 1.3 christos end:
2468 1.3 christos if (s)
2469 1.3 christos free(s);
2470 1.3 christos if (s2)
2471 1.3 christos free(s2);
2472 1.3 christos #undef ENC
2473 1.3 christos #undef DEC
2474 1.3 christos #undef OLD_DEC
2475 1.3 christos }
2476 1.3 christos
2477 1.3 christos static void
2478 1.3 christos http_base_test(void *ptr)
2479 1.3 christos {
2480 1.3 christos struct event_base *base = NULL;
2481 1.3 christos struct bufferevent *bev;
2482 1.3 christos evutil_socket_t fd;
2483 1.3 christos const char *http_request;
2484 1.3 christos ev_uint16_t port = 0;
2485 1.3 christos
2486 1.3 christos test_ok = 0;
2487 1.3 christos base = event_base_new();
2488 1.3 christos http = http_setup(&port, base);
2489 1.3 christos
2490 1.3 christos fd = http_connect("127.0.0.1", port);
2491 1.3 christos
2492 1.3 christos /* Stupid thing to send a request */
2493 1.3 christos bev = bufferevent_socket_new(base, fd, 0);
2494 1.3 christos bufferevent_setcb(bev, http_readcb, http_writecb,
2495 1.3 christos http_errorcb, base);
2496 1.3 christos bufferevent_base_set(base, bev);
2497 1.3 christos
2498 1.3 christos http_request =
2499 1.3 christos "GET /test HTTP/1.1\r\n"
2500 1.3 christos "Host: somehost\r\n"
2501 1.3 christos "Connection: close\r\n"
2502 1.3 christos "\r\n";
2503 1.3 christos
2504 1.3 christos bufferevent_write(bev, http_request, strlen(http_request));
2505 1.3 christos
2506 1.3 christos event_base_dispatch(base);
2507 1.3 christos
2508 1.3 christos bufferevent_free(bev);
2509 1.3 christos evutil_closesocket(fd);
2510 1.3 christos
2511 1.3 christos evhttp_free(http);
2512 1.3 christos
2513 1.3 christos tt_int_op(test_ok, ==, 2);
2514 1.3 christos
2515 1.3 christos end:
2516 1.3 christos if (base)
2517 1.3 christos event_base_free(base);
2518 1.3 christos }
2519 1.3 christos
2520 1.3 christos /*
2521 1.3 christos * the server is just going to close the connection if it times out during
2522 1.3 christos * reading the headers.
2523 1.3 christos */
2524 1.3 christos
2525 1.3 christos static void
2526 1.3 christos http_incomplete_readcb(struct bufferevent *bev, void *arg)
2527 1.3 christos {
2528 1.3 christos test_ok = -1;
2529 1.3 christos event_base_loopexit(exit_base,NULL);
2530 1.3 christos }
2531 1.3 christos
2532 1.3 christos static void
2533 1.3 christos http_incomplete_errorcb(struct bufferevent *bev, short what, void *arg)
2534 1.3 christos {
2535 1.3 christos if (what == (BEV_EVENT_READING|BEV_EVENT_EOF))
2536 1.3 christos test_ok++;
2537 1.3 christos else
2538 1.3 christos test_ok = -2;
2539 1.3 christos event_base_loopexit(exit_base,NULL);
2540 1.3 christos }
2541 1.3 christos
2542 1.3 christos static void
2543 1.3 christos http_incomplete_writecb(struct bufferevent *bev, void *arg)
2544 1.3 christos {
2545 1.3 christos if (arg != NULL) {
2546 1.3 christos evutil_socket_t fd = *(evutil_socket_t *)arg;
2547 1.3 christos /* terminate the write side to simulate EOF */
2548 1.3 christos shutdown(fd, SHUT_WR);
2549 1.3 christos }
2550 1.3 christos if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
2551 1.3 christos /* enable reading of the reply */
2552 1.3 christos bufferevent_enable(bev, EV_READ);
2553 1.3 christos test_ok++;
2554 1.3 christos }
2555 1.3 christos }
2556 1.3 christos
2557 1.3 christos static void
2558 1.3 christos _http_incomplete_test(struct basic_test_data *data, int use_timeout)
2559 1.3 christos {
2560 1.3 christos struct bufferevent *bev;
2561 1.3 christos evutil_socket_t fd;
2562 1.3 christos const char *http_request;
2563 1.3 christos ev_uint16_t port = 0;
2564 1.3 christos struct timeval tv_start, tv_end;
2565 1.3 christos
2566 1.3 christos exit_base = data->base;
2567 1.3 christos
2568 1.3 christos test_ok = 0;
2569 1.3 christos
2570 1.3 christos http = http_setup(&port, data->base);
2571 1.3 christos evhttp_set_timeout(http, 1);
2572 1.3 christos
2573 1.3 christos fd = http_connect("127.0.0.1", port);
2574 1.3 christos
2575 1.3 christos /* Stupid thing to send a request */
2576 1.3 christos bev = bufferevent_socket_new(data->base, fd, 0);
2577 1.3 christos bufferevent_setcb(bev,
2578 1.3 christos http_incomplete_readcb, http_incomplete_writecb,
2579 1.3 christos http_incomplete_errorcb, use_timeout ? NULL : &fd);
2580 1.3 christos
2581 1.3 christos http_request =
2582 1.3 christos "GET /test HTTP/1.1\r\n"
2583 1.3 christos "Host: somehost\r\n";
2584 1.3 christos
2585 1.3 christos bufferevent_write(bev, http_request, strlen(http_request));
2586 1.3 christos
2587 1.3 christos evutil_gettimeofday(&tv_start, NULL);
2588 1.3 christos
2589 1.3 christos event_base_dispatch(data->base);
2590 1.3 christos
2591 1.3 christos evutil_gettimeofday(&tv_end, NULL);
2592 1.3 christos evutil_timersub(&tv_end, &tv_start, &tv_end);
2593 1.3 christos
2594 1.3 christos bufferevent_free(bev);
2595 1.3 christos if (use_timeout) {
2596 1.3 christos evutil_closesocket(fd);
2597 1.3 christos }
2598 1.3 christos
2599 1.3 christos evhttp_free(http);
2600 1.3 christos
2601 1.3 christos if (use_timeout && tv_end.tv_sec >= 3) {
2602 1.3 christos tt_abort_msg("time");
2603 1.3 christos } else if (!use_timeout && tv_end.tv_sec >= 1) {
2604 1.3 christos /* we should be done immediately */
2605 1.3 christos tt_abort_msg("time");
2606 1.3 christos }
2607 1.3 christos
2608 1.3 christos tt_int_op(test_ok, ==, 2);
2609 1.3 christos end:
2610 1.3 christos ;
2611 1.3 christos }
2612 1.3 christos static void
2613 1.3 christos http_incomplete_test(void *arg)
2614 1.3 christos {
2615 1.3 christos _http_incomplete_test(arg, 0);
2616 1.3 christos }
2617 1.3 christos static void
2618 1.3 christos http_incomplete_timeout_test(void *arg)
2619 1.3 christos {
2620 1.3 christos _http_incomplete_test(arg, 1);
2621 1.3 christos }
2622 1.3 christos
2623 1.3 christos /*
2624 1.3 christos * the server is going to reply with chunked data.
2625 1.3 christos */
2626 1.3 christos
2627 1.3 christos static void
2628 1.3 christos http_chunked_readcb(struct bufferevent *bev, void *arg)
2629 1.3 christos {
2630 1.3 christos /* nothing here */
2631 1.3 christos }
2632 1.3 christos
2633 1.3 christos static void
2634 1.3 christos http_chunked_errorcb(struct bufferevent *bev, short what, void *arg)
2635 1.3 christos {
2636 1.3 christos if (!test_ok)
2637 1.3 christos goto out;
2638 1.3 christos
2639 1.3 christos test_ok = -1;
2640 1.3 christos
2641 1.3 christos if ((what & BEV_EVENT_EOF) != 0) {
2642 1.3 christos struct evhttp_request *req = evhttp_request_new(NULL, NULL);
2643 1.3 christos const char *header;
2644 1.3 christos enum message_read_status done;
2645 1.3 christos
2646 1.3 christos /* req->kind = EVHTTP_RESPONSE; */
2647 1.3 christos done = evhttp_parse_firstline(req, bufferevent_get_input(bev));
2648 1.3 christos if (done != ALL_DATA_READ)
2649 1.3 christos goto out;
2650 1.3 christos
2651 1.3 christos done = evhttp_parse_headers(req, bufferevent_get_input(bev));
2652 1.3 christos if (done != ALL_DATA_READ)
2653 1.3 christos goto out;
2654 1.3 christos
2655 1.3 christos header = evhttp_find_header(evhttp_request_get_input_headers(req), "Transfer-Encoding");
2656 1.3 christos if (header == NULL || strcmp(header, "chunked"))
2657 1.3 christos goto out;
2658 1.3 christos
2659 1.3 christos header = evhttp_find_header(evhttp_request_get_input_headers(req), "Connection");
2660 1.3 christos if (header == NULL || strcmp(header, "close"))
2661 1.3 christos goto out;
2662 1.3 christos
2663 1.3 christos header = evbuffer_readln(bufferevent_get_input(bev), NULL, EVBUFFER_EOL_CRLF);
2664 1.3 christos if (header == NULL)
2665 1.3 christos goto out;
2666 1.3 christos /* 13 chars */
2667 1.3 christos if (strcmp(header, "d")) {
2668 1.3 christos free(__UNCONST(header));
2669 1.3 christos goto out;
2670 1.3 christos }
2671 1.3 christos free(__UNCONST(header));
2672 1.3 christos
2673 1.3 christos if (strncmp((char *)evbuffer_pullup(bufferevent_get_input(bev), 13),
2674 1.3 christos "This is funny", 13))
2675 1.3 christos goto out;
2676 1.3 christos
2677 1.3 christos evbuffer_drain(bufferevent_get_input(bev), 13 + 2);
2678 1.3 christos
2679 1.3 christos header = evbuffer_readln(bufferevent_get_input(bev), NULL, EVBUFFER_EOL_CRLF);
2680 1.3 christos if (header == NULL)
2681 1.3 christos goto out;
2682 1.3 christos /* 18 chars */
2683 1.3 christos if (strcmp(header, "12"))
2684 1.3 christos goto out;
2685 1.3 christos free(__UNCONST(header));
2686 1.3 christos
2687 1.3 christos if (strncmp((char *)evbuffer_pullup(bufferevent_get_input(bev), 18),
2688 1.3 christos "but not hilarious.", 18))
2689 1.3 christos goto out;
2690 1.3 christos
2691 1.3 christos evbuffer_drain(bufferevent_get_input(bev), 18 + 2);
2692 1.3 christos
2693 1.3 christos header = evbuffer_readln(bufferevent_get_input(bev), NULL, EVBUFFER_EOL_CRLF);
2694 1.3 christos if (header == NULL)
2695 1.3 christos goto out;
2696 1.3 christos /* 8 chars */
2697 1.3 christos if (strcmp(header, "8")) {
2698 1.3 christos free(__UNCONST(header));
2699 1.3 christos goto out;
2700 1.3 christos }
2701 1.3 christos free(__UNCONST(header));
2702 1.3 christos
2703 1.3 christos if (strncmp((char *)evbuffer_pullup(bufferevent_get_input(bev), 8),
2704 1.3 christos "bwv 1052.", 8))
2705 1.3 christos goto out;
2706 1.3 christos
2707 1.3 christos evbuffer_drain(bufferevent_get_input(bev), 8 + 2);
2708 1.3 christos
2709 1.3 christos header = evbuffer_readln(bufferevent_get_input(bev), NULL, EVBUFFER_EOL_CRLF);
2710 1.3 christos if (header == NULL)
2711 1.3 christos goto out;
2712 1.3 christos /* 0 chars */
2713 1.3 christos if (strcmp(header, "0")) {
2714 1.3 christos free(__UNCONST(header));
2715 1.3 christos goto out;
2716 1.3 christos }
2717 1.3 christos free(__UNCONST(header));
2718 1.3 christos
2719 1.3 christos test_ok = 2;
2720 1.1 plunky
2721 1.3 christos evhttp_request_free(req);
2722 1.1 plunky }
2723 1.1 plunky
2724 1.3 christos out:
2725 1.3 christos event_base_loopexit(arg, NULL);
2726 1.3 christos }
2727 1.1 plunky
2728 1.3 christos static void
2729 1.3 christos http_chunked_writecb(struct bufferevent *bev, void *arg)
2730 1.3 christos {
2731 1.3 christos if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
2732 1.3 christos /* enable reading of the reply */
2733 1.3 christos bufferevent_enable(bev, EV_READ);
2734 1.3 christos test_ok++;
2735 1.1 plunky }
2736 1.1 plunky }
2737 1.1 plunky
2738 1.3 christos static void
2739 1.3 christos http_chunked_request_done(struct evhttp_request *req, void *arg)
2740 1.1 plunky {
2741 1.3 christos if (evhttp_request_get_response_code(req) != HTTP_OK) {
2742 1.3 christos fprintf(stderr, "FAILED\n");
2743 1.1 plunky exit(1);
2744 1.1 plunky }
2745 1.1 plunky
2746 1.3 christos if (evhttp_find_header(evhttp_request_get_input_headers(req),
2747 1.3 christos "Transfer-Encoding") == NULL) {
2748 1.3 christos fprintf(stderr, "FAILED\n");
2749 1.1 plunky exit(1);
2750 1.1 plunky }
2751 1.1 plunky
2752 1.3 christos if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != 13 + 18 + 8) {
2753 1.3 christos fprintf(stderr, "FAILED\n");
2754 1.1 plunky exit(1);
2755 1.1 plunky }
2756 1.1 plunky
2757 1.3 christos if (strncmp((char *)evbuffer_pullup(evhttp_request_get_input_buffer(req), 13 + 18 + 8),
2758 1.3 christos "This is funnybut not hilarious.bwv 1052",
2759 1.3 christos 13 + 18 + 8)) {
2760 1.3 christos fprintf(stderr, "FAILED\n");
2761 1.1 plunky exit(1);
2762 1.1 plunky }
2763 1.1 plunky
2764 1.1 plunky test_ok = 1;
2765 1.3 christos event_base_loopexit(arg, NULL);
2766 1.1 plunky }
2767 1.1 plunky
2768 1.1 plunky static void
2769 1.3 christos http_chunk_out_test(void *arg)
2770 1.1 plunky {
2771 1.3 christos struct basic_test_data *data = arg;
2772 1.1 plunky struct bufferevent *bev;
2773 1.3 christos evutil_socket_t fd;
2774 1.1 plunky const char *http_request;
2775 1.3 christos ev_uint16_t port = 0;
2776 1.3 christos struct timeval tv_start, tv_end;
2777 1.3 christos struct evhttp_connection *evcon = NULL;
2778 1.3 christos struct evhttp_request *req = NULL;
2779 1.3 christos int i;
2780 1.1 plunky
2781 1.3 christos exit_base = data->base;
2782 1.1 plunky test_ok = 0;
2783 1.1 plunky
2784 1.3 christos http = http_setup(&port, data->base);
2785 1.3 christos
2786 1.1 plunky fd = http_connect("127.0.0.1", port);
2787 1.1 plunky
2788 1.1 plunky /* Stupid thing to send a request */
2789 1.3 christos bev = bufferevent_socket_new(data->base, fd, 0);
2790 1.3 christos bufferevent_setcb(bev,
2791 1.3 christos http_chunked_readcb, http_chunked_writecb,
2792 1.3 christos http_chunked_errorcb, data->base);
2793 1.1 plunky
2794 1.3 christos http_request =
2795 1.3 christos "GET /chunked HTTP/1.1\r\n"
2796 1.3 christos "Host: somehost\r\n"
2797 1.3 christos "Connection: close\r\n"
2798 1.3 christos "\r\n";
2799 1.1 plunky
2800 1.1 plunky bufferevent_write(bev, http_request, strlen(http_request));
2801 1.1 plunky
2802 1.3 christos evutil_gettimeofday(&tv_start, NULL);
2803 1.1 plunky
2804 1.3 christos event_base_dispatch(data->base);
2805 1.1 plunky
2806 1.3 christos bufferevent_free(bev);
2807 1.1 plunky
2808 1.3 christos evutil_gettimeofday(&tv_end, NULL);
2809 1.3 christos evutil_timersub(&tv_end, &tv_start, &tv_end);
2810 1.1 plunky
2811 1.3 christos tt_int_op(tv_end.tv_sec, <, 1);
2812 1.1 plunky
2813 1.3 christos tt_int_op(test_ok, ==, 2);
2814 1.1 plunky
2815 1.3 christos /* now try again with the regular connection object */
2816 1.3 christos evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
2817 1.3 christos tt_assert(evcon);
2818 1.1 plunky
2819 1.3 christos /* make two requests to check the keepalive behavior */
2820 1.3 christos for (i = 0; i < 2; i++) {
2821 1.3 christos test_ok = 0;
2822 1.3 christos req = evhttp_request_new(http_chunked_request_done,data->base);
2823 1.1 plunky
2824 1.3 christos /* Add the information that we care about */
2825 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
2826 1.1 plunky
2827 1.3 christos /* We give ownership of the request to the connection */
2828 1.3 christos if (evhttp_make_request(evcon, req,
2829 1.3 christos EVHTTP_REQ_GET, "/chunked") == -1) {
2830 1.3 christos tt_abort_msg("Couldn't make request");
2831 1.3 christos }
2832 1.1 plunky
2833 1.3 christos event_base_dispatch(data->base);
2834 1.1 plunky
2835 1.3 christos tt_assert(test_ok == 1);
2836 1.1 plunky }
2837 1.1 plunky
2838 1.3 christos end:
2839 1.3 christos if (evcon)
2840 1.3 christos evhttp_connection_free(evcon);
2841 1.3 christos if (http)
2842 1.3 christos evhttp_free(http);
2843 1.1 plunky }
2844 1.1 plunky
2845 1.1 plunky static void
2846 1.3 christos http_stream_out_test(void *arg)
2847 1.1 plunky {
2848 1.3 christos struct basic_test_data *data = arg;
2849 1.3 christos ev_uint16_t port = 0;
2850 1.1 plunky struct evhttp_connection *evcon = NULL;
2851 1.1 plunky struct evhttp_request *req = NULL;
2852 1.3 christos
2853 1.1 plunky test_ok = 0;
2854 1.3 christos exit_base = data->base;
2855 1.1 plunky
2856 1.3 christos http = http_setup(&port, data->base);
2857 1.1 plunky
2858 1.3 christos evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
2859 1.3 christos tt_assert(evcon);
2860 1.1 plunky
2861 1.1 plunky /*
2862 1.1 plunky * At this point, we want to schedule a request to the HTTP
2863 1.1 plunky * server using our make request method.
2864 1.1 plunky */
2865 1.1 plunky
2866 1.3 christos req = evhttp_request_new(http_request_done,
2867 1.3 christos __UNCONST("This is funnybut not hilarious.bwv 1052"));
2868 1.1 plunky
2869 1.1 plunky /* Add the information that we care about */
2870 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
2871 1.1 plunky
2872 1.1 plunky /* We give ownership of the request to the connection */
2873 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/streamed")
2874 1.3 christos == -1) {
2875 1.3 christos tt_abort_msg("Couldn't make request");
2876 1.1 plunky }
2877 1.1 plunky
2878 1.3 christos event_base_dispatch(data->base);
2879 1.3 christos
2880 1.3 christos end:
2881 1.3 christos if (evcon)
2882 1.3 christos evhttp_connection_free(evcon);
2883 1.3 christos if (http)
2884 1.3 christos evhttp_free(http);
2885 1.3 christos }
2886 1.1 plunky
2887 1.3 christos static void
2888 1.3 christos http_stream_in_chunk(struct evhttp_request *req, void *arg)
2889 1.3 christos {
2890 1.3 christos struct evbuffer *reply = arg;
2891 1.1 plunky
2892 1.3 christos if (evhttp_request_get_response_code(req) != HTTP_OK) {
2893 1.3 christos fprintf(stderr, "FAILED\n");
2894 1.1 plunky exit(1);
2895 1.1 plunky }
2896 1.1 plunky
2897 1.3 christos evbuffer_add_buffer(reply, evhttp_request_get_input_buffer(req));
2898 1.1 plunky }
2899 1.1 plunky
2900 1.1 plunky static void
2901 1.3 christos http_stream_in_done(struct evhttp_request *req, void *arg)
2902 1.1 plunky {
2903 1.3 christos if (evbuffer_get_length(evhttp_request_get_input_buffer(req)) != 0) {
2904 1.3 christos fprintf(stderr, "FAILED\n");
2905 1.3 christos exit(1);
2906 1.1 plunky }
2907 1.1 plunky
2908 1.3 christos event_base_loopexit(exit_base, NULL);
2909 1.1 plunky }
2910 1.1 plunky
2911 1.3 christos /**
2912 1.3 christos * Makes a request and reads the response in chunks.
2913 1.3 christos */
2914 1.1 plunky static void
2915 1.3 christos _http_stream_in_test(struct basic_test_data *data, char const *url,
2916 1.3 christos size_t expected_len, char const *expected)
2917 1.1 plunky {
2918 1.3 christos struct evhttp_connection *evcon;
2919 1.3 christos struct evbuffer *reply = evbuffer_new();
2920 1.3 christos struct evhttp_request *req = NULL;
2921 1.3 christos ev_uint16_t port = 0;
2922 1.3 christos
2923 1.3 christos exit_base = data->base;
2924 1.3 christos http = http_setup(&port, data->base);
2925 1.3 christos
2926 1.3 christos evcon = evhttp_connection_base_new(data->base, NULL,"127.0.0.1", port);
2927 1.3 christos tt_assert(evcon);
2928 1.3 christos
2929 1.3 christos req = evhttp_request_new(http_stream_in_done, reply);
2930 1.3 christos evhttp_request_set_chunked_cb(req, http_stream_in_chunk);
2931 1.1 plunky
2932 1.3 christos /* We give ownership of the request to the connection */
2933 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, url) == -1) {
2934 1.3 christos tt_abort_msg("Couldn't make request");
2935 1.3 christos }
2936 1.1 plunky
2937 1.3 christos event_base_dispatch(data->base);
2938 1.1 plunky
2939 1.3 christos if (evbuffer_get_length(reply) != expected_len) {
2940 1.3 christos TT_DIE(("reply length %lu; expected %lu; FAILED (%s)\n",
2941 1.3 christos (unsigned long)evbuffer_get_length(reply),
2942 1.3 christos (unsigned long)expected_len,
2943 1.3 christos (char*)evbuffer_pullup(reply, -1)));
2944 1.3 christos }
2945 1.1 plunky
2946 1.3 christos if (memcmp(evbuffer_pullup(reply, -1), expected, expected_len) != 0) {
2947 1.3 christos tt_abort_msg("Memory mismatch");
2948 1.3 christos }
2949 1.1 plunky
2950 1.3 christos test_ok = 1;
2951 1.3 christos end:
2952 1.3 christos if (reply)
2953 1.3 christos evbuffer_free(reply);
2954 1.3 christos if (evcon)
2955 1.3 christos evhttp_connection_free(evcon);
2956 1.3 christos if (http)
2957 1.3 christos evhttp_free(http);
2958 1.1 plunky }
2959 1.1 plunky
2960 1.3 christos static void
2961 1.3 christos http_stream_in_test(void *arg)
2962 1.1 plunky {
2963 1.3 christos _http_stream_in_test(arg, "/chunked", 13 + 18 + 8,
2964 1.3 christos "This is funnybut not hilarious.bwv 1052");
2965 1.3 christos
2966 1.3 christos _http_stream_in_test(arg, "/test", strlen(BASIC_REQUEST_BODY),
2967 1.3 christos BASIC_REQUEST_BODY);
2968 1.1 plunky }
2969 1.1 plunky
2970 1.1 plunky static void
2971 1.3 christos http_stream_in_cancel_chunk(struct evhttp_request *req, void *arg)
2972 1.1 plunky {
2973 1.3 christos tt_int_op(evhttp_request_get_response_code(req), ==, HTTP_OK);
2974 1.1 plunky
2975 1.3 christos end:
2976 1.3 christos evhttp_cancel_request(req);
2977 1.3 christos event_base_loopexit(arg, NULL);
2978 1.3 christos }
2979 1.1 plunky
2980 1.3 christos static void
2981 1.3 christos http_stream_in_cancel_done(struct evhttp_request *req, void *arg)
2982 1.3 christos {
2983 1.3 christos /* should never be called */
2984 1.3 christos tt_fail_msg("In cancel done");
2985 1.1 plunky }
2986 1.1 plunky
2987 1.1 plunky static void
2988 1.3 christos http_stream_in_cancel_test(void *arg)
2989 1.1 plunky {
2990 1.3 christos struct basic_test_data *data = arg;
2991 1.3 christos struct evhttp_connection *evcon;
2992 1.3 christos struct evhttp_request *req = NULL;
2993 1.3 christos ev_uint16_t port = 0;
2994 1.1 plunky
2995 1.3 christos http = http_setup(&port, data->base);
2996 1.1 plunky
2997 1.3 christos evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
2998 1.3 christos tt_assert(evcon);
2999 1.1 plunky
3000 1.3 christos req = evhttp_request_new(http_stream_in_cancel_done, data->base);
3001 1.3 christos evhttp_request_set_chunked_cb(req, http_stream_in_cancel_chunk);
3002 1.1 plunky
3003 1.3 christos /* We give ownership of the request to the connection */
3004 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/chunked") == -1) {
3005 1.3 christos tt_abort_msg("Couldn't make request");
3006 1.3 christos }
3007 1.1 plunky
3008 1.3 christos event_base_dispatch(data->base);
3009 1.1 plunky
3010 1.3 christos test_ok = 1;
3011 1.3 christos end:
3012 1.3 christos evhttp_connection_free(evcon);
3013 1.1 plunky evhttp_free(http);
3014 1.1 plunky
3015 1.1 plunky }
3016 1.1 plunky
3017 1.1 plunky static void
3018 1.3 christos http_connection_fail_done(struct evhttp_request *req, void *arg)
3019 1.1 plunky {
3020 1.3 christos /* An ENETUNREACH error results in an unrecoverable
3021 1.3 christos * evhttp_connection error (see evhttp_connection_fail()). The
3022 1.3 christos * connection will be reset, and the user will be notified with a NULL
3023 1.3 christos * req parameter. */
3024 1.3 christos tt_assert(!req);
3025 1.1 plunky
3026 1.3 christos test_ok = 1;
3027 1.1 plunky
3028 1.3 christos end:
3029 1.3 christos event_base_loopexit(arg, NULL);
3030 1.3 christos }
3031 1.1 plunky
3032 1.3 christos /* Test unrecoverable evhttp_connection errors by generating an ENETUNREACH
3033 1.3 christos * error on connection. */
3034 1.3 christos static void
3035 1.3 christos http_connection_fail_test(void *arg)
3036 1.3 christos {
3037 1.3 christos struct basic_test_data *data = arg;
3038 1.3 christos ev_uint16_t port = 0;
3039 1.3 christos struct evhttp_connection *evcon = NULL;
3040 1.3 christos struct evhttp_request *req = NULL;
3041 1.1 plunky
3042 1.3 christos exit_base = data->base;
3043 1.3 christos test_ok = 0;
3044 1.1 plunky
3045 1.3 christos /* auto detect a port */
3046 1.3 christos http = http_setup(&port, data->base);
3047 1.3 christos evhttp_free(http);
3048 1.3 christos http = NULL;
3049 1.1 plunky
3050 1.3 christos /* Pick an unroutable address. This administratively scoped multicast
3051 1.3 christos * address should do when working with TCP. */
3052 1.3 christos evcon = evhttp_connection_base_new(data->base, NULL, "239.10.20.30", 80);
3053 1.3 christos tt_assert(evcon);
3054 1.1 plunky
3055 1.3 christos /*
3056 1.3 christos * At this point, we want to schedule an HTTP GET request
3057 1.3 christos * server using our make request method.
3058 1.3 christos */
3059 1.1 plunky
3060 1.3 christos req = evhttp_request_new(http_connection_fail_done, data->base);
3061 1.3 christos tt_assert(req);
3062 1.1 plunky
3063 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/") == -1) {
3064 1.3 christos tt_abort_msg("Couldn't make request");
3065 1.3 christos }
3066 1.1 plunky
3067 1.3 christos event_base_dispatch(data->base);
3068 1.1 plunky
3069 1.3 christos tt_int_op(test_ok, ==, 1);
3070 1.1 plunky
3071 1.3 christos end:
3072 1.3 christos if (evcon)
3073 1.3 christos evhttp_connection_free(evcon);
3074 1.1 plunky }
3075 1.1 plunky
3076 1.1 plunky static void
3077 1.3 christos http_connection_retry_done(struct evhttp_request *req, void *arg)
3078 1.1 plunky {
3079 1.3 christos tt_assert(req);
3080 1.3 christos tt_int_op(evhttp_request_get_response_code(req), !=, HTTP_OK);
3081 1.3 christos if (evhttp_find_header(evhttp_request_get_input_headers(req), "Content-Type") != NULL) {
3082 1.3 christos tt_abort_msg("(content type)\n");
3083 1.1 plunky }
3084 1.3 christos
3085 1.3 christos tt_uint_op(evbuffer_get_length(evhttp_request_get_input_buffer(req)), ==, 0);
3086 1.3 christos
3087 1.3 christos test_ok = 1;
3088 1.3 christos end:
3089 1.3 christos event_base_loopexit(arg,NULL);
3090 1.1 plunky }
3091 1.1 plunky
3092 1.3 christos static struct event_base *http_make_web_server_base=NULL;
3093 1.1 plunky static void
3094 1.3 christos http_make_web_server(evutil_socket_t fd, short what, void *arg)
3095 1.1 plunky {
3096 1.3 christos ev_uint16_t port = *(ev_uint16_t*)arg;
3097 1.3 christos http = http_setup(&port, http_make_web_server_base);
3098 1.1 plunky }
3099 1.1 plunky
3100 1.1 plunky static void
3101 1.3 christos http_connection_retry_test(void *arg)
3102 1.1 plunky {
3103 1.3 christos struct basic_test_data *data = arg;
3104 1.3 christos ev_uint16_t port = 0;
3105 1.1 plunky struct evhttp_connection *evcon = NULL;
3106 1.1 plunky struct evhttp_request *req = NULL;
3107 1.3 christos struct timeval tv, tv_start, tv_end;
3108 1.1 plunky
3109 1.3 christos exit_base = data->base;
3110 1.1 plunky test_ok = 0;
3111 1.1 plunky
3112 1.3 christos /* auto detect a port */
3113 1.3 christos http = http_setup(&port, data->base);
3114 1.3 christos evhttp_free(http);
3115 1.3 christos http = NULL;
3116 1.3 christos
3117 1.3 christos evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
3118 1.3 christos tt_assert(evcon);
3119 1.3 christos
3120 1.3 christos evhttp_connection_set_timeout(evcon, 1);
3121 1.3 christos /* also bind to local host */
3122 1.3 christos evhttp_connection_set_local_address(evcon, "127.0.0.1");
3123 1.1 plunky
3124 1.3 christos /*
3125 1.3 christos * At this point, we want to schedule an HTTP GET request
3126 1.3 christos * server using our make request method.
3127 1.3 christos */
3128 1.1 plunky
3129 1.3 christos req = evhttp_request_new(http_connection_retry_done, data->base);
3130 1.3 christos tt_assert(req);
3131 1.1 plunky
3132 1.3 christos /* Add the information that we care about */
3133 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
3134 1.1 plunky
3135 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
3136 1.3 christos "/?arg=val") == -1) {
3137 1.3 christos tt_abort_msg("Couldn't make request");
3138 1.3 christos }
3139 1.1 plunky
3140 1.1 plunky evutil_gettimeofday(&tv_start, NULL);
3141 1.3 christos event_base_dispatch(data->base);
3142 1.3 christos evutil_gettimeofday(&tv_end, NULL);
3143 1.3 christos evutil_timersub(&tv_end, &tv_start, &tv_end);
3144 1.3 christos tt_int_op(tv_end.tv_sec, <, 1);
3145 1.3 christos
3146 1.3 christos tt_int_op(test_ok, ==, 1);
3147 1.3 christos
3148 1.3 christos /*
3149 1.3 christos * now test the same but with retries
3150 1.3 christos */
3151 1.3 christos test_ok = 0;
3152 1.3 christos
3153 1.3 christos evhttp_connection_set_timeout(evcon, 1);
3154 1.3 christos evhttp_connection_set_retries(evcon, 1);
3155 1.3 christos
3156 1.3 christos req = evhttp_request_new(http_connection_retry_done, data->base);
3157 1.3 christos tt_assert(req);
3158 1.1 plunky
3159 1.3 christos /* Add the information that we care about */
3160 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
3161 1.3 christos
3162 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
3163 1.3 christos "/?arg=val") == -1) {
3164 1.3 christos tt_abort_msg("Couldn't make request");
3165 1.3 christos }
3166 1.3 christos
3167 1.3 christos evutil_gettimeofday(&tv_start, NULL);
3168 1.3 christos event_base_dispatch(data->base);
3169 1.1 plunky evutil_gettimeofday(&tv_end, NULL);
3170 1.1 plunky evutil_timersub(&tv_end, &tv_start, &tv_end);
3171 1.3 christos tt_int_op(tv_end.tv_sec, >, 1);
3172 1.3 christos tt_int_op(tv_end.tv_sec, <, 6);
3173 1.3 christos
3174 1.3 christos tt_assert(test_ok == 1);
3175 1.3 christos
3176 1.3 christos /*
3177 1.3 christos * now test the same but with retries and give it a web server
3178 1.3 christos * at the end
3179 1.3 christos */
3180 1.3 christos test_ok = 0;
3181 1.3 christos
3182 1.3 christos evhttp_connection_set_timeout(evcon, 1);
3183 1.3 christos evhttp_connection_set_retries(evcon, 3);
3184 1.3 christos
3185 1.3 christos req = evhttp_request_new(http_dispatcher_test_done, data->base);
3186 1.3 christos tt_assert(req);
3187 1.1 plunky
3188 1.3 christos /* Add the information that we care about */
3189 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
3190 1.3 christos
3191 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
3192 1.3 christos "/?arg=val") == -1) {
3193 1.3 christos tt_abort_msg("Couldn't make request");
3194 1.1 plunky }
3195 1.1 plunky
3196 1.3 christos /* start up a web server one second after the connection tried
3197 1.3 christos * to send a request
3198 1.3 christos */
3199 1.3 christos evutil_timerclear(&tv);
3200 1.3 christos tv.tv_sec = 1;
3201 1.3 christos http_make_web_server_base = data->base;
3202 1.3 christos event_base_once(data->base, -1, EV_TIMEOUT, http_make_web_server, &port, &tv);
3203 1.3 christos
3204 1.3 christos evutil_gettimeofday(&tv_start, NULL);
3205 1.3 christos event_base_dispatch(data->base);
3206 1.3 christos evutil_gettimeofday(&tv_end, NULL);
3207 1.3 christos
3208 1.3 christos evutil_timersub(&tv_end, &tv_start, &tv_end);
3209 1.1 plunky
3210 1.3 christos tt_int_op(tv_end.tv_sec, >, 1);
3211 1.3 christos tt_int_op(tv_end.tv_sec, <, 6);
3212 1.1 plunky
3213 1.3 christos tt_int_op(test_ok, ==, 1);
3214 1.1 plunky
3215 1.3 christos end:
3216 1.3 christos if (evcon)
3217 1.3 christos evhttp_connection_free(evcon);
3218 1.3 christos if (http)
3219 1.3 christos evhttp_free(http);
3220 1.3 christos }
3221 1.1 plunky
3222 1.3 christos static void
3223 1.3 christos http_primitives(void *ptr)
3224 1.3 christos {
3225 1.3 christos char *escaped = NULL;
3226 1.3 christos struct evhttp *xhttp = NULL;
3227 1.1 plunky
3228 1.3 christos escaped = evhttp_htmlescape("<script>");
3229 1.3 christos tt_assert(escaped);
3230 1.3 christos tt_str_op(escaped, ==, "<script>");
3231 1.3 christos free(escaped);
3232 1.1 plunky
3233 1.3 christos escaped = evhttp_htmlescape("\"\'&");
3234 1.3 christos tt_assert(escaped);
3235 1.3 christos tt_str_op(escaped, ==, ""'&");
3236 1.1 plunky
3237 1.3 christos xhttp = evhttp_new(NULL);
3238 1.3 christos tt_assert(xhttp);
3239 1.3 christos tt_int_op(evhttp_set_cb(xhttp, "/test", http_basic_cb, NULL), ==, 0);
3240 1.3 christos tt_int_op(evhttp_set_cb(xhttp, "/test", http_basic_cb, NULL), ==, -1);
3241 1.3 christos tt_int_op(evhttp_del_cb(xhttp, "/test"), ==, 0);
3242 1.3 christos tt_int_op(evhttp_del_cb(xhttp, "/test"), ==, -1);
3243 1.3 christos tt_int_op(evhttp_set_cb(xhttp, "/test", http_basic_cb, NULL), ==, 0);
3244 1.1 plunky
3245 1.3 christos end:
3246 1.3 christos if (escaped)
3247 1.3 christos free(escaped);
3248 1.3 christos if (xhttp)
3249 1.3 christos evhttp_free(xhttp);
3250 1.1 plunky }
3251 1.1 plunky
3252 1.1 plunky static void
3253 1.3 christos http_multi_line_header_test(void *arg)
3254 1.1 plunky {
3255 1.3 christos struct basic_test_data *data = arg;
3256 1.3 christos struct bufferevent *bev= NULL;
3257 1.3 christos evutil_socket_t fd = -1;
3258 1.1 plunky const char *http_start_request;
3259 1.3 christos ev_uint16_t port = 0;
3260 1.3 christos
3261 1.1 plunky test_ok = 0;
3262 1.1 plunky
3263 1.3 christos http = http_setup(&port, data->base);
3264 1.3 christos
3265 1.1 plunky fd = http_connect("127.0.0.1", port);
3266 1.1 plunky
3267 1.1 plunky /* Stupid thing to send a request */
3268 1.3 christos bev = bufferevent_socket_new(data->base, fd, 0);
3269 1.3 christos bufferevent_setcb(bev, http_readcb, http_writecb,
3270 1.3 christos http_errorcb, data->base);
3271 1.1 plunky
3272 1.1 plunky http_start_request =
3273 1.1 plunky "GET /test HTTP/1.1\r\n"
3274 1.1 plunky "Host: somehost\r\n"
3275 1.1 plunky "Connection: close\r\n"
3276 1.1 plunky "X-Multi: aaaaaaaa\r\n"
3277 1.1 plunky " a\r\n"
3278 1.1 plunky "\tEND\r\n"
3279 1.1 plunky "X-Last: last\r\n"
3280 1.1 plunky "\r\n";
3281 1.3 christos
3282 1.1 plunky bufferevent_write(bev, http_start_request, strlen(http_start_request));
3283 1.1 plunky
3284 1.3 christos event_base_dispatch(data->base);
3285 1.1 plunky
3286 1.3 christos tt_int_op(test_ok, ==, 4);
3287 1.3 christos end:
3288 1.3 christos if (bev)
3289 1.3 christos bufferevent_free(bev);
3290 1.3 christos if (fd >= 0)
3291 1.3 christos evutil_closesocket(fd);
3292 1.3 christos if (http)
3293 1.3 christos evhttp_free(http);
3294 1.1 plunky }
3295 1.1 plunky
3296 1.1 plunky static void
3297 1.1 plunky http_request_bad(struct evhttp_request *req, void *arg)
3298 1.1 plunky {
3299 1.1 plunky if (req != NULL) {
3300 1.1 plunky fprintf(stderr, "FAILED\n");
3301 1.1 plunky exit(1);
3302 1.1 plunky }
3303 1.1 plunky
3304 1.1 plunky test_ok = 1;
3305 1.3 christos event_base_loopexit(arg, NULL);
3306 1.1 plunky }
3307 1.1 plunky
3308 1.1 plunky static void
3309 1.3 christos http_negative_content_length_test(void *arg)
3310 1.1 plunky {
3311 1.3 christos struct basic_test_data *data = arg;
3312 1.3 christos ev_uint16_t port = 0;
3313 1.1 plunky struct evhttp_connection *evcon = NULL;
3314 1.1 plunky struct evhttp_request *req = NULL;
3315 1.3 christos
3316 1.1 plunky test_ok = 0;
3317 1.1 plunky
3318 1.3 christos http = http_setup(&port, data->base);
3319 1.1 plunky
3320 1.3 christos evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
3321 1.3 christos tt_assert(evcon);
3322 1.1 plunky
3323 1.1 plunky /*
3324 1.1 plunky * At this point, we want to schedule a request to the HTTP
3325 1.1 plunky * server using our make request method.
3326 1.1 plunky */
3327 1.1 plunky
3328 1.3 christos req = evhttp_request_new(http_request_bad, data->base);
3329 1.1 plunky
3330 1.1 plunky /* Cause the response to have a negative content-length */
3331 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "X-Negative", "makeitso");
3332 1.1 plunky
3333 1.1 plunky /* We give ownership of the request to the connection */
3334 1.1 plunky if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
3335 1.3 christos tt_abort_msg("Couldn't make request");
3336 1.3 christos }
3337 1.3 christos
3338 1.3 christos event_base_dispatch(data->base);
3339 1.3 christos
3340 1.3 christos end:
3341 1.3 christos if (evcon)
3342 1.3 christos evhttp_connection_free(evcon);
3343 1.3 christos if (http)
3344 1.3 christos evhttp_free(http);
3345 1.3 christos }
3346 1.3 christos
3347 1.3 christos
3348 1.3 christos static void
3349 1.3 christos http_data_length_constraints_test_done(struct evhttp_request *req, void *arg)
3350 1.3 christos {
3351 1.3 christos tt_assert(req);
3352 1.3 christos tt_int_op(evhttp_request_get_response_code(req), ==, HTTP_BADREQUEST);
3353 1.3 christos end:
3354 1.3 christos event_base_loopexit(arg, NULL);
3355 1.3 christos }
3356 1.3 christos
3357 1.3 christos static void
3358 1.3 christos http_large_entity_test_done(struct evhttp_request *req, void *arg)
3359 1.3 christos {
3360 1.3 christos tt_assert(req);
3361 1.3 christos tt_int_op(evhttp_request_get_response_code(req), ==, HTTP_ENTITYTOOLARGE);
3362 1.3 christos end:
3363 1.3 christos event_base_loopexit(arg, NULL);
3364 1.3 christos }
3365 1.3 christos
3366 1.3 christos static void
3367 1.3 christos http_data_length_constraints_test(void *arg)
3368 1.3 christos {
3369 1.3 christos struct basic_test_data *data = arg;
3370 1.3 christos ev_uint16_t port = 0;
3371 1.3 christos struct evhttp_connection *evcon = NULL;
3372 1.3 christos struct evhttp_request *req = NULL;
3373 1.3 christos char long_str[8192];
3374 1.3 christos
3375 1.3 christos test_ok = 0;
3376 1.3 christos
3377 1.3 christos http = http_setup(&port, data->base);
3378 1.3 christos
3379 1.3 christos evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
3380 1.3 christos tt_assert(evcon);
3381 1.3 christos
3382 1.3 christos /* also bind to local host */
3383 1.3 christos evhttp_connection_set_local_address(evcon, "127.0.0.1");
3384 1.3 christos
3385 1.3 christos /*
3386 1.3 christos * At this point, we want to schedule an HTTP GET request
3387 1.3 christos * server using our make request method.
3388 1.3 christos */
3389 1.3 christos
3390 1.3 christos req = evhttp_request_new(http_data_length_constraints_test_done, data->base);
3391 1.3 christos tt_assert(req);
3392 1.3 christos
3393 1.3 christos memset(long_str, 'a', 8192);
3394 1.3 christos long_str[8191] = '\0';
3395 1.3 christos /* Add the information that we care about */
3396 1.3 christos evhttp_set_max_headers_size(http, 8191);
3397 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
3398 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Longheader", long_str);
3399 1.3 christos
3400 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/?arg=val") == -1) {
3401 1.3 christos tt_abort_msg("Couldn't make request");
3402 1.3 christos }
3403 1.3 christos event_base_dispatch(data->base);
3404 1.3 christos
3405 1.3 christos req = evhttp_request_new(http_data_length_constraints_test_done, data->base);
3406 1.3 christos tt_assert(req);
3407 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
3408 1.3 christos
3409 1.3 christos /* GET /?arg=verylongvalue HTTP/1.1 */
3410 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, long_str) == -1) {
3411 1.3 christos tt_abort_msg("Couldn't make request");
3412 1.3 christos }
3413 1.3 christos event_base_dispatch(data->base);
3414 1.3 christos
3415 1.3 christos evhttp_set_max_body_size(http, 8190);
3416 1.3 christos req = evhttp_request_new(http_data_length_constraints_test_done, data->base);
3417 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
3418 1.3 christos evbuffer_add_printf(evhttp_request_get_output_buffer(req), "%s", long_str);
3419 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_POST, "/") == -1) {
3420 1.3 christos tt_abort_msg("Couldn't make request");
3421 1.3 christos }
3422 1.3 christos event_base_dispatch(data->base);
3423 1.3 christos
3424 1.3 christos req = evhttp_request_new(http_large_entity_test_done, data->base);
3425 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
3426 1.3 christos evhttp_add_header(evhttp_request_get_output_headers(req), "Expect", "100-continue");
3427 1.3 christos evbuffer_add_printf(evhttp_request_get_output_buffer(req), "%s", long_str);
3428 1.3 christos if (evhttp_make_request(evcon, req, EVHTTP_REQ_POST, "/") == -1) {
3429 1.3 christos tt_abort_msg("Couldn't make request");
3430 1.1 plunky }
3431 1.3 christos event_base_dispatch(data->base);
3432 1.3 christos
3433 1.3 christos test_ok = 1;
3434 1.3 christos end:
3435 1.3 christos if (evcon)
3436 1.3 christos evhttp_connection_free(evcon);
3437 1.3 christos if (http)
3438 1.3 christos evhttp_free(http);
3439 1.3 christos }
3440 1.3 christos
3441 1.3 christos /*
3442 1.3 christos * Testing client reset of server chunked connections
3443 1.3 christos */
3444 1.1 plunky
3445 1.3 christos struct terminate_state {
3446 1.3 christos struct event_base *base;
3447 1.3 christos struct evhttp_request *req;
3448 1.3 christos struct bufferevent *bev;
3449 1.3 christos evutil_socket_t fd;
3450 1.3 christos int gotclosecb: 1;
3451 1.3 christos };
3452 1.1 plunky
3453 1.3 christos static void
3454 1.3 christos terminate_chunked_trickle_cb(evutil_socket_t fd, short events, void *arg)
3455 1.3 christos {
3456 1.3 christos struct terminate_state *state = arg;
3457 1.3 christos struct evbuffer *evb;
3458 1.3 christos struct timeval tv;
3459 1.1 plunky
3460 1.3 christos if (evhttp_request_get_connection(state->req) == NULL) {
3461 1.3 christos test_ok = 1;
3462 1.3 christos evhttp_request_free(state->req);
3463 1.3 christos event_base_loopexit(state->base,NULL);
3464 1.3 christos return;
3465 1.1 plunky }
3466 1.1 plunky
3467 1.3 christos evb = evbuffer_new();
3468 1.3 christos evbuffer_add_printf(evb, "%p", evb);
3469 1.3 christos evhttp_send_reply_chunk(state->req, evb);
3470 1.3 christos evbuffer_free(evb);
3471 1.3 christos
3472 1.3 christos tv.tv_sec = 0;
3473 1.3 christos tv.tv_usec = 3000;
3474 1.3 christos EVUTIL_ASSERT(state);
3475 1.3 christos EVUTIL_ASSERT(state->base);
3476 1.3 christos event_base_once(state->base, -1, EV_TIMEOUT, terminate_chunked_trickle_cb, arg, &tv);
3477 1.3 christos }
3478 1.3 christos
3479 1.3 christos static void
3480 1.3 christos terminate_chunked_close_cb(struct evhttp_connection *evcon, void *arg)
3481 1.3 christos {
3482 1.3 christos struct terminate_state *state = arg;
3483 1.3 christos state->gotclosecb = 1;
3484 1.1 plunky }
3485 1.1 plunky
3486 1.3 christos static void
3487 1.3 christos terminate_chunked_cb(struct evhttp_request *req, void *arg)
3488 1.1 plunky {
3489 1.3 christos struct terminate_state *state = arg;
3490 1.3 christos struct timeval tv;
3491 1.3 christos
3492 1.3 christos /* we want to know if this connection closes on us */
3493 1.3 christos evhttp_connection_set_closecb(
3494 1.3 christos evhttp_request_get_connection(req),
3495 1.3 christos terminate_chunked_close_cb, arg);
3496 1.3 christos
3497 1.3 christos state->req = req;
3498 1.3 christos
3499 1.3 christos evhttp_send_reply_start(req, HTTP_OK, "OK");
3500 1.3 christos
3501 1.3 christos tv.tv_sec = 0;
3502 1.3 christos tv.tv_usec = 3000;
3503 1.3 christos event_base_once(state->base, -1, EV_TIMEOUT, terminate_chunked_trickle_cb, arg, &tv);
3504 1.3 christos }
3505 1.1 plunky
3506 1.3 christos static void
3507 1.3 christos terminate_chunked_client(evutil_socket_t fd, short event, void *arg)
3508 1.3 christos {
3509 1.3 christos struct terminate_state *state = arg;
3510 1.3 christos bufferevent_free(state->bev);
3511 1.3 christos evutil_closesocket(state->fd);
3512 1.3 christos }
3513 1.1 plunky
3514 1.3 christos static void
3515 1.3 christos terminate_readcb(struct bufferevent *bev, void *arg)
3516 1.3 christos {
3517 1.3 christos /* just drop the data */
3518 1.3 christos evbuffer_drain(bufferevent_get_input(bev), -1);
3519 1.1 plunky }
3520 1.3 christos
3521 1.3 christos
3522 1.3 christos static void
3523 1.3 christos http_terminate_chunked_test(void *arg)
3524 1.3 christos {
3525 1.3 christos struct basic_test_data *data = arg;
3526 1.3 christos struct bufferevent *bev = NULL;
3527 1.3 christos struct timeval tv;
3528 1.3 christos const char *http_request;
3529 1.3 christos ev_uint16_t port = 0;
3530 1.3 christos evutil_socket_t fd = -1;
3531 1.3 christos struct terminate_state terminate_state;
3532 1.3 christos
3533 1.3 christos test_ok = 0;
3534 1.3 christos
3535 1.3 christos http = http_setup(&port, data->base);
3536 1.3 christos evhttp_del_cb(http, "/test");
3537 1.3 christos tt_assert(evhttp_set_cb(http, "/test",
3538 1.3 christos terminate_chunked_cb, &terminate_state) == 0);
3539 1.3 christos
3540 1.3 christos fd = http_connect("127.0.0.1", port);
3541 1.3 christos
3542 1.3 christos /* Stupid thing to send a request */
3543 1.3 christos bev = bufferevent_socket_new(data->base, fd, 0);
3544 1.3 christos bufferevent_setcb(bev, terminate_readcb, http_writecb,
3545 1.3 christos http_errorcb, data->base);
3546 1.3 christos
3547 1.3 christos memset(&terminate_state, 0, sizeof(terminate_state));
3548 1.3 christos terminate_state.base = data->base;
3549 1.3 christos terminate_state.fd = fd;
3550 1.3 christos terminate_state.bev = bev;
3551 1.3 christos terminate_state.gotclosecb = 0;
3552 1.3 christos
3553 1.3 christos /* first half of the http request */
3554 1.3 christos http_request =
3555 1.3 christos "GET /test HTTP/1.1\r\n"
3556 1.3 christos "Host: some\r\n\r\n";
3557 1.3 christos
3558 1.3 christos bufferevent_write(bev, http_request, strlen(http_request));
3559 1.3 christos evutil_timerclear(&tv);
3560 1.3 christos tv.tv_usec = 10000;
3561 1.3 christos event_base_once(data->base, -1, EV_TIMEOUT, terminate_chunked_client, &terminate_state,
3562 1.3 christos &tv);
3563 1.3 christos
3564 1.3 christos event_base_dispatch(data->base);
3565 1.3 christos
3566 1.3 christos if (terminate_state.gotclosecb == 0)
3567 1.3 christos test_ok = 0;
3568 1.3 christos
3569 1.3 christos end:
3570 1.3 christos if (fd >= 0)
3571 1.3 christos evutil_closesocket(fd);
3572 1.3 christos if (http)
3573 1.3 christos evhttp_free(http);
3574 1.3 christos }
3575 1.3 christos
3576 1.3 christos #define HTTP_LEGACY(name) \
3577 1.3 christos { #name, run_legacy_test_fn, TT_ISOLATED|TT_LEGACY, &legacy_setup, \
3578 1.3 christos http_##name##_test }
3579 1.3 christos
3580 1.3 christos #define HTTP(name) \
3581 1.3 christos { #name, http_##name##_test, TT_ISOLATED, &basic_setup, NULL }
3582 1.3 christos
3583 1.3 christos struct testcase_t http_testcases[] = {
3584 1.3 christos { "primitives", http_primitives, 0, NULL, NULL },
3585 1.3 christos { "base", http_base_test, TT_FORK, NULL, NULL },
3586 1.3 christos { "bad_headers", http_bad_header_test, 0, NULL, NULL },
3587 1.3 christos { "parse_query", http_parse_query_test, 0, NULL, NULL },
3588 1.3 christos { "parse_uri", http_parse_uri_test, 0, NULL, NULL },
3589 1.3 christos { "parse_uri_nc", http_parse_uri_test, 0, &basic_setup, __UNCONST("nc") },
3590 1.3 christos { "uriencode", http_uriencode_test, 0, NULL, NULL },
3591 1.3 christos HTTP(basic),
3592 1.3 christos HTTP(cancel),
3593 1.3 christos HTTP(virtual_host),
3594 1.3 christos HTTP(post),
3595 1.3 christos HTTP(put),
3596 1.3 christos HTTP(delete),
3597 1.3 christos HTTP(allowed_methods),
3598 1.3 christos HTTP(failure),
3599 1.3 christos HTTP(connection),
3600 1.3 christos HTTP(persist_connection),
3601 1.3 christos HTTP(connection_async),
3602 1.3 christos HTTP(close_detection),
3603 1.3 christos HTTP(close_detection_delay),
3604 1.3 christos HTTP(bad_request),
3605 1.3 christos HTTP(incomplete),
3606 1.3 christos HTTP(incomplete_timeout),
3607 1.3 christos HTTP(terminate_chunked),
3608 1.3 christos
3609 1.3 christos HTTP(highport),
3610 1.3 christos HTTP(dispatcher),
3611 1.3 christos HTTP(multi_line_header),
3612 1.3 christos HTTP(negative_content_length),
3613 1.3 christos HTTP(chunk_out),
3614 1.3 christos HTTP(stream_out),
3615 1.3 christos
3616 1.3 christos HTTP(stream_in),
3617 1.3 christos HTTP(stream_in_cancel),
3618 1.3 christos
3619 1.3 christos HTTP(connection_fail),
3620 1.3 christos HTTP(connection_retry),
3621 1.3 christos HTTP(data_length_constraints),
3622 1.3 christos
3623 1.3 christos END_OF_TESTCASES
3624 1.3 christos };
3625 1.3 christos
3626