midori_http_parser 0.6.1.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.gitmodules +6 -0
  4. data/.travis.yml +33 -0
  5. data/Gemfile +2 -0
  6. data/LICENSE-MIT +20 -0
  7. data/README.md +90 -0
  8. data/Rakefile +6 -0
  9. data/bench/standalone.rb +23 -0
  10. data/bench/thin.rb +58 -0
  11. data/ext/ruby_http_parser/.gitignore +1 -0
  12. data/ext/ruby_http_parser/RubyHttpParserService.java +18 -0
  13. data/ext/ruby_http_parser/ext_help.h +18 -0
  14. data/ext/ruby_http_parser/extconf.rb +24 -0
  15. data/ext/ruby_http_parser/org/ruby_http_parser/RubyHttpParser.java +495 -0
  16. data/ext/ruby_http_parser/ruby_http_parser.c +516 -0
  17. data/ext/ruby_http_parser/vendor/.gitkeep +0 -0
  18. data/ext/ruby_http_parser/vendor/http-parser-java/AUTHORS +32 -0
  19. data/ext/ruby_http_parser/vendor/http-parser-java/LICENSE-MIT +48 -0
  20. data/ext/ruby_http_parser/vendor/http-parser-java/README.md +183 -0
  21. data/ext/ruby_http_parser/vendor/http-parser-java/TODO +28 -0
  22. data/ext/ruby_http_parser/vendor/http-parser-java/build.xml +74 -0
  23. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.c +2175 -0
  24. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.gyp +79 -0
  25. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.h +304 -0
  26. data/ext/ruby_http_parser/vendor/http-parser-java/src/Http-parser.java.iml +22 -0
  27. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/FieldData.java +41 -0
  28. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPCallback.java +8 -0
  29. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPDataCallback.java +34 -0
  30. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPErrorCallback.java +12 -0
  31. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPException.java +9 -0
  32. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPMethod.java +113 -0
  33. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPParser.java +36 -0
  34. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPParserUrl.java +76 -0
  35. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/ParserSettings.java +256 -0
  36. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/ParserType.java +13 -0
  37. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/Util.java +111 -0
  38. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPCallback.java +5 -0
  39. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPDataCallback.java +25 -0
  40. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPErrorCallback.java +7 -0
  41. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPParser.java +2171 -0
  42. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/ParserSettings.java +83 -0
  43. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Message.java +374 -0
  44. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/ParseUrl.java +51 -0
  45. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Requests.java +69 -0
  46. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Responses.java +52 -0
  47. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Test.java +16 -0
  48. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestHeaderOverflowError.java +48 -0
  49. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestLoaderNG.java +212 -0
  50. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestNoOverflowLongBody.java +62 -0
  51. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/UnitTest.java +117 -0
  52. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Upgrade.java +27 -0
  53. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Url.java +127 -0
  54. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Util.java +236 -0
  55. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/WrongContentLength.java +59 -0
  56. data/ext/ruby_http_parser/vendor/http-parser-java/test.c +3425 -0
  57. data/ext/ruby_http_parser/vendor/http-parser-java/tests.dumped +845 -0
  58. data/ext/ruby_http_parser/vendor/http-parser-java/tests.utf8 +17 -0
  59. data/ext/ruby_http_parser/vendor/http-parser-java/tools/byte_constants.rb +6 -0
  60. data/ext/ruby_http_parser/vendor/http-parser-java/tools/const_char.rb +13 -0
  61. data/ext/ruby_http_parser/vendor/http-parser-java/tools/lowcase.rb +15 -0
  62. data/ext/ruby_http_parser/vendor/http-parser-java/tools/parse_tests.rb +33 -0
  63. data/ext/ruby_http_parser/vendor/http-parser/AUTHORS +68 -0
  64. data/ext/ruby_http_parser/vendor/http-parser/LICENSE-MIT +23 -0
  65. data/ext/ruby_http_parser/vendor/http-parser/README.md +246 -0
  66. data/ext/ruby_http_parser/vendor/http-parser/bench.c +111 -0
  67. data/ext/ruby_http_parser/vendor/http-parser/contrib/parsertrace.c +160 -0
  68. data/ext/ruby_http_parser/vendor/http-parser/contrib/url_parser.c +47 -0
  69. data/ext/ruby_http_parser/vendor/http-parser/http_parser.c +2470 -0
  70. data/ext/ruby_http_parser/vendor/http-parser/http_parser.gyp +111 -0
  71. data/ext/ruby_http_parser/vendor/http-parser/http_parser.h +432 -0
  72. data/ext/ruby_http_parser/vendor/http-parser/test.c +4226 -0
  73. data/ext/ruby_http_parser/vendor/http-parser/test_fast +0 -0
  74. data/ext/ruby_http_parser/vendor/http-parser/test_g +0 -0
  75. data/lib/http/parser.rb +1 -0
  76. data/lib/http_parser.rb +21 -0
  77. data/midori_http_parser.gemspec +24 -0
  78. data/spec/parser_spec.rb +376 -0
  79. data/spec/spec_helper.rb +1 -0
  80. data/spec/support/requests.json +631 -0
  81. data/spec/support/responses.json +375 -0
  82. data/tasks/compile.rake +42 -0
  83. data/tasks/fixtures.rake +71 -0
  84. data/tasks/spec.rake +5 -0
  85. data/tasks/submodules.rake +7 -0
  86. metadata +206 -0
@@ -0,0 +1,4226 @@
1
+ /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to
5
+ * deal in the Software without restriction, including without limitation the
6
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ * sell copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19
+ * IN THE SOFTWARE.
20
+ */
21
+ #include "http_parser.h"
22
+ #include <stdlib.h>
23
+ #include <assert.h>
24
+ #include <stdio.h>
25
+ #include <stdlib.h> /* rand */
26
+ #include <string.h>
27
+ #include <stdarg.h>
28
+
29
+ #if defined(__APPLE__)
30
+ # undef strlcat
31
+ # undef strlncpy
32
+ # undef strlcpy
33
+ #endif /* defined(__APPLE__) */
34
+
35
+ #undef TRUE
36
+ #define TRUE 1
37
+ #undef FALSE
38
+ #define FALSE 0
39
+
40
+ #define MAX_HEADERS 13
41
+ #define MAX_ELEMENT_SIZE 2048
42
+ #define MAX_CHUNKS 16
43
+
44
+ #define MIN(a,b) ((a) < (b) ? (a) : (b))
45
+
46
+ static http_parser *parser;
47
+
48
+ struct message {
49
+ const char *name; // for debugging purposes
50
+ const char *raw;
51
+ enum http_parser_type type;
52
+ enum http_method method;
53
+ int status_code;
54
+ char response_status[MAX_ELEMENT_SIZE];
55
+ char request_path[MAX_ELEMENT_SIZE];
56
+ char request_url[MAX_ELEMENT_SIZE];
57
+ char fragment[MAX_ELEMENT_SIZE];
58
+ char query_string[MAX_ELEMENT_SIZE];
59
+ char body[MAX_ELEMENT_SIZE];
60
+ size_t body_size;
61
+ const char *host;
62
+ const char *userinfo;
63
+ uint16_t port;
64
+ int num_headers;
65
+ enum { NONE=0, FIELD, VALUE } last_header_element;
66
+ char headers [MAX_HEADERS][2][MAX_ELEMENT_SIZE];
67
+ int should_keep_alive;
68
+
69
+ int num_chunks;
70
+ int num_chunks_complete;
71
+ int chunk_lengths[MAX_CHUNKS];
72
+
73
+ const char *upgrade; // upgraded body
74
+
75
+ unsigned short http_major;
76
+ unsigned short http_minor;
77
+
78
+ int message_begin_cb_called;
79
+ int headers_complete_cb_called;
80
+ int message_complete_cb_called;
81
+ int message_complete_on_eof;
82
+ int body_is_final;
83
+ };
84
+
85
+ static int currently_parsing_eof;
86
+
87
+ static struct message messages[5];
88
+ static int num_messages;
89
+ static http_parser_settings *current_pause_parser;
90
+
91
+ /* * R E Q U E S T S * */
92
+ const struct message requests[] =
93
+ #define CURL_GET 0
94
+ { {.name= "curl get"
95
+ ,.type= HTTP_REQUEST
96
+ ,.raw= "GET /test HTTP/1.1\r\n"
97
+ "User-Agent: curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1\r\n"
98
+ "Host: 0.0.0.0=5000\r\n"
99
+ "Accept: */*\r\n"
100
+ "\r\n"
101
+ ,.should_keep_alive= TRUE
102
+ ,.message_complete_on_eof= FALSE
103
+ ,.http_major= 1
104
+ ,.http_minor= 1
105
+ ,.method= HTTP_GET
106
+ ,.query_string= ""
107
+ ,.fragment= ""
108
+ ,.request_path= "/test"
109
+ ,.request_url= "/test"
110
+ ,.num_headers= 3
111
+ ,.headers=
112
+ { { "User-Agent", "curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1" }
113
+ , { "Host", "0.0.0.0=5000" }
114
+ , { "Accept", "*/*" }
115
+ }
116
+ ,.body= ""
117
+ }
118
+
119
+ #define FIREFOX_GET 1
120
+ , {.name= "firefox get"
121
+ ,.type= HTTP_REQUEST
122
+ ,.raw= "GET /favicon.ico HTTP/1.1\r\n"
123
+ "Host: 0.0.0.0=5000\r\n"
124
+ "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0\r\n"
125
+ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
126
+ "Accept-Language: en-us,en;q=0.5\r\n"
127
+ "Accept-Encoding: gzip,deflate\r\n"
128
+ "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
129
+ "Keep-Alive: 300\r\n"
130
+ "Connection: keep-alive\r\n"
131
+ "\r\n"
132
+ ,.should_keep_alive= TRUE
133
+ ,.message_complete_on_eof= FALSE
134
+ ,.http_major= 1
135
+ ,.http_minor= 1
136
+ ,.method= HTTP_GET
137
+ ,.query_string= ""
138
+ ,.fragment= ""
139
+ ,.request_path= "/favicon.ico"
140
+ ,.request_url= "/favicon.ico"
141
+ ,.num_headers= 8
142
+ ,.headers=
143
+ { { "Host", "0.0.0.0=5000" }
144
+ , { "User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0" }
145
+ , { "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" }
146
+ , { "Accept-Language", "en-us,en;q=0.5" }
147
+ , { "Accept-Encoding", "gzip,deflate" }
148
+ , { "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7" }
149
+ , { "Keep-Alive", "300" }
150
+ , { "Connection", "keep-alive" }
151
+ }
152
+ ,.body= ""
153
+ }
154
+
155
+ #define DUMBFUCK 2
156
+ , {.name= "dumbfuck"
157
+ ,.type= HTTP_REQUEST
158
+ ,.raw= "GET /dumbfuck HTTP/1.1\r\n"
159
+ "aaaaaaaaaaaaa:++++++++++\r\n"
160
+ "\r\n"
161
+ ,.should_keep_alive= TRUE
162
+ ,.message_complete_on_eof= FALSE
163
+ ,.http_major= 1
164
+ ,.http_minor= 1
165
+ ,.method= HTTP_GET
166
+ ,.query_string= ""
167
+ ,.fragment= ""
168
+ ,.request_path= "/dumbfuck"
169
+ ,.request_url= "/dumbfuck"
170
+ ,.num_headers= 1
171
+ ,.headers=
172
+ { { "aaaaaaaaaaaaa", "++++++++++" }
173
+ }
174
+ ,.body= ""
175
+ }
176
+
177
+ #define FRAGMENT_IN_URI 3
178
+ , {.name= "fragment in url"
179
+ ,.type= HTTP_REQUEST
180
+ ,.raw= "GET /forums/1/topics/2375?page=1#posts-17408 HTTP/1.1\r\n"
181
+ "\r\n"
182
+ ,.should_keep_alive= TRUE
183
+ ,.message_complete_on_eof= FALSE
184
+ ,.http_major= 1
185
+ ,.http_minor= 1
186
+ ,.method= HTTP_GET
187
+ ,.query_string= "page=1"
188
+ ,.fragment= "posts-17408"
189
+ ,.request_path= "/forums/1/topics/2375"
190
+ /* XXX request url does include fragment? */
191
+ ,.request_url= "/forums/1/topics/2375?page=1#posts-17408"
192
+ ,.num_headers= 0
193
+ ,.body= ""
194
+ }
195
+
196
+ #define GET_NO_HEADERS_NO_BODY 4
197
+ , {.name= "get no headers no body"
198
+ ,.type= HTTP_REQUEST
199
+ ,.raw= "GET /get_no_headers_no_body/world HTTP/1.1\r\n"
200
+ "\r\n"
201
+ ,.should_keep_alive= TRUE
202
+ ,.message_complete_on_eof= FALSE /* would need Connection: close */
203
+ ,.http_major= 1
204
+ ,.http_minor= 1
205
+ ,.method= HTTP_GET
206
+ ,.query_string= ""
207
+ ,.fragment= ""
208
+ ,.request_path= "/get_no_headers_no_body/world"
209
+ ,.request_url= "/get_no_headers_no_body/world"
210
+ ,.num_headers= 0
211
+ ,.body= ""
212
+ }
213
+
214
+ #define GET_ONE_HEADER_NO_BODY 5
215
+ , {.name= "get one header no body"
216
+ ,.type= HTTP_REQUEST
217
+ ,.raw= "GET /get_one_header_no_body HTTP/1.1\r\n"
218
+ "Accept: */*\r\n"
219
+ "\r\n"
220
+ ,.should_keep_alive= TRUE
221
+ ,.message_complete_on_eof= FALSE /* would need Connection: close */
222
+ ,.http_major= 1
223
+ ,.http_minor= 1
224
+ ,.method= HTTP_GET
225
+ ,.query_string= ""
226
+ ,.fragment= ""
227
+ ,.request_path= "/get_one_header_no_body"
228
+ ,.request_url= "/get_one_header_no_body"
229
+ ,.num_headers= 1
230
+ ,.headers=
231
+ { { "Accept" , "*/*" }
232
+ }
233
+ ,.body= ""
234
+ }
235
+
236
+ #define GET_FUNKY_CONTENT_LENGTH 6
237
+ , {.name= "get funky content length body hello"
238
+ ,.type= HTTP_REQUEST
239
+ ,.raw= "GET /get_funky_content_length_body_hello HTTP/1.0\r\n"
240
+ "conTENT-Length: 5\r\n"
241
+ "\r\n"
242
+ "HELLO"
243
+ ,.should_keep_alive= FALSE
244
+ ,.message_complete_on_eof= FALSE
245
+ ,.http_major= 1
246
+ ,.http_minor= 0
247
+ ,.method= HTTP_GET
248
+ ,.query_string= ""
249
+ ,.fragment= ""
250
+ ,.request_path= "/get_funky_content_length_body_hello"
251
+ ,.request_url= "/get_funky_content_length_body_hello"
252
+ ,.num_headers= 1
253
+ ,.headers=
254
+ { { "conTENT-Length" , "5" }
255
+ }
256
+ ,.body= "HELLO"
257
+ }
258
+
259
+ #define POST_IDENTITY_BODY_WORLD 7
260
+ , {.name= "post identity body world"
261
+ ,.type= HTTP_REQUEST
262
+ ,.raw= "POST /post_identity_body_world?q=search#hey HTTP/1.1\r\n"
263
+ "Accept: */*\r\n"
264
+ "Transfer-Encoding: identity\r\n"
265
+ "Content-Length: 5\r\n"
266
+ "\r\n"
267
+ "World"
268
+ ,.should_keep_alive= TRUE
269
+ ,.message_complete_on_eof= FALSE
270
+ ,.http_major= 1
271
+ ,.http_minor= 1
272
+ ,.method= HTTP_POST
273
+ ,.query_string= "q=search"
274
+ ,.fragment= "hey"
275
+ ,.request_path= "/post_identity_body_world"
276
+ ,.request_url= "/post_identity_body_world?q=search#hey"
277
+ ,.num_headers= 3
278
+ ,.headers=
279
+ { { "Accept", "*/*" }
280
+ , { "Transfer-Encoding", "identity" }
281
+ , { "Content-Length", "5" }
282
+ }
283
+ ,.body= "World"
284
+ }
285
+
286
+ #define POST_CHUNKED_ALL_YOUR_BASE 8
287
+ , {.name= "post - chunked body: all your base are belong to us"
288
+ ,.type= HTTP_REQUEST
289
+ ,.raw= "POST /post_chunked_all_your_base HTTP/1.1\r\n"
290
+ "Transfer-Encoding: chunked\r\n"
291
+ "\r\n"
292
+ "1e\r\nall your base are belong to us\r\n"
293
+ "0\r\n"
294
+ "\r\n"
295
+ ,.should_keep_alive= TRUE
296
+ ,.message_complete_on_eof= FALSE
297
+ ,.http_major= 1
298
+ ,.http_minor= 1
299
+ ,.method= HTTP_POST
300
+ ,.query_string= ""
301
+ ,.fragment= ""
302
+ ,.request_path= "/post_chunked_all_your_base"
303
+ ,.request_url= "/post_chunked_all_your_base"
304
+ ,.num_headers= 1
305
+ ,.headers=
306
+ { { "Transfer-Encoding" , "chunked" }
307
+ }
308
+ ,.body= "all your base are belong to us"
309
+ ,.num_chunks_complete= 2
310
+ ,.chunk_lengths= { 0x1e }
311
+ }
312
+
313
+ #define TWO_CHUNKS_MULT_ZERO_END 9
314
+ , {.name= "two chunks ; triple zero ending"
315
+ ,.type= HTTP_REQUEST
316
+ ,.raw= "POST /two_chunks_mult_zero_end HTTP/1.1\r\n"
317
+ "Transfer-Encoding: chunked\r\n"
318
+ "\r\n"
319
+ "5\r\nhello\r\n"
320
+ "6\r\n world\r\n"
321
+ "000\r\n"
322
+ "\r\n"
323
+ ,.should_keep_alive= TRUE
324
+ ,.message_complete_on_eof= FALSE
325
+ ,.http_major= 1
326
+ ,.http_minor= 1
327
+ ,.method= HTTP_POST
328
+ ,.query_string= ""
329
+ ,.fragment= ""
330
+ ,.request_path= "/two_chunks_mult_zero_end"
331
+ ,.request_url= "/two_chunks_mult_zero_end"
332
+ ,.num_headers= 1
333
+ ,.headers=
334
+ { { "Transfer-Encoding", "chunked" }
335
+ }
336
+ ,.body= "hello world"
337
+ ,.num_chunks_complete= 3
338
+ ,.chunk_lengths= { 5, 6 }
339
+ }
340
+
341
+ #define CHUNKED_W_TRAILING_HEADERS 10
342
+ , {.name= "chunked with trailing headers. blech."
343
+ ,.type= HTTP_REQUEST
344
+ ,.raw= "POST /chunked_w_trailing_headers HTTP/1.1\r\n"
345
+ "Transfer-Encoding: chunked\r\n"
346
+ "\r\n"
347
+ "5\r\nhello\r\n"
348
+ "6\r\n world\r\n"
349
+ "0\r\n"
350
+ "Vary: *\r\n"
351
+ "Content-Type: text/plain\r\n"
352
+ "\r\n"
353
+ ,.should_keep_alive= TRUE
354
+ ,.message_complete_on_eof= FALSE
355
+ ,.http_major= 1
356
+ ,.http_minor= 1
357
+ ,.method= HTTP_POST
358
+ ,.query_string= ""
359
+ ,.fragment= ""
360
+ ,.request_path= "/chunked_w_trailing_headers"
361
+ ,.request_url= "/chunked_w_trailing_headers"
362
+ ,.num_headers= 3
363
+ ,.headers=
364
+ { { "Transfer-Encoding", "chunked" }
365
+ , { "Vary", "*" }
366
+ , { "Content-Type", "text/plain" }
367
+ }
368
+ ,.body= "hello world"
369
+ ,.num_chunks_complete= 3
370
+ ,.chunk_lengths= { 5, 6 }
371
+ }
372
+
373
+ #define CHUNKED_W_BULLSHIT_AFTER_LENGTH 11
374
+ , {.name= "with bullshit after the length"
375
+ ,.type= HTTP_REQUEST
376
+ ,.raw= "POST /chunked_w_bullshit_after_length HTTP/1.1\r\n"
377
+ "Transfer-Encoding: chunked\r\n"
378
+ "\r\n"
379
+ "5; ihatew3;whatthefuck=aretheseparametersfor\r\nhello\r\n"
380
+ "6; blahblah; blah\r\n world\r\n"
381
+ "0\r\n"
382
+ "\r\n"
383
+ ,.should_keep_alive= TRUE
384
+ ,.message_complete_on_eof= FALSE
385
+ ,.http_major= 1
386
+ ,.http_minor= 1
387
+ ,.method= HTTP_POST
388
+ ,.query_string= ""
389
+ ,.fragment= ""
390
+ ,.request_path= "/chunked_w_bullshit_after_length"
391
+ ,.request_url= "/chunked_w_bullshit_after_length"
392
+ ,.num_headers= 1
393
+ ,.headers=
394
+ { { "Transfer-Encoding", "chunked" }
395
+ }
396
+ ,.body= "hello world"
397
+ ,.num_chunks_complete= 3
398
+ ,.chunk_lengths= { 5, 6 }
399
+ }
400
+
401
+ #define WITH_QUOTES 12
402
+ , {.name= "with quotes"
403
+ ,.type= HTTP_REQUEST
404
+ ,.raw= "GET /with_\"stupid\"_quotes?foo=\"bar\" HTTP/1.1\r\n\r\n"
405
+ ,.should_keep_alive= TRUE
406
+ ,.message_complete_on_eof= FALSE
407
+ ,.http_major= 1
408
+ ,.http_minor= 1
409
+ ,.method= HTTP_GET
410
+ ,.query_string= "foo=\"bar\""
411
+ ,.fragment= ""
412
+ ,.request_path= "/with_\"stupid\"_quotes"
413
+ ,.request_url= "/with_\"stupid\"_quotes?foo=\"bar\""
414
+ ,.num_headers= 0
415
+ ,.headers= { }
416
+ ,.body= ""
417
+ }
418
+
419
+ #define APACHEBENCH_GET 13
420
+ /* The server receiving this request SHOULD NOT wait for EOF
421
+ * to know that content-length == 0.
422
+ * How to represent this in a unit test? message_complete_on_eof
423
+ * Compare with NO_CONTENT_LENGTH_RESPONSE.
424
+ */
425
+ , {.name = "apachebench get"
426
+ ,.type= HTTP_REQUEST
427
+ ,.raw= "GET /test HTTP/1.0\r\n"
428
+ "Host: 0.0.0.0:5000\r\n"
429
+ "User-Agent: ApacheBench/2.3\r\n"
430
+ "Accept: */*\r\n\r\n"
431
+ ,.should_keep_alive= FALSE
432
+ ,.message_complete_on_eof= FALSE
433
+ ,.http_major= 1
434
+ ,.http_minor= 0
435
+ ,.method= HTTP_GET
436
+ ,.query_string= ""
437
+ ,.fragment= ""
438
+ ,.request_path= "/test"
439
+ ,.request_url= "/test"
440
+ ,.num_headers= 3
441
+ ,.headers= { { "Host", "0.0.0.0:5000" }
442
+ , { "User-Agent", "ApacheBench/2.3" }
443
+ , { "Accept", "*/*" }
444
+ }
445
+ ,.body= ""
446
+ }
447
+
448
+ #define QUERY_URL_WITH_QUESTION_MARK_GET 14
449
+ /* Some clients include '?' characters in query strings.
450
+ */
451
+ , {.name = "query url with question mark"
452
+ ,.type= HTTP_REQUEST
453
+ ,.raw= "GET /test.cgi?foo=bar?baz HTTP/1.1\r\n\r\n"
454
+ ,.should_keep_alive= TRUE
455
+ ,.message_complete_on_eof= FALSE
456
+ ,.http_major= 1
457
+ ,.http_minor= 1
458
+ ,.method= HTTP_GET
459
+ ,.query_string= "foo=bar?baz"
460
+ ,.fragment= ""
461
+ ,.request_path= "/test.cgi"
462
+ ,.request_url= "/test.cgi?foo=bar?baz"
463
+ ,.num_headers= 0
464
+ ,.headers= {}
465
+ ,.body= ""
466
+ }
467
+
468
+ #define PREFIX_NEWLINE_GET 15
469
+ /* Some clients, especially after a POST in a keep-alive connection,
470
+ * will send an extra CRLF before the next request
471
+ */
472
+ , {.name = "newline prefix get"
473
+ ,.type= HTTP_REQUEST
474
+ ,.raw= "\r\nGET /test HTTP/1.1\r\n\r\n"
475
+ ,.should_keep_alive= TRUE
476
+ ,.message_complete_on_eof= FALSE
477
+ ,.http_major= 1
478
+ ,.http_minor= 1
479
+ ,.method= HTTP_GET
480
+ ,.query_string= ""
481
+ ,.fragment= ""
482
+ ,.request_path= "/test"
483
+ ,.request_url= "/test"
484
+ ,.num_headers= 0
485
+ ,.headers= { }
486
+ ,.body= ""
487
+ }
488
+
489
+ #define UPGRADE_REQUEST 16
490
+ , {.name = "upgrade request"
491
+ ,.type= HTTP_REQUEST
492
+ ,.raw= "GET /demo HTTP/1.1\r\n"
493
+ "Host: example.com\r\n"
494
+ "Connection: Upgrade\r\n"
495
+ "Sec-WebSocket-Key2: 12998 5 Y3 1 .P00\r\n"
496
+ "Sec-WebSocket-Protocol: sample\r\n"
497
+ "Upgrade: WebSocket\r\n"
498
+ "Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n"
499
+ "Origin: http://example.com\r\n"
500
+ "\r\n"
501
+ "Hot diggity dogg"
502
+ ,.should_keep_alive= TRUE
503
+ ,.message_complete_on_eof= FALSE
504
+ ,.http_major= 1
505
+ ,.http_minor= 1
506
+ ,.method= HTTP_GET
507
+ ,.query_string= ""
508
+ ,.fragment= ""
509
+ ,.request_path= "/demo"
510
+ ,.request_url= "/demo"
511
+ ,.num_headers= 7
512
+ ,.upgrade="Hot diggity dogg"
513
+ ,.headers= { { "Host", "example.com" }
514
+ , { "Connection", "Upgrade" }
515
+ , { "Sec-WebSocket-Key2", "12998 5 Y3 1 .P00" }
516
+ , { "Sec-WebSocket-Protocol", "sample" }
517
+ , { "Upgrade", "WebSocket" }
518
+ , { "Sec-WebSocket-Key1", "4 @1 46546xW%0l 1 5" }
519
+ , { "Origin", "http://example.com" }
520
+ }
521
+ ,.body= ""
522
+ }
523
+
524
+ #define CONNECT_REQUEST 17
525
+ , {.name = "connect request"
526
+ ,.type= HTTP_REQUEST
527
+ ,.raw= "CONNECT 0-home0.netscape.com:443 HTTP/1.0\r\n"
528
+ "User-agent: Mozilla/1.1N\r\n"
529
+ "Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n"
530
+ "\r\n"
531
+ "some data\r\n"
532
+ "and yet even more data"
533
+ ,.should_keep_alive= FALSE
534
+ ,.message_complete_on_eof= FALSE
535
+ ,.http_major= 1
536
+ ,.http_minor= 0
537
+ ,.method= HTTP_CONNECT
538
+ ,.query_string= ""
539
+ ,.fragment= ""
540
+ ,.request_path= ""
541
+ ,.request_url= "0-home0.netscape.com:443"
542
+ ,.num_headers= 2
543
+ ,.upgrade="some data\r\nand yet even more data"
544
+ ,.headers= { { "User-agent", "Mozilla/1.1N" }
545
+ , { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" }
546
+ }
547
+ ,.body= ""
548
+ }
549
+
550
+ #define REPORT_REQ 18
551
+ , {.name= "report request"
552
+ ,.type= HTTP_REQUEST
553
+ ,.raw= "REPORT /test HTTP/1.1\r\n"
554
+ "\r\n"
555
+ ,.should_keep_alive= TRUE
556
+ ,.message_complete_on_eof= FALSE
557
+ ,.http_major= 1
558
+ ,.http_minor= 1
559
+ ,.method= HTTP_REPORT
560
+ ,.query_string= ""
561
+ ,.fragment= ""
562
+ ,.request_path= "/test"
563
+ ,.request_url= "/test"
564
+ ,.num_headers= 0
565
+ ,.headers= {}
566
+ ,.body= ""
567
+ }
568
+
569
+ #define NO_HTTP_VERSION 19
570
+ , {.name= "request with no http version"
571
+ ,.type= HTTP_REQUEST
572
+ ,.raw= "GET /\r\n"
573
+ "\r\n"
574
+ ,.should_keep_alive= FALSE
575
+ ,.message_complete_on_eof= FALSE
576
+ ,.http_major= 0
577
+ ,.http_minor= 9
578
+ ,.method= HTTP_GET
579
+ ,.query_string= ""
580
+ ,.fragment= ""
581
+ ,.request_path= "/"
582
+ ,.request_url= "/"
583
+ ,.num_headers= 0
584
+ ,.headers= {}
585
+ ,.body= ""
586
+ }
587
+
588
+ #define MSEARCH_REQ 20
589
+ , {.name= "m-search request"
590
+ ,.type= HTTP_REQUEST
591
+ ,.raw= "M-SEARCH * HTTP/1.1\r\n"
592
+ "HOST: 239.255.255.250:1900\r\n"
593
+ "MAN: \"ssdp:discover\"\r\n"
594
+ "ST: \"ssdp:all\"\r\n"
595
+ "\r\n"
596
+ ,.should_keep_alive= TRUE
597
+ ,.message_complete_on_eof= FALSE
598
+ ,.http_major= 1
599
+ ,.http_minor= 1
600
+ ,.method= HTTP_MSEARCH
601
+ ,.query_string= ""
602
+ ,.fragment= ""
603
+ ,.request_path= "*"
604
+ ,.request_url= "*"
605
+ ,.num_headers= 3
606
+ ,.headers= { { "HOST", "239.255.255.250:1900" }
607
+ , { "MAN", "\"ssdp:discover\"" }
608
+ , { "ST", "\"ssdp:all\"" }
609
+ }
610
+ ,.body= ""
611
+ }
612
+
613
+ #define LINE_FOLDING_IN_HEADER 21
614
+ , {.name= "line folding in header value"
615
+ ,.type= HTTP_REQUEST
616
+ ,.raw= "GET / HTTP/1.1\r\n"
617
+ "Line1: abc\r\n"
618
+ "\tdef\r\n"
619
+ " ghi\r\n"
620
+ "\t\tjkl\r\n"
621
+ " mno \r\n"
622
+ "\t \tqrs\r\n"
623
+ "Line2: \t line2\t\r\n"
624
+ "Line3:\r\n"
625
+ " line3\r\n"
626
+ "Line4: \r\n"
627
+ " \r\n"
628
+ "Connection:\r\n"
629
+ " close\r\n"
630
+ "\r\n"
631
+ ,.should_keep_alive= FALSE
632
+ ,.message_complete_on_eof= FALSE
633
+ ,.http_major= 1
634
+ ,.http_minor= 1
635
+ ,.method= HTTP_GET
636
+ ,.query_string= ""
637
+ ,.fragment= ""
638
+ ,.request_path= "/"
639
+ ,.request_url= "/"
640
+ ,.num_headers= 5
641
+ ,.headers= { { "Line1", "abc\tdef ghi\t\tjkl mno \t \tqrs" }
642
+ , { "Line2", "line2\t" }
643
+ , { "Line3", "line3" }
644
+ , { "Line4", "" }
645
+ , { "Connection", "close" },
646
+ }
647
+ ,.body= ""
648
+ }
649
+
650
+
651
+ #define QUERY_TERMINATED_HOST 22
652
+ , {.name= "host terminated by a query string"
653
+ ,.type= HTTP_REQUEST
654
+ ,.raw= "GET http://hypnotoad.org?hail=all HTTP/1.1\r\n"
655
+ "\r\n"
656
+ ,.should_keep_alive= TRUE
657
+ ,.message_complete_on_eof= FALSE
658
+ ,.http_major= 1
659
+ ,.http_minor= 1
660
+ ,.method= HTTP_GET
661
+ ,.query_string= "hail=all"
662
+ ,.fragment= ""
663
+ ,.request_path= ""
664
+ ,.request_url= "http://hypnotoad.org?hail=all"
665
+ ,.host= "hypnotoad.org"
666
+ ,.num_headers= 0
667
+ ,.headers= { }
668
+ ,.body= ""
669
+ }
670
+
671
+ #define QUERY_TERMINATED_HOSTPORT 23
672
+ , {.name= "host:port terminated by a query string"
673
+ ,.type= HTTP_REQUEST
674
+ ,.raw= "GET http://hypnotoad.org:1234?hail=all HTTP/1.1\r\n"
675
+ "\r\n"
676
+ ,.should_keep_alive= TRUE
677
+ ,.message_complete_on_eof= FALSE
678
+ ,.http_major= 1
679
+ ,.http_minor= 1
680
+ ,.method= HTTP_GET
681
+ ,.query_string= "hail=all"
682
+ ,.fragment= ""
683
+ ,.request_path= ""
684
+ ,.request_url= "http://hypnotoad.org:1234?hail=all"
685
+ ,.host= "hypnotoad.org"
686
+ ,.port= 1234
687
+ ,.num_headers= 0
688
+ ,.headers= { }
689
+ ,.body= ""
690
+ }
691
+
692
+ #define SPACE_TERMINATED_HOSTPORT 24
693
+ , {.name= "host:port terminated by a space"
694
+ ,.type= HTTP_REQUEST
695
+ ,.raw= "GET http://hypnotoad.org:1234 HTTP/1.1\r\n"
696
+ "\r\n"
697
+ ,.should_keep_alive= TRUE
698
+ ,.message_complete_on_eof= FALSE
699
+ ,.http_major= 1
700
+ ,.http_minor= 1
701
+ ,.method= HTTP_GET
702
+ ,.query_string= ""
703
+ ,.fragment= ""
704
+ ,.request_path= ""
705
+ ,.request_url= "http://hypnotoad.org:1234"
706
+ ,.host= "hypnotoad.org"
707
+ ,.port= 1234
708
+ ,.num_headers= 0
709
+ ,.headers= { }
710
+ ,.body= ""
711
+ }
712
+
713
+ #define PATCH_REQ 25
714
+ , {.name = "PATCH request"
715
+ ,.type= HTTP_REQUEST
716
+ ,.raw= "PATCH /file.txt HTTP/1.1\r\n"
717
+ "Host: www.example.com\r\n"
718
+ "Content-Type: application/example\r\n"
719
+ "If-Match: \"e0023aa4e\"\r\n"
720
+ "Content-Length: 10\r\n"
721
+ "\r\n"
722
+ "cccccccccc"
723
+ ,.should_keep_alive= TRUE
724
+ ,.message_complete_on_eof= FALSE
725
+ ,.http_major= 1
726
+ ,.http_minor= 1
727
+ ,.method= HTTP_PATCH
728
+ ,.query_string= ""
729
+ ,.fragment= ""
730
+ ,.request_path= "/file.txt"
731
+ ,.request_url= "/file.txt"
732
+ ,.num_headers= 4
733
+ ,.headers= { { "Host", "www.example.com" }
734
+ , { "Content-Type", "application/example" }
735
+ , { "If-Match", "\"e0023aa4e\"" }
736
+ , { "Content-Length", "10" }
737
+ }
738
+ ,.body= "cccccccccc"
739
+ }
740
+
741
+ #define CONNECT_CAPS_REQUEST 26
742
+ , {.name = "connect caps request"
743
+ ,.type= HTTP_REQUEST
744
+ ,.raw= "CONNECT HOME0.NETSCAPE.COM:443 HTTP/1.0\r\n"
745
+ "User-agent: Mozilla/1.1N\r\n"
746
+ "Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n"
747
+ "\r\n"
748
+ ,.should_keep_alive= FALSE
749
+ ,.message_complete_on_eof= FALSE
750
+ ,.http_major= 1
751
+ ,.http_minor= 0
752
+ ,.method= HTTP_CONNECT
753
+ ,.query_string= ""
754
+ ,.fragment= ""
755
+ ,.request_path= ""
756
+ ,.request_url= "HOME0.NETSCAPE.COM:443"
757
+ ,.num_headers= 2
758
+ ,.upgrade=""
759
+ ,.headers= { { "User-agent", "Mozilla/1.1N" }
760
+ , { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" }
761
+ }
762
+ ,.body= ""
763
+ }
764
+
765
+ #if !HTTP_PARSER_STRICT
766
+ #define UTF8_PATH_REQ 27
767
+ , {.name= "utf-8 path request"
768
+ ,.type= HTTP_REQUEST
769
+ ,.raw= "GET /δ¶/δt/pope?q=1#narf HTTP/1.1\r\n"
770
+ "Host: github.com\r\n"
771
+ "\r\n"
772
+ ,.should_keep_alive= TRUE
773
+ ,.message_complete_on_eof= FALSE
774
+ ,.http_major= 1
775
+ ,.http_minor= 1
776
+ ,.method= HTTP_GET
777
+ ,.query_string= "q=1"
778
+ ,.fragment= "narf"
779
+ ,.request_path= "/δ¶/δt/pope"
780
+ ,.request_url= "/δ¶/δt/pope?q=1#narf"
781
+ ,.num_headers= 1
782
+ ,.headers= { {"Host", "github.com" }
783
+ }
784
+ ,.body= ""
785
+ }
786
+
787
+ #define HOSTNAME_UNDERSCORE 28
788
+ , {.name = "hostname underscore"
789
+ ,.type= HTTP_REQUEST
790
+ ,.raw= "CONNECT home_0.netscape.com:443 HTTP/1.0\r\n"
791
+ "User-agent: Mozilla/1.1N\r\n"
792
+ "Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n"
793
+ "\r\n"
794
+ ,.should_keep_alive= FALSE
795
+ ,.message_complete_on_eof= FALSE
796
+ ,.http_major= 1
797
+ ,.http_minor= 0
798
+ ,.method= HTTP_CONNECT
799
+ ,.query_string= ""
800
+ ,.fragment= ""
801
+ ,.request_path= ""
802
+ ,.request_url= "home_0.netscape.com:443"
803
+ ,.num_headers= 2
804
+ ,.upgrade=""
805
+ ,.headers= { { "User-agent", "Mozilla/1.1N" }
806
+ , { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" }
807
+ }
808
+ ,.body= ""
809
+ }
810
+ #endif /* !HTTP_PARSER_STRICT */
811
+
812
+ /* see https://github.com/ry/http-parser/issues/47 */
813
+ #define EAT_TRAILING_CRLF_NO_CONNECTION_CLOSE 29
814
+ , {.name = "eat CRLF between requests, no \"Connection: close\" header"
815
+ ,.raw= "POST / HTTP/1.1\r\n"
816
+ "Host: www.example.com\r\n"
817
+ "Content-Type: application/x-www-form-urlencoded\r\n"
818
+ "Content-Length: 4\r\n"
819
+ "\r\n"
820
+ "q=42\r\n" /* note the trailing CRLF */
821
+ ,.should_keep_alive= TRUE
822
+ ,.message_complete_on_eof= FALSE
823
+ ,.http_major= 1
824
+ ,.http_minor= 1
825
+ ,.method= HTTP_POST
826
+ ,.query_string= ""
827
+ ,.fragment= ""
828
+ ,.request_path= "/"
829
+ ,.request_url= "/"
830
+ ,.num_headers= 3
831
+ ,.upgrade= 0
832
+ ,.headers= { { "Host", "www.example.com" }
833
+ , { "Content-Type", "application/x-www-form-urlencoded" }
834
+ , { "Content-Length", "4" }
835
+ }
836
+ ,.body= "q=42"
837
+ }
838
+
839
+ /* see https://github.com/ry/http-parser/issues/47 */
840
+ #define EAT_TRAILING_CRLF_WITH_CONNECTION_CLOSE 30
841
+ , {.name = "eat CRLF between requests even if \"Connection: close\" is set"
842
+ ,.raw= "POST / HTTP/1.1\r\n"
843
+ "Host: www.example.com\r\n"
844
+ "Content-Type: application/x-www-form-urlencoded\r\n"
845
+ "Content-Length: 4\r\n"
846
+ "Connection: close\r\n"
847
+ "\r\n"
848
+ "q=42\r\n" /* note the trailing CRLF */
849
+ ,.should_keep_alive= FALSE
850
+ ,.message_complete_on_eof= FALSE /* input buffer isn't empty when on_message_complete is called */
851
+ ,.http_major= 1
852
+ ,.http_minor= 1
853
+ ,.method= HTTP_POST
854
+ ,.query_string= ""
855
+ ,.fragment= ""
856
+ ,.request_path= "/"
857
+ ,.request_url= "/"
858
+ ,.num_headers= 4
859
+ ,.upgrade= 0
860
+ ,.headers= { { "Host", "www.example.com" }
861
+ , { "Content-Type", "application/x-www-form-urlencoded" }
862
+ , { "Content-Length", "4" }
863
+ , { "Connection", "close" }
864
+ }
865
+ ,.body= "q=42"
866
+ }
867
+
868
+ #define PURGE_REQ 31
869
+ , {.name = "PURGE request"
870
+ ,.type= HTTP_REQUEST
871
+ ,.raw= "PURGE /file.txt HTTP/1.1\r\n"
872
+ "Host: www.example.com\r\n"
873
+ "\r\n"
874
+ ,.should_keep_alive= TRUE
875
+ ,.message_complete_on_eof= FALSE
876
+ ,.http_major= 1
877
+ ,.http_minor= 1
878
+ ,.method= HTTP_PURGE
879
+ ,.query_string= ""
880
+ ,.fragment= ""
881
+ ,.request_path= "/file.txt"
882
+ ,.request_url= "/file.txt"
883
+ ,.num_headers= 1
884
+ ,.headers= { { "Host", "www.example.com" } }
885
+ ,.body= ""
886
+ }
887
+
888
+ #define SEARCH_REQ 32
889
+ , {.name = "SEARCH request"
890
+ ,.type= HTTP_REQUEST
891
+ ,.raw= "SEARCH / HTTP/1.1\r\n"
892
+ "Host: www.example.com\r\n"
893
+ "\r\n"
894
+ ,.should_keep_alive= TRUE
895
+ ,.message_complete_on_eof= FALSE
896
+ ,.http_major= 1
897
+ ,.http_minor= 1
898
+ ,.method= HTTP_SEARCH
899
+ ,.query_string= ""
900
+ ,.fragment= ""
901
+ ,.request_path= "/"
902
+ ,.request_url= "/"
903
+ ,.num_headers= 1
904
+ ,.headers= { { "Host", "www.example.com" } }
905
+ ,.body= ""
906
+ }
907
+
908
+ #define PROXY_WITH_BASIC_AUTH 33
909
+ , {.name= "host:port and basic_auth"
910
+ ,.type= HTTP_REQUEST
911
+ ,.raw= "GET http://a%12:b!&*$@hypnotoad.org:1234/toto HTTP/1.1\r\n"
912
+ "\r\n"
913
+ ,.should_keep_alive= TRUE
914
+ ,.message_complete_on_eof= FALSE
915
+ ,.http_major= 1
916
+ ,.http_minor= 1
917
+ ,.method= HTTP_GET
918
+ ,.fragment= ""
919
+ ,.request_path= "/toto"
920
+ ,.request_url= "http://a%12:b!&*$@hypnotoad.org:1234/toto"
921
+ ,.host= "hypnotoad.org"
922
+ ,.userinfo= "a%12:b!&*$"
923
+ ,.port= 1234
924
+ ,.num_headers= 0
925
+ ,.headers= { }
926
+ ,.body= ""
927
+ }
928
+
929
+ #define LINE_FOLDING_IN_HEADER_WITH_LF 34
930
+ , {.name= "line folding in header value"
931
+ ,.type= HTTP_REQUEST
932
+ ,.raw= "GET / HTTP/1.1\n"
933
+ "Line1: abc\n"
934
+ "\tdef\n"
935
+ " ghi\n"
936
+ "\t\tjkl\n"
937
+ " mno \n"
938
+ "\t \tqrs\n"
939
+ "Line2: \t line2\t\n"
940
+ "Line3:\n"
941
+ " line3\n"
942
+ "Line4: \n"
943
+ " \n"
944
+ "Connection:\n"
945
+ " close\n"
946
+ "\n"
947
+ ,.should_keep_alive= FALSE
948
+ ,.message_complete_on_eof= FALSE
949
+ ,.http_major= 1
950
+ ,.http_minor= 1
951
+ ,.method= HTTP_GET
952
+ ,.query_string= ""
953
+ ,.fragment= ""
954
+ ,.request_path= "/"
955
+ ,.request_url= "/"
956
+ ,.num_headers= 5
957
+ ,.headers= { { "Line1", "abc\tdef ghi\t\tjkl mno \t \tqrs" }
958
+ , { "Line2", "line2\t" }
959
+ , { "Line3", "line3" }
960
+ , { "Line4", "" }
961
+ , { "Connection", "close" },
962
+ }
963
+ ,.body= ""
964
+ }
965
+
966
+ #define CONNECTION_MULTI 35
967
+ , {.name = "multiple connection header values with folding"
968
+ ,.type= HTTP_REQUEST
969
+ ,.raw= "GET /demo HTTP/1.1\r\n"
970
+ "Host: example.com\r\n"
971
+ "Connection: Something,\r\n"
972
+ " Upgrade, ,Keep-Alive\r\n"
973
+ "Sec-WebSocket-Key2: 12998 5 Y3 1 .P00\r\n"
974
+ "Sec-WebSocket-Protocol: sample\r\n"
975
+ "Upgrade: WebSocket\r\n"
976
+ "Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n"
977
+ "Origin: http://example.com\r\n"
978
+ "\r\n"
979
+ "Hot diggity dogg"
980
+ ,.should_keep_alive= TRUE
981
+ ,.message_complete_on_eof= FALSE
982
+ ,.http_major= 1
983
+ ,.http_minor= 1
984
+ ,.method= HTTP_GET
985
+ ,.query_string= ""
986
+ ,.fragment= ""
987
+ ,.request_path= "/demo"
988
+ ,.request_url= "/demo"
989
+ ,.num_headers= 7
990
+ ,.upgrade="Hot diggity dogg"
991
+ ,.headers= { { "Host", "example.com" }
992
+ , { "Connection", "Something, Upgrade, ,Keep-Alive" }
993
+ , { "Sec-WebSocket-Key2", "12998 5 Y3 1 .P00" }
994
+ , { "Sec-WebSocket-Protocol", "sample" }
995
+ , { "Upgrade", "WebSocket" }
996
+ , { "Sec-WebSocket-Key1", "4 @1 46546xW%0l 1 5" }
997
+ , { "Origin", "http://example.com" }
998
+ }
999
+ ,.body= ""
1000
+ }
1001
+
1002
+ #define CONNECTION_MULTI_LWS 36
1003
+ , {.name = "multiple connection header values with folding and lws"
1004
+ ,.type= HTTP_REQUEST
1005
+ ,.raw= "GET /demo HTTP/1.1\r\n"
1006
+ "Connection: keep-alive, upgrade\r\n"
1007
+ "Upgrade: WebSocket\r\n"
1008
+ "\r\n"
1009
+ "Hot diggity dogg"
1010
+ ,.should_keep_alive= TRUE
1011
+ ,.message_complete_on_eof= FALSE
1012
+ ,.http_major= 1
1013
+ ,.http_minor= 1
1014
+ ,.method= HTTP_GET
1015
+ ,.query_string= ""
1016
+ ,.fragment= ""
1017
+ ,.request_path= "/demo"
1018
+ ,.request_url= "/demo"
1019
+ ,.num_headers= 2
1020
+ ,.upgrade="Hot diggity dogg"
1021
+ ,.headers= { { "Connection", "keep-alive, upgrade" }
1022
+ , { "Upgrade", "WebSocket" }
1023
+ }
1024
+ ,.body= ""
1025
+ }
1026
+
1027
+ #define CONNECTION_MULTI_LWS_CRLF 37
1028
+ , {.name = "multiple connection header values with folding and lws"
1029
+ ,.type= HTTP_REQUEST
1030
+ ,.raw= "GET /demo HTTP/1.1\r\n"
1031
+ "Connection: keep-alive, \r\n upgrade\r\n"
1032
+ "Upgrade: WebSocket\r\n"
1033
+ "\r\n"
1034
+ "Hot diggity dogg"
1035
+ ,.should_keep_alive= TRUE
1036
+ ,.message_complete_on_eof= FALSE
1037
+ ,.http_major= 1
1038
+ ,.http_minor= 1
1039
+ ,.method= HTTP_GET
1040
+ ,.query_string= ""
1041
+ ,.fragment= ""
1042
+ ,.request_path= "/demo"
1043
+ ,.request_url= "/demo"
1044
+ ,.num_headers= 2
1045
+ ,.upgrade="Hot diggity dogg"
1046
+ ,.headers= { { "Connection", "keep-alive, upgrade" }
1047
+ , { "Upgrade", "WebSocket" }
1048
+ }
1049
+ ,.body= ""
1050
+ }
1051
+
1052
+ #define UPGRADE_POST_REQUEST 38
1053
+ , {.name = "upgrade post request"
1054
+ ,.type= HTTP_REQUEST
1055
+ ,.raw= "POST /demo HTTP/1.1\r\n"
1056
+ "Host: example.com\r\n"
1057
+ "Connection: Upgrade\r\n"
1058
+ "Upgrade: HTTP/2.0\r\n"
1059
+ "Content-Length: 15\r\n"
1060
+ "\r\n"
1061
+ "sweet post body"
1062
+ "Hot diggity dogg"
1063
+ ,.should_keep_alive= TRUE
1064
+ ,.message_complete_on_eof= FALSE
1065
+ ,.http_major= 1
1066
+ ,.http_minor= 1
1067
+ ,.method= HTTP_POST
1068
+ ,.request_path= "/demo"
1069
+ ,.request_url= "/demo"
1070
+ ,.num_headers= 4
1071
+ ,.upgrade="Hot diggity dogg"
1072
+ ,.headers= { { "Host", "example.com" }
1073
+ , { "Connection", "Upgrade" }
1074
+ , { "Upgrade", "HTTP/2.0" }
1075
+ , { "Content-Length", "15" }
1076
+ }
1077
+ ,.body= "sweet post body"
1078
+ }
1079
+
1080
+ #define CONNECT_WITH_BODY_REQUEST 39
1081
+ , {.name = "connect with body request"
1082
+ ,.type= HTTP_REQUEST
1083
+ ,.raw= "CONNECT foo.bar.com:443 HTTP/1.0\r\n"
1084
+ "User-agent: Mozilla/1.1N\r\n"
1085
+ "Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n"
1086
+ "Content-Length: 10\r\n"
1087
+ "\r\n"
1088
+ "blarfcicle"
1089
+ ,.should_keep_alive= FALSE
1090
+ ,.message_complete_on_eof= FALSE
1091
+ ,.http_major= 1
1092
+ ,.http_minor= 0
1093
+ ,.method= HTTP_CONNECT
1094
+ ,.request_url= "foo.bar.com:443"
1095
+ ,.num_headers= 3
1096
+ ,.upgrade="blarfcicle"
1097
+ ,.headers= { { "User-agent", "Mozilla/1.1N" }
1098
+ , { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" }
1099
+ , { "Content-Length", "10" }
1100
+ }
1101
+ ,.body= ""
1102
+ }
1103
+
1104
+ /* Examples from the Internet draft for LINK/UNLINK methods:
1105
+ * https://tools.ietf.org/id/draft-snell-link-method-01.html#rfc.section.5
1106
+ */
1107
+
1108
+ #define LINK_REQUEST 40
1109
+ , {.name = "link request"
1110
+ ,.type= HTTP_REQUEST
1111
+ ,.raw= "LINK /images/my_dog.jpg HTTP/1.1\r\n"
1112
+ "Host: example.com\r\n"
1113
+ "Link: <http://example.com/profiles/joe>; rel=\"tag\"\r\n"
1114
+ "Link: <http://example.com/profiles/sally>; rel=\"tag\"\r\n"
1115
+ "\r\n"
1116
+ ,.should_keep_alive= TRUE
1117
+ ,.message_complete_on_eof= FALSE
1118
+ ,.http_major= 1
1119
+ ,.http_minor= 1
1120
+ ,.method= HTTP_LINK
1121
+ ,.request_path= "/images/my_dog.jpg"
1122
+ ,.request_url= "/images/my_dog.jpg"
1123
+ ,.query_string= ""
1124
+ ,.fragment= ""
1125
+ ,.num_headers= 3
1126
+ ,.headers= { { "Host", "example.com" }
1127
+ , { "Link", "<http://example.com/profiles/joe>; rel=\"tag\"" }
1128
+ , { "Link", "<http://example.com/profiles/sally>; rel=\"tag\"" }
1129
+ }
1130
+ ,.body= ""
1131
+ }
1132
+
1133
+ #define UNLINK_REQUEST 41
1134
+ , {.name = "link request"
1135
+ ,.type= HTTP_REQUEST
1136
+ ,.raw= "UNLINK /images/my_dog.jpg HTTP/1.1\r\n"
1137
+ "Host: example.com\r\n"
1138
+ "Link: <http://example.com/profiles/sally>; rel=\"tag\"\r\n"
1139
+ "\r\n"
1140
+ ,.should_keep_alive= TRUE
1141
+ ,.message_complete_on_eof= FALSE
1142
+ ,.http_major= 1
1143
+ ,.http_minor= 1
1144
+ ,.method= HTTP_UNLINK
1145
+ ,.request_path= "/images/my_dog.jpg"
1146
+ ,.request_url= "/images/my_dog.jpg"
1147
+ ,.query_string= ""
1148
+ ,.fragment= ""
1149
+ ,.num_headers= 2
1150
+ ,.headers= { { "Host", "example.com" }
1151
+ , { "Link", "<http://example.com/profiles/sally>; rel=\"tag\"" }
1152
+ }
1153
+ ,.body= ""
1154
+ }
1155
+
1156
+ , {.name= NULL } /* sentinel */
1157
+ };
1158
+
1159
+ /* * R E S P O N S E S * */
1160
+ const struct message responses[] =
1161
+ #define GOOGLE_301 0
1162
+ { {.name= "google 301"
1163
+ ,.type= HTTP_RESPONSE
1164
+ ,.raw= "HTTP/1.1 301 Moved Permanently\r\n"
1165
+ "Location: http://www.google.com/\r\n"
1166
+ "Content-Type: text/html; charset=UTF-8\r\n"
1167
+ "Date: Sun, 26 Apr 2009 11:11:49 GMT\r\n"
1168
+ "Expires: Tue, 26 May 2009 11:11:49 GMT\r\n"
1169
+ "X-$PrototypeBI-Version: 1.6.0.3\r\n" /* $ char in header field */
1170
+ "Cache-Control: public, max-age=2592000\r\n"
1171
+ "Server: gws\r\n"
1172
+ "Content-Length: 219 \r\n"
1173
+ "\r\n"
1174
+ "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n"
1175
+ "<TITLE>301 Moved</TITLE></HEAD><BODY>\n"
1176
+ "<H1>301 Moved</H1>\n"
1177
+ "The document has moved\n"
1178
+ "<A HREF=\"http://www.google.com/\">here</A>.\r\n"
1179
+ "</BODY></HTML>\r\n"
1180
+ ,.should_keep_alive= TRUE
1181
+ ,.message_complete_on_eof= FALSE
1182
+ ,.http_major= 1
1183
+ ,.http_minor= 1
1184
+ ,.status_code= 301
1185
+ ,.response_status= "Moved Permanently"
1186
+ ,.num_headers= 8
1187
+ ,.headers=
1188
+ { { "Location", "http://www.google.com/" }
1189
+ , { "Content-Type", "text/html; charset=UTF-8" }
1190
+ , { "Date", "Sun, 26 Apr 2009 11:11:49 GMT" }
1191
+ , { "Expires", "Tue, 26 May 2009 11:11:49 GMT" }
1192
+ , { "X-$PrototypeBI-Version", "1.6.0.3" }
1193
+ , { "Cache-Control", "public, max-age=2592000" }
1194
+ , { "Server", "gws" }
1195
+ , { "Content-Length", "219 " }
1196
+ }
1197
+ ,.body= "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n"
1198
+ "<TITLE>301 Moved</TITLE></HEAD><BODY>\n"
1199
+ "<H1>301 Moved</H1>\n"
1200
+ "The document has moved\n"
1201
+ "<A HREF=\"http://www.google.com/\">here</A>.\r\n"
1202
+ "</BODY></HTML>\r\n"
1203
+ }
1204
+
1205
+ #define NO_CONTENT_LENGTH_RESPONSE 1
1206
+ /* The client should wait for the server's EOF. That is, when content-length
1207
+ * is not specified, and "Connection: close", the end of body is specified
1208
+ * by the EOF.
1209
+ * Compare with APACHEBENCH_GET
1210
+ */
1211
+ , {.name= "no content-length response"
1212
+ ,.type= HTTP_RESPONSE
1213
+ ,.raw= "HTTP/1.1 200 OK\r\n"
1214
+ "Date: Tue, 04 Aug 2009 07:59:32 GMT\r\n"
1215
+ "Server: Apache\r\n"
1216
+ "X-Powered-By: Servlet/2.5 JSP/2.1\r\n"
1217
+ "Content-Type: text/xml; charset=utf-8\r\n"
1218
+ "Connection: close\r\n"
1219
+ "\r\n"
1220
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1221
+ "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
1222
+ " <SOAP-ENV:Body>\n"
1223
+ " <SOAP-ENV:Fault>\n"
1224
+ " <faultcode>SOAP-ENV:Client</faultcode>\n"
1225
+ " <faultstring>Client Error</faultstring>\n"
1226
+ " </SOAP-ENV:Fault>\n"
1227
+ " </SOAP-ENV:Body>\n"
1228
+ "</SOAP-ENV:Envelope>"
1229
+ ,.should_keep_alive= FALSE
1230
+ ,.message_complete_on_eof= TRUE
1231
+ ,.http_major= 1
1232
+ ,.http_minor= 1
1233
+ ,.status_code= 200
1234
+ ,.response_status= "OK"
1235
+ ,.num_headers= 5
1236
+ ,.headers=
1237
+ { { "Date", "Tue, 04 Aug 2009 07:59:32 GMT" }
1238
+ , { "Server", "Apache" }
1239
+ , { "X-Powered-By", "Servlet/2.5 JSP/2.1" }
1240
+ , { "Content-Type", "text/xml; charset=utf-8" }
1241
+ , { "Connection", "close" }
1242
+ }
1243
+ ,.body= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1244
+ "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
1245
+ " <SOAP-ENV:Body>\n"
1246
+ " <SOAP-ENV:Fault>\n"
1247
+ " <faultcode>SOAP-ENV:Client</faultcode>\n"
1248
+ " <faultstring>Client Error</faultstring>\n"
1249
+ " </SOAP-ENV:Fault>\n"
1250
+ " </SOAP-ENV:Body>\n"
1251
+ "</SOAP-ENV:Envelope>"
1252
+ }
1253
+
1254
+ #define NO_HEADERS_NO_BODY_404 2
1255
+ , {.name= "404 no headers no body"
1256
+ ,.type= HTTP_RESPONSE
1257
+ ,.raw= "HTTP/1.1 404 Not Found\r\n\r\n"
1258
+ ,.should_keep_alive= FALSE
1259
+ ,.message_complete_on_eof= TRUE
1260
+ ,.http_major= 1
1261
+ ,.http_minor= 1
1262
+ ,.status_code= 404
1263
+ ,.response_status= "Not Found"
1264
+ ,.num_headers= 0
1265
+ ,.headers= {}
1266
+ ,.body_size= 0
1267
+ ,.body= ""
1268
+ }
1269
+
1270
+ #define NO_REASON_PHRASE 3
1271
+ , {.name= "301 no response phrase"
1272
+ ,.type= HTTP_RESPONSE
1273
+ ,.raw= "HTTP/1.1 301\r\n\r\n"
1274
+ ,.should_keep_alive = FALSE
1275
+ ,.message_complete_on_eof= TRUE
1276
+ ,.http_major= 1
1277
+ ,.http_minor= 1
1278
+ ,.status_code= 301
1279
+ ,.response_status= ""
1280
+ ,.num_headers= 0
1281
+ ,.headers= {}
1282
+ ,.body= ""
1283
+ }
1284
+
1285
+ #define TRAILING_SPACE_ON_CHUNKED_BODY 4
1286
+ , {.name="200 trailing space on chunked body"
1287
+ ,.type= HTTP_RESPONSE
1288
+ ,.raw= "HTTP/1.1 200 OK\r\n"
1289
+ "Content-Type: text/plain\r\n"
1290
+ "Transfer-Encoding: chunked\r\n"
1291
+ "\r\n"
1292
+ "25 \r\n"
1293
+ "This is the data in the first chunk\r\n"
1294
+ "\r\n"
1295
+ "1C\r\n"
1296
+ "and this is the second one\r\n"
1297
+ "\r\n"
1298
+ "0 \r\n"
1299
+ "\r\n"
1300
+ ,.should_keep_alive= TRUE
1301
+ ,.message_complete_on_eof= FALSE
1302
+ ,.http_major= 1
1303
+ ,.http_minor= 1
1304
+ ,.status_code= 200
1305
+ ,.response_status= "OK"
1306
+ ,.num_headers= 2
1307
+ ,.headers=
1308
+ { {"Content-Type", "text/plain" }
1309
+ , {"Transfer-Encoding", "chunked" }
1310
+ }
1311
+ ,.body_size = 37+28
1312
+ ,.body =
1313
+ "This is the data in the first chunk\r\n"
1314
+ "and this is the second one\r\n"
1315
+ ,.num_chunks_complete= 3
1316
+ ,.chunk_lengths= { 0x25, 0x1c }
1317
+ }
1318
+
1319
+ #define NO_CARRIAGE_RET 5
1320
+ , {.name="no carriage ret"
1321
+ ,.type= HTTP_RESPONSE
1322
+ ,.raw= "HTTP/1.1 200 OK\n"
1323
+ "Content-Type: text/html; charset=utf-8\n"
1324
+ "Connection: close\n"
1325
+ "\n"
1326
+ "these headers are from http://news.ycombinator.com/"
1327
+ ,.should_keep_alive= FALSE
1328
+ ,.message_complete_on_eof= TRUE
1329
+ ,.http_major= 1
1330
+ ,.http_minor= 1
1331
+ ,.status_code= 200
1332
+ ,.response_status= "OK"
1333
+ ,.num_headers= 2
1334
+ ,.headers=
1335
+ { {"Content-Type", "text/html; charset=utf-8" }
1336
+ , {"Connection", "close" }
1337
+ }
1338
+ ,.body= "these headers are from http://news.ycombinator.com/"
1339
+ }
1340
+
1341
+ #define PROXY_CONNECTION 6
1342
+ , {.name="proxy connection"
1343
+ ,.type= HTTP_RESPONSE
1344
+ ,.raw= "HTTP/1.1 200 OK\r\n"
1345
+ "Content-Type: text/html; charset=UTF-8\r\n"
1346
+ "Content-Length: 11\r\n"
1347
+ "Proxy-Connection: close\r\n"
1348
+ "Date: Thu, 31 Dec 2009 20:55:48 +0000\r\n"
1349
+ "\r\n"
1350
+ "hello world"
1351
+ ,.should_keep_alive= FALSE
1352
+ ,.message_complete_on_eof= FALSE
1353
+ ,.http_major= 1
1354
+ ,.http_minor= 1
1355
+ ,.status_code= 200
1356
+ ,.response_status= "OK"
1357
+ ,.num_headers= 4
1358
+ ,.headers=
1359
+ { {"Content-Type", "text/html; charset=UTF-8" }
1360
+ , {"Content-Length", "11" }
1361
+ , {"Proxy-Connection", "close" }
1362
+ , {"Date", "Thu, 31 Dec 2009 20:55:48 +0000"}
1363
+ }
1364
+ ,.body= "hello world"
1365
+ }
1366
+
1367
+ #define UNDERSTORE_HEADER_KEY 7
1368
+ // shown by
1369
+ // curl -o /dev/null -v "http://ad.doubleclick.net/pfadx/DARTSHELLCONFIGXML;dcmt=text/xml;"
1370
+ , {.name="underscore header key"
1371
+ ,.type= HTTP_RESPONSE
1372
+ ,.raw= "HTTP/1.1 200 OK\r\n"
1373
+ "Server: DCLK-AdSvr\r\n"
1374
+ "Content-Type: text/xml\r\n"
1375
+ "Content-Length: 0\r\n"
1376
+ "DCLK_imp: v7;x;114750856;0-0;0;17820020;0/0;21603567/21621457/1;;~okv=;dcmt=text/xml;;~cs=o\r\n\r\n"
1377
+ ,.should_keep_alive= TRUE
1378
+ ,.message_complete_on_eof= FALSE
1379
+ ,.http_major= 1
1380
+ ,.http_minor= 1
1381
+ ,.status_code= 200
1382
+ ,.response_status= "OK"
1383
+ ,.num_headers= 4
1384
+ ,.headers=
1385
+ { {"Server", "DCLK-AdSvr" }
1386
+ , {"Content-Type", "text/xml" }
1387
+ , {"Content-Length", "0" }
1388
+ , {"DCLK_imp", "v7;x;114750856;0-0;0;17820020;0/0;21603567/21621457/1;;~okv=;dcmt=text/xml;;~cs=o" }
1389
+ }
1390
+ ,.body= ""
1391
+ }
1392
+
1393
+ #define BONJOUR_MADAME_FR 8
1394
+ /* The client should not merge two headers fields when the first one doesn't
1395
+ * have a value.
1396
+ */
1397
+ , {.name= "bonjourmadame.fr"
1398
+ ,.type= HTTP_RESPONSE
1399
+ ,.raw= "HTTP/1.0 301 Moved Permanently\r\n"
1400
+ "Date: Thu, 03 Jun 2010 09:56:32 GMT\r\n"
1401
+ "Server: Apache/2.2.3 (Red Hat)\r\n"
1402
+ "Cache-Control: public\r\n"
1403
+ "Pragma: \r\n"
1404
+ "Location: http://www.bonjourmadame.fr/\r\n"
1405
+ "Vary: Accept-Encoding\r\n"
1406
+ "Content-Length: 0\r\n"
1407
+ "Content-Type: text/html; charset=UTF-8\r\n"
1408
+ "Connection: keep-alive\r\n"
1409
+ "\r\n"
1410
+ ,.should_keep_alive= TRUE
1411
+ ,.message_complete_on_eof= FALSE
1412
+ ,.http_major= 1
1413
+ ,.http_minor= 0
1414
+ ,.status_code= 301
1415
+ ,.response_status= "Moved Permanently"
1416
+ ,.num_headers= 9
1417
+ ,.headers=
1418
+ { { "Date", "Thu, 03 Jun 2010 09:56:32 GMT" }
1419
+ , { "Server", "Apache/2.2.3 (Red Hat)" }
1420
+ , { "Cache-Control", "public" }
1421
+ , { "Pragma", "" }
1422
+ , { "Location", "http://www.bonjourmadame.fr/" }
1423
+ , { "Vary", "Accept-Encoding" }
1424
+ , { "Content-Length", "0" }
1425
+ , { "Content-Type", "text/html; charset=UTF-8" }
1426
+ , { "Connection", "keep-alive" }
1427
+ }
1428
+ ,.body= ""
1429
+ }
1430
+
1431
+ #define RES_FIELD_UNDERSCORE 9
1432
+ /* Should handle spaces in header fields */
1433
+ , {.name= "field underscore"
1434
+ ,.type= HTTP_RESPONSE
1435
+ ,.raw= "HTTP/1.1 200 OK\r\n"
1436
+ "Date: Tue, 28 Sep 2010 01:14:13 GMT\r\n"
1437
+ "Server: Apache\r\n"
1438
+ "Cache-Control: no-cache, must-revalidate\r\n"
1439
+ "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n"
1440
+ ".et-Cookie: PlaxoCS=1274804622353690521; path=/; domain=.plaxo.com\r\n"
1441
+ "Vary: Accept-Encoding\r\n"
1442
+ "_eep-Alive: timeout=45\r\n" /* semantic value ignored */
1443
+ "_onnection: Keep-Alive\r\n" /* semantic value ignored */
1444
+ "Transfer-Encoding: chunked\r\n"
1445
+ "Content-Type: text/html\r\n"
1446
+ "Connection: close\r\n"
1447
+ "\r\n"
1448
+ "0\r\n\r\n"
1449
+ ,.should_keep_alive= FALSE
1450
+ ,.message_complete_on_eof= FALSE
1451
+ ,.http_major= 1
1452
+ ,.http_minor= 1
1453
+ ,.status_code= 200
1454
+ ,.response_status= "OK"
1455
+ ,.num_headers= 11
1456
+ ,.headers=
1457
+ { { "Date", "Tue, 28 Sep 2010 01:14:13 GMT" }
1458
+ , { "Server", "Apache" }
1459
+ , { "Cache-Control", "no-cache, must-revalidate" }
1460
+ , { "Expires", "Mon, 26 Jul 1997 05:00:00 GMT" }
1461
+ , { ".et-Cookie", "PlaxoCS=1274804622353690521; path=/; domain=.plaxo.com" }
1462
+ , { "Vary", "Accept-Encoding" }
1463
+ , { "_eep-Alive", "timeout=45" }
1464
+ , { "_onnection", "Keep-Alive" }
1465
+ , { "Transfer-Encoding", "chunked" }
1466
+ , { "Content-Type", "text/html" }
1467
+ , { "Connection", "close" }
1468
+ }
1469
+ ,.body= ""
1470
+ ,.num_chunks_complete= 1
1471
+ ,.chunk_lengths= {}
1472
+ }
1473
+
1474
+ #define NON_ASCII_IN_STATUS_LINE 10
1475
+ /* Should handle non-ASCII in status line */
1476
+ , {.name= "non-ASCII in status line"
1477
+ ,.type= HTTP_RESPONSE
1478
+ ,.raw= "HTTP/1.1 500 Oriëntatieprobleem\r\n"
1479
+ "Date: Fri, 5 Nov 2010 23:07:12 GMT+2\r\n"
1480
+ "Content-Length: 0\r\n"
1481
+ "Connection: close\r\n"
1482
+ "\r\n"
1483
+ ,.should_keep_alive= FALSE
1484
+ ,.message_complete_on_eof= FALSE
1485
+ ,.http_major= 1
1486
+ ,.http_minor= 1
1487
+ ,.status_code= 500
1488
+ ,.response_status= "Oriëntatieprobleem"
1489
+ ,.num_headers= 3
1490
+ ,.headers=
1491
+ { { "Date", "Fri, 5 Nov 2010 23:07:12 GMT+2" }
1492
+ , { "Content-Length", "0" }
1493
+ , { "Connection", "close" }
1494
+ }
1495
+ ,.body= ""
1496
+ }
1497
+
1498
+ #define HTTP_VERSION_0_9 11
1499
+ /* Should handle HTTP/0.9 */
1500
+ , {.name= "http version 0.9"
1501
+ ,.type= HTTP_RESPONSE
1502
+ ,.raw= "HTTP/0.9 200 OK\r\n"
1503
+ "\r\n"
1504
+ ,.should_keep_alive= FALSE
1505
+ ,.message_complete_on_eof= TRUE
1506
+ ,.http_major= 0
1507
+ ,.http_minor= 9
1508
+ ,.status_code= 200
1509
+ ,.response_status= "OK"
1510
+ ,.num_headers= 0
1511
+ ,.headers=
1512
+ {}
1513
+ ,.body= ""
1514
+ }
1515
+
1516
+ #define NO_CONTENT_LENGTH_NO_TRANSFER_ENCODING_RESPONSE 12
1517
+ /* The client should wait for the server's EOF. That is, when neither
1518
+ * content-length nor transfer-encoding is specified, the end of body
1519
+ * is specified by the EOF.
1520
+ */
1521
+ , {.name= "neither content-length nor transfer-encoding response"
1522
+ ,.type= HTTP_RESPONSE
1523
+ ,.raw= "HTTP/1.1 200 OK\r\n"
1524
+ "Content-Type: text/plain\r\n"
1525
+ "\r\n"
1526
+ "hello world"
1527
+ ,.should_keep_alive= FALSE
1528
+ ,.message_complete_on_eof= TRUE
1529
+ ,.http_major= 1
1530
+ ,.http_minor= 1
1531
+ ,.status_code= 200
1532
+ ,.response_status= "OK"
1533
+ ,.num_headers= 1
1534
+ ,.headers=
1535
+ { { "Content-Type", "text/plain" }
1536
+ }
1537
+ ,.body= "hello world"
1538
+ }
1539
+
1540
+ #define NO_BODY_HTTP10_KA_200 13
1541
+ , {.name= "HTTP/1.0 with keep-alive and EOF-terminated 200 status"
1542
+ ,.type= HTTP_RESPONSE
1543
+ ,.raw= "HTTP/1.0 200 OK\r\n"
1544
+ "Connection: keep-alive\r\n"
1545
+ "\r\n"
1546
+ ,.should_keep_alive= FALSE
1547
+ ,.message_complete_on_eof= TRUE
1548
+ ,.http_major= 1
1549
+ ,.http_minor= 0
1550
+ ,.status_code= 200
1551
+ ,.response_status= "OK"
1552
+ ,.num_headers= 1
1553
+ ,.headers=
1554
+ { { "Connection", "keep-alive" }
1555
+ }
1556
+ ,.body_size= 0
1557
+ ,.body= ""
1558
+ }
1559
+
1560
+ #define NO_BODY_HTTP10_KA_204 14
1561
+ , {.name= "HTTP/1.0 with keep-alive and a 204 status"
1562
+ ,.type= HTTP_RESPONSE
1563
+ ,.raw= "HTTP/1.0 204 No content\r\n"
1564
+ "Connection: keep-alive\r\n"
1565
+ "\r\n"
1566
+ ,.should_keep_alive= TRUE
1567
+ ,.message_complete_on_eof= FALSE
1568
+ ,.http_major= 1
1569
+ ,.http_minor= 0
1570
+ ,.status_code= 204
1571
+ ,.response_status= "No content"
1572
+ ,.num_headers= 1
1573
+ ,.headers=
1574
+ { { "Connection", "keep-alive" }
1575
+ }
1576
+ ,.body_size= 0
1577
+ ,.body= ""
1578
+ }
1579
+
1580
+ #define NO_BODY_HTTP11_KA_200 15
1581
+ , {.name= "HTTP/1.1 with an EOF-terminated 200 status"
1582
+ ,.type= HTTP_RESPONSE
1583
+ ,.raw= "HTTP/1.1 200 OK\r\n"
1584
+ "\r\n"
1585
+ ,.should_keep_alive= FALSE
1586
+ ,.message_complete_on_eof= TRUE
1587
+ ,.http_major= 1
1588
+ ,.http_minor= 1
1589
+ ,.status_code= 200
1590
+ ,.response_status= "OK"
1591
+ ,.num_headers= 0
1592
+ ,.headers={}
1593
+ ,.body_size= 0
1594
+ ,.body= ""
1595
+ }
1596
+
1597
+ #define NO_BODY_HTTP11_KA_204 16
1598
+ , {.name= "HTTP/1.1 with a 204 status"
1599
+ ,.type= HTTP_RESPONSE
1600
+ ,.raw= "HTTP/1.1 204 No content\r\n"
1601
+ "\r\n"
1602
+ ,.should_keep_alive= TRUE
1603
+ ,.message_complete_on_eof= FALSE
1604
+ ,.http_major= 1
1605
+ ,.http_minor= 1
1606
+ ,.status_code= 204
1607
+ ,.response_status= "No content"
1608
+ ,.num_headers= 0
1609
+ ,.headers={}
1610
+ ,.body_size= 0
1611
+ ,.body= ""
1612
+ }
1613
+
1614
+ #define NO_BODY_HTTP11_NOKA_204 17
1615
+ , {.name= "HTTP/1.1 with a 204 status and keep-alive disabled"
1616
+ ,.type= HTTP_RESPONSE
1617
+ ,.raw= "HTTP/1.1 204 No content\r\n"
1618
+ "Connection: close\r\n"
1619
+ "\r\n"
1620
+ ,.should_keep_alive= FALSE
1621
+ ,.message_complete_on_eof= FALSE
1622
+ ,.http_major= 1
1623
+ ,.http_minor= 1
1624
+ ,.status_code= 204
1625
+ ,.response_status= "No content"
1626
+ ,.num_headers= 1
1627
+ ,.headers=
1628
+ { { "Connection", "close" }
1629
+ }
1630
+ ,.body_size= 0
1631
+ ,.body= ""
1632
+ }
1633
+
1634
+ #define NO_BODY_HTTP11_KA_CHUNKED_200 18
1635
+ , {.name= "HTTP/1.1 with chunked endocing and a 200 response"
1636
+ ,.type= HTTP_RESPONSE
1637
+ ,.raw= "HTTP/1.1 200 OK\r\n"
1638
+ "Transfer-Encoding: chunked\r\n"
1639
+ "\r\n"
1640
+ "0\r\n"
1641
+ "\r\n"
1642
+ ,.should_keep_alive= TRUE
1643
+ ,.message_complete_on_eof= FALSE
1644
+ ,.http_major= 1
1645
+ ,.http_minor= 1
1646
+ ,.status_code= 200
1647
+ ,.response_status= "OK"
1648
+ ,.num_headers= 1
1649
+ ,.headers=
1650
+ { { "Transfer-Encoding", "chunked" }
1651
+ }
1652
+ ,.body_size= 0
1653
+ ,.body= ""
1654
+ ,.num_chunks_complete= 1
1655
+ }
1656
+
1657
+ #if !HTTP_PARSER_STRICT
1658
+ #define SPACE_IN_FIELD_RES 19
1659
+ /* Should handle spaces in header fields */
1660
+ , {.name= "field space"
1661
+ ,.type= HTTP_RESPONSE
1662
+ ,.raw= "HTTP/1.1 200 OK\r\n"
1663
+ "Server: Microsoft-IIS/6.0\r\n"
1664
+ "X-Powered-By: ASP.NET\r\n"
1665
+ "en-US Content-Type: text/xml\r\n" /* this is the problem */
1666
+ "Content-Type: text/xml\r\n"
1667
+ "Content-Length: 16\r\n"
1668
+ "Date: Fri, 23 Jul 2010 18:45:38 GMT\r\n"
1669
+ "Connection: keep-alive\r\n"
1670
+ "\r\n"
1671
+ "<xml>hello</xml>" /* fake body */
1672
+ ,.should_keep_alive= TRUE
1673
+ ,.message_complete_on_eof= FALSE
1674
+ ,.http_major= 1
1675
+ ,.http_minor= 1
1676
+ ,.status_code= 200
1677
+ ,.response_status= "OK"
1678
+ ,.num_headers= 7
1679
+ ,.headers=
1680
+ { { "Server", "Microsoft-IIS/6.0" }
1681
+ , { "X-Powered-By", "ASP.NET" }
1682
+ , { "en-US Content-Type", "text/xml" }
1683
+ , { "Content-Type", "text/xml" }
1684
+ , { "Content-Length", "16" }
1685
+ , { "Date", "Fri, 23 Jul 2010 18:45:38 GMT" }
1686
+ , { "Connection", "keep-alive" }
1687
+ }
1688
+ ,.body= "<xml>hello</xml>"
1689
+ }
1690
+ #endif /* !HTTP_PARSER_STRICT */
1691
+
1692
+ #define AMAZON_COM 20
1693
+ , {.name= "amazon.com"
1694
+ ,.type= HTTP_RESPONSE
1695
+ ,.raw= "HTTP/1.1 301 MovedPermanently\r\n"
1696
+ "Date: Wed, 15 May 2013 17:06:33 GMT\r\n"
1697
+ "Server: Server\r\n"
1698
+ "x-amz-id-1: 0GPHKXSJQ826RK7GZEB2\r\n"
1699
+ "p3p: policyref=\"http://www.amazon.com/w3c/p3p.xml\",CP=\"CAO DSP LAW CUR ADM IVAo IVDo CONo OTPo OUR DELi PUBi OTRi BUS PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA HEA PRE LOC GOV OTC \"\r\n"
1700
+ "x-amz-id-2: STN69VZxIFSz9YJLbz1GDbxpbjG6Qjmmq5E3DxRhOUw+Et0p4hr7c/Q8qNcx4oAD\r\n"
1701
+ "Location: http://www.amazon.com/Dan-Brown/e/B000AP9DSU/ref=s9_pop_gw_al1?_encoding=UTF8&refinementId=618073011&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-2&pf_rd_r=0SHYY5BZXN3KR20BNFAY&pf_rd_t=101&pf_rd_p=1263340922&pf_rd_i=507846\r\n"
1702
+ "Vary: Accept-Encoding,User-Agent\r\n"
1703
+ "Content-Type: text/html; charset=ISO-8859-1\r\n"
1704
+ "Transfer-Encoding: chunked\r\n"
1705
+ "\r\n"
1706
+ "1\r\n"
1707
+ "\n\r\n"
1708
+ "0\r\n"
1709
+ "\r\n"
1710
+ ,.should_keep_alive= TRUE
1711
+ ,.message_complete_on_eof= FALSE
1712
+ ,.http_major= 1
1713
+ ,.http_minor= 1
1714
+ ,.status_code= 301
1715
+ ,.response_status= "MovedPermanently"
1716
+ ,.num_headers= 9
1717
+ ,.headers= { { "Date", "Wed, 15 May 2013 17:06:33 GMT" }
1718
+ , { "Server", "Server" }
1719
+ , { "x-amz-id-1", "0GPHKXSJQ826RK7GZEB2" }
1720
+ , { "p3p", "policyref=\"http://www.amazon.com/w3c/p3p.xml\",CP=\"CAO DSP LAW CUR ADM IVAo IVDo CONo OTPo OUR DELi PUBi OTRi BUS PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA HEA PRE LOC GOV OTC \"" }
1721
+ , { "x-amz-id-2", "STN69VZxIFSz9YJLbz1GDbxpbjG6Qjmmq5E3DxRhOUw+Et0p4hr7c/Q8qNcx4oAD" }
1722
+ , { "Location", "http://www.amazon.com/Dan-Brown/e/B000AP9DSU/ref=s9_pop_gw_al1?_encoding=UTF8&refinementId=618073011&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-2&pf_rd_r=0SHYY5BZXN3KR20BNFAY&pf_rd_t=101&pf_rd_p=1263340922&pf_rd_i=507846" }
1723
+ , { "Vary", "Accept-Encoding,User-Agent" }
1724
+ , { "Content-Type", "text/html; charset=ISO-8859-1" }
1725
+ , { "Transfer-Encoding", "chunked" }
1726
+ }
1727
+ ,.body= "\n"
1728
+ ,.num_chunks_complete= 2
1729
+ ,.chunk_lengths= { 1 }
1730
+ }
1731
+
1732
+ #define EMPTY_REASON_PHRASE_AFTER_SPACE 20
1733
+ , {.name= "empty reason phrase after space"
1734
+ ,.type= HTTP_RESPONSE
1735
+ ,.raw= "HTTP/1.1 200 \r\n"
1736
+ "\r\n"
1737
+ ,.should_keep_alive= FALSE
1738
+ ,.message_complete_on_eof= TRUE
1739
+ ,.http_major= 1
1740
+ ,.http_minor= 1
1741
+ ,.status_code= 200
1742
+ ,.response_status= ""
1743
+ ,.num_headers= 0
1744
+ ,.headers= {}
1745
+ ,.body= ""
1746
+ }
1747
+
1748
+ #define CONTENT_LENGTH_X 21
1749
+ , {.name= "Content-Length-X"
1750
+ ,.type= HTTP_RESPONSE
1751
+ ,.raw= "HTTP/1.1 200 OK\r\n"
1752
+ "Content-Length-X: 0\r\n"
1753
+ "Transfer-Encoding: chunked\r\n"
1754
+ "\r\n"
1755
+ "2\r\n"
1756
+ "OK\r\n"
1757
+ "0\r\n"
1758
+ "\r\n"
1759
+ ,.should_keep_alive= TRUE
1760
+ ,.message_complete_on_eof= FALSE
1761
+ ,.http_major= 1
1762
+ ,.http_minor= 1
1763
+ ,.status_code= 200
1764
+ ,.response_status= "OK"
1765
+ ,.num_headers= 2
1766
+ ,.headers= { { "Content-Length-X", "0" }
1767
+ , { "Transfer-Encoding", "chunked" }
1768
+ }
1769
+ ,.body= "OK"
1770
+ ,.num_chunks_complete= 2
1771
+ ,.chunk_lengths= { 2 }
1772
+ }
1773
+
1774
+ , {.name= NULL } /* sentinel */
1775
+ };
1776
+
1777
+ /* strnlen() is a POSIX.2008 addition. Can't rely on it being available so
1778
+ * define it ourselves.
1779
+ */
1780
+ size_t
1781
+ strnlen(const char *s, size_t maxlen)
1782
+ {
1783
+ const char *p;
1784
+
1785
+ p = memchr(s, '\0', maxlen);
1786
+ if (p == NULL)
1787
+ return maxlen;
1788
+
1789
+ return p - s;
1790
+ }
1791
+
1792
+ size_t
1793
+ strlncat(char *dst, size_t len, const char *src, size_t n)
1794
+ {
1795
+ size_t slen;
1796
+ size_t dlen;
1797
+ size_t rlen;
1798
+ size_t ncpy;
1799
+
1800
+ slen = strnlen(src, n);
1801
+ dlen = strnlen(dst, len);
1802
+
1803
+ if (dlen < len) {
1804
+ rlen = len - dlen;
1805
+ ncpy = slen < rlen ? slen : (rlen - 1);
1806
+ memcpy(dst + dlen, src, ncpy);
1807
+ dst[dlen + ncpy] = '\0';
1808
+ }
1809
+
1810
+ assert(len > slen + dlen);
1811
+ return slen + dlen;
1812
+ }
1813
+
1814
+ size_t
1815
+ strlcat(char *dst, const char *src, size_t len)
1816
+ {
1817
+ return strlncat(dst, len, src, (size_t) -1);
1818
+ }
1819
+
1820
+ size_t
1821
+ strlncpy(char *dst, size_t len, const char *src, size_t n)
1822
+ {
1823
+ size_t slen;
1824
+ size_t ncpy;
1825
+
1826
+ slen = strnlen(src, n);
1827
+
1828
+ if (len > 0) {
1829
+ ncpy = slen < len ? slen : (len - 1);
1830
+ memcpy(dst, src, ncpy);
1831
+ dst[ncpy] = '\0';
1832
+ }
1833
+
1834
+ assert(len > slen);
1835
+ return slen;
1836
+ }
1837
+
1838
+ size_t
1839
+ strlcpy(char *dst, const char *src, size_t len)
1840
+ {
1841
+ return strlncpy(dst, len, src, (size_t) -1);
1842
+ }
1843
+
1844
+ int
1845
+ request_url_cb (http_parser *p, const char *buf, size_t len)
1846
+ {
1847
+ assert(p == parser);
1848
+ strlncat(messages[num_messages].request_url,
1849
+ sizeof(messages[num_messages].request_url),
1850
+ buf,
1851
+ len);
1852
+ return 0;
1853
+ }
1854
+
1855
+ int
1856
+ header_field_cb (http_parser *p, const char *buf, size_t len)
1857
+ {
1858
+ assert(p == parser);
1859
+ struct message *m = &messages[num_messages];
1860
+
1861
+ if (m->last_header_element != FIELD)
1862
+ m->num_headers++;
1863
+
1864
+ strlncat(m->headers[m->num_headers-1][0],
1865
+ sizeof(m->headers[m->num_headers-1][0]),
1866
+ buf,
1867
+ len);
1868
+
1869
+ m->last_header_element = FIELD;
1870
+
1871
+ return 0;
1872
+ }
1873
+
1874
+ int
1875
+ header_value_cb (http_parser *p, const char *buf, size_t len)
1876
+ {
1877
+ assert(p == parser);
1878
+ struct message *m = &messages[num_messages];
1879
+
1880
+ strlncat(m->headers[m->num_headers-1][1],
1881
+ sizeof(m->headers[m->num_headers-1][1]),
1882
+ buf,
1883
+ len);
1884
+
1885
+ m->last_header_element = VALUE;
1886
+
1887
+ return 0;
1888
+ }
1889
+
1890
+ void
1891
+ check_body_is_final (const http_parser *p)
1892
+ {
1893
+ if (messages[num_messages].body_is_final) {
1894
+ fprintf(stderr, "\n\n *** Error http_body_is_final() should return 1 "
1895
+ "on last on_body callback call "
1896
+ "but it doesn't! ***\n\n");
1897
+ assert(0);
1898
+ abort();
1899
+ }
1900
+ messages[num_messages].body_is_final = http_body_is_final(p);
1901
+ }
1902
+
1903
+ int
1904
+ body_cb (http_parser *p, const char *buf, size_t len)
1905
+ {
1906
+ assert(p == parser);
1907
+ strlncat(messages[num_messages].body,
1908
+ sizeof(messages[num_messages].body),
1909
+ buf,
1910
+ len);
1911
+ messages[num_messages].body_size += len;
1912
+ check_body_is_final(p);
1913
+ // printf("body_cb: '%s'\n", requests[num_messages].body);
1914
+ return 0;
1915
+ }
1916
+
1917
+ int
1918
+ count_body_cb (http_parser *p, const char *buf, size_t len)
1919
+ {
1920
+ assert(p == parser);
1921
+ assert(buf);
1922
+ messages[num_messages].body_size += len;
1923
+ check_body_is_final(p);
1924
+ return 0;
1925
+ }
1926
+
1927
+ int
1928
+ message_begin_cb (http_parser *p)
1929
+ {
1930
+ assert(p == parser);
1931
+ messages[num_messages].message_begin_cb_called = TRUE;
1932
+ return 0;
1933
+ }
1934
+
1935
+ int
1936
+ headers_complete_cb (http_parser *p)
1937
+ {
1938
+ assert(p == parser);
1939
+ messages[num_messages].method = parser->method;
1940
+ messages[num_messages].status_code = parser->status_code;
1941
+ messages[num_messages].http_major = parser->http_major;
1942
+ messages[num_messages].http_minor = parser->http_minor;
1943
+ messages[num_messages].headers_complete_cb_called = TRUE;
1944
+ messages[num_messages].should_keep_alive = http_should_keep_alive(parser);
1945
+ return 0;
1946
+ }
1947
+
1948
+ int
1949
+ message_complete_cb (http_parser *p)
1950
+ {
1951
+ assert(p == parser);
1952
+ if (messages[num_messages].should_keep_alive != http_should_keep_alive(parser))
1953
+ {
1954
+ fprintf(stderr, "\n\n *** Error http_should_keep_alive() should have same "
1955
+ "value in both on_message_complete and on_headers_complete "
1956
+ "but it doesn't! ***\n\n");
1957
+ assert(0);
1958
+ abort();
1959
+ }
1960
+
1961
+ if (messages[num_messages].body_size &&
1962
+ http_body_is_final(p) &&
1963
+ !messages[num_messages].body_is_final)
1964
+ {
1965
+ fprintf(stderr, "\n\n *** Error http_body_is_final() should return 1 "
1966
+ "on last on_body callback call "
1967
+ "but it doesn't! ***\n\n");
1968
+ assert(0);
1969
+ abort();
1970
+ }
1971
+
1972
+ messages[num_messages].message_complete_cb_called = TRUE;
1973
+
1974
+ messages[num_messages].message_complete_on_eof = currently_parsing_eof;
1975
+
1976
+ num_messages++;
1977
+ return 0;
1978
+ }
1979
+
1980
+ int
1981
+ response_status_cb (http_parser *p, const char *buf, size_t len)
1982
+ {
1983
+ assert(p == parser);
1984
+ strlncat(messages[num_messages].response_status,
1985
+ sizeof(messages[num_messages].response_status),
1986
+ buf,
1987
+ len);
1988
+ return 0;
1989
+ }
1990
+
1991
+ int
1992
+ chunk_header_cb (http_parser *p)
1993
+ {
1994
+ assert(p == parser);
1995
+ int chunk_idx = messages[num_messages].num_chunks;
1996
+ messages[num_messages].num_chunks++;
1997
+ if (chunk_idx < MAX_CHUNKS) {
1998
+ messages[num_messages].chunk_lengths[chunk_idx] = p->content_length;
1999
+ }
2000
+
2001
+ return 0;
2002
+ }
2003
+
2004
+ int
2005
+ chunk_complete_cb (http_parser *p)
2006
+ {
2007
+ assert(p == parser);
2008
+
2009
+ /* Here we want to verify that each chunk_header_cb is matched by a
2010
+ * chunk_complete_cb, so not only should the total number of calls to
2011
+ * both callbacks be the same, but they also should be interleaved
2012
+ * properly */
2013
+ assert(messages[num_messages].num_chunks ==
2014
+ messages[num_messages].num_chunks_complete + 1);
2015
+
2016
+ messages[num_messages].num_chunks_complete++;
2017
+ return 0;
2018
+ }
2019
+
2020
+ /* These dontcall_* callbacks exist so that we can verify that when we're
2021
+ * paused, no additional callbacks are invoked */
2022
+ int
2023
+ dontcall_message_begin_cb (http_parser *p)
2024
+ {
2025
+ if (p) { } // gcc
2026
+ fprintf(stderr, "\n\n*** on_message_begin() called on paused parser ***\n\n");
2027
+ abort();
2028
+ }
2029
+
2030
+ int
2031
+ dontcall_header_field_cb (http_parser *p, const char *buf, size_t len)
2032
+ {
2033
+ if (p || buf || len) { } // gcc
2034
+ fprintf(stderr, "\n\n*** on_header_field() called on paused parser ***\n\n");
2035
+ abort();
2036
+ }
2037
+
2038
+ int
2039
+ dontcall_header_value_cb (http_parser *p, const char *buf, size_t len)
2040
+ {
2041
+ if (p || buf || len) { } // gcc
2042
+ fprintf(stderr, "\n\n*** on_header_value() called on paused parser ***\n\n");
2043
+ abort();
2044
+ }
2045
+
2046
+ int
2047
+ dontcall_request_url_cb (http_parser *p, const char *buf, size_t len)
2048
+ {
2049
+ if (p || buf || len) { } // gcc
2050
+ fprintf(stderr, "\n\n*** on_request_url() called on paused parser ***\n\n");
2051
+ abort();
2052
+ }
2053
+
2054
+ int
2055
+ dontcall_body_cb (http_parser *p, const char *buf, size_t len)
2056
+ {
2057
+ if (p || buf || len) { } // gcc
2058
+ fprintf(stderr, "\n\n*** on_body_cb() called on paused parser ***\n\n");
2059
+ abort();
2060
+ }
2061
+
2062
+ int
2063
+ dontcall_headers_complete_cb (http_parser *p)
2064
+ {
2065
+ if (p) { } // gcc
2066
+ fprintf(stderr, "\n\n*** on_headers_complete() called on paused "
2067
+ "parser ***\n\n");
2068
+ abort();
2069
+ }
2070
+
2071
+ int
2072
+ dontcall_message_complete_cb (http_parser *p)
2073
+ {
2074
+ if (p) { } // gcc
2075
+ fprintf(stderr, "\n\n*** on_message_complete() called on paused "
2076
+ "parser ***\n\n");
2077
+ abort();
2078
+ }
2079
+
2080
+ int
2081
+ dontcall_response_status_cb (http_parser *p, const char *buf, size_t len)
2082
+ {
2083
+ if (p || buf || len) { } // gcc
2084
+ fprintf(stderr, "\n\n*** on_status() called on paused parser ***\n\n");
2085
+ abort();
2086
+ }
2087
+
2088
+ int
2089
+ dontcall_chunk_header_cb (http_parser *p)
2090
+ {
2091
+ if (p) { } // gcc
2092
+ fprintf(stderr, "\n\n*** on_chunk_header() called on paused parser ***\n\n");
2093
+ exit(1);
2094
+ }
2095
+
2096
+ int
2097
+ dontcall_chunk_complete_cb (http_parser *p)
2098
+ {
2099
+ if (p) { } // gcc
2100
+ fprintf(stderr, "\n\n*** on_chunk_complete() "
2101
+ "called on paused parser ***\n\n");
2102
+ exit(1);
2103
+ }
2104
+
2105
+ static http_parser_settings settings_dontcall =
2106
+ {.on_message_begin = dontcall_message_begin_cb
2107
+ ,.on_header_field = dontcall_header_field_cb
2108
+ ,.on_header_value = dontcall_header_value_cb
2109
+ ,.on_url = dontcall_request_url_cb
2110
+ ,.on_status = dontcall_response_status_cb
2111
+ ,.on_body = dontcall_body_cb
2112
+ ,.on_headers_complete = dontcall_headers_complete_cb
2113
+ ,.on_message_complete = dontcall_message_complete_cb
2114
+ ,.on_chunk_header = dontcall_chunk_header_cb
2115
+ ,.on_chunk_complete = dontcall_chunk_complete_cb
2116
+ };
2117
+
2118
+ /* These pause_* callbacks always pause the parser and just invoke the regular
2119
+ * callback that tracks content. Before returning, we overwrite the parser
2120
+ * settings to point to the _dontcall variety so that we can verify that
2121
+ * the pause actually did, you know, pause. */
2122
+ int
2123
+ pause_message_begin_cb (http_parser *p)
2124
+ {
2125
+ http_parser_pause(p, 1);
2126
+ *current_pause_parser = settings_dontcall;
2127
+ return message_begin_cb(p);
2128
+ }
2129
+
2130
+ int
2131
+ pause_header_field_cb (http_parser *p, const char *buf, size_t len)
2132
+ {
2133
+ http_parser_pause(p, 1);
2134
+ *current_pause_parser = settings_dontcall;
2135
+ return header_field_cb(p, buf, len);
2136
+ }
2137
+
2138
+ int
2139
+ pause_header_value_cb (http_parser *p, const char *buf, size_t len)
2140
+ {
2141
+ http_parser_pause(p, 1);
2142
+ *current_pause_parser = settings_dontcall;
2143
+ return header_value_cb(p, buf, len);
2144
+ }
2145
+
2146
+ int
2147
+ pause_request_url_cb (http_parser *p, const char *buf, size_t len)
2148
+ {
2149
+ http_parser_pause(p, 1);
2150
+ *current_pause_parser = settings_dontcall;
2151
+ return request_url_cb(p, buf, len);
2152
+ }
2153
+
2154
+ int
2155
+ pause_body_cb (http_parser *p, const char *buf, size_t len)
2156
+ {
2157
+ http_parser_pause(p, 1);
2158
+ *current_pause_parser = settings_dontcall;
2159
+ return body_cb(p, buf, len);
2160
+ }
2161
+
2162
+ int
2163
+ pause_headers_complete_cb (http_parser *p)
2164
+ {
2165
+ http_parser_pause(p, 1);
2166
+ *current_pause_parser = settings_dontcall;
2167
+ return headers_complete_cb(p);
2168
+ }
2169
+
2170
+ int
2171
+ pause_message_complete_cb (http_parser *p)
2172
+ {
2173
+ http_parser_pause(p, 1);
2174
+ *current_pause_parser = settings_dontcall;
2175
+ return message_complete_cb(p);
2176
+ }
2177
+
2178
+ int
2179
+ pause_response_status_cb (http_parser *p, const char *buf, size_t len)
2180
+ {
2181
+ http_parser_pause(p, 1);
2182
+ *current_pause_parser = settings_dontcall;
2183
+ return response_status_cb(p, buf, len);
2184
+ }
2185
+
2186
+ int
2187
+ pause_chunk_header_cb (http_parser *p)
2188
+ {
2189
+ http_parser_pause(p, 1);
2190
+ *current_pause_parser = settings_dontcall;
2191
+ return chunk_header_cb(p);
2192
+ }
2193
+
2194
+ int
2195
+ pause_chunk_complete_cb (http_parser *p)
2196
+ {
2197
+ http_parser_pause(p, 1);
2198
+ *current_pause_parser = settings_dontcall;
2199
+ return chunk_complete_cb(p);
2200
+ }
2201
+
2202
+ int
2203
+ connect_headers_complete_cb (http_parser *p)
2204
+ {
2205
+ headers_complete_cb(p);
2206
+ return 1;
2207
+ }
2208
+
2209
+ int
2210
+ connect_message_complete_cb (http_parser *p)
2211
+ {
2212
+ messages[num_messages].should_keep_alive = http_should_keep_alive(parser);
2213
+ return message_complete_cb(p);
2214
+ }
2215
+
2216
+ static http_parser_settings settings_pause =
2217
+ {.on_message_begin = pause_message_begin_cb
2218
+ ,.on_header_field = pause_header_field_cb
2219
+ ,.on_header_value = pause_header_value_cb
2220
+ ,.on_url = pause_request_url_cb
2221
+ ,.on_status = pause_response_status_cb
2222
+ ,.on_body = pause_body_cb
2223
+ ,.on_headers_complete = pause_headers_complete_cb
2224
+ ,.on_message_complete = pause_message_complete_cb
2225
+ ,.on_chunk_header = pause_chunk_header_cb
2226
+ ,.on_chunk_complete = pause_chunk_complete_cb
2227
+ };
2228
+
2229
+ static http_parser_settings settings =
2230
+ {.on_message_begin = message_begin_cb
2231
+ ,.on_header_field = header_field_cb
2232
+ ,.on_header_value = header_value_cb
2233
+ ,.on_url = request_url_cb
2234
+ ,.on_status = response_status_cb
2235
+ ,.on_body = body_cb
2236
+ ,.on_headers_complete = headers_complete_cb
2237
+ ,.on_message_complete = message_complete_cb
2238
+ ,.on_chunk_header = chunk_header_cb
2239
+ ,.on_chunk_complete = chunk_complete_cb
2240
+ };
2241
+
2242
+ static http_parser_settings settings_count_body =
2243
+ {.on_message_begin = message_begin_cb
2244
+ ,.on_header_field = header_field_cb
2245
+ ,.on_header_value = header_value_cb
2246
+ ,.on_url = request_url_cb
2247
+ ,.on_status = response_status_cb
2248
+ ,.on_body = count_body_cb
2249
+ ,.on_headers_complete = headers_complete_cb
2250
+ ,.on_message_complete = message_complete_cb
2251
+ ,.on_chunk_header = chunk_header_cb
2252
+ ,.on_chunk_complete = chunk_complete_cb
2253
+ };
2254
+
2255
+ static http_parser_settings settings_connect =
2256
+ {.on_message_begin = message_begin_cb
2257
+ ,.on_header_field = header_field_cb
2258
+ ,.on_header_value = header_value_cb
2259
+ ,.on_url = request_url_cb
2260
+ ,.on_status = response_status_cb
2261
+ ,.on_body = dontcall_body_cb
2262
+ ,.on_headers_complete = connect_headers_complete_cb
2263
+ ,.on_message_complete = connect_message_complete_cb
2264
+ ,.on_chunk_header = chunk_header_cb
2265
+ ,.on_chunk_complete = chunk_complete_cb
2266
+ };
2267
+
2268
+ static http_parser_settings settings_null =
2269
+ {.on_message_begin = 0
2270
+ ,.on_header_field = 0
2271
+ ,.on_header_value = 0
2272
+ ,.on_url = 0
2273
+ ,.on_status = 0
2274
+ ,.on_body = 0
2275
+ ,.on_headers_complete = 0
2276
+ ,.on_message_complete = 0
2277
+ ,.on_chunk_header = 0
2278
+ ,.on_chunk_complete = 0
2279
+ };
2280
+
2281
+ void
2282
+ parser_init (enum http_parser_type type)
2283
+ {
2284
+ num_messages = 0;
2285
+
2286
+ assert(parser == NULL);
2287
+
2288
+ parser = malloc(sizeof(http_parser));
2289
+
2290
+ http_parser_init(parser, type);
2291
+
2292
+ memset(&messages, 0, sizeof messages);
2293
+
2294
+ }
2295
+
2296
+ void
2297
+ parser_free ()
2298
+ {
2299
+ assert(parser);
2300
+ free(parser);
2301
+ parser = NULL;
2302
+ }
2303
+
2304
+ size_t parse (const char *buf, size_t len)
2305
+ {
2306
+ size_t nparsed;
2307
+ currently_parsing_eof = (len == 0);
2308
+ nparsed = http_parser_execute(parser, &settings, buf, len);
2309
+ return nparsed;
2310
+ }
2311
+
2312
+ size_t parse_count_body (const char *buf, size_t len)
2313
+ {
2314
+ size_t nparsed;
2315
+ currently_parsing_eof = (len == 0);
2316
+ nparsed = http_parser_execute(parser, &settings_count_body, buf, len);
2317
+ return nparsed;
2318
+ }
2319
+
2320
+ size_t parse_pause (const char *buf, size_t len)
2321
+ {
2322
+ size_t nparsed;
2323
+ http_parser_settings s = settings_pause;
2324
+
2325
+ currently_parsing_eof = (len == 0);
2326
+ current_pause_parser = &s;
2327
+ nparsed = http_parser_execute(parser, current_pause_parser, buf, len);
2328
+ return nparsed;
2329
+ }
2330
+
2331
+ size_t parse_connect (const char *buf, size_t len)
2332
+ {
2333
+ size_t nparsed;
2334
+ currently_parsing_eof = (len == 0);
2335
+ nparsed = http_parser_execute(parser, &settings_connect, buf, len);
2336
+ return nparsed;
2337
+ }
2338
+
2339
+ static inline int
2340
+ check_str_eq (const struct message *m,
2341
+ const char *prop,
2342
+ const char *expected,
2343
+ const char *found) {
2344
+ if ((expected == NULL) != (found == NULL)) {
2345
+ printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);
2346
+ printf("expected %s\n", (expected == NULL) ? "NULL" : expected);
2347
+ printf(" found %s\n", (found == NULL) ? "NULL" : found);
2348
+ return 0;
2349
+ }
2350
+ if (expected != NULL && 0 != strcmp(expected, found)) {
2351
+ printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);
2352
+ printf("expected '%s'\n", expected);
2353
+ printf(" found '%s'\n", found);
2354
+ return 0;
2355
+ }
2356
+ return 1;
2357
+ }
2358
+
2359
+ static inline int
2360
+ check_num_eq (const struct message *m,
2361
+ const char *prop,
2362
+ int expected,
2363
+ int found) {
2364
+ if (expected != found) {
2365
+ printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);
2366
+ printf("expected %d\n", expected);
2367
+ printf(" found %d\n", found);
2368
+ return 0;
2369
+ }
2370
+ return 1;
2371
+ }
2372
+
2373
+ #define MESSAGE_CHECK_STR_EQ(expected, found, prop) \
2374
+ if (!check_str_eq(expected, #prop, expected->prop, found->prop)) return 0
2375
+
2376
+ #define MESSAGE_CHECK_NUM_EQ(expected, found, prop) \
2377
+ if (!check_num_eq(expected, #prop, expected->prop, found->prop)) return 0
2378
+
2379
+ #define MESSAGE_CHECK_URL_EQ(u, expected, found, prop, fn) \
2380
+ do { \
2381
+ char ubuf[256]; \
2382
+ \
2383
+ if ((u)->field_set & (1 << (fn))) { \
2384
+ memcpy(ubuf, (found)->request_url + (u)->field_data[(fn)].off, \
2385
+ (u)->field_data[(fn)].len); \
2386
+ ubuf[(u)->field_data[(fn)].len] = '\0'; \
2387
+ } else { \
2388
+ ubuf[0] = '\0'; \
2389
+ } \
2390
+ \
2391
+ check_str_eq(expected, #prop, expected->prop, ubuf); \
2392
+ } while(0)
2393
+
2394
+ int
2395
+ message_eq (int index, int connect, const struct message *expected)
2396
+ {
2397
+ int i;
2398
+ struct message *m = &messages[index];
2399
+
2400
+ MESSAGE_CHECK_NUM_EQ(expected, m, http_major);
2401
+ MESSAGE_CHECK_NUM_EQ(expected, m, http_minor);
2402
+
2403
+ if (expected->type == HTTP_REQUEST) {
2404
+ MESSAGE_CHECK_NUM_EQ(expected, m, method);
2405
+ } else {
2406
+ MESSAGE_CHECK_NUM_EQ(expected, m, status_code);
2407
+ MESSAGE_CHECK_STR_EQ(expected, m, response_status);
2408
+ }
2409
+
2410
+ if (!connect) {
2411
+ MESSAGE_CHECK_NUM_EQ(expected, m, should_keep_alive);
2412
+ MESSAGE_CHECK_NUM_EQ(expected, m, message_complete_on_eof);
2413
+ }
2414
+
2415
+ assert(m->message_begin_cb_called);
2416
+ assert(m->headers_complete_cb_called);
2417
+ assert(m->message_complete_cb_called);
2418
+
2419
+
2420
+ MESSAGE_CHECK_STR_EQ(expected, m, request_url);
2421
+
2422
+ /* Check URL components; we can't do this w/ CONNECT since it doesn't
2423
+ * send us a well-formed URL.
2424
+ */
2425
+ if (*m->request_url && m->method != HTTP_CONNECT) {
2426
+ struct http_parser_url u;
2427
+
2428
+ if (http_parser_parse_url(m->request_url, strlen(m->request_url), 0, &u)) {
2429
+ fprintf(stderr, "\n\n*** failed to parse URL %s ***\n\n",
2430
+ m->request_url);
2431
+ abort();
2432
+ }
2433
+
2434
+ if (expected->host) {
2435
+ MESSAGE_CHECK_URL_EQ(&u, expected, m, host, UF_HOST);
2436
+ }
2437
+
2438
+ if (expected->userinfo) {
2439
+ MESSAGE_CHECK_URL_EQ(&u, expected, m, userinfo, UF_USERINFO);
2440
+ }
2441
+
2442
+ m->port = (u.field_set & (1 << UF_PORT)) ?
2443
+ u.port : 0;
2444
+
2445
+ MESSAGE_CHECK_URL_EQ(&u, expected, m, query_string, UF_QUERY);
2446
+ MESSAGE_CHECK_URL_EQ(&u, expected, m, fragment, UF_FRAGMENT);
2447
+ MESSAGE_CHECK_URL_EQ(&u, expected, m, request_path, UF_PATH);
2448
+ MESSAGE_CHECK_NUM_EQ(expected, m, port);
2449
+ }
2450
+
2451
+ if (connect) {
2452
+ check_num_eq(m, "body_size", 0, m->body_size);
2453
+ } else if (expected->body_size) {
2454
+ MESSAGE_CHECK_NUM_EQ(expected, m, body_size);
2455
+ } else {
2456
+ MESSAGE_CHECK_STR_EQ(expected, m, body);
2457
+ }
2458
+
2459
+ if (connect) {
2460
+ check_num_eq(m, "num_chunks_complete", 0, m->num_chunks_complete);
2461
+ } else {
2462
+ assert(m->num_chunks == m->num_chunks_complete);
2463
+ MESSAGE_CHECK_NUM_EQ(expected, m, num_chunks_complete);
2464
+ for (i = 0; i < m->num_chunks && i < MAX_CHUNKS; i++) {
2465
+ MESSAGE_CHECK_NUM_EQ(expected, m, chunk_lengths[i]);
2466
+ }
2467
+ }
2468
+
2469
+ MESSAGE_CHECK_NUM_EQ(expected, m, num_headers);
2470
+
2471
+ int r;
2472
+ for (i = 0; i < m->num_headers; i++) {
2473
+ r = check_str_eq(expected, "header field", expected->headers[i][0], m->headers[i][0]);
2474
+ if (!r) return 0;
2475
+ r = check_str_eq(expected, "header value", expected->headers[i][1], m->headers[i][1]);
2476
+ if (!r) return 0;
2477
+ }
2478
+
2479
+ MESSAGE_CHECK_STR_EQ(expected, m, upgrade);
2480
+
2481
+ return 1;
2482
+ }
2483
+
2484
+ /* Given a sequence of varargs messages, return the number of them that the
2485
+ * parser should successfully parse, taking into account that upgraded
2486
+ * messages prevent all subsequent messages from being parsed.
2487
+ */
2488
+ size_t
2489
+ count_parsed_messages(const size_t nmsgs, ...) {
2490
+ size_t i;
2491
+ va_list ap;
2492
+
2493
+ va_start(ap, nmsgs);
2494
+
2495
+ for (i = 0; i < nmsgs; i++) {
2496
+ struct message *m = va_arg(ap, struct message *);
2497
+
2498
+ if (m->upgrade) {
2499
+ va_end(ap);
2500
+ return i + 1;
2501
+ }
2502
+ }
2503
+
2504
+ va_end(ap);
2505
+ return nmsgs;
2506
+ }
2507
+
2508
+ /* Given a sequence of bytes and the number of these that we were able to
2509
+ * parse, verify that upgrade bodies are correct.
2510
+ */
2511
+ void
2512
+ upgrade_message_fix(char *body, const size_t nread, const size_t nmsgs, ...) {
2513
+ va_list ap;
2514
+ size_t i;
2515
+ size_t off = 0;
2516
+
2517
+ va_start(ap, nmsgs);
2518
+
2519
+ for (i = 0; i < nmsgs; i++) {
2520
+ struct message *m = va_arg(ap, struct message *);
2521
+
2522
+ off += strlen(m->raw);
2523
+
2524
+ if (m->upgrade) {
2525
+ off -= strlen(m->upgrade);
2526
+
2527
+ /* Check the portion of the response after its specified upgrade */
2528
+ if (!check_str_eq(m, "upgrade", body + off, body + nread)) {
2529
+ abort();
2530
+ }
2531
+
2532
+ /* Fix up the response so that message_eq() will verify the beginning
2533
+ * of the upgrade */
2534
+ *(body + nread + strlen(m->upgrade)) = '\0';
2535
+ messages[num_messages -1 ].upgrade = body + nread;
2536
+
2537
+ va_end(ap);
2538
+ return;
2539
+ }
2540
+ }
2541
+
2542
+ va_end(ap);
2543
+ printf("\n\n*** Error: expected a message with upgrade ***\n");
2544
+
2545
+ abort();
2546
+ }
2547
+
2548
+ static void
2549
+ print_error (const char *raw, size_t error_location)
2550
+ {
2551
+ fprintf(stderr, "\n*** %s ***\n\n",
2552
+ http_errno_description(HTTP_PARSER_ERRNO(parser)));
2553
+
2554
+ int this_line = 0, char_len = 0;
2555
+ size_t i, j, len = strlen(raw), error_location_line = 0;
2556
+ for (i = 0; i < len; i++) {
2557
+ if (i == error_location) this_line = 1;
2558
+ switch (raw[i]) {
2559
+ case '\r':
2560
+ char_len = 2;
2561
+ fprintf(stderr, "\\r");
2562
+ break;
2563
+
2564
+ case '\n':
2565
+ fprintf(stderr, "\\n\n");
2566
+
2567
+ if (this_line) goto print;
2568
+
2569
+ error_location_line = 0;
2570
+ continue;
2571
+
2572
+ default:
2573
+ char_len = 1;
2574
+ fputc(raw[i], stderr);
2575
+ break;
2576
+ }
2577
+ if (!this_line) error_location_line += char_len;
2578
+ }
2579
+
2580
+ fprintf(stderr, "[eof]\n");
2581
+
2582
+ print:
2583
+ for (j = 0; j < error_location_line; j++) {
2584
+ fputc(' ', stderr);
2585
+ }
2586
+ fprintf(stderr, "^\n\nerror location: %u\n", (unsigned int)error_location);
2587
+ }
2588
+
2589
+ void
2590
+ test_preserve_data (void)
2591
+ {
2592
+ char my_data[] = "application-specific data";
2593
+ http_parser parser;
2594
+ parser.data = my_data;
2595
+ http_parser_init(&parser, HTTP_REQUEST);
2596
+ if (parser.data != my_data) {
2597
+ printf("\n*** parser.data not preserved accross http_parser_init ***\n\n");
2598
+ abort();
2599
+ }
2600
+ }
2601
+
2602
+ struct url_test {
2603
+ const char *name;
2604
+ const char *url;
2605
+ int is_connect;
2606
+ struct http_parser_url u;
2607
+ int rv;
2608
+ };
2609
+
2610
+ const struct url_test url_tests[] =
2611
+ { {.name="proxy request"
2612
+ ,.url="http://hostname/"
2613
+ ,.is_connect=0
2614
+ ,.u=
2615
+ {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PATH)
2616
+ ,.port=0
2617
+ ,.field_data=
2618
+ {{ 0, 4 } /* UF_SCHEMA */
2619
+ ,{ 7, 8 } /* UF_HOST */
2620
+ ,{ 0, 0 } /* UF_PORT */
2621
+ ,{ 15, 1 } /* UF_PATH */
2622
+ ,{ 0, 0 } /* UF_QUERY */
2623
+ ,{ 0, 0 } /* UF_FRAGMENT */
2624
+ ,{ 0, 0 } /* UF_USERINFO */
2625
+ }
2626
+ }
2627
+ ,.rv=0
2628
+ }
2629
+
2630
+ , {.name="proxy request with port"
2631
+ ,.url="http://hostname:444/"
2632
+ ,.is_connect=0
2633
+ ,.u=
2634
+ {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PORT) | (1 << UF_PATH)
2635
+ ,.port=444
2636
+ ,.field_data=
2637
+ {{ 0, 4 } /* UF_SCHEMA */
2638
+ ,{ 7, 8 } /* UF_HOST */
2639
+ ,{ 16, 3 } /* UF_PORT */
2640
+ ,{ 19, 1 } /* UF_PATH */
2641
+ ,{ 0, 0 } /* UF_QUERY */
2642
+ ,{ 0, 0 } /* UF_FRAGMENT */
2643
+ ,{ 0, 0 } /* UF_USERINFO */
2644
+ }
2645
+ }
2646
+ ,.rv=0
2647
+ }
2648
+
2649
+ , {.name="CONNECT request"
2650
+ ,.url="hostname:443"
2651
+ ,.is_connect=1
2652
+ ,.u=
2653
+ {.field_set=(1 << UF_HOST) | (1 << UF_PORT)
2654
+ ,.port=443
2655
+ ,.field_data=
2656
+ {{ 0, 0 } /* UF_SCHEMA */
2657
+ ,{ 0, 8 } /* UF_HOST */
2658
+ ,{ 9, 3 } /* UF_PORT */
2659
+ ,{ 0, 0 } /* UF_PATH */
2660
+ ,{ 0, 0 } /* UF_QUERY */
2661
+ ,{ 0, 0 } /* UF_FRAGMENT */
2662
+ ,{ 0, 0 } /* UF_USERINFO */
2663
+ }
2664
+ }
2665
+ ,.rv=0
2666
+ }
2667
+
2668
+ , {.name="CONNECT request but not connect"
2669
+ ,.url="hostname:443"
2670
+ ,.is_connect=0
2671
+ ,.rv=1
2672
+ }
2673
+
2674
+ , {.name="proxy ipv6 request"
2675
+ ,.url="http://[1:2::3:4]/"
2676
+ ,.is_connect=0
2677
+ ,.u=
2678
+ {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PATH)
2679
+ ,.port=0
2680
+ ,.field_data=
2681
+ {{ 0, 4 } /* UF_SCHEMA */
2682
+ ,{ 8, 8 } /* UF_HOST */
2683
+ ,{ 0, 0 } /* UF_PORT */
2684
+ ,{ 17, 1 } /* UF_PATH */
2685
+ ,{ 0, 0 } /* UF_QUERY */
2686
+ ,{ 0, 0 } /* UF_FRAGMENT */
2687
+ ,{ 0, 0 } /* UF_USERINFO */
2688
+ }
2689
+ }
2690
+ ,.rv=0
2691
+ }
2692
+
2693
+ , {.name="proxy ipv6 request with port"
2694
+ ,.url="http://[1:2::3:4]:67/"
2695
+ ,.is_connect=0
2696
+ ,.u=
2697
+ {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PORT) | (1 << UF_PATH)
2698
+ ,.port=67
2699
+ ,.field_data=
2700
+ {{ 0, 4 } /* UF_SCHEMA */
2701
+ ,{ 8, 8 } /* UF_HOST */
2702
+ ,{ 18, 2 } /* UF_PORT */
2703
+ ,{ 20, 1 } /* UF_PATH */
2704
+ ,{ 0, 0 } /* UF_QUERY */
2705
+ ,{ 0, 0 } /* UF_FRAGMENT */
2706
+ ,{ 0, 0 } /* UF_USERINFO */
2707
+ }
2708
+ }
2709
+ ,.rv=0
2710
+ }
2711
+
2712
+ , {.name="CONNECT ipv6 address"
2713
+ ,.url="[1:2::3:4]:443"
2714
+ ,.is_connect=1
2715
+ ,.u=
2716
+ {.field_set=(1 << UF_HOST) | (1 << UF_PORT)
2717
+ ,.port=443
2718
+ ,.field_data=
2719
+ {{ 0, 0 } /* UF_SCHEMA */
2720
+ ,{ 1, 8 } /* UF_HOST */
2721
+ ,{ 11, 3 } /* UF_PORT */
2722
+ ,{ 0, 0 } /* UF_PATH */
2723
+ ,{ 0, 0 } /* UF_QUERY */
2724
+ ,{ 0, 0 } /* UF_FRAGMENT */
2725
+ ,{ 0, 0 } /* UF_USERINFO */
2726
+ }
2727
+ }
2728
+ ,.rv=0
2729
+ }
2730
+
2731
+ , {.name="ipv4 in ipv6 address"
2732
+ ,.url="http://[2001:0000:0000:0000:0000:0000:1.9.1.1]/"
2733
+ ,.is_connect=0
2734
+ ,.u=
2735
+ {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PATH)
2736
+ ,.port=0
2737
+ ,.field_data=
2738
+ {{ 0, 4 } /* UF_SCHEMA */
2739
+ ,{ 8, 37 } /* UF_HOST */
2740
+ ,{ 0, 0 } /* UF_PORT */
2741
+ ,{ 46, 1 } /* UF_PATH */
2742
+ ,{ 0, 0 } /* UF_QUERY */
2743
+ ,{ 0, 0 } /* UF_FRAGMENT */
2744
+ ,{ 0, 0 } /* UF_USERINFO */
2745
+ }
2746
+ }
2747
+ ,.rv=0
2748
+ }
2749
+
2750
+ , {.name="extra ? in query string"
2751
+ ,.url="http://a.tbcdn.cn/p/fp/2010c/??fp-header-min.css,fp-base-min.css,"
2752
+ "fp-channel-min.css,fp-product-min.css,fp-mall-min.css,fp-category-min.css,"
2753
+ "fp-sub-min.css,fp-gdp4p-min.css,fp-css3-min.css,fp-misc-min.css?t=20101022.css"
2754
+ ,.is_connect=0
2755
+ ,.u=
2756
+ {.field_set=(1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_QUERY)
2757
+ ,.port=0
2758
+ ,.field_data=
2759
+ {{ 0, 4 } /* UF_SCHEMA */
2760
+ ,{ 7, 10 } /* UF_HOST */
2761
+ ,{ 0, 0 } /* UF_PORT */
2762
+ ,{ 17, 12 } /* UF_PATH */
2763
+ ,{ 30,187 } /* UF_QUERY */
2764
+ ,{ 0, 0 } /* UF_FRAGMENT */
2765
+ ,{ 0, 0 } /* UF_USERINFO */
2766
+ }
2767
+ }
2768
+ ,.rv=0
2769
+ }
2770
+
2771
+ , {.name="space URL encoded"
2772
+ ,.url="/toto.html?toto=a%20b"
2773
+ ,.is_connect=0
2774
+ ,.u=
2775
+ {.field_set= (1<<UF_PATH) | (1<<UF_QUERY)
2776
+ ,.port=0
2777
+ ,.field_data=
2778
+ {{ 0, 0 } /* UF_SCHEMA */
2779
+ ,{ 0, 0 } /* UF_HOST */
2780
+ ,{ 0, 0 } /* UF_PORT */
2781
+ ,{ 0, 10 } /* UF_PATH */
2782
+ ,{ 11, 10 } /* UF_QUERY */
2783
+ ,{ 0, 0 } /* UF_FRAGMENT */
2784
+ ,{ 0, 0 } /* UF_USERINFO */
2785
+ }
2786
+ }
2787
+ ,.rv=0
2788
+ }
2789
+
2790
+
2791
+ , {.name="URL fragment"
2792
+ ,.url="/toto.html#titi"
2793
+ ,.is_connect=0
2794
+ ,.u=
2795
+ {.field_set= (1<<UF_PATH) | (1<<UF_FRAGMENT)
2796
+ ,.port=0
2797
+ ,.field_data=
2798
+ {{ 0, 0 } /* UF_SCHEMA */
2799
+ ,{ 0, 0 } /* UF_HOST */
2800
+ ,{ 0, 0 } /* UF_PORT */
2801
+ ,{ 0, 10 } /* UF_PATH */
2802
+ ,{ 0, 0 } /* UF_QUERY */
2803
+ ,{ 11, 4 } /* UF_FRAGMENT */
2804
+ ,{ 0, 0 } /* UF_USERINFO */
2805
+ }
2806
+ }
2807
+ ,.rv=0
2808
+ }
2809
+
2810
+ , {.name="complex URL fragment"
2811
+ ,.url="http://www.webmasterworld.com/r.cgi?f=21&d=8405&url="
2812
+ "http://www.example.com/index.html?foo=bar&hello=world#midpage"
2813
+ ,.is_connect=0
2814
+ ,.u=
2815
+ {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_QUERY) |\
2816
+ (1<<UF_FRAGMENT)
2817
+ ,.port=0
2818
+ ,.field_data=
2819
+ {{ 0, 4 } /* UF_SCHEMA */
2820
+ ,{ 7, 22 } /* UF_HOST */
2821
+ ,{ 0, 0 } /* UF_PORT */
2822
+ ,{ 29, 6 } /* UF_PATH */
2823
+ ,{ 36, 69 } /* UF_QUERY */
2824
+ ,{106, 7 } /* UF_FRAGMENT */
2825
+ ,{ 0, 0 } /* UF_USERINFO */
2826
+ }
2827
+ }
2828
+ ,.rv=0
2829
+ }
2830
+
2831
+ , {.name="complex URL from node js url parser doc"
2832
+ ,.url="http://host.com:8080/p/a/t/h?query=string#hash"
2833
+ ,.is_connect=0
2834
+ ,.u=
2835
+ {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PORT) | (1<<UF_PATH) |\
2836
+ (1<<UF_QUERY) | (1<<UF_FRAGMENT)
2837
+ ,.port=8080
2838
+ ,.field_data=
2839
+ {{ 0, 4 } /* UF_SCHEMA */
2840
+ ,{ 7, 8 } /* UF_HOST */
2841
+ ,{ 16, 4 } /* UF_PORT */
2842
+ ,{ 20, 8 } /* UF_PATH */
2843
+ ,{ 29, 12 } /* UF_QUERY */
2844
+ ,{ 42, 4 } /* UF_FRAGMENT */
2845
+ ,{ 0, 0 } /* UF_USERINFO */
2846
+ }
2847
+ }
2848
+ ,.rv=0
2849
+ }
2850
+
2851
+ , {.name="complex URL with basic auth from node js url parser doc"
2852
+ ,.url="http://a:b@host.com:8080/p/a/t/h?query=string#hash"
2853
+ ,.is_connect=0
2854
+ ,.u=
2855
+ {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PORT) | (1<<UF_PATH) |\
2856
+ (1<<UF_QUERY) | (1<<UF_FRAGMENT) | (1<<UF_USERINFO)
2857
+ ,.port=8080
2858
+ ,.field_data=
2859
+ {{ 0, 4 } /* UF_SCHEMA */
2860
+ ,{ 11, 8 } /* UF_HOST */
2861
+ ,{ 20, 4 } /* UF_PORT */
2862
+ ,{ 24, 8 } /* UF_PATH */
2863
+ ,{ 33, 12 } /* UF_QUERY */
2864
+ ,{ 46, 4 } /* UF_FRAGMENT */
2865
+ ,{ 7, 3 } /* UF_USERINFO */
2866
+ }
2867
+ }
2868
+ ,.rv=0
2869
+ }
2870
+
2871
+ , {.name="double @"
2872
+ ,.url="http://a:b@@hostname:443/"
2873
+ ,.is_connect=0
2874
+ ,.rv=1
2875
+ }
2876
+
2877
+ , {.name="proxy empty host"
2878
+ ,.url="http://:443/"
2879
+ ,.is_connect=0
2880
+ ,.rv=1
2881
+ }
2882
+
2883
+ , {.name="proxy empty port"
2884
+ ,.url="http://hostname:/"
2885
+ ,.is_connect=0
2886
+ ,.rv=1
2887
+ }
2888
+
2889
+ , {.name="CONNECT with basic auth"
2890
+ ,.url="a:b@hostname:443"
2891
+ ,.is_connect=1
2892
+ ,.rv=1
2893
+ }
2894
+
2895
+ , {.name="CONNECT empty host"
2896
+ ,.url=":443"
2897
+ ,.is_connect=1
2898
+ ,.rv=1
2899
+ }
2900
+
2901
+ , {.name="CONNECT empty port"
2902
+ ,.url="hostname:"
2903
+ ,.is_connect=1
2904
+ ,.rv=1
2905
+ }
2906
+
2907
+ , {.name="CONNECT with extra bits"
2908
+ ,.url="hostname:443/"
2909
+ ,.is_connect=1
2910
+ ,.rv=1
2911
+ }
2912
+
2913
+ , {.name="space in URL"
2914
+ ,.url="/foo bar/"
2915
+ ,.rv=1 /* s_dead */
2916
+ }
2917
+
2918
+ , {.name="proxy basic auth with space url encoded"
2919
+ ,.url="http://a%20:b@host.com/"
2920
+ ,.is_connect=0
2921
+ ,.u=
2922
+ {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_USERINFO)
2923
+ ,.port=0
2924
+ ,.field_data=
2925
+ {{ 0, 4 } /* UF_SCHEMA */
2926
+ ,{ 14, 8 } /* UF_HOST */
2927
+ ,{ 0, 0 } /* UF_PORT */
2928
+ ,{ 22, 1 } /* UF_PATH */
2929
+ ,{ 0, 0 } /* UF_QUERY */
2930
+ ,{ 0, 0 } /* UF_FRAGMENT */
2931
+ ,{ 7, 6 } /* UF_USERINFO */
2932
+ }
2933
+ }
2934
+ ,.rv=0
2935
+ }
2936
+
2937
+ , {.name="carriage return in URL"
2938
+ ,.url="/foo\rbar/"
2939
+ ,.rv=1 /* s_dead */
2940
+ }
2941
+
2942
+ , {.name="proxy double : in URL"
2943
+ ,.url="http://hostname::443/"
2944
+ ,.rv=1 /* s_dead */
2945
+ }
2946
+
2947
+ , {.name="proxy basic auth with double :"
2948
+ ,.url="http://a::b@host.com/"
2949
+ ,.is_connect=0
2950
+ ,.u=
2951
+ {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_USERINFO)
2952
+ ,.port=0
2953
+ ,.field_data=
2954
+ {{ 0, 4 } /* UF_SCHEMA */
2955
+ ,{ 12, 8 } /* UF_HOST */
2956
+ ,{ 0, 0 } /* UF_PORT */
2957
+ ,{ 20, 1 } /* UF_PATH */
2958
+ ,{ 0, 0 } /* UF_QUERY */
2959
+ ,{ 0, 0 } /* UF_FRAGMENT */
2960
+ ,{ 7, 4 } /* UF_USERINFO */
2961
+ }
2962
+ }
2963
+ ,.rv=0
2964
+ }
2965
+
2966
+ , {.name="line feed in URL"
2967
+ ,.url="/foo\nbar/"
2968
+ ,.rv=1 /* s_dead */
2969
+ }
2970
+
2971
+ , {.name="proxy empty basic auth"
2972
+ ,.url="http://@hostname/fo"
2973
+ ,.u=
2974
+ {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH)
2975
+ ,.port=0
2976
+ ,.field_data=
2977
+ {{ 0, 4 } /* UF_SCHEMA */
2978
+ ,{ 8, 8 } /* UF_HOST */
2979
+ ,{ 0, 0 } /* UF_PORT */
2980
+ ,{ 16, 3 } /* UF_PATH */
2981
+ ,{ 0, 0 } /* UF_QUERY */
2982
+ ,{ 0, 0 } /* UF_FRAGMENT */
2983
+ ,{ 0, 0 } /* UF_USERINFO */
2984
+ }
2985
+ }
2986
+ ,.rv=0
2987
+ }
2988
+ , {.name="proxy line feed in hostname"
2989
+ ,.url="http://host\name/fo"
2990
+ ,.rv=1 /* s_dead */
2991
+ }
2992
+
2993
+ , {.name="proxy % in hostname"
2994
+ ,.url="http://host%name/fo"
2995
+ ,.rv=1 /* s_dead */
2996
+ }
2997
+
2998
+ , {.name="proxy ; in hostname"
2999
+ ,.url="http://host;ame/fo"
3000
+ ,.rv=1 /* s_dead */
3001
+ }
3002
+
3003
+ , {.name="proxy basic auth with unreservedchars"
3004
+ ,.url="http://a!;-_!=+$@host.com/"
3005
+ ,.is_connect=0
3006
+ ,.u=
3007
+ {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) | (1<<UF_USERINFO)
3008
+ ,.port=0
3009
+ ,.field_data=
3010
+ {{ 0, 4 } /* UF_SCHEMA */
3011
+ ,{ 17, 8 } /* UF_HOST */
3012
+ ,{ 0, 0 } /* UF_PORT */
3013
+ ,{ 25, 1 } /* UF_PATH */
3014
+ ,{ 0, 0 } /* UF_QUERY */
3015
+ ,{ 0, 0 } /* UF_FRAGMENT */
3016
+ ,{ 7, 9 } /* UF_USERINFO */
3017
+ }
3018
+ }
3019
+ ,.rv=0
3020
+ }
3021
+
3022
+ , {.name="proxy only empty basic auth"
3023
+ ,.url="http://@/fo"
3024
+ ,.rv=1 /* s_dead */
3025
+ }
3026
+
3027
+ , {.name="proxy only basic auth"
3028
+ ,.url="http://toto@/fo"
3029
+ ,.rv=1 /* s_dead */
3030
+ }
3031
+
3032
+ , {.name="proxy emtpy hostname"
3033
+ ,.url="http:///fo"
3034
+ ,.rv=1 /* s_dead */
3035
+ }
3036
+
3037
+ , {.name="proxy = in URL"
3038
+ ,.url="http://host=ame/fo"
3039
+ ,.rv=1 /* s_dead */
3040
+ }
3041
+
3042
+ , {.name="ipv6 address with Zone ID"
3043
+ ,.url="http://[fe80::a%25eth0]/"
3044
+ ,.is_connect=0
3045
+ ,.u=
3046
+ {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH)
3047
+ ,.port=0
3048
+ ,.field_data=
3049
+ {{ 0, 4 } /* UF_SCHEMA */
3050
+ ,{ 8, 14 } /* UF_HOST */
3051
+ ,{ 0, 0 } /* UF_PORT */
3052
+ ,{ 23, 1 } /* UF_PATH */
3053
+ ,{ 0, 0 } /* UF_QUERY */
3054
+ ,{ 0, 0 } /* UF_FRAGMENT */
3055
+ ,{ 0, 0 } /* UF_USERINFO */
3056
+ }
3057
+ }
3058
+ ,.rv=0
3059
+ }
3060
+
3061
+ , {.name="ipv6 address with Zone ID, but '%' is not percent-encoded"
3062
+ ,.url="http://[fe80::a%eth0]/"
3063
+ ,.is_connect=0
3064
+ ,.u=
3065
+ {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH)
3066
+ ,.port=0
3067
+ ,.field_data=
3068
+ {{ 0, 4 } /* UF_SCHEMA */
3069
+ ,{ 8, 12 } /* UF_HOST */
3070
+ ,{ 0, 0 } /* UF_PORT */
3071
+ ,{ 21, 1 } /* UF_PATH */
3072
+ ,{ 0, 0 } /* UF_QUERY */
3073
+ ,{ 0, 0 } /* UF_FRAGMENT */
3074
+ ,{ 0, 0 } /* UF_USERINFO */
3075
+ }
3076
+ }
3077
+ ,.rv=0
3078
+ }
3079
+
3080
+ , {.name="ipv6 address ending with '%'"
3081
+ ,.url="http://[fe80::a%]/"
3082
+ ,.rv=1 /* s_dead */
3083
+ }
3084
+
3085
+ , {.name="ipv6 address with Zone ID including bad character"
3086
+ ,.url="http://[fe80::a%$HOME]/"
3087
+ ,.rv=1 /* s_dead */
3088
+ }
3089
+
3090
+ , {.name="just ipv6 Zone ID"
3091
+ ,.url="http://[%eth0]/"
3092
+ ,.rv=1 /* s_dead */
3093
+ }
3094
+
3095
+ #if HTTP_PARSER_STRICT
3096
+
3097
+ , {.name="tab in URL"
3098
+ ,.url="/foo\tbar/"
3099
+ ,.rv=1 /* s_dead */
3100
+ }
3101
+
3102
+ , {.name="form feed in URL"
3103
+ ,.url="/foo\fbar/"
3104
+ ,.rv=1 /* s_dead */
3105
+ }
3106
+
3107
+ #else /* !HTTP_PARSER_STRICT */
3108
+
3109
+ , {.name="tab in URL"
3110
+ ,.url="/foo\tbar/"
3111
+ ,.u=
3112
+ {.field_set=(1 << UF_PATH)
3113
+ ,.field_data=
3114
+ {{ 0, 0 } /* UF_SCHEMA */
3115
+ ,{ 0, 0 } /* UF_HOST */
3116
+ ,{ 0, 0 } /* UF_PORT */
3117
+ ,{ 0, 9 } /* UF_PATH */
3118
+ ,{ 0, 0 } /* UF_QUERY */
3119
+ ,{ 0, 0 } /* UF_FRAGMENT */
3120
+ ,{ 0, 0 } /* UF_USERINFO */
3121
+ }
3122
+ }
3123
+ ,.rv=0
3124
+ }
3125
+
3126
+ , {.name="form feed in URL"
3127
+ ,.url="/foo\fbar/"
3128
+ ,.u=
3129
+ {.field_set=(1 << UF_PATH)
3130
+ ,.field_data=
3131
+ {{ 0, 0 } /* UF_SCHEMA */
3132
+ ,{ 0, 0 } /* UF_HOST */
3133
+ ,{ 0, 0 } /* UF_PORT */
3134
+ ,{ 0, 9 } /* UF_PATH */
3135
+ ,{ 0, 0 } /* UF_QUERY */
3136
+ ,{ 0, 0 } /* UF_FRAGMENT */
3137
+ ,{ 0, 0 } /* UF_USERINFO */
3138
+ }
3139
+ }
3140
+ ,.rv=0
3141
+ }
3142
+ #endif
3143
+ };
3144
+
3145
+ void
3146
+ dump_url (const char *url, const struct http_parser_url *u)
3147
+ {
3148
+ unsigned int i;
3149
+
3150
+ printf("\tfield_set: 0x%x, port: %u\n", u->field_set, u->port);
3151
+ for (i = 0; i < UF_MAX; i++) {
3152
+ if ((u->field_set & (1 << i)) == 0) {
3153
+ printf("\tfield_data[%u]: unset\n", i);
3154
+ continue;
3155
+ }
3156
+
3157
+ printf("\tfield_data[%u]: off: %u len: %u part: \"%.*s\n\"",
3158
+ i,
3159
+ u->field_data[i].off,
3160
+ u->field_data[i].len,
3161
+ u->field_data[i].len,
3162
+ url + u->field_data[i].off);
3163
+ }
3164
+ }
3165
+
3166
+ void
3167
+ test_parse_url (void)
3168
+ {
3169
+ struct http_parser_url u;
3170
+ const struct url_test *test;
3171
+ unsigned int i;
3172
+ int rv;
3173
+
3174
+ for (i = 0; i < (sizeof(url_tests) / sizeof(url_tests[0])); i++) {
3175
+ test = &url_tests[i];
3176
+ memset(&u, 0, sizeof(u));
3177
+
3178
+ rv = http_parser_parse_url(test->url,
3179
+ strlen(test->url),
3180
+ test->is_connect,
3181
+ &u);
3182
+
3183
+ if (test->rv == 0) {
3184
+ if (rv != 0) {
3185
+ printf("\n*** http_parser_parse_url(\"%s\") \"%s\" test failed, "
3186
+ "unexpected rv %d ***\n\n", test->url, test->name, rv);
3187
+ abort();
3188
+ }
3189
+
3190
+ if (memcmp(&u, &test->u, sizeof(u)) != 0) {
3191
+ printf("\n*** http_parser_parse_url(\"%s\") \"%s\" failed ***\n",
3192
+ test->url, test->name);
3193
+
3194
+ printf("target http_parser_url:\n");
3195
+ dump_url(test->url, &test->u);
3196
+ printf("result http_parser_url:\n");
3197
+ dump_url(test->url, &u);
3198
+
3199
+ abort();
3200
+ }
3201
+ } else {
3202
+ /* test->rv != 0 */
3203
+ if (rv == 0) {
3204
+ printf("\n*** http_parser_parse_url(\"%s\") \"%s\" test failed, "
3205
+ "unexpected rv %d ***\n\n", test->url, test->name, rv);
3206
+ abort();
3207
+ }
3208
+ }
3209
+ }
3210
+ }
3211
+
3212
+ void
3213
+ test_method_str (void)
3214
+ {
3215
+ assert(0 == strcmp("GET", http_method_str(HTTP_GET)));
3216
+ assert(0 == strcmp("<unknown>", http_method_str(1337)));
3217
+ }
3218
+
3219
+ void
3220
+ test_message (const struct message *message)
3221
+ {
3222
+ size_t raw_len = strlen(message->raw);
3223
+ size_t msg1len;
3224
+ for (msg1len = 0; msg1len < raw_len; msg1len++) {
3225
+ parser_init(message->type);
3226
+
3227
+ size_t read;
3228
+ const char *msg1 = message->raw;
3229
+ const char *msg2 = msg1 + msg1len;
3230
+ size_t msg2len = raw_len - msg1len;
3231
+
3232
+ if (msg1len) {
3233
+ read = parse(msg1, msg1len);
3234
+
3235
+ if (message->upgrade && parser->upgrade && num_messages > 0) {
3236
+ messages[num_messages - 1].upgrade = msg1 + read;
3237
+ goto test;
3238
+ }
3239
+
3240
+ if (read != msg1len) {
3241
+ print_error(msg1, read);
3242
+ abort();
3243
+ }
3244
+ }
3245
+
3246
+
3247
+ read = parse(msg2, msg2len);
3248
+
3249
+ if (message->upgrade && parser->upgrade) {
3250
+ messages[num_messages - 1].upgrade = msg2 + read;
3251
+ goto test;
3252
+ }
3253
+
3254
+ if (read != msg2len) {
3255
+ print_error(msg2, read);
3256
+ abort();
3257
+ }
3258
+
3259
+ read = parse(NULL, 0);
3260
+
3261
+ if (read != 0) {
3262
+ print_error(message->raw, read);
3263
+ abort();
3264
+ }
3265
+
3266
+ test:
3267
+
3268
+ if (num_messages != 1) {
3269
+ printf("\n*** num_messages != 1 after testing '%s' ***\n\n", message->name);
3270
+ abort();
3271
+ }
3272
+
3273
+ if(!message_eq(0, 0, message)) abort();
3274
+
3275
+ parser_free();
3276
+ }
3277
+ }
3278
+
3279
+ void
3280
+ test_message_count_body (const struct message *message)
3281
+ {
3282
+ parser_init(message->type);
3283
+
3284
+ size_t read;
3285
+ size_t l = strlen(message->raw);
3286
+ size_t i, toread;
3287
+ size_t chunk = 4024;
3288
+
3289
+ for (i = 0; i < l; i+= chunk) {
3290
+ toread = MIN(l-i, chunk);
3291
+ read = parse_count_body(message->raw + i, toread);
3292
+ if (read != toread) {
3293
+ print_error(message->raw, read);
3294
+ abort();
3295
+ }
3296
+ }
3297
+
3298
+
3299
+ read = parse_count_body(NULL, 0);
3300
+ if (read != 0) {
3301
+ print_error(message->raw, read);
3302
+ abort();
3303
+ }
3304
+
3305
+ if (num_messages != 1) {
3306
+ printf("\n*** num_messages != 1 after testing '%s' ***\n\n", message->name);
3307
+ abort();
3308
+ }
3309
+
3310
+ if(!message_eq(0, 0, message)) abort();
3311
+
3312
+ parser_free();
3313
+ }
3314
+
3315
+ void
3316
+ test_simple (const char *buf, enum http_errno err_expected)
3317
+ {
3318
+ parser_init(HTTP_REQUEST);
3319
+
3320
+ enum http_errno err;
3321
+
3322
+ parse(buf, strlen(buf));
3323
+ err = HTTP_PARSER_ERRNO(parser);
3324
+ parse(NULL, 0);
3325
+
3326
+ parser_free();
3327
+
3328
+ /* In strict mode, allow us to pass with an unexpected HPE_STRICT as
3329
+ * long as the caller isn't expecting success.
3330
+ */
3331
+ #if HTTP_PARSER_STRICT
3332
+ if (err_expected != err && err_expected != HPE_OK && err != HPE_STRICT) {
3333
+ #else
3334
+ if (err_expected != err) {
3335
+ #endif
3336
+ fprintf(stderr, "\n*** test_simple expected %s, but saw %s ***\n\n%s\n",
3337
+ http_errno_name(err_expected), http_errno_name(err), buf);
3338
+ abort();
3339
+ }
3340
+ }
3341
+
3342
+ void
3343
+ test_invalid_header_content (int req, const char* str)
3344
+ {
3345
+ http_parser parser;
3346
+ http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
3347
+ size_t parsed;
3348
+ const char *buf;
3349
+ buf = req ?
3350
+ "GET / HTTP/1.1\r\n" :
3351
+ "HTTP/1.1 200 OK\r\n";
3352
+ parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
3353
+ assert(parsed == strlen(buf));
3354
+
3355
+ buf = str;
3356
+ size_t buflen = strlen(buf);
3357
+
3358
+ parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
3359
+ if (parsed != buflen) {
3360
+ assert(HTTP_PARSER_ERRNO(&parser) == HPE_INVALID_HEADER_TOKEN);
3361
+ return;
3362
+ }
3363
+
3364
+ fprintf(stderr,
3365
+ "\n*** Error expected but none in invalid header content test ***\n");
3366
+ abort();
3367
+ }
3368
+
3369
+ void
3370
+ test_invalid_header_field_content_error (int req)
3371
+ {
3372
+ test_invalid_header_content(req, "Foo: F\01ailure");
3373
+ test_invalid_header_content(req, "Foo: B\02ar");
3374
+ }
3375
+
3376
+ void
3377
+ test_invalid_header_field (int req, const char* str)
3378
+ {
3379
+ http_parser parser;
3380
+ http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
3381
+ size_t parsed;
3382
+ const char *buf;
3383
+ buf = req ?
3384
+ "GET / HTTP/1.1\r\n" :
3385
+ "HTTP/1.1 200 OK\r\n";
3386
+ parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
3387
+ assert(parsed == strlen(buf));
3388
+
3389
+ buf = str;
3390
+ size_t buflen = strlen(buf);
3391
+
3392
+ parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
3393
+ if (parsed != buflen) {
3394
+ assert(HTTP_PARSER_ERRNO(&parser) == HPE_INVALID_HEADER_TOKEN);
3395
+ return;
3396
+ }
3397
+
3398
+ fprintf(stderr,
3399
+ "\n*** Error expected but none in invalid header token test ***\n");
3400
+ abort();
3401
+ }
3402
+
3403
+ void
3404
+ test_invalid_header_field_token_error (int req)
3405
+ {
3406
+ test_invalid_header_field(req, "Fo@: Failure");
3407
+ test_invalid_header_field(req, "Foo\01\test: Bar");
3408
+ }
3409
+
3410
+ void
3411
+ test_double_content_length_error (int req)
3412
+ {
3413
+ http_parser parser;
3414
+ http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
3415
+ size_t parsed;
3416
+ const char *buf;
3417
+ buf = req ?
3418
+ "GET / HTTP/1.1\r\n" :
3419
+ "HTTP/1.1 200 OK\r\n";
3420
+ parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
3421
+ assert(parsed == strlen(buf));
3422
+
3423
+ buf = "Content-Length: 0\r\nContent-Length: 1\r\n\r\n";
3424
+ size_t buflen = strlen(buf);
3425
+
3426
+ parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
3427
+ if (parsed != buflen) {
3428
+ assert(HTTP_PARSER_ERRNO(&parser) == HPE_UNEXPECTED_CONTENT_LENGTH);
3429
+ return;
3430
+ }
3431
+
3432
+ fprintf(stderr,
3433
+ "\n*** Error expected but none in double content-length test ***\n");
3434
+ abort();
3435
+ }
3436
+
3437
+ void
3438
+ test_chunked_content_length_error (int req)
3439
+ {
3440
+ http_parser parser;
3441
+ http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
3442
+ size_t parsed;
3443
+ const char *buf;
3444
+ buf = req ?
3445
+ "GET / HTTP/1.1\r\n" :
3446
+ "HTTP/1.1 200 OK\r\n";
3447
+ parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
3448
+ assert(parsed == strlen(buf));
3449
+
3450
+ buf = "Transfer-Encoding: chunked\r\nContent-Length: 1\r\n\r\n";
3451
+ size_t buflen = strlen(buf);
3452
+
3453
+ parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
3454
+ if (parsed != buflen) {
3455
+ assert(HTTP_PARSER_ERRNO(&parser) == HPE_UNEXPECTED_CONTENT_LENGTH);
3456
+ return;
3457
+ }
3458
+
3459
+ fprintf(stderr,
3460
+ "\n*** Error expected but none in chunked content-length test ***\n");
3461
+ abort();
3462
+ }
3463
+
3464
+ void
3465
+ test_header_cr_no_lf_error (int req)
3466
+ {
3467
+ http_parser parser;
3468
+ http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
3469
+ size_t parsed;
3470
+ const char *buf;
3471
+ buf = req ?
3472
+ "GET / HTTP/1.1\r\n" :
3473
+ "HTTP/1.1 200 OK\r\n";
3474
+ parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
3475
+ assert(parsed == strlen(buf));
3476
+
3477
+ buf = "Foo: 1\rBar: 1\r\n\r\n";
3478
+ size_t buflen = strlen(buf);
3479
+
3480
+ parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
3481
+ if (parsed != buflen) {
3482
+ assert(HTTP_PARSER_ERRNO(&parser) == HPE_LF_EXPECTED);
3483
+ return;
3484
+ }
3485
+
3486
+ fprintf(stderr,
3487
+ "\n*** Error expected but none in header whitespace test ***\n");
3488
+ abort();
3489
+ }
3490
+
3491
+ void
3492
+ test_header_overflow_error (int req)
3493
+ {
3494
+ http_parser parser;
3495
+ http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
3496
+ size_t parsed;
3497
+ const char *buf;
3498
+ buf = req ? "GET / HTTP/1.1\r\n" : "HTTP/1.0 200 OK\r\n";
3499
+ parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
3500
+ assert(parsed == strlen(buf));
3501
+
3502
+ buf = "header-key: header-value\r\n";
3503
+ size_t buflen = strlen(buf);
3504
+
3505
+ int i;
3506
+ for (i = 0; i < 10000; i++) {
3507
+ parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
3508
+ if (parsed != buflen) {
3509
+ //fprintf(stderr, "error found on iter %d\n", i);
3510
+ assert(HTTP_PARSER_ERRNO(&parser) == HPE_HEADER_OVERFLOW);
3511
+ return;
3512
+ }
3513
+ }
3514
+
3515
+ fprintf(stderr, "\n*** Error expected but none in header overflow test ***\n");
3516
+ abort();
3517
+ }
3518
+
3519
+
3520
+ void
3521
+ test_header_nread_value ()
3522
+ {
3523
+ http_parser parser;
3524
+ http_parser_init(&parser, HTTP_REQUEST);
3525
+ size_t parsed;
3526
+ const char *buf;
3527
+ buf = "GET / HTTP/1.1\r\nheader: value\nhdr: value\r\n";
3528
+ parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
3529
+ assert(parsed == strlen(buf));
3530
+
3531
+ assert(parser.nread == strlen(buf));
3532
+ }
3533
+
3534
+
3535
+ static void
3536
+ test_content_length_overflow (const char *buf, size_t buflen, int expect_ok)
3537
+ {
3538
+ http_parser parser;
3539
+ http_parser_init(&parser, HTTP_RESPONSE);
3540
+ http_parser_execute(&parser, &settings_null, buf, buflen);
3541
+
3542
+ if (expect_ok)
3543
+ assert(HTTP_PARSER_ERRNO(&parser) == HPE_OK);
3544
+ else
3545
+ assert(HTTP_PARSER_ERRNO(&parser) == HPE_INVALID_CONTENT_LENGTH);
3546
+ }
3547
+
3548
+ void
3549
+ test_header_content_length_overflow_error (void)
3550
+ {
3551
+ #define X(size) \
3552
+ "HTTP/1.1 200 OK\r\n" \
3553
+ "Content-Length: " #size "\r\n" \
3554
+ "\r\n"
3555
+ const char a[] = X(1844674407370955160); /* 2^64 / 10 - 1 */
3556
+ const char b[] = X(18446744073709551615); /* 2^64-1 */
3557
+ const char c[] = X(18446744073709551616); /* 2^64 */
3558
+ #undef X
3559
+ test_content_length_overflow(a, sizeof(a) - 1, 1); /* expect ok */
3560
+ test_content_length_overflow(b, sizeof(b) - 1, 0); /* expect failure */
3561
+ test_content_length_overflow(c, sizeof(c) - 1, 0); /* expect failure */
3562
+ }
3563
+
3564
+ void
3565
+ test_chunk_content_length_overflow_error (void)
3566
+ {
3567
+ #define X(size) \
3568
+ "HTTP/1.1 200 OK\r\n" \
3569
+ "Transfer-Encoding: chunked\r\n" \
3570
+ "\r\n" \
3571
+ #size "\r\n" \
3572
+ "..."
3573
+ const char a[] = X(FFFFFFFFFFFFFFE); /* 2^64 / 16 - 1 */
3574
+ const char b[] = X(FFFFFFFFFFFFFFFF); /* 2^64-1 */
3575
+ const char c[] = X(10000000000000000); /* 2^64 */
3576
+ #undef X
3577
+ test_content_length_overflow(a, sizeof(a) - 1, 1); /* expect ok */
3578
+ test_content_length_overflow(b, sizeof(b) - 1, 0); /* expect failure */
3579
+ test_content_length_overflow(c, sizeof(c) - 1, 0); /* expect failure */
3580
+ }
3581
+
3582
+ void
3583
+ test_no_overflow_long_body (int req, size_t length)
3584
+ {
3585
+ http_parser parser;
3586
+ http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
3587
+ size_t parsed;
3588
+ size_t i;
3589
+ char buf1[3000];
3590
+ size_t buf1len = sprintf(buf1, "%s\r\nConnection: Keep-Alive\r\nContent-Length: %lu\r\n\r\n",
3591
+ req ? "POST / HTTP/1.0" : "HTTP/1.0 200 OK", (unsigned long)length);
3592
+ parsed = http_parser_execute(&parser, &settings_null, buf1, buf1len);
3593
+ if (parsed != buf1len)
3594
+ goto err;
3595
+
3596
+ for (i = 0; i < length; i++) {
3597
+ char foo = 'a';
3598
+ parsed = http_parser_execute(&parser, &settings_null, &foo, 1);
3599
+ if (parsed != 1)
3600
+ goto err;
3601
+ }
3602
+
3603
+ parsed = http_parser_execute(&parser, &settings_null, buf1, buf1len);
3604
+ if (parsed != buf1len) goto err;
3605
+ return;
3606
+
3607
+ err:
3608
+ fprintf(stderr,
3609
+ "\n*** error in test_no_overflow_long_body %s of length %lu ***\n",
3610
+ req ? "REQUEST" : "RESPONSE",
3611
+ (unsigned long)length);
3612
+ abort();
3613
+ }
3614
+
3615
+ void
3616
+ test_multiple3 (const struct message *r1, const struct message *r2, const struct message *r3)
3617
+ {
3618
+ int message_count = count_parsed_messages(3, r1, r2, r3);
3619
+
3620
+ char total[ strlen(r1->raw)
3621
+ + strlen(r2->raw)
3622
+ + strlen(r3->raw)
3623
+ + 1
3624
+ ];
3625
+ total[0] = '\0';
3626
+
3627
+ strcat(total, r1->raw);
3628
+ strcat(total, r2->raw);
3629
+ strcat(total, r3->raw);
3630
+
3631
+ parser_init(r1->type);
3632
+
3633
+ size_t read;
3634
+
3635
+ read = parse(total, strlen(total));
3636
+
3637
+ if (parser->upgrade) {
3638
+ upgrade_message_fix(total, read, 3, r1, r2, r3);
3639
+ goto test;
3640
+ }
3641
+
3642
+ if (read != strlen(total)) {
3643
+ print_error(total, read);
3644
+ abort();
3645
+ }
3646
+
3647
+ read = parse(NULL, 0);
3648
+
3649
+ if (read != 0) {
3650
+ print_error(total, read);
3651
+ abort();
3652
+ }
3653
+
3654
+ test:
3655
+
3656
+ if (message_count != num_messages) {
3657
+ fprintf(stderr, "\n\n*** Parser didn't see 3 messages only %d *** \n", num_messages);
3658
+ abort();
3659
+ }
3660
+
3661
+ if (!message_eq(0, 0, r1)) abort();
3662
+ if (message_count > 1 && !message_eq(1, 0, r2)) abort();
3663
+ if (message_count > 2 && !message_eq(2, 0, r3)) abort();
3664
+
3665
+ parser_free();
3666
+ }
3667
+
3668
+ /* SCAN through every possible breaking to make sure the
3669
+ * parser can handle getting the content in any chunks that
3670
+ * might come from the socket
3671
+ */
3672
+ void
3673
+ test_scan (const struct message *r1, const struct message *r2, const struct message *r3)
3674
+ {
3675
+ char total[80*1024] = "\0";
3676
+ char buf1[80*1024] = "\0";
3677
+ char buf2[80*1024] = "\0";
3678
+ char buf3[80*1024] = "\0";
3679
+
3680
+ strcat(total, r1->raw);
3681
+ strcat(total, r2->raw);
3682
+ strcat(total, r3->raw);
3683
+
3684
+ size_t read;
3685
+
3686
+ int total_len = strlen(total);
3687
+
3688
+ int total_ops = 2 * (total_len - 1) * (total_len - 2) / 2;
3689
+ int ops = 0 ;
3690
+
3691
+ size_t buf1_len, buf2_len, buf3_len;
3692
+ int message_count = count_parsed_messages(3, r1, r2, r3);
3693
+
3694
+ int i,j,type_both;
3695
+ for (type_both = 0; type_both < 2; type_both ++ ) {
3696
+ for (j = 2; j < total_len; j ++ ) {
3697
+ for (i = 1; i < j; i ++ ) {
3698
+
3699
+ if (ops % 1000 == 0) {
3700
+ printf("\b\b\b\b%3.0f%%", 100 * (float)ops /(float)total_ops);
3701
+ fflush(stdout);
3702
+ }
3703
+ ops += 1;
3704
+
3705
+ parser_init(type_both ? HTTP_BOTH : r1->type);
3706
+
3707
+ buf1_len = i;
3708
+ strlncpy(buf1, sizeof(buf1), total, buf1_len);
3709
+ buf1[buf1_len] = 0;
3710
+
3711
+ buf2_len = j - i;
3712
+ strlncpy(buf2, sizeof(buf1), total+i, buf2_len);
3713
+ buf2[buf2_len] = 0;
3714
+
3715
+ buf3_len = total_len - j;
3716
+ strlncpy(buf3, sizeof(buf1), total+j, buf3_len);
3717
+ buf3[buf3_len] = 0;
3718
+
3719
+ read = parse(buf1, buf1_len);
3720
+
3721
+ if (parser->upgrade) goto test;
3722
+
3723
+ if (read != buf1_len) {
3724
+ print_error(buf1, read);
3725
+ goto error;
3726
+ }
3727
+
3728
+ read += parse(buf2, buf2_len);
3729
+
3730
+ if (parser->upgrade) goto test;
3731
+
3732
+ if (read != buf1_len + buf2_len) {
3733
+ print_error(buf2, read);
3734
+ goto error;
3735
+ }
3736
+
3737
+ read += parse(buf3, buf3_len);
3738
+
3739
+ if (parser->upgrade) goto test;
3740
+
3741
+ if (read != buf1_len + buf2_len + buf3_len) {
3742
+ print_error(buf3, read);
3743
+ goto error;
3744
+ }
3745
+
3746
+ parse(NULL, 0);
3747
+
3748
+ test:
3749
+ if (parser->upgrade) {
3750
+ upgrade_message_fix(total, read, 3, r1, r2, r3);
3751
+ }
3752
+
3753
+ if (message_count != num_messages) {
3754
+ fprintf(stderr, "\n\nParser didn't see %d messages only %d\n",
3755
+ message_count, num_messages);
3756
+ goto error;
3757
+ }
3758
+
3759
+ if (!message_eq(0, 0, r1)) {
3760
+ fprintf(stderr, "\n\nError matching messages[0] in test_scan.\n");
3761
+ goto error;
3762
+ }
3763
+
3764
+ if (message_count > 1 && !message_eq(1, 0, r2)) {
3765
+ fprintf(stderr, "\n\nError matching messages[1] in test_scan.\n");
3766
+ goto error;
3767
+ }
3768
+
3769
+ if (message_count > 2 && !message_eq(2, 0, r3)) {
3770
+ fprintf(stderr, "\n\nError matching messages[2] in test_scan.\n");
3771
+ goto error;
3772
+ }
3773
+
3774
+ parser_free();
3775
+ }
3776
+ }
3777
+ }
3778
+ puts("\b\b\b\b100%");
3779
+ return;
3780
+
3781
+ error:
3782
+ fprintf(stderr, "i=%d j=%d\n", i, j);
3783
+ fprintf(stderr, "buf1 (%u) %s\n\n", (unsigned int)buf1_len, buf1);
3784
+ fprintf(stderr, "buf2 (%u) %s\n\n", (unsigned int)buf2_len , buf2);
3785
+ fprintf(stderr, "buf3 (%u) %s\n", (unsigned int)buf3_len, buf3);
3786
+ abort();
3787
+ }
3788
+
3789
+ // user required to free the result
3790
+ // string terminated by \0
3791
+ char *
3792
+ create_large_chunked_message (int body_size_in_kb, const char* headers)
3793
+ {
3794
+ int i;
3795
+ size_t wrote = 0;
3796
+ size_t headers_len = strlen(headers);
3797
+ size_t bufsize = headers_len + (5+1024+2)*body_size_in_kb + 6;
3798
+ char * buf = malloc(bufsize);
3799
+
3800
+ memcpy(buf, headers, headers_len);
3801
+ wrote += headers_len;
3802
+
3803
+ for (i = 0; i < body_size_in_kb; i++) {
3804
+ // write 1kb chunk into the body.
3805
+ memcpy(buf + wrote, "400\r\n", 5);
3806
+ wrote += 5;
3807
+ memset(buf + wrote, 'C', 1024);
3808
+ wrote += 1024;
3809
+ strcpy(buf + wrote, "\r\n");
3810
+ wrote += 2;
3811
+ }
3812
+
3813
+ memcpy(buf + wrote, "0\r\n\r\n", 6);
3814
+ wrote += 6;
3815
+ assert(wrote == bufsize);
3816
+
3817
+ return buf;
3818
+ }
3819
+
3820
+ /* Verify that we can pause parsing at any of the bytes in the
3821
+ * message and still get the result that we're expecting. */
3822
+ void
3823
+ test_message_pause (const struct message *msg)
3824
+ {
3825
+ char *buf = (char*) msg->raw;
3826
+ size_t buflen = strlen(msg->raw);
3827
+ size_t nread;
3828
+
3829
+ parser_init(msg->type);
3830
+
3831
+ do {
3832
+ nread = parse_pause(buf, buflen);
3833
+
3834
+ // We can only set the upgrade buffer once we've gotten our message
3835
+ // completion callback.
3836
+ if (messages[0].message_complete_cb_called &&
3837
+ msg->upgrade &&
3838
+ parser->upgrade) {
3839
+ messages[0].upgrade = buf + nread;
3840
+ goto test;
3841
+ }
3842
+
3843
+ if (nread < buflen) {
3844
+
3845
+ // Not much do to if we failed a strict-mode check
3846
+ if (HTTP_PARSER_ERRNO(parser) == HPE_STRICT) {
3847
+ parser_free();
3848
+ return;
3849
+ }
3850
+
3851
+ assert (HTTP_PARSER_ERRNO(parser) == HPE_PAUSED);
3852
+ }
3853
+
3854
+ buf += nread;
3855
+ buflen -= nread;
3856
+ http_parser_pause(parser, 0);
3857
+ } while (buflen > 0);
3858
+
3859
+ nread = parse_pause(NULL, 0);
3860
+ assert (nread == 0);
3861
+
3862
+ test:
3863
+ if (num_messages != 1) {
3864
+ printf("\n*** num_messages != 1 after testing '%s' ***\n\n", msg->name);
3865
+ abort();
3866
+ }
3867
+
3868
+ if(!message_eq(0, 0, msg)) abort();
3869
+
3870
+ parser_free();
3871
+ }
3872
+
3873
+ /* Verify that body and next message won't be parsed in responses to CONNECT */
3874
+ void
3875
+ test_message_connect (const struct message *msg)
3876
+ {
3877
+ char *buf = (char*) msg->raw;
3878
+ size_t buflen = strlen(msg->raw);
3879
+
3880
+ parser_init(msg->type);
3881
+
3882
+ parse_connect(buf, buflen);
3883
+
3884
+ if (num_messages != 1) {
3885
+ printf("\n*** num_messages != 1 after testing '%s' ***\n\n", msg->name);
3886
+ abort();
3887
+ }
3888
+
3889
+ if(!message_eq(0, 1, msg)) abort();
3890
+
3891
+ parser_free();
3892
+ }
3893
+
3894
+ int
3895
+ main (void)
3896
+ {
3897
+ parser = NULL;
3898
+ int i, j, k;
3899
+ int request_count;
3900
+ int response_count;
3901
+ unsigned long version;
3902
+ unsigned major;
3903
+ unsigned minor;
3904
+ unsigned patch;
3905
+
3906
+ version = http_parser_version();
3907
+ major = (version >> 16) & 255;
3908
+ minor = (version >> 8) & 255;
3909
+ patch = version & 255;
3910
+ printf("http_parser v%u.%u.%u (0x%06lx)\n", major, minor, patch, version);
3911
+
3912
+ printf("sizeof(http_parser) = %u\n", (unsigned int)sizeof(http_parser));
3913
+
3914
+ for (request_count = 0; requests[request_count].name; request_count++);
3915
+ for (response_count = 0; responses[response_count].name; response_count++);
3916
+
3917
+ //// API
3918
+ test_preserve_data();
3919
+ test_parse_url();
3920
+ test_method_str();
3921
+
3922
+ //// NREAD
3923
+ test_header_nread_value();
3924
+
3925
+ //// OVERFLOW CONDITIONS
3926
+
3927
+ test_header_overflow_error(HTTP_REQUEST);
3928
+ test_no_overflow_long_body(HTTP_REQUEST, 1000);
3929
+ test_no_overflow_long_body(HTTP_REQUEST, 100000);
3930
+
3931
+ test_header_overflow_error(HTTP_RESPONSE);
3932
+ test_no_overflow_long_body(HTTP_RESPONSE, 1000);
3933
+ test_no_overflow_long_body(HTTP_RESPONSE, 100000);
3934
+
3935
+ test_header_content_length_overflow_error();
3936
+ test_chunk_content_length_overflow_error();
3937
+
3938
+ //// HEADER FIELD CONDITIONS
3939
+ test_double_content_length_error(HTTP_REQUEST);
3940
+ test_chunked_content_length_error(HTTP_REQUEST);
3941
+ test_header_cr_no_lf_error(HTTP_REQUEST);
3942
+ test_invalid_header_field_token_error(HTTP_REQUEST);
3943
+ test_invalid_header_field_content_error(HTTP_REQUEST);
3944
+ test_double_content_length_error(HTTP_RESPONSE);
3945
+ test_chunked_content_length_error(HTTP_RESPONSE);
3946
+ test_header_cr_no_lf_error(HTTP_RESPONSE);
3947
+ test_invalid_header_field_token_error(HTTP_RESPONSE);
3948
+ test_invalid_header_field_content_error(HTTP_RESPONSE);
3949
+
3950
+ //// RESPONSES
3951
+
3952
+ for (i = 0; i < response_count; i++) {
3953
+ test_message(&responses[i]);
3954
+ }
3955
+
3956
+ for (i = 0; i < response_count; i++) {
3957
+ test_message_pause(&responses[i]);
3958
+ }
3959
+
3960
+ for (i = 0; i < response_count; i++) {
3961
+ test_message_connect(&responses[i]);
3962
+ }
3963
+
3964
+ for (i = 0; i < response_count; i++) {
3965
+ if (!responses[i].should_keep_alive) continue;
3966
+ for (j = 0; j < response_count; j++) {
3967
+ if (!responses[j].should_keep_alive) continue;
3968
+ for (k = 0; k < response_count; k++) {
3969
+ test_multiple3(&responses[i], &responses[j], &responses[k]);
3970
+ }
3971
+ }
3972
+ }
3973
+
3974
+ test_message_count_body(&responses[NO_HEADERS_NO_BODY_404]);
3975
+ test_message_count_body(&responses[TRAILING_SPACE_ON_CHUNKED_BODY]);
3976
+
3977
+ // test very large chunked response
3978
+ {
3979
+ char * msg = create_large_chunked_message(31337,
3980
+ "HTTP/1.0 200 OK\r\n"
3981
+ "Transfer-Encoding: chunked\r\n"
3982
+ "Content-Type: text/plain\r\n"
3983
+ "\r\n");
3984
+ struct message large_chunked =
3985
+ {.name= "large chunked"
3986
+ ,.type= HTTP_RESPONSE
3987
+ ,.raw= msg
3988
+ ,.should_keep_alive= FALSE
3989
+ ,.message_complete_on_eof= FALSE
3990
+ ,.http_major= 1
3991
+ ,.http_minor= 0
3992
+ ,.status_code= 200
3993
+ ,.response_status= "OK"
3994
+ ,.num_headers= 2
3995
+ ,.headers=
3996
+ { { "Transfer-Encoding", "chunked" }
3997
+ , { "Content-Type", "text/plain" }
3998
+ }
3999
+ ,.body_size= 31337*1024
4000
+ ,.num_chunks_complete= 31338
4001
+ };
4002
+ for (i = 0; i < MAX_CHUNKS; i++) {
4003
+ large_chunked.chunk_lengths[i] = 1024;
4004
+ }
4005
+ test_message_count_body(&large_chunked);
4006
+ free(msg);
4007
+ }
4008
+
4009
+
4010
+
4011
+ printf("response scan 1/2 ");
4012
+ test_scan( &responses[TRAILING_SPACE_ON_CHUNKED_BODY]
4013
+ , &responses[NO_BODY_HTTP10_KA_204]
4014
+ , &responses[NO_REASON_PHRASE]
4015
+ );
4016
+
4017
+ printf("response scan 2/2 ");
4018
+ test_scan( &responses[BONJOUR_MADAME_FR]
4019
+ , &responses[UNDERSTORE_HEADER_KEY]
4020
+ , &responses[NO_CARRIAGE_RET]
4021
+ );
4022
+
4023
+ puts("responses okay");
4024
+
4025
+
4026
+ /// REQUESTS
4027
+
4028
+ test_simple("GET / HTP/1.1\r\n\r\n", HPE_INVALID_VERSION);
4029
+
4030
+ // Extended characters - see nodejs/test/parallel/test-http-headers-obstext.js
4031
+ test_simple("GET / HTTP/1.1\r\n"
4032
+ "Test: Düsseldorf\r\n",
4033
+ HPE_OK);
4034
+
4035
+ // Well-formed but incomplete
4036
+ test_simple("GET / HTTP/1.1\r\n"
4037
+ "Content-Type: text/plain\r\n"
4038
+ "Content-Length: 6\r\n"
4039
+ "\r\n"
4040
+ "fooba",
4041
+ HPE_OK);
4042
+
4043
+ static const char *all_methods[] = {
4044
+ "DELETE",
4045
+ "GET",
4046
+ "HEAD",
4047
+ "POST",
4048
+ "PUT",
4049
+ //"CONNECT", //CONNECT can't be tested like other methods, it's a tunnel
4050
+ "OPTIONS",
4051
+ "TRACE",
4052
+ "COPY",
4053
+ "LOCK",
4054
+ "MKCOL",
4055
+ "MOVE",
4056
+ "PROPFIND",
4057
+ "PROPPATCH",
4058
+ "SEARCH",
4059
+ "UNLOCK",
4060
+ "BIND",
4061
+ "REBIND",
4062
+ "UNBIND",
4063
+ "ACL",
4064
+ "REPORT",
4065
+ "MKACTIVITY",
4066
+ "CHECKOUT",
4067
+ "MERGE",
4068
+ "M-SEARCH",
4069
+ "NOTIFY",
4070
+ "SUBSCRIBE",
4071
+ "UNSUBSCRIBE",
4072
+ "PATCH",
4073
+ "PURGE",
4074
+ "MKCALENDAR",
4075
+ "LINK",
4076
+ "UNLINK",
4077
+ 0 };
4078
+ const char **this_method;
4079
+ for (this_method = all_methods; *this_method; this_method++) {
4080
+ char buf[200];
4081
+ sprintf(buf, "%s / HTTP/1.1\r\n\r\n", *this_method);
4082
+ test_simple(buf, HPE_OK);
4083
+ }
4084
+
4085
+ static const char *bad_methods[] = {
4086
+ "ASDF",
4087
+ "C******",
4088
+ "COLA",
4089
+ "GEM",
4090
+ "GETA",
4091
+ "M****",
4092
+ "MKCOLA",
4093
+ "PROPPATCHA",
4094
+ "PUN",
4095
+ "PX",
4096
+ "SA",
4097
+ "hello world",
4098
+ 0 };
4099
+ for (this_method = bad_methods; *this_method; this_method++) {
4100
+ char buf[200];
4101
+ sprintf(buf, "%s / HTTP/1.1\r\n\r\n", *this_method);
4102
+ test_simple(buf, HPE_INVALID_METHOD);
4103
+ }
4104
+
4105
+ // illegal header field name line folding
4106
+ test_simple("GET / HTTP/1.1\r\n"
4107
+ "name\r\n"
4108
+ " : value\r\n"
4109
+ "\r\n",
4110
+ HPE_INVALID_HEADER_TOKEN);
4111
+
4112
+ const char *dumbfuck2 =
4113
+ "GET / HTTP/1.1\r\n"
4114
+ "X-SSL-Bullshit: -----BEGIN CERTIFICATE-----\r\n"
4115
+ "\tMIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx\r\n"
4116
+ "\tETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT\r\n"
4117
+ "\tAkNBMS0wKwYJKoZIhvcNAQkBFh5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMu\r\n"
4118
+ "\tdWswHhcNMDYwNzI3MTQxMzI4WhcNMDcwNzI3MTQxMzI4WjBbMQswCQYDVQQGEwJV\r\n"
4119
+ "\tSzERMA8GA1UEChMIZVNjaWVuY2UxEzARBgNVBAsTCk1hbmNoZXN0ZXIxCzAJBgNV\r\n"
4120
+ "\tBAcTmrsogriqMWLAk1DMRcwFQYDVQQDEw5taWNoYWVsIHBhcmQYJKoZIhvcNAQEB\r\n"
4121
+ "\tBQADggEPADCCAQoCggEBANPEQBgl1IaKdSS1TbhF3hEXSl72G9J+WC/1R64fAcEF\r\n"
4122
+ "\tW51rEyFYiIeZGx/BVzwXbeBoNUK41OK65sxGuflMo5gLflbwJtHBRIEKAfVVp3YR\r\n"
4123
+ "\tgW7cMA/s/XKgL1GEC7rQw8lIZT8RApukCGqOVHSi/F1SiFlPDxuDfmdiNzL31+sL\r\n"
4124
+ "\t0iwHDdNkGjy5pyBSB8Y79dsSJtCW/iaLB0/n8Sj7HgvvZJ7x0fr+RQjYOUUfrePP\r\n"
4125
+ "\tu2MSpFyf+9BbC/aXgaZuiCvSR+8Snv3xApQY+fULK/xY8h8Ua51iXoQ5jrgu2SqR\r\n"
4126
+ "\twgA7BUi3G8LFzMBl8FRCDYGUDy7M6QaHXx1ZWIPWNKsCAwEAAaOCAiQwggIgMAwG\r\n"
4127
+ "\tA1UdEwEB/wQCMAAwEQYJYIZIAYb4QgHTTPAQDAgWgMA4GA1UdDwEB/wQEAwID6DAs\r\n"
4128
+ "\tBglghkgBhvhCAQ0EHxYdVUsgZS1TY2llbmNlIFVzZXIgQ2VydGlmaWNhdGUwHQYD\r\n"
4129
+ "\tVR0OBBYEFDTt/sf9PeMaZDHkUIldrDYMNTBZMIGaBgNVHSMEgZIwgY+AFAI4qxGj\r\n"
4130
+ "\tloCLDdMVKwiljjDastqooXSkcjBwMQswCQYDVQQGEwJVSzERMA8GA1UEChMIZVNj\r\n"
4131
+ "\taWVuY2UxEjAQBgNVBAsTCUF1dGhvcml0eTELMAkGA1UEAxMCQ0ExLTArBgkqhkiG\r\n"
4132
+ "\t9w0BCQEWHmNhLW9wZXJhdG9yQGdyaWQtc3VwcG9ydC5hYy51a4IBADApBgNVHRIE\r\n"
4133
+ "\tIjAggR5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMudWswGQYDVR0gBBIwEDAO\r\n"
4134
+ "\tBgwrBgEEAdkvAQEBAQYwPQYJYIZIAYb4QgEEBDAWLmh0dHA6Ly9jYS5ncmlkLXN1\r\n"
4135
+ "\tcHBvcnQuYWMudmT4sopwqlBWsvcHViL2NybC9jYWNybC5jcmwwPQYJYIZIAYb4QgEDBDAWLmh0\r\n"
4136
+ "\tdHA6Ly9jYS5ncmlkLXN1cHBvcnQuYWMudWsvcHViL2NybC9jYWNybC5jcmwwPwYD\r\n"
4137
+ "\tVR0fBDgwNjA0oDKgMIYuaHR0cDovL2NhLmdyaWQt5hYy51ay9wdWIv\r\n"
4138
+ "\tY3JsL2NhY3JsLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAS/U4iiooBENGW/Hwmmd3\r\n"
4139
+ "\tXCy6Zrt08YjKCzGNjorT98g8uGsqYjSxv/hmi0qlnlHs+k/3Iobc3LjS5AMYr5L8\r\n"
4140
+ "\tUO7OSkgFFlLHQyC9JzPfmLCAugvzEbyv4Olnsr8hbxF1MbKZoQxUZtMVu29wjfXk\r\n"
4141
+ "\thTeApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK\r\n"
4142
+ "\twTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu\r\n"
4143
+ "\tYhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3\r\n"
4144
+ "\tRA==\r\n"
4145
+ "\t-----END CERTIFICATE-----\r\n"
4146
+ "\r\n";
4147
+ test_simple(dumbfuck2, HPE_OK);
4148
+
4149
+ const char *corrupted_connection =
4150
+ "GET / HTTP/1.1\r\n"
4151
+ "Host: www.example.com\r\n"
4152
+ "Connection\r\033\065\325eep-Alive\r\n"
4153
+ "Accept-Encoding: gzip\r\n"
4154
+ "\r\n";
4155
+ test_simple(corrupted_connection, HPE_INVALID_HEADER_TOKEN);
4156
+
4157
+ const char *corrupted_header_name =
4158
+ "GET / HTTP/1.1\r\n"
4159
+ "Host: www.example.com\r\n"
4160
+ "X-Some-Header\r\033\065\325eep-Alive\r\n"
4161
+ "Accept-Encoding: gzip\r\n"
4162
+ "\r\n";
4163
+ test_simple(corrupted_header_name, HPE_INVALID_HEADER_TOKEN);
4164
+
4165
+ #if 0
4166
+ // NOTE(Wed Nov 18 11:57:27 CET 2009) this seems okay. we just read body
4167
+ // until EOF.
4168
+ //
4169
+ // no content-length
4170
+ // error if there is a body without content length
4171
+ const char *bad_get_no_headers_no_body = "GET /bad_get_no_headers_no_body/world HTTP/1.1\r\n"
4172
+ "Accept: */*\r\n"
4173
+ "\r\n"
4174
+ "HELLO";
4175
+ test_simple(bad_get_no_headers_no_body, 0);
4176
+ #endif
4177
+ /* TODO sending junk and large headers gets rejected */
4178
+
4179
+
4180
+ /* check to make sure our predefined requests are okay */
4181
+ for (i = 0; requests[i].name; i++) {
4182
+ test_message(&requests[i]);
4183
+ }
4184
+
4185
+ for (i = 0; i < request_count; i++) {
4186
+ test_message_pause(&requests[i]);
4187
+ }
4188
+
4189
+ for (i = 0; i < request_count; i++) {
4190
+ if (!requests[i].should_keep_alive) continue;
4191
+ for (j = 0; j < request_count; j++) {
4192
+ if (!requests[j].should_keep_alive) continue;
4193
+ for (k = 0; k < request_count; k++) {
4194
+ test_multiple3(&requests[i], &requests[j], &requests[k]);
4195
+ }
4196
+ }
4197
+ }
4198
+
4199
+ printf("request scan 1/4 ");
4200
+ test_scan( &requests[GET_NO_HEADERS_NO_BODY]
4201
+ , &requests[GET_ONE_HEADER_NO_BODY]
4202
+ , &requests[GET_NO_HEADERS_NO_BODY]
4203
+ );
4204
+
4205
+ printf("request scan 2/4 ");
4206
+ test_scan( &requests[POST_CHUNKED_ALL_YOUR_BASE]
4207
+ , &requests[POST_IDENTITY_BODY_WORLD]
4208
+ , &requests[GET_FUNKY_CONTENT_LENGTH]
4209
+ );
4210
+
4211
+ printf("request scan 3/4 ");
4212
+ test_scan( &requests[TWO_CHUNKS_MULT_ZERO_END]
4213
+ , &requests[CHUNKED_W_TRAILING_HEADERS]
4214
+ , &requests[CHUNKED_W_BULLSHIT_AFTER_LENGTH]
4215
+ );
4216
+
4217
+ printf("request scan 4/4 ");
4218
+ test_scan( &requests[QUERY_URL_WITH_QUESTION_MARK_GET]
4219
+ , &requests[PREFIX_NEWLINE_GET ]
4220
+ , &requests[CONNECT_REQUEST]
4221
+ );
4222
+
4223
+ puts("requests okay");
4224
+
4225
+ return 0;
4226
+ }