mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Remove the trailer header from HTTP/1.1 response if no TE
This commit is contained in:
parent
5229d790fb
commit
5d1cf36358
2 changed files with 23 additions and 20 deletions
|
@ -927,8 +927,8 @@ commands(State0=#state{socket=Socket, transport=Transport, out_state=wait, strea
|
||||||
%% Send response headers and initiate chunked encoding.
|
%% Send response headers and initiate chunked encoding.
|
||||||
commands(State0=#state{socket=Socket, transport=Transport, streams=Streams}, StreamID,
|
commands(State0=#state{socket=Socket, transport=Transport, streams=Streams}, StreamID,
|
||||||
[{headers, StatusCode, Headers0}|Tail]) ->
|
[{headers, StatusCode, Headers0}|Tail]) ->
|
||||||
%% @todo Same as above.
|
%% @todo Same as above (about the last stream in the list).
|
||||||
#stream{version=Version} = lists:keyfind(StreamID, #stream.id, Streams),
|
Stream = #stream{version=Version} = lists:keyfind(StreamID, #stream.id, Streams),
|
||||||
{State1, Headers1} = case {cow_http:status_to_integer(StatusCode), Version} of
|
{State1, Headers1} = case {cow_http:status_to_integer(StatusCode), Version} of
|
||||||
{204, 'HTTP/1.1'} ->
|
{204, 'HTTP/1.1'} ->
|
||||||
{State0#state{out_state=done}, Headers0};
|
{State0#state{out_state=done}, Headers0};
|
||||||
|
@ -939,7 +939,11 @@ commands(State0=#state{socket=Socket, transport=Transport, streams=Streams}, Str
|
||||||
{_, 'HTTP/1.0'} ->
|
{_, 'HTTP/1.0'} ->
|
||||||
{State0#state{out_state=chunked, last_streamid=StreamID}, Headers0}
|
{State0#state{out_state=chunked, last_streamid=StreamID}, Headers0}
|
||||||
end,
|
end,
|
||||||
{State, Headers} = connection(State1, Headers1, StreamID, Version),
|
Headers2 = case stream_te(Stream) of
|
||||||
|
trailers -> Headers1;
|
||||||
|
_ -> maps:remove(<<"trailer">>, Headers1)
|
||||||
|
end,
|
||||||
|
{State, Headers} = connection(State1, Headers2, StreamID, Version),
|
||||||
Transport:send(Socket, cow_http:response(StatusCode, 'HTTP/1.1', headers_to_list(Headers))),
|
Transport:send(Socket, cow_http:response(StatusCode, 'HTTP/1.1', headers_to_list(Headers))),
|
||||||
commands(State, StreamID, Tail);
|
commands(State, StreamID, Tail);
|
||||||
%% Send a response body chunk.
|
%% Send a response body chunk.
|
||||||
|
@ -988,22 +992,7 @@ commands(State0=#state{socket=Socket, transport=Transport, streams=Streams}, Str
|
||||||
%% Send trailers.
|
%% Send trailers.
|
||||||
commands(State=#state{socket=Socket, transport=Transport, streams=Streams}, StreamID,
|
commands(State=#state{socket=Socket, transport=Transport, streams=Streams}, StreamID,
|
||||||
[{trailers, Trailers}|Tail]) ->
|
[{trailers, Trailers}|Tail]) ->
|
||||||
TE = case lists:keyfind(StreamID, #stream.id, Streams) of
|
case stream_te(lists:keyfind(StreamID, #stream.id, Streams)) of
|
||||||
%% HTTP/1.0 doesn't support chunked transfer-encoding.
|
|
||||||
#stream{version='HTTP/1.0'} ->
|
|
||||||
not_chunked;
|
|
||||||
%% No TE header was sent.
|
|
||||||
#stream{te=undefined} ->
|
|
||||||
no_trailers;
|
|
||||||
#stream{te=TE0} ->
|
|
||||||
try cow_http_hd:parse_te(TE0) of
|
|
||||||
{TE1, _} -> TE1
|
|
||||||
catch _:_ ->
|
|
||||||
%% If we can't parse the TE header, assume we can't send trailers.
|
|
||||||
no_trailers
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
case TE of
|
|
||||||
trailers ->
|
trailers ->
|
||||||
Transport:send(Socket, [
|
Transport:send(Socket, [
|
||||||
<<"0\r\n">>,
|
<<"0\r\n">>,
|
||||||
|
@ -1225,6 +1214,20 @@ connection_hd_is_close(Conn) ->
|
||||||
Conns = cow_http_hd:parse_connection(iolist_to_binary(Conn)),
|
Conns = cow_http_hd:parse_connection(iolist_to_binary(Conn)),
|
||||||
lists:member(<<"close">>, Conns).
|
lists:member(<<"close">>, Conns).
|
||||||
|
|
||||||
|
%% HTTP/1.0 doesn't support chunked transfer-encoding.
|
||||||
|
stream_te(#stream{version='HTTP/1.0'}) ->
|
||||||
|
not_chunked;
|
||||||
|
%% No TE header was sent.
|
||||||
|
stream_te(#stream{te=undefined}) ->
|
||||||
|
no_trailers;
|
||||||
|
stream_te(#stream{te=TE0}) ->
|
||||||
|
try cow_http_hd:parse_te(TE0) of
|
||||||
|
{TE1, _} -> TE1
|
||||||
|
catch _:_ ->
|
||||||
|
%% If we can't parse the TE header, assume we can't send trailers.
|
||||||
|
no_trailers
|
||||||
|
end.
|
||||||
|
|
||||||
%% This function is only called when an error occurs on a new stream.
|
%% This function is only called when an error occurs on a new stream.
|
||||||
-spec error_terminate(cowboy:http_status(), #state{}, _) -> no_return().
|
-spec error_terminate(cowboy:http_status(), #state{}, _) -> no_return().
|
||||||
error_terminate(StatusCode, State=#state{ref=Ref, peer=Peer, in_state=StreamState}, Reason) ->
|
error_terminate(StatusCode, State=#state{ref=Ref, peer=Peer, in_state=StreamState}, Reason) ->
|
||||||
|
|
|
@ -1912,7 +1912,7 @@ te_trailers(Config) ->
|
||||||
#{code := 200, headers := RespHeaders} = do_raw(Config, [
|
#{code := 200, headers := RespHeaders} = do_raw(Config, [
|
||||||
"GET /resp/stream_trailers HTTP/1.1\r\n"
|
"GET /resp/stream_trailers HTTP/1.1\r\n"
|
||||||
"Host: localhost\r\n"
|
"Host: localhost\r\n"
|
||||||
"TE: trailer\r\n"
|
"TE: trailers\r\n"
|
||||||
"\r\n"]),
|
"\r\n"]),
|
||||||
{_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, RespHeaders),
|
{_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, RespHeaders),
|
||||||
{_, <<"grpc-status">>} = lists:keyfind(<<"trailer">>, 1, RespHeaders),
|
{_, <<"grpc-status">>} = lists:keyfind(<<"trailer">>, 1, RespHeaders),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue