moonbridge

view moonbridge_http.lua @ 157:99a70d18e47c

Further work on new HTTP layer (code cleanup, work on body reading)
author jbe
date Wed May 27 01:51:04 2015 +0200 (2015-05-27)
parents 0c4221702ce1
children bd7225b30391
line source
1 #!/usr/bin/env lua
3 -- module preamble
4 local _G, _M = _ENV, {}
5 _ENV = setmetatable({}, {
6 __index = function(self, key)
7 local value = _M[key]; if value ~= nil then return value end
8 return _G[key]
9 end,
10 __newindex = _M
11 })
13 -- function that encodes certain HTML entities:
14 -- (not used by the library itself)
15 function encode_html(text)
16 return (
17 string.gsub(
18 text, '[<>&"]',
19 function(char)
20 if char == '<' then
21 return "&lt;"
22 elseif char == '>' then
23 return "&gt;"
24 elseif char == '&' then
25 return "&amp;"
26 elseif char == '"' then
27 return "&quot;"
28 end
29 end
30 )
31 )
33 end
35 -- function that encodes special characters for URIs:
36 -- (not used by the library itself)
37 function encode_uri(text)
38 return (
39 string.gsub(text, "[^0-9A-Za-z_%.~-]",
40 function (char)
41 return string.format("%%%02x", string.byte(char))
42 end
43 )
44 )
45 end
47 -- function undoing URL encoding:
48 do
49 local b0 = string.byte("0")
50 local b9 = string.byte("9")
51 local bA = string.byte("A")
52 local bF = string.byte("F")
53 local ba = string.byte("a")
54 local bf = string.byte("f")
55 function decode_uri(str)
56 return (
57 string.gsub(
58 string.gsub(str, "%+", " "),
59 "%%([0-9A-Fa-f][0-9A-Fa-f])",
60 function(hex)
61 local n1, n2 = string.byte(hex, 1, 2)
62 if n1 >= b0 and n1 <= b9 then n1 = n1 - b0
63 elseif n1 >= bA and n1 <= bF then n1 = n1 - bA + 10
64 elseif n1 >= ba and n1 <= bf then n1 = n1 - ba + 10
65 else error("Assertion failed") end
66 if n2 >= b0 and n2 <= b9 then n2 = n2 - b0
67 elseif n2 >= bA and n2 <= bF then n2 = n2 - bA + 10
68 elseif n2 >= ba and n2 <= bf then n2 = n2 - ba + 10
69 else error("Assertion failed") end
70 return string.char(n1 * 16 + n2)
71 end
72 )
73 )
74 end
75 end
77 -- status codes that carry no response body (in addition to 1xx):
78 -- (set to "zero_content_length" if Content-Length header is required)
79 status_without_response_body = {
80 ["101"] = true, -- list 101 to allow protocol switch
81 ["204"] = true,
82 ["205"] = "zero_content_length",
83 ["304"] = true
84 }
86 -- parses URL encoded form data:
87 local function read_urlencoded_form(data)
88 local tbl = {}
89 for rawkey, rawvalue in string.gmatch(data, "([^?=&]*)=([^?=&]*)") do
90 local key = decode_uri(rawkey)
91 local value = decode_uri(rawvalue)
92 local subtbl = tbl[key]
93 if subtbl then
94 subtbl[#subtbl+1] = value
95 else
96 tbl[key] = {value}
97 end
98 end
99 return tbl
100 end
102 -- extracts first value from each subtable:
103 local function get_first_values(tbl)
104 local newtbl = {}
105 for key, subtbl in pairs(tbl) do
106 newtbl[key] = subtbl[1]
107 end
108 return newtbl
109 end
111 request_pt = {}
112 request_mt = { __index = request_pt }
114 function request_pt:_init(handler, options)
115 self._application_handler = handler
116 -- process options:
117 options = options or {}
118 do
119 -- named arg "static_headers" is used to create the preamble:
120 local s = options.static_headers
121 local t = {}
122 if s then
123 if type(s) == "string" then
124 for line in string.gmatch(s, "[^\r\n]+") do
125 t[#t+1] = line
126 end
127 else
128 for i, kv in ipairs(options.static_headers) do
129 if type(kv) == "string" then
130 t[#t+1] = kv
131 else
132 t[#t+1] = kv[1] .. ": " .. kv[2]
133 end
134 end
135 end
136 end
137 t[#t+1] = ""
138 self._preamble = table.concat(t, "\r\n") -- preamble sent with every(!) HTTP response
139 end
140 self._input_chunk_size = options.maximum_input_chunk_size or options.chunk_size or 16384
141 self._output_chunk_size = options.minimum_output_chunk_size or options.chunk_size or 1024
142 self._header_size_limit = options.header_size_limit or 1024*1024
143 self._body_size_limit = options.body_size_limit or 64*1024*1024
144 local function init_timeout(name, default)
145 local value = options[name]
146 if value == nil then
147 self["_"..name] = default
148 else
149 self["_"..name] = value
150 end
151 end
152 init_timeout("request_idle_timeout", 330)
153 init_timeout("request_header_timeout", 30)
154 init_timeout("request_body_timeout", 1800)
155 init_timeout("response_timeout", 1830)
156 self._poll = options.poll_function or moonbridge_io.poll
157 self:_create_closure("_write_yield")
158 self:_create_closure("_handler")
159 self:_create_header_metatables()
160 end
162 function request_pt:_create_closure(name)
163 self[name.."_closure"] = function(...)
164 return self[name](self, ...)
165 end
166 end
168 function request_pt:_create_header_metatables()
169 -- table mapping header field names to value-lists:
170 self._headers_mt = {
171 __index = function(tbl, key)
172 local lowerkey = string.lower(key)
173 local result = self._headers[lowerkey]
174 if result == nil then
175 result = {}
176 end
177 tbl[lowerkey] = result
178 tbl[key] = result
179 return result
180 end
181 }
182 -- table mapping header field names to value-lists
183 -- (for headers with comma separated values):
184 self._headers_csv_table_mt = {
185 __index = function(tbl, key)
186 local result = {}
187 for i, line in ipairs(self.headers[key]) do
188 for entry in string.gmatch(line, "[^,]+") do
189 local value = string.match(entry, "^[ \t]*(..-)[ \t]*$")
190 if value then
191 result[#result+1] = value
192 end
193 end
194 end
195 tbl[key] = result
196 return result
197 end
198 }
199 -- table mapping header field names to a comma separated string
200 -- (for headers with comma separated values):
201 self._headers_csv_string_mt = {
202 __index = function(tbl, key)
203 local result = {}
204 for i, line in ipairs(self.headers[key]) do
205 result[#result+1] = line
206 end
207 result = string.concat(result, ", ")
208 tbl[key] = result
209 return result
210 end
211 }
212 -- table mapping header field names to a single string value
213 -- (or false if header has been sent multiple times):
214 self._headers_value_mt = {
215 __index = function(tbl, key)
216 if self._headers_value_nil[key] then
217 return nil
218 end
219 local result = nil
220 local values = self.headers_csv_table[key]
221 if #values == 0 then
222 self._headers_value_nil[key] = true
223 elseif #values == 1 then
224 result = values[1]
225 else
226 result = false
227 end
228 tbl[key] = result
229 return result
230 end
231 }
232 -- table mapping header field names to a flag table,
233 -- indicating if the comma separated value contains certain entries:
234 self._headers_flags_mt = {
235 __index = function(tbl, key)
236 local result = setmetatable({}, {
237 __index = function(tbl, key)
238 local lowerkey = string.lower(key)
239 local result = rawget(tbl, lowerkey) or false
240 tbl[lowerkey] = result
241 tbl[key] = result
242 return result
243 end
244 })
245 for i, value in ipairs(self.headers_csv_table[key]) do
246 result[string.lower(value)] = true
247 end
248 tbl[key] = result
249 return result
250 end
251 }
252 end
254 function request_pt:_create_magictable(name)
255 self[name] = setmetatable({}, self["_"..name.."_mt"])
256 end
258 function request_pt:_handler(socket)
259 self._socket = socket
260 self._survive = true
261 self._socket_set = {[socket] = true}
262 self._faulty = false
263 self._connection_close_requested = false
264 self._connection_close_responded = false
265 self:_create_magictable("headers")
266 self:_create_magictable("headers_csv_table")
267 self:_create_magictable("headers_csv_string")
268 self:_create_magictable("headers_value")
269 self:_create_magictable("headers_flags")
270 repeat
271 -- wait for input:
272 if not moonbridge_io.poll(self._socket_set, nil, self._request_idle_timeout) then
273 self:_error("408 Request Timeout", "Idle connection timed out")
274 return self._survive
275 end
276 -- read headers (with timeout):
277 do
278 local coro = coroutine.wrap(self._read_headers)
279 local timeout = self._request_header_timeout
280 local starttime = timeout and moonbridge_io.timeref()
281 while true do
282 local status = coro(self)
283 if status == nil then
284 local remaining
285 if timeout then
286 remaining = timeout - moonbridge_io.timeref(starttime)
287 end
288 if not self._poll(self._socket_set, nil, remaining) then
289 self:_error("408 Request Timeout", "Timeout while receiving headers")
290 return self._survive
291 end
292 elseif status == false then
293 return self._survive
294 elseif status == true then
295 break
296 else
297 error("Unexpected yield value")
298 end
299 end
300 end
301 -- prepare reading of body:
302 self._read_body_coro = coroutine.wrap(self._read_body)
303 -- set timeout for application handler:
304 timeout(self._response_timeout or 0)
305 -- call application handler:
306 if self._application_handler(self) ~= true then
307 self._survive = false
308 end
309 -- enforce request:finish()
310 request:finish()
311 -- reset timeout of application handler
312 timeout(0)
313 until self._connection_close_responded
314 return self._survive
315 end
317 function request_pt:_drain_input()
318 self._read_body_coro = "drain"
319 end
321 function request_pt:_consume_some_input()
322 local coro = self._read_body_coro
323 if coro == "drain" then
324 local bytes, status = self._socket:drain_nb(self._input_chunk_size)
325 if status == "eof" then
326 coro = nil
327 end
328 elseif coro then
329 local retval = coro(self)
330 if retval ~= nil then
331 coro = nil -- can't consume more data
332 end
333 end
334 end
336 function request_pt:_consume_all_input()
337 while self._read_body_coro do
338 self._poll(socket_set)
339 self:_consume_some_input()
340 end
341 end
343 function request_pt:_error(status, explanation)
344 end
346 function request_pt:_read(...)
347 local line, status = self._socket:read_yield(...)
348 if line == nil then
349 self._faulty = true
350 error(status)
351 else
352 return line, status
353 end
354 end
356 function request_pt:_read_headers()
357 local remaining = self._header_size_limit
358 -- read and parse request line:
359 local target, proto
360 do
361 local line, status = self:_read(remaining-2, "\n")
362 if status == "maxlen" then
363 self:_error("414 Request-URI Too Long")
364 return false
365 elseif status == "eof" then
366 if line ~= "" then
367 self:_error("400 Bad Request", "Unexpected EOF in request-URI line")
368 end
369 return false
370 end
371 remaining = remaining - #line
372 self.method, target, proto =
373 line:match("^([^ \t\r]+)[ \t]+([^ \t\r]+)[ \t]*([^ \t\r]*)[ \t]*\r?\n$")
374 if not request.method then
375 self:_error("400 Bad Request", "Invalid request-URI line")
376 return false
377 elseif proto ~= "HTTP/1.1" then
378 self:_error("505 HTTP Version Not Supported")
379 return false
380 end
381 end
382 -- read and parse headers:
383 self._headers = {}
384 self._headers_value_nil = {}
385 while true do
386 local line, status = self:_read(remaining, "\n");
387 if status == "maxlen" then
388 self:_error("431 Request Header Fields Too Large")
389 return false
390 elseif status == "eof" then
391 self:_error("400 Bad Request", "Unexpected EOF in request headers")
392 return false
393 end
394 remaining = remaining - #line
395 if line == "\r\n" or line == "\n" then
396 break
397 end
398 local key, value = string.match(line, "^([^ \t\r]+):[ \t]*(.-)[ \t]*\r?\n$")
399 if not key then
400 self:_error("400 Bad Request", "Invalid header line")
401 return false
402 end
403 local lowerkey = key:lower()
404 local values = self._headers[lowerkey]
405 if values then
406 values[#values+1] = value
407 else
408 self._headers[lowerkey] = {value}
409 end
410 end
411 -- process "Connection: close" header if existent:
412 self._connection_close_requested = self.headers_flags["Connection"]["close"]
413 -- process "Content-Length" header if existent:
414 do
415 local values = self.headers_csv_table["Content-Length"]
416 if #values > 0 then
417 self._request_body_content_length = tonumber(values[1])
418 local proper_value = tostring(request_body_content_length)
419 for i, value in ipairs(values) do
420 value = string.match(value, "^0*(.*)")
421 if value ~= proper_value then
422 self:_error("400 Bad Request", "Content-Length header(s) invalid")
423 return false
424 end
425 end
426 if request_body_content_length > self._body_size_limit then
427 self:_error("413 Request Entity Too Large", "Announced request body size is too big")
428 return false
429 end
430 end
431 end
432 -- process "Transfer-Encoding" header if existent:
433 do
434 local flag = self.headers_flags["Transfer-Encoding"]["chunked"]
435 local list = self.headers_csv_table["Transfer-Encoding"]
436 if (flag and #list ~= 1) or (not flag and #list ~= 0) then
437 self:_error("400 Bad Request", "Unexpected Transfer-Encoding")
438 return false
439 end
440 end
441 -- process "Expect" header if existent:
442 for i, value in ipairs(self.headers_csv_table["Expect"]) do
443 if string.lower(value) ~= "100-continue" then
444 self:_error("417 Expectation Failed", "Unexpected Expect header")
445 return false
446 end
447 end
448 -- get mandatory Host header according to RFC 7230:
449 self.host = self.headers_value["Host"]
450 if not self.host then
451 self:_error("400 Bad Request", "No valid host header")
452 return false
453 end
454 -- parse request target:
455 self.path, self.query = string.match(target, "^/([^?]*)(.*)$")
456 if not self.path then
457 local host2
458 host2, self.path, self.query = string.match(target, "^[Hh][Tt][Tt][Pp]://([^/?]+)/?([^?]*)(.*)$")
459 if host2 then
460 if self.host ~= host2 then
461 self:_error("400 Bad Request", "No valid host header")
462 return false
463 end
464 elseif not (target == "*" and self.method == "OPTIONS") then
465 self:_error("400 Bad Request", "Invalid request target")
466 return false
467 end
468 end
469 -- parse GET params:
470 if self.query then
471 self.get_params_list = read_urlencoded_form(request.query)
472 self.get_params = get_first_values(self.get_params_list)
473 end
474 -- parse cookies:
475 self.cookies = {}
476 for i, line in ipairs(self.headers["Cookie"]) do
477 for rawkey, rawvalue in
478 string.gmatch(line, "([^=; ]*)=([^=; ]*)")
479 do
480 self.cookies[decode_uri(rawkey)] = decode_uri(rawvalue)
481 end
482 end
483 -- indicate success:
484 return true
485 end
487 function request_pt:_read_body()
488 local remaining = self._body_size_limit
489 if request.headers_flags["Transfer-Encoding"]["chunked"] then
490 while true do
491 local line, status = self:_read(32 + remaining, "\n")
492 if status == "maxlen" then
493 self:_error("400 Bad Request", "Request body size limit exceeded")
494 return false
495 elseif status == "eof" then
496 self:_error("400 Bad Request", "Encoding error or unexpected EOF while reading next chunk of request body")
497 return false
498 end
499 local zeros, lenstr = string.match(line, "^(0*)([1-9A-Fa-f]+[0-9A-Fa-f]*)\r?\n$")
500 local chunkext
501 if lenstr then
502 chunkext = ""
503 else
504 zeros, lenstr, chunkext = string.match(line, "^(0*)([1-9A-Fa-f]+[0-9A-Fa-f]*)([ \t;].-)\r?\n$")
505 end
506 if not lenstr or #lenstr > 13 then
507 self:_error("400 Bad Request", "Encoding error while reading chunk of request body")
508 return false
509 end
510 local len = tonumber("0x" .. lenstr)
511 remaining = remaining - (#zeros + #chunkext + len)
512 if remaining < 0 then
513 self:_error("400 Bad Request", "Request body size limit exceeded")
514 return false
515 end
516 if len == 0 then break end
517 if self:_read_body_bytes(len) == false then
518 return false
519 end
520 local term, status = self:_read(2, "\n")
521 if status == "eof" then
522 self:_error("400 Bad Request", "Unexpected EOF while reading next chunk of request body")
523 return false
524 end
525 if term ~= "\r\n" and term ~= "\n" then
526 self:_error("400 Bad Request", "Encoding error while reading chunk of request body")
527 return false
528 end
529 end
530 while true do
531 local line, status = self:_read(2 + remaining, "\n")
532 if status == "eof" then
533 self:_error("400 Bad Request", "Unexpected EOF while reading chunk of request body")
534 return false
535 end
536 if line == "\r\n" or line == "\n" then break end
537 remaining = remaining - #line
538 if remaining < 0 then
539 self:_error("413 Request Entity Too Large", "Request body size limit exceeded while reading trailer section of chunked request body")
540 return false
541 end
542 end
543 elseif request_body_content_length then
544 if self._read_body_bytes(request_body_content_length) == false then
545 return false
546 end
547 end
548 -- indicate success:
549 return true
550 end
552 function request_pt:_read_body_bytes(remaining, callback)
553 while remaining > 0 do
554 local limit
555 if remaining > self._input_chunk_size then
556 limit = self._input_chunk_size
557 else
558 limit = remaining
559 end
560 local chunk, status = self:_read(limit)
561 if status == "eof" then
562 self:_error("400 Bad Request", "Unexpected EOF while reading chunk of request body")
563 return false
564 end
565 remaining = remaining - limit
566 if self._body_streamer then
567 self._body_streamer(chunk)
568 end
569 end
570 return true
571 end
573 function request_pt:_assert_not_faulty()
574 assert(not self._faulty, "Tried to use faulty request handle")
575 end
577 function request_pt:_write_yield()
578 self:_consume_some_input()
579 self._poll(self._socket_set, self._socket_set)
580 end
582 function request_pt:_write(...)
583 assert(self._socket:write_call(self._write_yield_closure, ...))
584 end
586 function request_pt:_flush(...)
587 assert(self._socket:write_call(self._write_yield_closure, ...))
588 end
590 -- function creating a HTTP handler:
591 function generate_handler(handler, options)
592 -- swap arguments if necessary (for convenience):
593 if type(handler) ~= "function" and type(options) == "function" then
594 handler, options = options, handler
595 end
596 local request = setmetatable({}, request_mt)
597 request:_init(handler, options)
598 return request._handler_closure
599 end
601 return _M

Impressum / About Us