aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cowboy.erl1
-rw-r--r--src/cowboy_http.erl16
-rw-r--r--src/cowboy_http_protocol.erl5
-rw-r--r--src/cowboy_http_req.erl26
-rw-r--r--src/cowboy_http_static.erl58
5 files changed, 83 insertions, 23 deletions
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.