aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2011-12-22 21:35:40 +0100
committerLoïc Hoguin <[email protected]>2011-12-22 21:35:40 +0100
commit72d91583b9c1de0e5a05da4de1218679b149921d (patch)
treedd744306c15121a8e7470252b2a6327bf1916393 /src
parenteea6b2ab803cec304e55069d9e79b54615b69b07 (diff)
downloadcowboy-72d91583b9c1de0e5a05da4de1218679b149921d.tar.gz
cowboy-72d91583b9c1de0e5a05da4de1218679b149921d.tar.bz2
cowboy-72d91583b9c1de0e5a05da4de1218679b149921d.zip
Add a max_keepalive HTTP protocol option
Based on the patch by Louis-Philippe Gauthier.
Diffstat (limited to 'src')
-rw-r--r--src/cowboy_http_protocol.erl14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl
index d08c18b..e7a5d2c 100644
--- a/src/cowboy_http_protocol.erl
+++ b/src/cowboy_http_protocol.erl
@@ -50,6 +50,8 @@
urldecode :: {fun((binary(), T) -> binary()), T},
req_empty_lines = 0 :: integer(),
max_empty_lines :: integer(),
+ req_keepalive = 1 :: integer(),
+ max_keepalive :: integer(),
max_line_length :: integer(),
timeout :: timeout(),
buffer = <<>> :: binary(),
@@ -73,6 +75,7 @@ start_link(ListenerPid, Socket, Transport, Opts) ->
init(ListenerPid, Socket, Transport, Opts) ->
Dispatch = proplists:get_value(dispatch, Opts, []),
MaxEmptyLines = proplists:get_value(max_empty_lines, Opts, 5),
+ MaxKeepalive = proplists:get_value(max_keepalive, Opts, infinity),
MaxLineLength = proplists:get_value(max_line_length, Opts, 4096),
Timeout = proplists:get_value(timeout, Opts, 5000),
URLDecDefault = {fun cowboy_http:urldecode/2, crash},
@@ -80,7 +83,8 @@ init(ListenerPid, Socket, Transport, Opts) ->
ok = cowboy:accept_ack(ListenerPid),
wait_request(#state{listener=ListenerPid, socket=Socket, transport=Transport,
dispatch=Dispatch, max_empty_lines=MaxEmptyLines,
- max_line_length=MaxLineLength, timeout=Timeout, urldecode=URLDec}).
+ max_keepalive=MaxKeepalive, max_line_length=MaxLineLength,
+ timeout=Timeout, urldecode=URLDec}).
%% @private
-spec parse_request(#state{}) -> ok | none().
@@ -351,13 +355,15 @@ terminate_request(HandlerState, Req, State) ->
-spec next_request(#http_req{}, #state{}, any()) -> ok | none().
next_request(Req=#http_req{connection=Conn, buffer=Buffer},
- State, HandlerRes) ->
+ State=#state{req_keepalive=Keepalive, max_keepalive=MaxKeepalive},
+ HandlerRes) ->
BodyRes = ensure_body_processed(Req),
RespRes = ensure_response(Req),
case {HandlerRes, BodyRes, RespRes, Conn} of
- {ok, ok, ok, keepalive} ->
+ {ok, ok, ok, keepalive} when Keepalive < MaxKeepalive ->
?MODULE:parse_request(State#state{
- buffer=Buffer, req_empty_lines=0});
+ buffer=Buffer, req_empty_lines=0,
+ req_keepalive=Keepalive + 1});
_Closed ->
terminate(State)
end.