-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_server.lua
237 lines (200 loc) · 5.42 KB
/
http_server.lua
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
if not ... then require'http_server_test'; return end
local http = require'http'
local time = require'time'
local glue = require'glue'
local errors = require'errors'
local _ = string.format
local attr = glue.attr
local push = table.insert
local server = {
type = 'http_server', http = http,
tls_options = {
loadfile = glue.readfile,
protocols = 'tlsv1.2',
ciphers = [[
ECDHE-ECDSA-AES256-GCM-SHA384
ECDHE-RSA-AES256-GCM-SHA384
ECDHE-ECDSA-CHACHA20-POLY1305
ECDHE-RSA-CHACHA20-POLY1305
ECDHE-ECDSA-AES128-GCM-SHA256
ECDHE-RSA-AES128-GCM-SHA256
ECDHE-ECDSA-AES256-SHA384
ECDHE-RSA-AES256-SHA384
ECDHE-ECDSA-AES128-SHA256
ECDHE-RSA-AES128-SHA256
]],
prefer_ciphers_server = true,
},
}
function server:bind_libs(libs)
for lib in libs:gmatch'[^%s]+' do
if lib == 'sock' then
local sock = require'sock'
self.tcp = sock.tcp
self.cowrap = sock.cowrap
self.newthread = sock.newthread
self.resume = sock.resume
self.thread = sock.thread
self.start = sock.start
self.sleep = sock.sleep
self.currentthread = sock.currentthread
elseif lib == 'sock_libtls' then
local socktls = require'sock_libtls'
self.stcp = socktls.server_stcp
elseif lib == 'zlib' then
self.http.zlib = require'zlib'
else
assert(false)
end
end
end
function server:time(ts)
return glue.time(ts)
end
server.request_finish = glue.noop --request finalizer stub
function server:log(tcp, severity, module, event, fmt, ...)
local logging = self.logging
if not logging or logging.filter[severity] then return end
local s = fmt and _(fmt, logging.args(...)) or ''
logging.log(severity, module, event, '%-4s %s', tcp, s)
end
function server:check(tcp, ret, ...)
if ret then return ret end
return self:log(tcp, 'ERROR', 'htsrv', ...)
end
function server:new(t)
local self = glue.object(self, {}, t)
if self.libs then
self:bind_libs(self.libs)
end
if self.debug and (self.logging == nil or self.logging == true) then
self.logging = require'logging'
end
local function handler(ctcp, listen_opt)
local http = self.http:new({
debug = self.debug,
max_line_size = self.max_line_size,
tcp = ctcp,
cowrap = self.cowrap,
currentthread = self.currentthread,
listen_options = listen_opt,
})
while not ctcp:closed() do
local req, err = http:read_request()
if not req then
local eof = errors.is(err, 'socket') and err.message == 'eof'
self:check(ctcp, eof, 'read_request', '%s', err)
break
end
local finished, out_func, sending_response
local function send_response(opt)
if opt.content == nil then
opt.content = ''
end
sending_response = true
local res = http:build_response(req, opt, self:time())
local ok, err = http:send_response(res)
self:check(ctcp, ok, 'send_response', '%s', err)
finished = true
end
function req.respond(req, opt)
if opt.want_out_function then
out_func = self.cowrap(function(yield)
opt.content = yield
send_response(opt)
end)
out_func()
return out_func
else
send_response(opt)
end
end
function req.raise(req, status, content)
local err
if type(status) == 'number' then
err = {status = status, content = content}
elseif type(status) == 'table' then
err = status
else
assert(false)
end
errors.raise('http_response', err)
end
req.thread = self.currentthread()
local ok, err = errors.catch(nil, self.respond, req)
self:request_finish(req)
if not ok then
if errors.is(err, 'http_response') then
assert(not sending_response, 'response already sent')
req:respond(err)
elseif not sending_response then
self:check(ctcp, false, 'respond', '%s', err)
req:respond{status = 500}
else
error(_('respond() error:\n%s', err))
end
elseif not finished then --eof not signaled.
if out_func then
out_func() --eof
else
send_response({})
end
end
--the request must be entirely read before we can read the next request.
if req.body_was_read == nil then
req:read_body()
end
assert(req.body_was_read, 'request body was not read')
end
end
local stop
function self:stop()
stop = true
end
self.sockets = {}
for i,t in ipairs(self.listen) do
if t.addr == false then
goto continue
end
local tcp = assert(self.tcp())
assert(tcp:setopt('reuseaddr', true))
local addr, port = t.addr or '*', t.port or (t.tls and 443 or 80)
local ok, err = tcp:listen(addr, port)
if not ok then
self:check(tcp, false, 'listen', '("%s", %s): %s', addr, port, err)
goto continue
end
self:log(tcp, 'note', 'htsrv', 'LISTEN', '%s:%d', addr, port)
if t.tls then
local opt = glue.update(self.tls_options, t.tls_options)
local stcp, err = self.stcp(tcp, opt)
if self:check(tcp, stcp, 'stcp', '%s', err) then
tcp:close()
goto continue
end
tcp = stcp
end
push(self.sockets, tcp)
function accept_connection()
local ctcp, err = tcp:accept()
if not self:check(tcp, ctcp, 'accept',' %s', err) then
return
end
self.thread(function()
self:log(ctcp, 'note', 'htsrv', 'accept')
local ok, err = glue.pcall(handler, ctcp, t)
self:log(ctcp, 'note', 'htsrv', 'closed')
self:check(ctcp, ok, 'handler', '%s', err)
ctcp:close()
end)
end
self.thread(function()
while not stop do
accept_connection()
end
end)
::continue::
end
return self
end
return server