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
|
// -*- mode: c++; c-basic-offset: 2; -*-
#include "common.hh"
#include "test.hh"
#include <cstdarg>
#include <cstring>
#include "http.hh"
namespace {
bool find_header(HeaderIterator* iter, std::string const& name,
std::string* value) {
for (; iter->valid(); iter->next()) {
if (iter->name_equal(name)) {
if (value) value->assign(iter->value());
return true;
}
}
return false;
}
bool good_resp(std::string const& name,
std::string const& in,
std::string const& proto,
uint16_t major, uint16_t minor,
uint16_t status, std::string const& status_msg,
char const* content,
...) {
std::unique_ptr<HttpResponse> resp;
size_t test = 0;
while (test <= in.size()) {
resp.reset(HttpResponse::parse(in.substr(0, test)));
if (resp) break;
++test;
}
if (!resp || !resp->good()) {
std::cerr << "good_resp:" << name << ": Invalid response" << std::endl;
return false;
}
resp.reset(HttpResponse::parse(in));
if (!resp || !resp->good()) {
std::cerr << "good_resp:"
<< name << ": Incomplete/invalid response" << std::endl;
return false;
}
if (proto != resp->proto() || !resp->proto_equal(proto)) {
std::cerr << "good_resp:" << name << ":protocol: Expected " << proto
<< " got " << resp->proto() << std::endl;
return false;
}
if (major != resp->proto_version().major
|| minor != resp->proto_version().minor) {
std::cerr << "good_resp:" << name << ":protocol_version: Expected "
<< major << "." << minor << " got "
<< resp->proto_version().major << "."
<< resp->proto_version().minor << std::endl;
return false;
}
if (status != resp->status_code()) {
std::cerr << "good_resp:" << name << ":status: Expected " << status
<< " got " << resp->status_code() << std::endl;
return false;
}
if (status_msg != resp->status_message()) {
std::cerr << "good_resp:" << name << ":status: Expected " << status_msg
<< " got " << resp->status_message() << std::endl;
return false;
}
va_list headers;
va_start(headers, content);
while (true) {
auto header = va_arg(headers, char const*);
if (!header) break;
auto value = va_arg(headers, char const*);
auto iter = resp->header();
std::string tmp;
if (find_header(iter.get(), header, &tmp)) {
if (tmp.compare(value)) {
std::cerr << "good_resp:" << name << ":header" << header
<< ": Expected " << value << " got " << tmp
<< std::endl;
return false;
}
} else {
std::cerr << "good_resp:" << name << ":header:" << header
<< ": Expected " << value << " got no value" << std::endl;
return false;
}
iter = resp->header(header);
if (!iter->valid()) {
std::cerr << "good_resp:" << name << ":header2:" << header
<< ": Expected " << value << " got no value" << std::endl;
return false;
}
if (!iter->name_equal(header)) {
std::cerr << "good_resp:" << name << ":header2:" << header
<< ": Expected " << header << " got " << iter->name()
<< std::endl;
return false;
}
if (iter->value().compare(value)) {
std::cerr << "good_resp:" << name << ":header2:" << header
<< ": Expected " << value << " got " << iter->value()
<< std::endl;
return false;
}
iter->next();
if (iter->valid()) {
std::cerr << "good_resp:" << name << ":header2:" << header
<< ": Expected " << value << " got multiple values"
<< std::endl;
return false;
}
}
va_end(headers);
if (resp->size() != test) {
std::cerr << "good_resp:" << name << ":size: Expected " << test
<< " got " << resp->size() << std::endl;
return false;
}
auto size = strlen(content);
std::string rest = in.substr(resp->size());
if (rest.size() != size) {
std::cerr << "good_resp:" << name << ":content: Expected "
<< size << " bytes got " << rest.size() << std::endl;
return false;
}
if (memcmp(content, rest.data(), size)) {
std::cerr << "good_resp:" << name << ":content: Expected "
<< content << " got " << rest << std::endl;
return false;
}
return true;
}
bool good_req(std::string const& name,
std::string const& in,
std::string const& method,
std::string const& url,
std::string const& proto,
uint16_t major, uint16_t minor,
char const* content,
...) {
std::unique_ptr<HttpRequest> req;
size_t test = 0;
while (test <= in.size()) {
req.reset(HttpRequest::parse(in.substr(0, test)));
if (req) break;
++test;
}
if (!req || !req->good()) {
std::cerr << "good_req:" << name << ": Invalid request" << std::endl;
return false;
}
req.reset(HttpRequest::parse(in));
if (!req || !req->good()) {
std::cerr << "good_req:"
<< name << ": Incomplete/invalid request" << std::endl;
return false;
}
if (method != req->method() || !req->method_equal(method)) {
std::cerr << "good_req:" << name << ":protocol: Expected " << proto
<< " got " << req->proto() << std::endl;
return false;
}
if (url != req->url()) {
std::cerr << "good_req:" << name << ":url: Expected " << url
<< " got " << req->url() << std::endl;
return false;
}
if (proto != req->proto() || !req->proto_equal(proto)) {
std::cerr << "good_req:" << name << ":protocol: Expected '" << proto
<< "' got '" << req->proto() << "'" << std::endl;
return false;
}
if (major != req->proto_version().major
|| minor != req->proto_version().minor) {
std::cerr << "good_req:" << name << ":protocol_version: Expected "
<< major << "." << minor << " got "
<< req->proto_version().major << "."
<< req->proto_version().minor << std::endl;
return false;
}
va_list headers;
va_start(headers, content);
while (true) {
auto header = va_arg(headers, char const*);
if (!header) break;
auto value = va_arg(headers, char const*);
auto iter = req->header();
std::string tmp;
if (find_header(iter.get(), header, &tmp)) {
if (tmp.compare(value)) {
std::cerr << "good_req:" << name << ":header" << header
<< ": Expected " << value << " got " << tmp
<< std::endl;
return false;
}
} else {
std::cerr << "good_req:" << name << ":header:" << header
<< ": Expected " << value << " got no value" << std::endl;
return false;
}
iter = req->header(header);
if (!iter->valid()) {
std::cerr << "good_req:" << name << ":header2:" << header
<< ": Expected " << value << " got no value" << std::endl;
return false;
}
if (!iter->name_equal(header)) {
std::cerr << "good_req:" << name << ":header2:" << header
<< ": Expected " << header << " got " << iter->name()
<< std::endl;
return false;
}
if (iter->value().compare(value)) {
std::cerr << "good_req:" << name << ":header2:" << header
<< ": Expected " << value << " got " << iter->value()
<< std::endl;
return false;
}
iter->next();
if (iter->valid()) {
std::cerr << "good_req:" << name << ":header2:" << header
<< ": Expected " << value << " got multiple values"
<< std::endl;
return false;
}
}
va_end(headers);
if (req->size() != test) {
std::cerr << "good_req:" << name << ":size: Expected " << test
<< " got " << req->size() << std::endl;
return false;
}
auto size = strlen(content);
std::string rest = in.substr(req->size());
if (rest.size() != size) {
std::cerr << "good_req:" << name << ":content: Expected "
<< size << " bytes got " << rest.size() << std::endl;
return false;
}
if (memcmp(content, rest.data(), size)) {
std::cerr << "good_req:" << name << ":content: Expected "
<< content << " got " << rest << std::endl;
return false;
}
return true;
}
bool bad_resp(std::string const& name, std::string const& in) {
std::unique_ptr<HttpResponse> resp(HttpResponse::parse(in));
if (!resp || resp->good()) {
std::cerr << "bad_resp:" << name << ": Expected invalid response"
<< std::endl;
return false;
}
return true;
}
bool short_resp(std::string const& name, std::string const& in) {
std::unique_ptr<HttpResponse> resp(HttpResponse::parse(in));
if (resp) {
std::cerr << "short_resp:" << name << ": Expected incomplete response"
<< std::endl;
return false;
}
return true;
}
bool req(std::string const& name, std::string const& out,
std::string const& method, std::string const& url,
std::string const& proto, uint16_t major, uint16_t minor,
char const* content, ...) {
Version ver;
ver.major = major;
ver.minor = minor;
std::unique_ptr<HttpRequestBuilder> r(
HttpRequestBuilder::create(method, url, proto, ver));
if (content) r->set_content(content);
va_list headers;
va_start(headers, content);
while (true) {
auto header = va_arg(headers, const char*);
if (!header) break;
auto value = va_arg(headers, const char*);
r->add_header(header, value);
}
va_end(headers);
auto tmp = r->build();
if (tmp != out) {
std::cerr << "req:" << name << ": Expected:\n"
<< out << "Got:\n" << tmp << std::endl;
return false;
}
return true;
}
bool resp(std::string const& name, std::string const& out,
std::string const& proto, uint16_t major, uint16_t minor,
uint16_t status_code, std::string const& status_message,
char const* content, ...) {
Version ver;
ver.major = major;
ver.minor = minor;
std::unique_ptr<HttpResponseBuilder> r(
HttpResponseBuilder::create(proto, ver, status_code, status_message));
if (content) r->set_content(content);
va_list headers;
va_start(headers, content);
while (true) {
auto header = va_arg(headers, const char*);
if (!header) break;
auto value = va_arg(headers, const char*);
r->add_header(header, value);
}
va_end(headers);
auto tmp = r->build();
if (tmp != out) {
std::cerr << "resp:" << name << ": Expected:\n"
<< out << "Got:\n" << tmp << std::endl;
return false;
}
return true;
}
bool tokens(std::string const& in, char const* header, ...) {
std::unique_ptr<HttpResponse> resp(HttpResponse::parse(in));
if (!resp) {
std::cerr << "tokens:" << header << ": Expected valid http" << std::endl;
return false;
}
auto iter = resp->header_tokens(header);
va_list tokens;
va_start(tokens, header);
while (true) {
auto token = va_arg(tokens, char const*);
if (!token) break;
if (!iter->valid()) {
std::cerr << "tokens:" << header << ": Expected " << token << " got "
<< "no more tokens" << std::endl;
return false;
}
if (iter->token().compare(token)) {
std::cerr << "tokens:" << header << ": Expected " << token << " got "
<< iter->token() << std::endl;
return false;
}
iter->next();
}
if (iter->valid()) {
std::cerr << "tokens:" << header << ": Expected no more tokens got "
<< iter->token() << std::endl;
return false;
}
return true;
}
} // namespace
int main() {
BEFORE;
RUN(good_resp("index_200_html",
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Accept-Ranges: bytes\r\n"
"ETag: \"1908074049\"\r\n"
"Last-Modified: Sat, 21 Apr 2012 09:42:46 GMT\r\n"
"Content-Length: 54\r\n"
"Date: Sat, 18 Feb 2017 14:30:13 GMT\r\n"
"Server: lighttpd/1.4.33\r\n"
"\r\n"
"<html><head><title></title></head><body></body></html>",
"HTTP", 1, 1, 200, "OK",
"<html><head><title></title></head><body></body></html>",
"content-type", "text/html",
"accept-ranges", "bytes",
"etag", "\"1908074049\"",
"last-modified", "Sat, 21 Apr 2012 09:42:46 GMT",
"content-length", "54",
"date", "Sat, 18 Feb 2017 14:30:13 GMT",
"Server", "lighttpd/1.4.33",
nullptr));
RUN(good_resp("linux_newline",
"HTTP/1.0 200 OK\n"
"OS: Unix\n"
"\n",
"HTTP", 1, 0, 200, "OK",
"",
"os", "Unix",
nullptr));
RUN(good_resp("mac_newline",
"HTTP/1.0 200 OK\r"
"OS: Unix\r"
"\r",
"HTTP", 1, 0, 200, "OK",
"",
"os", "Unix",
nullptr));
RUN(good_resp("empty_status_msg",
"HTTP/1.0 200 \r\n"
"\r\n",
"HTTP", 1, 0, 200, "",
"",
nullptr));
RUN(good_resp("empty_status_msg_without_separator",
"HTTP/1.0 200\r\n"
"\r\n",
"HTTP", 1, 0, 200, "",
"",
nullptr));
RUN(good_resp("continue_header",
"HTTP/1.0 200 OK\r\n"
"X: foo\r\n"
" bar\r\n"
"Y: test\r\n"
"\r\n",
"HTTP", 1, 0, 200, "OK",
"",
"X", "foo,bar",
"Y", "test",
nullptr));
RUN(good_resp("header_missing_space",
"HTTP/1.0 200 OK\r\n"
"X:foo\r\n"
"\r\n",
"HTTP", 1, 0, 200, "OK",
"",
"X", "foo",
nullptr));
RUN(good_req("minimal_request",
"GET / HTTP/1.0\r\n"
"\r\n",
"GET", "/", "HTTP", 1, 0,
"",
nullptr));
RUN(short_resp("empty", ""));
RUN(bad_resp("missing_version", "HTTP 200 OK\r\n\r\n"));
RUN(bad_resp("missing_minor", "HTTP/1 200 OK\r\n\r\n"));
RUN(bad_resp("missing_bad_major", "HTTP/X.1 200 OK\r\n\r\n"));
RUN(bad_resp("missing_bad_minor", "HTTP/1.Y 200 OK\r\n\r\n"));
RUN(bad_resp("missing_bad_status", "HTTP/1.0 000 OK\r\n\r\n"));
RUN(short_resp("missing_end", "HTTP/1.0 200 OK\r\n"));
RUN(req("basic",
"GET / HTTP/1.1\r\n"
"Host: example.org\r\n"
"User-Agent: curl/7.52.1\r\n"
"Accept: */*\r\n"
"\r\n",
"GET", "/", "HTTP", 1, 1, nullptr,
"host", "example.org",
"user-agent", "curl/7.52.1",
"accept", "*/*",
nullptr));
RUN(req("short",
"GET / HTTP/1.0\r\n"
"\r\n",
"GET", "/", "HTTP", 1, 0, nullptr,
nullptr));
RUN(req("post",
"POST / HTTP/1.1\r\n"
"Host: example.org\r\n"
"Content-Length: 3\r\n"
"\r\n"
"foo",
"POST", "/", "HTTP", 1, 1, "foo",
"host", "example.org",
nullptr));
RUN(req("post_with_set_content_length",
"POST / HTTP/1.1\r\n"
"Host: example.org\r\n"
"Content-Length: 6\r\n"
"\r\n"
"foo",
"POST", "/", "HTTP", 1, 1, "foo",
"host", "example.org",
"content-length", "6",
nullptr));
RUN(req("proxy_get",
"GET http://example.org/foo HTTP/1.1\r\n"
"Host: example.org\r\n"
"\r\n",
"GET", "http://example.org/foo", "HTTP", 1, 1, nullptr,
"host", "example.org",
nullptr));
RUN(req("multiline_header",
"GET / HTTP/1.1\r\n"
"Host: example.org\r\n"
"X: foo\r\n"
" bar\r\n"
"\r\n",
"GET", "/", "HTTP", 1, 1, nullptr,
"host", "example.org",
"x", "foo",
"", "bar",
nullptr));
RUN(resp("minimal_response",
"HTTP/1.0 500 Server error\r\n"
"Connection: close\r\n"
"\r\n",
"HTTP", 1, 0, 500, "Server error", nullptr,
"connection", "close",
nullptr));
RUN(tokens("HTTP/1.1 200 OK\r\n"
"\r\n", "Transfer-Encoding", nullptr));
RUN(tokens("HTTP/1.1 200 OK\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n", "Transfer-Encoding", "chunked", nullptr));
RUN(tokens("HTTP/1.1 200 OK\r\n"
"Transfer-Encoding: chunked, stuff; param=foo\r\n"
"\r\n", "Transfer-Encoding", "chunked", "stuff", nullptr));
RUN(tokens("HTTP/1.1 200 OK\r\n"
"Transfer-Encoding: chunked, stuff; param=\"foo\"\r\n"
"\r\n", "Transfer-Encoding", "chunked", "stuff", nullptr));
RUN(tokens("HTTP/1.1 200 OK\r\n"
"Transfer-Encoding: chunked;param=\"\\\"\",stuff\r\n"
"\r\n", "Transfer-Encoding", "chunked", "stuff", nullptr));
RUN(tokens("HTTP/1.1 200 OK\r\n"
"Transfer-Encoding: chunked;p1;p2=p3;=p4, stuff\r\n"
"\r\n", "Transfer-Encoding", "chunked", "stuff", nullptr));
RUN(tokens("HTTP/1.1 200 OK\r\n"
"Transfer-Encoding: chunked,stuff\r\n"
"\r\n", "Transfer-Encoding", "chunked", "stuff", nullptr));
RUN(tokens("HTTP/1.1 200 OK\r\n"
"Transfer-Encoding: chunked\r\n"
"Transfer-Encoding: stuff\r\n"
"\r\n", "Transfer-Encoding", "chunked", "stuff", nullptr));
RUN(tokens("HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"\r\n", "Content-Type", "text", nullptr));
AFTER;
}
|