Skip to content

Commit

Permalink
raw headers support
Browse files Browse the repository at this point in the history
  • Loading branch information
jimdigriz committed Sep 28, 2018
1 parent 3c8e6cf commit c32d2d7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
4 changes: 4 additions & 0 deletions doc/src/manual/cowboy_http.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ opts() :: #{
max_header_name_length => non_neg_integer(),
max_header_value_length => non_neg_integer(),
max_headers => non_neg_integer(),
headers_raw => boolean(),
max_keepalive => non_neg_integer(),
max_method_length => non_neg_integer(),
max_request_line_length => non_neg_integer(),
Expand Down Expand Up @@ -77,6 +78,9 @@ max_header_value_length (4096)::
max_headers (100)::
Maximum number of headers allowed per request.

headers_raw (false)::
Populate headers_raw in the request.

max_keepalive (100)::
Maximum number of requests allowed per connection.

Expand Down
36 changes: 28 additions & 8 deletions src/cowboy_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
max_header_name_length => non_neg_integer(),
max_header_value_length => non_neg_integer(),
max_headers => non_neg_integer(),
headers_raw => boolean(),
max_keepalive => non_neg_integer(),
max_method_length => non_neg_integer(),
max_request_line_length => non_neg_integer(),
Expand All @@ -57,6 +58,7 @@
qs = undefined :: binary(),
version = undefined :: cowboy:http_version(),
headers = undefined :: map() | undefined, %% @todo better type than map()
headers_raw = undefined :: binary(),
name = undefined :: binary() | undefined
}).

Expand Down Expand Up @@ -491,8 +493,9 @@ parse_version(_, State, _, _, _, _) ->
'Unsupported HTTP version. (RFC7230 2.6)'}).

before_parse_headers(Rest, State, M, A, P, Q, V) ->
HR = case maps:get(headers_raw, State#state.opts, false) of true -> <<>>; _ -> undefined end,
parse_header(Rest, State#state{in_state=#ps_header{
method=M, authority=A, path=P, qs=Q, version=V}}, #{}).
method=M, authority=A, path=P, qs=Q, version=V, headers_raw=HR}}, #{}).

%% Headers.

Expand Down Expand Up @@ -533,6 +536,9 @@ match_colon(<< _, Rest/bits >>, N) ->
match_colon(_, _) ->
nomatch.

parse_hd_name(<< $:, Rest/bits >>, State=#state{in_state=PS=#ps_header{headers_raw=HR0}}, H, SoFar) when is_binary(HR0) ->
HR = <<HR0/binary, $:>>,
parse_hd_before_value(Rest, State#state{in_state=PS#ps_header{headers_raw=HR}}, H, SoFar);
parse_hd_name(<< $:, Rest/bits >>, State, H, SoFar) ->
parse_hd_before_value(Rest, State, H, SoFar);
parse_hd_name(<< C, _/bits >>, State=#state{in_state=PS}, H, <<>>) when ?IS_WS(C) ->
Expand All @@ -543,12 +549,16 @@ parse_hd_name(<< C, _/bits >>, State=#state{in_state=PS}, H, _) when ?IS_WS(C) -
error_terminate(400, State#state{in_state=PS#ps_header{headers=H}},
{connection_error, protocol_error,
'Whitespace is not allowed between the header name and the colon. (RFC7230 3.2.4)'});
parse_hd_name(<< C, Rest/bits >>, State=#state{in_state=PS=#ps_header{headers_raw=HR0}}, H, SoFar) when is_binary(HR0) ->
HR = <<HR0/binary, C>>,
?LOWER(parse_hd_name, Rest, State#state{in_state=PS#ps_header{headers_raw=HR}}, H, SoFar);
parse_hd_name(<< C, Rest/bits >>, State, H, SoFar) ->
?LOWER(parse_hd_name, Rest, State, H, SoFar).

parse_hd_before_value(<< $\s, Rest/bits >>, S, H, N) ->
parse_hd_before_value(Rest, S, H, N);
parse_hd_before_value(<< $\t, Rest/bits >>, S, H, N) ->
parse_hd_before_value(<< C, Rest/bits >>, S=#state{in_state=PS=#ps_header{headers_raw=HR0}}, H, N) when ?IS_WS(C), is_binary(HR0) ->
HR = <<HR0/binary, C>>,
parse_hd_before_value(Rest, S#state{in_state=PS#ps_header{headers_raw=HR}}, H, N);
parse_hd_before_value(<< C, Rest/bits >>, S, H, N) when ?IS_WS(C) ->
parse_hd_before_value(Rest, S, H, N);
parse_hd_before_value(Buffer, State=#state{opts=Opts, in_state=PS}, H, N) ->
MaxLength = maps:get(max_header_value_length, Opts, 4096),
Expand All @@ -563,15 +573,24 @@ parse_hd_before_value(Buffer, State=#state{opts=Opts, in_state=PS}, H, N) ->
parse_hd_value(Buffer, State, H, N, <<>>)
end.

parse_hd_value(<< $\r, $\n, Rest/bits >>, S, Headers0, Name, SoFar) ->
parse_hd_value(<< $\r, $\n, Rest/bits >>, S=#state{in_state=PS=#ps_header{headers_raw=HR0}}, Headers0, Name, SoFar) ->
Value = clean_value_ws_end(SoFar, byte_size(SoFar) - 1),
Headers = case maps:get(Name, Headers0, undefined) of
undefined -> Headers0#{Name => Value};
%% The cookie header does not use proper HTTP header lists.
Value0 when Name =:= <<"cookie">> -> Headers0#{Name => << Value0/binary, "; ", Value/binary >>};
Value0 -> Headers0#{Name => << Value0/binary, ", ", Value/binary >>}
end,
parse_header(Rest, S, Headers);
if
is_binary(HR0) ->
HR = <<HR0/binary, $\r, $\n>>,
parse_header(Rest, S#state{in_state=PS#ps_header{headers_raw=HR}}, Headers);
true ->
parse_header(Rest, S, Headers)
end;
parse_hd_value(<< C, Rest/bits >>, S=#state{in_state=PS=#ps_header{headers_raw=HR0}}, H, N, SoFar) when is_binary(HR0) ->
HR = <<HR0/binary, C>>,
parse_hd_value(Rest, S#state{in_state=PS#ps_header{headers_raw=HR}}, H, N, << SoFar/binary, C >>);
parse_hd_value(<< C, Rest/bits >>, S, H, N, SoFar) ->
parse_hd_value(Rest, S, H, N, << SoFar/binary, C >>).

Expand Down Expand Up @@ -657,7 +676,7 @@ default_port(_) -> 80.

request(Buffer, State0=#state{ref=Ref, transport=Transport, peer=Peer, sock=Sock, cert=Cert,
in_streamid=StreamID, in_state=
PS=#ps_header{method=Method, path=Path, qs=Qs, version=Version}},
PS=#ps_header{method=Method, path=Path, qs=Qs, version=Version, headers_raw=HeadersRaw}},
Headers0, Host, Port) ->
Scheme = case Transport:secure() of
true -> <<"https">>;
Expand Down Expand Up @@ -709,6 +728,7 @@ request(Buffer, State0=#state{ref=Ref, transport=Transport, peer=Peer, sock=Sock
%% We are transparently taking care of transfer-encodings so
%% the user code has no need to know about it.
headers => maps:remove(<<"transfer-encoding">>, Headers),
headers_raw => HeadersRaw,
has_body => HasBody,
body_length => BodyLength
},
Expand All @@ -728,7 +748,7 @@ request(Buffer, State0=#state{ref=Ref, transport=Transport, peer=Peer, sock=Sock
{true, HTTP2Settings} ->
%% We save the headers in case the upgrade will fail
%% and we need to pass them to cowboy_stream:early_error.
http2_upgrade(State0#state{in_state=PS#ps_header{headers=Headers}},
http2_upgrade(State0#state{in_state=PS#ps_header{headers=Headers,headers_raw=undefined}},
Buffer, HTTP2Settings, Req)
end.

Expand Down

0 comments on commit c32d2d7

Please sign in to comment.