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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
--[[
LuCId HTTP-Slave
(c) 2009 Steven Barth <steven@midlink.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
$Id$
]]--
local ipairs, pairs = ipairs, pairs
local tostring, tonumber = tostring, tonumber
local pcall, assert, type = pcall, assert, type
local os = require "os"
local nixio = require "nixio"
local util = require "luci.util"
local ltn12 = require "luci.ltn12"
local proto = require "luci.http.protocol"
local table = require "table"
local date = require "luci.http.protocol.date"
--- HTTP Daemon
-- @cstyle instance
module "luci.lucid.http.server"
VERSION = "1.0"
statusmsg = {
[200] = "OK",
[206] = "Partial Content",
[301] = "Moved Permanently",
[302] = "Found",
[304] = "Not Modified",
[400] = "Bad Request",
[401] = "Unauthorized",
[403] = "Forbidden",
[404] = "Not Found",
[405] = "Method Not Allowed",
[408] = "Request Time-out",
[411] = "Length Required",
[412] = "Precondition Failed",
[416] = "Requested range not satisfiable",
[500] = "Internal Server Error",
[503] = "Server Unavailable",
}
--- Create a new IO resource response.
-- @class function
-- @param fd File descriptor
-- @param len Length of data
-- @return IO resource
IOResource = util.class()
function IOResource.__init__(self, fd, len)
self.fd, self.len = fd, len
end
--- Create a server handler.
-- @class function
-- @param name Name
-- @return Handler
Handler = util.class()
function Handler.__init__(self, name)
self.name = name or tostring(self)
end
--- Create a failure reply.
-- @param code HTTP status code
-- @param msg Status message
-- @return status code, header table, response source
function Handler.failure(self, code, msg)
return code, { ["Content-Type"] = "text/plain" }, ltn12.source.string(msg)
end
--- Add an access restriction.
-- @param restriction Restriction specification
function Handler.restrict(self, restriction)
if not self.restrictions then
self.restrictions = {restriction}
else
self.restrictions[#self.restrictions+1] = restriction
end
end
--- Enforce access restrictions.
-- @param request Request object
-- @return nil or HTTP statuscode, table of headers, response source
function Handler.checkrestricted(self, request)
if not self.restrictions then
return
end
local localif, user, pass
for _, r in ipairs(self.restrictions) do
local stat = true
if stat and r.interface then -- Interface restriction
if not localif then
for _, v in ipairs(request.server.interfaces) do
if v.addr == request.env.SERVER_ADDR then
localif = v.name
break
end
end
end
if r.interface ~= localif then
stat = false
end
end
if stat and r.user then -- User restriction
local rh, pwe
if not user then
rh = (request.headers.Authorization or ""):match("Basic (.*)")
rh = rh and nixio.bin.b64decode(rh) or ""
user, pass = rh:match("(.*):(.*)")
pass = pass or ""
end
pwe = nixio.getsp and nixio.getsp(r.user) or nixio.getpw(r.user)
local pwh = (user == r.user) and pwe and (pwe.pwdp or pwe.passwd)
if not pwh or #pwh < 1 or nixio.crypt(pass, pwh) ~= pwh then
stat = false
end
end
if stat then
return
end
end
return 401, {
["WWW-Authenticate"] = ('Basic realm=%q'):format(self.name),
["Content-Type"] = 'text/plain'
}, ltn12.source.string("Unauthorized")
end
--- Process a request.
-- @param request Request object
-- @param sourcein Request data source
-- @return HTTP statuscode, table of headers, response source
function Handler.process(self, request, sourcein)
local stat, code, hdr, sourceout
local stat, code, msg = self:checkrestricted(request)
if stat then -- Access Denied
return stat, code, msg
end
-- Detect request Method
local hname = "handle_" .. request.env.REQUEST_METHOD
if self[hname] then
-- Run the handler
stat, code, hdr, sourceout = pcall(self[hname], self, request, sourcein)
-- Check for any errors
if not stat then
return self:failure(500, code)
end
else
return self:failure(405, statusmsg[405])
end
return code, hdr, sourceout
end
--- Create a Virtual Host.
-- @class function
-- @return Virtual Host
VHost = util.class()
function VHost.__init__(self)
self.handlers = {}
end
--- Process a request and invoke the appropriate handler.
-- @param request Request object
-- @param ... Additional parameters passed to the handler
-- @return HTTP statuscode, table of headers, response source
function VHost.process(self, request, ...)
local handler
local hlen = -1
local uri = request.env.SCRIPT_NAME
local sc = ("/"):byte()
-- SCRIPT_NAME
request.env.SCRIPT_NAME = ""
-- Call URI part
request.env.PATH_INFO = uri
for k, h in pairs(self.handlers) do
if #k > hlen then
if uri == k or (uri:sub(1, #k) == k and uri:byte(#k+1) == sc) then
handler = h
hlen = #k
request.env.SCRIPT_NAME = k
request.env.PATH_INFO = uri:sub(#k+1)
end
end
end
if handler then
return handler:process(request, ...)
else
return 404, nil, ltn12.source.string("No such handler")
end
end
--- Get a list of registered handlers.
-- @return Table of handlers
function VHost.get_handlers(self)
return self.handlers
end
--- Register handler with a given URI prefix.
-- @oaram match URI prefix
-- @param handler Handler object
function VHost.set_handler(self, match, handler)
self.handlers[match] = handler
end
-- Remap IPv6-IPv4-compatibility addresses back to IPv4 addresses.
local function remapipv6(adr)
local map = "::ffff:"
if adr:sub(1, #map) == map then
return adr:sub(#map+1)
else
return adr
end
end
-- Create a source that decodes chunked-encoded data from a socket.
local function chunksource(sock, buffer)
buffer = buffer or ""
return function()
local output
local _, endp, count = buffer:find("^([0-9a-fA-F]+);?.-\r\n")
while not count and #buffer <= 1024 do
local newblock, code = sock:recv(1024 - #buffer)
if not newblock then
return nil, code
end
buffer = buffer .. newblock
_, endp, count = buffer:find("^([0-9a-fA-F]+);?.-\r\n")
end
count = tonumber(count, 16)
if not count then
return nil, -1, "invalid encoding"
elseif count == 0 then
return nil
elseif count + 2 <= #buffer - endp then
output = buffer:sub(endp+1, endp+count)
buffer = buffer:sub(endp+count+3)
return output
else
output = buffer:sub(endp+1, endp+count)
buffer = ""
if count - #output > 0 then
local remain, code = sock:recvall(count-#output)
if not remain then
return nil, code
end
output = output .. remain
count, code = sock:recvall(2)
else
count, code = sock:recvall(count+2-#buffer+endp)
end
if not count then
return nil, code
end
return output
end
end
end
-- Create a sink that chunk-encodes data and writes it on a given socket.
local function chunksink(sock)
return function(chunk, err)
if not chunk then
return sock:writeall("0\r\n\r\n")
else
return sock:writeall(("%X\r\n%s\r\n"):format(#chunk, chunk))
end
end
end
--- Create a server object.
-- @class function
-- @return Server object
Server = util.class()
function Server.__init__(self)
self.vhosts = {}
end
--- Get a list of registered virtual hosts.
-- @return Table of virtual hosts
function Server.get_vhosts(self)
return self.vhosts
end
--- Register a virtual host with a given name.
-- @param name Hostname
-- @param vhost Virtual host object
function Server.set_vhost(self, name, vhost)
self.vhosts[name] = vhost
end
--- Send a fatal error message to given client and close the connection.
-- @param client Client socket
-- @param code HTTP status code
-- @param msg status message
function Server.error(self, client, code, msg)
hcode = tostring(code)
client:writeall( "HTTP/1.0 " .. hcode .. " " ..
statusmsg[code] .. "\r\n" )
client:writeall( "Connection: close\r\n" )
client:writeall( "Content-Type: text/plain\r\n\r\n" )
if msg then
client:writeall( "HTTP-Error " .. code .. ": " .. msg .. "\r\n" )
end
client:close()
end
local hdr2env = {
["Content-Length"] = "CONTENT_LENGTH",
["Content-Type"] = "CONTENT_TYPE",
["Content-type"] = "CONTENT_TYPE",
["Accept"] = "HTTP_ACCEPT",
["Accept-Charset"] = "HTTP_ACCEPT_CHARSET",
["Accept-Encoding"] = "HTTP_ACCEPT_ENCODING",
["Accept-Language"] = "HTTP_ACCEPT_LANGUAGE",
["Connection"] = "HTTP_CONNECTION",
["Cookie"] = "HTTP_COOKIE",
["Host"] = "HTTP_HOST",
["Referer"] = "HTTP_REFERER",
["User-Agent"] = "HTTP_USER_AGENT"
}
--- Parse the request headers and prepare the environment.
-- @param source line-based input source
-- @return Request object
function Server.parse_headers(self, source)
local env = {}
local req = {env = env, headers = {}}
local line, err
repeat -- Ignore empty lines
line, err = source()
if not line then
return nil, err
end
until #line > 0
env.REQUEST_METHOD, env.REQUEST_URI, env.SERVER_PROTOCOL =
line:match("^([A-Z]+) ([^ ]+) (HTTP/1%.[01])$")
if not env.REQUEST_METHOD then
return nil, "invalid magic"
end
local key, envkey, val
repeat
line, err = source()
if not line then
return nil, err
elseif #line > 0 then
key, val = line:match("^([%w-]+)%s?:%s?(.*)")
if key then
req.headers[key] = val
envkey = hdr2env[key]
if envkey then
env[envkey] = val
end
else
return nil, "invalid header line"
end
else
break
end
until false
env.SCRIPT_NAME, env.QUERY_STRING = env.REQUEST_URI:match("([^?]*)%??(.*)")
return req
end
--- Handle a new client connection.
-- @param client client socket
-- @param env superserver environment
function Server.process(self, client, env)
local sourcein = function() end
local sourcehdr = client:linesource()
local sinkout
local buffer
local close = false
local stat, code, msg, message, err
client:setsockopt("socket", "rcvtimeo", 5)
client:setsockopt("socket", "sndtimeo", 5)
repeat
-- parse headers
message, err = self:parse_headers(sourcehdr)
-- any other error
if not message or err then
if err == 11 then -- EAGAIN
break
else
return self:error(client, 400, err)
end
end
-- Prepare sources and sinks
buffer = sourcehdr(true)
sinkout = client:sink()
message.server = env
if client:is_tls_socket() then
message.env.HTTPS = "on"
end
-- Addresses
message.env.REMOTE_ADDR = remapipv6(env.host)
message.env.REMOTE_PORT = env.port
local srvaddr, srvport = client:getsockname()
message.env.SERVER_ADDR = remapipv6(srvaddr)
message.env.SERVER_PORT = srvport
-- keep-alive
if message.env.SERVER_PROTOCOL == "HTTP/1.1" then
close = (message.env.HTTP_CONNECTION == "close")
else
close = not message.env.HTTP_CONNECTION
or message.env.HTTP_CONNECTION == "close"
end
-- Uncomment this to disable keep-alive
close = close or env.config.nokeepalive
if message.env.REQUEST_METHOD == "GET"
or message.env.REQUEST_METHOD == "HEAD" then
-- Be happy
elseif message.env.REQUEST_METHOD == "POST" then
-- If we have a HTTP/1.1 client and an Expect: 100-continue header
-- respond with HTTP 100 Continue message
if message.env.SERVER_PROTOCOL == "HTTP/1.1"
and message.headers.Expect == '100-continue' then
client:writeall("HTTP/1.1 100 Continue\r\n\r\n")
end
if message.headers['Transfer-Encoding'] and
message.headers['Transfer-Encoding'] ~= "identity" then
sourcein = chunksource(client, buffer)
buffer = nil
elseif message.env.CONTENT_LENGTH then
local len = tonumber(message.env.CONTENT_LENGTH)
if #buffer >= len then
sourcein = ltn12.source.string(buffer:sub(1, len))
buffer = buffer:sub(len+1)
else
sourcein = ltn12.source.cat(
ltn12.source.string(buffer),
client:blocksource(nil, len - #buffer)
)
end
else
return self:error(client, 411, statusmsg[411])
end
close = true
else
return self:error(client, 405, statusmsg[405])
end
local host = self.vhosts[message.env.HTTP_HOST] or self.vhosts[""]
if not host then
return self:error(client, 404, "No virtual host found")
end
local code, headers, sourceout = host:process(message, sourcein)
headers = headers or {}
-- Post process response
if sourceout then
if util.instanceof(sourceout, IOResource) then
if not headers["Content-Length"] then
headers["Content-Length"] = sourceout.len
end
end
if not headers["Content-Length"] then
if message.env.SERVER_PROTOCOL == "HTTP/1.1" then
headers["Transfer-Encoding"] = "chunked"
sinkout = chunksink(client)
else
close = true
end
end
elseif message.env.REQUEST_METHOD ~= "HEAD" then
headers["Content-Length"] = 0
end
if close then
headers["Connection"] = "close"
elseif message.env.SERVER_PROTOCOL == "HTTP/1.0" then
headers["Connection"] = "Keep-Alive"
end
headers["Date"] = date.to_http(os.time())
local header = {
message.env.SERVER_PROTOCOL .. " " .. tostring(code) .. " "
.. statusmsg[code],
"Server: LuCId-HTTPd/" .. VERSION
}
for k, v in pairs(headers) do
if type(v) == "table" then
for _, h in ipairs(v) do
header[#header+1] = k .. ": " .. h
end
else
header[#header+1] = k .. ": " .. v
end
end
header[#header+1] = ""
header[#header+1] = ""
-- Output
stat, code, msg = client:writeall(table.concat(header, "\r\n"))
if sourceout and stat then
if util.instanceof(sourceout, IOResource) then
stat, code, msg = sourceout.fd:copyz(client, sourceout.len)
else
stat, msg = ltn12.pump.all(sourceout, sinkout)
end
end
-- Write errors
if not stat then
if msg then
nixio.syslog("err", "Error sending data to " .. env.host ..
": " .. msg .. "\n")
end
break
end
if buffer then
sourcehdr(buffer)
end
until close
client:shutdown()
client:close()
end
|