mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Replace cowboy_req:host/1 with cowboy_req:raw_host/1
The latter is much more useful than the former, which ends up being removed.
This commit is contained in:
parent
e17e18668d
commit
6fa734b487
5 changed files with 30 additions and 32 deletions
|
@ -25,9 +25,8 @@
|
||||||
version = {1, 1} :: cowboy_http:version(),
|
version = {1, 1} :: cowboy_http:version(),
|
||||||
peer = undefined :: undefined |
|
peer = undefined :: undefined |
|
||||||
{inet:ip_address(), inet:port_number()},
|
{inet:ip_address(), inet:port_number()},
|
||||||
host = undefined :: undefined | cowboy_dispatcher:tokens(),
|
host = undefined :: undefined | binary(),
|
||||||
host_info = undefined :: undefined | cowboy_dispatcher:tokens(),
|
host_info = undefined :: undefined | cowboy_dispatcher:tokens(),
|
||||||
raw_host = undefined :: undefined | binary(),
|
|
||||||
port = undefined :: undefined | inet:port_number(),
|
port = undefined :: undefined | inet:port_number(),
|
||||||
path = undefined :: undefined | '*' | cowboy_dispatcher:tokens(),
|
path = undefined :: undefined | '*' | cowboy_dispatcher:tokens(),
|
||||||
path_info = undefined :: undefined | cowboy_dispatcher:tokens(),
|
path_info = undefined :: undefined | cowboy_dispatcher:tokens(),
|
||||||
|
|
|
@ -62,6 +62,7 @@
|
||||||
max_line_length :: integer(),
|
max_line_length :: integer(),
|
||||||
timeout :: timeout(),
|
timeout :: timeout(),
|
||||||
buffer = <<>> :: binary(),
|
buffer = <<>> :: binary(),
|
||||||
|
host_tokens = undefined :: undefined | cowboy_dispatcher:tokens(),
|
||||||
hibernate = false :: boolean(),
|
hibernate = false :: boolean(),
|
||||||
loop_timeout = infinity :: timeout(),
|
loop_timeout = infinity :: timeout(),
|
||||||
loop_timeout_ref :: undefined | reference()
|
loop_timeout_ref :: undefined | reference()
|
||||||
|
@ -184,18 +185,20 @@ wait_header(Req, State=#state{socket=Socket,
|
||||||
-spec header({http_header, integer(), cowboy_http:header(), any(), binary()}
|
-spec header({http_header, integer(), cowboy_http:header(), any(), binary()}
|
||||||
| http_eoh, #http_req{}, #state{}) -> ok.
|
| http_eoh, #http_req{}, #state{}) -> ok.
|
||||||
header({http_header, _I, 'Host', _R, RawHost}, Req=#http_req{
|
header({http_header, _I, 'Host', _R, RawHost}, Req=#http_req{
|
||||||
transport=Transport, host=undefined}, State) ->
|
transport=Transport}, State=#state{host_tokens=undefined}) ->
|
||||||
RawHost2 = cowboy_bstr:to_lower(RawHost),
|
RawHost2 = cowboy_bstr:to_lower(RawHost),
|
||||||
case catch cowboy_dispatcher:split_host(RawHost2) of
|
case catch cowboy_dispatcher:split_host(RawHost2) of
|
||||||
{Host, RawHost3, undefined} ->
|
{HostTokens, RawHost3, undefined} ->
|
||||||
Port = default_port(Transport:name()),
|
Port = default_port(Transport:name()),
|
||||||
parse_header(Req#http_req{
|
parse_header(Req#http_req{
|
||||||
host=Host, raw_host=RawHost3, port=Port,
|
host=RawHost3, port=Port,
|
||||||
headers=[{'Host', RawHost}|Req#http_req.headers]}, State);
|
headers=[{'Host', RawHost}|Req#http_req.headers]},
|
||||||
{Host, RawHost3, Port} ->
|
State#state{host_tokens=HostTokens});
|
||||||
|
{HostTokens, RawHost3, Port} ->
|
||||||
parse_header(Req#http_req{
|
parse_header(Req#http_req{
|
||||||
host=Host, raw_host=RawHost3, port=Port,
|
host=RawHost3, port=Port,
|
||||||
headers=[{'Host', RawHost}|Req#http_req.headers]}, State);
|
headers=[{'Host', RawHost}|Req#http_req.headers]},
|
||||||
|
State#state{host_tokens=HostTokens});
|
||||||
{'EXIT', _Reason} ->
|
{'EXIT', _Reason} ->
|
||||||
error_terminate(400, State)
|
error_terminate(400, State)
|
||||||
end;
|
end;
|
||||||
|
@ -216,14 +219,15 @@ header({http_header, _I, Field, _R, Value}, Req, State) ->
|
||||||
parse_header(Req#http_req{headers=[{Field2, Value}|Req#http_req.headers]},
|
parse_header(Req#http_req{headers=[{Field2, Value}|Req#http_req.headers]},
|
||||||
State);
|
State);
|
||||||
%% The Host header is required in HTTP/1.1.
|
%% The Host header is required in HTTP/1.1.
|
||||||
header(http_eoh, #http_req{version={1, 1}, host=undefined}, State) ->
|
header(http_eoh, #http_req{version={1, 1}},
|
||||||
|
State=#state{host_tokens=undefined}) ->
|
||||||
error_terminate(400, State);
|
error_terminate(400, State);
|
||||||
%% It is however optional in HTTP/1.0.
|
%% It is however optional in HTTP/1.0.
|
||||||
header(http_eoh, Req=#http_req{version={1, 0}, transport=Transport,
|
header(http_eoh, Req=#http_req{version={1, 0}, transport=Transport},
|
||||||
host=undefined}, State=#state{buffer=Buffer}) ->
|
State=#state{buffer=Buffer, host_tokens=undefined}) ->
|
||||||
Port = default_port(Transport:name()),
|
Port = default_port(Transport:name()),
|
||||||
onrequest(Req#http_req{host=[], raw_host= <<>>,
|
onrequest(Req#http_req{host= <<>>, port=Port, buffer=Buffer},
|
||||||
port=Port, buffer=Buffer}, State#state{buffer= <<>>});
|
State#state{buffer= <<>>, host_tokens=[]});
|
||||||
header(http_eoh, Req, State=#state{buffer=Buffer}) ->
|
header(http_eoh, Req, State=#state{buffer=Buffer}) ->
|
||||||
onrequest(Req#http_req{buffer=Buffer}, State#state{buffer= <<>>});
|
onrequest(Req#http_req{buffer=Buffer}, State#state{buffer= <<>>});
|
||||||
header(_Any, _Req, State) ->
|
header(_Any, _Req, State) ->
|
||||||
|
@ -244,12 +248,13 @@ onrequest(Req, State=#state{onrequest=OnRequest}) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec dispatch(#http_req{}, #state{}) -> ok.
|
-spec dispatch(#http_req{}, #state{}) -> ok.
|
||||||
dispatch(Req=#http_req{host=Host, path=Path},
|
dispatch(Req=#http_req{path=Path},
|
||||||
State=#state{dispatch=Dispatch}) ->
|
State=#state{dispatch=Dispatch, host_tokens=HostTokens}) ->
|
||||||
case cowboy_dispatcher:match(Host, Path, Dispatch) of
|
case cowboy_dispatcher:match(HostTokens, Path, Dispatch) of
|
||||||
{ok, Handler, Opts, Binds, HostInfo, PathInfo} ->
|
{ok, Handler, Opts, Binds, HostInfo, PathInfo} ->
|
||||||
handler_init(Req#http_req{host_info=HostInfo, path_info=PathInfo,
|
handler_init(Req#http_req{host_info=HostInfo, path_info=PathInfo,
|
||||||
bindings=Binds}, State#state{handler={Handler, Opts}});
|
bindings=Binds}, State#state{handler={Handler, Opts},
|
||||||
|
host_tokens=undefined});
|
||||||
{error, notfound, host} ->
|
{error, notfound, host} ->
|
||||||
error_terminate(400, State);
|
error_terminate(400, State);
|
||||||
{error, notfound, path} ->
|
{error, notfound, path} ->
|
||||||
|
@ -403,8 +408,8 @@ next_request(Req=#http_req{connection=Conn}, State=#state{
|
||||||
case {HandlerRes, BodyRes, RespRes, Conn} of
|
case {HandlerRes, BodyRes, RespRes, Conn} of
|
||||||
{ok, ok, ok, keepalive} ->
|
{ok, ok, ok, keepalive} ->
|
||||||
?MODULE:parse_request(State#state{
|
?MODULE:parse_request(State#state{
|
||||||
buffer=Buffer, req_empty_lines=0,
|
buffer=Buffer, host_tokens=undefined,
|
||||||
req_keepalive=Keepalive + 1});
|
req_empty_lines=0, req_keepalive=Keepalive + 1});
|
||||||
_Closed ->
|
_Closed ->
|
||||||
terminate(State)
|
terminate(State)
|
||||||
end.
|
end.
|
||||||
|
|
|
@ -28,7 +28,6 @@
|
||||||
-export([peer_addr/1]).
|
-export([peer_addr/1]).
|
||||||
-export([host/1]).
|
-export([host/1]).
|
||||||
-export([host_info/1]).
|
-export([host_info/1]).
|
||||||
-export([raw_host/1]).
|
|
||||||
-export([port/1]).
|
-export([port/1]).
|
||||||
-export([path/1]).
|
-export([path/1]).
|
||||||
-export([path_info/1]).
|
-export([path_info/1]).
|
||||||
|
@ -131,8 +130,8 @@ peer_addr(Req = #http_req{}) ->
|
||||||
end,
|
end,
|
||||||
{PeerAddr, Req3}.
|
{PeerAddr, Req3}.
|
||||||
|
|
||||||
%% @doc Return the tokens for the hostname requested.
|
%% @doc Return the host binary string.
|
||||||
-spec host(Req) -> {cowboy_dispatcher:tokens(), Req} when Req::req().
|
-spec host(Req) -> {binary(), Req} when Req::req().
|
||||||
host(Req) ->
|
host(Req) ->
|
||||||
{Req#http_req.host, Req}.
|
{Req#http_req.host, Req}.
|
||||||
|
|
||||||
|
@ -143,11 +142,6 @@ host(Req) ->
|
||||||
host_info(Req) ->
|
host_info(Req) ->
|
||||||
{Req#http_req.host_info, Req}.
|
{Req#http_req.host_info, Req}.
|
||||||
|
|
||||||
%% @doc Return the raw host directly taken from the request.
|
|
||||||
-spec raw_host(Req) -> {binary(), Req} when Req::req().
|
|
||||||
raw_host(Req) ->
|
|
||||||
{Req#http_req.raw_host, Req}.
|
|
||||||
|
|
||||||
%% @doc Return the port used for this request.
|
%% @doc Return the port used for this request.
|
||||||
-spec port(Req) -> {inet:port_number(), Req} when Req::req().
|
-spec port(Req) -> {inet:port_number(), Req} when Req::req().
|
||||||
port(Req) ->
|
port(Req) ->
|
||||||
|
@ -826,12 +820,12 @@ upgrade_reply(Status, Headers, Req=#http_req{
|
||||||
|
|
||||||
%% @doc Compact the request data by removing all non-system information.
|
%% @doc Compact the request data by removing all non-system information.
|
||||||
%%
|
%%
|
||||||
%% This essentially removes the host, path, query string, bindings and headers.
|
%% This essentially removes the path, query string, bindings and headers.
|
||||||
%% Use it when you really need to save up memory, for example when having
|
%% Use it when you really need to save up memory, for example when having
|
||||||
%% many concurrent long-running connections.
|
%% many concurrent long-running connections.
|
||||||
-spec compact(Req) -> Req when Req::req().
|
-spec compact(Req) -> Req when Req::req().
|
||||||
compact(Req) ->
|
compact(Req) ->
|
||||||
Req#http_req{host=undefined, host_info=undefined, path=undefined,
|
Req#http_req{host_info=undefined, path=undefined,
|
||||||
path_info=undefined, qs_vals=undefined,
|
path_info=undefined, qs_vals=undefined,
|
||||||
bindings=undefined, headers=[],
|
bindings=undefined, headers=[],
|
||||||
p_headers=[], cookies=[]}.
|
p_headers=[], cookies=[]}.
|
||||||
|
|
|
@ -672,7 +672,7 @@ create_path(Req=#http_req{meta=Meta}, State) ->
|
||||||
State2, 303)
|
State2, 303)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
create_path_location(#http_req{transport=Transport, raw_host=Host,
|
create_path_location(#http_req{transport=Transport, host=Host,
|
||||||
port=Port}, Path) ->
|
port=Port}, Path) ->
|
||||||
TransportName = Transport:name(),
|
TransportName = Transport:name(),
|
||||||
<< (create_path_location_protocol(TransportName))/binary, "://",
|
<< (create_path_location_protocol(TransportName))/binary, "://",
|
||||||
|
|
|
@ -155,7 +155,7 @@ upgrade_denied(#http_req{socket=Socket, transport=Transport,
|
||||||
-spec websocket_handshake(#state{}, #http_req{}, any()) -> closed.
|
-spec websocket_handshake(#state{}, #http_req{}, any()) -> closed.
|
||||||
websocket_handshake(State=#state{version=0, origin=Origin,
|
websocket_handshake(State=#state{version=0, origin=Origin,
|
||||||
challenge={Key1, Key2}}, Req=#http_req{socket=Socket,
|
challenge={Key1, Key2}}, Req=#http_req{socket=Socket,
|
||||||
transport=Transport, raw_host=Host, port=Port,
|
transport=Transport, host=Host, port=Port,
|
||||||
raw_path=Path, raw_qs=QS}, HandlerState) ->
|
raw_path=Path, raw_qs=QS}, HandlerState) ->
|
||||||
Location = hixie76_location(Transport:name(), Host, Port, Path, QS),
|
Location = hixie76_location(Transport:name(), Host, Port, Path, QS),
|
||||||
{ok, Req2} = cowboy_req:upgrade_reply(
|
{ok, Req2} = cowboy_req:upgrade_reply(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue