moonbridge

view moonbridge_http.lua @ 156:0c4221702ce1

Further work on new HTTP layer (read body, without parsing yet)
author jbe
date Tue May 26 02:06:17 2015 +0200 (2015-05-26)
parents 2c22b0f222c7
children 99a70d18e47c
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._consume_input = self._drain_input
264 self._headers = {}
265 self._headers_value_nil = {}
266 self._connection_close_requested = false
267 self._connection_close_responded = false
268 self:_create_magictable("headers")
269 self:_create_magictable("headers_csv_table")
270 self:_create_magictable("headers_csv_string")
271 self:_create_magictable("headers_value")
272 self:_create_magictable("headers_flags")
273 self.cookies = {}
274 repeat
275 -- wait for input:
276 if not moonbridge_io.poll(self._socket_set, nil, self._request_idle_timeout) then
277 self:_error("408 Request Timeout", "Idle connection timed out")
278 return self._survive
279 end
280 -- read headers (with timeout):
281 do
282 local coro = coroutine.wrap(self._read_headers)
283 local timeout = self._request_header_timeout
284 local starttime = timeout and moonbridge_io.timeref()
285 while true do
286 local status = coro(self)
287 if status == nil then
288 local remaining
289 if timeout then
290 remaining = timeout - moonbridge_io.timeref(starttime)
291 end
292 if not self._poll(self._socket_set, nil, remaining) then
293 self:_error("408 Request Timeout", "Timeout while receiving headers")
294 return self._survive
295 end
296 elseif status == false then
297 return self._survive
298 elseif status == true then
299 break
300 else
301 error("Unexpected yield value")
302 end
303 end
304 end
305 timeout(self._response_timeout or 0)
306 if self._application_handler(self) ~= true then
307 self._survive = false
308 end
309 request:finish()
310 timeout(0)
311 until self._connection_close_responded
312 return self._survive
313 end
315 function request_pt:_error(status, explanation)
316 end
318 function request_pt:_read(...)
319 local line, status = self._socket:read_yield(...)
320 if line == nil then
321 self._faulty = true
322 error(status)
323 else
324 return line, status
325 end
326 end
328 function request_pt:_read_headers()
329 local remaining = self._header_size_limit
330 -- read and parse request line:
331 local target, proto
332 do
333 local line, status = self:_read(remaining-2, "\n")
334 if status == "maxlen" then
335 self:_error("414 Request-URI Too Long")
336 return false
337 elseif status == "eof" then
338 if line ~= "" then
339 self:_error("400 Bad Request", "Unexpected EOF in request-URI line")
340 end
341 return false
342 end
343 remaining = remaining - #line
344 self.method, target, proto =
345 line:match("^([^ \t\r]+)[ \t]+([^ \t\r]+)[ \t]*([^ \t\r]*)[ \t]*\r?\n$")
346 if not request.method then
347 self:_error("400 Bad Request", "Invalid request-URI line")
348 return false
349 elseif proto ~= "HTTP/1.1" then
350 self:_error("505 HTTP Version Not Supported")
351 return false
352 end
353 end
354 -- read and parse headers:
355 while true do
356 local line, status = self:_read(remaining, "\n");
357 if status == "maxlen" then
358 self:_error("431 Request Header Fields Too Large")
359 return false
360 elseif status == "eof" then
361 self:_error("400 Bad Request", "Unexpected EOF in request headers")
362 return false
363 end
364 remaining = remaining - #line
365 if line == "\r\n" or line == "\n" then
366 break
367 end
368 local key, value = string.match(line, "^([^ \t\r]+):[ \t]*(.-)[ \t]*\r?\n$")
369 if not key then
370 self:_error("400 Bad Request", "Invalid header line")
371 return false
372 end
373 local lowerkey = key:lower()
374 local values = self._headers[lowerkey]
375 if values then
376 values[#values+1] = value
377 else
378 self._headers[lowerkey] = {value}
379 end
380 end
381 -- process "Connection: close" header if existent:
382 self._connection_close_requested = self.headers_flags["Connection"]["close"]
383 -- process "Content-Length" header if existent:
384 do
385 local values = self.headers_csv_table["Content-Length"]
386 if #values > 0 then
387 self._request_body_content_length = tonumber(values[1])
388 local proper_value = tostring(request_body_content_length)
389 for i, value in ipairs(values) do
390 value = string.match(value, "^0*(.*)")
391 if value ~= proper_value then
392 self:_error("400 Bad Request", "Content-Length header(s) invalid")
393 return false
394 end
395 end
396 if request_body_content_length > self._body_size_limit then
397 self:_error("413 Request Entity Too Large", "Announced request body size is too big")
398 return false
399 end
400 end
401 end
402 -- process "Transfer-Encoding" header if existent:
403 do
404 local flag = self.headers_flags["Transfer-Encoding"]["chunked"]
405 local list = self.headers_csv_table["Transfer-Encoding"]
406 if (flag and #list ~= 1) or (not flag and #list ~= 0) then
407 self:_error("400 Bad Request", "Unexpected Transfer-Encoding")
408 return false
409 end
410 end
411 -- process "Expect" header if existent:
412 for i, value in ipairs(self.headers_csv_table["Expect"]) do
413 if string.lower(value) ~= "100-continue" then
414 self:_error("417 Expectation Failed", "Unexpected Expect header")
415 return false
416 end
417 end
418 -- get mandatory Host header according to RFC 7230:
419 self.host = self.headers_value["Host"]
420 if not self.host then
421 self:_error("400 Bad Request", "No valid host header")
422 return false
423 end
424 -- parse request target:
425 self.path, self.query = string.match(target, "^/([^?]*)(.*)$")
426 if not self.path then
427 local host2
428 host2, self.path, self.query = string.match(target, "^[Hh][Tt][Tt][Pp]://([^/?]+)/?([^?]*)(.*)$")
429 if host2 then
430 if self.host ~= host2 then
431 self:_error("400 Bad Request", "No valid host header")
432 return false
433 end
434 elseif not (target == "*" and self.method == "OPTIONS") then
435 self:_error("400 Bad Request", "Invalid request target")
436 end
437 end
438 -- parse GET params:
439 if self.query then
440 self.get_params_list = read_urlencoded_form(request.query)
441 self.get_params = get_first_values(self.get_params_list)
442 end
443 -- parse cookies:
444 for i, line in ipairs(self.headers["Cookie"]) do
445 for rawkey, rawvalue in
446 string.gmatch(line, "([^=; ]*)=([^=; ]*)")
447 do
448 self.cookies[decode_uri(rawkey)] = decode_uri(rawvalue)
449 end
450 end
451 end
453 function request_pt:_read_body()
454 self:_assert_not_faulty()
455 local remaining = self._body_size_limit
456 if request.headers_flags["Transfer-Encoding"]["chunked"] then
457 while true do
458 local line, status = self:_read(32 + remaining, "\n")
459 if status == "maxlen" then
460 self:_error("400 Bad Request", "Request body size limit exceeded")
461 return false
462 elseif status == "eof" then
463 self:_error("400 Bad Request", "Encoding error or unexpected EOF while reading next chunk of request body")
464 return false
465 end
466 local zeros, lenstr = string.match(line, "^(0*)([1-9A-Fa-f]+[0-9A-Fa-f]*)\r?\n$")
467 local chunkext
468 if lenstr then
469 chunkext = ""
470 else
471 zeros, lenstr, chunkext = string.match(line, "^(0*)([1-9A-Fa-f]+[0-9A-Fa-f]*)([ \t;].-)\r?\n$")
472 end
473 if not lenstr or #lenstr > 13 then
474 self:_error("400 Bad Request", "Encoding error while reading chunk of request body")
475 return false
476 end
477 local len = tonumber("0x" .. lenstr)
478 remaining = remaining - (#zeros + #chunkext + len)
479 if remaining < 0 then
480 self:_error("400 Bad Request", "Request body size limit exceeded")
481 return false
482 end
483 if len == 0 then break end
484 if self:_read_body_bytes(len) == false then
485 return false
486 end
487 local term, status = self:_read(2, "\n")
488 if status == "eof" then
489 self:_error("400 Bad Request", "Unexpected EOF while reading next chunk of request body")
490 return false
491 end
492 if term ~= "\r\n" and term ~= "\n" then
493 self:_error("400 Bad Request", "Encoding error while reading chunk of request body")
494 return false
495 end
496 end
497 while true do
498 local line, status = self:_read(2 + remaining, "\n")
499 if status == "eof" then
500 self:_error("400 Bad Request", "Unexpected EOF while reading chunk of request body")
501 return false
502 end
503 if line == "\r\n" or line == "\n" then break end
504 remaining = remaining - #line
505 if remaining < 0 then
506 request_error(true, "413 Request Entity Too Large", "Request body size limit exceeded while reading trailer section of chunked request body")
507 end
508 end
509 elseif request_body_content_length then
510 if self._read_body_bytes(request_body_content_length) == false then
511 return false
512 end
513 end
514 end
516 function request_pt:_read_body_bytes(remaining, callback)
517 while remaining > 0 do
518 local limit
519 if remaining > self._input_chunk_size then
520 limit = self._input_chunk_size
521 else
522 limit = remaining
523 end
524 local chunk, status = self:_read(limit)
525 if status == "eof" then
526 self:_error("400 Bad Request", "Unexpected EOF while reading chunk of request body")
527 return false
528 end
529 remaining = remaining - limit
530 if self._body_streamer then
531 self._body_streamer(chunk)
532 end
533 end
534 end
536 function request_pt:_assert_not_faulty()
537 assert(not self._faulty, "Tried to use faulty request handle")
538 end
540 function request_pt:_write_yield()
541 self:_consume_input()
542 self._poll(self._socket_set, self._socket_set)
543 end
545 function request_pt:_write(...)
546 assert(self._socket:write_call(self._write_yield_closure, ...))
547 end
549 function request_pt:_flush(...)
550 assert(self._socket:write_call(self._write_yield_closure, ...))
551 end
553 function request_pt:_drain_input()
554 socket:drain_nb(self._input_chunk_size)
555 end
557 -- function creating a HTTP handler:
558 function generate_handler(handler, options)
559 -- swap arguments if necessary (for convenience):
560 if type(handler) ~= "function" and type(options) == "function" then
561 handler, options = options, handler
562 end
563 local request = setmetatable({}, request_mt)
564 request:_init(handler, options)
565 return request._handler_closure
566 end
568 return _M

Impressum / About Us