Skip to content

Commit

Permalink
add junk line skipping for 1XX responses (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
derek-zhou authored Jan 22, 2024
1 parent f190daf commit cba7875
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/hackney_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ execute(#hparser{state=Status, buffer=Buffer}=St, Bin) ->
on_first_line -> parse_first_line(NBuffer, St1, 0);
on_header -> parse_headers(St1);
on_body -> parse_body(St1);
on_trailers -> parse_trailers(St1)
on_trailers -> parse_trailers(St1);
on_junk -> skip_junks(St1)
end.

%% Empty lines must be using \r\n.
Expand Down Expand Up @@ -181,7 +182,13 @@ parse_first_line(Buffer, St=#hparser{type=Type,
{error, bad_request} -> parse_response_line(St)
end;
_ when Type =:= response ->
parse_response_line(St);
case parse_response_line(St) of
{error, bad_request} -> {error, bad_request};
{response, Version, StatusInt, Reason, NState} when StatusInt >= 200 ->
{response, Version, StatusInt, Reason, NState};
{response, _Version, _StatusInt, _Reason, _NState} ->
{more, St#hparser{empty_lines=Empty, state=on_junk}}
end;
_ when Type =:= request ->
parse_request_line(St)
end.
Expand Down Expand Up @@ -343,6 +350,16 @@ parse_header(Line, St) ->
parse_header_value(H) ->
hackney_bstr:trim(H).

skip_junks(#hparser{buffer=Buf}=St) ->
case binary:split(Buf, <<"\r\n">>) of
[<<>>, Rest] ->
{more, St#hparser{buffer=Rest, state=on_first_line}};
[_Line, Rest]->
skip_junks(St#hparser{buffer=Rest});
[Buf] ->
{more, St}
end.

parse_trailers(St) ->
case parse_trailers(St, []) of
{ok, _Trailers, #hparser{buffer=Rest1}} ->
Expand Down

0 comments on commit cba7875

Please sign in to comment.