mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Do not send empty chunks
User code may sometimes send an empty value which gets understood by the client as being the end of the stream while this was not intended. Ignoring empty values allow making sure the stream isn't ended by mistake.
This commit is contained in:
parent
8d2063bb2e
commit
d2205d9ea6
2 changed files with 7 additions and 2 deletions
|
@ -823,8 +823,11 @@ chunk(Data, #http_req{socket=Socket, transport=Transport,
|
||||||
ok = Transport:send(Socket, Data);
|
ok = Transport:send(Socket, Data);
|
||||||
chunk(Data, #http_req{socket=Socket, transport=Transport,
|
chunk(Data, #http_req{socket=Socket, transport=Transport,
|
||||||
resp_state=chunks}) ->
|
resp_state=chunks}) ->
|
||||||
ok = Transport:send(Socket, [integer_to_list(iolist_size(Data), 16),
|
case iolist_size(Data) of
|
||||||
<<"\r\n">>, Data, <<"\r\n">>]).
|
0 -> ok;
|
||||||
|
Size -> Transport:send(Socket, [integer_to_list(Size, 16),
|
||||||
|
<<"\r\n">>, Data, <<"\r\n">>])
|
||||||
|
end.
|
||||||
|
|
||||||
%% If ever made public, need to send nothing if HEAD.
|
%% If ever made public, need to send nothing if HEAD.
|
||||||
-spec last_chunk(Req) -> Req when Req::req().
|
-spec last_chunk(Req) -> Req when Req::req().
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
|
|
||||||
init(Req, Opts) ->
|
init(Req, Opts) ->
|
||||||
Req2 = cowboy_req:chunked_reply(200, Req),
|
Req2 = cowboy_req:chunked_reply(200, Req),
|
||||||
|
%% Try an empty chunk to make sure the stream doesn't get closed.
|
||||||
|
cowboy_req:chunk([<<>>], Req2),
|
||||||
timer:sleep(100),
|
timer:sleep(100),
|
||||||
cowboy_req:chunk("chunked_handler\r\n", Req2),
|
cowboy_req:chunk("chunked_handler\r\n", Req2),
|
||||||
timer:sleep(100),
|
timer:sleep(100),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue