From e00131824a10957973d724a5b2cf41b1ab598bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 22 Jan 2025 11:52:27 +0100 Subject: [PATCH] Use cow_deflate in cowboy_decompress_h The function inflate/3 was moved there to make it usable from within Cowlib itself. --- src/cowboy_decompress_h.erl | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/src/cowboy_decompress_h.erl b/src/cowboy_decompress_h.erl index 4e86e230..5a6c302e 100644 --- a/src/cowboy_decompress_h.erl +++ b/src/cowboy_decompress_h.erl @@ -68,7 +68,8 @@ data(StreamID, IsFin, Data, State=#state{next=Next0, enabled=false, read_body_bu fold(Commands, State#state{next=Next, read_body_is_fin=IsFin}); data(StreamID, IsFin, Data, State0=#state{next=Next0, ratio_limit=RatioLimit, inflate=Z, is_reading=true, read_body_buffer=Buffer}) -> - case inflate(Z, RatioLimit, buffer_to_iovec([Data|Buffer])) of + Limit = iolist_size(Data) * RatioLimit, + case cow_deflate:inflate(Z, buffer_to_iovec([Data|Buffer]), Limit) of {error, ErrorType} -> zlib:close(Z), Status = case ErrorType of @@ -236,22 +237,3 @@ do_build_accept_encoding([{ContentCoding, Q}|Tail], Acc0) -> do_build_accept_encoding(Tail, Acc); do_build_accept_encoding([], Acc) -> Acc. - -inflate(Z, RatioLimit, Data) -> - try - {Status, Output} = zlib:safeInflate(Z, Data), - Size = iolist_size(Output), - do_inflate(Z, Size, iolist_size(Data) * RatioLimit, Status, [Output]) - catch - error:data_error -> - {error, data_error} - end. - -do_inflate(_, Size, Limit, _, _) when Size > Limit -> - {error, size_error}; -do_inflate(Z, Size0, Limit, continue, Acc) -> - {Status, Output} = zlib:safeInflate(Z, []), - Size = Size0 + iolist_size(Output), - do_inflate(Z, Size, Limit, Status, [Output | Acc]); -do_inflate(_, _, _, finished, Acc) -> - {ok, iolist_to_binary(lists:reverse(Acc))}.