Skip to content

Commit cba7875

Browse files
authored
add junk line skipping for 1XX responses (#696)
1 parent f190daf commit cba7875

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/hackney_http.erl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ execute(#hparser{state=Status, buffer=Buffer}=St, Bin) ->
152152
on_first_line -> parse_first_line(NBuffer, St1, 0);
153153
on_header -> parse_headers(St1);
154154
on_body -> parse_body(St1);
155-
on_trailers -> parse_trailers(St1)
155+
on_trailers -> parse_trailers(St1);
156+
on_junk -> skip_junks(St1)
156157
end.
157158

158159
%% Empty lines must be using \r\n.
@@ -181,7 +182,13 @@ parse_first_line(Buffer, St=#hparser{type=Type,
181182
{error, bad_request} -> parse_response_line(St)
182183
end;
183184
_ when Type =:= response ->
184-
parse_response_line(St);
185+
case parse_response_line(St) of
186+
{error, bad_request} -> {error, bad_request};
187+
{response, Version, StatusInt, Reason, NState} when StatusInt >= 200 ->
188+
{response, Version, StatusInt, Reason, NState};
189+
{response, _Version, _StatusInt, _Reason, _NState} ->
190+
{more, St#hparser{empty_lines=Empty, state=on_junk}}
191+
end;
185192
_ when Type =:= request ->
186193
parse_request_line(St)
187194
end.
@@ -343,6 +350,16 @@ parse_header(Line, St) ->
343350
parse_header_value(H) ->
344351
hackney_bstr:trim(H).
345352

353+
skip_junks(#hparser{buffer=Buf}=St) ->
354+
case binary:split(Buf, <<"\r\n">>) of
355+
[<<>>, Rest] ->
356+
{more, St#hparser{buffer=Rest, state=on_first_line}};
357+
[_Line, Rest]->
358+
skip_junks(St#hparser{buffer=Rest});
359+
[Buf] ->
360+
{more, St}
361+
end.
362+
346363
parse_trailers(St) ->
347364
case parse_trailers(St, []) of
348365
{ok, _Trailers, #hparser{buffer=Rest1}} ->

0 commit comments

Comments
 (0)