diff options
Diffstat (limited to 'src/cowboy_protocol.erl')
-rw-r--r-- | src/cowboy_protocol.erl | 63 |
1 files changed, 36 insertions, 27 deletions
diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl index 558dcc3..5a20f6b 100644 --- a/src/cowboy_protocol.erl +++ b/src/cowboy_protocol.erl @@ -55,6 +55,7 @@ }). -include_lib("cowlib/include/cow_inline.hrl"). +-include_lib("cowlib/include/cow_parse.hrl"). %% API. @@ -74,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) -> @@ -264,13 +276,12 @@ match_colon(<< _, Rest/bits >>, N) -> match_colon(_, _) -> nomatch. +parse_hd_name(<< $:, Rest/bits >>, S, M, P, Q, V, H, SoFar) -> + parse_hd_before_value(Rest, S, M, P, Q, V, H, SoFar); +parse_hd_name(<< C, Rest/bits >>, S, M, P, Q, V, H, SoFar) when ?IS_WS(C) -> + parse_hd_name_ws(Rest, S, M, P, Q, V, H, SoFar); parse_hd_name(<< C, Rest/bits >>, S, M, P, Q, V, H, SoFar) -> - case C of - $: -> parse_hd_before_value(Rest, S, M, P, Q, V, H, SoFar); - $\s -> parse_hd_name_ws(Rest, S, M, P, Q, V, H, SoFar); - $\t -> parse_hd_name_ws(Rest, S, M, P, Q, V, H, SoFar); - ?INLINE_LOWERCASE(parse_hd_name, Rest, S, M, P, Q, V, H, SoFar) - end. + ?LOWER(parse_hd_name, Rest, S, M, P, Q, V, H, SoFar). parse_hd_name_ws(<< C, Rest/bits >>, S, M, P, Q, V, H, Name) -> case C of @@ -429,9 +440,7 @@ parse_host(<< $:, Rest/bits >>, false, Acc) -> parse_host(<< $], Rest/bits >>, true, Acc) -> parse_host(Rest, false, << Acc/binary, $] >>); parse_host(<< C, Rest/bits >>, E, Acc) -> - case C of - ?INLINE_LOWERCASE(parse_host, Rest, E, Acc) - end. + ?LOWER(parse_host, Rest, E, Acc). %% End of request parsing. %% |