/
regress_http.c
3611 lines (2879 loc) · 94.4 KB
/
regress_http.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
* Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#endif
#include "event2/event-config.h"
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _EVENT_HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/queue.h>
#ifndef WIN32
#include <sys/socket.h>
#include <signal.h>
#include <unistd.h>
#include <netdb.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "event2/dns.h"
#include "event2/event.h"
#include "event2/http.h"
#include "event2/buffer.h"
#include "event2/bufferevent.h"
#include "event2/util.h"
#include "log-internal.h"
#include "util-internal.h"
#include "http-internal.h"
#include "regress.h"
#include "regress_testutils.h"
static struct evhttp *http;
/* set if a test needs to call loopexit on a base */
static struct event_base *exit_base;
static char const BASIC_REQUEST_BODY[] = "This is funny";
static void http_basic_cb(struct evhttp_request *req, void *arg);
static void http_chunked_cb(struct evhttp_request *req, void *arg);
static void http_post_cb(struct evhttp_request *req, void *arg);
static void http_put_cb(struct evhttp_request *req, void *arg);
static void http_delete_cb(struct evhttp_request *req, void *arg);
static void http_delay_cb(struct evhttp_request *req, void *arg);
static void http_large_delay_cb(struct evhttp_request *req, void *arg);
static void http_badreq_cb(struct evhttp_request *req, void *arg);
static void http_dispatcher_cb(struct evhttp_request *req, void *arg);
static int
http_bind(struct evhttp *myhttp, ev_uint16_t *pport)
{
int port;
struct evhttp_bound_socket *sock;
sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", *pport);
if (sock == NULL)
event_errx(1, "Could not start web server");
port = regress_get_socket_port(evhttp_bound_socket_get_fd(sock));
if (port < 0)
return -1;
*pport = (ev_uint16_t) port;
return 0;
}
static struct evhttp *
http_setup(ev_uint16_t *pport, struct event_base *base)
{
struct evhttp *myhttp;
/* Try a few different ports */
myhttp = evhttp_new(base);
if (http_bind(myhttp, pport) < 0)
return NULL;
/* Register a callback for certain types of requests */
evhttp_set_cb(myhttp, "/test", http_basic_cb, base);
evhttp_set_cb(myhttp, "/chunked", http_chunked_cb, base);
evhttp_set_cb(myhttp, "/streamed", http_chunked_cb, base);
evhttp_set_cb(myhttp, "/postit", http_post_cb, base);
evhttp_set_cb(myhttp, "/putit", http_put_cb, base);
evhttp_set_cb(myhttp, "/deleteit", http_delete_cb, base);
evhttp_set_cb(myhttp, "/delay", http_delay_cb, base);
evhttp_set_cb(myhttp, "/largedelay", http_large_delay_cb, base);
evhttp_set_cb(myhttp, "/badrequest", http_badreq_cb, base);
evhttp_set_cb(myhttp, "/", http_dispatcher_cb, base);
return (myhttp);
}
#ifndef NI_MAXSERV
#define NI_MAXSERV 1024
#endif
static evutil_socket_t
http_connect(const char *address, u_short port)
{
/* Stupid code for connecting */
struct evutil_addrinfo ai, *aitop;
char strport[NI_MAXSERV];
struct sockaddr *sa;
int slen;
evutil_socket_t fd;
memset(&ai, 0, sizeof(ai));
ai.ai_family = AF_INET;
ai.ai_socktype = SOCK_STREAM;
evutil_snprintf(strport, sizeof(strport), "%d", port);
if (evutil_getaddrinfo(address, strport, &ai, &aitop) != 0) {
event_warn("getaddrinfo");
return (-1);
}
sa = aitop->ai_addr;
slen = aitop->ai_addrlen;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1)
event_err(1, "socket failed");
evutil_make_socket_nonblocking(fd);
if (connect(fd, sa, slen) == -1) {
#ifdef WIN32
int tmp_err = WSAGetLastError();
if (tmp_err != WSAEINPROGRESS && tmp_err != WSAEINVAL &&
tmp_err != WSAEWOULDBLOCK)
event_err(1, "connect failed");
#else
if (errno != EINPROGRESS)
event_err(1, "connect failed");
#endif
}
evutil_freeaddrinfo(aitop);
return (fd);
}
/* Helper: do a strcmp on the contents of buf and the string s. */
static int
evbuffer_datacmp(struct evbuffer *buf, const char *s)
{
size_t b_sz = evbuffer_get_length(buf);
size_t s_sz = strlen(s);
unsigned char *d;
int r;
if (b_sz < s_sz)
return -1;
d = evbuffer_pullup(buf, s_sz);
if ((r = memcmp(d, s, s_sz)))
return r;
if (b_sz > s_sz)
return 1;
else
return 0;
}
/* Helper: Return true iff buf contains s */
static int
evbuffer_contains(struct evbuffer *buf, const char *s)
{
struct evbuffer_ptr ptr;
ptr = evbuffer_search(buf, s, strlen(s), NULL);
return ptr.pos != -1;
}
static void
http_readcb(struct bufferevent *bev, void *arg)
{
const char *what = BASIC_REQUEST_BODY;
struct event_base *my_base = arg;
if (evbuffer_contains(bufferevent_get_input(bev), what)) {
struct evhttp_request *req = evhttp_request_new(NULL, NULL);
enum message_read_status done;
/* req->kind = EVHTTP_RESPONSE; */
done = evhttp_parse_firstline(req, bufferevent_get_input(bev));
if (done != ALL_DATA_READ)
goto out;
done = evhttp_parse_headers(req, bufferevent_get_input(bev));
if (done != ALL_DATA_READ)
goto out;
if (done == 1 &&
evhttp_find_header(evhttp_request_get_input_headers(req),
"Content-Type") != NULL)
test_ok++;
out:
evhttp_request_free(req);
bufferevent_disable(bev, EV_READ);
if (exit_base)
event_base_loopexit(exit_base, NULL);
else if (my_base)
event_base_loopexit(my_base, NULL);
else {
fprintf(stderr, "No way to exit loop!\n");
exit(1);
}
}
}
static void
http_writecb(struct bufferevent *bev, void *arg)
{
if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
/* enable reading of the reply */
bufferevent_enable(bev, EV_READ);
test_ok++;
}
}
static void
http_errorcb(struct bufferevent *bev, short what, void *arg)
{
test_ok = -2;
event_base_loopexit(arg, NULL);
}
static void
http_basic_cb(struct evhttp_request *req, void *arg)
{
struct evbuffer *evb = evbuffer_new();
int empty = evhttp_find_header(evhttp_request_get_input_headers(req), "Empty") != NULL;
event_debug(("%s: called\n", __func__));
evbuffer_add_printf(evb, BASIC_REQUEST_BODY);
/* For multi-line headers test */
{
const char *multi =
evhttp_find_header(evhttp_request_get_input_headers(req),"X-multi");
if (multi) {
if (strcmp("END", multi + strlen(multi) - 3) == 0)
test_ok++;
if (evhttp_find_header(evhttp_request_get_input_headers(req), "X-Last"))
test_ok++;
}
}
/* injecting a bad content-length */
if (evhttp_find_header(evhttp_request_get_input_headers(req), "X-Negative"))
evhttp_add_header(evhttp_request_get_output_headers(req),
"Content-Length", "-100");
/* allow sending of an empty reply */
evhttp_send_reply(req, HTTP_OK, "Everything is fine",
!empty ? evb : NULL);
evbuffer_free(evb);
}
static char const* const CHUNKS[] = {
"This is funny",
"but not hilarious.",
"bwv 1052"
};
struct chunk_req_state {
struct event_base *base;
struct evhttp_request *req;
int i;
};
static void
http_chunked_trickle_cb(evutil_socket_t fd, short events, void *arg)
{
struct evbuffer *evb = evbuffer_new();
struct chunk_req_state *state = arg;
struct timeval when = { 0, 0 };
evbuffer_add_printf(evb, "%s", CHUNKS[state->i]);
evhttp_send_reply_chunk(state->req, evb);
evbuffer_free(evb);
if (++state->i < (int) (sizeof(CHUNKS)/sizeof(CHUNKS[0]))) {
event_base_once(state->base, -1, EV_TIMEOUT,
http_chunked_trickle_cb, state, &when);
} else {
evhttp_send_reply_end(state->req);
free(state);
}
}
static void
http_chunked_cb(struct evhttp_request *req, void *arg)
{
struct timeval when = { 0, 0 };
struct chunk_req_state *state = malloc(sizeof(struct chunk_req_state));
event_debug(("%s: called\n", __func__));
memset(state, 0, sizeof(struct chunk_req_state));
state->req = req;
state->base = arg;
if (strcmp(evhttp_request_get_uri(req), "/streamed") == 0) {
evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Length", "39");
}
/* generate a chunked/streamed reply */
evhttp_send_reply_start(req, HTTP_OK, "Everything is fine");
/* but trickle it across several iterations to ensure we're not
* assuming it comes all at once */
event_base_once(arg, -1, EV_TIMEOUT, http_chunked_trickle_cb, state, &when);
}
static void
http_complete_write(evutil_socket_t fd, short what, void *arg)
{
struct bufferevent *bev = arg;
const char *http_request = "host\r\n"
"Connection: close\r\n"
"\r\n";
bufferevent_write(bev, http_request, strlen(http_request));
}
static void
http_basic_test(void *arg)
{
struct basic_test_data *data = arg;
struct timeval tv;
struct bufferevent *bev;
evutil_socket_t fd;
const char *http_request;
ev_uint16_t port = 0, port2 = 0;
test_ok = 0;
http = http_setup(&port, data->base);
/* bind to a second socket */
if (http_bind(http, &port2) == -1) {
fprintf(stdout, "FAILED (bind)\n");
exit(1);
}
fd = http_connect("127.0.0.1", port);
/* Stupid thing to send a request */
bev = bufferevent_socket_new(data->base, fd, 0);
bufferevent_setcb(bev, http_readcb, http_writecb,
http_errorcb, data->base);
/* first half of the http request */
http_request =
"GET /test HTTP/1.1\r\n"
"Host: some";
bufferevent_write(bev, http_request, strlen(http_request));
evutil_timerclear(&tv);
tv.tv_usec = 10000;
event_base_once(data->base,
-1, EV_TIMEOUT, http_complete_write, bev, &tv);
event_base_dispatch(data->base);
tt_assert(test_ok == 3);
/* connect to the second port */
bufferevent_free(bev);
evutil_closesocket(fd);
fd = http_connect("127.0.0.1", port2);
/* Stupid thing to send a request */
bev = bufferevent_socket_new(data->base, fd, 0);
bufferevent_setcb(bev, http_readcb, http_writecb,
http_errorcb, data->base);
http_request =
"GET /test HTTP/1.1\r\n"
"Host: somehost\r\n"
"Connection: close\r\n"
"\r\n";
bufferevent_write(bev, http_request, strlen(http_request));
event_base_dispatch(data->base);
tt_assert(test_ok == 5);
/* Connect to the second port again. This time, send an absolute uri. */
bufferevent_free(bev);
evutil_closesocket(fd);
fd = http_connect("127.0.0.1", port2);
/* Stupid thing to send a request */
bev = bufferevent_socket_new(data->base, fd, 0);
bufferevent_setcb(bev, http_readcb, http_writecb,
http_errorcb, data->base);
http_request =
"GET http://somehost.net/test HTTP/1.1\r\n"
"Host: somehost\r\n"
"Connection: close\r\n"
"\r\n";
bufferevent_write(bev, http_request, strlen(http_request));
event_base_dispatch(data->base);
tt_assert(test_ok == 7);
evhttp_free(http);
end:
;
}
static void
http_delay_reply(evutil_socket_t fd, short what, void *arg)
{
struct evhttp_request *req = arg;
evhttp_send_reply(req, HTTP_OK, "Everything is fine", NULL);
++test_ok;
}
static void
http_delay_cb(struct evhttp_request *req, void *arg)
{
struct timeval tv;
evutil_timerclear(&tv);
tv.tv_sec = 0;
tv.tv_usec = 200 * 1000;
event_base_once(arg, -1, EV_TIMEOUT, http_delay_reply, req, &tv);
}
static void
http_badreq_cb(struct evhttp_request *req, void *arg)
{
struct evbuffer *buf = evbuffer_new();
evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", "text/xml; charset=UTF-8");
evbuffer_add_printf(buf, "Hello, %s!", "127.0.0.1");
evhttp_send_reply(req, HTTP_OK, "OK", buf);
evbuffer_free(buf);
}
static void
http_badreq_errorcb(struct bufferevent *bev, short what, void *arg)
{
event_debug(("%s: called (what=%04x, arg=%p)", __func__, what, arg));
/* ignore */
}
#ifndef SHUT_WR
#ifdef WIN32
#define SHUT_WR SD_SEND
#else
#define SHUT_WR 1
#endif
#endif
static void
http_badreq_readcb(struct bufferevent *bev, void *arg)
{
const char *what = "Hello, 127.0.0.1";
const char *bad_request = "400 Bad Request";
if (evbuffer_contains(bufferevent_get_input(bev), bad_request)) {
TT_FAIL(("%s:bad request detected", __func__));
bufferevent_disable(bev, EV_READ);
event_base_loopexit(arg, NULL);
return;
}
if (evbuffer_contains(bufferevent_get_input(bev), what)) {
struct evhttp_request *req = evhttp_request_new(NULL, NULL);
enum message_read_status done;
/* req->kind = EVHTTP_RESPONSE; */
done = evhttp_parse_firstline(req, bufferevent_get_input(bev));
if (done != ALL_DATA_READ)
goto out;
done = evhttp_parse_headers(req, bufferevent_get_input(bev));
if (done != ALL_DATA_READ)
goto out;
if (done == 1 &&
evhttp_find_header(evhttp_request_get_input_headers(req),
"Content-Type") != NULL)
test_ok++;
out:
evhttp_request_free(req);
evbuffer_drain(bufferevent_get_input(bev), evbuffer_get_length(bufferevent_get_input(bev)));
}
shutdown(bufferevent_getfd(bev), SHUT_WR);
}
static void
http_badreq_successcb(evutil_socket_t fd, short what, void *arg)
{
event_debug(("%s: called (what=%04x, arg=%p)", __func__, what, arg));
event_base_loopexit(exit_base, NULL);
}
static void
http_bad_request_test(void *arg)
{
struct basic_test_data *data = arg;
struct timeval tv;
struct bufferevent *bev = NULL;
evutil_socket_t fd;
const char *http_request;
ev_uint16_t port=0, port2=0;
test_ok = 0;
exit_base = data->base;
http = http_setup(&port, data->base);
/* bind to a second socket */
if (http_bind(http, &port2) == -1)
TT_DIE(("Bind socket failed"));
/* NULL request test */
fd = http_connect("127.0.0.1", port);
/* Stupid thing to send a request */
bev = bufferevent_socket_new(data->base, fd, 0);
bufferevent_setcb(bev, http_badreq_readcb, http_writecb,
http_badreq_errorcb, data->base);
bufferevent_enable(bev, EV_READ);
/* real NULL request */
http_request = "";
bufferevent_write(bev, http_request, strlen(http_request));
shutdown(fd, SHUT_WR);
timerclear(&tv);
tv.tv_usec = 10000;
event_base_once(data->base, -1, EV_TIMEOUT, http_badreq_successcb, bev, &tv);
event_base_dispatch(data->base);
bufferevent_free(bev);
evutil_closesocket(fd);
if (test_ok != 0) {
fprintf(stdout, "FAILED\n");
exit(1);
}
/* Second answer (BAD REQUEST) on connection close */
/* connect to the second port */
fd = http_connect("127.0.0.1", port2);
/* Stupid thing to send a request */
bev = bufferevent_socket_new(data->base, fd, 0);
bufferevent_setcb(bev, http_badreq_readcb, http_writecb,
http_badreq_errorcb, data->base);
bufferevent_enable(bev, EV_READ);
/* first half of the http request */
http_request =
"GET /badrequest HTTP/1.0\r\n" \
"Connection: Keep-Alive\r\n" \
"\r\n";
bufferevent_write(bev, http_request, strlen(http_request));
timerclear(&tv);
tv.tv_usec = 10000;
event_base_once(data->base, -1, EV_TIMEOUT, http_badreq_successcb, bev, &tv);
event_base_dispatch(data->base);
tt_int_op(test_ok, ==, 2);
end:
evhttp_free(http);
if (bev)
bufferevent_free(bev);
}
static struct evhttp_connection *delayed_client;
static void
http_large_delay_cb(struct evhttp_request *req, void *arg)
{
struct timeval tv;
evutil_timerclear(&tv);
tv.tv_sec = 3;
event_base_once(arg, -1, EV_TIMEOUT, http_delay_reply, req, &tv);
evhttp_connection_fail(delayed_client, EVCON_HTTP_EOF);
}
/*
* HTTP DELETE test, just piggyback on the basic test
*/
static void
http_delete_cb(struct evhttp_request *req, void *arg)
{
struct evbuffer *evb = evbuffer_new();
int empty = evhttp_find_header(evhttp_request_get_input_headers(req), "Empty") != NULL;
/* Expecting a DELETE request */
if (evhttp_request_get_command(req) != EVHTTP_REQ_DELETE) {
fprintf(stdout, "FAILED (delete type)\n");
exit(1);
}
event_debug(("%s: called\n", __func__));
evbuffer_add_printf(evb, BASIC_REQUEST_BODY);
/* allow sending of an empty reply */
evhttp_send_reply(req, HTTP_OK, "Everything is fine",
!empty ? evb : NULL);
evbuffer_free(evb);
}
static void
http_delete_test(void *arg)
{
struct basic_test_data *data = arg;
struct bufferevent *bev;
evutil_socket_t fd;
const char *http_request;
ev_uint16_t port = 0;
test_ok = 0;
http = http_setup(&port, data->base);
fd = http_connect("127.0.0.1", port);
/* Stupid thing to send a request */
bev = bufferevent_socket_new(data->base, fd, 0);
bufferevent_setcb(bev, http_readcb, http_writecb,
http_errorcb, data->base);
http_request =
"DELETE /deleteit HTTP/1.1\r\n"
"Host: somehost\r\n"
"Connection: close\r\n"
"\r\n";
bufferevent_write(bev, http_request, strlen(http_request));
event_base_dispatch(data->base);
bufferevent_free(bev);
evutil_closesocket(fd);
evhttp_free(http);
tt_int_op(test_ok, ==, 2);
end:
;
}
static void
http_allowed_methods_eventcb(struct bufferevent *bev, short what, void *arg)
{
char **output = arg;
if ((what & (BEV_EVENT_ERROR|BEV_EVENT_EOF))) {
char buf[4096];
int n;
n = evbuffer_remove(bufferevent_get_input(bev), buf,
sizeof(buf)-1);
if (n >= 0) {
buf[n]='\0';
if (*output)
free(*output);
*output = strdup(buf);
}
event_base_loopexit(exit_base, NULL);
}
}
static void
http_allowed_methods_test(void *arg)
{
struct basic_test_data *data = arg;
struct bufferevent *bev1, *bev2, *bev3;
evutil_socket_t fd1, fd2, fd3;
const char *http_request;
char *result1=NULL, *result2=NULL, *result3=NULL;
ev_uint16_t port = 0;
exit_base = data->base;
test_ok = 0;
http = http_setup(&port, data->base);
fd1 = http_connect("127.0.0.1", port);
/* GET is out; PATCH is in. */
evhttp_set_allowed_methods(http, EVHTTP_REQ_PATCH);
/* Stupid thing to send a request */
bev1 = bufferevent_socket_new(data->base, fd1, 0);
bufferevent_enable(bev1, EV_READ|EV_WRITE);
bufferevent_setcb(bev1, NULL, NULL,
http_allowed_methods_eventcb, &result1);
http_request =
"GET /index.html HTTP/1.1\r\n"
"Host: somehost\r\n"
"Connection: close\r\n"
"\r\n";
bufferevent_write(bev1, http_request, strlen(http_request));
event_base_dispatch(data->base);
fd2 = http_connect("127.0.0.1", port);
bev2 = bufferevent_socket_new(data->base, fd2, 0);
bufferevent_enable(bev2, EV_READ|EV_WRITE);
bufferevent_setcb(bev2, NULL, NULL,
http_allowed_methods_eventcb, &result2);
http_request =
"PATCH /test HTTP/1.1\r\n"
"Host: somehost\r\n"
"Connection: close\r\n"
"\r\n";
bufferevent_write(bev2, http_request, strlen(http_request));
event_base_dispatch(data->base);
fd3 = http_connect("127.0.0.1", port);
bev3 = bufferevent_socket_new(data->base, fd3, 0);
bufferevent_enable(bev3, EV_READ|EV_WRITE);
bufferevent_setcb(bev3, NULL, NULL,
http_allowed_methods_eventcb, &result3);
http_request =
"FLOOP /test HTTP/1.1\r\n"
"Host: somehost\r\n"
"Connection: close\r\n"
"\r\n";
bufferevent_write(bev3, http_request, strlen(http_request));
event_base_dispatch(data->base);
bufferevent_free(bev1);
bufferevent_free(bev2);
bufferevent_free(bev3);
evutil_closesocket(fd1);
evutil_closesocket(fd2);
evutil_closesocket(fd3);
evhttp_free(http);
/* Method known but disallowed */
tt_assert(result1);
tt_assert(!strncmp(result1, "HTTP/1.1 501 ", strlen("HTTP/1.1 501 ")));
/* Method known and allowed */
tt_assert(result2);
tt_assert(!strncmp(result2, "HTTP/1.1 200 ", strlen("HTTP/1.1 200 ")));
/* Method unknown */
tt_assert(result3);
tt_assert(!strncmp(result3, "HTTP/1.1 501 ", strlen("HTTP/1.1 501 ")));
end:
if (result1)
free(result1);
if (result2)
free(result2);
if (result3)
free(result3);
}
static void http_request_done(struct evhttp_request *, void *);
static void http_request_empty_done(struct evhttp_request *, void *);
static void
_http_connection_test(struct basic_test_data *data, int persistent)
{
ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
test_ok = 0;
http = http_setup(&port, data->base);
evcon = evhttp_connection_base_new(data->base, NULL, "127.0.0.1", port);
tt_assert(evcon);
tt_assert(evhttp_connection_get_base(evcon) == data->base);
exit_base = data->base;
/*
* At this point, we want to schedule a request to the HTTP
* server using our make request method.
*/
req = evhttp_request_new(http_request_done, (void*) BASIC_REQUEST_BODY);
/* Add the information that we care about */
evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
/* We give ownership of the request to the connection */
if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
fprintf(stdout, "FAILED\n");
exit(1);
}
event_base_dispatch(data->base);
tt_assert(test_ok);
/* try to make another request over the same connection */
test_ok = 0;
req = evhttp_request_new(http_request_done, (void*) BASIC_REQUEST_BODY);
/* Add the information that we care about */
evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
/*
* if our connections are not supposed to be persistent; request
* a close from the server.
*/
if (!persistent)
evhttp_add_header(evhttp_request_get_output_headers(req), "Connection", "close");
/* We give ownership of the request to the connection */
if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
tt_abort_msg("couldn't make request");
}
event_base_dispatch(data->base);
/* make another request: request empty reply */
test_ok = 0;
req = evhttp_request_new(http_request_empty_done, data->base);
/* Add the information that we care about */
evhttp_add_header(evhttp_request_get_output_headers(req), "Empty", "itis");
/* We give ownership of the request to the connection */
if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
tt_abort_msg("Couldn't make request");
}
event_base_dispatch(data->base);
end:
if (evcon)
evhttp_connection_free(evcon);
if (http)
evhttp_free(http);
}
static void
http_connection_test(void *arg)
{
_http_connection_test(arg, 0);
}
static void
http_persist_connection_test(void *arg)
{
_http_connection_test(arg, 1);
}
static struct regress_dns_server_table search_table[] = {
{ "localhost", "A", "127.0.0.1", 0 },
{ NULL, NULL, NULL, 0 }
};
static void
http_connection_async_test(void *arg)
{
struct basic_test_data *data = arg;
ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
struct evdns_base *dns_base = NULL;
ev_uint16_t portnum = 0;
char address[64];
exit_base = data->base;
tt_assert(regress_dnsserver(data->base, &portnum, search_table));
dns_base = evdns_base_new(data->base, 0/* init name servers */);
tt_assert(dns_base);
/* Add ourself as the only nameserver, and make sure we really are
* the only nameserver. */
evutil_snprintf(address, sizeof(address), "127.0.0.1:%d", portnum);
evdns_base_nameserver_ip_add(dns_base, address);
test_ok = 0;
http = http_setup(&port, data->base);
evcon = evhttp_connection_base_new(data->base, dns_base, "127.0.0.1", port);
tt_assert(evcon);
/*
* At this point, we want to schedule a request to the HTTP
* server using our make request method.
*/
req = evhttp_request_new(http_request_done, (void*) BASIC_REQUEST_BODY);
/* Add the information that we care about */
evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
/* We give ownership of the request to the connection */
if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
fprintf(stdout, "FAILED\n");
exit(1);
}
event_base_dispatch(data->base);
tt_assert(test_ok);
/* try to make another request over the same connection */
test_ok = 0;
req = evhttp_request_new(http_request_done, (void*) BASIC_REQUEST_BODY);
/* Add the information that we care about */
evhttp_add_header(evhttp_request_get_output_headers(req), "Host", "somehost");
/*
* if our connections are not supposed to be persistent; request
* a close from the server.
*/
evhttp_add_header(evhttp_request_get_output_headers(req), "Connection", "close");
/* We give ownership of the request to the connection */
if (evhttp_make_request(evcon, req, EVHTTP_REQ_GET, "/test") == -1) {
tt_abort_msg("couldn't make request");
}
event_base_dispatch(data->base);
/* make another request: request empty reply */
test_ok = 0;
req = evhttp_request_new(http_request_empty_done, data->base);
/* Add the information that we care about */
evhttp_add_header(evhttp_request_get_output_headers(req), "Empty", "itis");
/* We give ownership of the request to the connection */