Skip to content

Commit

Permalink
Replace cowboy_req:path/1 with cowboy_req:raw_path/1
Browse files Browse the repository at this point in the history
The latter is much more useful than the former, which ends up
being removed.
  • Loading branch information
essen committed Sep 10, 2012
1 parent 6fa734b commit 79839b7
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 29 deletions.
3 changes: 1 addition & 2 deletions include/http.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
22 changes: 12 additions & 10 deletions src/cowboy_protocol.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand All @@ -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">>},
Expand Down Expand Up @@ -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} ->
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 6 additions & 14 deletions src/cowboy_req.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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]).
Expand Down Expand Up @@ -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}.

Expand All @@ -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().
Expand Down Expand Up @@ -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=[]}.
Expand Down
2 changes: 1 addition & 1 deletion src/cowboy_rest.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
2 changes: 1 addition & 1 deletion src/cowboy_websocket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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">>,
Expand Down
2 changes: 1 addition & 1 deletion test/rest_forbidden_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down

0 comments on commit 79839b7

Please sign in to comment.