aboutsummaryrefslogtreecommitdiffstats
path: root/lib/inets/test/http_format_SUITE.erl
blob: 4e10a97f58eaf9fb1b3285bcc4b3da865a39636c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2004-2016. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%%

-module(http_format_SUITE).

-include_lib("common_test/include/ct.hrl").
-include("http_internal.hrl").

%% Note: This directive should only be used in test suites.
-compile(export_all).

all() -> 
    [{group, chunk}, http_response, http_request,
     validate_request_line, {group, script}, is_absolut_uri,
     convert_netscapecookie_date, check_content_length_encoding].

groups() -> 
    [{script, [], [esi_parse_headers, cgi_parse_headers]},
     {chunk, [],
      [chunk_decode, chunk_encode, chunk_extensions_otp_6005,
       chunk_decode_otp_6264,
       chunk_decode_empty_chunk_otp_6511,
       chunk_whitespace_suffix,
       chunk_decode_trailer, chunk_max_headersize, chunk_max_bodysize, chunk_not_hex]}].

init_per_suite(Config) ->
    Config.

end_per_suite(_Config) ->
    ok.

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.


init_per_testcase(_, Config) ->
    Config.

end_per_testcase(_, _) ->
    ok.

%%-------------------------------------------------------------------------
%% Test cases starts here.
%%-------------------------------------------------------------------------

chunk_decode() ->
    [{doc, "Test http_chunk:decode/3"}].
chunk_decode(Config) when is_list(Config) ->
    ReqHeaders =  #http_request_h{'transfer-encoding' = "chunked"},
    ChunkedBody = "A" ++ ?CRLF ++ "1234567890" ++ ?CRLF ++ "4" ++
	?CRLF ++ "HEJ!" ++ ?CRLF ++ "0" ++ ?CRLF ++ ?CRLF,    
    {ok, {Headers, Body}} = 
	http_chunk:decode(list_to_binary(ChunkedBody),
			  ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE),
    "1234567890HEJ!"  = binary_to_list(Body),
    %% When the "chunked" is removed by the decoding the header
    %% will become empty in this case i.e. undefined!
    NewReqHeaders = http_chunk:handle_headers(ReqHeaders, Headers),
    undefined = NewReqHeaders#http_request_h.'transfer-encoding',

    NewChunkedBody = ["A" ++ [?CR], [?LF] ++ "12345", "67890" ++ ?CRLF ++ "4"
		      ++ ?CRLF ++ "HEJ!" ++ ?CRLF ++ "0" ++ [?CR], 
		      [?LF, ?CR, ?LF]], 

    {Module, Function, Args} = 
	http_chunk:decode(list_to_binary(hd(NewChunkedBody)),
			  ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE),

    {_, Body} = parse(Module, Function, Args, tl(NewChunkedBody)),
    "1234567890HEJ!" = binary_to_list(Body).

%%-------------------------------------------------------------------------
chunk_extensions_otp_6005() ->
    [{doc, "Make sure so called extensions are ignored"}].
chunk_extensions_otp_6005(Config) when is_list(Config)->
    ChunkedBody = "A;ignore this" ++ ?CRLF ++ "1234567890" ++
	?CRLF ++ "4" ++ ?CRLF  ++ "HEJ!"++ ?CRLF  ++ "0" ++
	";extensionname=extensionvalue;foo=bar" ++ ?CRLF ++ ?CRLF,
     {ok, {["content-length:14"], Body}} = 
	http_chunk:decode(list_to_binary(ChunkedBody),
			  ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE),
	"1234567890HEJ!"  = binary_to_list(Body),

    ChunkedBody1 = ["A;", "ignore this" ++ [?CR], [?LF] ++ "1234567890" ++
		    ?CRLF ++ "4" ++ ?CRLF  ++ "HEJ!"++ ?CRLF  ++ "0" ++
		    ";extensionname=extensionvalue;foo=bar" ++ ?CRLF ++ ?CRLF],
    
    {Module1, Function1, Args1} = 
	http_chunk:decode(list_to_binary(hd(ChunkedBody1)),
			  ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE),
    
    {_, NewBody} = parse(Module1, Function1, Args1, tl(ChunkedBody1)),
    "1234567890HEJ!" = binary_to_list(NewBody).

%%-------------------------------------------------------------------------
chunk_decode_otp_6264() ->
    [{doc, "Check that 0 in the body does not count as the last chunk"}].
chunk_decode_otp_6264(Config) when is_list(Config)->
    ChunkedBody = "A;ignore this" ++ ?CRLF ++ "1234567890" ++
	?CRLF ++ "4" ++ ?CRLF  ++ "0123"++ ?CRLF  ++ "0" ++
	";extensionname=extensionvalue;foo=bar" ++ ?CRLF ++ ?CRLF,
     {ok, {["content-length:14"], Body}} = 
	http_chunk:decode(list_to_binary(ChunkedBody),
			  ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE),
	"12345678900123"  = binary_to_list(Body),

     NewChunkedBody = ["A" ++ [?CR], [?LF] ++ "12345", "67890" ++ ?CRLF ++ "1"
		      ++ ?CRLF ++ "0" ++ ?CRLF ++ "0" ++ [?CR], 
		      [?LF, ?CR, ?LF]],

    {Module, Function, Args} = 
	http_chunk:decode(list_to_binary(hd(NewChunkedBody)),
			  ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE),
    
    {_, NewBody} = parse(Module, Function, Args, tl(NewChunkedBody)),
    "12345678900" = binary_to_list(NewBody),
    
    NewChunkedBody1 = ["A" ++ [?CR], [?LF] ++ "12345", "67890" ++ ?CRLF ++ "1"
		      ++ ?CRLF ++ "0", ?CRLF ++ "0", [?CR], [?LF], 
		      [?CR], [?LF]],

    {Module1, Function1, Args1} = 
	http_chunk:decode(list_to_binary(hd(NewChunkedBody1)),
			  ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE),
    
    {_, NewBody} = parse(Module1, Function1, Args1, tl(NewChunkedBody1)),
    "12345678900" = binary_to_list(NewBody).
%%-------------------------------------------------------------------------
chunk_decode_empty_chunk_otp_6511(Config) when is_list(Config) ->
    ChunkedBody = "0" ++ ?CRLF ++ ?CRLF,
    {ok,{["content-length:0"],<<>>}}  = 
	http_chunk:decode(list_to_binary(ChunkedBody),
			  ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE).
    
%%-------------------------------------------------------------------------
chunk_whitespace_suffix() ->
    [{doc, "Test whitespace after chunked length header"}].
chunk_whitespace_suffix(Config) when is_list(Config) ->
    ChunkedBody = "1a  ; ignore-stuff-here" ++ ?CRLF ++
	"abcdefghijklmnopqrstuvwxyz" ++ ?CRLF ++ "10   "  ++ ?CRLF
	++ "1234567890abcdef" ++ ?CRLF  ++ "0  " ++ ?CRLF
	++ "some-footer:some-value"  ++ ?CRLF
	++ "another-footer:another-value" ++ ?CRLF ++ ?CRLF,
    {ok, {["content-length:42", "another-footer:another-value",
	   "some-footer:some-value", ""],
	  <<"abcdefghijklmnopqrstuvwxyz1234567890abcdef">>}} =
	http_chunk:decode(list_to_binary(ChunkedBody),
			  ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE).

%%-------------------------------------------------------------------------
chunk_decode_trailer() ->
    [{doc,"Make sure trailers are handled correctly. Trailers should"
     "become new headers"}].
chunk_decode_trailer(Config) when is_list(Config)->
    ChunkedBody = "1a; ignore-stuff-here" ++ ?CRLF ++ 
	"abcdefghijklmnopqrstuvwxyz" ++ ?CRLF ++ "10"  ++ ?CRLF 
	++ "1234567890abcdef" ++ ?CRLF  ++ "0" ++ ?CRLF 
	++ "some-footer:some-value"  ++ ?CRLF 
	++ "another-footer:another-value" ++ ?CRLF ++ ?CRLF,

    {ok, {Headers, Body}} = 
    http_chunk:decode(list_to_binary(ChunkedBody),
		      ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE),
    
    %% There is no guaranteed order of headers.
    true = lists:member("content-length:42", Headers),
    true = lists:member("some-footer:some-value", Headers),
    true = lists:member("another-footer:another-value", Headers),
    "abcdefghijklmnopqrstuvwxyz1234567890abcdef"  = binary_to_list(Body), 

    ChunkedBody1 = "1a" ++ ?CRLF ++ 
	"abcdefghijklmnopqrstuvwxyz" ++ ?CRLF ++ "10"  ++ ?CRLF 
	++ "1234567890abcdef" ++ ?CRLF  ++ "0" ++ ?CRLF
	++ "some-footer:some-value"  ++ ?CRLF  ++ ?CRLF,
    
    {ok, {Headers1, Body1}} = 
	http_chunk:decode(list_to_binary(ChunkedBody1),
			  ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE),
    
    true = lists:member("content-length:42", Headers1),
    true = lists:member("some-footer:some-value", Headers1),
    false = lists:member("another-footer:another-value", Headers1),
    "abcdefghijklmnopqrstuvwxyz1234567890abcdef"  = binary_to_list(Body1), 


    ChunkedBody2 = ["1a", ?CRLF ++ 
		    "abcdefghijklmnopqrstuvwxyz" ++ ?CRLF ++ "10"  ++ ?CRLF 
		    ++ "1234567890abcdef" ++ ?CRLF  ++ "0", ";", 
		    "ignore stuff here=foobar", ?CRLF ++
		    "some-footer:some-value", ?CRLF, ?CRLF],
      
     {Module, Function, Args} = 
 	http_chunk:decode(list_to_binary(hd(ChunkedBody2)),
 			  ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE),
    
     {_, NewBody} = parse(Module, Function, Args, tl(ChunkedBody2)),
     "abcdefghijklmnopqrstuvwxyz1234567890abcdef" = binary_to_list(NewBody),
   
    ChunkedBody3 = ["1a", ?CRLF ++ 
		    "abcdefghijklmnopqrstuvwxyz", ?CRLF ++ "10"  ++  ?CRLF
		    ++ "1234567890abcdef" ++  ?CRLF ++ "0" ++ ?CRLF 
		    ++ "some-footer:some-value", [?CR], [?LF] , ?CRLF],
    
     {Module1, Function1, Args1} = 
 	http_chunk:decode(list_to_binary(hd(ChunkedBody3)),
 			  ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE),
    
     {_, NewBody} = parse(Module1, Function1, Args1, tl(ChunkedBody3)),
     "abcdefghijklmnopqrstuvwxyz1234567890abcdef" = binary_to_list(NewBody).

%%-------------------------------------------------------------------------
chunk_encode() ->
    [{doc, "Test http_chunk:encode/1 & http_chunk:encode_last/0"}].
chunk_encode(Config) when is_list(Config) ->
    <<54, ?CR, ?LF, 102,111,111,98,97,114, ?CR, ?LF>> = 
	http_chunk:encode(list_to_binary("foobar")),
    ["6", ?CR, ?LF,"foobar", ?CR, ?LF] = http_chunk:encode("foobar"),
    <<$0, ?CR, ?LF, ?CR, ?LF >> = http_chunk:encode_last().
%%-------------------------------------------------------------------------
chunk_max_headersize() ->
    [{doc, "Test max header limit"}].
chunk_max_headersize(Config) when is_list(Config) ->
    ChunkedBody = "1a; ignore-stuff-here" ++ ?CRLF ++ 
	"abcdefghijklmnopqrstuvwxyz" ++ ?CRLF ++ "10"  ++ ?CRLF 
	++ "1234567890abcdef" ++ ?CRLF  ++ "0" ++ ?CRLF 
	++ "some-footer:some-value"  ++ ?CRLF 
	++ "another-footer:another-value" ++ ?CRLF ++ ?CRLF,
    
    {ok, {_, _}} = 
	http_chunk:decode(list_to_binary(ChunkedBody),
			  ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE),
    
    %% Too long in length header
    {error,{header_too_long, {max, 1}}} = 
	(catch http_chunk:decode(list_to_binary(ChunkedBody),
				 ?HTTP_MAX_BODY_SIZE, 1)),
    
    %% Too long in extension field
    {error,{header_too_long, {max, 10}}} = 
	(catch http_chunk:decode(list_to_binary(ChunkedBody),
				 ?HTTP_MAX_BODY_SIZE, 10)),
    
    %% Too long in trailer
    {error,{header_too_long, {max, 30}}} = 
	(catch http_chunk:decode(list_to_binary(ChunkedBody),
				 ?HTTP_MAX_BODY_SIZE, 30)).
%%-------------------------------------------------------------------------
chunk_not_hex() ->
    [{doc, "Test bad chunked length header"}].
chunk_not_hex(Config) when is_list(Config) ->
     ChunkedBody = "åäö; ignore-stuff-here" ++ ?CRLF ++ 
	"abcdefghijklmnopqrstuvwxyz" ++ ?CRLF ++ "10"  ++ ?CRLF 
	++ "1234567890abcdef" ++ ?CRLF  ++ "0" ++ ?CRLF 
	++ "some-footer:some-value"  ++ ?CRLF 
	++ "another-footer:another-value" ++ ?CRLF ++ ?CRLF,
     {error,{chunk_size, "åäö"}} = 
	(catch http_chunk:decode(list_to_binary(ChunkedBody),
				 ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE)).
%%-------------------------------------------------------------------------
chunk_max_bodysize() ->
    [{doc, "Test max body limit"}].
chunk_max_bodysize(Config) when is_list(Config) ->
     ChunkedBody = "1a; ignore-stuff-here" ++ ?CRLF ++ 
	"abcdefghijklmnopqrstuvwxyz" ++ ?CRLF ++ "10"  ++ ?CRLF 
	++ "1234567890abcdef" ++ ?CRLF  ++ "0" ++ ?CRLF 
	++ "some-footer:some-value"  ++ ?CRLF 
	++ "another-footer:another-value" ++ ?CRLF ++ ?CRLF,
    {ok, {_, _}} = 
	http_chunk:decode(list_to_binary(ChunkedBody),
			  ?HTTP_MAX_BODY_SIZE, ?HTTP_MAX_HEADER_SIZE),
    
    %% Too long body
    {error,{body_too_big, {max, 10}}} = 
	 (catch http_chunk:decode(list_to_binary(ChunkedBody),
				  10, ?HTTP_MAX_HEADER_SIZE)).

%%-------------------------------------------------------------------------
http_response() ->
    [{doc, "Test httpc_response:parse*. This test case will simulate that the "
     "message will be recived a little at the time on a socket and the "
     "package may be broken up into smaller parts at arbitrary point."}].
http_response(Config) when is_list(Config) ->

    HttpHead1 = ["HTTP", "/1.1 ", "20", "0 ", "ok", [?CR, ?LF], 
		 "content-length:83" ++ ?CRLF ++ "content", "-type:", 
		 "text/html" ++ ?CRLF ++  
		 "date:Thu, 28 Oct 2004 07:57:43 GMT" ++ 
		 [?CR], [?LF, ?CR, ?LF]], 
    {"HTTP/1.1",
     200,
     "ok",
     #http_response_h{'content-length' = "83",
		      'content-type' = "text/html",
		      date = "Thu, 28 Oct 2004 07:57:43 GMT"},
     <<>>} =
	parse(httpc_response, parse, [?HTTP_MAX_HEADER_SIZE, false],
	      HttpHead1),

    HttpHead2 = ["HTTP/1.1 200", " ok", [?CR], [?LF] ++ 
		 "content-length:83" ++ ?CRLF ++ "content-type:", 
		 "text/html" ++ ?CRLF ++  
		 "date:" ++ "Thu, 28 Oct 2004 07:57:43 GMT" ++ 
		 ?CRLF, ?CRLF], 
    {"HTTP/1.1",
     200,
     "ok",
     #http_response_h{'content-length' = "83",
		      'content-type' = "text/html",
		      date = "Thu, 28 Oct 2004 07:57:43 GMT"},
     <<>>} =
	parse(httpc_response, parse, [?HTTP_MAX_HEADER_SIZE, false], 
	      HttpHead2),

    HttpHead3 = ["HTTP/1.1 200 ", "ok", ?CRLF ++ 
		 "content-length:83" ++ ?CRLF ++ "content-type:", 
		 "text/html" ++ ?CRLF ++  
		 "date:" ++ "Thu, 28 Oct 2004 07:57:43 GMT" ++ 
		 [?CR, ?LF,?CR], [?LF]], 
    {"HTTP/1.1",
     200,
     "ok",
     #http_response_h{'content-length' = "83",
		      'content-type' = "text/html",
		      date = "Thu, 28 Oct 2004 07:57:43 GMT"},
     <<>>} =
	parse(httpc_response, parse, [?HTTP_MAX_HEADER_SIZE, false], 
	      HttpHead3),
    
    HttpBody = ["<HTML>\n<HEAD>\n<TITLE> dummy </TITLE>\n</HEAD>\n<BODY>\n",
		"<H1>dummy</H1>\n</BODY>\n</HTML>\n"],
    
    NewBody = lists:flatten(HttpBody), 
    Length = length(NewBody),
    NewBody = 
	binary_to_list(parse
		       (httpc_response, whole_body, [<<>>,Length], 
			HttpBody)),

    HttpBody1 = ["<HTML", ">\n<HEAD>", "\n<TITLE> dummy </TITLE>\n</HEAD>\n",
		 "<BODY>\n", "<H1>du", "mmy</H1>\n</BODY>\n</HTML>\n"],

    NewBody1 = lists:flatten(HttpBody1),
    Length1 = length(NewBody1),
    NewBody1 = binary_to_list(parse
			      (httpc_response, whole_body,
			       [<<>>,Length1], HttpBody1)),
    ok.
%%-------------------------------------------------------------------------
http_request() ->
    [{doc, "Test httpd_request:parse* This test case will simulate that the " 
     "message will be recived a little at the time on a socket and the " 
      "package may be broken up into smaller parts at arbitrary point."}].
http_request(Config) when is_list(Config) ->

    HttpHead = ["GE", "T ", "http://www.erlang", ".org ", "HTTP", 
		"/1.1" ++ ?CRLF ++ "host:", 
		"www.erlang.org" ++ [?CR], 
		[?LF] ++ "te: " ++ ?CRLF, ?CRLF], 
    {"GET",
     "http://www.erlang.org",
     "HTTP/1.1",
     {#http_request_h{host = "www.erlang.org", te = []},
      [{"te", []}, {"host", "www.erlang.org"}]}, <<>>} =
	parse(httpd_request, parse, [[{max_header, ?HTTP_MAX_HEADER_SIZE},
				      {max_version, ?HTTP_MAX_VERSION_STRING}, 
				      {max_method, ?HTTP_MAX_METHOD_STRING},
				      {max_content_length, ?HTTP_MAX_CONTENT_LENGTH}
				     ]],
	      HttpHead),

    HttpHead1 = ["GET http://www.erlang.org HTTP/1.1" ++ 
		 [?CR], [?LF, ?CR, ?LF]],
    {"GET",
     "http://www.erlang.org",
     "HTTP/1.1",
     {#http_request_h{}, []}, <<>>} =
	parse(httpd_request, parse,  [[{max_header, ?HTTP_MAX_HEADER_SIZE},
				       {max_version, ?HTTP_MAX_VERSION_STRING}, 
				       {max_method, ?HTTP_MAX_METHOD_STRING},
				       {max_content_length, ?HTTP_MAX_CONTENT_LENGTH}
				      ]], HttpHead1),


    HttpHead2 = ["GET http://www.erlang.org HTTP/1.1" ++ 
		 [?CR, ?LF, ?CR], [?LF]],
    {"GET",
     "http://www.erlang.org",
     "HTTP/1.1",
     {#http_request_h{}, []}, <<>>} =
	parse(httpd_request, parse, [[{max_header, ?HTTP_MAX_HEADER_SIZE},
				      {max_version, ?HTTP_MAX_VERSION_STRING}, 
				      {max_method, ?HTTP_MAX_METHOD_STRING},
				      {max_content_length, ?HTTP_MAX_CONTENT_LENGTH}
				     ]], HttpHead2),

    %% Note the following body is not related to the headers above
    HttpBody = ["<HTML>\n<HEAD>\n<TITLE> dummy </TITLE>\n</HEAD>\n<BODY>\n",
		"<H1>dummy</H1>\n</BODY>\n</HTML>\n"],

    NewBody = lists:flatten(HttpBody),
    Length = length(NewBody),
    NewBody = 
	binary_to_list(parse
		       (httpd_request, whole_body, [<<>>,Length], HttpBody)),

    HttpBody1 = ["<HTML", ">\n<HEAD>", "\n<TITLE> dummy </TITLE>\n</HEAD>\n",
		 "<BODY>\n", "<H1>du", "mmy</H1>\n</BODY>\n</HTML>\n"],

    NewBody1 = lists:flatten(HttpBody1), 
    Length1 = length(NewBody1),
    NewBody1 = 	
	binary_to_list(parse
		       (httpd_request, whole_body, 
			[<<>>, Length1], HttpBody1)).
%%-------------------------------------------------------------------------
validate_request_line() ->
    [{doc, "Test httpd_request:validate/3. Makes sure you can not get past"
     " the server_root and that the request is recognized by the server"
     " and protcol version."}].
validate_request_line(Config) when is_list(Config) ->

    %% HTTP/0.9 only has GET requests
    ok = 
	httpd_request:validate("GET", "http://www.erlang/org", "HTTP/0.9"),
    {error, {not_supported, 
	     {"HEAD", "http://www.erlang/org", "HTTP/0.9"}}} =
	httpd_request:validate("HEAD", "http://www.erlang/org", "HTTP/0.9"),
    {error, {not_supported, 
	     {"TRACE", "http://www.erlang/org", "HTTP/0.9"}}} =
	httpd_request:validate("TRACE", "http://www.erlang/org", "HTTP/0.9"),
    {error, {not_supported, 
	     {"POST", "http://www.erlang/org", "HTTP/0.9"}}} =
	httpd_request:validate("POST", "http://www.erlang/org", "HTTP/0.9"),

    %% HTTP/1.* 
    ok = httpd_request:validate("HEAD", "http://www.erlang/org", 
			       "HTTP/1.1"),
    ok = httpd_request:validate("GET", "http://www.erlang/org", 
			       "HTTP/1.1"),  
    ok = httpd_request:validate("POST","http://www.erlang/org", 
			       "HTTP/1.1"),
    ok = httpd_request:validate("TRACE","http://www.erlang/org",
			       "HTTP/1.1"),
    {error, {not_supported, 
	     {"FOOBAR", "http://www.erlang/org", "HTTP/1.1"}}} =
	httpd_request:validate("FOOBAR", "http://www.erlang/org", 
			       "HTTP/1.1"),

    %% Attempts to get outside of server_root directory by relative links 
    ForbiddenUri = "http://127.0.0.1:8888/../../../../../etc/passwd",
    {error, {bad_request, {forbidden, ForbiddenUri}}} = 
	httpd_request:validate("GET", ForbiddenUri, "HTTP/1.1"),

    ForbiddenUri2 = 
	"http://127.0.0.1:8888/././././././../../../../../etc/passwd",
    {error, {bad_request, {forbidden, ForbiddenUri2}}} = 
	httpd_request:validate("GET", ForbiddenUri2, "HTTP/1.1"),

    HexForbiddenUri = "http://127.0.0.1:8888/%2e%2e/%2e%2e/%2e%2e/" 
	"home/ingela/test.html",
    {error, {bad_request, {forbidden, HexForbiddenUri}}} = 
	httpd_request:validate("GET", HexForbiddenUri, "HTTP/1.1"),

    NewForbiddenUri = 
	"http://127.0.0.1:8888/foobar/../../../home/ingela/test.html",
    {error, {bad_request, {forbidden, NewForbiddenUri}}} = 
	httpd_request:validate("GET", NewForbiddenUri, "HTTP/1.1"),

    NewForbiddenUri1 = 
	"http://127.0.0.1:8888/../home/ingela/test.html",
    {error, {bad_request, {forbidden, NewForbiddenUri1}}} = 
	httpd_request:validate("GET", NewForbiddenUri1, "HTTP/1.1").

%%-------------------------------------------------------------------------
check_content_length_encoding() ->
    [{doc, "Test http_request:headers/2. Check that the content-length is"
      " encoded even when it is zero."}].
check_content_length_encoding(Config) when is_list(Config) ->

    %% Check that the content-length is preserved.
    %% Sanity check.
    Header1 = http_request:http_headers(#http_request_h{'content-length'="123"}),
    true = (string:str(Header1, "content-length: 123\r\n") > 0),
    %% Check that content-length=0 is handled correctly.
    Header2 = http_request:http_headers(#http_request_h{'content-length'="0"}),
    true = (string:str(Header2, "content-length: 0\r\n") > 0).

%%-------------------------------------------------------------------------
esi_parse_headers() ->
    [{doc, "Test httpd_esi:*. All header values are received in the same"
      " erlang message."}].
esi_parse_headers(Config) when is_list(Config) ->

    ESIResult = "content-type:text/html\r\ndate:Thu, 28 Oct 2004 07:57:43 "
 	"GMT\r\nstatus:200 OK\r\n\r\nFoobar",
    
    {"content-type:text/html\r\ndate:Thu, 28 Oct 2004 07:57:43 GMT\r\nst"
     "atus:200 OK\r\n" = Headers,
     "Foobar"} = httpd_esi:parse_headers(ESIResult),
    
    {ok,[{"date","Thu, 28 Oct 2004 07:57:43 GMT"},
	 {"content-type","text/html"}], 200} = 
	httpd_esi:handle_headers(Headers),
    
    ESIResult2 = 
	"location:http://foo.bar.se\r\ndate:Thu, 28 Oct 2004 07:57:43 "
	"GMT\r\n\r\n",
    
    {"location:http://foo.bar.se\r\ndate:Thu, 28 Oct 2004 07:57:43 GMT\r\n" = 
     Headers2,[]}
	= httpd_esi:parse_headers(ESIResult2),
    
    {ok,[{"date","Thu, 28 Oct 2004 07:57:43 GMT"},
	 {"location","http://foo.bar.se"}], 302} =
	httpd_esi:handle_headers(Headers2),
    
    {proceed,"/foo/bar.html"} = 
	httpd_esi:handle_headers("location:/foo/bar.html\r\n").

%%--------------------------------------------------------------------
cgi_parse_headers() ->
    [{doc, "Test httpd_cgi:*. This test case will simulate that the "
     "message will be recived a little at the time on a socket and the "
     "package may be broken up into smaller parts at arbitrary point."}].

cgi_parse_headers(Config) when is_list(Config) ->

    CGIResult = ["content-type:text", "/html\ndate:Thu, 28 Oct 2004 07:57:43 "
		 "GMT\nst", "atus:200 OK\n", "\nFoobar"],

    {Headers, Body} = 
	parse(httpd_cgi, parse_headers, [<<>>, [], []], CGIResult),
    
    "Foobar" = binary_to_list(Body),

    {ok,[{"content-type","text/html"},
	 {"date","Thu, 28 Oct 2004 07:57:43 GMT"}], {200,"OK"}}  = 
	httpd_cgi:handle_headers(Headers),

    CGIResult2 = ["location:http://foo.bar.se\ndate:Thu, 28 Oct 2004"
		  " 07:57:43 GMT\n\n"],
    {Headers2,  _} = parse(httpd_cgi, parse_headers, 
			   [<<>>, [], []], CGIResult2),

    {ok,[{"location","http://foo.bar.se"},
	 {"date","Thu, 28 Oct 2004 07:57:43 GMT"}], {302,"Redirect"}} =
	httpd_cgi:handle_headers(Headers2),
    
    {proceed,"/foo/bar.html"} = 
	httpd_cgi:handle_headers(["location:/foo/bar.html\n"]),

    CGIHTTPResult = ["Content-Type:text", "/html\n", "Connection:close\r\n",
		     "Content-Language:en\r\nAge:", "4711\r\n\r\n\nfoobar"],

    {Headers3,  _} = parse(httpd_cgi, parse_headers, 
			   [<<>>, [], []], CGIHTTPResult),

    {ok,[{"content-type","text/html"},
	 {"connection","close"},
	 {"content-language","en"},
	 {"age","4711"}], {200,"ok"}}  = httpd_cgi:handle_headers(Headers3).    
%%-------------------------------------------------------------------------
is_absolut_uri() ->
    [{doc, "Test http_request:is_absolut_uri/1."}].
is_absolut_uri(Config) when is_list(Config) ->
    true = http_request:is_absolut_uri("http://www.erlang.org"),
    true = http_request:is_absolut_uri("https://www.erlang.org"),
    false = http_request:is_absolut_uri("index.html").

%%-------------------------------------------------------------------------
convert_netscapecookie_date() ->
    [{doc, "Test http_util:convert_netscapecookie_date/1."}].
convert_netscapecookie_date(Config) when is_list(Config) ->
    {{2006,1,6},{8,59,38}} = 
	http_util:convert_netscapecookie_date("Mon, 06-Jan-2006 08:59:38 GMT"),
    {{2006,2, 7},{8,59,38}} = 
	http_util:convert_netscapecookie_date("Tue, 07-Feb-2006 08:59:38 GMT"),
    {{2006,3,8},{8,59,38}} = 
	http_util:convert_netscapecookie_date("Wdy, 08-Mar-2006 08:59:38 GMT"),
    {{2006,4,9},{8,59,38}} = 
	http_util:convert_netscapecookie_date("Thu, 09-Apr-2006 08:59:38 GMT"),
    {{2006,5,10},{8,59,38}} = 
	http_util:convert_netscapecookie_date("Fri, 10-May-2006 08:59:38 GMT"),
    {{2006,6,11},{8,59,38}} = 
	http_util:convert_netscapecookie_date("Sat, 11-Jun-2006 08:59:38 GMT"),
    {{2006,7,12},{8,59,38}} = 
	http_util:convert_netscapecookie_date("Sun, 12-Jul-2006 08:59:38 GMT"),
    {{2006,8,12},{8,59,38}} = 
	http_util:convert_netscapecookie_date("Sun, 12-Aug-2006 08:59:38 GMT"),
    {{2006,9,12},{8,59,38}} = 
	http_util:convert_netscapecookie_date("Sun, 12-Sep-2006 08:59:38 GMT"),
    {{2006,10,12},{8,59,38}} = 
	http_util:convert_netscapecookie_date("Sun, 12-Oct-2006 08:59:38 GMT"),
    {{2006,11,12},{8,59,38}} = 
	http_util:convert_netscapecookie_date("Sun, 12-Nov-2006 08:59:38 GMT"),
    {{2006,12,12},{8,59,38}} = 
	http_util:convert_netscapecookie_date("Sun, 12-Dec-2006 08:59:38 GMT"),
    {{2006,12,12},{8,59,38}} = 
	http_util:convert_netscapecookie_date("Sun 12-Dec-2006 08:59:38 GMT"),
    {{2006,12,12},{8,59,38}} = 
	http_util:convert_netscapecookie_date("Sun, 12-Dec-06 08:59:38 GMT"),
    {{2006,12,12},{8,59,38}} = 
	http_util:convert_netscapecookie_date("Sun 12-Dec-06 08:59:38 GMT"),
    {{2036,1,1},{8,0,1}} = 
	http_util:convert_netscapecookie_date("Tue Jan 01 08:00:01 2036 GMT").
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------

parse(Module, Function, Args, [Data | Rest]) ->
    case  Module:Function([list_to_binary(Data) | Args]) of
	{ok, Result} ->
	    Result;
	{NewModule, NewFunction, NewArgs} ->
	    parse(NewModule, NewFunction, NewArgs, Rest)
    end.