forked from esl/lhttpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.erl
178 lines (163 loc) · 6.63 KB
/
webserver.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
%%% ----------------------------------------------------------------------------
%%% Copyright (c) 2009, Erlang Training and Consulting Ltd.
%%% All rights reserved.
%%%
%%% Redistribution and use in source and binary forms, with or without
%%% modification, are permitted provided that the following conditions are met:
%%% * Redistributions of source code must retain the above copyright
%%% notice, this list of conditions and the following disclaimer.
%%% * Redistributions in binary form must reproduce the above copyright
%%% notice, this list of conditions and the following disclaimer in the
%%% documentation and/or other materials provided with the distribution.
%%% * Neither the name of Erlang Training and Consulting Ltd. nor the
%%% names of its contributors may be used to endorse or promote products
%%% derived from this software without specific prior written permission.
%%%
%%% THIS SOFTWARE IS PROVIDED BY Erlang Training and Consulting Ltd. ''AS IS''
%%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
%%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
%%% ARE DISCLAIMED. IN NO EVENT SHALL Erlang Training and Consulting Ltd. BE
%%% LIABLE SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
%%% BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
%%% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
%%% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
%%% ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
%%% ----------------------------------------------------------------------------
%%% @author Oscar Hellström <[email protected]>
%%% @author Magnus Henoch <[email protected]>
%%% @doc Simple web server for testing purposes
%%% @end
-module(webserver).
-export([start/2, start/3, read_chunked/3]).
-export([accept_connection/4]).
start(Module, Responders) ->
start(Module, Responders, inet).
start(Module, Responders, Family) ->
case get_addr("localhost", Family) of
{ok, Addr} ->
LS = listen(Module, Addr, Family),
spawn_link(?MODULE, accept_connection, [self(), Module, LS, Responders]),
port(Module, LS);
Error ->
Error
end.
accept_connection(Parent, Module, ListenSocket, Responders) ->
Socket = accept(Module, ListenSocket),
server_loop(Module, Socket, nil, [], Responders),
unlink(Parent).
read_chunked(Module, Socket, Headers) ->
Body = read_chunks(Module, Socket, []),
ok = setopts(Module, Socket, [{packet, httph}]),
Trailers = read_trailers(Module, Socket, Headers),
% For next request
ok = setopts(Module, Socket, [{packet, http}]),
{Body, Trailers}.
read_chunks(Module, Socket, Acc) ->
ok = setopts(Module, Socket, [{packet, line}]),
case Module:recv(Socket, 0) of
{ok, HexSizeExtension} ->
case chunk_size(HexSizeExtension, []) of
0 ->
list_to_binary(lists:reverse(Acc));
Size ->
setopts(Module, Socket, [{packet, raw}]),
{ok, <<Chunk:Size/binary, "\r\n">>} =
Module:recv(Socket, Size + 2),
read_chunks(Module, Socket, [Chunk | Acc])
end;
{error, Reason} ->
erlang:error(Reason)
end.
read_trailers(Module, Socket, Hdrs) ->
case Module:recv(Socket, 0) of
{ok, {http_header, _, Name, _, Value}} when is_atom(Name) ->
Trailer = {atom_to_list(Name), Value},
read_trailers(Module, Socket, [Trailer | Hdrs]);
{ok, {http_header, _, Name, _, Value}} when is_list(Name) ->
Trailer = {Name, Value},
read_trailers(Module, Socket, [Trailer | Hdrs]);
{ok, http_eoh} -> Hdrs
end.
server_loop(Module, Socket, _, _, []) ->
Module:close(Socket);
server_loop(Module, Socket, Request, Headers, Responders) ->
case Module:recv(Socket, 0) of
{ok, {http_request, _, _, _} = NewRequest} ->
server_loop(Module, Socket, NewRequest, Headers, Responders);
{ok, {http_header, _, Field, _, Value}} when is_atom(Field) ->
NewHeaders = [{atom_to_list(Field), Value} | Headers],
server_loop(Module, Socket, Request, NewHeaders, Responders);
{ok, {http_header, _, Field, _, Value}} when is_list(Field) ->
NewHeaders = [{Field, Value} | Headers],
server_loop(Module, Socket, Request, NewHeaders, Responders);
{ok, http_eoh} ->
RequestBody = case proplists:get_value("Content-Length", Headers) of
undefined ->
<<>>;
"0" ->
<<>>;
SLength ->
Length = list_to_integer(SLength),
setopts(Module, Socket, [{packet, raw}]),
{ok, Body} = Module:recv(Socket, Length),
setopts(Module, Socket, [{packet, http}]),
Body
end,
Responder = hd(Responders),
Responder(Module, Socket, Request, Headers, RequestBody),
server_loop(Module, Socket, none, [], tl(Responders));
{error, closed} ->
Module:close(Socket)
end.
listen(ssl, Addr, Family) ->
Opts = [
Family,
{packet, http},
binary,
{active, false},
{ip, Addr},
{verify,0},
{keyfile, "../test/key.pem"},
{certfile, "../test/crt.pem"}
],
{ok, LS} = ssl:listen(0, Opts),
LS;
listen(Module, Addr, Family) ->
{ok, LS} = Module:listen(0, [
Family,
{packet, http},
binary,
{active, false},
{ip, Addr}
]),
LS.
get_addr(Host, Family) ->
case inet:getaddr(Host, Family) of
{ok, Addr} ->
{ok, Addr};
_ ->
{error, family_not_supported}
end.
accept(ssl, ListenSocket) ->
{ok, Socket} = ssl:transport_accept(ListenSocket, 10000),
ok = ssl:ssl_accept(Socket),
Socket;
accept(Module, ListenSocket) ->
{ok, Socket} = Module:accept(ListenSocket, 1000),
Socket.
setopts(ssl, Socket, Options) ->
ssl:setopts(Socket, Options);
setopts(_, Socket, Options) ->
inet:setopts(Socket, Options).
port(ssl, Socket) ->
{ok, {_, Port}} = ssl:sockname(Socket),
Port;
port(_, Socket) ->
{ok, Port} = inet:port(Socket),
Port.
chunk_size(<<$;, _/binary>>, Acc) ->
erlang:list_to_integer(lists:reverse(Acc), 16);
chunk_size(<<"\r\n">>, Acc) ->
erlang:list_to_integer(lists:reverse(Acc), 16);
chunk_size(<<Char, Rest/binary>>, Acc) ->
chunk_size(Rest, [Char | Acc]).