%% %% %CopyrightBegin% %% %% Copyright Ericsson AB 2004-2015. 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_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) -> 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(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_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 = ["\n
\n