diff options
-rw-r--r-- | ROADMAP.md | 40 | ||||
-rw-r--r-- | src/cowboy.erl | 1 | ||||
-rw-r--r-- | src/cowboy_http.erl | 16 | ||||
-rw-r--r-- | src/cowboy_http_protocol.erl | 5 | ||||
-rw-r--r-- | src/cowboy_http_req.erl | 26 | ||||
-rw-r--r-- | src/cowboy_http_static.erl | 58 | ||||
-rw-r--r-- | test/http_SUITE.erl | 39 |
7 files changed, 122 insertions, 63 deletions
@@ -61,56 +61,20 @@ are not ordered. Tools like curl expect a 100 Continue before sending a request body by default. -* Content-Encoding support. - - Cowboy should be able to send encoded content automatically. - The default should be to encode, but the developer must be - able to override this default either for the whole listener - or just for a single reply. - -* Improve body reading API. - - We want to have various different things for reading the - body. First, there should be raw functions for the different - ways to read the body: basic, transfer encoded, multipart. - Each should allow us to limit the size of what is read. - - On top of these functions there should be two more - advanced functions: one would return the result of parsing - a x-www-form-urlencoded body; the other would parse a - multipart request, save files from the multipart data to - a temporary location and return a proplist of values if any - along with the files details. This behavior is similar to - what is done automatically by PHP with its $_FILES array. - - The advanced functions are of course here for convenience - only and it should be trivial to reimplement them directly - in a Cowboy application if needed. +* Convert the multipart code to stream_body. * Complete the work on Websockets. Now that the Autobahn test suite is available (make inttests), we have a definite way to know whether Cowboy's implementation of Websockets is right. The work can thus be completed. The - remaining tasks are proper UTF8 handling and fragmentation. + remaining tasks are proper UTF8 handling. * SPDY support. While SPDY probably won't be added directly to Cowboy, work has been started on making Cowboy use SPDY. -* Hooks. - - Customizable hooks would allow the developer to extend Cowboy - easily. Two kinds of hooks are needed: before dispatching the - request, and before sending a reply. - - The first would allow us to apply site-wide functions like - authentication or request logging and modify the Req if needed. - - The second is more interesting for response logging or to - filter the replies, for example to send custom error pages. - * Transport upgrades. Some protocols allow an upgrade from TCP to SSL without diff --git a/src/cowboy.erl b/src/cowboy.erl index 7963df2..1097197 100644 --- a/src/cowboy.erl +++ b/src/cowboy.erl @@ -51,7 +51,6 @@ start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) Transport, TransOpts, Protocol, ProtoOpts)). %% @doc Stop a listener identified by <em>Ref</em>. -%% @todo Currently request processes aren't terminated with the listener. -spec stop_listener(any()) -> ok | {error, not_found}. stop_listener(Ref) -> case supervisor:terminate_child(cowboy_sup, {cowboy_listener_sup, Ref}) of diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl index 0289ef3..f8d3314 100644 --- a/src/cowboy_http.erl +++ b/src/cowboy_http.erl @@ -270,7 +270,15 @@ maybe_qparam(Data, Fun) -> fun (<< $;, Rest/binary >>) -> whitespace(Rest, fun (Rest2) -> - qparam(Rest2, Fun) + %% This is a non-strict parsing clause required by some user agents + %% that use the wrong delimiter putting a charset where a qparam is + %% expected. + try qparam(Rest2, Fun) of + Result -> Result + catch + error:function_clause -> + Fun(<<",", Rest2/binary>>, 1000) + end end); (Rest) -> Fun(Rest, 1000) @@ -885,6 +893,12 @@ nonempty_charset_list_test_() -> {<<"iso-8859-5, unicode-1-1;q=0.8">>, [ {<<"iso-8859-5">>, 1000}, {<<"unicode-1-1">>, 800} + ]}, + %% Some user agents send this invalid value for the Accept-Charset header + {<<"ISO-8859-1;utf-8;q=0.7,*;q=0.7">>, [ + {<<"iso-8859-1">>, 1000}, + {<<"utf-8">>, 700}, + {<<"*">>, 700} ]} ], [{V, fun() -> R = nonempty_list(V, fun conneg/2) end} || {V, R} <- Tests]. diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl index 7713a7e..9e1ad88 100644 --- a/src/cowboy_http_protocol.erl +++ b/src/cowboy_http_protocol.erl @@ -438,12 +438,13 @@ ensure_response(#http_req{socket=Socket, transport=Transport, %% Only send an error reply if there is no resp_sent message. -spec error_terminate(cowboy_http:status(), #state{}) -> ok. -error_terminate(Code, State=#state{socket=Socket, transport=Transport}) -> +error_terminate(Code, State=#state{socket=Socket, transport=Transport, + onresponse=OnResponse}) -> receive {cowboy_http_req, resp_sent} -> ok after 0 -> _ = cowboy_http_req:reply(Code, #http_req{ - socket=Socket, transport=Transport, + socket=Socket, transport=Transport, onresponse=OnResponse, connection=close, pid=self(), resp_state=waiting}), ok end, diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl index 411d059..8f1f789 100644 --- a/src/cowboy_http_req.erl +++ b/src/cowboy_http_req.erl @@ -576,21 +576,13 @@ multipart_data(Req=#http_req{body_state=waiting}) -> {{<<"multipart">>, _SubType, Params}, Req2} = parse_header('Content-Type', Req), {_, Boundary} = lists:keyfind(<<"boundary">>, 1, Params), - {Length, Req3=#http_req{buffer=Buffer}} = - parse_header('Content-Length', Req2), - multipart_data(Req3, Length, cowboy_multipart:parser(Boundary), Buffer); + {Length, Req3} = parse_header('Content-Length', Req2), + multipart_data(Req3, Length, {more, cowboy_multipart:parser(Boundary)}); multipart_data(Req=#http_req{body_state={multipart, Length, Cont}}) -> multipart_data(Req, Length, Cont()); multipart_data(Req=#http_req{body_state=done}) -> {eof, Req}. -multipart_data(Req, Length, Parser, Buffer) when byte_size(Buffer) >= Length -> - << Data:Length/binary, Rest/binary >> = Buffer, - multipart_data(Req#http_req{buffer=Rest}, 0, Parser(Data)); -multipart_data(Req, Length, Parser, Buffer) -> - NewLength = Length - byte_size(Buffer), - multipart_data(Req#http_req{buffer= <<>>}, NewLength, Parser(Buffer)). - multipart_data(Req, Length, {headers, Headers, Cont}) -> {{headers, Headers}, Req#http_req{body_state={multipart, Length, Cont}}}; multipart_data(Req, Length, {body, Data, Cont}) -> @@ -601,15 +593,15 @@ multipart_data(Req, 0, eof) -> {eof, Req#http_req{body_state=done}}; multipart_data(Req=#http_req{socket=Socket, transport=Transport}, Length, eof) -> + %% We just want to skip so no need to stream data here. {ok, _Data} = Transport:recv(Socket, Length, 5000), {eof, Req#http_req{body_state=done}}; -multipart_data(Req=#http_req{socket=Socket, transport=Transport}, - Length, {more, Parser}) when Length > 0 -> - case Transport:recv(Socket, 0, 5000) of - {ok, << Data:Length/binary, Buffer/binary >>} -> - multipart_data(Req#http_req{buffer=Buffer}, 0, Parser(Data)); - {ok, Data} -> - multipart_data(Req, Length - byte_size(Data), Parser(Data)) +multipart_data(Req, Length, {more, Parser}) when Length > 0 -> + case stream_body(Req) of + {ok, << Data:Length/binary, Buffer/binary >>, Req2} -> + multipart_data(Req2#http_req{buffer=Buffer}, 0, Parser(Data)); + {ok, Data, Req2} -> + multipart_data(Req2, Length - byte_size(Data), Parser(Data)) end. %% @doc Skip a part returned by the multipart parser. diff --git a/src/cowboy_http_static.erl b/src/cowboy_http_static.erl index 8b9f558..65e2595 100644 --- a/src/cowboy_http_static.erl +++ b/src/cowboy_http_static.erl @@ -31,7 +31,7 @@ %% The handler must be configured with a request path prefix to serve files %% under and the path to a directory to read files from. The request path prefix %% is defined in the path pattern of the cowboy dispatch rule for the handler. -%% The request path pattern must end with a ``'...''' token. +%% The request path pattern must end with a `...' token. %% The directory path can be set to either an absolute or relative path in the %% form of a list or binary string representation of a file system path. A list %% of binary path segments, as is used throughout cowboy, is also a valid @@ -133,6 +133,40 @@ %% [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "), %% {strong, iolist_to_binary(Checksum)}. %% ''' +%% +%% == File configuration == +%% +%% If the file system path being served does not share a common suffix with +%% the request path it is possible to override the file path using the `file' +%% option. The value of this option is expected to be a relative path within +%% the static file directory specified using the `directory' option. +%% The path must be in the form of a list or binary string representation of a +%% file system path. A list of binary path segments, as is used throughout +%% cowboy, is also a valid. +%% +%% When the `file' option is used the same file will be served for all requests +%% matching the cowboy dispatch fule for the handler. It is not necessary to +%% end the request path pattern with a `...' token because the request path +%% will not be used to determine which file to serve from the static directory. +%% +%% === Examples === +%% +%% ``` +%% %% Serve cowboy/priv/www/index.html as http://example.com/ +%% {[], cowboy_http_static, +%% [{directory, {priv_dir, cowboy, [<<"www">>]}} +%% {file, <<"index.html">>}]} +%% +%% %% Serve cowboy/priv/www/page.html under http://example.com/*/page +%% {['*', <<"page">>], cowboy_http_static, +%% [{directory, {priv_dir, cowboy, [<<"www">>]}} +%% {file, <<"page.html">>}]}. +%% +%% %% Always serve cowboy/priv/www/other.html under http://example.com/other +%% {[<<"other">>, '...'], cowboy_http_static, +%% [{directory, {priv_dir, cowboy, [<<"www">>]}} +%% {file, "other.html"}]} +%% ''' -module(cowboy_http_static). %% include files @@ -189,7 +223,10 @@ rest_init(Req, Opts) -> {attributes, Attrs} -> {fun attr_etag_function/2, Attrs}; {_, _}=ETagFunction1 -> ETagFunction1 end, - {Filepath, Req1} = cowboy_http_req:path_info(Req), + {Filepath, Req1} = case lists:keyfind(file, 1, Opts) of + {_, Filepath2} -> {filepath_path(Filepath2), Req}; + false -> cowboy_http_req:path_info(Req) + end, State = case check_path(Filepath) of error -> #state{filepath=error, fileinfo=error, mimetypes=undefined, @@ -341,6 +378,14 @@ directory_path({priv_dir, App, Path}) when is_binary(Path) -> directory_path(Path) -> Path. +%% @private Ensure that a file path is of the same type as a request path. +-spec filepath_path(dirpath()) -> Path::[binary()]. +filepath_path([H|_]=Path) when is_integer(H) -> + filename:split(list_to_binary(Path)); +filepath_path(Path) when is_binary(Path) -> + filename:split(Path); +filepath_path([H|_]=Path) when is_binary(H) -> + Path. %% @private Validate a request path for unsafe characters. %% There is no way to escape special characters in a filesystem path. @@ -459,5 +504,14 @@ directory_path_test_() -> ?_eq("a/b", P("a/b")) ]. +filepath_path_test_() -> + P = fun filepath_path/1, + [?_eq([<<"a">>], P("a")), + ?_eq([<<"a">>], P(<<"a">>)), + ?_eq([<<"a">>], P([<<"a">>])), + ?_eq([<<"a">>, <<"b">>], P("a/b")), + ?_eq([<<"a">>, <<"b">>], P(<<"a/b">>)), + ?_eq([<<"a">>, <<"b">>], P([<<"a">>, <<"b">>])) + ]. -endif. diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl index 97caafe..095d6b5 100644 --- a/test/http_SUITE.erl +++ b/test/http_SUITE.erl @@ -45,6 +45,7 @@ -export([nc_zero/1]). -export([onrequest/1]). -export([onrequest_reply/1]). +-export([onresponse_crash/1]). -export([onresponse_reply/1]). -export([pipeline/1]). -export([rest_keepalive/1]). @@ -58,6 +59,8 @@ -export([static_attribute_etag/1]). -export([static_function_etag/1]). -export([static_mimetypes_function/1]). +-export([static_specify_file/1]). +-export([static_specify_file_catchall/1]). -export([static_test_file/1]). -export([static_test_file_css/1]). -export([stream_body_set_resp/1]). @@ -101,6 +104,8 @@ groups() -> static_attribute_etag, static_function_etag, static_mimetypes_function, + static_specify_file, + static_specify_file_catchall, static_test_file, static_test_file_css, stream_body_set_resp, @@ -116,6 +121,7 @@ groups() -> onrequest_reply ]}, {onresponse, [], [ + onresponse_crash, onresponse_reply ]} ]. @@ -134,7 +140,7 @@ init_per_group(http, Config) -> Port = 33080, Transport = cowboy_tcp_transport, Config1 = init_static_dir(Config), - cowboy:start_listener(http, 100, + {ok, _} = cowboy:start_listener(http, 100, Transport, [{port, Port}], cowboy_http_protocol, [ {dispatch, init_dispatch(Config1)}, @@ -156,7 +162,7 @@ init_per_group(https, Config) -> application:start(crypto), application:start(public_key), application:start(ssl), - {ok,_} = cowboy:start_listener(https, 100, + {ok, _} = cowboy:start_listener(https, 100, Transport, Opts ++ [{port, Port}], cowboy_http_protocol, [ {dispatch, init_dispatch(Config1)}, @@ -241,6 +247,10 @@ init_dispatch(Config) -> {[<<"static_function_etag">>, '...'], cowboy_http_static, [{directory, ?config(static_dir, Config)}, {etag, {fun static_function_etag/2, etag_data}}]}, + {[<<"static_specify_file">>, '...'], cowboy_http_static, + [{directory, ?config(static_dir, Config)}, + {mimetypes, [{<<".css">>, [<<"text/css">>]}]}, + {file, <<"test_file.css">>}]}, {[<<"multipart">>], http_handler_multipart, []}, {[<<"echo">>, <<"body">>], http_handler_echo_body, []}, {[<<"simple">>], rest_simple_resource, []}, @@ -604,6 +614,13 @@ onrequest_hook(Req) -> Req3 end. +onresponse_crash(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/handler_errors?case=init_before_reply", Config), Client), + {ok, 777, Headers, Client3} = cowboy_client:response(Client2), + {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers). + onresponse_reply(Config) -> Client = ?config(client, Config), {ok, Client2} = cowboy_client:request(<<"GET">>, @@ -796,6 +813,24 @@ static_mimetypes_function(Config) -> {<<"content-type">>, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers). +static_specify_file(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/static_specify_file", Config), Client), + {ok, 200, Headers, Client3} = cowboy_client:response(Client2), + {<<"content-type">>, <<"text/css">>} + = lists:keyfind(<<"content-type">>, 1, Headers), + {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3). + +static_specify_file_catchall(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/static_specify_file/none", Config), Client), + {ok, 200, Headers, Client3} = cowboy_client:response(Client2), + {<<"content-type">>, <<"text/css">>} + = lists:keyfind(<<"content-type">>, 1, Headers), + {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3). + static_test_file(Config) -> Client = ?config(client, Config), {ok, Client2} = cowboy_client:request(<<"GET">>, |