aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cowboy_protocol.erl47
1 files changed, 29 insertions, 18 deletions
diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl
index b1cdc3a..5a20f6b 100644
--- a/src/cowboy_protocol.erl
+++ b/src/cowboy_protocol.erl
@@ -75,25 +75,36 @@ get_value(Key, Opts, Default) ->
-spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok.
init(Ref, Socket, Transport, Opts) ->
- Compress = get_value(compress, Opts, false),
- MaxEmptyLines = get_value(max_empty_lines, Opts, 5),
- MaxHeaderNameLength = get_value(max_header_name_length, Opts, 64),
- MaxHeaderValueLength = get_value(max_header_value_length, Opts, 4096),
- MaxHeaders = get_value(max_headers, Opts, 100),
- MaxKeepalive = get_value(max_keepalive, Opts, 100),
- MaxRequestLineLength = get_value(max_request_line_length, Opts, 4096),
- Middlewares = get_value(middlewares, Opts, [cowboy_router, cowboy_handler]),
- Env = [{listener, Ref}|get_value(env, Opts, [])],
- OnResponse = get_value(onresponse, Opts, undefined),
- Timeout = get_value(timeout, Opts, 5000),
ok = ranch:accept_ack(Ref),
- wait_request(<<>>, #state{socket=Socket, transport=Transport,
- middlewares=Middlewares, compress=Compress, env=Env,
- max_empty_lines=MaxEmptyLines, max_keepalive=MaxKeepalive,
- max_request_line_length=MaxRequestLineLength,
- max_header_name_length=MaxHeaderNameLength,
- max_header_value_length=MaxHeaderValueLength, max_headers=MaxHeaders,
- onresponse=OnResponse, timeout=Timeout, until=until(Timeout)}, 0).
+ Timeout = get_value(timeout, Opts, 5000),
+ Until = until(Timeout),
+ case recv(Socket, Transport, Until) of
+ {ok, Data} ->
+ OnFirstRequest = get_value(onfirstrequest, Opts, undefined),
+ case OnFirstRequest of
+ undefined -> ok;
+ _ -> OnFirstRequest(Ref, Socket, Transport, Opts)
+ end,
+ Compress = get_value(compress, Opts, false),
+ MaxEmptyLines = get_value(max_empty_lines, Opts, 5),
+ MaxHeaderNameLength = get_value(max_header_name_length, Opts, 64),
+ MaxHeaderValueLength = get_value(max_header_value_length, Opts, 4096),
+ MaxHeaders = get_value(max_headers, Opts, 100),
+ MaxKeepalive = get_value(max_keepalive, Opts, 100),
+ MaxRequestLineLength = get_value(max_request_line_length, Opts, 4096),
+ Middlewares = get_value(middlewares, Opts, [cowboy_router, cowboy_handler]),
+ Env = [{listener, Ref}|get_value(env, Opts, [])],
+ OnResponse = get_value(onresponse, Opts, undefined),
+ parse_request(Data, #state{socket=Socket, transport=Transport,
+ middlewares=Middlewares, compress=Compress, env=Env,
+ max_empty_lines=MaxEmptyLines, max_keepalive=MaxKeepalive,
+ max_request_line_length=MaxRequestLineLength,
+ max_header_name_length=MaxHeaderNameLength,
+ max_header_value_length=MaxHeaderValueLength, max_headers=MaxHeaders,
+ onresponse=OnResponse, timeout=Timeout, until=Until}, 0);
+ {error, _} ->
+ terminate(#state{socket=Socket, transport=Transport}) %% @todo ridiculous
+ end.
-spec until(timeout()) -> non_neg_integer() | infinity.
until(infinity) ->