From b2243aa54438dbea4137960c9dc6f54ac746f429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 26 Sep 2012 14:11:53 +0200 Subject: Optimize cowboy_protocol * #state{} changes are avoided where possible * #state{} is now smaller and use less memory * the Req object is created only after the whole request is parsed * parsing makes use of a single binary match context * external calls are avoided in the critical path * URL fragment is now extracted properly (retrieval API next commit) * argument orders to local functions modified to avoid extra operations * dispatching waits as long as possible before tokenizing host/path * handler opts are no longer shown in the error messages except in init The code may not look as beautiful as it was before. But it really is, for parsing code. The parsing section of the file may be skipped if your eyes start to burn. --- src/cowboy_req.erl | 66 ++++++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 37 deletions(-) (limited to 'src/cowboy_req.erl') diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl index 9cc7760..10c5e96 100644 --- a/src/cowboy_req.erl +++ b/src/cowboy_req.erl @@ -42,7 +42,7 @@ -module(cowboy_req). %% Request API. --export([new/9]). +-export([new/14]). -export([method/1]). -export([version/1]). -export([peer/1]). @@ -103,10 +103,6 @@ -export([ensure_response/2]). %% Private setter/getter API. --export([set_host/4]). --export([set_connection/2]). --export([add_header/3]). --export([set_buffer/2]). -export([set_bindings/4]). -export([get_resp_state/1]). -export([get_buffer/1]). @@ -172,15 +168,36 @@ %% %% This function takes care of setting the owner's pid to self(). %% @private --spec new(inet:socket(), module(), keepalive | close, - binary(), cowboy_http:version(), binary(), binary(), - undefined | fun(), undefined | {fun(), atom()}) +%% @todo Fragment. +-spec new(inet:socket(), module(), binary(), binary(), binary(), binary(), + cowboy_http:version(), cowboy_http:headers(), binary(), + inet:port_number() | undefined, binary(), boolean(), + undefined | cowboy_protocol:onresponse_fun(), + undefined | {fun(), atom()}) -> req(). -new(Socket, Transport, Connection, Method, Version, Path, Qs, +new(Socket, Transport, Method, Path, Query, _Fragment, + Version, Headers, Host, Port, Buffer, CanKeepalive, OnResponse, URLDecode) -> - #http_req{socket=Socket, transport=Transport, connection=Connection, - pid=self(), method=Method, version=Version, path=Path, qs=Qs, - onresponse=OnResponse, urldecode=URLDecode}. + Req = #http_req{socket=Socket, transport=Transport, pid=self(), + method=Method, path=Path, qs=Query, version=Version, + headers=Headers, host=Host, port=Port, buffer=Buffer, + onresponse=OnResponse, urldecode=URLDecode}, + case CanKeepalive of + false -> + Req#http_req{connection=close}; + true -> + case lists:keymember(<<"connection">>, 1, Headers) of + false when Version =:= {1, 1} -> + Req; %% keepalive + false -> + Req#http_req{connection=close}; + true -> + {ok, Tokens, Req2} = parse_header(<<"connection">>, Req), + %% @todo Might want to bring this function into cowboy_req. + Connection = cowboy_http:connection_to_atom(Tokens), + Req2#http_req{connection=Connection} + end + end. %% @doc Return the HTTP method of the request. -spec method(Req) -> {binary(), Req} when Req::req(). @@ -967,31 +984,6 @@ ensure_response(#http_req{socket=Socket, transport=Transport, %% Private setter/getter API. -%% @private --spec set_host(binary(), inet:port_number(), binary(), Req) - -> Req when Req::req(). -set_host(Host, Port, RawHost, Req=#http_req{headers=Headers}) -> - Req#http_req{host=Host, port=Port, headers=[{<<"host">>, RawHost}|Headers]}. - -%% @private --spec set_connection(binary(), Req) -> Req when Req::req(). -set_connection(RawConnection, Req=#http_req{headers=Headers}) -> - Req2 = Req#http_req{headers=[{<<"connection">>, RawConnection}|Headers]}, - {ok, ConnTokens, Req3} = parse_header(<<"connection">>, Req2), - ConnAtom = cowboy_http:connection_to_atom(ConnTokens), - Req3#http_req{connection=ConnAtom}. - -%% @private --spec add_header(binary(), binary(), Req) - -> Req when Req::req(). -add_header(Name, Value, Req=#http_req{headers=Headers}) -> - Req#http_req{headers=[{Name, Value}|Headers]}. - -%% @private --spec set_buffer(binary(), Req) -> Req when Req::req(). -set_buffer(Buffer, Req) -> - Req#http_req{buffer=Buffer}. - %% @private -spec set_bindings(cowboy_dispatcher:tokens(), cowboy_dispatcher:tokens(), cowboy_dispatcher:bindings(), Req) -> Req when Req::req(). -- cgit v1.2.3