Просмотр исходного кода

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.
Loïc Hoguin 12 лет назад
Родитель
Сommit
6fa734b487
5 измененных файлов с 30 добавлено и 32 удалено
  1. 1 2
      include/http.hrl
  2. 23 18
      src/cowboy_protocol.erl
  3. 4 10
      src/cowboy_req.erl
  4. 1 1
      src/cowboy_rest.erl
  5. 1 1
      src/cowboy_websocket.erl

+ 1 - 2
include/http.hrl

@@ -25,9 +25,8 @@
 	version    = {1, 1}    :: cowboy_http:version(),
 	peer       = undefined :: undefined |
 								{inet:ip_address(), inet:port_number()},
-	host       = undefined :: undefined | cowboy_dispatcher:tokens(),
+	host       = undefined :: undefined | binary(),
 	host_info  = undefined :: undefined | cowboy_dispatcher:tokens(),
-	raw_host   = undefined :: undefined | binary(),
 	port       = undefined :: undefined | inet:port_number(),
 	path       = undefined :: undefined | '*' | cowboy_dispatcher:tokens(),
 	path_info  = undefined :: undefined | cowboy_dispatcher:tokens(),

+ 23 - 18
src/cowboy_protocol.erl

@@ -62,6 +62,7 @@
 	max_line_length :: integer(),
 	timeout :: timeout(),
 	buffer = <<>> :: binary(),
+	host_tokens = undefined :: undefined | cowboy_dispatcher:tokens(),
 	hibernate = false :: boolean(),
 	loop_timeout = infinity :: timeout(),
 	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()}
 	| http_eoh, #http_req{}, #state{}) -> ok.
 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),
 	case catch cowboy_dispatcher:split_host(RawHost2) of
-		{Host, RawHost3, undefined} ->
+		{HostTokens, RawHost3, undefined} ->
 			Port = default_port(Transport:name()),
 			parse_header(Req#http_req{
-				host=Host, raw_host=RawHost3, port=Port,
-				headers=[{'Host', RawHost}|Req#http_req.headers]}, State);
-		{Host, RawHost3, Port} ->
+				host=RawHost3, port=Port,
+				headers=[{'Host', RawHost}|Req#http_req.headers]},
+				State#state{host_tokens=HostTokens});
+		{HostTokens, RawHost3, Port} ->
 			parse_header(Req#http_req{
-				host=Host, raw_host=RawHost3, port=Port,
-				headers=[{'Host', RawHost}|Req#http_req.headers]}, State);
+				host=RawHost3, port=Port,
+				headers=[{'Host', RawHost}|Req#http_req.headers]},
+				State#state{host_tokens=HostTokens});
 		{'EXIT', _Reason} ->
 			error_terminate(400, State)
 	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]},
 		State);
 %% 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);
 %% It is however optional in HTTP/1.0.
-header(http_eoh, Req=#http_req{version={1, 0}, transport=Transport,
-		host=undefined}, State=#state{buffer=Buffer}) ->
+header(http_eoh, Req=#http_req{version={1, 0}, transport=Transport},
+		State=#state{buffer=Buffer, host_tokens=undefined}) ->
 	Port = default_port(Transport:name()),
-	onrequest(Req#http_req{host=[], raw_host= <<>>,
-		port=Port, buffer=Buffer}, State#state{buffer= <<>>});
+	onrequest(Req#http_req{host= <<>>, port=Port, buffer=Buffer},
+		State#state{buffer= <<>>, host_tokens=[]});
 header(http_eoh, Req, State=#state{buffer=Buffer}) ->
 	onrequest(Req#http_req{buffer=Buffer}, State#state{buffer= <<>>});
 header(_Any, _Req, State) ->
@@ -244,12 +248,13 @@ onrequest(Req, State=#state{onrequest=OnRequest}) ->
 	end.
 
 -spec dispatch(#http_req{}, #state{}) -> ok.
-dispatch(Req=#http_req{host=Host, path=Path},
-		State=#state{dispatch=Dispatch}) ->
-	case cowboy_dispatcher:match(Host, Path, Dispatch) of
+dispatch(Req=#http_req{path=Path},
+		State=#state{dispatch=Dispatch, host_tokens=HostTokens}) ->
+	case cowboy_dispatcher:match(HostTokens, Path, Dispatch) of
 		{ok, Handler, Opts, Binds, HostInfo, 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_terminate(400, State);
 		{error, notfound, path} ->
@@ -403,8 +408,8 @@ next_request(Req=#http_req{connection=Conn}, State=#state{
 	case {HandlerRes, BodyRes, RespRes, Conn} of
 		{ok, ok, ok, keepalive} ->
 			?MODULE:parse_request(State#state{
-				buffer=Buffer, req_empty_lines=0,
-				req_keepalive=Keepalive + 1});
+				buffer=Buffer, host_tokens=undefined,
+				req_empty_lines=0, req_keepalive=Keepalive + 1});
 		_Closed ->
 			terminate(State)
 	end.

+ 4 - 10
src/cowboy_req.erl

@@ -28,7 +28,6 @@
 -export([peer_addr/1]).
 -export([host/1]).
 -export([host_info/1]).
--export([raw_host/1]).
 -export([port/1]).
 -export([path/1]).
 -export([path_info/1]).
@@ -131,8 +130,8 @@ peer_addr(Req = #http_req{}) ->
 	end,
 	{PeerAddr, Req3}.
 
-%% @doc Return the tokens for the hostname requested.
--spec host(Req) -> {cowboy_dispatcher:tokens(), Req} when Req::req().
+%% @doc Return the host binary string.
+-spec host(Req) -> {binary(), Req} when Req::req().
 host(Req) ->
 	{Req#http_req.host, Req}.
 
@@ -143,11 +142,6 @@ host(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.
 -spec port(Req) -> {inet:port_number(), Req} when Req::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.
 %%
-%% 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
 %% many concurrent long-running connections.
 -spec compact(Req) -> Req when Req::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,
 		bindings=undefined, headers=[],
 		p_headers=[], cookies=[]}.

+ 1 - 1
src/cowboy_rest.erl

@@ -672,7 +672,7 @@ create_path(Req=#http_req{meta=Meta}, State) ->
 				State2, 303)
 	end.
 
-create_path_location(#http_req{transport=Transport, raw_host=Host,
+create_path_location(#http_req{transport=Transport, host=Host,
 		port=Port}, Path) ->
 	TransportName = Transport:name(),
 	<< (create_path_location_protocol(TransportName))/binary, "://",

+ 1 - 1
src/cowboy_websocket.erl

@@ -155,7 +155,7 @@ upgrade_denied(#http_req{socket=Socket, transport=Transport,
 -spec websocket_handshake(#state{}, #http_req{}, any()) -> closed.
 websocket_handshake(State=#state{version=0, origin=Origin,
 		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) ->
 	Location = hixie76_location(Transport:name(), Host, Port, Path, QS),
 	{ok, Req2} = cowboy_req:upgrade_reply(