%% %% %CopyrightBegin% %% %% Copyright Ericsson AB 2004-2010. 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_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() -> [{suite_callbacks,[ts_install_scb]}]. 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_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 = ["\n
\n