%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2004-2011. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%

-module(http_format_SUITE).
-author('ingela@erix.ericsson.se').

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

%% Test server specific exports
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, init_per_group/2,end_per_group/2, init_per_testcase/2, end_per_testcase/2]).

%% Test cases must be exported.
-export([ chunk_decode/1, chunk_encode/1,
	  chunk_extensions_otp_6005/1, chunk_decode_otp_6264/1,
	  chunk_decode_empty_chunk_otp_6511/1,
	  chunk_decode_trailer/1,
	  http_response/1, http_request/1, validate_request_line/1,
	  esi_parse_headers/1, cgi_parse_headers/1,
	  is_absolut_uri/1, convert_netscapecookie_date/1]).

suite() -> [{ct_hooks,[ts_install_cth]}].

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

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_decode_trailer]}].

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) ->
    Dog = test_server:timetrap(?t:minutes(1)),
    NewConfig = lists:keydelete(watchdog, 1, Config),
    [{watchdog, Dog} | NewConfig].

end_per_testcase(_, Config) ->
    Dog = ?config(watchdog, Config),
    test_server:timetrap_cancel(Dog),
    ok.

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


%%-------------------------------------------------------------------------
chunk_decode(doc) ->
    ["Test http_chunk:decode/3"];
chunk_decode(suite) ->
    [];
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),

    ok.

%%-------------------------------------------------------------------------
chunk_extensions_otp_6005(doc) ->
    ["Make sure so called extensions are ignored"];
chunk_extensions_otp_6005(suite) ->
    [];
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),
    ok.

%%-------------------------------------------------------------------------
chunk_decode_otp_6264(doc) ->
      ["Check that 0 in the body does not count as the last chunk"];
chunk_decode_otp_6264(suite) ->
    [];
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),
    
    ok.
%%-------------------------------------------------------------------------
chunk_decode_empty_chunk_otp_6511(doc) ->
    [""];
chunk_decode_empty_chunk_otp_6511(suite) ->
    [];
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),
    ok.
    
%%-------------------------------------------------------------------------
chunk_decode_trailer(doc) ->
    ["Make sure trailers are handled correctly. Trailers should"
     "become new headers"];
chunk_decode_trailer(suite) ->
    [];
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),
    
    ok.

%%-------------------------------------------------------------------------
chunk_encode(doc) ->
    ["Test http_chunk:encode/1 & http_chunk:encode_last/0"];
chunk_encode(suite) ->
    [];
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(),
    ok.


%%-------------------------------------------------------------------------
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(suite) ->
    [];
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(suite) ->
    [];
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, [?HTTP_MAX_HEADER_SIZE], 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, [?HTTP_MAX_HEADER_SIZE], 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, [?HTTP_MAX_HEADER_SIZE], 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)),    
    ok.
%%-------------------------------------------------------------------------
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(suite) ->
    [];
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"),

    ok.
%%-------------------------------------------------------------------------
esi_parse_headers(doc) ->
    ["Test httpd_esi:*. All header values are received in the same"
     " erlang message."];
esi_parse_headers(suite) ->
    [];
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"),
    ok.

%%--------------------------------------------------------------------
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(suite) ->
    [];
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),

    ok.
    
%%-------------------------------------------------------------------------
is_absolut_uri(doc) ->
    ["Test http_request:is_absolut_uri/1."];
is_absolut_uri(suite) ->
    [];
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(suite) ->
    [];
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"),
     ok.

%%--------------------------------------------------------------------
%%% 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.



