aboutsummaryrefslogtreecommitdiffstats
path: root/test/http_SUITE_data/http_loop_stream_recv.erl
blob: 87113c6d5e0b87c3d156381e93be595ad165ca53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
%% Feel free to use, reuse and abuse the code in this file.

-module(http_loop_stream_recv).
-export([init/3]).
-export([info/3]).
-export([terminate/3]).

init({_, http}, Req, _) ->
	receive after 100 -> ok end,
	self() ! stream,
	{loop, Req, 1, 100}.

info(stream, Req, Id) ->
	case stream_next(Req) of
		{ok, Id, Req2} ->
			info(stream, Req2, Id+1);
		{done, Req2} ->
			{ok, Req3} = cowboy_req:reply(200, Req2),
			{ok, Req3, Id}
	end.

terminate({normal, shutdown}, _, _) ->
	ok.

stream_next(Req) ->
	stream_next(<<>>, Req).

stream_next(Buffer, Req) ->
	case cowboy_req:stream_body(Req) of
		{ok, Packet, Req2} ->
			case <<Buffer/binary, Packet/binary>> of
				<<Id:32>> ->
					{ok, Id, Req2};
				Buffer2 when byte_size(Buffer2) < 4 ->
					stream_next(Buffer2, Req2);
				_InvalidBuffer ->
					{error, invalid_chunk}
			end;
		Other ->
			Other
	end.