Skip to content

Commit

Permalink
Prevent the content-length header field from being duplicated in resp…
Browse files Browse the repository at this point in the history
…onses.
  • Loading branch information
beb63 committed Jul 5, 2024
1 parent 1512481 commit 457c467
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/elli_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,22 @@ handle_response(Req, Buffer, {file, ResponseCode, UserHeaders,
case elli_util:normalize_range(Range, Size) of
undefined ->
send_file(Req, ResponseCode,
[{<<"Content-Length">>, Size} |
ResponseHeaders],
add_headers([{<<"Content-Length">>, Size}],
ResponseHeaders),
Filename, {0, 0});
{Offset, Length} ->
ERange = elli_util:encode_range({Offset, Length}, Size),
send_file(Req, 206,
lists:append(ResponseHeaders,
[{<<"Content-Length">>, Length},
{<<"Content-Range">>, ERange}]),
add_headers([{<<"Content-Length">>, Length},
{<<"Content-Range">>, ERange}],
ResponseHeaders),
Filename, {Offset, Length});
invalid_range ->
ERange = elli_util:encode_range(invalid_range, Size),
send_response(Req, 416,
lists:append(ResponseHeaders,
[{<<"Content-Length">>, 0},
{<<"Content-Range">>, ERange}]),
add_headers([{<<"Content-Length">>, 0},
{<<"Content-Range">>, ERange}],
ResponseHeaders),
[])
end,
t(send_end),
Expand Down Expand Up @@ -703,6 +703,11 @@ connection(Req, UserHeaders) ->
[]
end.

add_headers([{Header, _Value} = HeaderField | Tail], Headers) ->
add_headers(Tail, [HeaderField | lists:keydelete(Header, 1, Headers)]);
add_headers([], NewHeaders) ->
NewHeaders.

content_length(Headers, Body)->
?IF(is_header_defined(?CONTENT_LENGTH_HEADER, Headers), [],
{?CONTENT_LENGTH_HEADER, iolist_size(Body)}).
Expand Down

0 comments on commit 457c467

Please sign in to comment.