Skip to content

Commit

Permalink
Merge pull request #710 from Polexy/master
Browse files Browse the repository at this point in the history
Body parsing optimization
  • Loading branch information
benoitc authored Mar 1, 2023
2 parents e3872f7 + 5e74354 commit 9a4f16b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/hackney_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ transfer_decode(Data, St=#hparser{
{ok, Chunk, St#hparser{buffer=Rest}};
more ->
{more, St#hparser{buffer=Data}, Buf};
{more, TransferState2} ->
{more, St#hparser{buffer=Data, body_state={stream, TransferDecode, TransferState2, ContentDecode}}, Buf};
{done, Rest} ->
{done, Rest};
{done, Data2, _Rest} ->
Expand Down Expand Up @@ -424,7 +426,7 @@ te_chunked(Data, _) ->
{ok, Chunk, Rest1} ->
{chunk_ok, Chunk, Rest1};
eof ->
more
{more, {byte_size(Rest), Size}}
end;
eof ->
more
Expand Down
10 changes: 8 additions & 2 deletions src/hackney_response.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
-module(hackney_response).

-include("hackney.hrl").
-include("hackney_lib.hrl").

-type response_state() :: start | waiting | on_status | on_headers | on_body.
-export_type([response_state/0]).
Expand Down Expand Up @@ -165,8 +166,8 @@ stream_body1(Error, _Client) ->

-spec stream_body_recv(binary(), #client{})
-> {ok, binary(), #client{}} | {error, term()}.
stream_body_recv(Buffer, Client=#client{version=Version, clen=CLen}) ->
case recv(Client) of
stream_body_recv(Buffer, Client=#client{version=Version, clen=CLen, parser=#hparser{body_state={stream, _, TransferState, _}}}) ->
case recv(Client, TransferState) of
{ok, Data} ->
stream_body(Data, Client);
{error, Reason} ->
Expand Down Expand Up @@ -347,6 +348,11 @@ maybe_close(#client{version={Min,Maj}, headers=Headers, clen=CLen}) ->
recv(#client{transport=Transport, socket=Skt, recv_timeout=Timeout}) ->
Transport:recv(Skt, 0, Timeout).

recv(#client{transport=Transport, socket=Skt, recv_timeout=Timeout}, {_BufSize, undefined}) ->
Transport:recv(Skt, 0, Timeout);
recv(#client{transport=Transport, socket=Skt, recv_timeout=Timeout}, {BufSize, ExpectedSize}) ->
Transport:recv(Skt, ExpectedSize - BufSize, Timeout).

close(#client{socket=nil}=Client) ->
Client#client{state = closed};
close(#client{transport=Transport, socket=Skt}=Client) ->
Expand Down

0 comments on commit 9a4f16b

Please sign in to comment.