Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Body parsing optimization #710

Merged
merged 1 commit into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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