moonbridge

view moonbridge_http.lua @ 155:2c22b0f222c7

Further work on new HTTP layer
author jbe
date Mon May 25 02:28:34 2015 +0200 (2015-05-25)
parents 831f2d4b2d73
children 0c4221702ce1
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 local function init_timeout(name, default)
144 local value = options[name]
145 if value == nil then
146 self["_"..name] = default
147 else
148 self["_"..name] = value
149 end
150 end
151 init_timeout("request_idle_timeout", 330)
152 init_timeout("request_header_timeout", 30)
153 init_timeout("request_body_timeout", 1800)
154 init_timeout("response_timeout", 1830)
155 self._poll = options.poll_function or moonbridge_io.poll
156 self:_create_closure("_write_yield")
157 self:_create_closure("_handler")
158 self:_create_header_metatables()
159 end
161 function request_pt:_create_closure(name)
162 self[name.."_closure"] = function(...)
163 return self[name](self, ...)
164 end
165 end
167 function request_pt:_create_header_metatables()
168 -- table mapping header field names to value-lists:
169 self._headers_mt = {
170 __index = function(tbl, key)
171 local lowerkey = string.lower(key)
172 local result = self._headers[lowerkey]
173 if result == nil then
174 result = {}
175 end
176 tbl[lowerkey] = result
177 tbl[key] = result
178 return result
179 end
180 }
181 -- table mapping header field names to value-lists
182 -- (for headers with comma separated values):
183 self._headers_csv_table_mt = {
184 __index = function(tbl, key)
185 local result = {}
186 for i, line in ipairs(self.headers[key]) do
187 for entry in string.gmatch(line, "[^,]+") do
188 local value = string.match(entry, "^[ \t]*(..-)[ \t]*$")
189 if value then
190 result[#result+1] = value
191 end
192 end
193 end
194 tbl[key] = result
195 return result
196 end
197 }
198 -- table mapping header field names to a comma separated string
199 -- (for headers with comma separated values):
200 self._headers_csv_string_mt = {
201 __index = function(tbl, key)
202 local result = {}
203 for i, line in ipairs(self.headers[key]) do
204 result[#result+1] = line
205 end
206 result = string.concat(result, ", ")
207 tbl[key] = result
208 return result
209 end
210 }
211 -- table mapping header field names to a single string value
212 -- (or false if header has been sent multiple times):
213 self._headers_value_mt = {
214 __index = function(tbl, key)
215 if self._headers_value_nil[key] then
216 return nil
217 end
218 local result = nil
219 local values = self.headers_csv_table[key]
220 if #values == 0 then
221 self._headers_value_nil[key] = true
222 elseif #values == 1 then
223 result = values[1]
224 else
225 result = false
226 end
227 tbl[key] = result
228 return result
229 end
230 }
231 -- table mapping header field names to a flag table,
232 -- indicating if the comma separated value contains certain entries:
233 self._headers_flags_mt = {
234 __index = function(tbl, key)
235 local result = setmetatable({}, {
236 __index = function(tbl, key)
237 local lowerkey = string.lower(key)
238 local result = rawget(tbl, lowerkey) or false
239 tbl[lowerkey] = result
240 tbl[key] = result
241 return result
242 end
243 })
244 for i, value in ipairs(self.headers_csv_table[key]) do
245 result[string.lower(value)] = true
246 end
247 tbl[key] = result
248 return result
249 end
250 }
251 end
253 function request_pt:_create_magictable(name)
254 self[name] = setmetatable({}, self["_"..name.."_mt"])
255 end
257 function request_pt:_handler(socket)
258 self._socket = socket
259 self._survive = true
260 self._socket_set = {[socket] = true}
261 self._faulty = false
262 self._consume_input = self._drain_input
263 self._headers = {}
264 self._headers_value_nil = {}
265 self._connection_close_requested = false
266 self._connection_close_responded = false
267 self:_create_magictable("headers")
268 self:_create_magictable("headers_csv_table")
269 self:_create_magictable("headers_csv_string")
270 self:_create_magictable("headers_value")
271 self:_create_magictable("headers_flags")
272 self.cookies = {}
273 repeat
274 -- wait for input:
275 if not moonbridge_io.poll(self._socket_set, nil, self._request_idle_timeout) then
276 self:_error("408 Request Timeout", "Idle connection timed out")
277 return self._survive
278 end
279 -- read headers (with timeout):
280 do
281 local coro = coroutine.wrap(self._read_headers)
282 local timeout = self._request_header_timeout
283 local starttime = timeout and moonbridge_io.timeref()
284 while true do
285 local status = coro(self)
286 if status == nil then
287 local remaining
288 if timeout then
289 remaining = timeout - moonbridge_io.timeref(starttime)
290 end
291 if not self._poll(self._socket_set, nil, remaining) then
292 self:_error("408 Request Timeout", "Timeout while receiving headers")
293 return self._survive
294 end
295 elseif status == false then
296 return self._survive
297 elseif status == true then
298 break
299 else
300 error("Unexpected yield value")
301 end
302 end
303 end
304 timeout(self._response_timeout or 0)
305 if self._application_handler(self) ~= true then
306 self._survive = false
307 end
308 request:finish()
309 timeout(0)
310 until self._connection_close_responded
311 return self._survive
312 end
314 function request_pt:_error(status, explanation)
315 end
317 function request_pt:_read(...)
318 local line, status = self._socket:read_yield(...)
319 if line == nil then
320 self._faulty = true
321 error(status)
322 else
323 return line, status
324 end
325 end
327 function request_pt:_read_headers()
328 local remaining = self._header_size_limit
329 -- read and parse request line:
330 local target, proto
331 do
332 local line, status = self:_read(remaining-2, "\n")
333 if status == "maxlen" then
334 self:_error("414 Request-URI Too Long")
335 return false
336 elseif status == "eof" then
337 if line ~= "" then
338 self:_error("400 Bad Request", "Unexpected EOF in request-URI line")
339 end
340 return false
341 end
342 remaining = remaining - #line
343 self.method, target, proto =
344 line:match("^([^ \t\r]+)[ \t]+([^ \t\r]+)[ \t]*([^ \t\r]*)[ \t]*\r?\n$")
345 if not request.method then
346 self:_error("400 Bad Request", "Invalid request-URI line")
347 return false
348 elseif proto ~= "HTTP/1.1" then
349 self:_error("505 HTTP Version Not Supported")
350 return false
351 end
352 end
353 -- read and parse headers:
354 while true do
355 local line, status = self:_read(remaining, "\n");
356 if status == "maxlen" then
357 self:_error("431 Request Header Fields Too Large")
358 return false
359 elseif status == "eof" then
360 self:_error("400 Bad Request", "Unexpected EOF in request headers")
361 return false
362 end
363 remaining = remaining - #line
364 if line == "\r\n" or line == "\n" then
365 break
366 end
367 local key, value = string.match(line, "^([^ \t\r]+):[ \t]*(.-)[ \t]*\r?\n$")
368 if not key then
369 self:_error("400 Bad Request", "Invalid header line")
370 return false
371 end
372 local lowerkey = key:lower()
373 local values = self._headers[lowerkey]
374 if values then
375 values[#values+1] = value
376 else
377 self._headers[lowerkey] = {value}
378 end
379 end
380 -- process "Connection: close" header if existent:
381 self._connection_close_requested = self.headers_flags["Connection"]["close"]
382 -- process "Content-Length" header if existent:
383 do
384 local values = self.headers_csv_table["Content-Length"]
385 if #values > 0 then
386 self._request_body_content_length = tonumber(values[1])
387 local proper_value = tostring(request_body_content_length)
388 for i, value in ipairs(values) do
389 value = string.match(value, "^0*(.*)")
390 if value ~= proper_value then
391 self:_error("400 Bad Request", "Content-Length header(s) invalid")
392 return false
393 end
394 end
395 if request_body_content_length > self._body_size_limit then
396 self:_error("413 Request Entity Too Large", "Announced request body size is too big")
397 return false
398 end
399 end
400 end
401 -- process "Transfer-Encoding" header if existent:
402 do
403 local flag = self.headers_flags["Transfer-Encoding"]["chunked"]
404 local list = self.headers_csv_table["Transfer-Encoding"]
405 if (flag and #list ~= 1) or (not flag and #list ~= 0) then
406 self:_error("400 Bad Request", "Unexpected Transfer-Encoding")
407 return false
408 end
409 end
410 -- process "Expect" header if existent:
411 for i, value in ipairs(self.headers_csv_table["Expect"]) do
412 if string.lower(value) ~= "100-continue" then
413 self:_error("417 Expectation Failed", "Unexpected Expect header")
414 return false
415 end
416 end
417 -- get mandatory Host header according to RFC 7230:
418 self.host = self.headers_value["Host"]
419 if not self.host then
420 self:_error("400 Bad Request", "No valid host header")
421 return false
422 end
423 -- parse request target:
424 self.path, self.query = string.match(target, "^/([^?]*)(.*)$")
425 if not self.path then
426 local host2
427 host2, self.path, self.query = string.match(target, "^[Hh][Tt][Tt][Pp]://([^/?]+)/?([^?]*)(.*)$")
428 if host2 then
429 if self.host ~= host2 then
430 self:_error("400 Bad Request", "No valid host header")
431 return false
432 end
433 elseif not (target == "*" and self.method == "OPTIONS") then
434 self:_error("400 Bad Request", "Invalid request target")
435 end
436 end
437 -- parse GET params:
438 if self.query then
439 self.get_params_list = read_urlencoded_form(request.query)
440 self.get_params = get_first_values(self.get_params_list)
441 end
442 -- parse cookies:
443 for i, line in ipairs(self.headers["Cookie"]) do
444 for rawkey, rawvalue in
445 string.gmatch(line, "([^=; ]*)=([^=; ]*)")
446 do
447 self.cookies[decode_uri(rawkey)] = decode_uri(rawvalue)
448 end
449 end
450 end
452 function request_pt:_assert_not_faulty()
453 assert(not self._faulty, "Tried to use faulty request handle")
454 end
456 function request_pt:_write_yield()
457 self:_consume_input()
458 self._poll(self._socket_set, self._socket_set)
459 end
461 function request_pt:_write(...)
462 assert(self._socket:write_call(self._write_yield_closure, ...))
463 end
465 function request_pt:_flush(...)
466 assert(self._socket:write_call(self._write_yield_closure, ...))
467 end
469 function request_pt:_drain_input()
470 socket:drain_nb(self._input_chunk_size)
471 end
473 -- function creating a HTTP handler:
474 function generate_handler(handler, options)
475 -- swap arguments if necessary (for convenience):
476 if type(handler) ~= "function" and type(options) == "function" then
477 handler, options = options, handler
478 end
479 local request = setmetatable({}, request_mt)
480 request:_init(handler, options)
481 return request._handler_closure
482 end
484 return _M

Impressum / About Us