mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-15 12:40:25 +00:00
Return "400 Bad Request" instead of crashing process on incorrect queries like /%qq
This commit is contained in:
parent
d3277b08ce
commit
0ce16b3cea
2 changed files with 20 additions and 10 deletions
|
@ -67,7 +67,8 @@
|
||||||
-> {ok, module(), any(), bindings(),
|
-> {ok, module(), any(), bindings(),
|
||||||
HostInfo::undefined | tokens(),
|
HostInfo::undefined | tokens(),
|
||||||
PathInfo::undefined | tokens()}
|
PathInfo::undefined | tokens()}
|
||||||
| {error, notfound, host} | {error, notfound, path}.
|
| {error, notfound, host} | {error, notfound, path}
|
||||||
|
| {error, badrequest, path}.
|
||||||
match([], _, _) ->
|
match([], _, _) ->
|
||||||
{error, notfound, host};
|
{error, notfound, host};
|
||||||
match([{'_', PathMatchs}|_Tail], _, Path) ->
|
match([{'_', PathMatchs}|_Tail], _, Path) ->
|
||||||
|
@ -91,7 +92,7 @@ match(Dispatch, Host, Path) ->
|
||||||
-> {ok, module(), any(), bindings(),
|
-> {ok, module(), any(), bindings(),
|
||||||
HostInfo::undefined | tokens(),
|
HostInfo::undefined | tokens(),
|
||||||
PathInfo::undefined | tokens()}
|
PathInfo::undefined | tokens()}
|
||||||
| {error, notfound, path}.
|
| {error, notfound, path} | {error, badrequest, path}.
|
||||||
match_path([], _, _, _) ->
|
match_path([], _, _, _) ->
|
||||||
{error, notfound, path};
|
{error, notfound, path};
|
||||||
match_path([{'_', Handler, Opts}|_Tail], HostInfo, _, Bindings) ->
|
match_path([{'_', Handler, Opts}|_Tail], HostInfo, _, Bindings) ->
|
||||||
|
@ -106,6 +107,8 @@ match_path([{PathMatch, Handler, Opts}|Tail], HostInfo, Tokens,
|
||||||
{true, PathBinds, PathInfo} ->
|
{true, PathBinds, PathInfo} ->
|
||||||
{ok, Handler, Opts, Bindings ++ PathBinds, HostInfo, PathInfo}
|
{ok, Handler, Opts, Bindings ++ PathBinds, HostInfo, PathInfo}
|
||||||
end;
|
end;
|
||||||
|
match_path(_Dispatch, _HostInfo, badrequest, _Bindings) ->
|
||||||
|
{error, badrequest, path};
|
||||||
match_path(Dispatch, HostInfo, Path, Bindings) ->
|
match_path(Dispatch, HostInfo, Path, Bindings) ->
|
||||||
match_path(Dispatch, HostInfo, split_path(Path), Bindings).
|
match_path(Dispatch, HostInfo, split_path(Path), Bindings).
|
||||||
|
|
||||||
|
@ -138,6 +141,7 @@ split_path(<< $/, Path/bits >>) ->
|
||||||
split_path(Path, []).
|
split_path(Path, []).
|
||||||
|
|
||||||
split_path(Path, Acc) ->
|
split_path(Path, Acc) ->
|
||||||
|
try
|
||||||
case binary:match(Path, <<"/">>) of
|
case binary:match(Path, <<"/">>) of
|
||||||
nomatch when Path =:= <<>> ->
|
nomatch when Path =:= <<>> ->
|
||||||
lists:reverse([cowboy_http:urldecode(S) || S <- Acc]);
|
lists:reverse([cowboy_http:urldecode(S) || S <- Acc]);
|
||||||
|
@ -146,6 +150,10 @@ split_path(Path, Acc) ->
|
||||||
{Pos, _} ->
|
{Pos, _} ->
|
||||||
<< Segment:Pos/binary, _:8, Rest/bits >> = Path,
|
<< Segment:Pos/binary, _:8, Rest/bits >> = Path,
|
||||||
split_path(Rest, [Segment|Acc])
|
split_path(Rest, [Segment|Acc])
|
||||||
|
end
|
||||||
|
catch
|
||||||
|
error:badarg ->
|
||||||
|
badrequest
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec list_match(tokens(), match_rule(), bindings())
|
-spec list_match(tokens(), match_rule(), bindings())
|
||||||
|
|
|
@ -464,6 +464,8 @@ dispatch(Req, State=#state{dispatch=Dispatch}, Host, Path) ->
|
||||||
handler_init(Req2, State, Handler, Opts);
|
handler_init(Req2, State, Handler, Opts);
|
||||||
{error, notfound, host} ->
|
{error, notfound, host} ->
|
||||||
error_terminate(400, State);
|
error_terminate(400, State);
|
||||||
|
{error, badrequest, path} ->
|
||||||
|
error_terminate(400, State);
|
||||||
{error, notfound, path} ->
|
{error, notfound, path} ->
|
||||||
error_terminate(404, State)
|
error_terminate(404, State)
|
||||||
end.
|
end.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue