diff options
-rw-r--r-- | .gitignore | 6 | ||||
-rw-r--r-- | Makefile | 42 | ||||
-rw-r--r-- | ROADMAP.md | 40 | ||||
-rw-r--r-- | include/http.hrl | 2 | ||||
-rw-r--r-- | rebar.config | 6 | ||||
-rw-r--r-- | rebar.tests.config | 7 | ||||
-rw-r--r-- | src/cowboy.erl | 1 | ||||
-rw-r--r-- | src/cowboy_client.erl | 270 | ||||
-rw-r--r-- | src/cowboy_dispatcher.erl | 2 | ||||
-rw-r--r-- | src/cowboy_http.erl | 8 | ||||
-rw-r--r-- | src/cowboy_http_protocol.erl | 50 | ||||
-rw-r--r-- | src/cowboy_http_req.erl | 152 | ||||
-rw-r--r-- | src/cowboy_http_websocket.erl | 31 | ||||
-rw-r--r-- | src/cowboy_ssl_transport.erl | 7 | ||||
-rw-r--r-- | src/cowboy_tcp_transport.erl | 6 | ||||
-rw-r--r-- | test/http_SUITE.erl | 1262 | ||||
-rw-r--r-- | test/http_handler_long_polling.erl | 2 | ||||
-rw-r--r-- | test/http_handler_loop_timeout.erl | 15 |
18 files changed, 1141 insertions, 768 deletions
@@ -1,4 +1,4 @@ -.cowboy_dialyzer.plt +.cowboy.plt .eunit deps doc/*.css @@ -8,7 +8,3 @@ doc/edoc-info ebin logs test/*.beam -*.un~ -*~ -\#*\# -*.swp @@ -1,41 +1,59 @@ # See LICENSE for licensing information. +PROJECT = cowboy + DIALYZER = dialyzer REBAR = rebar all: app -app: deps - @$(REBAR) compile +# Application. deps: @$(REBAR) get-deps +app: deps + @$(REBAR) compile + clean: @$(REBAR) clean rm -f test/*.beam rm -f erl_crash.dump -tests: clean app eunit ct +docs: clean-docs + @$(REBAR) doc skip_deps=true + +clean-docs: + rm -f doc/*.css + rm -f doc/*.html + rm -f doc/*.png + rm -f doc/edoc-info + +# Tests. + +deps/proper: + @$(REBAR) -C rebar.tests.config get-deps + cd deps/proper && $(REBAR) compile + +tests: clean deps/proper app eunit ct -inttests: clean app eunit intct +inttests: clean deps/proper app eunit intct eunit: - @$(REBAR) eunit skip_deps=true + @$(REBAR) -C rebar.tests.config eunit skip_deps=true ct: - @$(REBAR) ct skip_deps=true suites=http,proper,ws + @$(REBAR) -C rebar.tests.config ct skip_deps=true suites=http,proper,ws intct: - @$(REBAR) ct skip_deps=true suites=http,proper,ws,autobahn + @$(REBAR) -C rebar.tests.config ct skip_deps=true suites=http,proper,ws,autobahn + +# Dialyzer. build-plt: - @$(DIALYZER) --build_plt --output_plt .cowboy_dialyzer.plt \ + @$(DIALYZER) --build_plt --output_plt .$(PROJECT).plt \ --apps kernel stdlib sasl inets crypto public_key ssl dialyze: - @$(DIALYZER) --src src --plt .cowboy_dialyzer.plt --no_native \ + @$(DIALYZER) --src src --plt .$(PROJECT).plt --no_native \ -Werror_handling -Wrace_conditions -Wunmatched_returns # -Wunderspecs - -docs: - @$(REBAR) doc skip_deps=true @@ -61,56 +61,20 @@ are not ordered. Tools like curl expect a 100 Continue before sending a request body by default. -* Content-Encoding support. - - Cowboy should be able to send encoded content automatically. - The default should be to encode, but the developer must be - able to override this default either for the whole listener - or just for a single reply. - -* Improve body reading API. - - We want to have various different things for reading the - body. First, there should be raw functions for the different - ways to read the body: basic, transfer encoded, multipart. - Each should allow us to limit the size of what is read. - - On top of these functions there should be two more - advanced functions: one would return the result of parsing - a x-www-form-urlencoded body; the other would parse a - multipart request, save files from the multipart data to - a temporary location and return a proplist of values if any - along with the files details. This behavior is similar to - what is done automatically by PHP with its $_FILES array. - - The advanced functions are of course here for convenience - only and it should be trivial to reimplement them directly - in a Cowboy application if needed. +* Convert the multipart code to stream_body. * Complete the work on Websockets. Now that the Autobahn test suite is available (make inttests), we have a definite way to know whether Cowboy's implementation of Websockets is right. The work can thus be completed. The - remaining tasks are proper UTF8 handling and fragmentation. + remaining tasks are proper UTF8 handling. * SPDY support. While SPDY probably won't be added directly to Cowboy, work has been started on making Cowboy use SPDY. -* Hooks. - - Customizable hooks would allow the developer to extend Cowboy - easily. Two kinds of hooks are needed: before dispatching the - request, and before sending a reply. - - The first would allow us to apply site-wide functions like - authentication or request logging and modify the Req if needed. - - The second is more interesting for response logging or to - filter the replies, for example to send custom error pages. - * Transport upgrades. Some protocols allow an upgrade from TCP to SSL without diff --git a/include/http.hrl b/include/http.hrl index 21d837b..dc849c2 100644 --- a/include/http.hrl +++ b/include/http.hrl @@ -52,5 +52,7 @@ fun(() -> {sent, non_neg_integer()})}, %% Functions. + onresponse = undefined :: undefined | fun((cowboy_http:status(), + cowboy_http:headers(), #http_req{}) -> #http_req{}), urldecode :: {fun((binary(), T) -> binary()), T} }). diff --git a/rebar.config b/rebar.config index 82d1fca..ef0f6ed 100644 --- a/rebar.config +++ b/rebar.config @@ -1,9 +1,3 @@ -{cover_enabled, true}. -{deps, [ - {proper, "1.0", - {git, "git://github.com/manopapad/proper.git", {tag, "v1.0"}}} -]}. -{eunit_opts, [verbose, {report, {eunit_surefire, [{dir, "."}]}}]}. {erl_opts, [ %% bin_opt_info, %% warn_missing_spec, diff --git a/rebar.tests.config b/rebar.tests.config new file mode 100644 index 0000000..14daa1d --- /dev/null +++ b/rebar.tests.config @@ -0,0 +1,7 @@ +{cover_enabled, true}. +{deps, [ + {proper, "1.0", + {git, "git://github.com/manopapad/proper.git", {tag, "v1.0"}}} +]}. +{eunit_opts, [verbose, {report, {eunit_surefire, [{dir, "."}]}}]}. +{erl_opts, []}. diff --git a/src/cowboy.erl b/src/cowboy.erl index 7963df2..1097197 100644 --- a/src/cowboy.erl +++ b/src/cowboy.erl @@ -51,7 +51,6 @@ start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) Transport, TransOpts, Protocol, ProtoOpts)). %% @doc Stop a listener identified by <em>Ref</em>. -%% @todo Currently request processes aren't terminated with the listener. -spec stop_listener(any()) -> ok | {error, not_found}. stop_listener(Ref) -> case supervisor:terminate_child(cowboy_sup, {cowboy_listener_sup, Ref}) of diff --git a/src/cowboy_client.erl b/src/cowboy_client.erl new file mode 100644 index 0000000..e46619f --- /dev/null +++ b/src/cowboy_client.erl @@ -0,0 +1,270 @@ +%% Copyright (c) 2012, Loïc Hoguin <[email protected]> +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% @private +-module(cowboy_client). + +-export([init/1]). +-export([state/1]). +-export([transport/1]). + +-export([connect/4]). +-export([raw_request/2]). +-export([request/3]). +-export([request/4]). +-export([request/5]). +-export([response/1]). +-export([response_body/1]). +-export([skip_body/1]). +-export([stream_status/1]). +-export([stream_headers/1]). +-export([stream_header/1]). +-export([stream_body/1]). + +-record(client, { + state = wait :: wait | request | response | response_body, + opts = [] :: [any()], + socket = undefined :: undefined | inet:socket(), + transport = undefined :: module(), + timeout = 5000 :: timeout(), %% @todo Configurable. + buffer = <<>> :: binary(), + connection = keepalive :: keepalive | close, + version = {1, 1} :: cowboy_http:version(), + response_body = undefined :: undefined | non_neg_integer() +}). + +init(Opts) -> + {ok, #client{opts=Opts}}. + +state(#client{state=State}) -> + State. + +transport(#client{socket=undefined}) -> + {error, notconnected}; +transport(#client{transport=Transport, socket=Socket}) -> + {ok, Transport, Socket}. + +connect(Transport, Host, Port, Client) + when is_binary(Host) -> + connect(Transport, binary_to_list(Host), Port, Client); +connect(Transport, Host, Port, Client=#client{state=State, opts=Opts}) + when is_atom(Transport), is_list(Host), + is_integer(Port), is_record(Client, client), + State =:= wait -> + {ok, Socket} = Transport:connect(Host, Port, Opts), + {ok, Client#client{state=request, socket=Socket, transport=Transport}}. + +raw_request(Data, Client=#client{state=response_body}) -> + {done, Client2} = skip_body(Client), + raw_request(Data, Client2); +raw_request(Data, Client=#client{ + state=State, socket=Socket, transport=Transport}) + when State =:= request -> + ok = Transport:send(Socket, Data), + {ok, Client}. + +request(Method, URL, Client) -> + request(Method, URL, [], <<>>, Client). + +request(Method, URL, Headers, Client) -> + request(Method, URL, Headers, <<>>, Client). + +request(Method, URL, Headers, Body, Client=#client{state=response_body}) -> + {done, Client2} = skip_body(Client), + request(Method, URL, Headers, Body, Client2); +request(Method, URL, Headers, Body, Client=#client{ + state=State, version=Version}) + when State =:= wait; State =:= request -> + {Transport, FullHost, Host, Port, Path} = parse_url(URL), + {ok, Client2} = case State of + wait -> connect(Transport, Host, Port, Client); + request -> {ok, Client} + end, + VersionBin = cowboy_http:version_to_binary(Version), + %% @todo do keepalive too, allow override... + Headers2 = [ + {<<"host">>, FullHost}, + {<<"user-agent">>, <<"Cow">>} + |Headers], + Headers3 = case iolist_size(Body) of + 0 -> Headers2; + Length -> [{<<"content-length">>, integer_to_list(Length)}|Headers2] + end, + HeadersData = [[Name, <<": ">>, Value, <<"\r\n">>] + || {Name, Value} <- Headers3], + Data = [Method, <<" ">>, Path, <<" ">>, VersionBin, <<"\r\n">>, + HeadersData, <<"\r\n">>, Body], + raw_request(Data, Client2). + +parse_url(<< "https://", Rest/binary >>) -> + parse_url(Rest, cowboy_ssl_transport); +parse_url(<< "http://", Rest/binary >>) -> + parse_url(Rest, cowboy_tcp_transport); +parse_url(URL) -> + parse_url(URL, cowboy_tcp_transport). + +parse_url(URL, Transport) -> + case binary:split(URL, <<"/">>) of + [Peer] -> + {Host, Port} = parse_peer(Peer, Transport), + {Transport, Peer, Host, Port, <<"/">>}; + [Peer, Path] -> + {Host, Port} = parse_peer(Peer, Transport), + {Transport, Peer, Host, Port, [<<"/">>, Path]} + end. + +parse_peer(Peer, Transport) -> + case binary:split(Peer, <<":">>) of + [Host] when Transport =:= cowboy_tcp_transport -> + {binary_to_list(Host), 80}; + [Host] when Transport =:= cowboy_ssl_transport -> + {binary_to_list(Host), 443}; + [Host, Port] -> + {binary_to_list(Host), list_to_integer(binary_to_list(Port))} + end. + +response(Client=#client{state=response_body}) -> + {done, Client2} = skip_body(Client), + response(Client2); +response(Client=#client{state=request}) -> + case stream_status(Client) of + {ok, Status, _, Client2} -> + case stream_headers(Client2) of + {ok, Headers, Client3} -> + {ok, Status, Headers, Client3}; + {error, Reason} -> + {error, Reason} + end; + {error, Reason} -> + {error, Reason} + end. + +response_body(Client=#client{state=response_body}) -> + response_body_loop(Client, <<>>). + +response_body_loop(Client, Acc) -> + case stream_body(Client) of + {ok, Data, Client2} -> + response_body_loop(Client2, << Acc/binary, Data/binary >>); + {done, Client2} -> + {ok, Acc, Client2}; + {error, Reason} -> + {error, Reason} + end. + +skip_body(Client=#client{state=response_body}) -> + case stream_body(Client) of + {ok, _, Client2} -> skip_body(Client2); + Done -> Done + end. + +stream_status(Client=#client{state=State, buffer=Buffer}) + when State =:= request -> + case binary:split(Buffer, <<"\r\n">>) of + [Line, Rest] -> + parse_status(Client#client{state=response, buffer=Rest}, Line); + _ -> + case recv(Client) of + {ok, Data} -> + Buffer2 = << Buffer/binary, Data/binary >>, + stream_status(Client#client{buffer=Buffer2}); + {error, Reason} -> + {error, Reason} + end + end. + +parse_status(Client, << "HTTP/", High, ".", Low, " ", + S3, S2, S1, " ", StatusStr/binary >>) + when High >= $0, High =< $9, Low >= $0, Low =< $9, + S3 >= $0, S3 =< $9, S2 >= $0, S2 =< $9, S1 >= $0, S1 =< $9 -> + Version = {High - $0, Low - $0}, + Status = (S3 - $0) * 100 + (S2 - $0) * 10 + S1 - $0, + {ok, Status, StatusStr, Client#client{version=Version}}. + +stream_headers(Client=#client{state=State}) + when State =:= response -> + stream_headers(Client, []). + +stream_headers(Client, Acc) -> + case stream_header(Client) of + {ok, Name, Value, Client2} -> + stream_headers(Client2, [{Name, Value}|Acc]); + {done, Client2} -> + {ok, Acc, Client2}; + {error, Reason} -> + {error, Reason} + end. + +stream_header(Client=#client{state=State, buffer=Buffer, + response_body=RespBody}) when State =:= response -> + case binary:split(Buffer, <<"\r\n">>) of + [<<>>, Rest] -> + %% If we have a body, set response_body. + Client2 = case RespBody of + undefined -> Client#client{state=request}; + 0 -> Client#client{state=request}; + _ -> Client#client{state=response_body} + end, + {done, Client2#client{buffer=Rest}}; + [Line, Rest] -> + %% @todo Do a better parsing later on. + [Name, Value] = binary:split(Line, <<": ">>), + Name2 = cowboy_bstr:to_lower(Name), + Client2 = case Name2 of + <<"content-length">> -> + Length = list_to_integer(binary_to_list(Value)), + if Length >= 0 -> ok end, + Client#client{response_body=Length}; + _ -> + Client + end, + {ok, Name2, Value, Client2#client{buffer=Rest}}; + _ -> + case recv(Client) of + {ok, Data} -> + Buffer2 = << Buffer/binary, Data/binary >>, + stream_header(Client#client{buffer=Buffer2}); + {error, Reason} -> + {error, Reason} + end + end. + +stream_body(Client=#client{state=response_body, response_body=RespBody}) + when RespBody =:= undefined; RespBody =:= 0 -> + {done, Client#client{state=request, response_body=undefined}}; +stream_body(Client=#client{state=response_body, buffer=Buffer, + response_body=Length}) when is_integer(Length) -> + case byte_size(Buffer) of + 0 -> + case recv(Client) of + {ok, Body} when byte_size(Body) =< Length -> + Length2 = Length - byte_size(Body), + {ok, Body, Client#client{response_body=Length2}}; + {ok, Data} -> + << Body:Length/binary, Rest/binary >> = Data, + {ok, Body, Client#client{buffer=Rest, + response_body=undefined}}; + {error, Reason} -> + {error, Reason} + end; + N when N =< Length -> + Length2 = Length - N, + {ok, Buffer, Client#client{buffer= <<>>, response_body=Length2}}; + _ -> + << Body:Length/binary, Rest/binary >> = Buffer, + {ok, Body, Client#client{buffer=Rest, response_body=undefined}} + end. + +recv(#client{socket=Socket, transport=Transport, timeout=Timeout}) -> + Transport:recv(Socket, 0, Timeout). diff --git a/src/cowboy_dispatcher.erl b/src/cowboy_dispatcher.erl index db40e63..6de8b49 100644 --- a/src/cowboy_dispatcher.erl +++ b/src/cowboy_dispatcher.erl @@ -215,6 +215,8 @@ split_path_test_() -> {<<"?">>, [], <<"">>, <<"">>}, {<<"???">>, [], <<"">>, <<"??">>}, {<<"/">>, [], <<"/">>, <<"">>}, + {<<"/extend//cowboy">>, [<<"extend">>, <<>>, <<"cowboy">>], + <<"/extend//cowboy">>, <<>>}, {<<"/users">>, [<<"users">>], <<"/users">>, <<"">>}, {<<"/users?">>, [<<"users">>], <<"/users">>, <<"">>}, {<<"/users?a">>, [<<"users">>], <<"/users">>, <<"a">>}, diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl index 7bea8e0..f8d3314 100644 --- a/src/cowboy_http.erl +++ b/src/cowboy_http.erl @@ -26,7 +26,8 @@ -export([te_chunked/2, te_identity/2, ce_identity/1]). %% Interpretation. --export([connection_to_atom/1, urldecode/1, urldecode/2, urlencode/1, +-export([connection_to_atom/1, version_to_binary/1, + urldecode/1, urldecode/2, urlencode/1, urlencode/2, x_www_form_urlencoded/2]). -type method() :: 'OPTIONS' | 'GET' | 'HEAD' @@ -781,6 +782,11 @@ connection_to_atom([<<"close">>|_Tail]) -> connection_to_atom([_Any|Tail]) -> connection_to_atom(Tail). +%% @doc Convert an HTTP version tuple to its binary form. +-spec version_to_binary(version()) -> binary(). +version_to_binary({1, 1}) -> <<"HTTP/1.1">>; +version_to_binary({1, 0}) -> <<"HTTP/1.0">>. + %% @doc Decode a URL encoded binary. %% @equiv urldecode(Bin, crash) -spec urldecode(binary()) -> binary(). diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl index 04abfbc..9e1ad88 100644 --- a/src/cowboy_http_protocol.erl +++ b/src/cowboy_http_protocol.erl @@ -48,6 +48,8 @@ dispatch :: cowboy_dispatcher:dispatch_rules(), handler :: {module(), any()}, onrequest :: undefined | fun((#http_req{}) -> #http_req{}), + onresponse = undefined :: undefined | fun((cowboy_http:status(), + cowboy_http:headers(), #http_req{}) -> #http_req{}), urldecode :: {fun((binary(), T) -> binary()), T}, req_empty_lines = 0 :: integer(), max_empty_lines :: integer(), @@ -79,6 +81,7 @@ init(ListenerPid, Socket, Transport, Opts) -> MaxKeepalive = proplists:get_value(max_keepalive, Opts, infinity), MaxLineLength = proplists:get_value(max_line_length, Opts, 4096), OnRequest = proplists:get_value(onrequest, Opts), + OnResponse = proplists:get_value(onresponse, Opts), Timeout = proplists:get_value(timeout, Opts, 5000), URLDecDefault = {fun cowboy_http:urldecode/2, crash}, URLDec = proplists:get_value(urldecode, Opts, URLDecDefault), @@ -86,7 +89,8 @@ init(ListenerPid, Socket, Transport, Opts) -> wait_request(#state{listener=ListenerPid, socket=Socket, transport=Transport, dispatch=Dispatch, max_empty_lines=MaxEmptyLines, max_keepalive=MaxKeepalive, max_line_length=MaxLineLength, - timeout=Timeout, onrequest=OnRequest, urldecode=URLDec}). + timeout=Timeout, onrequest=OnRequest, onresponse=OnResponse, + urldecode=URLDec}). %% @private -spec parse_request(#state{}) -> ok. @@ -121,19 +125,28 @@ request({http_request, Method, {absoluteURI, _Scheme, _Host, _Port, Path}, request({http_request, Method, {abs_path, Path}, Version}, State); request({http_request, Method, {abs_path, AbsPath}, Version}, State=#state{socket=Socket, transport=Transport, - urldecode={URLDecFun, URLDecArg}=URLDec}) -> + req_keepalive=Keepalive, max_keepalive=MaxKeepalive, + onresponse=OnResponse, urldecode={URLDecFun, URLDecArg}=URLDec}) -> URLDecode = fun(Bin) -> URLDecFun(Bin, URLDecArg) end, {Path, RawPath, Qs} = cowboy_dispatcher:split_path(AbsPath, URLDecode), - ConnAtom = version_to_connection(Version), + ConnAtom = if Keepalive < MaxKeepalive -> version_to_connection(Version); + true -> close + end, parse_header(#http_req{socket=Socket, transport=Transport, connection=ConnAtom, pid=self(), method=Method, version=Version, - path=Path, raw_path=RawPath, raw_qs=Qs, urldecode=URLDec}, State); + path=Path, raw_path=RawPath, raw_qs=Qs, onresponse=OnResponse, + urldecode=URLDec}, State); request({http_request, Method, '*', Version}, - State=#state{socket=Socket, transport=Transport, urldecode=URLDec}) -> - ConnAtom = version_to_connection(Version), + State=#state{socket=Socket, transport=Transport, + req_keepalive=Keepalive, max_keepalive=MaxKeepalive, + onresponse=OnResponse, urldecode=URLDec}) -> + ConnAtom = if Keepalive < MaxKeepalive -> version_to_connection(Version); + true -> close + end, parse_header(#http_req{socket=Socket, transport=Transport, connection=ConnAtom, pid=self(), method=Method, version=Version, - path='*', raw_path= <<"*">>, raw_qs= <<>>, urldecode=URLDec}, State); + path='*', raw_path= <<"*">>, raw_qs= <<>>, onresponse=OnResponse, + urldecode=URLDec}, State); request({http_request, _Method, _URI, _Version}, State) -> error_terminate(501, State); request({http_error, <<"\r\n">>}, @@ -186,7 +199,9 @@ header({http_header, _I, 'Host', _R, RawHost}, Req=#http_req{ header({http_header, _I, 'Host', _R, _V}, Req, State) -> parse_header(Req, State); header({http_header, _I, 'Connection', _R, Connection}, - Req=#http_req{headers=Headers}, State) -> + Req=#http_req{headers=Headers}, State=#state{ + req_keepalive=Keepalive, max_keepalive=MaxKeepalive}) + when Keepalive < MaxKeepalive -> Req2 = Req#http_req{headers=[{'Connection', Connection}|Headers]}, {ConnTokens, Req3} = cowboy_http_req:parse_header('Connection', Req2), @@ -316,16 +331,15 @@ handler_loop_timeout(State=#state{loop_timeout=Timeout, loop_timeout_ref=PrevRef}) -> _ = case PrevRef of undefined -> ignore; PrevRef -> erlang:cancel_timer(PrevRef) end, - TRef = make_ref(), - erlang:send_after(Timeout, self(), {?MODULE, timeout, TRef}), + TRef = erlang:start_timer(Timeout, self(), ?MODULE), State#state{loop_timeout_ref=TRef}. -spec handler_loop(any(), #http_req{}, #state{}) -> ok. handler_loop(HandlerState, Req, State=#state{loop_timeout_ref=TRef}) -> receive - {?MODULE, timeout, TRef} -> + {timeout, TRef, ?MODULE} -> terminate_request(HandlerState, Req, State); - {?MODULE, timeout, OlderTRef} when is_reference(OlderTRef) -> + {timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) -> handler_loop(HandlerState, Req, State); Message -> handler_call(HandlerState, Req, State, Message) @@ -376,15 +390,14 @@ terminate_request(HandlerState, Req, State) -> next_request(Req, State, HandlerRes). -spec next_request(#http_req{}, #state{}, any()) -> ok. -next_request(Req=#http_req{connection=Conn}, - State=#state{req_keepalive=Keepalive, max_keepalive=MaxKeepalive}, - HandlerRes) -> +next_request(Req=#http_req{connection=Conn}, State=#state{ + req_keepalive=Keepalive}, HandlerRes) -> RespRes = ensure_response(Req), {BodyRes, Buffer} = ensure_body_processed(Req), %% Flush the resp_sent message before moving on. receive {cowboy_http_req, resp_sent} -> ok after 0 -> ok end, case {HandlerRes, BodyRes, RespRes, Conn} of - {ok, ok, ok, keepalive} when Keepalive < MaxKeepalive -> + {ok, ok, ok, keepalive} -> ?MODULE:parse_request(State#state{ buffer=Buffer, req_empty_lines=0, req_keepalive=Keepalive + 1}); @@ -425,12 +438,13 @@ ensure_response(#http_req{socket=Socket, transport=Transport, %% Only send an error reply if there is no resp_sent message. -spec error_terminate(cowboy_http:status(), #state{}) -> ok. -error_terminate(Code, State=#state{socket=Socket, transport=Transport}) -> +error_terminate(Code, State=#state{socket=Socket, transport=Transport, + onresponse=OnResponse}) -> receive {cowboy_http_req, resp_sent} -> ok after 0 -> _ = cowboy_http_req:reply(Code, #http_req{ - socket=Socket, transport=Transport, + socket=Socket, transport=Transport, onresponse=OnResponse, connection=close, pid=self(), resp_state=waiting}), ok end, diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl index a6e8834..8f1f789 100644 --- a/src/cowboy_http_req.erl +++ b/src/cowboy_http_req.erl @@ -456,10 +456,11 @@ stream_body(Req=#http_req{body_state=done}) -> -spec stream_body_recv(#http_req{}) -> {ok, binary(), #http_req{}} | {error, atom()}. -stream_body_recv(Req=#http_req{transport=Transport, socket=Socket}) -> +stream_body_recv(Req=#http_req{ + transport=Transport, socket=Socket, buffer=Buffer}) -> %% @todo Allow configuring the timeout. case Transport:recv(Socket, 0, 5000) of - {ok, Data} -> transfer_decode(Data, Req); + {ok, Data} -> transfer_decode(<< Buffer/binary, Data/binary >>, Req); {error, Reason} -> {error, Reason} end. @@ -477,7 +478,7 @@ transfer_decode(Data, Req=#http_req{ {stream, TransferDecode, TransferState2, ContentDecode}}); %% @todo {header(s) for chunked more -> - stream_body_recv(Req); + stream_body_recv(Req#http_req{buffer=Data}); {done, Length, Rest} -> Req2 = transfer_decode_done(Length, Rest, Req), {done, Req2}; @@ -562,34 +563,26 @@ body_qs(Req=#http_req{urldecode={URLDecFun, URLDecArg}}) -> %% %% Use this function for multipart streaming. For each part in the request, %% this function returns <em>{headers, Headers}</em> followed by a sequence of -%% <em>{data, Data}</em> tuples and finally <em>end_of_part</em>. When there +%% <em>{body, Data}</em> tuples and finally <em>end_of_part</em>. When there %% is no part to parse anymore, <em>eof</em> is returned. %% %% If the request Content-Type is not a multipart one, <em>{error, badarg}</em> %% is returned. -spec multipart_data(#http_req{}) -> {{headers, cowboy_http:headers()} - | {data, binary()} | end_of_part | eof, + | {body, binary()} | end_of_part | eof, #http_req{}}. multipart_data(Req=#http_req{body_state=waiting}) -> {{<<"multipart">>, _SubType, Params}, Req2} = parse_header('Content-Type', Req), {_, Boundary} = lists:keyfind(<<"boundary">>, 1, Params), - {Length, Req3=#http_req{buffer=Buffer}} = - parse_header('Content-Length', Req2), - multipart_data(Req3, Length, cowboy_multipart:parser(Boundary), Buffer); + {Length, Req3} = parse_header('Content-Length', Req2), + multipart_data(Req3, Length, {more, cowboy_multipart:parser(Boundary)}); multipart_data(Req=#http_req{body_state={multipart, Length, Cont}}) -> multipart_data(Req, Length, Cont()); multipart_data(Req=#http_req{body_state=done}) -> {eof, Req}. -multipart_data(Req, Length, Parser, Buffer) when byte_size(Buffer) >= Length -> - << Data:Length/binary, Rest/binary >> = Buffer, - multipart_data(Req#http_req{buffer=Rest}, 0, Parser(Data)); -multipart_data(Req, Length, Parser, Buffer) -> - NewLength = Length - byte_size(Buffer), - multipart_data(Req#http_req{buffer= <<>>}, NewLength, Parser(Buffer)). - multipart_data(Req, Length, {headers, Headers, Cont}) -> {{headers, Headers}, Req#http_req{body_state={multipart, Length, Cont}}}; multipart_data(Req, Length, {body, Data, Cont}) -> @@ -600,15 +593,15 @@ multipart_data(Req, 0, eof) -> {eof, Req#http_req{body_state=done}}; multipart_data(Req=#http_req{socket=Socket, transport=Transport}, Length, eof) -> + %% We just want to skip so no need to stream data here. {ok, _Data} = Transport:recv(Socket, Length, 5000), {eof, Req#http_req{body_state=done}}; -multipart_data(Req=#http_req{socket=Socket, transport=Transport}, - Length, {more, Parser}) when Length > 0 -> - case Transport:recv(Socket, 0, 5000) of - {ok, << Data:Length/binary, Buffer/binary >>} -> - multipart_data(Req#http_req{buffer=Buffer}, 0, Parser(Data)); - {ok, Data} -> - multipart_data(Req, Length - byte_size(Data), Parser(Data)) +multipart_data(Req, Length, {more, Parser}) when Length > 0 -> + case stream_body(Req) of + {ok, << Data:Length/binary, Buffer/binary >>, Req2} -> + multipart_data(Req2#http_req{buffer=Buffer}, 0, Parser(Data)); + {ok, Data, Req2} -> + multipart_data(Req2, Length - byte_size(Data), Parser(Data)) end. %% @doc Skip a part returned by the multipart parser. @@ -667,7 +660,6 @@ set_resp_body(Body, Req) -> set_resp_body_fun(StreamLen, StreamFun, Req) -> {ok, Req#http_req{resp_body={StreamLen, StreamFun}}}. - %% @doc Return whether the given header has been set for the response. -spec has_resp_header(cowboy_http:header(), #http_req{}) -> boolean(). has_resp_header(Name, #http_req{resp_headers=RespHeaders}) -> @@ -695,24 +687,29 @@ reply(Status, Headers, Req=#http_req{resp_body=Body}) -> %% @doc Send a reply to the client. -spec reply(cowboy_http:status(), cowboy_http:headers(), iodata(), #http_req{}) -> {ok, #http_req{}}. -reply(Status, Headers, Body, Req=#http_req{socket=Socket, - transport=Transport, connection=Connection, pid=ReqPid, +reply(Status, Headers, Body, Req=#http_req{socket=Socket, transport=Transport, + version=Version, connection=Connection, method=Method, resp_state=waiting, resp_headers=RespHeaders}) -> RespConn = response_connection(Headers, Connection), ContentLen = case Body of {CL, _} -> CL; _ -> iolist_size(Body) end, - Head = response_head(Status, Headers, RespHeaders, [ - {<<"Connection">>, atom_to_connection(Connection)}, + HTTP11Headers = case Version of + {1, 1} -> [{<<"Connection">>, atom_to_connection(Connection)}]; + _ -> [] + end, + {ReplyType, Req2} = response(Status, Headers, RespHeaders, [ {<<"Content-Length">>, integer_to_list(ContentLen)}, {<<"Date">>, cowboy_clock:rfc1123()}, {<<"Server">>, <<"Cowboy">>} - ]), - case {Method, Body} of - {'HEAD', _} -> Transport:send(Socket, Head); - {_, {_, StreamFun}} -> Transport:send(Socket, Head), StreamFun(); - {_, _} -> Transport:send(Socket, [Head, Body]) + |HTTP11Headers], Req), + if Method =:= 'HEAD' -> ok; + ReplyType =:= hook -> ok; %% Hook replied for us, stop there. + true -> + case Body of + {_, StreamFun} -> StreamFun(); + _ -> Transport:send(Socket, Body) + end end, - ReqPid ! {?MODULE, resp_sent}, - {ok, Req#http_req{connection=RespConn, resp_state=done, + {ok, Req2#http_req{connection=RespConn, resp_state=done, resp_headers=[], resp_body= <<>>}}. %% @equiv chunked_reply(Status, [], Req) @@ -724,25 +721,21 @@ chunked_reply(Status, Req) -> %% @see cowboy_http_req:chunk/2 -spec chunked_reply(cowboy_http:status(), cowboy_http:headers(), #http_req{}) -> {ok, #http_req{}}. -chunked_reply(Status, Headers, Req=#http_req{socket=Socket, - transport=Transport, version=Version, connection=Connection, - pid=ReqPid, resp_state=waiting, resp_headers=RespHeaders}) -> +chunked_reply(Status, Headers, Req=#http_req{ + version=Version, connection=Connection, + resp_state=waiting, resp_headers=RespHeaders}) -> RespConn = response_connection(Headers, Connection), - DefaultHeaders = [ - {<<"Date">>, cowboy_clock:rfc1123()}, - {<<"Server">>, <<"Cowboy">>} - ], - DefaultHeaders2 = case Version of + HTTP11Headers = case Version of {1, 1} -> [ - {<<"Connection">>, atom_to_connection(Connection)}, - {<<"Transfer-Encoding">>, <<"chunked">>} - ] ++ DefaultHeaders; - _ -> DefaultHeaders + {<<"Connection">>, atom_to_connection(Connection)}, + {<<"Transfer-Encoding">>, <<"chunked">>}]; + _ -> [] end, - Head = response_head(Status, Headers, RespHeaders, DefaultHeaders2), - Transport:send(Socket, Head), - ReqPid ! {?MODULE, resp_sent}, - {ok, Req#http_req{connection=RespConn, resp_state=chunks, + {_, Req2} = response(Status, Headers, RespHeaders, [ + {<<"Date">>, cowboy_clock:rfc1123()}, + {<<"Server">>, <<"Cowboy">>} + |HTTP11Headers], Req), + {ok, Req2#http_req{connection=RespConn, resp_state=chunks, resp_headers=[], resp_body= <<>>}}. %% @doc Send a chunk of data. @@ -761,14 +754,12 @@ chunk(Data, #http_req{socket=Socket, transport=Transport, resp_state=chunks}) -> %% @private -spec upgrade_reply(cowboy_http:status(), cowboy_http:headers(), #http_req{}) -> {ok, #http_req{}}. -upgrade_reply(Status, Headers, Req=#http_req{socket=Socket, transport=Transport, - pid=ReqPid, resp_state=waiting, resp_headers=RespHeaders}) -> - Head = response_head(Status, Headers, RespHeaders, [ +upgrade_reply(Status, Headers, Req=#http_req{ + resp_state=waiting, resp_headers=RespHeaders}) -> + {_, Req2} = response(Status, Headers, RespHeaders, [ {<<"Connection">>, <<"Upgrade">>} - ]), - Transport:send(Socket, Head), - ReqPid ! {?MODULE, resp_sent}, - {ok, Req#http_req{resp_state=done, resp_headers=[], resp_body= <<>>}}. + ], Req), + {ok, Req2#http_req{resp_state=done, resp_headers=[], resp_body= <<>>}}. %% Misc API. @@ -798,6 +789,35 @@ transport(#http_req{transport=Transport, socket=Socket}) -> %% Internal. +-spec response(cowboy_http:status(), cowboy_http:headers(), + cowboy_http:headers(), cowboy_http:headers(), #http_req{}) + -> {normal | hook, #http_req{}}. +response(Status, Headers, RespHeaders, DefaultHeaders, Req=#http_req{ + socket=Socket, transport=Transport, version=Version, + pid=ReqPid, onresponse=OnResponse}) -> + FullHeaders = response_merge_headers(Headers, RespHeaders, DefaultHeaders), + Req2 = case OnResponse of + undefined -> Req; + OnResponse -> OnResponse(Status, FullHeaders, + %% Don't call 'onresponse' from the hook itself. + Req#http_req{resp_headers=[], resp_body= <<>>, + onresponse=undefined}) + end, + ReplyType = case Req2#http_req.resp_state of + waiting -> + HTTPVer = cowboy_http:version_to_binary(Version), + StatusLine = << HTTPVer/binary, " ", + (status(Status))/binary, "\r\n" >>, + HeaderLines = [[Key, <<": ">>, Value, <<"\r\n">>] + || {Key, Value} <- FullHeaders], + Transport:send(Socket, [StatusLine, HeaderLines, <<"\r\n">>]), + ReqPid ! {?MODULE, resp_sent}, + normal; + _ -> + hook + end, + {ReplyType, Req2}. + -spec response_connection(cowboy_http:headers(), keepalive | close) -> keepalive | close. response_connection([], Connection) -> @@ -819,17 +839,13 @@ response_connection_parse(ReplyConn) -> Tokens = cowboy_http:nonempty_list(ReplyConn, fun cowboy_http:token/2), cowboy_http:connection_to_atom(Tokens). --spec response_head(cowboy_http:status(), cowboy_http:headers(), - cowboy_http:headers(), cowboy_http:headers()) -> iolist(). -response_head(Status, Headers, RespHeaders, DefaultHeaders) -> - StatusLine = <<"HTTP/1.1 ", (status(Status))/binary, "\r\n">>, +-spec response_merge_headers(cowboy_http:headers(), cowboy_http:headers(), + cowboy_http:headers()) -> cowboy_http:headers(). +response_merge_headers(Headers, RespHeaders, DefaultHeaders) -> Headers2 = [{header_to_binary(Key), Value} || {Key, Value} <- Headers], - Headers3 = merge_headers( + merge_headers( merge_headers(Headers2, RespHeaders), - DefaultHeaders), - Headers4 = [[Key, <<": ">>, Value, <<"\r\n">>] - || {Key, Value} <- Headers3], - [StatusLine, Headers4, <<"\r\n">>]. + DefaultHeaders). -spec merge_headers(cowboy_http:headers(), cowboy_http:headers()) -> cowboy_http:headers(). @@ -894,6 +910,9 @@ status(423) -> <<"423 Locked">>; status(424) -> <<"424 Failed Dependency">>; status(425) -> <<"425 Unordered Collection">>; status(426) -> <<"426 Upgrade Required">>; +status(428) -> <<"428 Precondition Required">>; +status(429) -> <<"429 Too Many Requests">>; +status(431) -> <<"431 Request Header Fields Too Large">>; status(500) -> <<"500 Internal Server Error">>; status(501) -> <<"501 Not Implemented">>; status(502) -> <<"502 Bad Gateway">>; @@ -903,6 +922,7 @@ status(505) -> <<"505 HTTP Version Not Supported">>; status(506) -> <<"506 Variant Also Negotiates">>; status(507) -> <<"507 Insufficient Storage">>; status(510) -> <<"510 Not Extended">>; +status(511) -> <<"511 Network Authentication Required">>; status(B) when is_binary(B) -> B. -spec header_to_binary(cowboy_http:header()) -> binary(). diff --git a/src/cowboy_http_websocket.erl b/src/cowboy_http_websocket.erl index ab96e93..f550041 100644 --- a/src/cowboy_http_websocket.erl +++ b/src/cowboy_http_websocket.erl @@ -14,27 +14,9 @@ %% @doc WebSocket protocol implementation. %% -%% Supports the protocol version 0 (hixie-76), version 7 (hybi-7) -%% and version 8 (hybi-8, hybi-9 and hybi-10). -%% -%% Version 0 is supported by the following browsers: -%% <ul> -%% <li>Firefox 4-5 (disabled by default)</li> -%% <li>Chrome 6-13</li> -%% <li>Safari 5.0.1+</li> -%% <li>Opera 11.00+ (disabled by default)</li> -%% </ul> -%% -%% Version 7 is supported by the following browser: -%% <ul> -%% <li>Firefox 6</li> -%% </ul> -%% -%% Version 8+ is supported by the following browsers: -%% <ul> -%% <li>Firefox 7+</li> -%% <li>Chrome 14+</li> -%% </ul> +%% When using websockets, make sure that the crypto application is +%% included in your release. If you are not using releases then there +%% is no need for concern as crypto is already included. -module(cowboy_http_websocket). -export([upgrade/4]). %% API. @@ -232,8 +214,7 @@ handler_loop_timeout(State=#state{timeout=infinity}) -> handler_loop_timeout(State=#state{timeout=Timeout, timeout_ref=PrevRef}) -> _ = case PrevRef of undefined -> ignore; PrevRef -> erlang:cancel_timer(PrevRef) end, - TRef = make_ref(), - erlang:send_after(Timeout, self(), {?MODULE, timeout, TRef}), + TRef = erlang:start_timer(Timeout, self(), ?MODULE), State#state{timeout_ref=TRef}. %% @private @@ -248,9 +229,9 @@ handler_loop(State=#state{messages={OK, Closed, Error}, timeout_ref=TRef}, handler_terminate(State, Req, HandlerState, {error, closed}); {Error, Socket, Reason} -> handler_terminate(State, Req, HandlerState, {error, Reason}); - {?MODULE, timeout, TRef} -> + {timeout, TRef, ?MODULE} -> websocket_close(State, Req, HandlerState, {normal, timeout}); - {?MODULE, timeout, OlderTRef} when is_reference(OlderTRef) -> + {timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) -> handler_loop(State, Req, HandlerState, SoFar); Message -> handler_call(State, Req, HandlerState, diff --git a/src/cowboy_ssl_transport.erl b/src/cowboy_ssl_transport.erl index 3b130f0..a661622 100644 --- a/src/cowboy_ssl_transport.erl +++ b/src/cowboy_ssl_transport.erl @@ -25,6 +25,7 @@ -module(cowboy_ssl_transport). -export([name/0, messages/0, listen/1, accept/2, recv/3, send/2, setopts/2, controlling_process/2, peername/1, close/1, sockname/1]). +-export([connect/3]). %% @doc Name of this transport API, <em>ssl</em>. -spec name() -> ssl. @@ -37,6 +38,12 @@ name() -> ssl. -spec messages() -> {ssl, ssl_closed, ssl_error}. messages() -> {ssl, ssl_closed, ssl_error}. +%% @private +%% @todo Probably filter Opts? +connect(Host, Port, Opts) when is_list(Host), is_integer(Port) -> + ssl:connect(Host, Port, + Opts ++ [binary, {active, false}, {packet, raw}]). + %% @doc Setup a socket to listen on the given port on the local host. %% %% The available options are: diff --git a/src/cowboy_tcp_transport.erl b/src/cowboy_tcp_transport.erl index f197dd1..079494d 100644 --- a/src/cowboy_tcp_transport.erl +++ b/src/cowboy_tcp_transport.erl @@ -21,6 +21,7 @@ -export([name/0, messages/0, listen/1, accept/2, recv/3, send/2, setopts/2, controlling_process/2, peername/1, close/1, sockname/1]). +-export([connect/3]). %% @doc Name of this transport API, <em>tcp</em>. -spec name() -> tcp. @@ -33,6 +34,11 @@ name() -> tcp. -spec messages() -> {tcp, tcp_closed, tcp_error}. messages() -> {tcp, tcp_closed, tcp_error}. +%% @private +connect(Host, Port, Opts) when is_list(Host), is_integer(Port) -> + gen_tcp:connect(Host, Port, + Opts ++ [binary, {active, false}, {packet, raw}]). + %% @doc Setup a socket to listen on the given port on the local host. %% %% The available options are: diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl index cebc1e6..e247138 100644 --- a/test/http_SUITE.erl +++ b/test/http_SUITE.erl @@ -17,44 +17,110 @@ -include_lib("common_test/include/ct.hrl"). --export([all/0, groups/0, init_per_suite/1, end_per_suite/1, - init_per_group/2, end_per_group/2]). %% ct. --export([chunked_response/1, headers_dupe/1, headers_huge/1, - keepalive_nl/1, max_keepalive/1, nc_rand/1, nc_zero/1, - pipeline/1, raw/1, set_resp_header/1, set_resp_overwrite/1, - set_resp_body/1, stream_body_set_resp/1, response_as_req/1, - static_mimetypes_function/1, static_attribute_etag/1, - static_function_etag/1, multipart/1, te_identity/1, - te_chunked/1, te_chunked_delayed/1]). %% http. --export([http_200/1, http_404/1, handler_errors/1, - file_200/1, file_403/1, dir_403/1, file_404/1, - file_400/1]). %% http and https. --export([http_10_hostless/1, http_10_chunkless/1]). %% misc. --export([rest_simple/1, rest_keepalive/1, rest_keepalive_post/1, - rest_nodelete/1, rest_resource_etags/1]). %% rest. --export([onrequest/1, onrequest_reply/1]). %% hooks. +%% ct. +-export([all/0]). +-export([groups/0]). +-export([init_per_suite/1]). +-export([end_per_suite/1]). +-export([init_per_group/2]). +-export([end_per_group/2]). + +%% Tests. +-export([check_raw_status/1]). +-export([check_status/1]). +-export([chunked_response/1]). +-export([echo_body/1]). +-export([error_chain_handle_after_reply/1]). +-export([error_chain_handle_before_reply/1]). +-export([error_handle_after_reply/1]). +-export([error_init_after_reply/1]). +-export([error_init_reply_handle_error/1]). +-export([headers_dupe/1]). +-export([http10_chunkless/1]). +-export([http10_hostless/1]). +-export([keepalive_max/1]). +-export([keepalive_nl/1]). +-export([multipart/1]). +-export([nc_rand/1]). +-export([nc_zero/1]). +-export([onrequest/1]). +-export([onrequest_reply/1]). +-export([onresponse_crash/1]). +-export([onresponse_reply/1]). +-export([pipeline/1]). +-export([rest_keepalive/1]). +-export([rest_keepalive_post/1]). +-export([rest_nodelete/1]). +-export([rest_resource_etags/1]). +-export([rest_resource_etags_if_none_match/1]). +-export([set_resp_body/1]). +-export([set_resp_header/1]). +-export([set_resp_overwrite/1]). +-export([static_attribute_etag/1]). +-export([static_function_etag/1]). +-export([static_mimetypes_function/1]). +-export([static_test_file/1]). +-export([static_test_file_css/1]). +-export([stream_body_set_resp/1]). +-export([te_chunked/1]). +-export([te_chunked_delayed/1]). +-export([te_identity/1]). %% ct. all() -> - [{group, http}, {group, https}, {group, misc}, {group, rest}, - {group, hooks}]. + [{group, http}, {group, https}, {group, onrequest}, {group, onresponse}]. groups() -> - BaseTests = [http_200, http_404, handler_errors, - file_200, file_403, dir_403, file_404, file_400], - [{http, [], [chunked_response, headers_dupe, headers_huge, - keepalive_nl, max_keepalive, nc_rand, nc_zero, pipeline, raw, - set_resp_header, set_resp_overwrite, - set_resp_body, response_as_req, stream_body_set_resp, - static_mimetypes_function, static_attribute_etag, - static_function_etag, multipart, te_identity, te_chunked, - te_chunked_delayed] ++ BaseTests}, - {https, [], BaseTests}, - {misc, [], [http_10_hostless, http_10_chunkless]}, - {rest, [], [rest_simple, rest_keepalive, rest_keepalive_post, - rest_nodelete, rest_resource_etags]}, - {hooks, [], [onrequest, onrequest_reply]}]. + Tests = [ + check_raw_status, + check_status, + chunked_response, + echo_body, + error_chain_handle_after_reply, + error_chain_handle_before_reply, + error_handle_after_reply, + error_init_after_reply, + error_init_reply_handle_error, + headers_dupe, + http10_chunkless, + http10_hostless, + keepalive_max, + keepalive_nl, + multipart, + nc_rand, + nc_zero, + pipeline, + rest_keepalive, + rest_keepalive_post, + rest_nodelete, + rest_resource_etags, + rest_resource_etags_if_none_match, + set_resp_body, + set_resp_header, + set_resp_overwrite, + static_attribute_etag, + static_function_etag, + static_mimetypes_function, + static_test_file, + static_test_file_css, + stream_body_set_resp, + te_chunked, + te_chunked_delayed, + te_identity + ], + [ + {http, [], Tests}, + {https, [], Tests}, + {onrequest, [], [ + onrequest, + onrequest_reply + ]}, + {onresponse, [], [ + onresponse_crash, + onresponse_reply + ]} + ]. init_per_suite(Config) -> application:start(inets), @@ -68,57 +134,68 @@ end_per_suite(_Config) -> init_per_group(http, Config) -> Port = 33080, + Transport = cowboy_tcp_transport, Config1 = init_static_dir(Config), - cowboy:start_listener(http, 100, - cowboy_tcp_transport, [{port, Port}], - cowboy_http_protocol, [{max_keepalive, 50}, - {dispatch, init_http_dispatch(Config1)}] + {ok, _} = cowboy:start_listener(http, 100, + Transport, [{port, Port}], + cowboy_http_protocol, [ + {dispatch, init_dispatch(Config1)}, + {max_keepalive, 50}, + {timeout, 500}] ), - [{scheme, "http"}, {port, Port}|Config1]; + {ok, Client} = cowboy_client:init([]), + [{scheme, <<"http">>}, {port, Port}, {opts, []}, + {transport, Transport}, {client, Client}|Config1]; init_per_group(https, Config) -> Port = 33081, + Transport = cowboy_ssl_transport, + Opts = [ + {certfile, ?config(data_dir, Config) ++ "cert.pem"}, + {keyfile, ?config(data_dir, Config) ++ "key.pem"}, + {password, "cowboy"} + ], Config1 = init_static_dir(Config), application:start(crypto), application:start(public_key), application:start(ssl), - DataDir = ?config(data_dir, Config), - {ok,_} = cowboy:start_listener(https, 100, - cowboy_ssl_transport, [ - {port, Port}, {certfile, DataDir ++ "cert.pem"}, - {keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}], - cowboy_http_protocol, [{dispatch, init_https_dispatch(Config1)}] + {ok, _} = cowboy:start_listener(https, 100, + Transport, Opts ++ [{port, Port}], + cowboy_http_protocol, [ + {dispatch, init_dispatch(Config1)}, + {max_keepalive, 50}, + {timeout, 500}] ), - [{scheme, "https"}, {port, Port}|Config1]; -init_per_group(misc, Config) -> + {ok, Client} = cowboy_client:init(Opts), + [{scheme, <<"https">>}, {port, Port}, {opts, Opts}, + {transport, Transport}, {client, Client}|Config1]; +init_per_group(onrequest, Config) -> Port = 33082, - {ok,_} = cowboy:start_listener(misc, 100, - cowboy_tcp_transport, [{port, Port}], - cowboy_http_protocol, [{dispatch, [{'_', [ - {[<<"chunked_response">>], chunked_handler, []}, - {[], http_handler, []} - ]}]}]), - [{port, Port}|Config]; -init_per_group(rest, Config) -> + Transport = cowboy_tcp_transport, + {ok, _} = cowboy:start_listener(onrequest, 100, + Transport, [{port, Port}], + cowboy_http_protocol, [ + {dispatch, init_dispatch(Config)}, + {max_keepalive, 50}, + {onrequest, fun onrequest_hook/1}, + {timeout, 500} + ]), + {ok, Client} = cowboy_client:init([]), + [{scheme, <<"http">>}, {port, Port}, {opts, []}, + {transport, Transport}, {client, Client}|Config]; +init_per_group(onresponse, Config) -> Port = 33083, - {ok,_} = cowboy:start_listener(rest, 100, - cowboy_tcp_transport, [{port, Port}], - cowboy_http_protocol, [{dispatch, [{'_', [ - {[<<"simple">>], rest_simple_resource, []}, - {[<<"forbidden_post">>], rest_forbidden_resource, [true]}, - {[<<"simple_post">>], rest_forbidden_resource, [false]}, - {[<<"nodelete">>], rest_nodelete_resource, []}, - {[<<"resetags">>], rest_resource_etags, []} - ]}]}]), - [{scheme, "http"},{port, Port}|Config]; -init_per_group(hooks, Config) -> - Port = 33084, - {ok, _} = cowboy:start_listener(hooks, 100, - cowboy_tcp_transport, [{port, Port}], + Transport = cowboy_tcp_transport, + {ok, _} = cowboy:start_listener(onresponse, 100, + Transport, [{port, Port}], cowboy_http_protocol, [ - {dispatch, init_http_dispatch(Config)}, - {onrequest, fun onrequest_hook/1} + {dispatch, init_dispatch(Config)}, + {max_keepalive, 50}, + {onresponse, fun onresponse_hook/3}, + {timeout, 500} ]), - [{scheme, "http"}, {port, Port}|Config]. + {ok, Client} = cowboy_client:init([]), + [{scheme, <<"http">>}, {port, Port}, {opts, []}, + {transport, Transport}, {client, Client}|Config]. end_per_group(https, Config) -> cowboy:stop_listener(https), @@ -130,13 +207,13 @@ end_per_group(https, Config) -> end_per_group(http, Config) -> cowboy:stop_listener(http), end_static_dir(Config); -end_per_group(Listener, _Config) -> - cowboy:stop_listener(Listener), +end_per_group(Name, _) -> + cowboy:stop_listener(Name), ok. %% Dispatch configuration. -init_http_dispatch(Config) -> +init_dispatch(Config) -> [ {[<<"localhost">>], [ {[<<"chunked_response">>], chunked_handler, []}, @@ -168,14 +245,16 @@ init_http_dispatch(Config) -> {etag, {fun static_function_etag/2, etag_data}}]}, {[<<"multipart">>], http_handler_multipart, []}, {[<<"echo">>, <<"body">>], http_handler_echo_body, []}, + {[<<"simple">>], rest_simple_resource, []}, + {[<<"forbidden_post">>], rest_forbidden_resource, [true]}, + {[<<"simple_post">>], rest_forbidden_resource, [false]}, + {[<<"nodelete">>], rest_nodelete_resource, []}, + {[<<"resetags">>], rest_resource_etags, []}, + {[<<"loop_timeout">>], http_handler_loop_timeout, []}, {[], http_handler, []} ]} ]. -init_https_dispatch(Config) -> - init_http_dispatch(Config). - - init_static_dir(Config) -> Dir = filename:join(?config(priv_dir, Config), "static"), Level1 = fun(Name) -> filename:join(Dir, Name) end, @@ -199,69 +278,263 @@ end_static_dir(Config) -> ok = file:del_dir(Dir), Config. -%% http. +%% Convenience functions. + +quick_raw(Data, Config) -> + Client = ?config(client, Config), + Transport = ?config(transport, Config), + {ok, Client2} = cowboy_client:connect( + Transport, "localhost", ?config(port, Config), Client), + {ok, Client3} = cowboy_client:raw_request(Data, Client2), + case cowboy_client:response(Client3) of + {ok, Status, _, _} -> Status; + {error, _} -> closed + end. + +build_url(Path, Config) -> + {scheme, Scheme} = lists:keyfind(scheme, 1, Config), + {port, Port} = lists:keyfind(port, 1, Config), + PortBin = list_to_binary(integer_to_list(Port)), + PathBin = list_to_binary(Path), + << Scheme/binary, "://localhost:", PortBin/binary, PathBin/binary >>. + +quick_get(URL, Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url(URL, Config), Client), + {ok, Status, _, _} = cowboy_client:response(Client2), + Status. + +body_to_chunks(_, <<>>, Acc) -> + lists:reverse([<<"0\r\n\r\n">>|Acc]); +body_to_chunks(ChunkSize, Body, Acc) -> + BodySize = byte_size(Body), + ChunkSize2 = case BodySize < ChunkSize of + true -> BodySize; + false -> ChunkSize + end, + << Chunk:ChunkSize2/binary, Rest/binary >> = Body, + ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)), + body_to_chunks(ChunkSize, Rest, + [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]). + +%% Tests. + +check_raw_status(Config) -> + Huge = [$0 || _ <- lists:seq(1, 5000)], + HugeCookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77=" + "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _ <- lists:seq(1, 40)]), + ResponsePacket = +"HTTP/1.0 302 Found +Location: http://www.google.co.il/ +Cache-Control: private +Content-Type: text/html; charset=UTF-8 +Set-Cookie: PREF=ID=568f67013d4a7afa:FF=0:TM=1323014101:LM=1323014101:S=XqctDWC65MzKT0zC; expires=Tue, 03-Dec-2013 15:55:01 GMT; path=/; domain=.google.com +Date: Sun, 04 Dec 2011 15:55:01 GMT +Server: gws +Content-Length: 221 +X-XSS-Protection: 1; mode=block +X-Frame-Options: SAMEORIGIN +<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"> +<TITLE>302 Moved</TITLE></HEAD><BODY> +<H1>302 Moved</H1> +The document has moved +<A HREF=\"http://www.google.co.il/\">here</A>. +</BODY></HTML>", + Tests = [ + {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n" + "Set-Cookie: ", HugeCookie, "\r\n\r\n"]}, + {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"}, + {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"}, + {400, "\n"}, + {400, "Garbage\r\n\r\n"}, + {400, "\r\n\r\n\r\n\r\n\r\n\r\n"}, + {400, "GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n"}, + {400, "GET http://proxy/ HTTP/1.1\r\n\r\n"}, + {400, ResponsePacket}, + {408, "GET / HTTP/1.1\r\n"}, + {408, "GET / HTTP/1.1\r\nHost: localhost"}, + {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"}, + {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"}, + {413, Huge}, + {413, "GET / HTTP/1.1\r\n" ++ Huge}, + {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"}, + {closed, ""}, + {closed, "\r\n"}, + {closed, "\r\n\r\n"}, + {closed, "GET / HTTP/1.1"} + ], + _ = [{Status, Packet} = begin + Ret = quick_raw(Packet, Config), + {Ret, Packet} + end || {Status, Packet} <- Tests]. + +check_status(Config) -> + Tests = [ + {102, "/long_polling"}, + {200, "/"}, + {200, "/simple"}, + {204, "/loop_timeout"}, + {400, "/static/%2f"}, + {400, "/static/%2e"}, + {400, "/static/%2e%2e"}, + {403, "/static/test_dir"}, + {403, "/static/test_dir/"}, + {403, "/static/test_noread"}, + {404, "/not/found"}, + {404, "/static/not_found"}, + {500, "/handler_errors?case=handler_before_reply"}, + {500, "/handler_errors?case=init_before_reply"}, + {666, "/init_shutdown"} + ], + _ = [{Status, URL} = begin + Ret = quick_get(URL, Config), + {Ret, URL} + end || {Status, URL} <- Tests]. + +%% @todo Convert to cowboy_client. chunked_response(Config) -> - {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "chunked_handler\r\nworks fine!"}} = - httpc:request(build_url("/chunked_response", Config)). + {ok, {{"HTTP/1.1", 200, "OK"}, _, "chunked_handler\r\nworks fine!"}} + = httpc:request(binary_to_list(build_url("/chunked_response", Config))). + +%% Check if sending requests whose size is around the MTU breaks something. +echo_body(Config) -> + Client = ?config(client, Config), + {ok, [{mtu, MTU}]} = inet:ifget("lo", [mtu]), + [begin + Body = list_to_binary(lists:duplicate(Size, $a)), + {ok, Client2} = cowboy_client:request(<<"POST">>, + build_url("/echo/body", Config), + [{<<"connection">>, <<"close">>}], + Body, Client), + {ok, 200, _, Client3} = cowboy_client:response(Client2), + {ok, Body, _} = cowboy_client:response_body(Client3) + end || Size <- lists:seq(MTU - 500, MTU)]. + +error_chain_handle_after_reply(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/", Config), Client), + {ok, Client3} = cowboy_client:request(<<"GET">>, + build_url("/handler_errors?case=handle_after_reply", Config), Client2), + {ok, 200, _, Client4} = cowboy_client:response(Client3), + {ok, 200, _, Client5} = cowboy_client:response(Client4), + {error, closed} = cowboy_client:response(Client5). + +error_chain_handle_before_reply(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/", Config), Client), + {ok, Client3} = cowboy_client:request(<<"GET">>, + build_url("/handler_errors?case=handle_before_reply", Config), Client2), + {ok, 200, _, Client4} = cowboy_client:response(Client3), + {ok, 500, _, Client5} = cowboy_client:response(Client4), + {error, closed} = cowboy_client:response(Client5). + +error_handle_after_reply(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/handler_errors?case=handle_after_reply", Config), Client), + {ok, 200, _, Client3} = cowboy_client:response(Client2), + {error, closed} = cowboy_client:response(Client3). + +error_init_after_reply(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/handler_errors?case=init_after_reply", Config), Client), + {ok, 200, _, Client3} = cowboy_client:response(Client2), + {error, closed} = cowboy_client:response(Client3). + +error_init_reply_handle_error(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/handler_errors?case=init_reply_handle_error", Config), Client), + {ok, 200, _, Client3} = cowboy_client:response(Client2), + {error, closed} = cowboy_client:response(Client3). headers_dupe(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - ok = gen_tcp:send(Socket, "GET /headers/dupe HTTP/1.1\r\n" - "Host: localhost\r\nConnection: keep-alive\r\n\r\n"), - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - {_Start, _Length} = binary:match(Data, <<"Connection: close">>), - nomatch = binary:match(Data, <<"Connection: keep-alive">>), - {error, closed} = gen_tcp:recv(Socket, 0, 1000). - -headers_huge(Config) -> - Cookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77=" - "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _N <- lists:seq(1, 40)]), - {_Packet, 200} = raw_req(["GET / HTTP/1.0\r\nHost: localhost\r\n" - "Set-Cookie: ", Cookie, "\r\n\r\n"], Config). + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/headers/dupe", Config), Client), + {ok, 200, Headers, Client3} = cowboy_client:response(Client2), + {<<"connection">>, <<"close">>} + = lists:keyfind(<<"connection">>, 1, Headers), + Connections = [H || H = {Name, _} <- Headers, Name =:= <<"connection">>], + 1 = length(Connections), + {error, closed} = cowboy_client:response(Client3). + +http10_chunkless(Config) -> + Client = ?config(client, Config), + Transport = ?config(transport, Config), + {ok, Client2} = cowboy_client:connect( + Transport, "localhost", ?config(port, Config), Client), + Data = "GET /chunked_response HTTP/1.0\r\nHost: localhost\r\n\r\n", + {ok, Client3} = cowboy_client:raw_request(Data, Client2), + {ok, 200, Headers, Client4} = cowboy_client:response(Client3), + false = lists:keyfind(<<"transfer-encoding">>, 1, Headers), + %% Hack: we just try to get 28 bytes and compare. + {ok, Transport, Socket} = cowboy_client:transport(Client4), + Buffer = element(7, Client4), + Buffer2 = case Transport:recv(Socket, 28 - byte_size(Buffer), 1000) of + {ok, Recv} -> << Buffer/binary, Recv/binary >>; + _ -> Buffer + end, + <<"chunked_handler\r\nworks fine!">> = Buffer2. -keepalive_nl(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - ok = keepalive_nl_loop(Socket, 10), - ok = gen_tcp:close(Socket). +http10_hostless(Config) -> + Port10 = ?config(port, Config) + 10, + Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)), + cowboy:start_listener(Name, 5, + ?config(transport, Config), ?config(opts, Config) ++ [{port, Port10}], + cowboy_http_protocol, [ + {dispatch, [{'_', [ + {[<<"http1.0">>, <<"hostless">>], http_handler, []}]}]}, + {max_keepalive, 50}, + {timeout, 500}] + ), + 200 = quick_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n", + [{port, Port10}|Config]), + cowboy:stop_listener(http10). -keepalive_nl_loop(_Socket, 0) -> - ok; -keepalive_nl_loop(Socket, N) -> - ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n" - "Host: localhost\r\nConnection: keep-alive\r\n\r\n"), - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>), - nomatch = binary:match(Data, <<"Connection: close">>), - ok = gen_tcp:send(Socket, "\r\n"), %% extra nl - keepalive_nl_loop(Socket, N - 1). - -max_keepalive(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - ok = max_keepalive_loop(Socket, 50), - {error, closed} = gen_tcp:recv(Socket, 0, 1000). +keepalive_max(Config) -> + Client = ?config(client, Config), + URL = build_url("/", Config), + ok = keepalive_max_loop(Client, URL, 50). -max_keepalive_loop(_Socket, 0) -> +keepalive_max_loop(_, _, 0) -> ok; -max_keepalive_loop(Socket, N) -> - ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n" - "Host: localhost\r\nConnection: keep-alive\r\n\r\n"), - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>), - case N of - 1 -> {_, _} = binary:match(Data, <<"Connection: close">>); - N -> nomatch = binary:match(Data, <<"Connection: close">>) +keepalive_max_loop(Client, URL, N) -> + Headers = [{<<"connection">>, <<"keep-alive">>}], + {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client), + {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2), + Expected = case N of + 1 -> <<"close">>; + N -> <<"keep-alive">> end, - keepalive_nl_loop(Socket, N - 1). + {<<"connection">>, Expected} + = lists:keyfind(<<"connection">>, 1, RespHeaders), + keepalive_max_loop(Client3, URL, N - 1). + +keepalive_nl(Config) -> + Client = ?config(client, Config), + URL = build_url("/", Config), + ok = keepalive_nl_loop(Client, URL, 10). + +keepalive_nl_loop(_, _, 0) -> + ok; +keepalive_nl_loop(Client, URL, N) -> + Headers = [{<<"connection">>, <<"keep-alive">>}], + {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client), + {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2), + {<<"connection">>, <<"keep-alive">>} + = lists:keyfind(<<"connection">>, 1, RespHeaders), + {ok, Transport, Socket} = cowboy_client:transport(Client2), + ok = Transport:send(Socket, <<"\r\n">>), %% empty line + keepalive_nl_loop(Client3, URL, N - 1). multipart(Config) -> - Url = build_url("/multipart", Config), + Client = ?config(client, Config), Body = << "This is a preamble." "\r\n--OHai\r\nX-Name:answer\r\n\r\n42" @@ -269,21 +542,18 @@ multipart(Config) -> "\r\n--OHai--" "This is an epiloque." >>, - Request = {Url, [], "multipart/x-makes-no-sense; boundary=OHai", Body}, - {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, Response}} = - httpc:request(post, Request, [], [{body_format, binary}]), - Parts = binary_to_term(Response), + {ok, Client2} = cowboy_client:request(<<"POST">>, + build_url("/multipart", Config), + [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}], + Body, Client), + {ok, 200, _, Client3} = cowboy_client:response(Client2), + {ok, RespBody, _} = cowboy_client:response_body(Client3), + Parts = binary_to_term(RespBody), Parts = [ {[{<<"X-Name">>, <<"answer">>}], <<"42">>}, {[{'Server', <<"Cowboy">>}], <<"It rocks!\r\n">>} ]. -nc_rand(Config) -> - nc_reqs(Config, "/dev/urandom"). - -nc_zero(Config) -> - nc_reqs(Config, "/dev/zero"). - nc_reqs(Config, Input) -> Cat = os:find_executable("cat"), Nc = os:find_executable("nc"), @@ -295,235 +565,229 @@ nc_reqs(Config, Input) -> _Good -> %% Throw garbage at the server then check if it's still up. {port, Port} = lists:keyfind(port, 1, Config), - [nc_run_req(Port, Input) || _N <- lists:seq(1, 100)], - Packet = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n", - {Packet, 200} = raw_req(Packet, Config) + StrPort = integer_to_list(Port), + [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort) + || _ <- lists:seq(1, 100)], + 200 = quick_get("/", Config) end. -nc_run_req(Port, Input) -> - os:cmd("cat " ++ Input ++ " | nc localhost " ++ integer_to_list(Port)). - -pipeline(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - ok = gen_tcp:send(Socket, - "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n" - "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n" - "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n" - "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n" - "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"), - Data = pipeline_recv(Socket, <<>>), - Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]), - 5 = length(Reqs), - pipeline_check(Reqs). - -pipeline_check([]) -> - ok; -pipeline_check([Req|Tail]) -> - << "HTTP/1.1 200", _Rest/bits >> = Req, - pipeline_check(Tail). - -pipeline_recv(Socket, SoFar) -> - case gen_tcp:recv(Socket, 0, 6000) of - {ok, Data} -> - pipeline_recv(Socket, << SoFar/binary, Data/binary >>); - {error, closed} -> - ok = gen_tcp:close(Socket), - SoFar - end. - -raw_req(Packet, Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - ok = gen_tcp:send(Socket, Packet), - Res = case gen_tcp:recv(Socket, 0, 6000) of - {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>} -> - list_to_integer(binary_to_list(Str)); - {error, Reason} -> - Reason - end, - gen_tcp:close(Socket), - {Packet, Res}. - -%% Send a raw request. Return the response code and the full response. -raw_resp(Request, Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - Transport = case ?config(scheme, Config) of - "http" -> gen_tcp; - "https" -> ssl - end, - {ok, Socket} = Transport:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - ok = Transport:send(Socket, Request), - {StatusCode, Response} = case recv_loop(Transport, Socket, <<>>) of - {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >> = Bin} -> - {list_to_integer(binary_to_list(Str)), Bin}; - {ok, Bin} -> - {badresp, Bin}; - {error, Reason} -> - {Reason, <<>>} - end, - Transport:close(Socket), - {Response, StatusCode}. - -recv_loop(Transport, Socket, Acc) -> - case Transport:recv(Socket, 0, 6000) of - {ok, Data} -> - recv_loop(Transport, Socket, <<Acc/binary, Data/binary>>); - {error, closed} -> - ok = Transport:close(Socket), - {ok, Acc}; - {error, Reason} -> - {error, Reason} - end. - - - -raw(Config) -> - Huge = [$0 || _N <- lists:seq(1, 5000)], - Tests = [ - {"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200}, - {"\n", 400}, - {"Garbage\r\n\r\n", 400}, - {"\r\n\r\n\r\n\r\n\r\n\r\n", 400}, - {"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400}, - {"", closed}, - {"\r\n", closed}, - {"\r\n\r\n", closed}, - {"GET / HTTP/1.1", closed}, - {"GET / HTTP/1.1\r\n", 408}, - {"GET / HTTP/1.1\r\nHost: localhost", 408}, - {"GET / HTTP/1.1\r\nHost: localhost\r\n", 408}, - {"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408}, - {"GET http://proxy/ HTTP/1.1\r\n\r\n", 400}, - {"GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n", 200}, - {"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505}, - {"GET /init_shutdown HTTP/1.1\r\nHost: localhost\r\n\r\n", 666}, - {"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n\r\n", 102}, - {Huge, 413}, - {"GET / HTTP/1.1\r\n" ++ Huge, 413} - ], - [{Packet, StatusCode} = raw_req(Packet, Config) - || {Packet, StatusCode} <- Tests]. +nc_rand(Config) -> + nc_reqs(Config, "/dev/urandom"). -set_resp_header(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - ok = gen_tcp:send(Socket, "GET /set_resp/header HTTP/1.1\r\n" - "Host: localhost\r\nConnection: close\r\n\r\n"), - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - {_, _} = binary:match(Data, <<"Vary: Accept">>), - {_, _} = binary:match(Data, <<"Set-Cookie: ">>). +nc_zero(Config) -> + nc_reqs(Config, "/dev/zero"). -set_resp_overwrite(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - ok = gen_tcp:send(Socket, "GET /set_resp/overwrite HTTP/1.1\r\n" - "Host: localhost\r\nConnection: close\r\n\r\n"), - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - {_Start, _Length} = binary:match(Data, <<"Server: DesireDrive/1.0">>). +onrequest(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/", Config), Client), + {ok, 200, Headers, Client3} = cowboy_client:response(Client2), + {<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers), + {ok, <<"http_handler">>, _} = cowboy_client:response_body(Client3). -set_resp_body(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - ok = gen_tcp:send(Socket, "GET /set_resp/body HTTP/1.1\r\n" - "Host: localhost\r\nConnection: close\r\n\r\n"), - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - {_Start, _Length} = binary:match(Data, <<"\r\n\r\n" - "A flameless dance does not equal a cycle">>). - -response_as_req(Config) -> - Packet = -"HTTP/1.0 302 Found -Location: http://www.google.co.il/ -Cache-Control: private -Content-Type: text/html; charset=UTF-8 -Set-Cookie: PREF=ID=568f67013d4a7afa:FF=0:TM=1323014101:LM=1323014101:S=XqctDWC65MzKT0zC; expires=Tue, 03-Dec-2013 15:55:01 GMT; path=/; domain=.google.com -Date: Sun, 04 Dec 2011 15:55:01 GMT -Server: gws -Content-Length: 221 -X-XSS-Protection: 1; mode=block -X-Frame-Options: SAMEORIGIN +onrequest_reply(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/?reply=1", Config), Client), + {ok, 200, Headers, Client3} = cowboy_client:response(Client2), + {<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers), + {ok, <<"replied!">>, _} = cowboy_client:response_body(Client3). + +%% Hook for the above onrequest tests. +onrequest_hook(Req) -> + case cowboy_http_req:qs_val(<<"reply">>, Req) of + {undefined, Req2} -> + {ok, Req3} = cowboy_http_req:set_resp_header( + 'Server', <<"Serenity">>, Req2), + Req3; + {_, Req2} -> + {ok, Req3} = cowboy_http_req:reply( + 200, [], <<"replied!">>, Req2), + Req3 + end. -<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"> -<TITLE>302 Moved</TITLE></HEAD><BODY> -<H1>302 Moved</H1> -The document has moved -<A HREF=\"http://www.google.co.il/\">here</A>. -</BODY></HTML>", - {Packet, 400} = raw_req(Packet, Config). +onresponse_crash(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/handler_errors?case=init_before_reply", Config), Client), + {ok, 777, Headers, Client3} = cowboy_client:response(Client2), + {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers). + +onresponse_reply(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/", Config), Client), + {ok, 777, Headers, Client3} = cowboy_client:response(Client2), + {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers), + %% Make sure we don't get the body initially sent. + {error, closed} = cowboy_client:response_body(Client3). + +%% Hook for the above onresponse tests. +onresponse_hook(_, Headers, Req) -> + {ok, Req2} = cowboy_http_req:reply( + <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req), + Req2. -stream_body_set_resp(Config) -> - {Packet, 200} = raw_resp( - "GET /stream_body/set_resp HTTP/1.1\r\n" - "Host: localhost\r\nConnection: close\r\n\r\n", Config), - {_Start, _Length} = binary:match(Packet, <<"stream_body_set_resp">>). +pipeline(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/", Config), Client), + {ok, Client3} = cowboy_client:request(<<"GET">>, + build_url("/", Config), Client2), + {ok, Client4} = cowboy_client:request(<<"GET">>, + build_url("/", Config), Client3), + {ok, Client5} = cowboy_client:request(<<"GET">>, + build_url("/", Config), Client4), + {ok, Client6} = cowboy_client:request(<<"GET">>, + build_url("/", Config), [{<<"connection">>, <<"close">>}], Client5), + {ok, 200, _, Client7} = cowboy_client:response(Client6), + {ok, 200, _, Client8} = cowboy_client:response(Client7), + {ok, 200, _, Client9} = cowboy_client:response(Client8), + {ok, 200, _, Client10} = cowboy_client:response(Client9), + {ok, 200, _, Client11} = cowboy_client:response(Client10), + {error, closed} = cowboy_client:response(Client11). -static_mimetypes_function(Config) -> - TestURL = build_url("/static_mimetypes_function/test.html", Config), - {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} = - httpc:request(TestURL), - "text/html" = ?config("content-type", Headers1). +rest_keepalive(Config) -> + Client = ?config(client, Config), + URL = build_url("/simple", Config), + ok = rest_keepalive_loop(Client, URL, 10). -handler_errors(Config) -> - Request = fun(Case) -> - raw_resp(["GET /handler_errors?case=", Case, " HTTP/1.1\r\n", - "Host: localhost\r\n\r\n"], Config) end, +rest_keepalive_loop(_, _, 0) -> + ok; +rest_keepalive_loop(Client, URL, N) -> + Headers = [{<<"connection">>, <<"keep-alive">>}], + {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client), + {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2), + {<<"connection">>, <<"keep-alive">>} + = lists:keyfind(<<"connection">>, 1, RespHeaders), + rest_keepalive_loop(Client3, URL, N - 1). - {_Packet1, 500} = Request("init_before_reply"), +rest_keepalive_post(Config) -> + Client = ?config(client, Config), + ok = rest_keepalive_post_loop(Config, Client, forbidden_post, 10). - {Packet2, 200} = Request("init_after_reply"), - nomatch = binary:match(Packet2, <<"HTTP/1.1 500">>), +rest_keepalive_post_loop(_, _, _, 0) -> + ok; +rest_keepalive_post_loop(Config, Client, simple_post, N) -> + Headers = [ + {<<"connection">>, <<"keep-alive">>}, + {<<"content-type">>, <<"text/plain">>} + ], + {ok, Client2} = cowboy_client:request(<<"POST">>, + build_url("/simple_post", Config), Headers, "12345", Client), + {ok, 303, RespHeaders, Client3} = cowboy_client:response(Client2), + {<<"connection">>, <<"keep-alive">>} + = lists:keyfind(<<"connection">>, 1, RespHeaders), + rest_keepalive_post_loop(Config, Client3, forbidden_post, N - 1); +rest_keepalive_post_loop(Config, Client, forbidden_post, N) -> + Headers = [ + {<<"connection">>, <<"keep-alive">>}, + {<<"content-type">>, <<"text/plain">>} + ], + {ok, Client2} = cowboy_client:request(<<"POST">>, + build_url("/forbidden_post", Config), Headers, "12345", Client), + {ok, 403, RespHeaders, Client3} = cowboy_client:response(Client2), + {<<"connection">>, <<"keep-alive">>} + = lists:keyfind(<<"connection">>, 1, RespHeaders), + rest_keepalive_post_loop(Config, Client3, simple_post, N - 1). - {Packet3, 200} = Request("init_reply_handle_error"), - nomatch = binary:match(Packet3, <<"HTTP/1.1 500">>), +rest_nodelete(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"DELETE">>, + build_url("/nodelete", Config), Client), + {ok, 500, _, _} = cowboy_client:response(Client2). + +rest_resource_get_etag(Config, Type) -> + rest_resource_get_etag(Config, Type, []). + +rest_resource_get_etag(Config, Type, Headers) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/resetags?type=" ++ Type, Config), Headers, Client), + {ok, Status, RespHeaders, _} = cowboy_client:response(Client2), + case lists:keyfind(<<"etag">>, 1, RespHeaders) of + false -> {Status, false}; + {<<"etag">>, ETag} -> {Status, ETag} + end. - {_Packet4, 500} = Request("handle_before_reply"), +rest_resource_etags(Config) -> + Tests = [ + {200, <<"W/\"etag-header-value\"">>, "tuple-weak"}, + {200, <<"\"etag-header-value\"">>, "tuple-strong"}, + {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"}, + {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"}, + {500, false, "binary-strong-unquoted"}, + {500, false, "binary-weak-unquoted"} + ], + _ = [{Status, ETag, Type} = begin + {Ret, RespETag} = rest_resource_get_etag(Config, Type), + {Ret, RespETag, Type} + end || {Status, ETag, Type} <- Tests]. - {Packet5, 200} = Request("handle_after_reply"), - nomatch = binary:match(Packet5, <<"HTTP/1.1 500">>), +rest_resource_etags_if_none_match(Config) -> + Tests = [ + {304, <<"W/\"etag-header-value\"">>, "tuple-weak"}, + {304, <<"\"etag-header-value\"">>, "tuple-strong"}, + {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"}, + {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"} + ], + _ = [{Status, Type} = begin + {Ret, _} = rest_resource_get_etag(Config, Type, + [{<<"if-none-match">>, ETag}]), + {Ret, Type} + end || {Status, ETag, Type} <- Tests]. - {Packet6, 200} = raw_resp([ - "GET / HTTP/1.1\r\n", - "Host: localhost\r\n", - "Connection: keep-alive\r\n\r\n", - "GET /handler_errors?case=handle_after_reply\r\n", - "Host: localhost\r\n\r\n"], Config), - nomatch = binary:match(Packet6, <<"HTTP/1.1 500">>), +set_resp_body(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/set_resp/body", Config), Client), + {ok, 200, _, Client3} = cowboy_client:response(Client2), + {ok, <<"A flameless dance does not equal a cycle">>, _} + = cowboy_client:response_body(Client3). - {Packet7, 200} = raw_resp([ - "GET / HTTP/1.1\r\n", - "Host: localhost\r\n", - "Connection: keep-alive\r\n\r\n", - "GET /handler_errors?case=handle_before_reply HTTP/1.1\r\n", - "Host: localhost\r\n\r\n"], Config), - {{_, _}, _} = {binary:match(Packet7, <<"HTTP/1.1 500">>), Packet7}, +set_resp_header(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/set_resp/header", Config), Client), + {ok, 200, Headers, _} = cowboy_client:response(Client2), + {<<"vary">>, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers), + {<<"set-cookie">>, _} = lists:keyfind(<<"set-cookie">>, 1, Headers). - done. +set_resp_overwrite(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/set_resp/overwrite", Config), Client), + {ok, 200, Headers, _} = cowboy_client:response(Client2), + {<<"server">>, <<"DesireDrive/1.0">>} + = lists:keyfind(<<"server">>, 1, Headers). static_attribute_etag(Config) -> - TestURL = build_url("/static_attribute_etag/test.html", Config), - {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} = - httpc:request(TestURL), - false = ?config("etag", Headers1) =:= undefined, - {ok, {{"HTTP/1.1", 200, "OK"}, Headers2, "test.html\n"}} = - httpc:request(TestURL), - true = ?config("etag", Headers1) =:= ?config("etag", Headers2). + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/static_attribute_etag/test.html", Config), Client), + {ok, Client3} = cowboy_client:request(<<"GET">>, + build_url("/static_attribute_etag/test.html", Config), Client2), + {ok, 200, Headers1, Client4} = cowboy_client:response(Client3), + {ok, 200, Headers2, _} = cowboy_client:response(Client4), + {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1), + {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2), + false = ETag1 =:= undefined, + ETag1 = ETag2. static_function_etag(Config) -> - TestURL = build_url("/static_function_etag/test.html", Config), - {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} = - httpc:request(TestURL), - false = ?config("etag", Headers1) =:= undefined, - {ok, {{"HTTP/1.1", 200, "OK"}, Headers2, "test.html\n"}} = - httpc:request(TestURL), - true = ?config("etag", Headers1) =:= ?config("etag", Headers2). - + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/static_function_etag/test.html", Config), Client), + {ok, Client3} = cowboy_client:request(<<"GET">>, + build_url("/static_function_etag/test.html", Config), Client2), + {ok, 200, Headers1, Client4} = cowboy_client:response(Client3), + {ok, 200, Headers2, _} = cowboy_client:response(Client4), + {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1), + {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2), + false = ETag1 =:= undefined, + ETag1 = ETag2. + +%% Callback function for generating the ETag for the above test. static_function_etag(Arguments, etag_data) -> {_, Filepath} = lists:keyfind(filepath, 1, Arguments), {_, _Filesize} = lists:keyfind(filesize, 1, Arguments), @@ -533,260 +797,68 @@ static_function_etag(Arguments, etag_data) -> [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "), {strong, iolist_to_binary(Checksum)}. -te_identity(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - StrLen = integer_to_list(byte_size(Body)), - ok = gen_tcp:send(Socket, ["GET /echo/body HTTP/1.1\r\n" - "Host: localhost\r\nConnection: close\r\n" - "Content-Length: ", StrLen, "\r\n\r\n", Body]), - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - {_, _} = binary:match(Data, Body). +static_mimetypes_function(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/static_mimetypes_function/test.html", Config), Client), + {ok, 200, Headers, _} = cowboy_client:response(Client2), + {<<"content-type">>, <<"text/html">>} + = lists:keyfind(<<"content-type">>, 1, Headers). + +static_test_file(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/static/test_file", Config), Client), + {ok, 200, Headers, _} = cowboy_client:response(Client2), + {<<"content-type">>, <<"application/octet-stream">>} + = lists:keyfind(<<"content-type">>, 1, Headers). + +static_test_file_css(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/static/test_file.css", Config), Client), + {ok, 200, Headers, _} = cowboy_client:response(Client2), + {<<"content-type">>, <<"text/css">>} + = lists:keyfind(<<"content-type">>, 1, Headers). + +stream_body_set_resp(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/stream_body/set_resp", Config), Client), + {ok, 200, _, Client3} = cowboy_client:response(Client2), + {ok, <<"stream_body_set_resp">>, _} + = cowboy_client:response_body(Client3). te_chunked(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), + Client = ?config(client, Config), Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), Chunks = body_to_chunks(50, Body, []), - ok = gen_tcp:send(Socket, ["GET /echo/body HTTP/1.1\r\n" - "Host: localhost\r\nConnection: close\r\n" - "Transfer-Encoding: chunked\r\n\r\n", Chunks]), - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - {_, _} = binary:match(Data, Body). + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/echo/body", Config), + [{<<"transfer-encoding">>, <<"chunked">>}], + Chunks, Client), + {ok, 200, _, Client3} = cowboy_client:response(Client2), + {ok, Body, _} = cowboy_client:response_body(Client3). te_chunked_delayed(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), + Client = ?config(client, Config), Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), Chunks = body_to_chunks(50, Body, []), - ok = gen_tcp:send(Socket, ["GET /echo/body HTTP/1.1\r\n" - "Host: localhost\r\nConnection: close\r\n" - "Transfer-Encoding: chunked\r\n\r\n"]), - _ = [begin ok = gen_tcp:send(Socket, Chunk), ok = timer:sleep(10) end - || Chunk <- Chunks], - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - {_, _} = binary:match(Data, Body). + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/echo/body", Config), + [{<<"transfer-encoding">>, <<"chunked">>}], Client), + {ok, Transport, Socket} = cowboy_client:transport(Client2), + _ = [begin + ok = Transport:send(Socket, Chunk), + ok = timer:sleep(10) + end || Chunk <- Chunks], + {ok, 200, _, Client3} = cowboy_client:response(Client2), + {ok, Body, _} = cowboy_client:response_body(Client3). -body_to_chunks(_, <<>>, Acc) -> - lists:reverse([<<"0\r\n\r\n">>|Acc]); -body_to_chunks(ChunkSize, Body, Acc) -> - BodySize = byte_size(Body), - ChunkSize2 = case BodySize < ChunkSize of - true -> BodySize; - false -> ChunkSize - end, - << Chunk:ChunkSize2/binary, Rest/binary >> = Body, - ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)), - body_to_chunks(ChunkSize, Rest, - [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]). - -%% http and https. - -build_url(Path, Config) -> - {scheme, Scheme} = lists:keyfind(scheme, 1, Config), - {port, Port} = lists:keyfind(port, 1, Config), - Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path. - -http_200(Config) -> - {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} = - httpc:request(build_url("/", Config)). - -http_404(Config) -> - {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} = - httpc:request(build_url("/not/found", Config)). - -file_200(Config) -> - {ok, {{"HTTP/1.1", 200, "OK"}, Headers, "test_file\n"}} = - httpc:request(build_url("/static/test_file", Config)), - "application/octet-stream" = ?config("content-type", Headers), - - {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test_file.css\n"}} = - httpc:request(build_url("/static/test_file.css", Config)), - "text/css" = ?config("content-type", Headers1). - -file_403(Config) -> - {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} = - httpc:request(build_url("/static/test_noread", Config)). - -dir_403(Config) -> - {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} = - httpc:request(build_url("/static/test_dir", Config)), - {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} = - httpc:request(build_url("/static/test_dir/", Config)). - -file_404(Config) -> - {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} = - httpc:request(build_url("/static/not_found", Config)). - -file_400(Config) -> - {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers, _Body}} = - httpc:request(build_url("/static/%2f", Config)), - {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers1, _Body1}} = - httpc:request(build_url("/static/%2e", Config)), - {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers2, _Body2}} = - httpc:request(build_url("/static/%2e%2e", Config)). - -%% misc. - -http_10_hostless(Config) -> - Packet = "GET / HTTP/1.0\r\n\r\n", - {Packet, 200} = raw_req(Packet, Config). - -http_10_chunkless(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - Packet = "GET /chunked_response HTTP/1.0\r\nContent-Length: 0\r\n\r\n", - ok = gen_tcp:send(Socket, Packet), - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - nomatch = binary:match(Data, <<"Transfer-Encoding">>), - {_, _} = binary:match(Data, <<"chunked_handler\r\nworks fine!">>), - ok = gen_tcp:close(Socket). - -%% rest. - -rest_simple(Config) -> - Packet = "GET /simple HTTP/1.1\r\nHost: localhost\r\n\r\n", - {Packet, 200} = raw_req(Packet, Config). - -rest_keepalive(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - ok = rest_keepalive_loop(Socket, 100), - ok = gen_tcp:close(Socket). - -rest_keepalive_loop(_Socket, 0) -> - ok; -rest_keepalive_loop(Socket, N) -> - ok = gen_tcp:send(Socket, "GET /simple HTTP/1.1\r\n" - "Host: localhost\r\nConnection: keep-alive\r\n\r\n"), - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>), - nomatch = binary:match(Data, <<"Connection: close">>), - rest_keepalive_loop(Socket, N - 1). - -rest_keepalive_post(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - ok = rest_keepalive_post_loop(Socket, 10, forbidden_post), - ok = gen_tcp:close(Socket). - -rest_keepalive_post_loop(_Socket, 0, _) -> - ok; -rest_keepalive_post_loop(Socket, N, simple_post) -> - ok = gen_tcp:send(Socket, "POST /simple_post HTTP/1.1\r\n" - "Host: localhost\r\nConnection: keep-alive\r\n" - "Content-Length: 5\r\nContent-Type: text/plain\r\n\r\n12345"), - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - {0, 12} = binary:match(Data, <<"HTTP/1.1 303">>), - nomatch = binary:match(Data, <<"Connection: close">>), - rest_keepalive_post_loop(Socket, N - 1, forbidden_post); -rest_keepalive_post_loop(Socket, N, forbidden_post) -> - ok = gen_tcp:send(Socket, "POST /forbidden_post HTTP/1.1\r\n" - "Host: localhost\r\nConnection: keep-alive\r\n" - "Content-Length: 5\r\nContent-Type: text/plain\r\n\r\n12345"), - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - {0, 12} = binary:match(Data, <<"HTTP/1.1 403">>), - nomatch = binary:match(Data, <<"Connection: close">>), - rest_keepalive_post_loop(Socket, N - 1, simple_post). - -rest_nodelete(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - Request = "DELETE /nodelete HTTP/1.1\r\nHost: localhost\r\n\r\n", - ok = gen_tcp:send(Socket, Request), - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - {0, 12} = binary:match(Data, <<"HTTP/1.1 500">>), - ok = gen_tcp:close(Socket). - -rest_resource_etags(Config) -> - %% The Etag header should be set to the return value of generate_etag/2. - fun() -> - %% Correct return values from generate_etag/2. - {Packet1, 200} = raw_resp([ - "GET /resetags?type=tuple-weak HTTP/1.1\r\n", - "Host: localhost\r\n", "Connection: close\r\n", "\r\n"], Config), - {_,_} = binary:match(Packet1, <<"ETag: W/\"etag-header-value\"\r\n">>), - {Packet2, 200} = raw_resp([ - "GET /resetags?type=tuple-strong HTTP/1.1\r\n", - "Host: localhost\r\n", "Connection: close\r\n", "\r\n"], Config), - {_,_} = binary:match(Packet2, <<"ETag: \"etag-header-value\"\r\n">>), - %% Backwards compatible return values from generate_etag/2. - {Packet3, 200} = raw_resp([ - "GET /resetags?type=binary-weak-quoted HTTP/1.1\r\n", - "Host: localhost\r\n", "Connection: close\r\n", "\r\n"], Config), - {_,_} = binary:match(Packet3, <<"ETag: W/\"etag-header-value\"\r\n">>), - {Packet4, 200} = raw_resp([ - "GET /resetags?type=binary-strong-quoted HTTP/1.1\r\n", - "Host: localhost\r\n", "Connection: close\r\n", "\r\n"], Config), - {_,_} = binary:match(Packet4, <<"ETag: \"etag-header-value\"\r\n">>), - %% Invalid return values from generate_etag/2. - {_Packet5, 500} = raw_resp([ - "GET /resetags?type=binary-strong-unquoted HTTP/1.1\r\n", - "Host: localhost\r\n", "Connection: close\r\n", "\r\n"], Config), - {_Packet6, 500} = raw_resp([ - "GET /resetags?type=binary-weak-unquoted HTTP/1.1\r\n", - "Host: localhost\r\n", "Connection: close\r\n", "\r\n"], Config) - end(), - - %% The return value of generate_etag/2 should match the request header. - fun() -> - %% Correct return values from generate_etag/2. - {_Packet1, 304} = raw_resp([ - "GET /resetags?type=tuple-weak HTTP/1.1\r\n", - "Host: localhost\r\n", "Connection: close\r\n", - "If-None-Match: W/\"etag-header-value\"\r\n", "\r\n"], Config), - {_Packet2, 304} = raw_resp([ - "GET /resetags?type=tuple-strong HTTP/1.1\r\n", - "Host: localhost\r\n", "Connection: close\r\n", - "If-None-Match: \"etag-header-value\"\r\n", "\r\n"], Config), - %% Backwards compatible return values from generate_etag/2. - {_Packet3, 304} = raw_resp([ - "GET /resetags?type=binary-weak-quoted HTTP/1.1\r\n", - "Host: localhost\r\n", "Connection: close\r\n", - "If-None-Match: W/\"etag-header-value\"\r\n", "\r\n"], Config), - {_Packet4, 304} = raw_resp([ - "GET /resetags?type=binary-strong-quoted HTTP/1.1\r\n", - "Host: localhost\r\n", "Connection: close\r\n", - "If-None-Match: \"etag-header-value\"\r\n", "\r\n"], Config) - end(). - -onrequest(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"), - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - {_, _} = binary:match(Data, <<"Server: Serenity">>), - {_, _} = binary:match(Data, <<"http_handler">>), - gen_tcp:close(Socket). - -onrequest_reply(Config) -> - {port, Port} = lists:keyfind(port, 1, Config), - {ok, Socket} = gen_tcp:connect("localhost", Port, - [binary, {active, false}, {packet, raw}]), - ok = gen_tcp:send(Socket, "GET /?reply=1 HTTP/1.1\r\nHost: localhost\r\n\r\n"), - {ok, Data} = gen_tcp:recv(Socket, 0, 6000), - {_, _} = binary:match(Data, <<"Server: Cowboy">>), - nomatch = binary:match(Data, <<"http_handler">>), - {_, _} = binary:match(Data, <<"replied!">>), - gen_tcp:close(Socket). - -onrequest_hook(Req) -> - case cowboy_http_req:qs_val(<<"reply">>, Req) of - {undefined, Req2} -> - {ok, Req3} = cowboy_http_req:set_resp_header( - 'Server', <<"Serenity">>, Req2), - Req3; - {_, Req2} -> - {ok, Req3} = cowboy_http_req:reply( - 200, [], <<"replied!">>, Req2), - Req3 - end. +te_identity(Config) -> + Client = ?config(client, Config), + Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/echo/body", Config), [], Body, Client), + {ok, 200, _, Client3} = cowboy_client:response(Client2), + {ok, Body, _} = cowboy_client:response_body(Client3). diff --git a/test/http_handler_long_polling.erl b/test/http_handler_long_polling.erl index e838619..8d08265 100644 --- a/test/http_handler_long_polling.erl +++ b/test/http_handler_long_polling.erl @@ -6,7 +6,7 @@ init({_Transport, http}, Req, _Opts) -> erlang:send_after(500, self(), timeout), - {loop, Req, 9, 5000, hibernate}. + {loop, Req, 5, 5000, hibernate}. handle(_Req, _State) -> exit(badarg). diff --git a/test/http_handler_loop_timeout.erl b/test/http_handler_loop_timeout.erl new file mode 100644 index 0000000..76d6ea8 --- /dev/null +++ b/test/http_handler_loop_timeout.erl @@ -0,0 +1,15 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_handler_loop_timeout). +-export([init/3, info/3, terminate/2]). + +init({_, http}, Req, _) -> + erlang:send_after(1000, self(), error_timeout), + {loop, Req, undefined, 500, hibernate}. + +info(error_timeout, Req, State) -> + {ok, Req2} = cowboy_http_req:reply(500, Req), + {ok, Req2, State}. + +terminate(_, _) -> + ok. |