0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-16 13:10:24 +00:00

cowboy_protocol: accept host using ipv6 literal

This commit is contained in:
YAMAMOTO Takashi 2013-08-24 02:13:44 +09:00
parent d68b3de9d9
commit 77f7427b41
2 changed files with 37 additions and 33 deletions

View file

@ -54,7 +54,7 @@
%% Internal. %% Internal.
-export([init/4]). -export([init/4]).
-export([parse_request/3]). -export([parse_request/3]).
-export([parse_host/2]). -export([parse_host/3]).
-export([resume/6]). -export([resume/6]).
-type opts() :: [{compress, boolean()} -type opts() :: [{compress, boolean()}
@ -426,7 +426,7 @@ request(B, State=#state{transport=Transport}, M, P, Q, Version, Headers) ->
request(B, State, M, P, Q, Version, Headers, request(B, State, M, P, Q, Version, Headers,
<<>>, default_port(Transport:name())); <<>>, default_port(Transport:name()));
{_, RawHost} -> {_, RawHost} ->
case catch parse_host(RawHost, <<>>) of case catch parse_host(RawHost, false, <<>>) of
{'EXIT', _} -> {'EXIT', _} ->
error_terminate(400, State); error_terminate(400, State);
{Host, undefined} -> {Host, undefined} ->
@ -443,39 +443,43 @@ default_port(ssl) -> 443;
default_port(_) -> 80. default_port(_) -> 80.
%% Another hurtful block of code. :) %% Another hurtful block of code. :)
parse_host(<<>>, Acc) -> parse_host(<< $[, Rest/bits >>, false, <<>>) ->
parse_host(Rest, true, << $[ >>);
parse_host(<<>>, false, Acc) ->
{Acc, undefined}; {Acc, undefined};
parse_host(<< $:, Rest/bits >>, Acc) -> parse_host(<< $:, Rest/bits >>, false, Acc) ->
{Acc, list_to_integer(binary_to_list(Rest))}; {Acc, list_to_integer(binary_to_list(Rest))};
parse_host(<< C, Rest/bits >>, Acc) -> parse_host(<< $], Rest/bits >>, true, Acc) ->
parse_host(Rest, false, << Acc/binary, $] >>);
parse_host(<< C, Rest/bits >>, E, Acc) ->
case C of case C of
$A -> parse_host(Rest, << Acc/binary, $a >>); $A -> parse_host(Rest, E, << Acc/binary, $a >>);
$B -> parse_host(Rest, << Acc/binary, $b >>); $B -> parse_host(Rest, E, << Acc/binary, $b >>);
$C -> parse_host(Rest, << Acc/binary, $c >>); $C -> parse_host(Rest, E, << Acc/binary, $c >>);
$D -> parse_host(Rest, << Acc/binary, $d >>); $D -> parse_host(Rest, E, << Acc/binary, $d >>);
$E -> parse_host(Rest, << Acc/binary, $e >>); $E -> parse_host(Rest, E, << Acc/binary, $e >>);
$F -> parse_host(Rest, << Acc/binary, $f >>); $F -> parse_host(Rest, E, << Acc/binary, $f >>);
$G -> parse_host(Rest, << Acc/binary, $g >>); $G -> parse_host(Rest, E, << Acc/binary, $g >>);
$H -> parse_host(Rest, << Acc/binary, $h >>); $H -> parse_host(Rest, E, << Acc/binary, $h >>);
$I -> parse_host(Rest, << Acc/binary, $i >>); $I -> parse_host(Rest, E, << Acc/binary, $i >>);
$J -> parse_host(Rest, << Acc/binary, $j >>); $J -> parse_host(Rest, E, << Acc/binary, $j >>);
$K -> parse_host(Rest, << Acc/binary, $k >>); $K -> parse_host(Rest, E, << Acc/binary, $k >>);
$L -> parse_host(Rest, << Acc/binary, $l >>); $L -> parse_host(Rest, E, << Acc/binary, $l >>);
$M -> parse_host(Rest, << Acc/binary, $m >>); $M -> parse_host(Rest, E, << Acc/binary, $m >>);
$N -> parse_host(Rest, << Acc/binary, $n >>); $N -> parse_host(Rest, E, << Acc/binary, $n >>);
$O -> parse_host(Rest, << Acc/binary, $o >>); $O -> parse_host(Rest, E, << Acc/binary, $o >>);
$P -> parse_host(Rest, << Acc/binary, $p >>); $P -> parse_host(Rest, E, << Acc/binary, $p >>);
$Q -> parse_host(Rest, << Acc/binary, $q >>); $Q -> parse_host(Rest, E, << Acc/binary, $q >>);
$R -> parse_host(Rest, << Acc/binary, $r >>); $R -> parse_host(Rest, E, << Acc/binary, $r >>);
$S -> parse_host(Rest, << Acc/binary, $s >>); $S -> parse_host(Rest, E, << Acc/binary, $s >>);
$T -> parse_host(Rest, << Acc/binary, $t >>); $T -> parse_host(Rest, E, << Acc/binary, $t >>);
$U -> parse_host(Rest, << Acc/binary, $u >>); $U -> parse_host(Rest, E, << Acc/binary, $u >>);
$V -> parse_host(Rest, << Acc/binary, $v >>); $V -> parse_host(Rest, E, << Acc/binary, $v >>);
$W -> parse_host(Rest, << Acc/binary, $w >>); $W -> parse_host(Rest, E, << Acc/binary, $w >>);
$X -> parse_host(Rest, << Acc/binary, $x >>); $X -> parse_host(Rest, E, << Acc/binary, $x >>);
$Y -> parse_host(Rest, << Acc/binary, $y >>); $Y -> parse_host(Rest, E, << Acc/binary, $y >>);
$Z -> parse_host(Rest, << Acc/binary, $z >>); $Z -> parse_host(Rest, E, << Acc/binary, $z >>);
_ -> parse_host(Rest, << Acc/binary, C >>) _ -> parse_host(Rest, E, << Acc/binary, C >>)
end. end.
%% End of request parsing. %% End of request parsing.

View file

@ -474,7 +474,7 @@ request_init(Parent, StreamID, Peer,
#special_headers{method=Method, path=Path, version=Version, #special_headers{method=Method, path=Path, version=Version,
host=Host}) -> host=Host}) ->
Version2 = parse_version(Version), Version2 = parse_version(Version),
{Host2, Port} = cowboy_protocol:parse_host(Host, <<>>), {Host2, Port} = cowboy_protocol:parse_host(Host, false, <<>>),
{Path2, Query} = parse_path(Path, <<>>), {Path2, Query} = parse_path(Path, <<>>),
Req = cowboy_req:new({Parent, StreamID}, ?MODULE, Peer, Req = cowboy_req:new({Parent, StreamID}, ?MODULE, Peer,
Method, Path2, Query, Version2, Headers, Method, Path2, Query, Version2, Headers,