From 457c467d14ff3f3883c7ede4b5059bff9bb7855c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bo=20Bergstr=C3=B6m?= Date: Fri, 5 Jul 2024 12:27:21 +0200 Subject: [PATCH] Prevent the content-length header field from being duplicated in responses. --- src/elli_http.erl | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/elli_http.erl b/src/elli_http.erl index aaa131d..d88557b 100644 --- a/src/elli_http.erl +++ b/src/elli_http.erl @@ -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), @@ -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)}).