This repository has been archived by the owner on Oct 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_http_message.c
708 lines (641 loc) · 29.4 KB
/
test_http_message.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
#include <glib.h>
#include <string.h>
#include "http_message.h"
static void test_http_request_new(void) {
http_request_t *req = http_request_new();
g_assert(req->body == NULL);
http_request_free(req);
}
static void test_http_request_parser_init(void) {
http_request_t *req = http_request_new();
http_parser parser;
http_request_parser_init(req, &parser);
http_parser_init(&parser);
g_assert(req->body == NULL);
g_assert(parser.data == req);
http_request_free(req);
}
static void test_http_request_make1(void) {
http_request_t *req = http_request_new();
http_request_make(req, "GET", "/test/this?thing=1&stuff=2&fun&good");
http_request_header_append(req, "user-agent",
"curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18");
http_request_header_append(req, "host", "localhost:8080");
http_request_header_append(req, "accept", "*/*");
static const char *sdata =
"GET /test/this?thing=1&stuff=2&fun&good HTTP/1.1\r\n"
"User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18\r\n"
"Host: localhost:8080\r\n"
"Accept: */*\r\n"
"\r\n";
GString *s = http_request_data(req);
g_assert(g_strcmp0(sdata, s->str) == 0);
g_string_free(s, TRUE);
http_request_free(req);
}
static void test_http_request_make_parse(void) {
http_request_t *req = http_request_new();
http_request_make(req, "GET", "/test/this?thing=1&stuff=2&fun&good");
http_request_header_append(req, "user-agent",
"curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18");
http_request_header_append(req, "host", "localhost:8080");
http_request_header_append(req, "accept", "*/*");
GString *s = http_request_data(req);
http_request_t *req2 = http_request_new();
http_parser parser;
http_request_parser_init(req2, &parser);
http_parser_init(&parser);
http_parser_execute(&parser, s->str, s->len, 0);
g_assert(!http_parser_has_error(&parser));
g_assert(http_parser_is_finished(&parser));
g_string_free(s, TRUE);
http_request_free(req2);
http_request_free(req);
}
static void test_http_request_parser_normalize_header_names(void) {
http_request_t *req = http_request_new();
http_parser parser;
http_request_parser_init(req, &parser);
static const char *sdata =
"GET /test/this?thing=1&stuff=2&fun&good HTTP/1.1\r\n"
"usER-agEnT: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18\r\n"
"host: localhost:8080\r\n"
"ACCEPT: */*\r\n\r\n";
http_parser_init(&parser);
char *data = g_strdup(sdata);
char *p = data;
while (!http_parser_is_finished(&parser) &&
!http_parser_has_error(&parser) &&
*p != 0)
{
p += 1; /* feed parser 1 byte at a time */
http_parser_execute(&parser, data, p-data, p-data-1);
}
g_assert(!http_parser_has_error(&parser));
g_assert(http_parser_is_finished(&parser));
g_assert(g_strcmp0(req->method, "GET") == 0);
g_assert(g_strcmp0(req->uri, "/test/this?thing=1&stuff=2&fun&good") == 0);
g_assert(g_strcmp0(req->path, "/test/this") == 0);
g_assert(g_strcmp0(req->query_string, "thing=1&stuff=2&fun&good") == 0);
g_assert(g_strcmp0(req->http_version, "HTTP/1.1") == 0);
g_assert(g_strcmp0(req->body, NULL) == 0);
g_assert(req->body_length == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "User-Agent"),
"curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Host"), "localhost:8080") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Accept"), "*/*") == 0);
http_request_free(req);
g_free(data);
}
static void test_http_request_parser_headers(void) {
http_request_t *req = http_request_new();
http_parser parser;
http_request_parser_init(req, &parser);
static const char *sdata =
"GET http://b.scorecardresearch.com/b?C1=8&C2=6035047&C3=463.9924&C4=ad21868c&C5=173229&C6=16jfaue1ukmeoq&C7=http%3A//remotecontrol.mtv.com/2011/01/20/sammi-sweetheart-giancoloa-terrell-owens-hair/&C8=Hot%20Shots%3A%20Sammi%20%u2018Sweetheart%u2019%20Lets%20Terrell%20Owens%20Play%20With%20Her%20Hair%20%BB%20MTV%20Remote%20Control%20Blog&C9=&C10=1680x1050&rn=58013009 HTTP/1.1\r\n"
"User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18\r\n"
"Host: localhost:8080\r\n"
"Accept: */*\r\n"
"Accept-Encoding: gzip,deflate,sdch\r\n"
"Accept-Language: en-US,en;q=0.8\r\n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n"
"Cookie: UID=11f039-200.050.203.060-1205406020\r\n"
"\r\n";
http_parser_init(&parser);
char *data = g_strdup(sdata);
http_parser_execute(&parser, data, strlen(data), 0);
g_assert(!http_parser_has_error(&parser));
g_assert(http_parser_is_finished(&parser));
g_assert(g_strcmp0(req->method, "GET") == 0);
g_assert(g_strcmp0(req->http_version, "HTTP/1.1") == 0);
g_assert(g_strcmp0(req->body, NULL) == 0);
g_assert(req->body_length == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "User-Agent"),
"curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Host"), "localhost:8080") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Accept"), "*/*") == 0);
http_request_free(req);
g_free(data);
}
static void test_http_request_parser_unicode_escape(void) {
http_request_t *req = http_request_new();
http_parser parser;
http_request_parser_init(req, &parser);
static const char *sdata =
"GET http://b.scorecardresearch.com/b?C1=8&C2=6035047&C3=463.9924&C4=ad21868c&C5=173229&C6=16jfaue1ukmeoq&C7=http%3A//remotecontrol.mtv.com/2011/01/20/sammi-sweetheart-giancoloa-terrell-owens-hair/&C8=Hot%20Shots%3A%20Sammi%20%u2018Sweetheart%u2019%20Lets%20Terrell%20Owens%20Play%20With%20Her%20Hair%20%BB%20MTV%20Remote%20Control%20Blog&C9=&C10=1680x1050&rn=58013009 HTTP/1.1\r\n"
"User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18\r\n"
"Host: localhost:8080\r\n"
"Accept: */*\r\n\r\n";
http_parser_init(&parser);
char *data = g_strdup(sdata);
http_parser_execute(&parser, data, strlen(data), 0);
g_assert(!http_parser_has_error(&parser));
g_assert(http_parser_is_finished(&parser));
g_assert(g_strcmp0(req->method, "GET") == 0);
g_assert(g_strcmp0(req->http_version, "HTTP/1.1") == 0);
g_assert(g_strcmp0(req->body, NULL) == 0);
g_assert(req->body_length == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "User-Agent"),
"curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Host"), "localhost:8080") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Accept"), "*/*") == 0);
http_request_free(req);
g_free(data);
}
static void test_http_request_parser_percents(void) {
http_request_t *req = http_request_new();
http_parser parser;
http_request_parser_init(req, &parser);
static const char *sdata =
"GET http://bh.contextweb.com/bh/getuid?url=http://image2.pubmatic.com/AdServer/Pug?vcode=bz0yJnR5cGU9MSZqcz0xJmNvZGU9ODI1JnRsPTQzMjAw&piggybackCookie=%%CWGUID%%,User_tokens:%%USER_TOKENS%% HTTP/1.1\r\n"
"User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18\r\n"
"Host: localhost:8080\r\n"
"Accept: */*\r\n\r\n";
http_parser_init(&parser);
char *data = g_strdup(sdata);
http_parser_execute(&parser, data, strlen(data), 0);
g_assert(!http_parser_has_error(&parser));
g_assert(http_parser_is_finished(&parser));
g_assert(g_strcmp0(req->method, "GET") == 0);
g_assert(g_strcmp0(req->http_version, "HTTP/1.1") == 0);
g_assert(g_strcmp0(req->body, NULL) == 0);
g_assert(req->body_length == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "User-Agent"),
"curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Host"), "localhost:8080") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Accept"), "*/*") == 0);
http_request_free(req);
g_free(data);
}
static void test_http_request_parser_bad_percents(void) {
http_request_t *req = http_request_new();
http_parser parser;
http_request_parser_init(req, &parser);
/* this site likes to truncate urls included in their requests, which means they chop up % encoding in the original urls */
/* for example in this url: Trends%2C%&cv= */
static const char *sdata =
"GET http://b.scorecardresearch.com/b?c1=8&c2=3005693&rn=698454542&c7=http%3A%2F%2Fwww.readwriteweb.com%2F&c3=1&c4=http%3A%2F%2Fwww.readwriteweb.com%2F&c8=ReadWriteWeb%20-%20Web%20Apps%2C%20Web%20Technology%20Trends%2C%&cv=2.2&cs=js HTTP/1.1\r\n"
"User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18\r\n"
"Host: localhost:8080\r\n"
"Accept: */*\r\n\r\n";
http_parser_init(&parser);
char *data = g_strdup(sdata);
http_parser_execute(&parser, data, strlen(data), 0);
g_assert(!http_parser_has_error(&parser));
g_assert(http_parser_is_finished(&parser));
g_assert(g_strcmp0(req->method, "GET") == 0);
#if 0
g_assert(g_strcmp0(req->uri, "/test/this?thing=1&stuff=2&fun&good") == 0);
g_assert(g_strcmp0(req->path, "/test/this") == 0);
g_assert(g_strcmp0(req->query_string, "thing=1&stuff=2&fun&good") == 0);
#endif
g_assert(g_strcmp0(req->http_version, "HTTP/1.1") == 0);
g_assert(g_strcmp0(req->body, NULL) == 0);
g_assert(req->body_length == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "User-Agent"),
"curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Host"), "localhost:8080") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Accept"), "*/*") == 0);
http_request_free(req);
g_free(data);
}
static void test_http_request_parser_p0(void) {
http_request_t *req = http_request_new();
http_parser parser;
http_request_parser_init(req, &parser);
static const char *sdata =
"GET /test/this?thing=1&stuff=2&fun&good HTTP/1.1\r\n"
"User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18\r\n"
"Host: localhost:8080\r\n"
"Accept: */*\r\n\r\n";
http_parser_init(&parser);
char *data = g_strdup(sdata);
char *p = data;
while (!http_parser_is_finished(&parser) &&
!http_parser_has_error(&parser) &&
*p != 0)
{
p += 1; /* feed parser 1 byte at a time */
http_parser_execute(&parser, data, p-data, p-data-1);
}
g_assert(!http_parser_has_error(&parser));
g_assert(http_parser_is_finished(&parser));
g_assert(g_strcmp0(req->method, "GET") == 0);
g_assert(g_strcmp0(req->uri, "/test/this?thing=1&stuff=2&fun&good") == 0);
g_assert(g_strcmp0(req->path, "/test/this") == 0);
g_assert(g_strcmp0(req->query_string, "thing=1&stuff=2&fun&good") == 0);
g_assert(g_strcmp0(req->http_version, "HTTP/1.1") == 0);
g_assert(g_strcmp0(req->body, NULL) == 0);
g_assert(req->body_length == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "User-Agent"),
"curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Host"), "localhost:8080") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Accept"), "*/*") == 0);
http_request_free(req);
g_free(data);
}
static void test_http_request_parser_p1(void) {
http_request_t *req = http_request_new();
http_parser parser;
http_request_parser_init(req, &parser);
static const char *sdata =
"GET /test/this?thing=1&stuff=2&fun&good HTTP/1.1\r\n"
"User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18\r\n"
"Host: localhost:8080\r\n"
"Accept: */*\r\n\r\n";
http_parser_init(&parser);
char *data = g_strdup(sdata);
/* parse all in one go */
http_parser_execute(&parser, data, strlen(data), 0);
g_assert(!http_parser_has_error(&parser));
g_assert(http_parser_is_finished(&parser));
g_assert(g_strcmp0(req->method, "GET") == 0);
g_assert(g_strcmp0(req->uri, "/test/this?thing=1&stuff=2&fun&good") == 0);
g_assert(g_strcmp0(req->path, "/test/this") == 0);
g_assert(g_strcmp0(req->query_string, "thing=1&stuff=2&fun&good") == 0);
g_assert(g_strcmp0(req->http_version, "HTTP/1.1") == 0);
g_assert(g_strcmp0(req->body, NULL) == 0);
g_assert(req->body_length == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "User-Agent"),
"curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Host"), "localhost:8080") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Accept"), "*/*") == 0);
http_request_free(req);
g_free(data);
}
static void test_http_request_parser_p2(void) {
http_request_t *req = http_request_new();
http_parser parser;
http_request_parser_init(req, &parser);
static const char *sdata = "GET /test/this?thing=1&stuff=2&fun&good HTTP/1.1\r\n";
http_parser_init(&parser);
char *data = g_strdup(sdata);
http_parser_execute(&parser, data, strlen(data), 0);
g_assert(!http_parser_has_error(&parser));
g_assert(!http_parser_is_finished(&parser));
g_assert(g_strcmp0(req->method, "GET") == 0);
g_assert(g_strcmp0(req->uri, "/test/this?thing=1&stuff=2&fun&good") == 0);
g_assert(g_strcmp0(req->path, "/test/this") == 0);
g_assert(g_strcmp0(req->query_string, "thing=1&stuff=2&fun&good") == 0);
g_assert(g_strcmp0(req->http_version, "HTTP/1.1") == 0);
g_assert(g_strcmp0(req->body, NULL) == 0);
g_assert(req->body_length == 0);
http_request_free(req);
g_free(data);
}
static void test_http_request_parser_p3(void) {
http_request_t *req = http_request_new();
http_parser parser;
http_request_parser_init(req, &parser);
static const char *sdata = "\x01\xff 83949475dsf--==\x45 dsfsdfds";
http_parser_init(&parser);
char *data = g_strdup(sdata);
http_parser_execute(&parser, data, strlen(data), 0);
g_assert(http_parser_has_error(&parser));
g_assert(!http_parser_is_finished(&parser));
http_request_free(req);
g_free(data);
}
static void test_http_request_parser_proxy_http12(void) {
http_request_t *req = http_request_new();
http_parser parser;
http_request_parser_init(req, &parser);
static const char *sdata =
"GET http://example.com:9182/test/this?thing=1&stuff=2&fun&good HTTP/1.1\r\n"
"User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18\r\n"
"Host: localhost:8080\r\n"
"Accept: */*\r\n\r\n";
http_parser_init(&parser);
char *data = g_strdup(sdata);
/* parse all in one go */
http_parser_execute(&parser, data, strlen(data), 0);
g_assert(!http_parser_has_error(&parser));
g_assert(http_parser_is_finished(&parser));
g_assert(g_strcmp0(req->method, "GET") == 0);
g_assert(g_strcmp0(req->uri, "http://example.com:9182/test/this?thing=1&stuff=2&fun&good") == 0);
/* path is NULL when fully qualified uri is used */
/* TODO: maybe add support for full uri parsing */
g_assert(req->path == NULL);
g_assert(req->query_string == NULL);
g_assert(g_strcmp0(req->http_version, "HTTP/1.1") == 0);
g_assert(g_strcmp0(req->body, NULL) == 0);
g_assert(req->body_length == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "User-Agent"),
"curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Host"), "localhost:8080") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Accept"), "*/*") == 0);
http_request_free(req);
g_free(data);
}
static void test_http_request_clear(void) {
http_request_t *req = http_request_new();
http_parser parser;
http_request_parser_init(req, &parser);
static const char *sdata =
"GET /test/this?thing=1&stuff=2&fun&good HTTP/1.1\r\n"
"User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18\r\n"
"Host: localhost:8080\r\n"
"Accept: */*\r\n\r\n";
http_parser_init(&parser);
char *data = g_strdup(sdata);
/* parse all in one go */
http_parser_execute(&parser, data, strlen(data), 0);
g_assert(!http_parser_has_error(&parser));
g_assert(http_parser_is_finished(&parser));
g_assert(g_strcmp0(req->method, "GET") == 0);
g_assert(g_strcmp0(req->uri, "/test/this?thing=1&stuff=2&fun&good") == 0);
g_assert(g_strcmp0(req->path, "/test/this") == 0);
g_assert(g_strcmp0(req->query_string, "thing=1&stuff=2&fun&good") == 0);
g_assert(g_strcmp0(req->http_version, "HTTP/1.1") == 0);
g_assert(g_strcmp0(req->body, NULL) == 0);
g_assert(req->body_length == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "User-Agent"),
"curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Host"), "localhost:8080") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Accept"), "*/*") == 0);
/* clear this request */
http_request_clear(req);
g_assert(req->method == 0);
g_assert(req->uri == 0);
g_assert(req->path == 0);
g_assert(req->query_string == 0);
g_assert(req->http_version == 0);
g_assert(req->body == 0);
g_assert(req->body_length == 0);
http_parser_init(&parser);
http_parser_execute(&parser, data, strlen(data), 0);
g_assert(!http_parser_has_error(&parser));
g_assert(http_parser_is_finished(&parser));
g_assert(g_strcmp0(req->method, "GET") == 0);
g_assert(g_strcmp0(req->uri, "/test/this?thing=1&stuff=2&fun&good") == 0);
g_assert(g_strcmp0(req->path, "/test/this") == 0);
g_assert(g_strcmp0(req->query_string, "thing=1&stuff=2&fun&good") == 0);
g_assert(g_strcmp0(req->http_version, "HTTP/1.1") == 0);
g_assert(g_strcmp0(req->body, NULL) == 0);
g_assert(req->body_length == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "User-Agent"),
"curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Host"), "localhost:8080") == 0);
g_assert(g_strcmp0(http_request_header_getstr(req, "Accept"), "*/*") == 0);
http_request_free(req);
g_free(data);
}
static void test_http_request_headers(void) {
http_request_t *req = http_request_new();
http_request_header_append(req, "test-a", "test-a");
http_request_header_append(req, "test-b", "test-b");
http_request_header_append(req, "test-c", "test-c");
http_request_header_append(req, "test-a", "test-a");
g_assert(g_strcmp0("test-a", http_request_header_getstr(req, "test-a")));
g_assert(g_strcmp0("test-b", http_request_header_getstr(req, "test-b")));
g_assert(g_strcmp0("test-c", http_request_header_getstr(req, "test-c")));
g_assert(http_request_header_remove(req, "Test-A"));
g_assert(http_request_header_remove(req, "Test-B"));
g_assert(http_request_header_remove(req, "Test-C"));
g_assert(http_request_header_remove(req, "Test-A") == FALSE);
http_request_free(req);
}
/* http response */
static void test_http_response_new(void) {
http_response_t *resp = http_response_new(200, "OK");
g_assert(resp->status_code == 200);
g_assert(g_strcmp0("OK", resp->reason) == 0);
g_assert(g_strcmp0("HTTP/1.1", resp->http_version) == 0);
g_assert(resp->body == NULL);
http_response_free(resp);
}
static void test_http_response_data(void) {
http_response_t *resp = http_response_new(200, "OK");
http_response_header_append(resp, "Host", "localhost");
http_response_header_append(resp, "Content-Length", "0");
g_assert(resp->status_code == 200);
g_assert(g_strcmp0("OK", resp->reason) == 0);
g_assert(g_strcmp0("HTTP/1.1", resp->http_version) == 0);
g_assert(resp->body == NULL);
g_assert(g_strcmp0("0", http_response_header_getstr(resp, "Content-Length")) == 0);
GString *s = http_response_data(resp);
static const char *expected_data =
"HTTP/1.1 200 OK\r\n"
"Host: localhost\r\n"
"Content-Length: 0\r\n"
"\r\n";
g_assert(g_strcmp0(expected_data, s->str) == 0);
g_string_free(s, TRUE);
http_response_free(resp);
}
static void test_http_response_body(void) {
http_response_t *resp = http_response_new(200, "OK");
http_response_header_append(resp, "Host", "localhost");
http_response_header_append(resp, "Content-Type", "text/plain");
static const char *body = "this is a test.\r\nthis is only a test.";
char numstr[32];
snprintf(numstr, sizeof(numstr), "%zu", strlen(body));
http_response_set_body(resp, body);
g_assert(resp->status_code == 200);
g_assert(g_strcmp0("OK", resp->reason) == 0);
g_assert(g_strcmp0("HTTP/1.1", resp->http_version) == 0);
g_assert(g_strcmp0(body, resp->body) == 0);
g_assert(g_strcmp0("text/plain", http_response_header_getstr(resp, "Content-Type")) == 0);
g_assert(g_strcmp0("37", http_response_header_getstr(resp, "Content-Length")) == 0);
GString *s = http_response_data(resp);
static const char *expected_data =
"HTTP/1.1 200 OK\r\n"
"Host: localhost\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: 37\r\n"
"\r\n";
g_assert(g_strcmp0(expected_data, s->str) == 0);
g_string_free(s, TRUE);
http_response_free(resp);
}
static void test_http_response_parser_init(void) {
http_response_t *resp = http_response_new(200, "OK");
httpclient_parser parser;
http_response_parser_init(resp, &parser);
httpclient_parser_init(&parser);
g_assert(resp->body == NULL);
g_assert(parser.data == resp);
http_response_free(resp);
}
static void test_http_response_parser_normalize_header_names(void) {
http_response_t *resp = http_response_new(200, "OK");
httpclient_parser parser;
http_response_parser_init(resp, &parser);
httpclient_parser_init(&parser);
static const char *sdata =
"HTTP/1.1 200 OK\r\n"
"cOnTent-leNgtH: 37\r\n"
"ConteNt-tYpE: text/plain\r\n"
"HOST: localhost\r\n\r\n"
"this is a test.\r\nthis is only a test.";
char *data = g_strdup(sdata);
char *p = data;
while (!httpclient_parser_is_finished(&parser) &&
!httpclient_parser_has_error(&parser) &&
*p != 0)
{
p += 1; /* feed parser 1 byte at a time */
httpclient_parser_execute(&parser, data, p-data, p-data-1);
}
g_assert(!httpclient_parser_has_error(&parser));
g_assert(httpclient_parser_is_finished(&parser));
g_assert(g_strcmp0("HTTP/1.1", resp->http_version) == 0);
g_assert(resp->status_code == 200);
g_assert(g_strcmp0("OK", resp->reason) == 0);
g_assert(g_strcmp0("this is a test.\r\nthis is only a test.", resp->body) == 0);
g_assert(g_strcmp0("37", http_response_header_getstr(resp, "Content-Length")) == 0);
g_assert(g_strcmp0("text/plain", http_response_header_getstr(resp, "Content-Type")) == 0);
http_response_free(resp);
g_free(data);
}
static void test_http_response_parser_p0(void) {
http_response_t *resp = http_response_new(200, "OK");
httpclient_parser parser;
http_response_parser_init(resp, &parser);
httpclient_parser_init(&parser);
static const char *sdata =
"HTTP/1.1 200 OK\r\n"
"Content-Length: 37\r\n"
"Content-Type: text/plain\r\n"
"Host: localhost\r\n\r\n"
"this is a test.\r\nthis is only a test.";
char *data = g_strdup(sdata);
char *p = data;
while (!httpclient_parser_is_finished(&parser) &&
!httpclient_parser_has_error(&parser) &&
*p != 0)
{
p += 1; /* feed parser 1 byte at a time */
httpclient_parser_execute(&parser, data, p-data, p-data-1);
}
g_assert(!httpclient_parser_has_error(&parser));
g_assert(httpclient_parser_is_finished(&parser));
g_assert(g_strcmp0("HTTP/1.1", resp->http_version) == 0);
g_assert(resp->status_code == 200);
g_assert(g_strcmp0("OK", resp->reason) == 0);
g_assert(g_strcmp0("this is a test.\r\nthis is only a test.", resp->body) == 0);
g_assert(g_strcmp0("37", http_response_header_getstr(resp, "Content-Length")) == 0);
g_assert(g_strcmp0("text/plain", http_response_header_getstr(resp, "Content-Type")) == 0);
http_response_free(resp);
g_free(data);
}
static void test_http_response_parser_p1(void) {
http_response_t *resp = http_response_new(200, "OK");
httpclient_parser parser;
http_response_parser_init(resp, &parser);
httpclient_parser_init(&parser);
static const char *sdata =
"HTTP/1.1 200 OK\r\n"
"Content-Length: 37\r\n"
"Content-Type: text/plain\r\n"
"Host: localhost\r\n\r\n"
"this is a test.\r\nthis is only a test.";
char *data = g_strdup(sdata);
httpclient_parser_execute(&parser, data, strlen(data), 0);
g_assert(!httpclient_parser_has_error(&parser));
g_assert(httpclient_parser_is_finished(&parser));
g_assert(g_strcmp0("this is a test.\r\nthis is only a test.", resp->body) == 0);
g_assert(parser.data == resp);
g_assert(g_strcmp0("HTTP/1.1", resp->http_version) == 0);
g_assert(resp->status_code == 200);
g_assert(g_strcmp0("OK", resp->reason) == 0);
g_assert(g_strcmp0("37", http_response_header_getstr(resp, "Content-Length")) == 0);
g_assert(g_strcmp0("text/plain", http_response_header_getstr(resp, "Content-Type")) == 0);
http_response_free(resp);
g_free(data);
}
static void test_http_response_parser_chunked(void) {
http_response_t *resp = http_response_new(200, "OK");
httpclient_parser parser;
http_response_parser_init(resp, &parser);
httpclient_parser_init(&parser);
/* borrowed from http://en.wikipedia.org/wiki/Chunked_transfer_encoding */
static const char *sdata =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n"
"Transfer-Encoding: chunked\r\n\r\n"
"25\r\n"
"This is the data in the first chunk\r\n\r\n"
"1C\r\n"
"and this is the second one\r\n\r\n"
"3\r\n"
"con\r\n"
"8\r\n"
"sequence\r\n"
"0\r\n"
"\r\n";
char *data = g_strdup(sdata);
httpclient_parser_execute(&parser, data, strlen(data), 0);
g_assert(!httpclient_parser_has_error(&parser));
g_assert(httpclient_parser_is_finished(&parser));
g_assert(g_strcmp0("HTTP/1.1", resp->http_version) == 0);
g_assert(resp->status_code == 200);
g_assert(g_strcmp0("OK", resp->reason) == 0);
g_assert(g_strcmp0("text/plain", http_response_header_getstr(resp, "Content-Type")) == 0);
g_assert(g_strcmp0("chunked", http_response_header_getstr(resp, "Transfer-Encoding")) == 0);
/* reset the parse for the chunked part */
httpclient_parser_init(&parser);
httpclient_parser_execute(&parser, resp->body, strlen(resp->body), 0);
g_assert(!httpclient_parser_has_error(&parser));
g_assert(httpclient_parser_is_finished(&parser));
g_assert(resp->chunk_size == 37);
g_assert(resp->last_chunk == FALSE);
const gchar *mark = resp->body+resp->chunk_size+2; // 2 for terminating crlf
httpclient_parser_init(&parser);
httpclient_parser_execute(&parser, mark, strlen(mark), 0);
g_assert(!httpclient_parser_has_error(&parser));
g_assert(httpclient_parser_is_finished(&parser));
g_assert(resp->chunk_size == 28);
g_assert(resp->last_chunk == FALSE);
mark = resp->body+resp->chunk_size+2;
httpclient_parser_init(&parser);
httpclient_parser_execute(&parser, mark, strlen(mark), 0);
g_assert(!httpclient_parser_has_error(&parser));
g_assert(httpclient_parser_is_finished(&parser));
g_assert(resp->chunk_size == 3);
g_assert(resp->last_chunk == FALSE);
mark = resp->body+resp->chunk_size+2;
httpclient_parser_init(&parser);
httpclient_parser_execute(&parser, mark, strlen(mark), 0);
g_assert(!httpclient_parser_has_error(&parser));
g_assert(httpclient_parser_is_finished(&parser));
g_assert(resp->chunk_size == 8);
g_assert(resp->last_chunk == FALSE);
mark = resp->body+resp->chunk_size+2;
httpclient_parser_init(&parser);
httpclient_parser_execute(&parser, mark, strlen(mark), 0);
g_assert(!httpclient_parser_has_error(&parser));
g_assert(httpclient_parser_is_finished(&parser));
g_assert(resp->chunk_size == 0);
g_assert(resp->last_chunk == TRUE);
http_response_free(resp);
g_free(data);
}
int main(int argc, char *argv[]) {
g_test_init(&argc, &argv, NULL);
g_test_add_func("/http/request/new", test_http_request_new);
g_test_add_func("/http/request/make1", test_http_request_make1);
g_test_add_func("/http/request/make_parse", test_http_request_make_parse);
g_test_add_func("/http/request/parser/init", test_http_request_parser_init);
g_test_add_func("/http/request/parser/normalize_header_names", test_http_request_parser_normalize_header_names);
g_test_add_func("/http/request/parser/unicode_escape", test_http_request_parser_unicode_escape);
g_test_add_func("/http/request/parser/percents", test_http_request_parser_percents);
g_test_add_func("/http/request/parser/bad_percents", test_http_request_parser_bad_percents);
g_test_add_func("/http/request/parser/headers", test_http_request_parser_headers);
g_test_add_func("/http/request/parser/p0", test_http_request_parser_p0);
g_test_add_func("/http/request/parser/p1", test_http_request_parser_p1);
g_test_add_func("/http/request/parser/p2", test_http_request_parser_p2);
g_test_add_func("/http/request/parser/p3", test_http_request_parser_p3);
g_test_add_func("/http/request/parser/proxy_http12", test_http_request_parser_proxy_http12);
g_test_add_func("/http/request/headers", test_http_request_headers);
g_test_add_func("/http/request/clear", test_http_request_clear);
g_test_add_func("/http/response/new", test_http_response_new);
g_test_add_func("/http/response/data", test_http_response_data);
g_test_add_func("/http/response/body", test_http_response_body);
g_test_add_func("/http/response/parser/init", test_http_response_parser_init);
g_test_add_func("/http/response/parser/normalize_header_names", test_http_response_parser_normalize_header_names);
g_test_add_func("/http/response/parser/p0", test_http_response_parser_p0);
g_test_add_func("/http/response/parser/p1", test_http_response_parser_p1);
g_test_add_func("/http/response/parser/chunked", test_http_response_parser_chunked);
return g_test_run();
}