aboutsummaryrefslogtreecommitdiffstats
path: root/test/transport_capabilities_protocol.erl
blob: ba5024aa3ddb99cbf7c2f5fad9453723cadf8b79 (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
42
43
44
45
46
47
48
49
50
51
52
53
-module(transport_capabilities_protocol).
-behaviour(ranch_protocol).

-export([start_link/4]).
-export([init/3]).

start_link(Ref, _Socket, Transport, Opts) ->
	Pid = spawn_link(?MODULE, init, [Ref, Transport, Opts]),
	{ok, Pid}.

init(Ref, Transport, _Opts = []) ->
	{ok, Socket} = ranch:handshake(Ref),
	loop(Socket, Transport).

loop(Socket, Transport) ->
	case Transport:recv(Socket, 0, 5000) of
		{ok, Data} ->
			Reply =
			case check(Socket, Transport, Data) of
				ok ->
					<<"OK">>;
				error ->
					<<"ERROR">>
			end,
			Transport:send(Socket, Reply),
			loop(Socket, Transport);
		_ ->
			ok = Transport:close(Socket)
	end.

check(Socket, Transport, <<"getopts/2">>) ->
	case catch Transport:getopts(Socket, []) of
		{ok, _} ->
			ok;
		_ ->
			error
	end;

check(Socket, Transport, <<"getstat/1">>) ->
	case catch Transport:getstat(Socket) of
		{ok, _} ->
			ok;
		_ ->
			error
	end;

check(Socket, Transport, <<"getstat/2">>) ->
	case catch Transport:getstat(Socket, []) of
		{ok, _} ->
			ok;
		_ ->
			error
	end.