-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathflow_control_handler.erl
54 lines (44 loc) · 1.73 KB
/
flow_control_handler.erl
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
42
43
44
45
46
47
48
49
50
51
52
53
54
-module(flow_control_handler).
-include_lib("chatterbox/include/http2.hrl").
-define(SEND_BYTES, 68).
-behaviour(h2_stream).
-export([
init/3,
on_receive_request_headers/2,
on_send_push_promise/2,
on_receive_request_data/2,
on_request_end_stream/1
]).
-record(state, {conn_pid :: pid(),
stream_id :: stream_id()
}).
-spec init(pid(), stream_id(), list()) -> {ok, any()}.
init(ConnPid, StreamId, _Opts) ->
{ok, #state{conn_pid=ConnPid,
stream_id=StreamId}}.
-spec on_receive_request_headers(
Headers :: hpack:headers(),
CallbackState :: any()) -> {ok, NewState :: any()}.
on_receive_request_headers(_Headers, State) -> {ok, State}.
-spec on_send_push_promise(
Headers :: hpack:headers(),
CallbackState :: any()) -> {ok, NewState :: any()}.
on_send_push_promise(_Headers, State) -> {ok, State}.
-spec on_receive_request_data(
iodata(),
CallbackState :: any())-> {ok, NewState :: any()}.
on_receive_request_data(_Data, State) -> {ok, State}.
-spec on_request_end_stream(
CallbackState :: any()) ->
{ok, NewState :: any()}.
on_request_end_stream(State=#state{conn_pid=ConnPid,
stream_id=StreamId}) ->
ResponseHeaders = [
{<<":status">>,<<"200">>}
],
h2_connection:send_headers(ConnPid, StreamId, ResponseHeaders),
h2_connection:send_body(ConnPid, StreamId, crypto:strong_rand_bytes(?SEND_BYTES),
[{send_end_stream, false}]),
timer:sleep(200),
h2_connection:send_body(ConnPid, StreamId, crypto:strong_rand_bytes(?SEND_BYTES)),
{ok, State}.