Browse Source

Replace cowboy_req:path/1 with cowboy_req:raw_path/1

The latter is much more useful than the former, which ends up
being removed.
Loïc Hoguin 12 years ago
parent
commit
79839b7bb5
6 changed files with 22 additions and 29 deletions
  1. 1 2
      include/http.hrl
  2. 12 10
      src/cowboy_protocol.erl
  3. 6 14
      src/cowboy_req.erl
  4. 1 1
      src/cowboy_rest.erl
  5. 1 1
      src/cowboy_websocket.erl
  6. 1 1
      test/rest_forbidden_resource.erl

+ 1 - 2
include/http.hrl

@@ -28,9 +28,8 @@
 	host       = undefined :: undefined | binary(),
 	host_info  = undefined :: undefined | cowboy_dispatcher:tokens(),
 	port       = undefined :: undefined | inet:port_number(),
-	path       = undefined :: undefined | '*' | cowboy_dispatcher:tokens(),
+	path       = undefined :: undefined | binary(),
 	path_info  = undefined :: undefined | cowboy_dispatcher:tokens(),
-	raw_path   = undefined :: undefined | binary(),
 	qs_vals    = undefined :: undefined | list({binary(), binary() | true}),
 	raw_qs     = undefined :: undefined | binary(),
 	bindings   = undefined :: undefined | cowboy_dispatcher:bindings(),

+ 12 - 10
src/cowboy_protocol.erl

@@ -63,6 +63,7 @@
 	timeout :: timeout(),
 	buffer = <<>> :: binary(),
 	host_tokens = undefined :: undefined | cowboy_dispatcher:tokens(),
+	path_tokens = undefined :: undefined | '*' | cowboy_dispatcher:tokens(),
 	hibernate = false :: boolean(),
 	loop_timeout = infinity :: timeout(),
 	loop_timeout_ref :: undefined | reference()
@@ -133,14 +134,15 @@ request({http_request, Method, {abs_path, AbsPath}, Version},
 		req_keepalive=Keepalive, max_keepalive=MaxKeepalive,
 		onresponse=OnResponse, urldecode={URLDecFun, URLDecArg}=URLDec}) ->
 	URLDecode = fun(Bin) -> URLDecFun(Bin, URLDecArg) end,
-	{Path, RawPath, Qs} = cowboy_dispatcher:split_path(AbsPath, URLDecode),
+	{PathTokens, RawPath, Qs}
+		= cowboy_dispatcher:split_path(AbsPath, URLDecode),
 	ConnAtom = if Keepalive < MaxKeepalive -> version_to_connection(Version);
 		true -> close
 	end,
 	parse_header(#http_req{socket=Socket, transport=Transport,
 		connection=ConnAtom, pid=self(), method=Method, version=Version,
-		path=Path, raw_path=RawPath, raw_qs=Qs, onresponse=OnResponse,
-		urldecode=URLDec}, State);
+		path=RawPath, raw_qs=Qs, onresponse=OnResponse, urldecode=URLDec},
+		State#state{path_tokens=PathTokens});
 request({http_request, Method, '*', Version},
 		State=#state{socket=Socket, transport=Transport,
 		req_keepalive=Keepalive, max_keepalive=MaxKeepalive,
@@ -150,8 +152,8 @@ request({http_request, Method, '*', Version},
 	end,
 	parse_header(#http_req{socket=Socket, transport=Transport,
 		connection=ConnAtom, pid=self(), method=Method, version=Version,
-		path='*', raw_path= <<"*">>, raw_qs= <<>>, onresponse=OnResponse,
-		urldecode=URLDec}, State);
+		path= <<"*">>, raw_qs= <<>>, onresponse=OnResponse,
+		urldecode=URLDec}, State#state{path_tokens='*'});
 request({http_request, _Method, _URI, _Version}, State) ->
 	error_terminate(501, State);
 request({http_error, <<"\r\n">>},
@@ -248,13 +250,13 @@ onrequest(Req, State=#state{onrequest=OnRequest}) ->
 	end.
 
 -spec dispatch(#http_req{}, #state{}) -> ok.
-dispatch(Req=#http_req{path=Path},
-		State=#state{dispatch=Dispatch, host_tokens=HostTokens}) ->
-	case cowboy_dispatcher:match(HostTokens, Path, Dispatch) of
+dispatch(Req, State=#state{dispatch=Dispatch,
+		host_tokens=HostTokens, path_tokens=PathTokens}) ->
+	case cowboy_dispatcher:match(HostTokens, PathTokens, 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},
-				host_tokens=undefined});
+				host_tokens=undefined, path_tokens=undefined});
 		{error, notfound, host} ->
 			error_terminate(400, State);
 		{error, notfound, path} ->
@@ -408,7 +410,7 @@ 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, host_tokens=undefined,
+				buffer=Buffer, host_tokens=undefined, path_tokens=undefined,
 				req_empty_lines=0, req_keepalive=Keepalive + 1});
 		_Closed ->
 			terminate(State)

+ 6 - 14
src/cowboy_req.erl

@@ -31,7 +31,6 @@
 -export([port/1]).
 -export([path/1]).
 -export([path_info/1]).
--export([raw_path/1]).
 -export([qs_val/2]).
 -export([qs_val/3]).
 -export([qs_vals/1]).
@@ -147,12 +146,8 @@ host_info(Req) ->
 port(Req) ->
 	{Req#http_req.port, Req}.
 
-%% @doc Return the path segments for the path requested.
-%%
-%% Following RFC2396, this function may return path segments containing any
-%% character, including <em>/</em> if, and only if, a <em>/</em> was escaped
-%% and part of a path segment in the path requested.
--spec path(Req) -> {cowboy_dispatcher:tokens(), Req} when Req::req().
+%% @doc Return the path binary string.
+-spec path(Req) -> {binary(), Req} when Req::req().
 path(Req) ->
 	{Req#http_req.path, Req}.
 
@@ -163,11 +158,6 @@ path(Req) ->
 path_info(Req) ->
 	{Req#http_req.path_info, Req}.
 
-%% @doc Return the raw path directly taken from the request.
--spec raw_path(Req) -> {binary(), Req} when Req::req().
-raw_path(Req) ->
-	{Req#http_req.raw_path, Req}.
-
 %% @equiv qs_val(Name, Req, undefined)
 -spec qs_val(binary(), Req)
 	-> {binary() | true | undefined, Req} when Req::req().
@@ -820,12 +810,14 @@ upgrade_reply(Status, Headers, Req=#http_req{
 
 %% @doc Compact the request data by removing all non-system information.
 %%
-%% This essentially removes the path, query string, bindings and headers.
+%% This essentially removes the host and path info, query string, bindings,
+%% headers and cookies.
+%%
 %% 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_info=undefined, path=undefined,
+	Req#http_req{host_info=undefined,
 		path_info=undefined, qs_vals=undefined,
 		bindings=undefined, headers=[],
 		p_headers=[], cookies=[]}.

+ 1 - 1
src/cowboy_rest.erl

@@ -706,7 +706,7 @@ process_post(Req, State) ->
 is_conflict(Req, State) ->
 	expect(Req, State, is_conflict, false, fun put_resource/2, 409).
 
-put_resource(Req=#http_req{raw_path=RawPath, meta=Meta}, State) ->
+put_resource(Req=#http_req{path=RawPath, meta=Meta}, State) ->
 	Req2 = Req#http_req{meta=[{put_path, RawPath}|Meta]},
 	put_resource(Req2, State, fun is_new_resource/2).
 

+ 1 - 1
src/cowboy_websocket.erl

@@ -156,7 +156,7 @@ upgrade_denied(#http_req{socket=Socket, transport=Transport,
 websocket_handshake(State=#state{version=0, origin=Origin,
 		challenge={Key1, Key2}}, Req=#http_req{socket=Socket,
 		transport=Transport, host=Host, port=Port,
-		raw_path=Path, raw_qs=QS}, HandlerState) ->
+		path=Path, raw_qs=QS}, HandlerState) ->
 	Location = hixie76_location(Transport:name(), Host, Port, Path, QS),
 	{ok, Req2} = cowboy_req:upgrade_reply(
 		<<"101 WebSocket Protocol Handshake">>,

+ 1 - 1
test/rest_forbidden_resource.erl

@@ -27,7 +27,7 @@ post_is_create(Req, State) ->
 	{true, Req, State}.
 
 create_path(Req, State) ->
-	{Path, Req2} = cowboy_req:raw_path(Req),
+	{Path, Req2} = cowboy_req:path(Req),
 	{Path, Req2, State}.
 
 to_text(Req, State) ->