aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cowboy.app.src2
-rw-r--r--src/cowboy.erl18
-rw-r--r--src/cowboy_app.erl12
-rw-r--r--src/cowboy_bstr.erl16
-rw-r--r--src/cowboy_client.erl275
-rw-r--r--src/cowboy_clock.erl39
-rw-r--r--src/cowboy_handler.erl10
-rw-r--r--src/cowboy_http.erl255
-rw-r--r--src/cowboy_http_handler.erl19
-rw-r--r--src/cowboy_loop_handler.erl25
-rw-r--r--src/cowboy_middleware.erl13
-rw-r--r--src/cowboy_multipart.erl313
-rw-r--r--src/cowboy_protocol.erl113
-rw-r--r--src/cowboy_req.erl418
-rw-r--r--src/cowboy_rest.erl9
-rw-r--r--src/cowboy_router.erl49
-rw-r--r--src/cowboy_spdy.erl82
-rw-r--r--src/cowboy_static.erl19
-rw-r--r--src/cowboy_sub_protocol.erl16
-rw-r--r--src/cowboy_sup.erl16
-rw-r--r--src/cowboy_websocket.erl20
-rw-r--r--src/cowboy_websocket_handler.erl36
22 files changed, 252 insertions, 1523 deletions
diff --git a/src/cowboy.app.src b/src/cowboy.app.src
index e98eed3..39c69f1 100644
--- a/src/cowboy.app.src
+++ b/src/cowboy.app.src
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, 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
diff --git a/src/cowboy.erl b/src/cowboy.erl
index abc7911..2b50dfb 100644
--- a/src/cowboy.erl
+++ b/src/cowboy.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, 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
@@ -12,7 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Convenience API to start and stop HTTP/HTTPS listeners.
-module(cowboy).
-export([start_http/4]).
@@ -37,7 +36,6 @@
fun((http_status(), http_headers(), iodata(), Req) -> Req).
-export_type([onresponse_fun/0]).
-%% @doc Start an HTTP listener.
-spec start_http(ranch:ref(), non_neg_integer(), ranch_tcp:opts(),
cowboy_protocol:opts()) -> {ok, pid()} | {error, any()}.
start_http(Ref, NbAcceptors, TransOpts, ProtoOpts)
@@ -45,7 +43,6 @@ start_http(Ref, NbAcceptors, TransOpts, ProtoOpts)
ranch:start_listener(Ref, NbAcceptors,
ranch_tcp, TransOpts, cowboy_protocol, ProtoOpts).
-%% @doc Start an HTTPS listener.
-spec start_https(ranch:ref(), non_neg_integer(), ranch_ssl:opts(),
cowboy_protocol:opts()) -> {ok, pid()} | {error, any()}.
start_https(Ref, NbAcceptors, TransOpts, ProtoOpts)
@@ -53,7 +50,6 @@ start_https(Ref, NbAcceptors, TransOpts, ProtoOpts)
ranch:start_listener(Ref, NbAcceptors,
ranch_ssl, TransOpts, cowboy_protocol, ProtoOpts).
-%% @doc Start a SPDY listener.
-spec start_spdy(ranch:ref(), non_neg_integer(), ranch_ssl:opts(),
cowboy_spdy:opts()) -> {ok, pid()} | {error, any()}.
start_spdy(Ref, NbAcceptors, TransOpts, ProtoOpts)
@@ -66,20 +62,14 @@ start_spdy(Ref, NbAcceptors, TransOpts, ProtoOpts)
ranch:start_listener(Ref, NbAcceptors,
ranch_ssl, TransOpts2, cowboy_spdy, ProtoOpts).
-%% @doc Stop a listener.
--spec stop_listener(ranch:ref()) -> ok.
+-spec stop_listener(ranch:ref()) -> ok | {error, not_found}.
stop_listener(Ref) ->
ranch:stop_listener(Ref).
-%% @doc Convenience function for setting an environment value.
-%%
-%% Allows you to update live an environment value used by middlewares.
-%% This function is primarily intended to simplify updating the dispatch
-%% list used for routing.
-spec set_env(ranch:ref(), atom(), any()) -> ok.
set_env(Ref, Name, Value) ->
Opts = ranch:get_protocol_options(Ref),
{_, Env} = lists:keyfind(env, 1, Opts),
- Env2 = [{Name, Value}|lists:keydelete(Name, 1, Env)],
- Opts2 = lists:keyreplace(env, 1, Opts, {env, Env2}),
+ Opts2 = lists:keyreplace(env, 1, Opts,
+ {env, lists:keystore(Name, 1, Env, {Name, Value})}),
ok = ranch:set_protocol_options(Ref, Opts2).
diff --git a/src/cowboy_app.erl b/src/cowboy_app.erl
index b46ba1d..1161d91 100644
--- a/src/cowboy_app.erl
+++ b/src/cowboy_app.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, 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
@@ -12,18 +12,16 @@
%% 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_app).
-behaviour(application).
-%% API.
-export([start/2]).
-export([stop/1]).
-%% API.
-
-start(_Type, _Args) ->
+-spec start(_, _) -> {ok, pid()}.
+start(_, _) ->
cowboy_sup:start_link().
-stop(_State) ->
+-spec stop(_) -> ok.
+stop(_) ->
ok.
diff --git a/src/cowboy_bstr.erl b/src/cowboy_bstr.erl
index 0c1f66a..98d2cf7 100644
--- a/src/cowboy_bstr.erl
+++ b/src/cowboy_bstr.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, 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
@@ -12,7 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Binary string manipulation.
-module(cowboy_bstr).
%% Binary strings.
@@ -24,11 +23,9 @@
-export([char_to_lower/1]).
-export([char_to_upper/1]).
-%% @doc Capitalize a token.
-%%
%% The first letter and all letters after a dash are capitalized.
%% This is the form seen for header names in the HTTP/1.1 RFC and
-%% others. Note that using this form isn't required, as header name
+%% others. Note that using this form isn't required, as header names
%% are case insensitive, and it is only provided for use with eventual
%% badly implemented clients.
-spec capitalize_token(B) -> B when B::binary().
@@ -43,19 +40,14 @@ capitalize_token(<< C, Rest/bits >>, true, Acc) ->
capitalize_token(<< C, Rest/bits >>, false, Acc) ->
capitalize_token(Rest, false, << Acc/binary, (char_to_lower(C)) >>).
-%% @doc Convert a binary string to lowercase.
-spec to_lower(B) -> B when B::binary().
to_lower(B) ->
<< << (char_to_lower(C)) >> || << C >> <= B >>.
-%% @doc Convert a binary string to uppercase.
-spec to_upper(B) -> B when B::binary().
to_upper(B) ->
<< << (char_to_upper(C)) >> || << C >> <= B >>.
-%% @doc Convert [A-Z] characters to lowercase.
-%% @end
-%% We gain noticeable speed by matching each value directly.
-spec char_to_lower(char()) -> char().
char_to_lower($A) -> $a;
char_to_lower($B) -> $b;
@@ -85,7 +77,6 @@ char_to_lower($Y) -> $y;
char_to_lower($Z) -> $z;
char_to_lower(Ch) -> Ch.
-%% @doc Convert [a-z] characters to uppercase.
-spec char_to_upper(char()) -> char().
char_to_upper($a) -> $A;
char_to_upper($b) -> $B;
@@ -118,9 +109,7 @@ char_to_upper(Ch) -> Ch.
%% Tests.
-ifdef(TEST).
-
capitalize_token_test_() ->
- %% {Header, Result}
Tests = [
{<<"heLLo-woRld">>, <<"Hello-World">>},
{<<"Sec-Websocket-Version">>, <<"Sec-Websocket-Version">>},
@@ -131,5 +120,4 @@ capitalize_token_test_() ->
{<<"Sec-WebSocket---Version">>, <<"Sec-Websocket---Version">>}
],
[{H, fun() -> R = capitalize_token(H) end} || {H, R} <- Tests].
-
-endif.
diff --git a/src/cowboy_client.erl b/src/cowboy_client.erl
deleted file mode 100644
index 10aaa9c..0000000
--- a/src/cowboy_client.erl
+++ /dev/null
@@ -1,275 +0,0 @@
-%% Copyright (c) 2012-2013, 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 = 'HTTP/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 = atom_to_binary(Version, latin1),
- %% @todo do keepalive too, allow override...
- Headers2 = case lists:keyfind(<<"host">>, 1, Headers) of
- false -> [{<<"host">>, FullHost}|Headers];
- _ -> Headers
- end,
- Headers3 = [
- {<<"user-agent">>, <<"Cow">>}
- |Headers2],
- Headers4 = case iolist_size(Body) of
- 0 -> Headers3;
- Length -> [{<<"content-length">>, integer_to_list(Length)}|Headers3]
- end,
- HeadersData = [[Name, <<": ">>, Value, <<"\r\n">>]
- || {Name, Value} <- Headers4],
- Data = [Method, <<" ">>, Path, <<" ">>, VersionBin, <<"\r\n">>,
- HeadersData, <<"\r\n">>, Body],
- raw_request(Data, Client2).
-
-parse_url(<< "https://", Rest/binary >>) ->
- parse_url(Rest, ranch_ssl);
-parse_url(<< "http://", Rest/binary >>) ->
- parse_url(Rest, ranch_tcp);
-parse_url(URL) ->
- parse_url(URL, ranch_tcp).
-
-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 =:= ranch_tcp ->
- {binary_to_list(Host), 80};
- [Host] when Transport =:= ranch_ssl ->
- {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_version(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_version(Client, << "HTTP/1.1 ", Rest/binary >>) ->
- parse_status(Client, Rest, 'HTTP/1.1');
-parse_version(Client, << "HTTP/1.0 ", Rest/binary >>) ->
- parse_status(Client, Rest, 'HTTP/1.0').
-
-parse_status(Client, << S3, S2, S1, " ", StatusStr/binary >>, Version)
- when S3 >= $0, S3 =< $9, S2 >= $0, S2 =< $9, S1 >= $0, S1 =< $9 ->
- 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_clock.erl b/src/cowboy_clock.erl
index 6fb522b..2de3470 100644
--- a/src/cowboy_clock.erl
+++ b/src/cowboy_clock.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, 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
@@ -12,8 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Date and time related functions.
-%%
%% While a gen_server process runs in the background to update
%% the cache of formatted dates every second, all API calls are
%% local and directly read from the ETS cache table, providing
@@ -41,68 +39,63 @@
tref = undefined :: undefined | timer:tref()
}).
--define(SERVER, ?MODULE).
--define(TABLE, ?MODULE).
-
%% API.
-%% @private
-spec start_link() -> {ok, pid()}.
start_link() ->
- gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
-%% @private
-spec stop() -> stopped.
stop() ->
- gen_server:call(?SERVER, stop).
+ gen_server:call(?MODULE, stop).
-%% @doc Return the current date and time formatted according to RFC-1123.
-spec rfc1123() -> binary().
rfc1123() ->
- ets:lookup_element(?TABLE, rfc1123, 2).
+ ets:lookup_element(?MODULE, rfc1123, 2).
-%% @doc Return the given date and time formatted according to RFC-1123.
-spec rfc1123(calendar:datetime()) -> binary().
rfc1123(DateTime) ->
update_rfc1123(<<>>, undefined, DateTime).
%% gen_server.
-%% @private
+-spec init([]) -> {ok, #state{}}.
init([]) ->
- ?TABLE = ets:new(?TABLE, [set, protected,
+ ?MODULE = ets:new(?MODULE, [set, protected,
named_table, {read_concurrency, true}]),
T = erlang:universaltime(),
B = update_rfc1123(<<>>, undefined, T),
{ok, TRef} = timer:send_interval(1000, update),
- ets:insert(?TABLE, {rfc1123, B}),
+ ets:insert(?MODULE, {rfc1123, B}),
{ok, #state{universaltime=T, rfc1123=B, tref=TRef}}.
-%% @private
+-spec handle_call(any(), _, State)
+ -> {reply, ignored, State} | {stop, normal, stopped, State}
+ when State::#state{}.
handle_call(stop, _From, State=#state{tref=TRef}) ->
{ok, cancel} = timer:cancel(TRef),
{stop, normal, stopped, State};
handle_call(_Request, _From, State) ->
{reply, ignored, State}.
-%% @private
+-spec handle_cast(_, State) -> {noreply, State} when State::#state{}.
handle_cast(_Msg, State) ->
{noreply, State}.
-%% @private
+-spec handle_info(any(), State) -> {noreply, State} when State::#state{}.
handle_info(update, #state{universaltime=Prev, rfc1123=B1, tref=TRef}) ->
T = erlang:universaltime(),
B2 = update_rfc1123(B1, Prev, T),
- ets:insert(?TABLE, {rfc1123, B2}),
+ ets:insert(?MODULE, {rfc1123, B2}),
{noreply, #state{universaltime=T, rfc1123=B2, tref=TRef}};
handle_info(_Info, State) ->
{noreply, State}.
-%% @private
+-spec terminate(_, _) -> ok.
terminate(_Reason, _State) ->
ok.
-%% @private
+-spec code_change(_, State, _) -> {ok, State} when State::#state{}.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
@@ -174,7 +167,6 @@ month(12) -> <<"Dec">>.
%% Tests.
-ifdef(TEST).
-
update_rfc1123_test_() ->
Tests = [
{<<"Sat, 14 May 2011 14:25:33 GMT">>, undefined,
@@ -215,5 +207,4 @@ pad_int_test_() ->
{56, <<"56">>}, {57, <<"57">>}, {58, <<"58">>}, {59, <<"59">>}
],
[{I, fun() -> O = pad_int(I) end} || {I, O} <- Tests].
-
-endif.
diff --git a/src/cowboy_handler.erl b/src/cowboy_handler.erl
index e431ba8..7780e20 100644
--- a/src/cowboy_handler.erl
+++ b/src/cowboy_handler.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, 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
@@ -12,7 +12,7 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Handler middleware.
+%% Handler middleware.
%%
%% Execute the handler given by the <em>handler</em> and <em>handler_opts</em>
%% environment values. The result of this execution is added to the
@@ -27,8 +27,6 @@
%% by default. This can be configured through the <em>loop_max_buffer</em>
%% environment value. The request will be terminated with an
%% <em>{error, overflow}</em> reason if this threshold is reached.
-%%
-%% @see cowboy_http_handler
-module(cowboy_handler).
-behaviour(cowboy_middleware).
@@ -45,7 +43,6 @@
resp_sent = false :: boolean()
}).
-%% @private
-spec execute(Req, Env)
-> {ok, Req, Env} | {error, 500, Req}
| {suspend, ?MODULE, handler_loop, [any()]}
@@ -148,8 +145,6 @@ handler_after_callback(Req, State=#state{resp_sent=false}, Handler,
handler_after_callback(Req, State, Handler, HandlerState) ->
handler_before_loop(Req, State, Handler, HandlerState).
-%% We don't listen for Transport closes because that would force us
-%% to receive data and buffer it indefinitely.
-spec handler_before_loop(Req, #state{}, module(), any())
-> {ok, Req, cowboy_middleware:env()}
| {error, 500, Req} | {suspend, module(), atom(), [any()]}
@@ -177,7 +172,6 @@ handler_loop_timeout(State=#state{loop_timeout=Timeout,
TRef = erlang:start_timer(Timeout, self(), ?MODULE),
State#state{loop_timeout_ref=TRef}.
-%% @private
-spec handler_loop(Req, #state{}, module(), any())
-> {ok, Req, cowboy_middleware:env()}
| {error, 500, Req} | {suspend, module(), atom(), [any()]}
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl
index ea454d6..1cf73bf 100644
--- a/src/cowboy_http.erl
+++ b/src/cowboy_http.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]>
%% Copyright (c) 2011, Anthony Ramine <[email protected]>
%%
%% Permission to use, copy, modify, and/or distribute this software for any
@@ -13,7 +13,7 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Core HTTP parsing API.
+%% Deprecated HTTP parsing API.
-module(cowboy_http).
%% Parsing.
@@ -40,19 +40,10 @@
-export([parameterized_tokens/1]).
%% Decoding.
--export([te_chunked/2]).
--export([te_identity/2]).
-export([ce_identity/1]).
-%% Interpretation.
--export([urldecode/1]).
--export([urldecode/2]).
--export([urlencode/1]).
--export([urlencode/2]).
-
%% Parsing.
-%% @doc Parse a non-empty list of the given type.
-spec nonempty_list(binary(), fun()) -> [any(), ...] | {error, badarg}.
nonempty_list(Data, Fun) ->
case list(Data, Fun, []) of
@@ -61,7 +52,6 @@ nonempty_list(Data, Fun) ->
L -> lists:reverse(L)
end.
-%% @doc Parse a list of the given type.
-spec list(binary(), fun()) -> list() | {error, badarg}.
list(Data, Fun) ->
case list(Data, Fun, []) of
@@ -89,8 +79,6 @@ list(Data, Fun, Acc) ->
end)
end).
-%% @doc Parse a content type.
-%%
%% We lowercase the charset header as we know it's case insensitive.
-spec content_type(binary()) -> any().
content_type(Data) ->
@@ -112,7 +100,6 @@ content_type(Data) ->
end)
end).
-%% @doc Parse a media range.
-spec media_range(binary(), fun()) -> any().
media_range(Data, Fun) ->
media_type(Data,
@@ -155,7 +142,6 @@ media_range_param_value(Data, Fun, Type, SubType, Acc, Attr) ->
Type, SubType, [{Attr, Value}|Acc])
end).
-%% @doc Parse a media type.
-spec media_type(binary(), fun()) -> any().
media_type(Data, Fun) ->
token_ci(Data,
@@ -215,8 +201,6 @@ accept_ext_value(Data, Fun, Type, SubType, Params, Quality, Acc, Attr) ->
Type, SubType, Params, Quality, [{Attr, Value}|Acc])
end).
-%% @doc Parse a conneg header (Accept-Charset, Accept-Encoding),
-%% followed by an optional quality value.
-spec conneg(binary(), fun()) -> any().
conneg(Data, Fun) ->
token_ci(Data,
@@ -228,7 +212,6 @@ conneg(Data, Fun) ->
end)
end).
-%% @doc Parse a language range, followed by an optional quality value.
-spec language_range(binary(), fun()) -> any().
language_range(<< $*, Rest/binary >>, Fun) ->
language_range_ret(Rest, Fun, '*');
@@ -289,12 +272,10 @@ maybe_qparam(Data, Fun) ->
Fun(Rest, 1000)
end).
-%% @doc Parse a quality parameter string (for example q=0.500).
-spec qparam(binary(), fun()) -> any().
qparam(<< Q, $=, Data/binary >>, Fun) when Q =:= $q; Q =:= $Q ->
qvalue(Data, Fun).
-%% @doc Parse either a list of entity tags or a "*".
-spec entity_tag_match(binary()) -> any().
entity_tag_match(<< $*, Rest/binary >>) ->
whitespace(Rest,
@@ -304,7 +285,6 @@ entity_tag_match(<< $*, Rest/binary >>) ->
entity_tag_match(Data) ->
nonempty_list(Data, fun entity_tag/2).
-%% @doc Parse an entity-tag.
-spec entity_tag(binary(), fun()) -> any().
entity_tag(<< "W/", Rest/binary >>, Fun) ->
opaque_tag(Rest, Fun, weak);
@@ -318,7 +298,6 @@ opaque_tag(Data, Fun, Strength) ->
(Rest, OpaqueTag) -> Fun(Rest, {Strength, OpaqueTag})
end).
-%% @doc Parse an expectation.
-spec expectation(binary(), fun()) -> any().
expectation(Data, Fun) ->
token_ci(Data,
@@ -334,7 +313,6 @@ expectation(Data, Fun) ->
Fun(Rest, Expectation)
end).
-%% @doc Parse a list of parameters (a=b;c=d).
-spec params(binary(), fun()) -> any().
params(Data, Fun) ->
params(Data, Fun, []).
@@ -366,9 +344,6 @@ param(Data, Fun) ->
end)
end).
-%% @doc Parse an HTTP date (RFC1123, RFC850 or asctime date).
-%% @end
-%%
%% While this may not be the most efficient date parsing we can do,
%% it should work fine for our purposes because all HTTP dates should
%% be sent as RFC1123 dates in HTTP/1.1.
@@ -391,7 +366,6 @@ http_date(Data) ->
HTTPDate
end.
-%% @doc Parse an RFC1123 date.
-spec rfc1123_date(binary()) -> any().
rfc1123_date(Data) ->
wkday(Data,
@@ -411,7 +385,6 @@ rfc1123_date(Data) ->
{error, badarg}
end).
-%% @doc Parse an RFC850 date.
-spec rfc850_date(binary()) -> any().
%% From the RFC:
%% HTTP/1.1 clients and caches SHOULD assume that an RFC-850 date
@@ -435,7 +408,6 @@ rfc850_date(Data) ->
{error, badarg}
end).
-%% @doc Parse an asctime date.
-spec asctime_date(binary()) -> any().
asctime_date(Data) ->
wkday(Data,
@@ -594,7 +566,6 @@ time(<< H1, H2, ":", M1, M2, ":", S1, S2, Rest/binary >>, Fun)
{error, badarg}
end.
-%% @doc Skip whitespace.
-spec whitespace(binary(), fun()) -> any().
whitespace(<< C, Rest/binary >>, Fun)
when C =:= $\s; C =:= $\t ->
@@ -602,7 +573,6 @@ whitespace(<< C, Rest/binary >>, Fun)
whitespace(Data, Fun) ->
Fun(Data).
-%% @doc Parse a list of digits as a non negative integer.
-spec digits(binary()) -> non_neg_integer() | {error, badarg}.
digits(Data) ->
digits(Data,
@@ -629,8 +599,6 @@ digits(<< C, Rest/binary >>, Fun, Acc)
digits(Data, Fun, Acc) ->
Fun(Data, Acc).
-%% @doc Parse a list of case-insensitive alpha characters.
-%%
%% Changes all characters to lowercase.
-spec alpha(binary(), fun()) -> any().
alpha(Data, Fun) ->
@@ -673,14 +641,11 @@ word(Data, Fun) ->
(Rest, Token) -> Fun(Rest, Token)
end).
-%% @doc Parse a case-insensitive token.
-%%
%% Changes all characters to lowercase.
-spec token_ci(binary(), fun()) -> any().
token_ci(Data, Fun) ->
token(Data, Fun, ci, <<>>).
-%% @doc Parse a token.
-spec token(binary(), fun()) -> any().
token(Data, Fun) ->
token(Data, Fun, cs, <<>>).
@@ -701,7 +666,6 @@ token(<< C, Rest/binary >>, Fun, Case = ci, Acc) ->
token(<< C, Rest/binary >>, Fun, Case, Acc) ->
token(Rest, Fun, Case, << Acc/binary, C >>).
-%% @doc Parse a quoted string.
-spec quoted_string(binary(), fun()) -> any().
quoted_string(<< $", Rest/binary >>, Fun) ->
quoted_string(Rest, Fun, <<>>).
@@ -716,7 +680,6 @@ quoted_string(<< $\\, C, Rest/binary >>, Fun, Acc) ->
quoted_string(<< C, Rest/binary >>, Fun, Acc) ->
quoted_string(Rest, Fun, << Acc/binary, C >>).
-%% @doc Parse a quality value.
-spec qvalue(binary(), fun()) -> any().
qvalue(<< $0, $., Rest/binary >>, Fun) ->
qvalue(Rest, Fun, 0, 100);
@@ -745,8 +708,7 @@ qvalue(<< C, Rest/binary >>, Fun, Q, M)
qvalue(Data, Fun, Q, _M) ->
Fun(Data, Q).
-%% @doc Parse authorization value according rfc 2617.
-%% Only Basic authorization is supported so far.
+%% Only RFC2617 Basic authorization is supported so far.
-spec authorization(binary(), binary()) -> {binary(), any()} | {error, badarg}.
authorization(UserPass, Type = <<"basic">>) ->
whitespace(UserPass,
@@ -762,7 +724,6 @@ authorization(UserPass, Type = <<"basic">>) ->
authorization(String, Type) ->
whitespace(String, fun(Rest) -> {Type, Rest} end).
-%% @doc Parse user credentials.
-spec authorization_basic_userid(binary(), fun()) -> any().
authorization_basic_userid(Data, Fun) ->
authorization_basic_userid(Data, Fun, <<>>).
@@ -781,8 +742,6 @@ authorization_basic_userid(<<C, Rest/binary>>, Fun, Acc) ->
authorization_basic_password(Data, Fun) ->
authorization_basic_password(Data, Fun, <<>>).
-authorization_basic_password(<<>>, _Fun, <<>>) ->
- {error, badarg};
authorization_basic_password(<<C, _Rest/binary>>, _Fun, _Acc)
when C < 32; C=:= 127 ->
{error, badarg};
@@ -791,7 +750,6 @@ authorization_basic_password(<<>>, Fun, Acc) ->
authorization_basic_password(<<C, Rest/binary>>, Fun, Acc) ->
authorization_basic_password(Rest, Fun, <<Acc/binary, C>>).
-%% @doc Parse range header according rfc 2616.
-spec range(binary()) -> {Unit, [Range]} | {error, badarg} when
Unit :: binary(),
Range :: {non_neg_integer(), non_neg_integer() | infinity} | neg_integer().
@@ -849,7 +807,6 @@ range_digits(Data, Default, Fun) ->
Fun(Data, Default)
end).
-%% @doc Parse a non empty list of tokens followed with optional parameters.
-spec parameterized_tokens(binary()) -> any().
parameterized_tokens(Data) ->
nonempty_list(Data,
@@ -894,170 +851,15 @@ parameterized_tokens_param(Data, Fun) ->
%% Decoding.
-%% @doc Decode a stream of chunks.
--spec te_chunked(Bin, TransferState)
- -> more | {more, non_neg_integer(), Bin, TransferState}
- | {ok, Bin, Bin, TransferState}
- | {done, non_neg_integer(), Bin} | {error, badarg}
- when Bin::binary(), TransferState::{non_neg_integer(), non_neg_integer()}.
-te_chunked(<< "0\r\n\r\n", Rest/binary >>, {0, Streamed}) ->
- {done, Streamed, Rest};
-te_chunked(Data, {0, Streamed}) ->
- %% @todo We are expecting an hex size, not a general token.
- token(Data,
- fun (<< "\r\n", Rest/binary >>, BinLen) ->
- case list_to_integer(binary_to_list(BinLen), 16) of
- %% Final chunk is parsed in one go above. Rest would be
- %% <<\r\n">> if complete.
- 0 when byte_size(Rest) < 2 ->
- more;
- %% Normal chunk. Add 2 to Len for trailing <<"\r\n">>. Note
- %% that repeated <<"-2\r\n">> would be streamed, and
- %% accumulated, until out of memory if Len could be -2.
- Len when Len > 0 ->
- te_chunked(Rest, {Len + 2, Streamed})
- end;
- %% Chunk size shouldn't take too many bytes,
- %% don't try to stream forever.
- (Rest, _) when byte_size(Rest) < 16 ->
- more;
- (_, _) ->
- {error, badarg}
- end);
-%% <<"\n">> from trailing <<"\r\n">>.
-te_chunked(<< "\n", Rest/binary>>, {1, Streamed}) ->
- {ok, <<>>, Rest, {0, Streamed}};
-te_chunked(<<>>, State={1, _Streamed}) ->
- {more, 1, <<>>, State};
-%% Remainder of chunk (if any) and as much of trailing <<"\r\n">> as possible.
-te_chunked(Data, {ChunkRem, Streamed}) when byte_size(Data) >= ChunkRem - 2 ->
- ChunkSize = ChunkRem - 2,
- Streamed2 = Streamed + ChunkSize,
- case Data of
- << Chunk:ChunkSize/binary, "\r\n", Rest/binary >> ->
- {ok, Chunk, Rest, {0, Streamed2}};
- << Chunk:ChunkSize/binary, "\r" >> ->
- {more, 1, Chunk, {1, Streamed2}};
- << Chunk:ChunkSize/binary >> ->
- {more, 2, Chunk, {2, Streamed2}}
- end;
-%% Incomplete chunk.
-te_chunked(Data, {ChunkRem, Streamed}) ->
- ChunkRem2 = ChunkRem - byte_size(Data),
- Streamed2 = Streamed + byte_size(Data),
- {more, ChunkRem2, Data, {ChunkRem2, Streamed2}}.
-
-%% @doc Decode an identity stream.
--spec te_identity(Bin, TransferState)
- -> {more, non_neg_integer(), Bin, TransferState}
- | {done, Bin, non_neg_integer(), Bin}
- when Bin::binary(), TransferState::{non_neg_integer(), non_neg_integer()}.
-te_identity(Data, {Streamed, Total})
- when Streamed + byte_size(Data) < Total ->
- Streamed2 = Streamed + byte_size(Data),
- {more, Total - Streamed2, Data, {Streamed2, Total}};
-te_identity(Data, {Streamed, Total}) ->
- Size = Total - Streamed,
- << Data2:Size/binary, Rest/binary >> = Data,
- {done, Data2, Total, Rest}.
-
-%% @doc Decode an identity content.
+%% @todo Move this to cowlib too I suppose. :-)
-spec ce_identity(binary()) -> {ok, binary()}.
ce_identity(Data) ->
{ok, Data}.
-%% Interpretation.
-
-%% @doc Decode a URL encoded binary.
-%% @equiv urldecode(Bin, crash)
--spec urldecode(binary()) -> binary().
-urldecode(Bin) when is_binary(Bin) ->
- urldecode(Bin, <<>>, crash).
-
-%% @doc Decode a URL encoded binary.
-%% The second argument specifies how to handle percent characters that are not
-%% followed by two valid hex characters. Use `skip' to ignore such errors,
-%% if `crash' is used the function will fail with the reason `badarg'.
--spec urldecode(binary(), crash | skip) -> binary().
-urldecode(Bin, OnError) when is_binary(Bin) ->
- urldecode(Bin, <<>>, OnError).
-
--spec urldecode(binary(), binary(), crash | skip) -> binary().
-urldecode(<<$%, H, L, Rest/binary>>, Acc, OnError) ->
- G = unhex(H),
- M = unhex(L),
- if G =:= error; M =:= error ->
- case OnError of skip -> ok; crash -> erlang:error(badarg) end,
- urldecode(<<H, L, Rest/binary>>, <<Acc/binary, $%>>, OnError);
- true ->
- urldecode(Rest, <<Acc/binary, (G bsl 4 bor M)>>, OnError)
- end;
-urldecode(<<$%, Rest/binary>>, Acc, OnError) ->
- case OnError of skip -> ok; crash -> erlang:error(badarg) end,
- urldecode(Rest, <<Acc/binary, $%>>, OnError);
-urldecode(<<$+, Rest/binary>>, Acc, OnError) ->
- urldecode(Rest, <<Acc/binary, $ >>, OnError);
-urldecode(<<C, Rest/binary>>, Acc, OnError) ->
- urldecode(Rest, <<Acc/binary, C>>, OnError);
-urldecode(<<>>, Acc, _OnError) ->
- Acc.
-
--spec unhex(byte()) -> byte() | error.
-unhex(C) when C >= $0, C =< $9 -> C - $0;
-unhex(C) when C >= $A, C =< $F -> C - $A + 10;
-unhex(C) when C >= $a, C =< $f -> C - $a + 10;
-unhex(_) -> error.
-
-
-%% @doc URL encode a string binary.
-%% @equiv urlencode(Bin, [])
--spec urlencode(binary()) -> binary().
-urlencode(Bin) ->
- urlencode(Bin, []).
-
-%% @doc URL encode a string binary.
-%% The `noplus' option disables the default behaviour of quoting space
-%% characters, `\s', as `+'. The `upper' option overrides the default behaviour
-%% of writing hex numbers using lowecase letters to using uppercase letters
-%% instead.
--spec urlencode(binary(), [noplus|upper]) -> binary().
-urlencode(Bin, Opts) ->
- Plus = not lists:member(noplus, Opts),
- Upper = lists:member(upper, Opts),
- urlencode(Bin, <<>>, Plus, Upper).
-
--spec urlencode(binary(), binary(), boolean(), boolean()) -> binary().
-urlencode(<<C, Rest/binary>>, Acc, P=Plus, U=Upper) ->
- if C >= $0, C =< $9 -> urlencode(Rest, <<Acc/binary, C>>, P, U);
- C >= $A, C =< $Z -> urlencode(Rest, <<Acc/binary, C>>, P, U);
- C >= $a, C =< $z -> urlencode(Rest, <<Acc/binary, C>>, P, U);
- C =:= $.; C =:= $-; C =:= $~; C =:= $_ ->
- urlencode(Rest, <<Acc/binary, C>>, P, U);
- C =:= $ , Plus ->
- urlencode(Rest, <<Acc/binary, $+>>, P, U);
- true ->
- H = C band 16#F0 bsr 4, L = C band 16#0F,
- H1 = if Upper -> tohexu(H); true -> tohexl(H) end,
- L1 = if Upper -> tohexu(L); true -> tohexl(L) end,
- urlencode(Rest, <<Acc/binary, $%, H1, L1>>, P, U)
- end;
-urlencode(<<>>, Acc, _Plus, _Upper) ->
- Acc.
-
--spec tohexu(byte()) -> byte().
-tohexu(C) when C < 10 -> $0 + C;
-tohexu(C) when C < 16 -> $A + C - 10.
-
--spec tohexl(byte()) -> byte().
-tohexl(C) when C < 10 -> $0 + C;
-tohexl(C) when C < 16 -> $a + C - 10.
-
%% Tests.
-ifdef(TEST).
-
nonempty_charset_list_test_() ->
- %% {Value, Result}
Tests = [
{<<>>, {error, badarg}},
{<<"iso-8859-5, unicode-1-1;q=0.8">>, [
@@ -1074,7 +876,6 @@ nonempty_charset_list_test_() ->
[{V, fun() -> R = nonempty_list(V, fun conneg/2) end} || {V, R} <- Tests].
nonempty_language_range_list_test_() ->
- %% {Value, Result}
Tests = [
{<<"da, en-gb;q=0.8, en;q=0.7">>, [
{<<"da">>, 1000},
@@ -1094,7 +895,6 @@ nonempty_language_range_list_test_() ->
|| {V, R} <- Tests].
nonempty_token_list_test_() ->
- %% {Value, Result}
Tests = [
{<<>>, {error, badarg}},
{<<" ">>, {error, badarg}},
@@ -1110,7 +910,6 @@ nonempty_token_list_test_() ->
[{V, fun() -> R = nonempty_list(V, fun token/2) end} || {V, R} <- Tests].
media_range_list_test_() ->
- %% {Tokens, Result}
Tests = [
{<<"audio/*; q=0.2, audio/basic">>, [
{{<<"audio">>, <<"*">>, []}, 200, []},
@@ -1155,7 +954,6 @@ media_range_list_test_() ->
[{V, fun() -> R = list(V, fun media_range/2) end} || {V, R} <- Tests].
entity_tag_match_test_() ->
- %% {Tokens, Result}
Tests = [
{<<"\"xyzzy\"">>, [{strong, <<"xyzzy">>}]},
{<<"\"xyzzy\", W/\"r2d2xxxx\", \"c3piozzzz\"">>,
@@ -1167,7 +965,6 @@ entity_tag_match_test_() ->
[{V, fun() -> R = entity_tag_match(V) end} || {V, R} <- Tests].
http_date_test_() ->
- %% {Tokens, Result}
Tests = [
{<<"Sun, 06 Nov 1994 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}},
{<<"Sunday, 06-Nov-94 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}},
@@ -1176,28 +973,24 @@ http_date_test_() ->
[{V, fun() -> R = http_date(V) end} || {V, R} <- Tests].
rfc1123_date_test_() ->
- %% {Tokens, Result}
Tests = [
{<<"Sun, 06 Nov 1994 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}}
],
[{V, fun() -> R = rfc1123_date(V) end} || {V, R} <- Tests].
rfc850_date_test_() ->
- %% {Tokens, Result}
Tests = [
{<<"Sunday, 06-Nov-94 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}}
],
[{V, fun() -> R = rfc850_date(V) end} || {V, R} <- Tests].
asctime_date_test_() ->
- %% {Tokens, Result}
Tests = [
{<<"Sun Nov 6 08:49:37 1994">>, {{1994, 11, 6}, {8, 49, 37}}}
],
[{V, fun() -> R = asctime_date(V) end} || {V, R} <- Tests].
content_type_test_() ->
- %% {ContentType, Result}
Tests = [
{<<"text/plain; charset=iso-8859-4">>,
{<<"text">>, <<"plain">>, [{<<"charset">>, <<"iso-8859-4">>}]}},
@@ -1214,7 +1007,6 @@ content_type_test_() ->
[{V, fun () -> R = content_type(V) end} || {V, R} <- Tests].
parameterized_tokens_test_() ->
- %% {ParameterizedTokens, Result}
Tests = [
{<<"foo">>, [{<<"foo">>, []}]},
{<<"bar; baz=2">>, [{<<"bar">>, [{<<"baz">>, <<"2">>}]}]},
@@ -1225,7 +1017,6 @@ parameterized_tokens_test_() ->
[{V, fun () -> R = parameterized_tokens(V) end} || {V, R} <- Tests].
digits_test_() ->
- %% {Digits, Result}
Tests = [
{<<"42 ">>, 42},
{<<"69\t">>, 69},
@@ -1233,45 +1024,12 @@ digits_test_() ->
],
[{V, fun() -> R = digits(V) end} || {V, R} <- Tests].
-urldecode_test_() ->
- F = fun(Qs, O) ->
- try urldecode(Qs, O) of
- R ->
- {ok, R}
- catch _:E ->
- {error, E}
- end
- end,
- Tests = [
- {<<"%20">>, crash, {ok, <<" ">>}},
- {<<"+">>, crash, {ok, <<" ">>}},
- {<<"%00">>, crash, {ok, <<0>>}},
- {<<"%fF">>, crash, {ok, <<255>>}},
- {<<"123">>, crash, {ok, <<"123">>}},
- {<<"%i5">>, skip, {ok, <<"%i5">>}},
- {<<"%5">>, skip, {ok, <<"%5">>}},
- {<<"%i5">>, crash, {error, badarg}},
- {<<"%5">>, crash, {error, badarg}}
- ],
- [{Qs, fun() -> R = F(Qs,O) end} || {Qs, O, R} <- Tests].
-
-urlencode_test_() ->
- Tests = [
- {<<255,0>>, [], <<"%ff%00">>},
- {<<255,0>>, [upper], <<"%FF%00">>},
- {<<" ">>, [], <<"+">>},
- {<<" ">>, [noplus], <<"%20">>},
- {<<"aBc">>, [], <<"aBc">>},
- {<<".-~_">>, [], <<".-~_">>}
- ],
- Tests2 = [{<<255, " ">>,<<"%ff+">>}],
- [{V, fun() -> R = urlencode(V, O) end} || {V, O, R} <- Tests] ++
- [{V, fun() -> R = urlencode(V) end} || {V, R} <- Tests2].
-
http_authorization_test_() ->
Tests = [
{<<"basic">>, <<"QWxsYWRpbjpvcGVuIHNlc2FtZQ==">>,
{<<"basic">>, {<<"Alladin">>, <<"open sesame">>}}},
+ {<<"basic">>, <<"dXNlcm5hbWU6">>,
+ {<<"basic">>, {<<"username">>, <<>>}}},
{<<"basic">>, <<"dXNlcm5hbWUK">>,
{error, badarg}},
{<<"basic">>, <<"_[]@#$%^&*()-AA==">>,
@@ -1305,5 +1063,4 @@ http_range_test_() ->
{error, badarg}}
],
[fun() -> R = range(V) end ||{V, R} <- Tests].
-
-endif.
diff --git a/src/cowboy_http_handler.erl b/src/cowboy_http_handler.erl
index 3ad8f88..14c7987 100644
--- a/src/cowboy_http_handler.erl
+++ b/src/cowboy_http_handler.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, 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
@@ -12,23 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Behaviour for short-lived HTTP handlers.
-%%
-%% <em>init/3</em> allows you to initialize a state for all subsequent
-%% callbacks, and indicate to Cowboy whether you accept to handle the
-%% request or want to shutdown without handling it, in which case the
-%% <em>handle/2</em> call will simply be skipped.
-%%
-%% <em>handle/2</em> allows you to handle the request. It receives the
-%% state previously defined.
-%%
-%% <em>terminate/3</em> allows you to clean up. It receives the
-%% termination reason and the state previously defined.
-%%
-%% There is no required operation to perform in any of these callbacks
-%% other than returning the proper values. Make sure you always return
-%% the last modified Req so that Cowboy has the up to date information
-%% about the request.
-module(cowboy_http_handler).
-type opts() :: any().
diff --git a/src/cowboy_loop_handler.erl b/src/cowboy_loop_handler.erl
index af49e57..edef77f 100644
--- a/src/cowboy_loop_handler.erl
+++ b/src/cowboy_loop_handler.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, 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
@@ -12,29 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Behaviour for long-lived HTTP handlers.
-%%
-%% <em>init/3</em> allows you to initialize a state for all subsequent
-%% callbacks, and indicate to Cowboy whether you accept to handle the
-%% request or want to shutdown without handling it, in which case the
-%% receive loop and <em>info/3</em> calls will simply be skipped.
-%%
-%% <em>info/3</em> allows you to handle the messages this process will
-%% receive. It receives the message and the state previously defined.
-%% It can decide to stop the receive loop or continue receiving.
-%%
-%% <em>terminate/3</em> allows you to clean up. It receives the
-%% termination reason and the state previously defined.
-%%
-%% There is no required operation to perform in any of these callbacks
-%% other than returning the proper values. Make sure you always return
-%% the last modified Req so that Cowboy has the up to date information
-%% about the request.
-%%
-%% It is recommended to use hibernate if this process is not going to
-%% receive a lot of messages. It is also recommended to use a timeout
-%% value so that the connection gets closed after a long period of
-%% inactivity.
-module(cowboy_loop_handler).
-type opts() :: any().
diff --git a/src/cowboy_middleware.erl b/src/cowboy_middleware.erl
index 40c9407..fa0f5bc 100644
--- a/src/cowboy_middleware.erl
+++ b/src/cowboy_middleware.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2013-2014, 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
@@ -12,17 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Behaviour for middlewares.
-%%
-%% Only one function needs to be implemented, <em>execute/2</em>.
-%% It receives the Req and the environment and returns them
-%% optionally modified. It can decide to stop the processing with
-%% or without an error. It is also possible to hibernate the process
-%% if needed.
-%%
-%% A middleware can perform any operation. Make sure you always return
-%% the last modified Req so that Cowboy has the up to date information
-%% about the request.
-module(cowboy_middleware).
-type env() :: [{atom(), any()}].
diff --git a/src/cowboy_multipart.erl b/src/cowboy_multipart.erl
deleted file mode 100644
index 4df5a27..0000000
--- a/src/cowboy_multipart.erl
+++ /dev/null
@@ -1,313 +0,0 @@
-%% Copyright (c) 2011, Anthony Ramine <[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.
-
-%% @doc Multipart parser.
--module(cowboy_multipart).
-
--export([parser/1]).
--export([content_disposition/1]).
-
--type part_parser() :: parser(more(part_result())).
--type parser(T) :: fun((binary()) -> T).
--type more(T) :: T | {more, parser(T)}.
--type part_result() :: headers() | eof.
--type headers() :: {headers, http_headers(), body_cont()}.
--type http_headers() :: [{binary(), binary()}].
--type body_cont() :: cont(more(body_result())).
--type cont(T) :: fun(() -> T).
--type body_result() :: {body, binary(), body_cont()} | end_of_part().
--type end_of_part() :: {end_of_part, cont(more(part_result()))}.
--type disposition() :: {binary(), [{binary(), binary()}]}.
-
-%% API.
-
-%% @doc Return a multipart parser for the given boundary.
--spec parser(binary()) -> part_parser().
-parser(Boundary) when is_binary(Boundary) ->
- fun (Bin) when is_binary(Bin) -> parse(Bin, Boundary) end.
-
-%% @doc Parse a content disposition.
-%% @todo Parse the MIME header instead of the HTTP one.
--spec content_disposition(binary()) -> disposition().
-content_disposition(Data) ->
- cowboy_http:token_ci(Data,
- fun (_Rest, <<>>) -> {error, badarg};
- (Rest, Disposition) ->
- cowboy_http:params(Rest,
- fun (<<>>, Params) -> {Disposition, Params};
- (_Rest2, _) -> {error, badarg}
- end)
- end).
-
-%% Internal.
-
-%% @doc Entry point of the multipart parser, skips over the preamble if any.
--spec parse(binary(), binary()) -> more(part_result()).
-parse(Bin, Boundary) when byte_size(Bin) >= byte_size(Boundary) + 2 ->
- BoundarySize = byte_size(Boundary),
- Pattern = pattern(Boundary),
- case Bin of
- <<"--", Boundary:BoundarySize/binary, Rest/binary>> ->
- % Data starts with initial boundary, skip preamble parsing.
- parse_boundary_tail(Rest, Pattern);
- _ ->
- % Parse preamble.
- skip(Bin, Pattern)
- end;
-parse(Bin, Boundary) ->
- % Not enough data to know if the data begins with a boundary.
- more(Bin, fun (NewBin) -> parse(NewBin, Boundary) end).
-
--type pattern() :: {binary:cp(), non_neg_integer()}.
--type patterns() :: {pattern(), pattern()}.
-
-%% @doc Return two compiled binary patterns with their sizes in bytes.
-%% The boundary pattern is the boundary prepended with "\r\n--".
-%% The boundary suffix pattern matches all prefixes of the boundary.
--spec pattern(binary()) -> patterns().
-pattern(Boundary) ->
- MatchPattern = <<"\r\n--", Boundary/binary>>,
- MatchPrefixes = prefixes(MatchPattern),
- {{binary:compile_pattern(MatchPattern), byte_size(MatchPattern)},
- {binary:compile_pattern(MatchPrefixes), byte_size(MatchPattern)}}.
-
-%% @doc Return all prefixes of a binary string.
-%% The list of prefixes includes the full string.
--spec prefixes(binary()) -> [binary()].
-prefixes(<<C, Rest/binary>>) ->
- prefixes(Rest, <<C>>).
-
--spec prefixes(binary(), binary()) -> [binary()].
-prefixes(<<C, Rest/binary>>, Acc) ->
- [Acc|prefixes(Rest, <<Acc/binary, C>>)];
-prefixes(<<>>, Acc) ->
- [Acc].
-
-%% @doc Test if a boundary is a possble suffix.
-%% The patterns are expected to have been returned from `pattern/1'.
--spec suffix_match(binary(), patterns()) -> nomatch | {integer(), integer()}.
-suffix_match(Bin, {_Boundary, {Pat, Len}}) ->
- Size = byte_size(Bin),
- suffix_match(Bin, Pat, Size, max(-Size, -Len)).
-
--spec suffix_match(binary(), binary:cp(), non_neg_integer(), 0|neg_integer()) ->
- nomatch | {integer(), integer()}.
-suffix_match(_Bin, _Pat, _Size, _Match=0) ->
- nomatch;
-suffix_match(Bin, Pat, Size, Match) when Match < 0 ->
- case binary:match(Bin, Pat, [{scope, {Size, Match}}]) of
- {Pos, Len}=Part when Pos + Len =:= Size -> Part;
- {_, Len} -> suffix_match(Bin, Pat, Size, Match + Len);
- nomatch -> nomatch
- end.
-
-%% @doc Parse remaining characters of a line beginning with the boundary.
-%% If followed by "--", <em>eof</em> is returned and parsing is finished.
--spec parse_boundary_tail(binary(), patterns()) -> more(part_result()).
-parse_boundary_tail(Bin, Pattern) when byte_size(Bin) >= 2 ->
- case Bin of
- <<"--", _Rest/binary>> ->
- % Boundary is followed by "--", end parsing.
- eof;
- _ ->
- % No dash after boundary, proceed with unknown chars and lwsp
- % removal.
- parse_boundary_eol(Bin, Pattern)
- end;
-parse_boundary_tail(Bin, Pattern) ->
- % Boundary may be followed by "--", need more data.
- more(Bin, fun (NewBin) -> parse_boundary_tail(NewBin, Pattern) end).
-
-%% @doc Skip whitespace and unknown chars until CRLF.
--spec parse_boundary_eol(binary(), patterns()) -> more(part_result()).
-parse_boundary_eol(Bin, Pattern) ->
- case binary:match(Bin, <<"\r\n">>) of
- {CrlfStart, _Length} ->
- % End of line found, remove optional whitespace.
- <<_:CrlfStart/binary, Rest/binary>> = Bin,
- Fun = fun (Rest2) -> parse_boundary_crlf(Rest2, Pattern) end,
- cowboy_http:whitespace(Rest, Fun);
- nomatch ->
- % CRLF not found in the given binary.
- RestStart = max(byte_size(Bin) - 1, 0),
- <<_:RestStart/binary, Rest/binary>> = Bin,
- more(Rest, fun (NewBin) -> parse_boundary_eol(NewBin, Pattern) end)
- end.
-
--spec parse_boundary_crlf(binary(), patterns()) -> more(part_result()).
-parse_boundary_crlf(<<"\r\n", Rest/binary>>, Pattern) ->
- % The binary is at least 2 bytes long as this function is only called by
- % parse_boundary_eol/3 when CRLF has been found so a more tuple will never
- % be returned from here.
- parse_headers(Rest, Pattern);
-parse_boundary_crlf(Bin, Pattern) ->
- % Unspecified behaviour here: RFC 2046 doesn't say what to do when LWSP is
- % not followed directly by a new line. In this implementation it is
- % considered part of the boundary so EOL needs to be searched again.
- parse_boundary_eol(Bin, Pattern).
-
--spec parse_headers(binary(), patterns()) -> more(part_result()).
-parse_headers(Bin, Pattern) ->
- parse_headers(Bin, Pattern, []).
-
--spec parse_headers(binary(), patterns(), http_headers()) -> more(part_result()).
-parse_headers(Bin, Pattern, Acc) ->
- case erlang:decode_packet(httph_bin, Bin, []) of
- {ok, {http_header, _, Name, _, Value}, Rest} ->
- Name2 = case is_atom(Name) of
- true -> cowboy_bstr:to_lower(atom_to_binary(Name, latin1));
- false -> cowboy_bstr:to_lower(Name)
- end,
- parse_headers(Rest, Pattern, [{Name2, Value} | Acc]);
- {ok, http_eoh, Rest} ->
- Headers = lists:reverse(Acc),
- {headers, Headers, fun () -> parse_body(Rest, Pattern) end};
- {ok, {http_error, _}, _} ->
- % Skip malformed parts.
- skip(Bin, Pattern);
- {more, _} ->
- more(Bin, fun (NewBin) -> parse_headers(NewBin, Pattern, Acc) end)
- end.
-
--spec parse_body(binary(), patterns()) -> more(body_result()).
-parse_body(Bin, Pattern = {{P, PSize}, _}) when byte_size(Bin) >= PSize ->
- case binary:match(Bin, P) of
- {0, _Length} ->
- <<_:PSize/binary, Rest/binary>> = Bin,
- end_of_part(Rest, Pattern);
- {BoundaryStart, _Length} ->
- % Boundary found, this is the latest partial body that will be
- % returned for this part.
- <<PBody:BoundaryStart/binary, _:PSize/binary, Rest/binary>> = Bin,
- FResult = end_of_part(Rest, Pattern),
- {body, PBody, fun () -> FResult end};
- nomatch ->
- case suffix_match(Bin, Pattern) of
- nomatch ->
- %% Prefix of boundary not found at end of input. it's
- %% safe to return the whole binary. Saves copying of
- %% next input onto tail of current input binary.
- {body, Bin, fun () -> parse_body(<<>>, Pattern) end};
- {BoundaryStart, Len} ->
- PBody = binary:part(Bin, 0, BoundaryStart),
- Rest = binary:part(Bin, BoundaryStart, Len),
- {body, PBody, fun () -> parse_body(Rest, Pattern) end}
- end
- end;
-parse_body(Bin, Pattern) ->
- more(Bin, fun (NewBin) -> parse_body(NewBin, Pattern) end).
-
--spec end_of_part(binary(), patterns()) -> end_of_part().
-end_of_part(Bin, Pattern) ->
- {end_of_part, fun () -> parse_boundary_tail(Bin, Pattern) end}.
-
--spec skip(binary(), patterns()) -> more(part_result()).
-skip(Bin, Pattern = {{P, PSize}, _}) ->
- case binary:match(Bin, P) of
- {BoundaryStart, _Length} ->
- % Boundary found, proceed with parsing of the next part.
- RestStart = BoundaryStart + PSize,
- <<_:RestStart/binary, Rest/binary>> = Bin,
- parse_boundary_tail(Rest, Pattern);
- nomatch ->
- % Boundary not found, need more data.
- RestStart = max(byte_size(Bin) - PSize + 1, 0),
- <<_:RestStart/binary, Rest/binary>> = Bin,
- more(Rest, fun (NewBin) -> skip(NewBin, Pattern) end)
- end.
-
--spec more(binary(), parser(T)) -> {more, parser(T)}.
-more(<<>>, F) ->
- {more, F};
-more(Bin, InnerF) ->
- F = fun (NewData) when is_binary(NewData) ->
- InnerF(<<Bin/binary, NewData/binary>>)
- end,
- {more, F}.
-
-%% Tests.
-
--ifdef(TEST).
-
-multipart_test_() ->
- %% {Body, Result}
- Tests = [
- {<<"--boundary--">>, []},
- {<<"preamble\r\n--boundary--">>, []},
- {<<"--boundary--\r\nepilogue">>, []},
- {<<"\r\n--boundary\r\nA:b\r\nC:d\r\n\r\n\r\n--boundary--">>,
- [{[{<<"a">>, <<"b">>}, {<<"c">>, <<"d">>}], <<>>}]},
- {
- <<
- "--boundary\r\nX-Name:answer\r\n\r\n42"
- "\r\n--boundary\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
- "\r\n--boundary--"
- >>,
- [
- {[{<<"x-name">>, <<"answer">>}], <<"42">>},
- {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
- ]
- }
- ],
- [{title(V), fun () -> R = acc_multipart(V) end} || {V, R} <- Tests].
-
-acc_multipart(V) ->
- acc_multipart((parser(<<"boundary">>))(V), []).
-
-acc_multipart({headers, Headers, Cont}, Acc) ->
- acc_multipart(Cont(), [{Headers, []}|Acc]);
-acc_multipart({body, Body, Cont}, [{Headers, BodyAcc}|Acc]) ->
- acc_multipart(Cont(), [{Headers, [Body|BodyAcc]}|Acc]);
-acc_multipart({end_of_part, Cont}, [{Headers, BodyAcc}|Acc]) ->
- Body = list_to_binary(lists:reverse(BodyAcc)),
- acc_multipart(Cont(), [{Headers, Body}|Acc]);
-acc_multipart(eof, Acc) ->
- lists:reverse(Acc).
-
-content_disposition_test_() ->
- %% {Disposition, Result}
- Tests = [
- {<<"form-data; name=id">>, {<<"form-data">>, [{<<"name">>, <<"id">>}]}},
- {<<"inline">>, {<<"inline">>, []}},
- {<<"attachment; \tfilename=brackets-slides.pdf">>,
- {<<"attachment">>, [{<<"filename">>, <<"brackets-slides.pdf">>}]}}
- ],
- [{title(V), fun () -> R = content_disposition(V) end} || {V, R} <- Tests].
-
-title(Bin) ->
- Title = lists:foldl(
- fun ({T, R}, V) -> re:replace(V, T, R, [global]) end,
- Bin,
- [{"\t", "\\\\t"}, {"\r", "\\\\r"}, {"\n", "\\\\n"}]
- ),
- iolist_to_binary(Title).
-
-suffix_test_() ->
- Tests = [
- {nomatch, <<>>, <<"ABC">>},
- {{0, 1}, <<"\r">>, <<"ABC">>},
- {{0, 2}, <<"\r\n">>, <<"ABC">>},
- {{0, 4}, <<"\r\n--">>, <<"ABC">>},
- {{0, 5}, <<"\r\n--A">>, <<"ABC">>},
- {{0, 6}, <<"\r\n--AB">>, <<"ABC">>},
- {{0, 7}, <<"\r\n--ABC">>, <<"ABC">>},
- {nomatch, <<"\r\n--AB1">>, <<"ABC">>},
- {{1, 1}, <<"1\r">>, <<"ABC">>},
- {{2, 2}, <<"12\r\n">>, <<"ABC">>},
- {{3, 4}, <<"123\r\n--">>, <<"ABC">>}
- ],
- [fun() -> Part = suffix_match(Packet, pattern(Boundary)) end ||
- {Part, Packet, Boundary} <- Tests].
-
--endif.
diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl
index fdc1126..22faf1b 100644
--- a/src/cowboy_protocol.erl
+++ b/src/cowboy_protocol.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]>
%% Copyright (c) 2011, Anthony Ramine <[email protected]>
%%
%% Permission to use, copy, modify, and/or distribute this software for any
@@ -13,39 +13,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc HTTP protocol handler.
-%%
-%% The available options are:
-%% <dl>
-%% <dt>compress</dt><dd>Whether to automatically compress the response
-%% body when the conditions are met. Disabled by default.</dd>
-%% <dt>env</dt><dd>The environment passed and optionally modified
-%% by middlewares.</dd>
-%% <dt>max_empty_lines</dt><dd>Max number of empty lines before a request.
-%% Defaults to 5.</dd>
-%% <dt>max_header_name_length</dt><dd>Max length allowed for header names.
-%% Defaults to 64.</dd>
-%% <dt>max_header_value_length</dt><dd>Max length allowed for header values.
-%% Defaults to 4096.</dd>
-%% <dt>max_headers</dt><dd>Max number of headers allowed.
-%% Defaults to 100.</dd>
-%% <dt>max_keepalive</dt><dd>Max number of requests allowed in a single
-%% keep-alive session. Defaults to 100.</dd>
-%% <dt>max_request_line_length</dt><dd>Max length allowed for the request
-%% line. Defaults to 4096.</dd>
-%% <dt>middlewares</dt><dd>The list of middlewares to execute when a
-%% request is received.</dd>
-%% <dt>onrequest</dt><dd>Optional fun that allows Req interaction before
-%% any dispatching is done. Host info, path info and bindings are thus
-%% not available at this point.</dd>
-%% <dt>onresponse</dt><dd>Optional fun that allows replacing a response
-%% sent by the application.</dd>
-%% <dt>timeout</dt><dd>Time in milliseconds a client has to send the
-%% full request line and headers. Defaults to 5000 milliseconds.</dd>
-%% </dl>
-%%
-%% Note that there is no need to monitor these processes when using Cowboy as
-%% an application as it already supervises them under the listener supervisor.
-module(cowboy_protocol).
%% API.
@@ -89,9 +56,10 @@
until :: non_neg_integer() | infinity
}).
+-include_lib("cowlib/include/cow_inline.hrl").
+
%% API.
-%% @doc Start an HTTP protocol process.
-spec start_link(ranch:ref(), inet:socket(), module(), opts()) -> {ok, pid()}.
start_link(Ref, Socket, Transport, Opts) ->
Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts]),
@@ -99,15 +67,13 @@ start_link(Ref, Socket, Transport, Opts) ->
%% Internal.
-%% @doc Faster alternative to proplists:get_value/3.
-%% @private
+%% Faster alternative to proplists:get_value/3.
get_value(Key, Opts, Default) ->
case lists:keyfind(Key, 1, Opts) of
{_, Value} -> Value;
_ -> Default
end.
-%% @private
-spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok.
init(Ref, Socket, Transport, Opts) ->
Compress = get_value(compress, Opts, false),
@@ -170,7 +136,6 @@ wait_request(Buffer, State=#state{socket=Socket, transport=Transport,
terminate(State)
end.
-%% @private
-spec parse_request(binary(), #state{}, non_neg_integer()) -> ok.
%% Empty lines must be using \r\n.
parse_request(<< $\n, _/binary >>, State, _) ->
@@ -292,44 +257,12 @@ match_colon(<< _, Rest/bits >>, N) ->
match_colon(_, _) ->
nomatch.
-%% I know, this isn't exactly pretty. But this is the most critical
-%% code path and as such needs to be optimized to death.
-%%
-%% ... Sorry for your eyes.
-%%
-%% But let's be honest, that's still pretty readable.
parse_hd_name(<< C, Rest/bits >>, S, M, P, Q, V, H, SoFar) ->
case C of
$: -> parse_hd_before_value(Rest, S, M, P, Q, V, H, SoFar);
$\s -> parse_hd_name_ws(Rest, S, M, P, Q, V, H, SoFar);
$\t -> parse_hd_name_ws(Rest, S, M, P, Q, V, H, SoFar);
- $A -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $a >>);
- $B -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $b >>);
- $C -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $c >>);
- $D -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $d >>);
- $E -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $e >>);
- $F -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $f >>);
- $G -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $g >>);
- $H -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $h >>);
- $I -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $i >>);
- $J -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $j >>);
- $K -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $k >>);
- $L -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $l >>);
- $M -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $m >>);
- $N -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $n >>);
- $O -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $o >>);
- $P -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $p >>);
- $Q -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $q >>);
- $R -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $r >>);
- $S -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $s >>);
- $T -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $t >>);
- $U -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $u >>);
- $V -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $v >>);
- $W -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $w >>);
- $X -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $x >>);
- $Y -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $y >>);
- $Z -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $z >>);
- C -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, C >>)
+ ?INLINE_LOWERCASE(parse_hd_name, Rest, S, M, P, Q, V, H, SoFar)
end.
parse_hd_name_ws(<< C, Rest/bits >>, S, M, P, Q, V, H, Name) ->
@@ -405,7 +338,8 @@ parse_hd_value(<< $\r, Rest/bits >>, S, M, P, Q, V, Headers, Name, SoFar) ->
<< $\n >> ->
wait_hd_value_nl(<<>>, S, M, P, Q, V, Headers, Name, SoFar);
<< $\n, C, Rest2/bits >> when C =:= $\s; C =:= $\t ->
- parse_hd_value(Rest2, S, M, P, Q, V, Headers, Name, SoFar);
+ parse_hd_value(Rest2, S, M, P, Q, V, Headers, Name,
+ << SoFar/binary, C >>);
<< $\n, Rest2/bits >> ->
parse_header(Rest2, S, M, P, Q, V, [{Name, SoFar}|Headers])
end;
@@ -441,8 +375,6 @@ request(B, State=#state{transport=Transport}, M, P, Q, Version, Headers) ->
default_port(ssl) -> 443;
default_port(_) -> 80.
-%% Another hurtful block of code. :)
-%%
%% Same code as cow_http:parse_fullhost/1, but inline because we
%% really want this to go fast.
parse_host(<< $[, Rest/bits >>, false, <<>>) ->
@@ -455,33 +387,7 @@ parse_host(<< $], Rest/bits >>, true, Acc) ->
parse_host(Rest, false, << Acc/binary, $] >>);
parse_host(<< C, Rest/bits >>, E, Acc) ->
case C of
- $A -> parse_host(Rest, E, << Acc/binary, $a >>);
- $B -> parse_host(Rest, E, << Acc/binary, $b >>);
- $C -> parse_host(Rest, E, << Acc/binary, $c >>);
- $D -> parse_host(Rest, E, << Acc/binary, $d >>);
- $E -> parse_host(Rest, E, << Acc/binary, $e >>);
- $F -> parse_host(Rest, E, << Acc/binary, $f >>);
- $G -> parse_host(Rest, E, << Acc/binary, $g >>);
- $H -> parse_host(Rest, E, << Acc/binary, $h >>);
- $I -> parse_host(Rest, E, << Acc/binary, $i >>);
- $J -> parse_host(Rest, E, << Acc/binary, $j >>);
- $K -> parse_host(Rest, E, << Acc/binary, $k >>);
- $L -> parse_host(Rest, E, << Acc/binary, $l >>);
- $M -> parse_host(Rest, E, << Acc/binary, $m >>);
- $N -> parse_host(Rest, E, << Acc/binary, $n >>);
- $O -> parse_host(Rest, E, << Acc/binary, $o >>);
- $P -> parse_host(Rest, E, << Acc/binary, $p >>);
- $Q -> parse_host(Rest, E, << Acc/binary, $q >>);
- $R -> parse_host(Rest, E, << Acc/binary, $r >>);
- $S -> parse_host(Rest, E, << Acc/binary, $s >>);
- $T -> parse_host(Rest, E, << Acc/binary, $t >>);
- $U -> parse_host(Rest, E, << Acc/binary, $u >>);
- $V -> parse_host(Rest, E, << Acc/binary, $v >>);
- $W -> parse_host(Rest, E, << Acc/binary, $w >>);
- $X -> parse_host(Rest, E, << Acc/binary, $x >>);
- $Y -> parse_host(Rest, E, << Acc/binary, $y >>);
- $Z -> parse_host(Rest, E, << Acc/binary, $z >>);
- _ -> parse_host(Rest, E, << Acc/binary, C >>)
+ ?INLINE_LOWERCASE(parse_host, Rest, E, Acc)
end.
%% End of request parsing.
@@ -538,7 +444,6 @@ execute(Req, State, Env, [Middleware|Tail]) ->
error_terminate(Code, Req2, State)
end.
-%% @private
-spec resume(#state{}, cowboy_middleware:env(), [module()],
module(), module(), [any()]) -> ok.
resume(State, Env, Tail, Module, Function, Args) ->
@@ -569,8 +474,8 @@ next_request(Req, State=#state{req_keepalive=Keepalive, timeout=Timeout},
_ -> close
end,
%% Flush the resp_sent message before moving on.
- receive {cowboy_req, resp_sent} -> ok after 0 -> ok end,
if HandlerRes =:= ok, Buffer =/= close ->
+ receive {cowboy_req, resp_sent} -> ok after 0 -> ok end,
?MODULE:parse_request(Buffer,
State#state{req_keepalive=Keepalive + 1,
until=until(Timeout)}, 0);
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl
index d98e395..eec4e88 100644
--- a/src/cowboy_req.erl
+++ b/src/cowboy_req.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]>
%% Copyright (c) 2011, Anthony Ramine <[email protected]>
%%
%% Permission to use, copy, modify, and/or distribute this software for any
@@ -13,31 +13,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc HTTP request manipulation API.
-%%
-%% The functions in this module try to follow this pattern for their
-%% return types:
-%% <dl>
-%% <dt>access:</dt>
-%% <dd><em>{Value, Req}</em></dd>
-%% <dt>action:</dt>
-%% <dd><em>{Result, Req} | {Result, Value, Req} | {error, atom()}</em></dd>
-%% <dt>modification:</dt>
-%% <dd><em>Req</em></dd>
-%% <dt>question (<em>has_*</em> or <em>is_*</em>):</dt>
-%% <dd><em>boolean()</em></dd>
-%% </dl>
-%%
-%% Exceptions include <em>chunk/2</em> which always returns <em>'ok'</em>,
-%% and <em>to_list/1</em> which returns a list of key/values.
-%%
-%% Also note that all body reading functions perform actions, as Cowboy
-%% doesn't read the request body until they are called.
-%%
-%% Whenever <em>Req</em> is returned, it should always be kept in place of
-%% the one given as argument in your function call, because it keeps
-%% track of the request and response state. Doing so allows Cowboy to do
-%% some lazy evaluation and cache results when possible.
-module(cowboy_req).
%% Request API.
@@ -82,8 +57,11 @@
-export([body/2]).
-export([body_qs/1]).
-export([body_qs/2]).
--export([multipart_data/1]).
--export([multipart_skip/1]).
+
+%% Multipart API.
+-export([part/1]).
+-export([part_body/1]).
+-export([part_body/2]).
%% Response API.
-export([set_resp_cookie/4]).
@@ -122,11 +100,7 @@
-> {ok, binary()}
| {error, atom()}).
-type transfer_decode_fun() :: fun((binary(), any())
- -> {ok, binary(), binary(), any()}
- | more | {more, non_neg_integer(), binary(), any()}
- | {done, non_neg_integer(), binary()}
- | {done, binary(), non_neg_integer(), binary()}
- | {error, atom()}).
+ -> cow_http_te:decode_ret()).
-type resp_body_fun() :: fun((any(), module()) -> ok).
-type send_chunk_fun() :: fun((iodata()) -> ok | {error, atom()}).
@@ -159,8 +133,8 @@
%% Request body.
body_state = waiting :: waiting | done | {stream, non_neg_integer(),
transfer_decode_fun(), any(), content_decode_fun()},
- multipart = undefined :: undefined | {non_neg_integer(), fun()},
buffer = <<>> :: binary(),
+ multipart = undefined :: undefined | {binary(), binary()},
%% Response.
resp_compress = false :: boolean(),
@@ -181,13 +155,6 @@
%% Request API.
-%% @doc Create a new HTTP Req object.
-%%
-%% This function takes care of setting the owner's pid to self().
-%% @private
-%%
-%% Since we always need to parse the Connection header, we do it
-%% in an optimized way and add the parsed value to p_headers' cache.
-spec new(any(), module(),
undefined | {inet:ip_address(), inet:port_number()},
binary(), binary(), binary(),
@@ -210,72 +177,58 @@ new(Socket, Transport, Peer, Method, Path, Query,
false ->
Req; %% keepalive
{_, ConnectionHeader} ->
- Tokens = parse_connection_before(ConnectionHeader, []),
+ Tokens = cow_http_hd:parse_connection(ConnectionHeader),
Connection = connection_to_atom(Tokens),
Req#http_req{connection=Connection,
p_headers=[{<<"connection">>, Tokens}]}
end
end.
-%% @doc Return the HTTP method of the request.
-spec method(Req) -> {binary(), Req} when Req::req().
method(Req) ->
{Req#http_req.method, Req}.
-%% @doc Return the HTTP version used for the request.
-spec version(Req) -> {cowboy:http_version(), Req} when Req::req().
version(Req) ->
{Req#http_req.version, Req}.
-%% @doc Return the peer address and port number of the remote host.
-spec peer(Req)
-> {{inet:ip_address(), inet:port_number()}, Req}
when Req::req().
peer(Req) ->
{Req#http_req.peer, Req}.
-%% @doc Return the host binary string.
-spec host(Req) -> {binary(), Req} when Req::req().
host(Req) ->
{Req#http_req.host, Req}.
-%% @doc Return the extra host information obtained from partially matching
-%% the hostname using <em>'...'</em>.
-spec host_info(Req)
-> {cowboy_router:tokens() | undefined, Req} when Req::req().
host_info(Req) ->
{Req#http_req.host_info, Req}.
-%% @doc Return the port used for this request.
-spec port(Req) -> {inet:port_number(), Req} when Req::req().
port(Req) ->
{Req#http_req.port, Req}.
-%% @doc Return the path binary string.
-spec path(Req) -> {binary(), Req} when Req::req().
path(Req) ->
{Req#http_req.path, Req}.
-%% @doc Return the extra path information obtained from partially matching
-%% the patch using <em>'...'</em>.
-spec path_info(Req)
-> {cowboy_router:tokens() | undefined, Req} when Req::req().
path_info(Req) ->
{Req#http_req.path_info, Req}.
-%% @doc Return the raw query string directly taken from the request.
-spec qs(Req) -> {binary(), Req} when Req::req().
qs(Req) ->
{Req#http_req.qs, Req}.
-%% @equiv qs_val(Name, Req, undefined)
-spec qs_val(binary(), Req)
-> {binary() | true | undefined, Req} when Req::req().
qs_val(Name, Req) when is_binary(Name) ->
qs_val(Name, Req, undefined).
-%% @doc Return the query string value for the given key, or a default if
-%% missing.
-spec qs_val(binary(), Req, Default)
-> {binary() | true | Default, Req} when Req::req(), Default::any().
qs_val(Name, Req=#http_req{qs=RawQs, qs_vals=undefined}, Default)
@@ -288,7 +241,6 @@ qs_val(Name, Req, Default) ->
false -> {Default, Req}
end.
-%% @doc Return the full list of query string values.
-spec qs_vals(Req) -> {list({binary(), binary() | true}), Req} when Req::req().
qs_vals(Req=#http_req{qs=RawQs, qs_vals=undefined}) ->
QsVals = cow_qs:parse_qs(RawQs),
@@ -296,10 +248,7 @@ qs_vals(Req=#http_req{qs=RawQs, qs_vals=undefined}) ->
qs_vals(Req=#http_req{qs_vals=QsVals}) ->
{QsVals, Req}.
-%% @doc Return the request URL as a binary without the path and query string.
-%%
%% The URL includes the scheme, host and port only.
-%% @see cowboy_req:url/1
-spec host_url(Req) -> {undefined | binary(), Req} when Req::req().
host_url(Req=#http_req{port=undefined}) ->
{undefined, Req};
@@ -316,8 +265,6 @@ host_url(Req=#http_req{transport=Transport, host=Host, port=Port}) ->
end,
{<< "http", Secure/binary, "://", Host/binary, PortBin/binary >>, Req}.
-%% @doc Return the full request URL as a binary.
-%%
%% The URL includes the scheme, host, port, path and query string.
-spec url(Req) -> {undefined | binary(), Req} when Req::req().
url(Req=#http_req{}) ->
@@ -333,13 +280,10 @@ url(HostURL, Req=#http_req{path=Path, qs=QS}) ->
end,
{<< HostURL/binary, Path/binary, QS2/binary >>, Req}.
-%% @equiv binding(Name, Req, undefined)
-spec binding(atom(), Req) -> {any() | undefined, Req} when Req::req().
binding(Name, Req) when is_atom(Name) ->
binding(Name, Req, undefined).
-%% @doc Return the binding value for the given key obtained when matching
-%% the host and path against the dispatch list, or a default if missing.
-spec binding(atom(), Req, Default)
-> {any() | Default, Req} when Req::req(), Default::any().
binding(Name, Req, Default) when is_atom(Name) ->
@@ -348,18 +292,15 @@ binding(Name, Req, Default) when is_atom(Name) ->
false -> {Default, Req}
end.
-%% @doc Return the full list of binding values.
-spec bindings(Req) -> {[{atom(), any()}], Req} when Req::req().
bindings(Req) ->
{Req#http_req.bindings, Req}.
-%% @equiv header(Name, Req, undefined)
-spec header(binary(), Req)
-> {binary() | undefined, Req} when Req::req().
header(Name, Req) ->
header(Name, Req, undefined).
-%% @doc Return the header value for the given key, or a default if missing.
-spec header(binary(), Req, Default)
-> {binary() | Default, Req} when Req::req(), Default::any().
header(Name, Req, Default) ->
@@ -368,16 +309,10 @@ header(Name, Req, Default) ->
false -> {Default, Req}
end.
-%% @doc Return the full list of headers.
-spec headers(Req) -> {cowboy:http_headers(), Req} when Req::req().
headers(Req) ->
{Req#http_req.headers, Req}.
-%% @doc Semantically parse headers.
-%%
-%% When the value isn't found, a proper default value for the type
-%% returned is used as a return value.
-%% @see parse_header/3
-spec parse_header(binary(), Req)
-> {ok, any(), Req} | {undefined, binary(), Req}
| {error, badarg} when Req::req().
@@ -387,14 +322,10 @@ parse_header(Name, Req=#http_req{p_headers=PHeaders}) ->
{Name, Value} -> {ok, Value, Req}
end.
-%% @doc Default values for semantic header parsing.
-spec parse_header_default(binary()) -> any().
parse_header_default(<<"transfer-encoding">>) -> [<<"identity">>];
parse_header_default(_Name) -> undefined.
-%% @doc Semantically parse headers.
-%%
-%% When the header is unknown, the value is returned directly without parsing.
-spec parse_header(binary(), Req, any())
-> {ok, any(), Req} | {undefined, binary(), Req}
| {error, badarg} when Req::req().
@@ -424,7 +355,7 @@ parse_header(Name = <<"authorization">>, Req, Default) ->
cowboy_http:token_ci(Value, fun cowboy_http:authorization/2)
end);
parse_header(Name = <<"content-length">>, Req, Default) ->
- parse_header(Name, Req, Default, fun cowboy_http:digits/1);
+ parse_header(Name, Req, Default, fun cow_http_hd:parse_content_length/1);
parse_header(Name = <<"content-type">>, Req, Default) ->
parse_header(Name, Req, Default, fun cowboy_http:content_type/1);
parse_header(Name = <<"cookie">>, Req, Default) ->
@@ -451,10 +382,10 @@ parse_header(Name, Req, Default)
fun (Value) ->
cowboy_http:nonempty_list(Value, fun cowboy_http:token/2)
end);
-%% @todo Extension parameters.
-parse_header(Name, Req, Default)
- when Name =:= <<"transfer-encoding">>;
- Name =:= <<"upgrade">> ->
+parse_header(Name = <<"transfer-encoding">>, Req, Default) ->
+ parse_header(Name, Req, Default, fun cow_http_hd:parse_transfer_encoding/1);
+%% @todo Product version.
+parse_header(Name = <<"upgrade">>, Req, Default) ->
parse_header(Name, Req, Default,
fun (Value) ->
cowboy_http:nonempty_list(Value, fun cowboy_http:token_ci/2)
@@ -478,14 +409,11 @@ parse_header(Name, Req=#http_req{p_headers=PHeaders}, Default, Fun) ->
end
end.
-%% @equiv cookie(Name, Req, undefined)
-spec cookie(binary(), Req)
-> {binary() | undefined, Req} when Req::req().
cookie(Name, Req) when is_binary(Name) ->
cookie(Name, Req, undefined).
-%% @doc Return the cookie value for the given key, or a default if
-%% missing.
-spec cookie(binary(), Req, Default)
-> {binary() | Default, Req} when Req::req(), Default::any().
cookie(Name, Req=#http_req{cookies=undefined}, Default) when is_binary(Name) ->
@@ -501,7 +429,6 @@ cookie(Name, Req, Default) ->
false -> {Default, Req}
end.
-%% @doc Return the full list of cookie values.
-spec cookies(Req) -> {list({binary(), binary()}), Req} when Req::req().
cookies(Req=#http_req{cookies=undefined}) ->
case parse_header(<<"cookie">>, Req) of
@@ -516,16 +443,10 @@ cookies(Req=#http_req{cookies=undefined}) ->
cookies(Req=#http_req{cookies=Cookies}) ->
{Cookies, Req}.
-%% @equiv meta(Name, Req, undefined)
-spec meta(atom(), Req) -> {any() | undefined, Req} when Req::req().
meta(Name, Req) ->
meta(Name, Req, undefined).
-%% @doc Return metadata information about the request.
-%%
-%% Metadata information varies from one protocol to another. Websockets
-%% would define the protocol version here, while REST would use it to
-%% indicate which media type, language and charset were retained.
-spec meta(atom(), Req, any()) -> {any(), Req} when Req::req().
meta(Name, Req, Default) ->
case lists:keyfind(Name, 1, Req#http_req.meta) of
@@ -533,18 +454,12 @@ meta(Name, Req, Default) ->
false -> {Default, Req}
end.
-%% @doc Set metadata information.
-%%
-%% You can use this function to attach information about the request.
-%%
-%% If the value already exists it will be overwritten.
-spec set_meta(atom(), any(), Req) -> Req when Req::req().
set_meta(Name, Value, Req=#http_req{meta=Meta}) ->
- Req#http_req{meta=[{Name, Value}|lists:keydelete(Name, 1, Meta)]}.
+ Req#http_req{meta=lists:keystore(Name, 1, Meta, {Name, Value})}.
%% Request Body API.
-%% @doc Return whether the request message has a body.
-spec has_body(req()) -> boolean().
has_body(Req) ->
case lists:keyfind(<<"content-length">>, 1, Req#http_req.headers) of
@@ -556,8 +471,6 @@ has_body(Req) ->
lists:keymember(<<"transfer-encoding">>, 1, Req#http_req.headers)
end.
-%% @doc Return the request message body length, if known.
-%%
%% The length may not be known if Transfer-Encoding is not identity,
%% and the body hasn't been read at the time of the call.
-spec body_length(Req) -> {undefined | non_neg_integer(), Req} when Req::req().
@@ -570,12 +483,6 @@ body_length(Req) ->
{undefined, Req2}
end.
-%% @doc Initialize body streaming and set custom decoding functions.
-%%
-%% Calling this function is optional. It should only be used if you
-%% need to override the default behavior of Cowboy. Otherwise you
-%% should call stream_body/{1,2} directly.
-%%
%% Two decodings happen. First a decoding function is applied to the
%% transferred data, and then another is applied to the actual content.
%%
@@ -584,34 +491,17 @@ body_length(Req) ->
%% also initialized through this function.
%%
%% Content encoding is generally used for compression.
-%%
-%% Standard encodings can be found in cowboy_http.
-spec init_stream(transfer_decode_fun(), any(), content_decode_fun(), Req)
-> {ok, Req} when Req::req().
init_stream(TransferDecode, TransferState, ContentDecode, Req) ->
{ok, Req#http_req{body_state=
{stream, 0, TransferDecode, TransferState, ContentDecode}}}.
-%% @equiv stream_body(1000000, Req)
-spec stream_body(Req) -> {ok, binary(), Req}
| {done, Req} | {error, atom()} when Req::req().
stream_body(Req) ->
stream_body(1000000, Req).
-%% @doc Stream the request's body.
-%%
-%% This is the most low level function to read the request body.
-%%
-%% In most cases, if they weren't defined before using init_stream/4,
-%% this function will guess which transfer and content encodings were
-%% used for building the request body, and configure the decoding
-%% functions that will be used when streaming.
-%%
-%% It then starts streaming the body, returning {ok, Data, Req}
-%% for each streamed part, and {done, Req} when it's finished streaming.
-%%
-%% You can limit the size of the chunks being returned by using the
-%% first argument which is the size in bytes. It defaults to 1000000 bytes.
-spec stream_body(non_neg_integer(), Req) -> {ok, binary(), Req}
| {done, Req} | {error, atom()} when Req::req().
stream_body(MaxLength, Req=#http_req{body_state=waiting, version=Version,
@@ -629,7 +519,7 @@ stream_body(MaxLength, Req=#http_req{body_state=waiting, version=Version,
{ok, [<<"chunked">>], Req2} ->
stream_body(MaxLength, Req2#http_req{body_state=
{stream, 0,
- fun cowboy_http:te_chunked/2, {0, 0},
+ fun cow_http_te:stream_chunked/2, {0, 0},
fun cowboy_http:ce_identity/1}});
{ok, [<<"identity">>], Req2} ->
{Length, Req3} = body_length(Req2),
@@ -639,7 +529,7 @@ stream_body(MaxLength, Req=#http_req{body_state=waiting, version=Version,
Length ->
stream_body(MaxLength, Req3#http_req{body_state=
{stream, Length,
- fun cowboy_http:te_identity/2, {0, Length},
+ fun cow_http_te:stream_identity/2, {0, Length},
fun cowboy_http:ce_identity/1}})
end
end;
@@ -663,31 +553,34 @@ stream_body_recv(MaxLength, Req=#http_req{
{error, Reason} -> {error, Reason}
end.
+%% @todo Handle chunked after-the-facts headers.
+%% @todo Depending on the length returned we might want to 0 or +5 it.
-spec transfer_decode(binary(), Req)
-> {ok, binary(), Req} | {error, atom()} when Req::req().
transfer_decode(Data, Req=#http_req{body_state={stream, _,
TransferDecode, TransferState, ContentDecode}}) ->
case TransferDecode(Data, TransferState) of
- {ok, Data2, Rest, TransferState2} ->
- content_decode(ContentDecode, Data2,
- Req#http_req{buffer=Rest, body_state={stream, 0,
- TransferDecode, TransferState2, ContentDecode}});
- %% @todo {header(s) for chunked
more ->
stream_body_recv(0, Req#http_req{buffer=Data, body_state={stream,
0, TransferDecode, TransferState, ContentDecode}});
- {more, Length, Data2, TransferState2} ->
+ {more, Data2, TransferState2} ->
+ content_decode(ContentDecode, Data2,
+ Req#http_req{body_state={stream, 0,
+ TransferDecode, TransferState2, ContentDecode}});
+ {more, Data2, Length, TransferState2} when is_integer(Length) ->
content_decode(ContentDecode, Data2,
Req#http_req{body_state={stream, Length,
TransferDecode, TransferState2, ContentDecode}});
+ {more, Data2, Rest, TransferState2} ->
+ content_decode(ContentDecode, Data2,
+ Req#http_req{buffer=Rest, body_state={stream, 0,
+ TransferDecode, TransferState2, ContentDecode}});
{done, Length, Rest} ->
Req2 = transfer_decode_done(Length, Rest, Req),
{done, Req2};
{done, Data2, Length, Rest} ->
Req2 = transfer_decode_done(Length, Rest, Req),
- content_decode(ContentDecode, Data2, Req2);
- {error, Reason} ->
- {error, Reason}
+ content_decode(ContentDecode, Data2, Req2)
end.
-spec transfer_decode_done(non_neg_integer(), binary(), Req)
@@ -704,7 +597,6 @@ transfer_decode_done(Length, Rest, Req=#http_req{
Req#http_req{buffer=Rest, body_state=done,
headers=Headers3, p_headers=PHeaders3}.
-%% @todo Probably needs a Rest.
-spec content_decode(content_decode_fun(), binary(), Req)
-> {ok, binary(), Req} | {error, atom()} when Req::req().
content_decode(ContentDecode, Data, Req) ->
@@ -713,12 +605,10 @@ content_decode(ContentDecode, Data, Req) ->
{error, Reason} -> {error, Reason}
end.
-%% @equiv body(8000000, Req)
-spec body(Req) -> {ok, binary(), Req} | {error, atom()} when Req::req().
body(Req) ->
body(8000000, Req).
-%% @doc Return the body sent with the request.
-spec body(non_neg_integer() | infinity, Req)
-> {ok, binary(), Req} | {error, atom()} when Req::req().
body(MaxBodyLength, Req) ->
@@ -754,15 +644,12 @@ skip_body(Req) ->
{error, Reason} -> {error, Reason}
end.
-%% @equiv body_qs(16000, Req)
-spec body_qs(Req)
-> {ok, [{binary(), binary() | true}], Req} | {error, atom()}
when Req::req().
body_qs(Req) ->
body_qs(16000, Req).
-%% @doc Return the body sent with the request, parsed as an
-%% application/x-www-form-urlencoded string.
%% Essentially a POST query string.
-spec body_qs(non_neg_integer() | infinity, Req)
-> {ok, [{binary(), binary() | true}], Req} | {error, atom()}
@@ -775,65 +662,78 @@ body_qs(MaxBodyLength, Req) ->
{error, Reason}
end.
-%% Multipart Request API.
+%% Multipart API.
-%% @doc Return data from the multipart parser.
-%%
-%% Use this function for multipart streaming. For each part in the request,
-%% this function returns <em>{headers, Headers, Req}</em> followed by a sequence of
-%% <em>{body, Data, Req}</em> tuples and finally <em>{end_of_part, Req}</em>. When there
-%% is no part to parse anymore, <em>{eof, Req}</em> is returned.
--spec multipart_data(Req)
- -> {headers, cowboy:http_headers(), Req} | {body, binary(), Req}
- | {end_of_part | eof, Req} when Req::req().
-multipart_data(Req=#http_req{body_state=waiting}) ->
- {ok, {<<"multipart">>, _SubType, Params}, Req2} =
- parse_header(<<"content-type">>, Req),
- {_, Boundary} = lists:keyfind(<<"boundary">>, 1, Params),
- {ok, Length, Req3} = parse_header(<<"content-length">>, Req2),
- multipart_data(Req3, Length, {more, cowboy_multipart:parser(Boundary)});
-multipart_data(Req=#http_req{multipart={Length, Cont}}) ->
- multipart_data(Req, Length, Cont());
-multipart_data(Req=#http_req{body_state=done}) ->
- {eof, Req}.
-
-multipart_data(Req, Length, {headers, Headers, Cont}) ->
- {headers, Headers, Req#http_req{multipart={Length, Cont}}};
-multipart_data(Req, Length, {body, Data, Cont}) ->
- {body, Data, Req#http_req{multipart={Length, Cont}}};
-multipart_data(Req, Length, {end_of_part, Cont}) ->
- {end_of_part, Req#http_req{multipart={Length, Cont}}};
-multipart_data(Req, 0, eof) ->
- {eof, Req#http_req{body_state=done, multipart=undefined}};
-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=undefined}};
-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))
+-spec part(Req)
+ -> {ok, cow_multipart:headers(), Req} | {done, Req}
+ when Req::req().
+part(Req=#http_req{multipart=undefined}) ->
+ part(init_multipart(Req));
+part(Req) ->
+ {ok, Data, Req2} = stream_multipart(Req),
+ part(Data, Req2).
+
+part(Buffer, Req=#http_req{multipart={Boundary, _}}) ->
+ case cow_multipart:parse_headers(Buffer, Boundary) of
+ more ->
+ {ok, Data, Req2} = stream_multipart(Req),
+ part(<< Buffer/binary, Data/binary >>, Req2);
+ {more, Buffer2} ->
+ {ok, Data, Req2} = stream_multipart(Req),
+ part(<< Buffer2/binary, Data/binary >>, Req2);
+ {ok, Headers, Rest} ->
+ {ok, Headers, Req#http_req{multipart={Boundary, Rest}}};
+ %% Ignore epilogue.
+ {done, _} ->
+ {done, Req#http_req{multipart=undefined}}
end.
-%% @doc Skip a part returned by the multipart parser.
-%%
-%% This function repeatedly calls <em>multipart_data/1</em> until
-%% <em>{end_of_part, Req}</em> or <em>{eof, Req}</em> is parsed.
--spec multipart_skip(Req) -> {ok, Req} when Req::req().
-multipart_skip(Req) ->
- case multipart_data(Req) of
- {end_of_part, Req2} -> {ok, Req2};
- {eof, Req2} -> {ok, Req2};
- {_, _, Req2} -> multipart_skip(Req2)
+-spec part_body(Req)
+ -> {ok, binary(), Req} | {more, binary(), Req}
+ when Req::req().
+part_body(Req) ->
+ part_body(8000000, Req).
+
+-spec part_body(non_neg_integer(), Req)
+ -> {ok, binary(), Req} | {more, binary(), Req}
+ when Req::req().
+part_body(MaxLength, Req=#http_req{multipart=undefined}) ->
+ part_body(MaxLength, init_multipart(Req));
+part_body(MaxLength, Req) ->
+ part_body(<<>>, MaxLength, Req, <<>>).
+
+part_body(Buffer, MaxLength, Req=#http_req{multipart={Boundary, _}}, Acc)
+ when byte_size(Acc) > MaxLength ->
+ {more, Acc, Req#http_req{multipart={Boundary, Buffer}}};
+part_body(Buffer, MaxLength, Req=#http_req{multipart={Boundary, _}}, Acc) ->
+ {ok, Data, Req2} = stream_multipart(Req),
+ case cow_multipart:parse_body(<< Buffer/binary, Data/binary >>, Boundary) of
+ {ok, Body} ->
+ part_body(<<>>, MaxLength, Req2, << Acc/binary, Body/binary >>);
+ {ok, Body, Rest} ->
+ part_body(Rest, MaxLength, Req2, << Acc/binary, Body/binary >>);
+ done ->
+ {ok, Acc, Req2};
+ {done, Body} ->
+ {ok, << Acc/binary, Body/binary >>, Req2};
+ {done, Body, Rest} ->
+ {ok, << Acc/binary, Body/binary >>,
+ Req2#http_req{multipart={Boundary, Rest}}}
end.
+init_multipart(Req) ->
+ {ok, {<<"multipart">>, _, Params}, Req2}
+ = parse_header(<<"content-type">>, Req),
+ {_, Boundary} = lists:keyfind(<<"boundary">>, 1, Params),
+ Req2#http_req{multipart={Boundary, <<>>}}.
+
+stream_multipart(Req=#http_req{multipart={_, <<>>}}) ->
+ stream_body(Req);
+stream_multipart(Req=#http_req{multipart={Boundary, Buffer}}) ->
+ {ok, Buffer, Req#http_req{multipart={Boundary, <<>>}}}.
+
%% Response API.
-%% @doc Add a cookie header to the response.
-%%
%% The cookie name cannot contain any of the following characters:
%% =,;\s\t\r\n\013\014
%%
@@ -845,46 +745,19 @@ set_resp_cookie(Name, Value, Opts, Req) ->
Cookie = cow_cookie:setcookie(Name, Value, Opts),
set_resp_header(<<"set-cookie">>, Cookie, Req).
-%% @doc Add a header to the response.
-spec set_resp_header(binary(), iodata(), Req)
-> Req when Req::req().
set_resp_header(Name, Value, Req=#http_req{resp_headers=RespHeaders}) ->
Req#http_req{resp_headers=[{Name, Value}|RespHeaders]}.
-%% @doc Add a body to the response.
-%%
-%% The body set here is ignored if the response is later sent using
-%% anything other than reply/2 or reply/3. The response body is expected
-%% to be a binary or an iolist.
-spec set_resp_body(iodata(), Req) -> Req when Req::req().
set_resp_body(Body, Req) ->
Req#http_req{resp_body=Body}.
-%% @doc Add a body stream function to the response.
-%%
-%% The body set here is ignored if the response is later sent using
-%% anything other than reply/2 or reply/3.
-%%
-%% Setting a response stream function without a length means that the
-%% body will be sent until the connection is closed. Cowboy will make
-%% sure that the connection is closed with no extra step required.
-%%
-%% To inform the client that a body has been sent with this request,
-%% Cowboy will add a "Transfer-Encoding: identity" header to the
-%% response.
-spec set_resp_body_fun(resp_body_fun(), Req) -> Req when Req::req().
set_resp_body_fun(StreamFun, Req) when is_function(StreamFun) ->
Req#http_req{resp_body=StreamFun}.
-%% @doc Add a body function to the response.
-%%
-%% The body set here is ignored if the response is later sent using
-%% anything other than reply/2 or reply/3.
-%%
-%% Cowboy will call the given response stream function after sending the
-%% headers. This function must send the specified number of bytes to the
-%% socket it will receive as argument.
-%%
%% If the body function crashes while writing the response body or writes
%% fewer bytes than declared the behaviour is undefined.
-spec set_resp_body_fun(non_neg_integer(), resp_body_fun(), Req)
@@ -898,12 +771,10 @@ set_resp_body_fun(chunked, StreamFun, Req)
when is_function(StreamFun) ->
Req#http_req{resp_body={chunked, StreamFun}}.
-%% @doc Return whether the given header has been set for the response.
-spec has_resp_header(binary(), req()) -> boolean().
has_resp_header(Name, #http_req{resp_headers=RespHeaders}) ->
lists:keymember(Name, 1, RespHeaders).
-%% @doc Return whether a body has been set for the response.
-spec has_resp_body(req()) -> boolean().
has_resp_body(#http_req{resp_body=RespBody}) when is_function(RespBody) ->
true;
@@ -914,25 +785,21 @@ has_resp_body(#http_req{resp_body={Length, _}}) ->
has_resp_body(#http_req{resp_body=RespBody}) ->
iolist_size(RespBody) > 0.
-%% @doc Remove a header previously set for the response.
-spec delete_resp_header(binary(), Req)
-> Req when Req::req().
delete_resp_header(Name, Req=#http_req{resp_headers=RespHeaders}) ->
RespHeaders2 = lists:keydelete(Name, 1, RespHeaders),
Req#http_req{resp_headers=RespHeaders2}.
-%% @equiv reply(Status, [], [], Req)
-spec reply(cowboy:http_status(), Req) -> {ok, Req} when Req::req().
reply(Status, Req=#http_req{resp_body=Body}) ->
reply(Status, [], Body, Req).
-%% @equiv reply(Status, Headers, [], Req)
-spec reply(cowboy:http_status(), cowboy:http_headers(), Req)
-> {ok, Req} when Req::req().
reply(Status, Headers, Req=#http_req{resp_body=Body}) ->
reply(Status, Headers, Body, Req).
-%% @doc Send a reply to the client.
-spec reply(cowboy:http_status(), cowboy:http_headers(),
iodata() | {non_neg_integer() | resp_body_fun()}, Req)
-> {ok, Req} when Req::req().
@@ -1060,22 +927,16 @@ reply_no_compress(Status, Headers, Body, Req,
Req),
Req2.
-%% @equiv chunked_reply(Status, [], Req)
-spec chunked_reply(cowboy:http_status(), Req) -> {ok, Req} when Req::req().
chunked_reply(Status, Req) ->
chunked_reply(Status, [], Req).
-%% @doc Initiate the sending of a chunked reply to the client.
-%% @see cowboy_req:chunk/2
-spec chunked_reply(cowboy:http_status(), cowboy:http_headers(), Req)
-> {ok, Req} when Req::req().
chunked_reply(Status, Headers, Req) ->
{_, Req2} = chunked_response(Status, Headers, Req),
{ok, Req2}.
-%% @doc Send a chunk of data.
-%%
-%% A chunked reply must have been initiated before calling this function.
-spec chunk(iodata(), req()) -> ok | {error, atom()}.
chunk(_Data, #http_req{method= <<"HEAD">>}) ->
ok;
@@ -1090,8 +951,7 @@ chunk(Data, #http_req{socket=Socket, transport=Transport,
Transport:send(Socket, [integer_to_list(iolist_size(Data), 16),
<<"\r\n">>, Data, <<"\r\n">>]).
-%% @doc Finish the chunked reply.
-%% @todo If ever made public, need to send nothing if HEAD.
+%% If ever made public, need to send nothing if HEAD.
-spec last_chunk(Req) -> Req when Req::req().
last_chunk(Req=#http_req{socket=Socket, transport=cowboy_spdy}) ->
_ = cowboy_spdy:stream_close(Socket),
@@ -1100,8 +960,6 @@ last_chunk(Req=#http_req{socket=Socket, transport=Transport}) ->
_ = Transport:send(Socket, <<"0\r\n\r\n">>),
Req#http_req{resp_state=done}.
-%% @doc Send an upgrade reply.
-%% @private
-spec upgrade_reply(cowboy:http_status(), cowboy:http_headers(), Req)
-> {ok, Req} when Req::req().
upgrade_reply(Status, Headers, Req=#http_req{transport=Transport,
@@ -1112,10 +970,7 @@ upgrade_reply(Status, Headers, Req=#http_req{transport=Transport,
], <<>>, Req),
{ok, Req2#http_req{resp_state=done, resp_headers=[], resp_body= <<>>}}.
-%% @doc Send a reply if one hasn't been sent already.
-%%
%% Meant to be used internally for sending errors after crashes.
-%% @private
-spec maybe_reply(cowboy:http_status(), req()) -> ok.
maybe_reply(Status, Req) ->
receive
@@ -1125,8 +980,6 @@ maybe_reply(Status, Req) ->
ok
end.
-%% @doc Ensure the response has been sent fully.
-%% @private
-spec ensure_response(req(), cowboy:http_status()) -> ok.
%% The response has already been fully sent to the client.
ensure_response(#http_req{resp_state=done}, _) ->
@@ -1148,12 +1001,10 @@ ensure_response(#http_req{}, _) ->
%% Private setter/getter API.
-%% @private
-spec append_buffer(binary(), Req) -> Req when Req::req().
append_buffer(Suffix, Req=#http_req{buffer=Buffer}) ->
Req#http_req{buffer= << Buffer/binary, Suffix/binary >>}.
-%% @private
-spec get(atom(), req()) -> any(); ([atom()], req()) -> any().
get(List, Req) when is_list(List) ->
[g(Atom, Req) || Atom <- List];
@@ -1188,7 +1039,6 @@ g(socket, #http_req{socket=Ret}) -> Ret;
g(transport, #http_req{transport=Ret}) -> Ret;
g(version, #http_req{version=Ret}) -> Ret.
-%% @private
-spec set([{atom(), any()}], Req) -> Req when Req::req().
set([], Req) -> Req;
set([{bindings, Val}|Tail], Req) -> set(Tail, Req#http_req{bindings=Val});
@@ -1218,7 +1068,6 @@ set([{socket, Val}|Tail], Req) -> set(Tail, Req#http_req{socket=Val});
set([{transport, Val}|Tail], Req) -> set(Tail, Req#http_req{transport=Val});
set([{version, Val}|Tail], Req) -> set(Tail, Req#http_req{version=Val}).
-%% @private
-spec set_bindings(cowboy_router:tokens(), cowboy_router:tokens(),
cowboy_router:bindings(), Req) -> Req when Req::req().
set_bindings(HostInfo, PathInfo, Bindings, Req) ->
@@ -1227,13 +1076,6 @@ set_bindings(HostInfo, PathInfo, Bindings, Req) ->
%% Misc API.
-%% @doc Compact the request data by removing all non-system information.
-%%
-%% This essentially removes the host and path info, query string, bindings,
-%% headers and cookies.
-%%
-%% Use it when you really need to save up memory, for example when having
-%% many concurrent long-running connections.
-spec compact(Req) -> Req when Req::req().
compact(Req) ->
Req#http_req{host_info=undefined,
@@ -1241,13 +1083,10 @@ compact(Req) ->
bindings=undefined, headers=[],
p_headers=[], cookies=[]}.
-%% @doc Prevent any further responses.
-%% @private
-spec lock(Req) -> Req when Req::req().
lock(Req) ->
Req#http_req{resp_state=locked}.
-%% @doc Convert the Req object to a list of key/values.
-spec to_list(req()) -> [{atom(), any()}].
to_list(Req) ->
lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))).
@@ -1340,7 +1179,7 @@ response_connection([], Connection) ->
response_connection([{Name, Value}|Tail], Connection) ->
case Name of
<<"connection">> ->
- Tokens = parse_connection_before(Value, []),
+ Tokens = cow_http_hd:parse_connection(Value),
connection_to_atom(Tokens);
_ ->
response_connection(Tail, Connection)
@@ -1382,57 +1221,6 @@ atom_to_connection(keepalive) ->
atom_to_connection(close) ->
<<"close">>.
-%% Optimized parsing functions for the Connection header.
-parse_connection_before(<<>>, Acc) ->
- lists:reverse(Acc);
-parse_connection_before(<< C, Rest/bits >>, Acc)
- when C =:= $,; C =:= $\s; C =:= $\t ->
- parse_connection_before(Rest, Acc);
-parse_connection_before(Buffer, Acc) ->
- parse_connection(Buffer, Acc, <<>>).
-
-%% An evil block of code appeared!
-parse_connection(<<>>, Acc, <<>>) ->
- lists:reverse(Acc);
-parse_connection(<<>>, Acc, Token) ->
- lists:reverse([Token|Acc]);
-parse_connection(<< C, Rest/bits >>, Acc, Token)
- when C =:= $,; C =:= $\s; C =:= $\t ->
- parse_connection_before(Rest, [Token|Acc]);
-parse_connection(<< C, Rest/bits >>, Acc, Token) ->
- case C of
- $A -> parse_connection(Rest, Acc, << Token/binary, $a >>);
- $B -> parse_connection(Rest, Acc, << Token/binary, $b >>);
- $C -> parse_connection(Rest, Acc, << Token/binary, $c >>);
- $D -> parse_connection(Rest, Acc, << Token/binary, $d >>);
- $E -> parse_connection(Rest, Acc, << Token/binary, $e >>);
- $F -> parse_connection(Rest, Acc, << Token/binary, $f >>);
- $G -> parse_connection(Rest, Acc, << Token/binary, $g >>);
- $H -> parse_connection(Rest, Acc, << Token/binary, $h >>);
- $I -> parse_connection(Rest, Acc, << Token/binary, $i >>);
- $J -> parse_connection(Rest, Acc, << Token/binary, $j >>);
- $K -> parse_connection(Rest, Acc, << Token/binary, $k >>);
- $L -> parse_connection(Rest, Acc, << Token/binary, $l >>);
- $M -> parse_connection(Rest, Acc, << Token/binary, $m >>);
- $N -> parse_connection(Rest, Acc, << Token/binary, $n >>);
- $O -> parse_connection(Rest, Acc, << Token/binary, $o >>);
- $P -> parse_connection(Rest, Acc, << Token/binary, $p >>);
- $Q -> parse_connection(Rest, Acc, << Token/binary, $q >>);
- $R -> parse_connection(Rest, Acc, << Token/binary, $r >>);
- $S -> parse_connection(Rest, Acc, << Token/binary, $s >>);
- $T -> parse_connection(Rest, Acc, << Token/binary, $t >>);
- $U -> parse_connection(Rest, Acc, << Token/binary, $u >>);
- $V -> parse_connection(Rest, Acc, << Token/binary, $v >>);
- $W -> parse_connection(Rest, Acc, << Token/binary, $w >>);
- $X -> parse_connection(Rest, Acc, << Token/binary, $x >>);
- $Y -> parse_connection(Rest, Acc, << Token/binary, $y >>);
- $Z -> parse_connection(Rest, Acc, << Token/binary, $z >>);
- C -> parse_connection(Rest, Acc, << Token/binary, C >>)
- end.
-
-%% @doc Walk through a tokens list and return whether
-%% the connection is keepalive or closed.
-%%
%% We don't match on "keep-alive" since it is the default value.
-spec connection_to_atom([binary()]) -> keepalive | close.
connection_to_atom([]) ->
@@ -1505,7 +1293,6 @@ status(B) when is_binary(B) -> B.
%% Tests.
-ifdef(TEST).
-
url_test() ->
{undefined, _} =
url(#http_req{transport=ranch_tcp, host= <<>>, port= undefined,
@@ -1533,19 +1320,7 @@ url_test() ->
path= <<"/path">>, qs= <<"dummy=2785">>, pid=self()}),
ok.
-parse_connection_test_() ->
- %% {Binary, Result}
- Tests = [
- {<<"close">>, [<<"close">>]},
- {<<"ClOsE">>, [<<"close">>]},
- {<<"Keep-Alive">>, [<<"keep-alive">>]},
- {<<"keep-alive, Upgrade">>, [<<"keep-alive">>, <<"upgrade">>]}
- ],
- [{B, fun() -> R = parse_connection_before(B, []) end}
- || {B, R} <- Tests].
-
connection_to_atom_test_() ->
- %% {Tokens, Result}
Tests = [
{[<<"close">>], close},
{[<<"keep-alive">>], keepalive},
@@ -1569,5 +1344,4 @@ merge_headers_test_() ->
{<<"server">>,<<"Cowboy">>}]}
],
[fun() -> Res = merge_headers(L,R) end || {L, R, Res} <- Tests].
-
-endif.
diff --git a/src/cowboy_rest.erl b/src/cowboy_rest.erl
index 30ed15d..63e4dd9 100644
--- a/src/cowboy_rest.erl
+++ b/src/cowboy_rest.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, 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
@@ -12,8 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc REST protocol implementation.
-%%
%% Originally based on the Webmachine Diagram from Alan Dean and
%% Justin Sheehy.
-module(cowboy_rest).
@@ -57,11 +55,6 @@
expires :: undefined | no_call | calendar:datetime()
}).
-%% @doc Upgrade a HTTP request to the REST protocol.
-%%
-%% You do not need to call this function manually. To upgrade to the REST
-%% protocol, you simply need to return <em>{upgrade, protocol, {@module}}</em>
-%% in your <em>cowboy_http_handler:init/3</em> handler function.
-spec upgrade(Req, Env, module(), any())
-> {ok, Req, Env} | {error, 500, Req}
when Req::cowboy_req:req(), Env::cowboy_middleware:env().
diff --git a/src/cowboy_router.erl b/src/cowboy_router.erl
index 16af2d4..ef91c6d 100644
--- a/src/cowboy_router.erl
+++ b/src/cowboy_router.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, 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
@@ -12,7 +12,7 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Routing middleware.
+%% Routing middleware.
%%
%% Resolve the handler to be used for the request based on the
%% routing information found in the <em>dispatch</em> environment value.
@@ -51,8 +51,6 @@
-opaque dispatch_rules() :: [dispatch_rule()].
-export_type([dispatch_rules/0]).
-%% @doc Compile a list of routes into the dispatch format used
-%% by Cowboy's routing.
-spec compile(routes()) -> dispatch_rules().
compile(Routes) ->
compile(Routes, []).
@@ -162,7 +160,6 @@ compile_brackets_split(<< $], Rest/binary >>, Acc, 0) ->
compile_brackets_split(<< C, Rest/binary >>, Acc, N) ->
compile_brackets_split(Rest, << Acc/binary, C >>, N).
-%% @private
-spec execute(Req, Env)
-> {ok, Req, Env} | {error, 400 | 404, Req}
when Req::cowboy_req:req(), Env::cowboy_middleware:env().
@@ -183,7 +180,7 @@ execute(Req, Env) ->
%% Internal.
-%% @doc Match hostname tokens and path tokens against dispatch rules.
+%% Match hostname tokens and path tokens against dispatch rules.
%%
%% It is typically used for matching tokens for the hostname and path of
%% the request against a global dispatch rule for your listener.
@@ -300,7 +297,6 @@ check_constraint({_, int}, Value) ->
check_constraint({_, function, Fun}, Value) ->
Fun(Value).
-%% @doc Split a hostname into a list of tokens.
-spec split_host(binary()) -> tokens().
split_host(Host) ->
split_host(Host, []).
@@ -317,8 +313,6 @@ split_host(Host, Acc) ->
split_host(Rest, [Segment|Acc])
end.
-%% @doc Split a path into a list of path segments.
-%%
%% Following RFC2396, this function may return path segments containing any
%% character, including <em>/</em> if, and only if, a <em>/</em> was escaped
%% and part of a path segment.
@@ -332,9 +326,9 @@ split_path(Path, Acc) ->
try
case binary:match(Path, <<"/">>) of
nomatch when Path =:= <<>> ->
- lists:reverse([cowboy_http:urldecode(S) || S <- Acc]);
+ lists:reverse([cow_qs:urldecode(S) || S <- Acc]);
nomatch ->
- lists:reverse([cowboy_http:urldecode(S) || S <- [Path|Acc]]);
+ lists:reverse([cow_qs:urldecode(S) || S <- [Path|Acc]]);
{Pos, _} ->
<< Segment:Pos/binary, _:8, Rest/bits >> = Path,
split_path(Rest, [Segment|Acc])
@@ -376,9 +370,7 @@ list_match(_List, _Match, _Binds) ->
%% Tests.
-ifdef(TEST).
-
compile_test_() ->
- %% {Routes, Result}
Tests = [
%% Match any host and path.
{[{'_', [{'_', h, o}]}],
@@ -390,14 +382,22 @@ compile_test_() ->
{[<<"path">>, <<"to">>, <<"resource">>], [], hb, ob}]}]},
{[{'_', [{"/path/to/resource/", h, o}]}],
[{'_', [], [{[<<"path">>, <<"to">>, <<"resource">>], [], h, o}]}]},
- {[{'_', [{"/путь/к/ресурсу/", h, o}]}],
- [{'_', [], [{[<<"путь">>, <<"к">>, <<"ресурсу">>], [], h, o}]}]},
+ % Cyrillic from a latin1 encoded file.
+ {[{'_', [{[47,208,191,209,131,209,130,209,140,47,208,186,47,209,128,
+ 208,181,209,129,209,131,209,128,209,129,209,131,47], h, o}]}],
+ [{'_', [], [{[<<208,191,209,131,209,130,209,140>>, <<208,186>>,
+ <<209,128,208,181,209,129,209,131,209,128,209,129,209,131>>],
+ [], h, o}]}]},
{[{"cowboy.example.org.", [{'_', h, o}]}],
[{[<<"org">>, <<"example">>, <<"cowboy">>], [], [{'_', [], h, o}]}]},
{[{".cowboy.example.org", [{'_', h, o}]}],
[{[<<"org">>, <<"example">>, <<"cowboy">>], [], [{'_', [], h, o}]}]},
- {[{"некий.сайт.рф.", [{'_', h, o}]}],
- [{[<<"рф">>, <<"сайт">>, <<"некий">>], [], [{'_', [], h, o}]}]},
+ % Cyrillic from a latin1 encoded file.
+ {[{[208,189,208,181,208,186,208,184,208,185,46,209,129,208,176,
+ 208,185,209,130,46,209,128,209,132,46], [{'_', h, o}]}],
+ [{[<<209,128,209,132>>, <<209,129,208,176,208,185,209,130>>,
+ <<208,189,208,181,208,186,208,184,208,185>>],
+ [], [{'_', [], h, o}]}]},
{[{":subdomain.example.org", [{"/hats/:name/prices", h, o}]}],
[{[<<"org">>, <<"example">>, subdomain], [], [
{[<<"hats">>, name, <<"prices">>], [], h, o}]}]},
@@ -425,7 +425,6 @@ compile_test_() ->
fun() -> Rs = compile(Rt) end} || {Rt, Rs} <- Tests].
split_host_test_() ->
- %% {Host, Result}
Tests = [
{<<"">>, []},
{<<"*">>, [<<"*">>]},
@@ -442,7 +441,6 @@ split_host_test_() ->
[{H, fun() -> R = split_host(H) end} || {H, R} <- Tests].
split_path_test_() ->
- %% {Path, Result, QueryString}
Tests = [
{<<"/">>, []},
{<<"/extend//cowboy">>, [<<"extend">>, <<>>, <<"cowboy">>]},
@@ -473,7 +471,6 @@ match_test_() ->
{'_', [], match_any, []}
]}
],
- %% {Host, Path, Result}
Tests = [
{<<"any">>, <<"/">>, {ok, match_any, [], []}},
{<<"www.any.ninenines.eu">>, <<"/users/42/mails">>,
@@ -506,8 +503,9 @@ match_info_test_() ->
{[<<"eu">>, <<"ninenines">>, '...'], [], [
{'_', [], match_any, []}
]},
- {[<<"рф">>, <<"сайт">>], [], [
- {[<<"путь">>, '...'], [], match_path, []}
+ % Cyrillic from a latin1 encoded file.
+ {[<<209,128,209,132>>, <<209,129,208,176,208,185,209,130>>], [], [
+ {[<<208,191,209,131,209,130,209,140>>, '...'], [], match_path, []}
]}
],
Tests = [
@@ -523,8 +521,10 @@ match_info_test_() ->
{ok, match_path, [], [], undefined, [<<"path_info">>]}},
{<<"www.ninenines.eu">>, <<"/pathinfo/is/next/foo/bar">>,
{ok, match_path, [], [], undefined, [<<"foo">>, <<"bar">>]}},
- {<<"сайт.рф">>, <<"/путь/домой">>,
- {ok, match_path, [], [], undefined, [<<"домой">>]}}
+ % Cyrillic from a latin1 encoded file.
+ {<<209,129,208,176,208,185,209,130,46,209,128,209,132>>,
+ <<47,208,191,209,131,209,130,209,140,47,208,180,208,190,208,188,208,190,208,185>>,
+ {ok, match_path, [], [], undefined, [<<208,180,208,190,208,188,208,190,208,185>>]}}
],
[{lists:flatten(io_lib:format("~p, ~p", [H, P])), fun() ->
R = match(Dispatch, H, P)
@@ -569,5 +569,4 @@ match_same_bindings_test() ->
{error, notfound, path} = match(Dispatch3,
<<"ninenines.eu">>, <<"/path/to">>),
ok.
-
-endif.
diff --git a/src/cowboy_spdy.erl b/src/cowboy_spdy.erl
index dd7882c..ce75419 100644
--- a/src/cowboy_spdy.erl
+++ b/src/cowboy_spdy.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2013-2014, 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
@@ -12,10 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc SPDY protocol handler.
-%%
-%% Note that there is no need to monitor these processes when using Cowboy as
-%% an application as it already supervises them under the listener supervisor.
-module(cowboy_spdy).
%% API.
@@ -37,17 +33,22 @@
%% Internal transport functions.
-export([name/0]).
+-export([messages/0]).
-export([recv/3]).
-export([send/2]).
-export([sendfile/2]).
+-export([setopts/2]).
+
+-type streamid() :: non_neg_integer().
+-type socket() :: {pid(), streamid()}.
-record(child, {
- streamid :: non_neg_integer(),
+ streamid :: streamid(),
pid :: pid(),
input = nofin :: fin | nofin,
in_buffer = <<>> :: binary(),
- is_recv = false :: {true, {non_neg_integer(), pid()},
- pid(), non_neg_integer(), reference()} | false,
+ is_recv = false :: false | {active, socket(), pid()}
+ | {passive, socket(), pid(), non_neg_integer(), reference()},
output = nofin :: fin | nofin
}).
@@ -63,7 +64,7 @@
peer,
zdef,
zinf,
- last_streamid = 0 :: non_neg_integer(),
+ last_streamid = 0 :: streamid(),
children = [] :: [#child{}]
}).
@@ -75,7 +76,6 @@
%% API.
-%% @doc Start a SPDY protocol process.
-spec start_link(any(), inet:socket(), module(), any()) -> {ok, pid()}.
start_link(Ref, Socket, Transport, Opts) ->
proc_lib:start_link(?MODULE, init,
@@ -83,15 +83,13 @@ start_link(Ref, Socket, Transport, Opts) ->
%% Internal.
-%% @doc Faster alternative to proplists:get_value/3.
-%% @private
+%% Faster alternative to proplists:get_value/3.
get_value(Key, Opts, Default) ->
case lists:keyfind(Key, 1, Opts) of
{_, Value} -> Value;
_ -> Default
end.
-%% @private
-spec init(pid(), ranch:ref(), inet:socket(), module(), opts()) -> ok.
init(Parent, Ref, Socket, Transport, Opts) ->
process_flag(trap_exit, true),
@@ -142,15 +140,15 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport,
TRef = erlang:send_after(Timeout, self(),
{recv_timeout, FromSocket}),
loop(replace_child(Child#child{
- is_recv={true, FromSocket, FromPid, Length, TRef}},
+ is_recv={passive, FromSocket, FromPid, Length, TRef}},
State))
end;
{recv_timeout, {Pid, StreamID}}
when Pid =:= self() ->
- Child = #child{is_recv={true, FromSocket, FromPid, _, _}}
+ Child = #child{is_recv={passive, FromSocket, FromPid, _, _}}
= get_child(StreamID, State),
FromPid ! {recv, FromSocket, {error, timeout}},
- loop(replace_child(Child#child{is_recv=false}, State));
+ loop(replace_child(Child#child{is_recv=passive}, State));
{reply, {Pid, StreamID}, Status, Headers}
when Pid =:= self() ->
Child = #child{output=nofin} = get_child(StreamID, State),
@@ -182,6 +180,22 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport,
Child = #child{output=nofin} = get_child(StreamID, State),
data_from_file(State, StreamID, Filepath),
loop(replace_child(Child#child{output=fin}, State));
+ {active, FromSocket = {Pid, StreamID}, FromPid} when Pid =:= self() ->
+ Child = #child{in_buffer=InBuffer, is_recv=false}
+ = get_child(StreamID, State),
+ case InBuffer of
+ <<>> ->
+ loop(replace_child(Child#child{
+ is_recv={active, FromSocket, FromPid}}, State));
+ _ ->
+ FromPid ! {spdy, FromSocket, InBuffer},
+ loop(replace_child(Child#child{in_buffer= <<>>}, State))
+ end;
+ {passive, FromSocket = {Pid, StreamID}, FromPid} when Pid =:= self() ->
+ Child = #child{is_recv=IsRecv} = get_child(StreamID, State),
+ %% Make sure we aren't in the middle of a recv call.
+ case IsRecv of false -> ok; {active, FromSocket, FromPid} -> ok end,
+ loop(replace_child(Child#child{is_recv=false}, State));
{'EXIT', Parent, Reason} ->
exit(Reason);
{'EXIT', Pid, _} ->
@@ -209,6 +223,7 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport,
terminate(State)
end.
+-spec system_continue(_, _, #state{}) -> ok.
system_continue(_, _, State) ->
loop(State).
@@ -216,6 +231,7 @@ system_continue(_, _, State) ->
system_terminate(Reason, _, _, _) ->
exit(Reason).
+-spec system_code_change(Misc, _, _, _) -> {ok, Misc} when Misc::#state{}.
system_code_change(Misc, _, _, _) ->
{ok, Misc}.
@@ -264,11 +280,14 @@ handle_frame(State, {data, StreamID, IsFin, Data}) ->
Data2 = << Buffer/binary, Data/binary >>,
IsFin2 = if IsFin -> fin; true -> nofin end,
Child2 = case IsRecv of
- {true, FromSocket, FromPid, 0, TRef} ->
+ {active, FromSocket, FromPid} ->
+ FromPid ! {spdy, FromSocket, Data},
+ Child#child{input=IsFin2, is_recv=false};
+ {passive, FromSocket, FromPid, 0, TRef} ->
FromPid ! {recv, FromSocket, {ok, Data2}},
cancel_recv_timeout(StreamID, TRef),
Child#child{input=IsFin2, in_buffer= <<>>, is_recv=false};
- {true, FromSocket, FromPid, Length, TRef}
+ {passive, FromSocket, FromPid, Length, TRef}
when byte_size(Data2) >= Length ->
<< Data3:Length/binary, Rest/binary >> = Data2,
FromPid ! {recv, FromSocket, {ok, Data3}},
@@ -358,6 +377,11 @@ delete_child(Pid, State=#state{children=Children}) ->
%% Request process.
+-spec request_init(socket(), {inet:ip_address(), inet:port_number()},
+ cowboy:onrequest_fun(), cowboy:onresponse_fun(),
+ cowboy_middleware:env(), [module()],
+ binary(), binary(), binary(), binary(), [{binary(), binary()}])
+ -> ok.
request_init(FakeSocket, Peer, OnRequest, OnResponse,
Env, Middlewares, Method, Host, Path, Version, Headers) ->
{Host2, Port} = cow_http:parse_fullhost(Host),
@@ -394,7 +418,6 @@ execute(Req, Env, [Middleware|Tail]) ->
cowboy_req:maybe_reply(Status, Req2)
end.
-%% @private
-spec resume(cowboy_middleware:env(), [module()],
module(), module(), [any()]) -> ok.
resume(Env, Tail, Module, Function, Args) ->
@@ -412,6 +435,7 @@ resume(Env, Tail, Module, Function, Args) ->
%% Reply functions used by cowboy_req.
+-spec reply(socket(), binary(), cowboy:http_headers(), iodata()) -> ok.
reply(Socket = {Pid, _}, Status, Headers, Body) ->
_ = case iolist_size(Body) of
0 -> Pid ! {reply, Socket, Status, Headers};
@@ -419,23 +443,33 @@ reply(Socket = {Pid, _}, Status, Headers, Body) ->
end,
ok.
+-spec stream_reply(socket(), binary(), cowboy:http_headers()) -> ok.
stream_reply(Socket = {Pid, _}, Status, Headers) ->
_ = Pid ! {stream_reply, Socket, Status, Headers},
ok.
+-spec stream_data(socket(), iodata()) -> ok.
stream_data(Socket = {Pid, _}, Data) ->
_ = Pid ! {stream_data, Socket, Data},
ok.
+-spec stream_close(socket()) -> ok.
stream_close(Socket = {Pid, _}) ->
_ = Pid ! {stream_close, Socket},
ok.
%% Internal transport functions.
+-spec name() -> spdy.
name() ->
spdy.
+-spec messages() -> {spdy, spdy_closed, spdy_error}.
+messages() ->
+ {spdy, spdy_closed, spdy_error}.
+
+-spec recv(socket(), non_neg_integer(), timeout())
+ -> {ok, binary()} | {error, timeout}.
recv(Socket = {Pid, _}, Length, Timeout) ->
_ = Pid ! {recv, Socket, self(), Length, Timeout},
receive
@@ -443,12 +477,22 @@ recv(Socket = {Pid, _}, Length, Timeout) ->
Ret
end.
+-spec send(socket(), iodata()) -> ok.
send(Socket, Data) ->
stream_data(Socket, Data).
%% We don't wait for the result of the actual sendfile call,
%% therefore we can't know how much was actually sent.
%% This isn't a problem as we don't use this value in Cowboy.
+-spec sendfile(socket(), file:name_all()) -> {ok, undefined}.
sendfile(Socket = {Pid, _}, Filepath) ->
_ = Pid ! {sendfile, Socket, Filepath},
{ok, undefined}.
+
+-spec setopts(inet:socket(), list()) -> ok.
+setopts(Socket = {Pid, _}, [{active, once}]) ->
+ _ = Pid ! {active, Socket, self()},
+ ok;
+setopts(Socket = {Pid, _}, [{active, false}]) ->
+ _ = Pid ! {passive, Socket, self()},
+ ok.
diff --git a/src/cowboy_static.erl b/src/cowboy_static.erl
index 476d1b8..d10ee87 100644
--- a/src/cowboy_static.erl
+++ b/src/cowboy_static.erl
@@ -1,5 +1,5 @@
-%% Copyright (c) 2013, Loïc Hoguin <[email protected]>
%% Copyright (c) 2011, Magnus Klaar <[email protected]>
+%% Copyright (c) 2013-2014, 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
@@ -39,10 +39,11 @@
-type state() :: {binary(), {ok, #file_info{}} | {error, atom()}, extra()}.
+-spec init(_, _, _) -> {upgrade, protocol, cowboy_rest}.
init(_, _, _) ->
{upgrade, protocol, cowboy_rest}.
-%% @doc Resolve the file that will be sent and get its file information.
+%% Resolve the file that will be sent and get its file information.
%% If the handler is configured to manage a directory, check that the
%% requested file is inside the configured directory.
@@ -197,7 +198,7 @@ bad_path_win32_check_test_() ->
end} || P <- Tests].
-endif.
-%% @doc Reject requests that tried to access a file outside
+%% Reject requests that tried to access a file outside
%% the target directory.
-spec malformed_request(Req, State)
@@ -205,7 +206,7 @@ bad_path_win32_check_test_() ->
malformed_request(Req, State) ->
{State =:= error, Req, State}.
-%% @doc Directories, files that can't be accessed at all and
+%% Directories, files that can't be accessed at all and
%% files with no read flag are forbidden.
-spec forbidden(Req, State)
@@ -221,7 +222,7 @@ forbidden(Req, State={_, {ok, #file_info{access=Access}}, _})
forbidden(Req, State) ->
{false, Req, State}.
-%% @doc Detect the mimetype of the file.
+%% Detect the mimetype of the file.
-spec content_types_provided(Req, State)
-> {[{binary(), get_file}], Req, State}
@@ -236,7 +237,7 @@ content_types_provided(Req, State={Path, _, Extra}) ->
{[{Type, get_file}], Req, State}
end.
-%% @doc Assume the resource doesn't exist if it's not a regular file.
+%% Assume the resource doesn't exist if it's not a regular file.
-spec resource_exists(Req, State)
-> {boolean(), Req, State}
@@ -246,7 +247,7 @@ resource_exists(Req, State={_, {ok, #file_info{type=regular}}, _}) ->
resource_exists(Req, State) ->
{false, Req, State}.
-%% @doc Generate an etag for the file.
+%% Generate an etag for the file.
-spec generate_etag(Req, State)
-> {{strong | weak, binary()}, Req, State}
@@ -266,7 +267,7 @@ generate_default_etag(Size, Mtime) ->
{strong, list_to_binary(integer_to_list(
erlang:phash2({Size, Mtime}, 16#ffffffff)))}.
-%% @doc Return the time of last modification of the file.
+%% Return the time of last modification of the file.
-spec last_modified(Req, State)
-> {calendar:datetime(), Req, State}
@@ -274,7 +275,7 @@ generate_default_etag(Size, Mtime) ->
last_modified(Req, State={_, {ok, #file_info{mtime=Modified}}, _}) ->
{Modified, Req, State}.
-%% @doc Stream the file.
+%% Stream the file.
%% @todo Export cowboy_req:resp_body_fun()?
-spec get_file(Req, State)
diff --git a/src/cowboy_sub_protocol.erl b/src/cowboy_sub_protocol.erl
index 26ccd7e..713c3cd 100644
--- a/src/cowboy_sub_protocol.erl
+++ b/src/cowboy_sub_protocol.erl
@@ -1,4 +1,5 @@
%% Copyright (c) 2013, James Fish <[email protected]>
+%% Copyright (c) 2013-2014, 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
@@ -12,21 +13,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Behaviour for sub protocols.
-%%
-%% Only one function needs to be implemented, <em>upgrade/4</em>.
-%% It receives the Req, the environment, the handler that the request has been
-%% routed to and the handler's options. It acts exactly the same as a
-%% middleware, so returns the same values a middleware's execute/2.
-%%
-%% Once the sub protocol has processed the request it should add the result
-%% to the environment. This is done by adding the tuple {result, Value} to the
-%% environment list. To continue handling requests on the current connection the
-%% Value should be the atom ok. Any other value will prevent the processing of
-%% subsequent requests.
-%%
-%% <em>upgrade/4</em> will be called when a handler's init/3 returns
-%% {upgrade, protocol, Module}, where Module is the module of the sub protocol.
-module(cowboy_sub_protocol).
-callback upgrade(Req, Env, module(), any())
diff --git a/src/cowboy_sup.erl b/src/cowboy_sup.erl
index 0e4e59a..cf48595 100644
--- a/src/cowboy_sup.erl
+++ b/src/cowboy_sup.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, 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
@@ -12,26 +12,18 @@
%% 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_sup).
-behaviour(supervisor).
-%% API.
-export([start_link/0]).
-
-%% supervisor.
-export([init/1]).
--define(SUPERVISOR, ?MODULE).
-
-%% API.
-
-spec start_link() -> {ok, pid()}.
start_link() ->
- supervisor:start_link({local, ?SUPERVISOR}, ?MODULE, []).
-
-%% supervisor.
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+-spec init([])
+ -> {ok, {{supervisor:strategy(), 10, 10}, [supervisor:child_spec()]}}.
init([]) ->
Procs = [{cowboy_clock, {cowboy_clock, start_link, []},
permanent, 5000, worker, [cowboy_clock]}],
diff --git a/src/cowboy_websocket.erl b/src/cowboy_websocket.erl
index 40046e1..98e25e6 100644
--- a/src/cowboy_websocket.erl
+++ b/src/cowboy_websocket.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, 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
@@ -12,8 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Websocket protocol implementation.
-%%
%% Cowboy supports versions 7 through 17 of the Websocket drafts.
%% It also supports RFC6455, the proposed standard for Websocket.
-module(cowboy_websocket).
@@ -23,10 +21,7 @@
%% @todo Remove when we support only R16B+.
-compile(nowarn_deprecated_function).
-%% API.
-export([upgrade/4]).
-
-%% Internal.
-export([handler_loop/4]).
-type close_code() :: 1000..4999.
@@ -42,6 +37,8 @@
-type frag_state() :: undefined
| {nofin, opcode(), binary()} | {fin, opcode(), binary()}.
-type rsv() :: << _:3 >>.
+-type terminate_reason() :: {normal | error | remote, atom()}
+ | {remote, close_code(), binary()}.
-record(state, {
env :: cowboy_middleware:env(),
@@ -60,11 +57,6 @@
deflate_state :: undefined | port()
}).
-%% @doc Upgrade an HTTP request to the Websocket protocol.
-%%
-%% You do not need to call this function manually. To upgrade to the Websocket
-%% protocol, you simply need to return <em>{upgrade, protocol, {@module}}</em>
-%% in your <em>cowboy_http_handler:init/3</em> handler function.
-spec upgrade(Req, Env, module(), any())
-> {ok, Req, Env} | {error, 400, Req}
| {suspend, module(), atom(), [any()]}
@@ -213,7 +205,6 @@ handler_loop_timeout(State=#state{timeout=Timeout, timeout_ref=PrevRef}) ->
TRef = erlang:start_timer(Timeout, self(), ?MODULE),
State#state{timeout_ref=TRef}.
-%% @private
-spec handler_loop(#state{}, Req, any(), binary())
-> {ok, Req, cowboy_middleware:env()}
| {suspend, module(), atom(), [any()]}
@@ -730,8 +721,7 @@ websocket_send_many([Frame|Tail], State) ->
{Error, State2} -> {Error, State2}
end.
--spec websocket_close(#state{}, Req, any(),
- {atom(), atom()} | {remote, close_code(), binary()})
+-spec websocket_close(#state{}, Req, any(), terminate_reason())
-> {ok, Req, cowboy_middleware:env()}
when Req::cowboy_req:req().
websocket_close(State=#state{socket=Socket, transport=Transport},
@@ -752,7 +742,7 @@ websocket_close(State=#state{socket=Socket, transport=Transport},
end,
handler_terminate(State, Req, HandlerState, Reason).
--spec handler_terminate(#state{}, Req, any(), atom() | {atom(), atom()})
+-spec handler_terminate(#state{}, Req, any(), terminate_reason())
-> {ok, Req, cowboy_middleware:env()}
when Req::cowboy_req:req().
handler_terminate(#state{env=Env, handler=Handler},
diff --git a/src/cowboy_websocket_handler.erl b/src/cowboy_websocket_handler.erl
index 8bbbc08..177e5f6 100644
--- a/src/cowboy_websocket_handler.erl
+++ b/src/cowboy_websocket_handler.erl
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011-2014, 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
@@ -12,40 +12,6 @@
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-%% @doc Handler for HTTP WebSocket requests.
-%%
-%% WebSocket handlers must implement five callbacks: <em>init/3</em>,
-%% <em>websocket_init/3</em>, <em>websocket_handle/3</em>,
-%% <em>websocket_info/3</em> and <em>websocket_terminate/3</em>.
-%% These callbacks will only be called if the connection is upgraded
-%% to WebSocket in the HTTP handler's <em>init/3</em> callback.
-%% They are then called in that order, although <em>websocket_handle/3</em>
-%% will be called for each packet received, and <em>websocket_info</em>
-%% for each message received.
-%%
-%% <em>websocket_init/3</em> is meant for initialization. It receives
-%% information about the transport and protocol used, along with the handler
-%% options from the dispatch list. You can define a request-wide state here.
-%% If you are going to want to compact the request, you should probably do it
-%% here.
-%%
-%% <em>websocket_handle/3</em> receives the data from the socket. It can reply
-%% something, do nothing or close the connection.
-%%
-%% <em>websocket_info/3</em> receives messages sent to the process. It has
-%% the same reply format as <em>websocket_handle/3</em> described above. Note
-%% that unlike in a <em>gen_server</em>, when <em>websocket_info/3</em>
-%% replies something, it is always to the socket, not to the process that
-%% originated the message.
-%%
-%% <em>websocket_terminate/3</em> is meant for cleaning up. It also receives
-%% the request and the state previously defined, along with a reason for
-%% termination.
-%%
-%% All of <em>websocket_init/3</em>, <em>websocket_handle/3</em> and
-%% <em>websocket_info/3</em> can decide to hibernate the process by adding
-%% an extra element to the returned tuple, containing the atom
-%% <em>hibernate</em>. Doing so helps save memory and improve CPU usage.
-module(cowboy_websocket_handler).
-type opts() :: any().