2013-11-18 20:32:47 +00:00
|
|
|
%% Feel free to use, reuse and abuse the code in this file.
|
|
|
|
|
|
|
|
-module(http_loop_stream_recv).
|
2014-09-26 15:58:44 +03:00
|
|
|
|
|
|
|
-export([init/2]).
|
2013-11-18 20:32:47 +00:00
|
|
|
-export([info/3]).
|
|
|
|
-export([terminate/3]).
|
|
|
|
|
2014-09-26 15:58:44 +03:00
|
|
|
init(Req, _) ->
|
2013-11-18 20:32:47 +00:00
|
|
|
receive after 100 -> ok end,
|
|
|
|
self() ! stream,
|
Allow passing options to sub protocols
Before this commit we had an issue where configuring a
Websocket connection was simply not possible without
doing magic, adding callbacks or extra return values.
The init/2 function only allowed setting hibernate
and timeout options.
After this commit, when switching to a different
type of handler you can either return
{module, Req, State}
or
{module, Req, State, Opts}
where Opts is any value (as far as the sub protocol
interface is concerned) and is ultimately checked
by the custom handlers.
A large protocol like Websocket would accept only
a map there, with many different options, while a
small interface like loop handlers would allow
passing hibernate and nothing else.
For Websocket, hibernate must be set from the
websocket_init/1 callback, because init/2 executes
in a separate process.
Sub protocols now have two callbacks: one with the
Opts value, one without.
The loop handler code was largely reworked and
simplified. It does not need to manage a timeout
or read from the socket anymore, it's the job of
the protocol code. A lot of unnecessary stuff was
therefore removed.
Websocket compression must now be enabled from
the handler options instead of per listener. This
means that a project can have two separate Websocket
handlers with different options. Compression is
still disabled by default, and the idle_timeout
value was changed from inifnity to 60000 (60 seconds),
as that's safer and is also a good value for mobile
devices.
2017-02-18 18:26:20 +01:00
|
|
|
{cowboy_loop, Req, undefined}.
|
2013-11-18 20:32:47 +00:00
|
|
|
|
2014-03-24 14:20:52 +01:00
|
|
|
info(stream, Req, undefined) ->
|
|
|
|
stream(Req, 1, <<>>).
|
|
|
|
|
|
|
|
stream(Req, ID, Acc) ->
|
2016-08-10 11:49:31 +02:00
|
|
|
case cowboy_req:read_body(Req) of
|
Add request body reading options
The options were added to allow developers to fix timeout
issues when reading large bodies. It is also a cleaner and
easier to extend interface.
This commit deprecates the functions init_stream, stream_body
and skip_body which are no longer needed. They will be removed
in 1.0.
The body function can now take an additional argument that is a
list of options. The body_qs, part and part_body functions can
too and simply pass this argument down to the body call.
There are options for disabling the automatic continue reply,
setting a maximum length to be returned (soft limit), setting
the read length and read timeout, and setting the transfer and
content decode functions.
The return value of the body and body_qs have changed slightly.
The body function now works similarly to the part_body function,
in that it returns either an ok or a more tuple depending on
whether there is additional data to be read. The body_qs function
can return a badlength tuple if the body is too big. The default
size has been increased from 16KB to 64KB.
The default read length and timeout have been tweaked and vary
depending on the function called.
The body function will now adequately process chunked bodies,
which means that the body_qs function will too. But this means
that the behavior has changed slightly and your code should be
tested properly when updating your code.
The body and body_qs still accept a length as first argument
for compatibility purpose with older code. Note that this form
is deprecated and will be removed in 1.0. The part and part_body
function, being new and never having been in a release yet, have
this form completely removed in this commit.
Again, while most code should work as-is, you should make sure
that it actually does before pushing this to production.
2014-06-02 23:09:43 +02:00
|
|
|
{ok, <<>>, Req2} ->
|
2014-11-07 19:22:36 +02:00
|
|
|
{stop, cowboy_req:reply(200, Req2), undefined};
|
Add request body reading options
The options were added to allow developers to fix timeout
issues when reading large bodies. It is also a cleaner and
easier to extend interface.
This commit deprecates the functions init_stream, stream_body
and skip_body which are no longer needed. They will be removed
in 1.0.
The body function can now take an additional argument that is a
list of options. The body_qs, part and part_body functions can
too and simply pass this argument down to the body call.
There are options for disabling the automatic continue reply,
setting a maximum length to be returned (soft limit), setting
the read length and read timeout, and setting the transfer and
content decode functions.
The return value of the body and body_qs have changed slightly.
The body function now works similarly to the part_body function,
in that it returns either an ok or a more tuple depending on
whether there is additional data to be read. The body_qs function
can return a badlength tuple if the body is too big. The default
size has been increased from 16KB to 64KB.
The default read length and timeout have been tweaked and vary
depending on the function called.
The body function will now adequately process chunked bodies,
which means that the body_qs function will too. But this means
that the behavior has changed slightly and your code should be
tested properly when updating your code.
The body and body_qs still accept a length as first argument
for compatibility purpose with older code. Note that this form
is deprecated and will be removed in 1.0. The part and part_body
function, being new and never having been in a release yet, have
this form completely removed in this commit.
Again, while most code should work as-is, you should make sure
that it actually does before pushing this to production.
2014-06-02 23:09:43 +02:00
|
|
|
{_, Data, Req2} ->
|
|
|
|
parse_id(Req2, ID, << Acc/binary, Data/binary >>)
|
2014-03-24 14:20:52 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
parse_id(Req, ID, Data) ->
|
|
|
|
case Data of
|
|
|
|
<< ID:32, Rest/bits >> ->
|
|
|
|
parse_id(Req, ID + 1, Rest);
|
|
|
|
_ ->
|
|
|
|
stream(Req, ID, Data)
|
2013-11-18 20:32:47 +00:00
|
|
|
end.
|
|
|
|
|
2014-11-07 19:22:36 +02:00
|
|
|
terminate(stop, _, _) ->
|
2013-11-18 20:32:47 +00:00
|
|
|
ok.
|