moonbridge

view moonbridge_http.lua @ 165:00f01d945e13

Work on new HTTP module implementation
author jbe
date Sat Jun 13 18:02:57 2015 +0200 (2015-06-13)
parents 27dc025e76cc
children 41a44ae5c293
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 function generate_handler(handler, options)
112 -- swap arguments if necessary (for convenience):
113 if type(handler) ~= "function" and type(options) == "function" then
114 handler, options = options, handler
115 end
116 -- helper function to process options:
117 local function default(name, default_value)
118 local value = options[name]
119 if value == nil then
120 return default_value
121 else
122 return value or nil
123 end
124 end
125 -- process options:
126 options = options or {}
127 local preamble = "" -- preamble sent with every(!) HTTP response
128 do
129 -- named arg "static_headers" is used to create the preamble:
130 local s = options.static_headers
131 local t = {}
132 if s then
133 if type(s) == "string" then
134 for line in string.gmatch(s, "[^\r\n]+") do
135 t[#t+1] = line
136 end
137 else
138 for i, kv in ipairs(options.static_headers) do
139 if type(kv) == "string" then
140 t[#t+1] = kv
141 else
142 t[#t+1] = kv[1] .. ": " .. kv[2]
143 end
144 end
145 end
146 end
147 t[#t+1] = ""
148 preamble = table.concat(t, "\r\n")
149 end
150 local input_chunk_size = options.maximum_input_chunk_size or options.chunk_size or 16384
151 local output_chunk_size = options.minimum_output_chunk_size or options.chunk_size or 1024
152 local header_size_limit = options.header_size_limit or 1024*1024
153 local body_size_limit = options.body_size_limit or 64*1024*1024
154 local request_idle_timeout = default("request_idle_timeout", 330)
155 local request_header_timeout = default("request_header_timeout", 30)
156 local request_body_timeout = default("request_body_timeout", 60)
157 local request_response_timeout = default("request_response_timeout", 1800)
158 local poll = options.poll_function or moonbridge_io.poll
159 -- return socket handler:
160 return function(socket)
161 local socket_set = {[socket] = true} -- used for poll function
162 local survive = true -- set to false if process shall be terminated later
163 local consume -- function that reads some input if possible
164 -- function that drains some input if possible:
165 local function drain()
166 local bytes, status = socket:drain_nb(input_chunk_size)
167 if not bytes or status == "eof" then
168 consume = nil
169 end
170 end
171 -- function trying to unblock socket by reading:
172 local function unblock()
173 if consume then
174 poll(socket_set, socket_set)
175 consume()
176 else
177 poll(nil, socket_set)
178 end
179 end
180 -- function that enforces consumption of all input:
181 local function consume_all()
182 while consume do
183 poll(socket_set, nil)
184 consume()
185 end
186 end
187 -- handle requests in a loop:
188 repeat
189 -- create a new request object:
190 local request = {
191 -- allow access to underlying socket:
192 socket = socket,
193 -- cookies are simply stored in a table:
194 cookies = {},
195 -- table mapping header field names to value-lists
196 -- (raw access, but case-insensitive):
197 headers = setmetatable({}, {
198 __index = function(self, key)
199 local lowerkey = string.lower(key)
200 if lowerkey == key then
201 return
202 end
203 local result = rawget(self, lowerkey)
204 if result == nil then
205 result = {}
206 end
207 self[lowerkey] = result
208 self[key] = result
209 return result
210 end
211 }),
212 -- table mapping header field names to value-lists
213 -- (for headers with comma separated values):
214 headers_csv_table = setmetatable({}, {
215 __index = function(self, key)
216 local result = {}
217 for i, line in ipairs(request.headers[key]) do
218 for entry in string.gmatch(line, "[^,]+") do
219 local value = string.match(entry, "^[ \t]*(..-)[ \t]*$")
220 if value then
221 result[#result+1] = value
222 end
223 end
224 end
225 self[key] = result
226 return result
227 end
228 }),
229 -- table mapping header field names to a comma separated string
230 -- (for headers with comma separated values):
231 headers_csv_string = setmetatable({}, {
232 __index = function(self, key)
233 local result = {}
234 for i, line in ipairs(request.headers[key]) do
235 result[#result+1] = line
236 end
237 result = string.concat(result, ", ")
238 self[key] = result
239 return result
240 end
241 }),
242 -- table mapping header field names to a single string value
243 -- (or false if header has been sent multiple times):
244 headers_value = setmetatable({}, {
245 __index = function(self, key)
246 if headers_value_nil[key] then
247 return nil
248 end
249 local result = nil
250 local values = request.headers_csv_table[key]
251 if #values == 0 then
252 headers_value_nil[key] = true
253 elseif #values == 1 then
254 result = values[1]
255 else
256 result = false
257 end
258 self[key] = result
259 return result
260 end
261 }),
262 -- table mapping header field names to a flag table,
263 -- indicating if the comma separated value contains certain entries:
264 headers_flags = setmetatable({}, {
265 __index = function(self, key)
266 local result = setmetatable({}, {
267 __index = function(self, key)
268 local lowerkey = string.lower(key)
269 local result = rawget(self, lowerkey) or false
270 self[lowerkey] = result
271 self[key] = result
272 return result
273 end
274 })
275 for i, value in ipairs(request.headers_csv_table[key]) do
276 result[string.lower(value)] = true
277 end
278 self[key] = result
279 return result
280 end
281 })
282 }
283 -- local variables to track the state:
284 local state = "init" -- one of:
285 -- "init" (initial state)
286 -- "prepare" (configureation in preparation)
287 -- "no_status_sent" (configuration complete)
288 -- "info_status_sent" (1xx status code has been sent)
289 -- "bodyless_status_sent" (204/304 status code has been sent)
290 -- "status_sent" (regular status code has been sent)
291 -- "headers_sent" (headers have been terminated)
292 -- "finished" (request has been answered completely)
293 -- "faulty" (I/O or protocaol error)
294 local close_requested = false -- "Connection: close" requested
295 local close_responded = false -- "Connection: close" sent
296 local content_length = nil -- value of Content-Length header sent
297 local chunk_parts = {} -- list of chunks to send
298 local chunk_bytes = 0 -- sum of lengths of chunks to send
299 -- functions to assert proper output/closing:
300 local function assert_output(...)
301 local retval, errmsg = ...
302 if retval then return ... end
303 state = "faulty"
304 socket:reset()
305 error("Could not send data to client: " .. errmsg)
306 end
307 local function assert_close(...)
308 local retval, errmsg = ...
309 if retval then return ... end
310 state = "faulty"
311 error("Could not finish sending data to client: " .. errmsg)
312 end
313 -- function to assert non-faulty handle:
314 local function assert_not_faulty()
315 assert(state ~= "faulty", "Tried to use faulty request handle")
316 end
317 -- functions to send data to the browser:
318 local function send(...)
319 assert_output(socket:write_call(unblock, ...))
320 end
321 local function send_flush(...)
322 assert_output(socket:flush_call(unblock, ...))
323 end
324 -- function to finish request:
325 local function finish()
326 if close_responded then
327 -- discard any input:
328 consume = drain
329 -- close output stream:
330 send_flush()
331 assert_close(socket:finish())
332 -- wait for EOF of peer to avoid immediate TCP RST condition:
333 consume_all()
334 -- fully close socket:
335 assert_close(socket:close())
336 else
337 send_flush()
338 consume_all()
339 end
340 end
341 -- function that writes out buffered chunks (without flushing the socket):
342 function send_chunk()
343 if chunk_bytes > 0 then
344 assert_output(socket:write(string.format("%x\r\n", chunk_bytes)))
345 for i = 1, #chunk_parts do -- TODO: evaluated only once?
346 send(chunk_parts[i])
347 chunk_parts[i] = nil
348 end
349 chunk_bytes = 0
350 send("\r\n")
351 end
352 end
353 -- function to prepare (or skip) body processing:
354 local function prepare()
355 assert_not_faulty()
356 if state == "prepare" then
357 error("Unexpected internal status in HTTP engine (recursive prepare)")
358 elseif state ~= "init" then
359 return
360 end
361 state = "prepare"
362 -- TODO
363 state = "no_status_sent"
364 end
365 -- method to ignore input and close connection after response:
366 function request:monologue()
367 assert_not_faulty()
368 if
369 state == "headers_sent" or
370 state == "finished"
371 then
372 error("All HTTP headers have already been sent")
373 end
374 local old_state = state
375 state = "faulty"
376 consume = drain
377 close_requested = true
378 if old_state == "init" or old_state == "prepare" then -- TODO: ok?
379 state = "no_status_sent"
380 else
381 state = old_state
382 end
383 end
384 --
385 -- method to send a HTTP response status (e.g. "200 OK"):
386 function request:send_status(status)
387 prepare()
388 local old_state = state
389 state = "faulty"
390 if old_state == "info_status_sent" then
391 send_flush("\r\n")
392 elseif old_state ~= "no_status_sent" then
393 error("HTTP status has already been sent")
394 end
395 local status1 = string.sub(status, 1, 1)
396 local status3 = string.sub(status, 1, 3)
397 send("HTTP/1.1 ", status, "\r\n", preamble)
398 local wrb = status_without_response_body[status3]
399 if wrb then
400 state = "bodyless_status_sent"
401 if wrb == "zero_content_length" then
402 request:send_header("Content-Length", 0)
403 end
404 elseif status1 == "1" then
405 state = "info_status_sent"
406 else
407 state = "status_sent"
408 end
409 end
410 -- method to send a HTTP response header:
411 -- (key and value must be provided as separate args)
412 function request:send_header(key, value)
413 assert_not_faulty()
414 if
415 state == "init" or
416 state == "prepare" or
417 state == "no_status_sent"
418 then
419 error("HTTP status has not been sent yet")
420 elseif
421 state == "headers_sent" or
422 state == "finished"
423 then
424 error("All HTTP headers have already been sent")
425 end
426 local key_lower = string.lower(key)
427 if key_lower == "content-length" then
428 if state == "info_status_sent" then
429 error("Cannot set Content-Length for informational status response")
430 end
431 local cl = assert(tonumber(value), "Invalid content-length")
432 if content_length == nil then
433 content_length = cl
434 elseif content_length == cl then
435 return
436 else
437 error("Content-Length has been set multiple times with different values")
438 end
439 elseif key_lower == "connection" then
440 for entry in string.gmatch(string.lower(value), "[^,]+") do
441 if string.match(entry, "^[ \t]*close[ \t]*$") then
442 if state == "info_status_sent" then
443 error("Cannot set \"Connection: close\" for informational status response")
444 end
445 close_responded = true
446 break
447 end
448 end
449 end
450 assert_output(socket:write(key, ": ", value, "\r\n"))
451 end
452 -- function to terminate header section in response, optionally flushing:
453 -- (may be called multiple times unless response is finished)
454 local function finish_headers(with_flush)
455 if state == "finished" then
456 error("Response has already been finished")
457 elseif state == "info_status_sent" then
458 send_flush("\r\n")
459 state = "no_status_sent"
460 elseif state == "bodyless_status_sent" then
461 if close_requested and not close_responded then
462 request:send_header("Connection", "close")
463 end
464 send("\r\n")
465 finish()
466 state = "finished"
467 elseif state == "status_sent" then
468 if not content_length then
469 request:send_header("Transfer-Encoding", "chunked")
470 end
471 if close_requested and not close_responded then
472 request:send_header("Connection", "close")
473 end
474 send("\r\n")
475 if request.method == "HEAD" then
476 finish()
477 elseif with_flush then
478 send_flush()
479 end
480 state = "headers_sent"
481 elseif state ~= "headers_sent" then
482 error("HTTP status has not been sent yet")
483 end
484 end
485 -- method to finish and flush headers:
486 function request:finish_headers()
487 assert_not_faulty()
488 finish_headers(true)
489 end
490 -- method to send body data:
491 function request:send_data(...)
492 assert_not_faulty()
493 if output_state == "info_status_sent" then
494 error("No (non-informational) HTTP status has been sent yet")
495 elseif output_state == "bodyless_status_sent" then
496 error("Cannot send response data for body-less status message")
497 end
498 finish_headers(false)
499 if output_state ~= "headers_sent" then
500 error("Unexpected internal status in HTTP engine")
501 end
502 if request.method == "HEAD" then
503 return
504 end
505 for i = 1, select("#", ...) do
506 local str = tostring(select(i, ...))
507 if #str > 0 then
508 if content_length then
509 local bytes_to_send = #str
510 if bytes_sent + bytes_to_send > content_length then
511 error("Content length exceeded")
512 else
513 send(str)
514 bytes_sent = bytes_sent + bytes_to_send
515 end
516 else
517 chunk_bytes = chunk_bytes + #str
518 chunk_parts[#chunk_parts+1] = str
519 end
520 end
521 end
522 if chunk_bytes >= output_chunk_size then
523 send_chunk()
524 end
525 end
526 -- method to flush output buffer:
527 function request:flush()
528 assert_not_faulty()
529 send_chunk()
530 send_flush()
531 end
532 -- method to finish response:
533 function request:finish()
534 assert_not_faulty()
535 if state == "finished" then
536 return
537 elseif state == "info_status_sent" then
538 error("Informational HTTP response can be finished with :finish_headers() method")
539 end
540 finish_headers(false)
541 if state == "headers_sent" then
542 if request.method ~= "HEAD" then
543 state = "faulty"
544 if content_length then
545 if bytes_sent ~= content_length then
546 error("Content length not used")
547 end
548 else
549 send_chunk()
550 send("0\r\n\r\n")
551 end
552 finish()
553 end
554 state = "finished"
555 elseif state ~= "finished" then
556 error("Unexpected internal status in HTTP engine")
557 end
558 end
559 -- function to report an error:
560 local function request_error(throw_error, status, text)
561 local errmsg = "Error while reading request from client. Error response: " .. status
562 if text then
563 errmsg = errmsg .. " (" .. text .. ")"
564 end
565 if
566 state == "init" or
567 state == "prepare" or
568 state == "no_status_sent" or
569 state == "info_status_sent"
570 then
571 local error_response_status, errmsg2 = pcall(function()
572 request:monologue()
573 request:send_status(status)
574 request:send_header("Content-Type", "text/plain")
575 request:send_data(status, "\n")
576 if text then
577 request:send_data("\n", text, "\n")
578 end
579 request:finish()
580 end)
581 if not error_response_status then
582 error("Unexpected error while sending error response: " .. errmsg2)
583 end
584 elseif state ~= "faulty" then
585 assert_close(socket:reset())
586 end
587 if throw_error then
588 error(errmsg)
589 else
590 return survive
591 end
592 end
593 -- wait for input:
594 if not poll(socket_set, nil, request_idle_timeout) then
595 return request_error(false, "408 Request Timeout", "Idle connection timed out")
596 end
597 until close_responded
598 return survive
599 end
600 end
602 return _M

Impressum / About Us