ry / libebb

a lightweight HTTP server library for C

This URL has Read+Write access

ry (author)
Sun Feb 22 05:25:22 -0800 2009
commit  a919b9a4ef5d1f293bb1039357fb5acf103a09ad
tree    c8072bdea28c36d6a9ab11e9f04c276daa0f4355
parent  f410928dc674a2b7c322ba7ff2bd194014ebe21a
libebb / test_request_parser.c
117aecd9 » ry 2008-07-21 add copyright headers 1 /* unit tests for request parser
1ee84c77 » ry 2008-08-26 update email address 2 * Copyright 2008 ryah dahl, ry@ndahl.us
117aecd9 » ry 2008-07-21 add copyright headers 3 *
4 * This software may be distributed under the "MIT" license included in the
5 * README
6 */
4a32be1d » ry 2008-07-21 Updated documentation, adde... 7 #include "ebb_request_parser.h"
072669e5 » ry 2008-07-15 Move parser tests to its ow... 8 #include <stdlib.h>
9 #include <assert.h>
10 #include <stdio.h>
11 #include <string.h>
12
8b611115 » ry 2008-07-17 working on the test server.... 13 #define TRUE 1
14 #define FALSE 0
15
b90e2c40 » ry 2008-07-17 fix memory allocation in te... 16 #define MAX_HEADERS 500
17 #define MAX_ELEMENT_SIZE 500
18
a1433934 » ry 2008-07-16 removed all those callbacks... 19 static ebb_request_parser parser;
072669e5 » ry 2008-07-15 Move parser tests to its ow... 20 struct request_data {
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 21 const char *raw;
8a85dac7 » ry 2008-07-19 change request method to a ... 22 int request_method;
b90e2c40 » ry 2008-07-17 fix memory allocation in te... 23 char request_path[MAX_ELEMENT_SIZE];
24 char request_uri[MAX_ELEMENT_SIZE];
25 char fragment[MAX_ELEMENT_SIZE];
26 char query_string[MAX_ELEMENT_SIZE];
27 char body[MAX_ELEMENT_SIZE];
072669e5 » ry 2008-07-15 Move parser tests to its ow... 28 int num_headers;
b90e2c40 » ry 2008-07-17 fix memory allocation in te... 29 char header_fields[MAX_HEADERS][MAX_ELEMENT_SIZE];
30 char header_values[MAX_HEADERS][MAX_ELEMENT_SIZE];
0ca30982 » ry 2008-07-23 added ebb_request_should_ke... 31 int should_keep_alive;
3840ba81 » ry 2008-07-17 more work on the test serve... 32 ebb_request request;
072669e5 » ry 2008-07-15 Move parser tests to its ow... 33 };
34 static struct request_data requests[5];
35 static int num_requests;
36
1d077275 » ry 2008-07-17 fixed bug in test program 37 const struct request_data curl_get =
52542a7c » ry 2008-07-19 clean up 38 { raw: "GET /test HTTP/1.1\r\n"
39 "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"
40 "Host: 0.0.0.0:5000\r\n"
41 "Accept: */*\r\n"
42 "\r\n"
0ca30982 » ry 2008-07-23 added ebb_request_should_ke... 43 , should_keep_alive: TRUE
8a85dac7 » ry 2008-07-19 change request method to a ... 44 , request_method: EBB_GET
1d077275 » ry 2008-07-17 fixed bug in test program 45 , query_string: ""
46 , fragment: ""
47 , request_path: "/test"
48 , request_uri: "/test"
49 , num_headers: 3
50 , header_fields: { "User-Agent", "Host", "Accept" }
51 , header_values: { "curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1", "0.0.0.0:5000", "*/*" }
52 , body: ""
53 };
54
55 const struct request_data firefox_get =
52542a7c » ry 2008-07-19 clean up 56 { raw: "GET /favicon.ico HTTP/1.1\r\n"
57 "Host: 0.0.0.0:5000\r\n"
58 "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0\r\n"
59 "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
60 "Accept-Language: en-us,en;q=0.5\r\n"
61 "Accept-Encoding: gzip,deflate\r\n"
62 "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
63 "Keep-Alive: 300\r\n"
64 "Connection: keep-alive\r\n"
65 "\r\n"
0ca30982 » ry 2008-07-23 added ebb_request_should_ke... 66 , should_keep_alive: TRUE
8a85dac7 » ry 2008-07-19 change request method to a ... 67 , request_method: EBB_GET
1d077275 » ry 2008-07-17 fixed bug in test program 68 , query_string: ""
69 , fragment: ""
70 , request_path: "/favicon.ico"
71 , request_uri: "/favicon.ico"
72 , num_headers: 8
73 , header_fields:
74 { "Host"
75 , "User-Agent"
76 , "Accept"
77 , "Accept-Language"
78 , "Accept-Encoding"
79 , "Accept-Charset"
80 , "Keep-Alive"
81 , "Connection"
82 }
83 , header_values:
84 { "0.0.0.0:5000"
85 , "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0"
86 , "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
87 , "en-us,en;q=0.5"
88 , "gzip,deflate"
89 , "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
90 , "300"
91 , "keep-alive"
92 }
93 , body: ""
94 };
95
4794b36c » ry 2008-07-16 clean up tests 96 const struct request_data dumbfuck =
52542a7c » ry 2008-07-19 clean up 97 { raw: "GET /dumbfuck HTTP/1.1\r\n"
98 "aaaaaaaaaaaaa:++++++++++\r\n"
99 "\r\n"
0ca30982 » ry 2008-07-23 added ebb_request_should_ke... 100 , should_keep_alive: TRUE
8a85dac7 » ry 2008-07-19 change request method to a ... 101 , request_method: EBB_GET
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 102 , query_string: ""
103 , fragment: ""
4794b36c » ry 2008-07-16 clean up tests 104 , request_path: "/dumbfuck"
105 , request_uri: "/dumbfuck"
106 , num_headers: 1
107 , header_fields: { "aaaaaaaaaaaaa" }
108 , header_values: { "++++++++++" }
109 , body: ""
110 };
111
112 const struct request_data fragment_in_uri =
52542a7c » ry 2008-07-19 clean up 113 { raw: "GET /forums/1/topics/2375?page=1#posts-17408 HTTP/1.1\r\n"
114 "\r\n"
0ca30982 » ry 2008-07-23 added ebb_request_should_ke... 115 , should_keep_alive: TRUE
8a85dac7 » ry 2008-07-19 change request method to a ... 116 , request_method: EBB_GET
4794b36c » ry 2008-07-16 clean up tests 117 , query_string: "page=1"
118 , fragment: "posts-17408"
119 , request_path: "/forums/1/topics/2375"
120 /* XXX request uri does not include fragment? */
121 , request_uri: "/forums/1/topics/2375?page=1"
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 122 , num_headers: 0
123 , body: ""
124 };
125
4794b36c » ry 2008-07-16 clean up tests 126
127 // get - no headers - no body
128 const struct request_data get_no_headers_no_body =
52542a7c » ry 2008-07-19 clean up 129 { raw: "GET /get_no_headers_no_body/world HTTP/1.1\r\n"
130 "\r\n"
0ca30982 » ry 2008-07-23 added ebb_request_should_ke... 131 , should_keep_alive: TRUE
8a85dac7 » ry 2008-07-19 change request method to a ... 132 , request_method: EBB_GET
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 133 , query_string: ""
134 , fragment: ""
4794b36c » ry 2008-07-16 clean up tests 135 , request_path: "/get_no_headers_no_body/world"
136 , request_uri: "/get_no_headers_no_body/world"
137 , num_headers: 0
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 138 , body: ""
139 };
140
4794b36c » ry 2008-07-16 clean up tests 141 // get - one header - no body
142 const struct request_data get_one_header_no_body =
52542a7c » ry 2008-07-19 clean up 143 { raw: "GET /get_one_header_no_body HTTP/1.1\r\n"
144 "Accept: */*\r\n"
145 "\r\n"
0ca30982 » ry 2008-07-23 added ebb_request_should_ke... 146 , should_keep_alive: TRUE
8a85dac7 » ry 2008-07-19 change request method to a ... 147 , request_method: EBB_GET
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 148 , query_string: ""
149 , fragment: ""
4794b36c » ry 2008-07-16 clean up tests 150 , request_path: "/get_one_header_no_body"
151 , request_uri: "/get_one_header_no_body"
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 152 , num_headers: 1
153 , header_fields: { "Accept" }
154 , header_values: { "*/*" }
155 , body: ""
156 };
157
158 // get - no headers - body "HELLO"
4794b36c » ry 2008-07-16 clean up tests 159 const struct request_data get_funky_content_length_body_hello =
0ca30982 » ry 2008-07-23 added ebb_request_should_ke... 160 { raw: "GET /get_funky_content_length_body_hello HTTP/1.0\r\n"
52542a7c » ry 2008-07-19 clean up 161 "conTENT-Length: 5\r\n"
162 "\r\n"
163 "HELLO"
0ca30982 » ry 2008-07-23 added ebb_request_should_ke... 164 , should_keep_alive: FALSE
8a85dac7 » ry 2008-07-19 change request method to a ... 165 , request_method: EBB_GET
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 166 , query_string: ""
167 , fragment: ""
4794b36c » ry 2008-07-16 clean up tests 168 , request_path: "/get_funky_content_length_body_hello"
169 , request_uri: "/get_funky_content_length_body_hello"
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 170 , num_headers: 1
171 , header_fields: { "conTENT-Length" }
172 , header_values: { "5" }
173 , body: "HELLO"
174 };
175
176 // post - one header - body "World"
f8b67637 » ry 2008-07-19 add a multipart/form bounda... 177 const struct request_data post_identity_body_world =
178 { raw: "POST /post_identity_body_world?q=search#hey HTTP/1.1\r\n"
52542a7c » ry 2008-07-19 clean up 179 "Accept: */*\r\n"
f8b67637 » ry 2008-07-19 add a multipart/form bounda... 180 "Transfer-Encoding: identity\r\n"
52542a7c » ry 2008-07-19 clean up 181 "Content-Length: 5\r\n"
182 "\r\n"
183 "World"
0ca30982 » ry 2008-07-23 added ebb_request_should_ke... 184 , should_keep_alive: TRUE
8a85dac7 » ry 2008-07-19 change request method to a ... 185 , request_method: EBB_POST
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 186 , query_string: "q=search"
187 , fragment: "hey"
f8b67637 » ry 2008-07-19 add a multipart/form bounda... 188 , request_path: "/post_identity_body_world"
189 , request_uri: "/post_identity_body_world?q=search"
190 , num_headers: 3
191 , header_fields: { "Accept", "Transfer-Encoding", "Content-Length" }
192 , header_values: { "*/*", "identity", "5" }
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 193 , body: "World"
194 };
195
196 // post - no headers - chunked body "all your base are belong to us"
4794b36c » ry 2008-07-16 clean up tests 197 const struct request_data post_chunked_all_your_base =
52542a7c » ry 2008-07-19 clean up 198 { raw: "POST /post_chunked_all_your_base HTTP/1.1\r\n"
199 "Transfer-Encoding: chunked\r\n"
200 "\r\n"
201 "1e\r\nall your base are belong to us\r\n"
202 "0\r\n"
203 "\r\n"
0ca30982 » ry 2008-07-23 added ebb_request_should_ke... 204 , should_keep_alive: TRUE
8a85dac7 » ry 2008-07-19 change request method to a ... 205 , request_method: EBB_POST
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 206 , query_string: ""
207 , fragment: ""
4794b36c » ry 2008-07-16 clean up tests 208 , request_path: "/post_chunked_all_your_base"
209 , request_uri: "/post_chunked_all_your_base"
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 210 , num_headers: 1
211 , header_fields: { "Transfer-Encoding" }
212 , header_values: { "chunked" }
213 , body: "all your base are belong to us"
214 };
215
216 // two chunks ; triple zero ending
4794b36c » ry 2008-07-16 clean up tests 217 const struct request_data two_chunks_mult_zero_end =
52542a7c » ry 2008-07-19 clean up 218 { raw: "POST /two_chunks_mult_zero_end HTTP/1.1\r\n"
219 "Transfer-Encoding: chunked\r\n"
220 "\r\n"
221 "5\r\nhello\r\n"
222 "6\r\n world\r\n"
223 "000\r\n"
224 "\r\n"
0ca30982 » ry 2008-07-23 added ebb_request_should_ke... 225 , should_keep_alive: TRUE
8a85dac7 » ry 2008-07-19 change request method to a ... 226 , request_method: EBB_POST
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 227 , query_string: ""
228 , fragment: ""
4794b36c » ry 2008-07-16 clean up tests 229 , request_path: "/two_chunks_mult_zero_end"
230 , request_uri: "/two_chunks_mult_zero_end"
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 231 , num_headers: 1
232 , header_fields: { "Transfer-Encoding" }
233 , header_values: { "chunked" }
234 , body: "hello world"
235 };
236
237
238 // chunked with trailing headers. blech.
4794b36c » ry 2008-07-16 clean up tests 239 const struct request_data chunked_w_trailing_headers =
52542a7c » ry 2008-07-19 clean up 240 { raw: "POST /chunked_w_trailing_headers HTTP/1.1\r\n"
241 "Transfer-Encoding: chunked\r\n"
242 "\r\n"
243 "5\r\nhello\r\n"
244 "6\r\n world\r\n"
245 "0\r\n"
246 "Vary: *\r\n"
247 "Content-Type: text/plain\r\n"
248 "\r\n"
0ca30982 » ry 2008-07-23 added ebb_request_should_ke... 249 , should_keep_alive: TRUE
8a85dac7 » ry 2008-07-19 change request method to a ... 250 , request_method: EBB_POST
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 251 , query_string: ""
252 , fragment: ""
4794b36c » ry 2008-07-16 clean up tests 253 , request_path: "/chunked_w_trailing_headers"
254 , request_uri: "/chunked_w_trailing_headers"
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 255 , num_headers: 1
256 , header_fields: { "Transfer-Encoding" }
257 , header_values: { "chunked" }
258 , body: "hello world"
259 };
260
261 // with bullshit after the length
4794b36c » ry 2008-07-16 clean up tests 262 const struct request_data chunked_w_bullshit_after_length =
52542a7c » ry 2008-07-19 clean up 263 { raw: "POST /chunked_w_bullshit_after_length HTTP/1.1\r\n"
264 "Transfer-Encoding: chunked\r\n"
265 "\r\n"
266 "5; ihatew3;whatthefuck=aretheseparametersfor\r\nhello\r\n"
267 "6; blahblah; blah\r\n world\r\n"
268 "0\r\n"
269 "\r\n"
0ca30982 » ry 2008-07-23 added ebb_request_should_ke... 270 , should_keep_alive: TRUE
8a85dac7 » ry 2008-07-19 change request method to a ... 271 , request_method: EBB_POST
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 272 , query_string: ""
273 , fragment: ""
4794b36c » ry 2008-07-16 clean up tests 274 , request_path: "/chunked_w_bullshit_after_length"
275 , request_uri: "/chunked_w_bullshit_after_length"
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 276 , num_headers: 1
277 , header_fields: { "Transfer-Encoding" }
278 , header_values: { "chunked" }
279 , body: "hello world"
280 };
281
536351d7 » ry 2008-07-21 removed my funky C style - ... 282 const struct request_data *fixtures[] =
283 { &curl_get
284 , &firefox_get
285 , &dumbfuck
286 , &fragment_in_uri
287 , &get_no_headers_no_body
288 , &get_one_header_no_body
289 , &get_funky_content_length_body_hello
290 , &post_identity_body_world
291 , &post_chunked_all_your_base
292 , &two_chunks_mult_zero_end
293 , &chunked_w_trailing_headers
294 , &chunked_w_bullshit_after_length
295 , NULL
296 };
297
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 298
299 int request_data_eq
300 ( struct request_data *r1
301 , const struct request_data *r2
302 )
303 {
0ca30982 » ry 2008-07-23 added ebb_request_should_ke... 304 if(ebb_request_should_keep_alive(&r1->request) != r2->should_keep_alive) {
305 printf("requests disagree on keep-alive");
306 return FALSE;
307 }
308
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 309 if(0 != strcmp(r1->body, r2->body)) {
310 printf("body '%s' != '%s'\n", r1->body, r2->body);
311 return FALSE;
312 }
313 if(0 != strcmp(r1->fragment, r2->fragment)) {
314 printf("fragment '%s' != '%s'\n", r1->fragment, r2->fragment);
315 return FALSE;
316 }
317 if(0 != strcmp(r1->query_string, r2->query_string)) {
318 printf("query_string '%s' != '%s'\n", r1->query_string, r2->query_string);
319 return FALSE;
320 }
8a85dac7 » ry 2008-07-19 change request method to a ... 321 if(r1->request.method != r2->request_method) {
322 printf("request_method '%d' != '%d'\n", r1->request.method, r2->request_method);
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 323 return FALSE;
324 }
325 if(0 != strcmp(r1->request_path, r2->request_path)) {
326 printf("request_path '%s' != '%s'\n", r1->request_path, r2->request_path);
327 return FALSE;
328 }
329 if(0 != strcmp(r1->request_uri, r2->request_uri)) {
330 printf("request_uri '%s' != '%s'\n", r1->request_uri, r2->request_uri);
331 return FALSE;
332 }
333 if(r1->num_headers != r2->num_headers) {
334 printf("num_headers '%d' != '%d'\n", r1->num_headers, r2->num_headers);
335 return FALSE;
336 }
337 int i;
338 for(i = 0; i < r1->num_headers; i++) {
339 if(0 != strcmp(r1->header_fields[i], r2->header_fields[i])) {
340 printf("header field '%s' != '%s'\n", r1->header_fields[i], r2->header_fields[i]);
341 return FALSE;
342 }
343 if(0 != strcmp(r1->header_values[i], r2->header_values[i])) {
344 printf("header field '%s' != '%s'\n", r1->header_values[i], r2->header_values[i]);
345 return FALSE;
346 }
347 }
348 return TRUE;
349 }
350
351 int request_eq
352 ( int index
353 , const struct request_data *expected
354 )
355 {
356 return request_data_eq(&requests[index], expected);
357 }
358
3840ba81 » ry 2008-07-17 more work on the test serve... 359 void request_complete(ebb_request *info)
072669e5 » ry 2008-07-15 Move parser tests to its ow... 360 {
361 num_requests++;
362 }
363
3177fb5d » ry 2008-07-18 test_request_parser now pas... 364 void request_path_cb(ebb_request *request, const char *p, size_t len)
072669e5 » ry 2008-07-15 Move parser tests to its ow... 365 {
3177fb5d » ry 2008-07-18 test_request_parser now pas... 366 strncat(requests[num_requests].request_path, p, len);
072669e5 » ry 2008-07-15 Move parser tests to its ow... 367 }
368
3177fb5d » ry 2008-07-18 test_request_parser now pas... 369 void request_uri_cb(ebb_request *request, const char *p, size_t len)
072669e5 » ry 2008-07-15 Move parser tests to its ow... 370 {
3177fb5d » ry 2008-07-18 test_request_parser now pas... 371 strncat(requests[num_requests].request_uri, p, len);
072669e5 » ry 2008-07-15 Move parser tests to its ow... 372 }
373
3177fb5d » ry 2008-07-18 test_request_parser now pas... 374 void query_string_cb(ebb_request *request, const char *p, size_t len)
072669e5 » ry 2008-07-15 Move parser tests to its ow... 375 {
3177fb5d » ry 2008-07-18 test_request_parser now pas... 376 strncat(requests[num_requests].query_string, p, len);
072669e5 » ry 2008-07-15 Move parser tests to its ow... 377 }
378
3177fb5d » ry 2008-07-18 test_request_parser now pas... 379 void fragment_cb(ebb_request *request, const char *p, size_t len)
072669e5 » ry 2008-07-15 Move parser tests to its ow... 380 {
3177fb5d » ry 2008-07-18 test_request_parser now pas... 381 strncat(requests[num_requests].fragment, p, len);
072669e5 » ry 2008-07-15 Move parser tests to its ow... 382 }
383
3177fb5d » ry 2008-07-18 test_request_parser now pas... 384 void header_field_cb(ebb_request *request, const char *p, size_t len, int header_index)
072669e5 » ry 2008-07-15 Move parser tests to its ow... 385 {
3177fb5d » ry 2008-07-18 test_request_parser now pas... 386 strncat(requests[num_requests].header_fields[header_index], p, len);
072669e5 » ry 2008-07-15 Move parser tests to its ow... 387 }
388
3177fb5d » ry 2008-07-18 test_request_parser now pas... 389 void header_value_cb(ebb_request *request, const char *p, size_t len, int header_index)
390 {
391 strncat(requests[num_requests].header_values[header_index], p, len);
392 requests[num_requests].num_headers = header_index + 1;
393 }
072669e5 » ry 2008-07-15 Move parser tests to its ow... 394
3177fb5d » ry 2008-07-18 test_request_parser now pas... 395 void body_handler(ebb_request *request, const char *p, size_t len)
072669e5 » ry 2008-07-15 Move parser tests to its ow... 396 {
397 strncat(requests[num_requests].body, p, len);
a32800ae » ry 2008-07-16 Rename a few things. clean up. 398 // printf("body_handler: '%s'\n", requests[num_requests].body);
072669e5 » ry 2008-07-15 Move parser tests to its ow... 399 }
400
028aea9b » ry 2008-07-20 moved most callbacks into e... 401 ebb_request* new_request ()
402 {
403 requests[num_requests].num_headers = 0;
404 requests[num_requests].request_method = -1;
405 requests[num_requests].request_path[0] = 0;
406 requests[num_requests].request_uri[0] = 0;
407 requests[num_requests].fragment[0] = 0;
408 requests[num_requests].query_string[0] = 0;
409 requests[num_requests].body[0] = 0;
410 int i;
411 for(i = 0; i < MAX_HEADERS; i++) {
412 requests[num_requests].header_fields[i][0] = 0;
413 requests[num_requests].header_values[i][0] = 0;
414 }
415
416 ebb_request *r = &requests[num_requests].request;
417 ebb_request_init(r);
418
e33da647 » ry 2008-07-31 renamed callbacks in ebb_re... 419 r->on_complete = request_complete;
420 r->on_header_field = header_field_cb;
421 r->on_header_value = header_value_cb;
422 r->on_path = request_path_cb;
423 r->on_uri = request_uri_cb;
424 r->on_fragment = fragment_cb;
425 r->on_query_string = query_string_cb;
426 r->on_body = body_handler;
a919b9a4 » ry 2009-02-22 clean ups for c++ inclusion 427 r->on_headers_complete = NULL;
028aea9b » ry 2008-07-20 moved most callbacks into e... 428
429 r->data = &requests[num_requests];
430 // printf("new request %d\n", num_requests);
431 return r;
432 }
433
085f3c89 » ry 2008-07-15 Add several tests, fix some... 434 void parser_init()
072669e5 » ry 2008-07-15 Move parser tests to its ow... 435 {
436 num_requests = 0;
437
a1433934 » ry 2008-07-16 removed all those callbacks... 438 ebb_request_parser_init(&parser);
072669e5 » ry 2008-07-15 Move parser tests to its ow... 439
3840ba81 » ry 2008-07-17 more work on the test serve... 440 parser.new_request = new_request;
085f3c89 » ry 2008-07-15 Add several tests, fix some... 441 }
442
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 443 int test_request
444 ( const struct request_data *request_data
445 )
446 {
447 size_t traversed = 0;
448 parser_init();
449
a1433934 » ry 2008-07-16 removed all those callbacks... 450 traversed = ebb_request_parser_execute( &parser
588680de » ry 2008-08-12 refactor body skipping in t... 451 , request_data->raw
452 , strlen(request_data->raw)
453 );
a1433934 » ry 2008-07-16 removed all those callbacks... 454 if( ebb_request_parser_has_error(&parser) )
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 455 return FALSE;
a1433934 » ry 2008-07-16 removed all those callbacks... 456 if(! ebb_request_parser_is_finished(&parser) )
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 457 return FALSE;
458 if(num_requests != 1)
459 return FALSE;
460
461 return request_eq(0, request_data);
462 }
463
085f3c89 » ry 2008-07-15 Add several tests, fix some... 464 int test_error
465 ( const char *buf
466 )
467 {
468 size_t traversed = 0;
469 parser_init();
072669e5 » ry 2008-07-15 Move parser tests to its ow... 470
a1433934 » ry 2008-07-16 removed all those callbacks... 471 traversed = ebb_request_parser_execute(&parser, buf, strlen(buf));
072669e5 » ry 2008-07-15 Move parser tests to its ow... 472
a1433934 » ry 2008-07-16 removed all those callbacks... 473 return ebb_request_parser_has_error(&parser);
072669e5 » ry 2008-07-15 Move parser tests to its ow... 474 }
475
90db87de » ry 2008-07-15 more tests. starting to fee... 476
477 int test_multiple3
478 ( const struct request_data *r1
479 , const struct request_data *r2
480 , const struct request_data *r3
072669e5 » ry 2008-07-15 Move parser tests to its ow... 481 )
482 {
483 char total[80*1024] = "\0";
484
90db87de » ry 2008-07-15 more tests. starting to fee... 485 strcat(total, r1->raw);
486 strcat(total, r2->raw);
487 strcat(total, r3->raw);
072669e5 » ry 2008-07-15 Move parser tests to its ow... 488
489 size_t traversed = 0;
085f3c89 » ry 2008-07-15 Add several tests, fix some... 490 parser_init();
072669e5 » ry 2008-07-15 Move parser tests to its ow... 491
a1433934 » ry 2008-07-16 removed all those callbacks... 492 traversed = ebb_request_parser_execute(&parser, total, strlen(total));
072669e5 » ry 2008-07-15 Move parser tests to its ow... 493
494
a1433934 » ry 2008-07-16 removed all those callbacks... 495 if( ebb_request_parser_has_error(&parser) )
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 496 return FALSE;
a1433934 » ry 2008-07-16 removed all those callbacks... 497 if(! ebb_request_parser_is_finished(&parser) )
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 498 return FALSE;
499 if(num_requests != 3)
500 return FALSE;
072669e5 » ry 2008-07-15 Move parser tests to its ow... 501
3177fb5d » ry 2008-07-18 test_request_parser now pas... 502 if(!request_eq(0, r1)){
503 printf("request 1 error.\n");
504 return FALSE;
505 }
506 if(!request_eq(1, r2)){
507 printf("request 2 error.\n");
508 return FALSE;
509 }
510 if(!request_eq(2, r3)){
511 printf("request 3 error.\n");
512 return FALSE;
513 }
514
515 return TRUE;
90db87de » ry 2008-07-15 more tests. starting to fee... 516 }
517
518 /**
519 * SCAN through every possible breaking to make sure the
520 * parser can handle getting the content in any chunks that
521 * might come from the socket
522 */
523 int test_scan2
524 ( const struct request_data *r1
525 , const struct request_data *r2
526 , const struct request_data *r3
527 )
528 {
529 char total[80*1024] = "\0";
530 char buf1[80*1024] = "\0";
531 char buf2[80*1024] = "\0";
532
533 strcat(total, r1->raw);
534 strcat(total, r2->raw);
535 strcat(total, r3->raw);
536
537 int total_len = strlen(total);
538
539 //printf("total_len = %d\n", total_len);
540 int i;
541 for(i = 1; i < total_len - 1; i ++ ) {
542
543 parser_init();
544
545 int buf1_len = i;
546 strncpy(buf1, total, buf1_len);
547 buf1[buf1_len] = 0;
548
549 int buf2_len = total_len - i;
550 strncpy(buf2, total+i, buf2_len);
551 buf2[buf2_len] = 0;
552
a1433934 » ry 2008-07-16 removed all those callbacks... 553 ebb_request_parser_execute(&parser, buf1, buf1_len);
90db87de » ry 2008-07-15 more tests. starting to fee... 554
a1433934 » ry 2008-07-16 removed all those callbacks... 555 if( ebb_request_parser_has_error(&parser) ) {
90db87de » ry 2008-07-15 more tests. starting to fee... 556 return FALSE;
557 }
558 /*
a1433934 » ry 2008-07-16 removed all those callbacks... 559 if(ebb_request_parser_is_finished(&parser))
90db87de » ry 2008-07-15 more tests. starting to fee... 560 return FALSE;
561 */
562
a1433934 » ry 2008-07-16 removed all those callbacks... 563 ebb_request_parser_execute(&parser, buf2, buf2_len);
90db87de » ry 2008-07-15 more tests. starting to fee... 564
a1433934 » ry 2008-07-16 removed all those callbacks... 565 if( ebb_request_parser_has_error(&parser))
90db87de » ry 2008-07-15 more tests. starting to fee... 566 return FALSE;
a1433934 » ry 2008-07-16 removed all those callbacks... 567 if(!ebb_request_parser_is_finished(&parser))
90db87de » ry 2008-07-15 more tests. starting to fee... 568 return FALSE;
569
570 if(3 != num_requests) {
571 printf("scan error: got %d requests in iteration %d\n", num_requests, i);
572 return FALSE;
573 }
574
575 if(!request_eq(0, r1)) {
576 printf("not maching r1\n");
577 return FALSE;
578 }
579 if(!request_eq(1, r2)) {
580 printf("not maching r2\n");
581 return FALSE;
582 }
583 if(!request_eq(2, r3)) {
584 printf("not maching r3\n");
585 return FALSE;
586 }
587 }
588 return TRUE;
589 }
590
591 int test_scan3
592 ( const struct request_data *r1
593 , const struct request_data *r2
594 , const struct request_data *r3
595 )
596 {
597 char total[80*1024] = "\0";
598 char buf1[80*1024] = "\0";
599 char buf2[80*1024] = "\0";
600 char buf3[80*1024] = "\0";
601
602 strcat(total, r1->raw);
603 strcat(total, r2->raw);
604 strcat(total, r3->raw);
605
606 int total_len = strlen(total);
607
608 //printf("total_len = %d\n", total_len);
609 int i,j;
610 for(j = 2; j < total_len - 1; j ++ ) {
611 for(i = 1; i < j; i ++ ) {
612
613 parser_init();
614
615
616
3177fb5d » ry 2008-07-18 test_request_parser now pas... 617
90db87de » ry 2008-07-15 more tests. starting to fee... 618 int buf1_len = i;
619 strncpy(buf1, total, buf1_len);
620 buf1[buf1_len] = 0;
621
622 int buf2_len = j - i;
623 strncpy(buf2, total+i, buf2_len);
624 buf2[buf2_len] = 0;
625
626 int buf3_len = total_len - j;
627 strncpy(buf3, total+j, buf3_len);
628 buf3[buf3_len] = 0;
629
630 /*
631 printf("buf1: %s - %d\n", buf1, buf1_len);
632 printf("buf2: %s - %d \n", buf2, buf2_len );
633 printf("buf3: %s - %d\n\n", buf3, buf3_len);
634 */
635
a1433934 » ry 2008-07-16 removed all those callbacks... 636 ebb_request_parser_execute(&parser, buf1, buf1_len);
90db87de » ry 2008-07-15 more tests. starting to fee... 637
a1433934 » ry 2008-07-16 removed all those callbacks... 638 if( ebb_request_parser_has_error(&parser) ) {
90db87de » ry 2008-07-15 more tests. starting to fee... 639 return FALSE;
640 }
641
a1433934 » ry 2008-07-16 removed all those callbacks... 642 ebb_request_parser_execute(&parser, buf2, buf2_len);
90db87de » ry 2008-07-15 more tests. starting to fee... 643
a1433934 » ry 2008-07-16 removed all those callbacks... 644 if( ebb_request_parser_has_error(&parser) ) {
90db87de » ry 2008-07-15 more tests. starting to fee... 645 return FALSE;
646 }
647
a1433934 » ry 2008-07-16 removed all those callbacks... 648 ebb_request_parser_execute(&parser, buf3, buf3_len);
90db87de » ry 2008-07-15 more tests. starting to fee... 649
a1433934 » ry 2008-07-16 removed all those callbacks... 650 if( ebb_request_parser_has_error(&parser))
90db87de » ry 2008-07-15 more tests. starting to fee... 651 return FALSE;
a1433934 » ry 2008-07-16 removed all those callbacks... 652 if(!ebb_request_parser_is_finished(&parser))
90db87de » ry 2008-07-15 more tests. starting to fee... 653 return FALSE;
654
655 if(3 != num_requests) {
656 printf("scan error: only got %d requests in iteration %d\n", num_requests, i);
657 return FALSE;
658 }
659
660 if(!request_eq(0, r1)) {
661 printf("not maching r1\n");
662 return FALSE;
663 }
664 if(!request_eq(1, r2)) {
665 printf("not maching r2\n");
666 return FALSE;
667 }
668 if(!request_eq(2, r3)) {
669 printf("not maching r3\n");
670 return FALSE;
671 }
672 }
673 }
674 return TRUE;
072669e5 » ry 2008-07-15 Move parser tests to its ow... 675 }
676
677 int main()
678 {
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 679
072669e5 » ry 2008-07-15 Move parser tests to its ow... 680 assert(test_error("hello world"));
681 assert(test_error("GET / HTP/1.1\r\n\r\n"));
682
1d077275 » ry 2008-07-17 fixed bug in test program 683 assert(test_request(&curl_get));
684 assert(test_request(&firefox_get));
685
072669e5 » ry 2008-07-15 Move parser tests to its ow... 686 // Zed's header tests
687
4794b36c » ry 2008-07-16 clean up tests 688 assert(test_request(&dumbfuck));
072669e5 » ry 2008-07-15 Move parser tests to its ow... 689
690 const char *dumbfuck2 = "GET / HTTP/1.1\r\nX-SSL-Bullshit: -----BEGIN CERTIFICATE-----\r\n\tMIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx\r\n\tETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT\r\n\tAkNBMS0wKwYJKoZIhvcNAQkBFh5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMu\r\n\tdWswHhcNMDYwNzI3MTQxMzI4WhcNMDcwNzI3MTQxMzI4WjBbMQswCQYDVQQGEwJV\r\n\tSzERMA8GA1UEChMIZVNjaWVuY2UxEzARBgNVBAsTCk1hbmNoZXN0ZXIxCzAJBgNV\r\n\tBAcTmrsogriqMWLAk1DMRcwFQYDVQQDEw5taWNoYWVsIHBhcmQYJKoZIhvcNAQEB\r\n\tBQADggEPADCCAQoCggEBANPEQBgl1IaKdSS1TbhF3hEXSl72G9J+WC/1R64fAcEF\r\n\tW51rEyFYiIeZGx/BVzwXbeBoNUK41OK65sxGuflMo5gLflbwJtHBRIEKAfVVp3YR\r\n\tgW7cMA/s/XKgL1GEC7rQw8lIZT8RApukCGqOVHSi/F1SiFlPDxuDfmdiNzL31+sL\r\n\t0iwHDdNkGjy5pyBSB8Y79dsSJtCW/iaLB0/n8Sj7HgvvZJ7x0fr+RQjYOUUfrePP\r\n\tu2MSpFyf+9BbC/aXgaZuiCvSR+8Snv3xApQY+fULK/xY8h8Ua51iXoQ5jrgu2SqR\r\n\twgA7BUi3G8LFzMBl8FRCDYGUDy7M6QaHXx1ZWIPWNKsCAwEAAaOCAiQwggIgMAwG\r\n\tA1UdEwEB/wQCMAAwEQYJYIZIAYb4QgEBBAQDAgWgMA4GA1UdDwEB/wQEAwID6DAs\r\n\tBglghkgBhvhCAQ0EHxYdVUsgZS1TY2llbmNlIFVzZXIgQ2VydGlmaWNhdGUwHQYD\r\n\tVR0OBBYEFDTt/sf9PeMaZDHkUIldrDYMNTBZMIGaBgNVHSMEgZIwgY+AFAI4qxGj\r\n\tloCLDdMVKwiljjDastqooXSkcjBwMQswCQYDVQQGEwJVSzERMA8GA1UEChMIZVNj\r\n\taWVuY2UxEjAQBgNVBAsTCUF1dGhvcml0eTELMAkGA1UEAxMCQ0ExLTArBgkqhkiG\r\n\t9w0BCQEWHmNhLW9wZXJhdG9yQGdyaWQtc3VwcG9ydC5hYy51a4IBADApBgNVHRIE\r\n\tIjAggR5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMudWswGQYDVR0gBBIwEDAO\r\n\tBgwrBgEEAdkvAQEBAQYwPQYJYIZIAYb4QgEEBDAWLmh0dHA6Ly9jYS5ncmlkLXN1\r\n\tcHBvcnQuYWMudmT4sopwqlBWsvcHViL2NybC9jYWNybC5jcmwwPQYJYIZIAYb4QgEDBDAWLmh0\r\n\tdHA6Ly9jYS5ncmlkLXN1cHBvcnQuYWMudWsvcHViL2NybC9jYWNybC5jcmwwPwYD\r\n\tVR0fBDgwNjA0oDKgMIYuaHR0cDovL2NhLmdyaWQt5hYy51ay9wdWIv\r\n\tY3JsL2NhY3JsLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAS/U4iiooBENGW/Hwmmd3\r\n\tXCy6Zrt08YjKCzGNjorT98g8uGsqYjSxv/hmi0qlnlHs+k/3Iobc3LjS5AMYr5L8\r\n\tUO7OSkgFFlLHQyC9JzPfmLCAugvzEbyv4Olnsr8hbxF1MbKZoQxUZtMVu29wjfXk\r\n\thTeApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK\r\n\twTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu\r\n\tYhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3\r\n\tRA==\r\n\t-----END CERTIFICATE-----\r\n\r\n";
691 assert(test_error(dumbfuck2));
692
4794b36c » ry 2008-07-16 clean up tests 693 assert(test_request(&fragment_in_uri));
072669e5 » ry 2008-07-15 Move parser tests to its ow... 694
695 /* TODO sending junk and large headers gets rejected */
696
697
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 698 /* check to make sure our predefined requests are okay */
699
4794b36c » ry 2008-07-16 clean up tests 700 assert(test_request(&get_no_headers_no_body));
701 assert(test_request(&get_one_header_no_body));
702 assert(test_request(&get_no_headers_no_body));
072669e5 » ry 2008-07-15 Move parser tests to its ow... 703
704 // no content-length
4794b36c » ry 2008-07-16 clean up tests 705 const char *bad_get_no_headers_no_body = "GET /bad_get_no_headers_no_body/world HTTP/1.1\r\nAccept: */*\r\nHELLO\r\n";
706 assert(test_error(bad_get_no_headers_no_body)); // error if there is a body without content length
072669e5 » ry 2008-07-15 Move parser tests to its ow... 707
4794b36c » ry 2008-07-16 clean up tests 708 assert(test_request(&get_funky_content_length_body_hello));
f8b67637 » ry 2008-07-19 add a multipart/form bounda... 709 assert(test_request(&post_identity_body_world));
4794b36c » ry 2008-07-16 clean up tests 710 assert(test_request(&post_chunked_all_your_base));
711 assert(test_request(&two_chunks_mult_zero_end));
712 assert(test_request(&chunked_w_trailing_headers));
072669e5 » ry 2008-07-15 Move parser tests to its ow... 713
4794b36c » ry 2008-07-16 clean up tests 714 assert(test_request(&chunked_w_bullshit_after_length));
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 715 assert(1 == requests[0].request.version_major);
716 assert(1 == requests[0].request.version_minor);
072669e5 » ry 2008-07-15 Move parser tests to its ow... 717
718 // three requests - no bodies
3177fb5d » ry 2008-07-18 test_request_parser now pas... 719 assert( test_multiple3( &get_no_headers_no_body
720 , &get_one_header_no_body
721 , &get_no_headers_no_body
722 ));
072669e5 » ry 2008-07-15 Move parser tests to its ow... 723
724 // three requests - one body
4794b36c » ry 2008-07-16 clean up tests 725 assert( test_multiple3(&get_no_headers_no_body, &get_funky_content_length_body_hello, &get_no_headers_no_body));
072669e5 » ry 2008-07-15 Move parser tests to its ow... 726
727 // three requests with bodies -- last is chunked
f8b67637 » ry 2008-07-19 add a multipart/form bounda... 728 assert( test_multiple3(&get_funky_content_length_body_hello, &post_identity_body_world, &post_chunked_all_your_base));
072669e5 » ry 2008-07-15 Move parser tests to its ow... 729
730 // three chunked requests
4794b36c » ry 2008-07-16 clean up tests 731 assert( test_multiple3(&two_chunks_mult_zero_end, &post_chunked_all_your_base, &chunked_w_trailing_headers));
085f3c89 » ry 2008-07-15 Add several tests, fix some... 732
733
4794b36c » ry 2008-07-16 clean up tests 734 assert(test_scan2(&get_no_headers_no_body, &get_one_header_no_body, &get_no_headers_no_body));
f8b67637 » ry 2008-07-19 add a multipart/form bounda... 735 assert(test_scan2(&get_funky_content_length_body_hello, &post_identity_body_world, &post_chunked_all_your_base));
4794b36c » ry 2008-07-16 clean up tests 736 assert(test_scan2(&two_chunks_mult_zero_end, &chunked_w_trailing_headers, &chunked_w_bullshit_after_length));
085f3c89 » ry 2008-07-15 Add several tests, fix some... 737
4794b36c » ry 2008-07-16 clean up tests 738 assert(test_scan3(&get_no_headers_no_body, &get_one_header_no_body, &get_no_headers_no_body));
f8b67637 » ry 2008-07-19 add a multipart/form bounda... 739 assert(test_scan3(&get_funky_content_length_body_hello, &post_identity_body_world, &post_chunked_all_your_base));
4794b36c » ry 2008-07-16 clean up tests 740 assert(test_scan3(&two_chunks_mult_zero_end, &chunked_w_trailing_headers, &chunked_w_bullshit_after_length));
085f3c89 » ry 2008-07-15 Add several tests, fix some... 741
c1a5d8e5 » ry 2008-07-15 cleaning up tests. whew! 742
072669e5 » ry 2008-07-15 Move parser tests to its ow... 743 printf("okay\n");
744 return 0;
745 }
746