From 79839b7bb5ae3aef77eb7ab704efa6168927845f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 10 Sep 2012 12:25:07 +0200 Subject: 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. --- src/cowboy_protocol.erl | 22 ++++++++++++---------- src/cowboy_req.erl | 20 ++++++-------------- src/cowboy_rest.erl | 2 +- src/cowboy_websocket.erl | 2 +- 4 files changed, 20 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl index 6bb8ff5..65012a5 100644 --- a/src/cowboy_protocol.erl +++ b/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) diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl index 54693f7..fd1399d 100644 --- a/src/cowboy_req.erl +++ b/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 / if, and only if, a / 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=[]}. diff --git a/src/cowboy_rest.erl b/src/cowboy_rest.erl index be2baaf..2f3a2e4 100644 --- a/src/cowboy_rest.erl +++ b/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). diff --git a/src/cowboy_websocket.erl b/src/cowboy_websocket.erl index 4604e39..b669ef7 100644 --- a/src/cowboy_websocket.erl +++ b/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">>, -- cgit v1.2.3