moonbridge

view moonbridge_http.lua @ 175:4cf337821a52

Minor code cleanup and changed default timeout values
author jbe
date Thu Jun 18 04:09:21 2015 +0200 (2015-06-18)
parents d6db92e0f231
children b2d024220782
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 -- handling of GET/POST param tables:
87 local new_params_list -- defined later
88 do
89 local params_list_mapping = setmetatable({}, {__mode="k"})
90 local function nextnonempty(tbl, key)
91 while true do
92 key = next(tbl, key)
93 if key == nil then
94 return nil
95 end
96 local value = tbl[key]
97 if #value > 0 then
98 return key, value
99 end
100 end
101 end
102 local function nextvalue(tbl, key)
103 while true do
104 key = next(tbl, key)
105 if key == nil then
106 return nil
107 end
108 local value = tbl[key][1]
109 if value ~= nil then
110 return key, value
111 end
112 end
113 end
114 local params_list_metatable = {
115 __index = function(self, key)
116 local tbl = {}
117 self[key] = tbl
118 return tbl
119 end,
120 __pairs = function(self)
121 return nextnonempty, self, nil
122 end
123 }
124 local params_metatable = {
125 __index = function(self, key)
126 return params_list_mapping[self][key][1]
127 end,
128 __newindex = function(self, key, value)
129 params_list_mapping[self][key] = {value}
130 end,
131 __pairs = function(self)
132 return nextvalue, params_list_mapping[self], nil
133 end
134 }
135 -- function that returns a table to store key value-list pairs,
136 -- and a second table automatically mapping keys to the first value
137 -- using the key value-list pairs in the first table:
138 new_params_list = function()
139 local params_list = setmetatable({}, params_list_metatable)
140 local params = setmetatable({}, params_metatable)
141 params_list_mapping[params] = params_list
142 return params_list, params
143 end
144 end
146 -- function parsing URL encoded form data and storing it in
147 -- a key value-list pairs structure that has to be
148 -- previously obtained by calling by new_params_list():
149 local function read_urlencoded_form(tbl, data)
150 for rawkey, rawvalue in string.gmatch(data, "([^?=&]*)=([^?=&]*)") do
151 local subtbl = tbl[decode_uri(rawkey)]
152 subtbl[#subtbl+1] = decode_uri(rawvalue)
153 end
154 end
156 -- function to convert a HTTP request handler to a socket handler:
157 function generate_handler(handler, options)
158 -- swap arguments if necessary (for convenience):
159 if type(handler) ~= "function" and type(options) == "function" then
160 handler, options = options, handler
161 end
162 -- helper function to process options:
163 local function default(name, default_value)
164 local value = options[name]
165 if value == nil then
166 return default_value
167 else
168 return value or nil
169 end
170 end
171 -- process options:
172 options = options or {}
173 local preamble = "" -- preamble sent with every(!) HTTP response
174 do
175 -- named arg "static_headers" is used to create the preamble:
176 local s = options.static_headers
177 local t = {}
178 if s then
179 if type(s) == "string" then
180 for line in string.gmatch(s, "[^\r\n]+") do
181 t[#t+1] = line
182 end
183 else
184 for i, kv in ipairs(s) do
185 if type(kv) == "string" then
186 t[#t+1] = kv
187 else
188 t[#t+1] = kv[1] .. ": " .. kv[2]
189 end
190 end
191 end
192 end
193 t[#t+1] = ""
194 preamble = table.concat(t, "\r\n")
195 end
196 local input_chunk_size = options.maximum_input_chunk_size or options.chunk_size or 16384
197 local output_chunk_size = options.minimum_output_chunk_size or options.chunk_size or 1024
198 local header_size_limit = options.header_size_limit or 1024*1024
199 local body_size_limit = options.body_size_limit or 64*1024*1024
200 local request_idle_timeout = default("request_idle_timeout", 65)
201 local request_header_timeout = default("request_header_timeout", 30)
202 local request_body_timeout = default("request_body_timeout", 300)
203 local response_timeout = default("response_timeout", 1800)
204 local poll = options.poll_function or moonbridge_io.poll
205 -- return socket handler:
206 return function(socket)
207 local socket_set = {[socket] = true} -- used for poll function
208 local survive = true -- set to false if process shall be terminated later
209 local consume -- function that reads some input if possible
210 -- function that drains some input if possible:
211 local function drain()
212 local bytes, status = socket:drain_nb(input_chunk_size)
213 if not bytes or status == "eof" then
214 consume = nil
215 end
216 end
217 -- function trying to unblock socket by reading:
218 local function unblock()
219 if consume then
220 poll(socket_set, socket_set)
221 consume()
222 else
223 poll(nil, socket_set)
224 end
225 end
226 -- function that enforces consumption of all input:
227 local function consume_all()
228 while consume do
229 poll(socket_set, nil)
230 consume()
231 end
232 end
233 -- handle requests in a loop:
234 repeat
235 -- copy limits:
236 local remaining_header_size_limit = header_size_limit
237 local remaining_body_size_limit = body_size_limit
238 -- table for caching nil values:
239 local headers_value_nil = {}
240 -- create a new request object with metatable:
241 local request -- allow references to local variable
242 request = {
243 -- allow access to underlying socket:
244 socket = socket,
245 -- cookies are simply stored in a table:
246 cookies = {},
247 -- table mapping header field names to value-lists
248 -- (raw access, but case-insensitive):
249 headers = setmetatable({}, {
250 __index = function(self, key)
251 local lowerkey = string.lower(key)
252 if lowerkey == key then
253 return
254 end
255 local result = rawget(self, lowerkey)
256 if result == nil then
257 result = {}
258 end
259 self[lowerkey] = result
260 self[key] = result
261 return result
262 end
263 }),
264 -- table mapping header field names to value-lists
265 -- (for headers with comma separated values):
266 headers_csv_table = setmetatable({}, {
267 __index = function(self, key)
268 local result = {}
269 for i, line in ipairs(request.headers[key]) do
270 for entry in string.gmatch(line, "[^,]+") do
271 local value = string.match(entry, "^[ \t]*(..-)[ \t]*$")
272 if value then
273 result[#result+1] = value
274 end
275 end
276 end
277 self[key] = result
278 return result
279 end
280 }),
281 -- table mapping header field names to a comma separated string
282 -- (for headers with comma separated values):
283 headers_csv_string = setmetatable({}, {
284 __index = function(self, key)
285 local result = {}
286 for i, line in ipairs(request.headers[key]) do
287 result[#result+1] = line
288 end
289 result = table.concat(result, ", ")
290 self[key] = result
291 return result
292 end
293 }),
294 -- table mapping header field names to a single string value
295 -- (or false if header has been sent multiple times):
296 headers_value = setmetatable({}, {
297 __index = function(self, key)
298 if headers_value_nil[key] then
299 return nil
300 end
301 local result = nil
302 local values = request.headers_csv_table[key]
303 if #values == 0 then
304 headers_value_nil[key] = true
305 elseif #values == 1 then
306 result = values[1]
307 else
308 result = false
309 end
310 self[key] = result
311 return result
312 end
313 }),
314 -- table mapping header field names to a flag table,
315 -- indicating if the comma separated value contains certain entries:
316 headers_flags = setmetatable({}, {
317 __index = function(self, key)
318 local result = setmetatable({}, {
319 __index = function(self, key)
320 local lowerkey = string.lower(key)
321 local result = rawget(self, lowerkey) or false
322 self[lowerkey] = result
323 self[key] = result
324 return result
325 end
326 })
327 for i, value in ipairs(request.headers_csv_table[key]) do
328 result[string.lower(value)] = true
329 end
330 self[key] = result
331 return result
332 end
333 })
334 }
335 -- create metatable for request object:
336 local request_mt = {}
337 setmetatable(request, request_mt)
338 -- callback for request body streaming:
339 local process_body_chunk
340 -- local variables to track the state:
341 local state = "init" -- one of:
342 -- "init" (initial state)
343 -- "no_status_sent" (configuration complete)
344 -- "info_status_sent" (1xx status code has been sent)
345 -- "bodyless_status_sent" (204/304 status code has been sent)
346 -- "status_sent" (regular status code has been sent)
347 -- "headers_sent" (headers have been terminated)
348 -- "finished" (request has been answered completely)
349 -- "faulty" (I/O or protocaol error)
350 local close_requested = false -- "Connection: close" requested
351 local close_responded = false -- "Connection: close" sent
352 local content_length = nil -- value of Content-Length header sent
353 local chunk_parts = {} -- list of chunks to send
354 local chunk_bytes = 0 -- sum of lengths of chunks to send
355 local streamed_post_params = {} -- mapping from POST field name to stream function
356 local streamed_post_param_patterns = {} -- list of POST field pattern and stream function pairs
357 -- functions to assert proper output/closing:
358 local function assert_output(...)
359 local retval, errmsg = ...
360 if retval then return ... end
361 state = "faulty"
362 socket:reset()
363 error("Could not send data to client: " .. errmsg)
364 end
365 local function assert_close(...)
366 local retval, errmsg = ...
367 if retval then return ... end
368 state = "faulty"
369 error("Could not finish sending data to client: " .. errmsg)
370 end
371 -- function to assert non-faulty handle:
372 local function assert_not_faulty()
373 assert(state ~= "faulty", "Tried to use faulty request handle")
374 end
375 -- functions to send data to the browser:
376 local function send(...)
377 assert_output(socket:write_call(unblock, ...))
378 end
379 local function send_flush(...)
380 assert_output(socket:flush_call(unblock, ...))
381 end
382 -- function to finish request:
383 local function finish()
384 if close_responded then
385 -- discard any input:
386 consume = drain
387 -- close output stream:
388 send_flush()
389 assert_close(socket:finish())
390 -- wait for EOF of peer to avoid immediate TCP RST condition:
391 consume_all()
392 -- fully close socket:
393 assert_close(socket:close())
394 else
395 send_flush()
396 process_body_chunk = nil
397 consume_all()
398 end
399 end
400 -- function that writes out buffered chunks (without flushing the socket):
401 function send_chunk()
402 if chunk_bytes > 0 then
403 assert_output(socket:write(string.format("%x\r\n", chunk_bytes)))
404 for i = 1, #chunk_parts do -- TODO: evaluated only once?
405 send(chunk_parts[i])
406 chunk_parts[i] = nil
407 end
408 chunk_bytes = 0
409 send("\r\n")
410 end
411 end
412 -- function to report an error:
413 local function request_error(throw_error, status, text)
414 local errmsg = "Error while reading request from client. Error response: " .. status
415 if text then
416 errmsg = errmsg .. " (" .. text .. ")"
417 end
418 if
419 state == "init" or
420 state == "no_status_sent" or
421 state == "info_status_sent"
422 then
423 local error_response_status, errmsg2 = pcall(function()
424 request:monologue()
425 request:send_status(status)
426 request:send_header("Content-Type", "text/plain")
427 request:send_data(status, "\n")
428 if text then
429 request:send_data("\n", text, "\n")
430 end
431 request:finish()
432 end)
433 if not error_response_status then
434 error("Unexpected error while sending error response: " .. errmsg2)
435 end
436 elseif state ~= "faulty" then
437 state = "faulty"
438 assert_close(socket:reset())
439 end
440 if throw_error then
441 error(errmsg)
442 else
443 return survive
444 end
445 end
446 -- read functions
447 local function read(...)
448 local data, status = socket:read_yield(...)
449 if data == nil then
450 request_error(true, "400 Bad Request", "Read error")
451 end
452 if status == "eof" then
453 request_error(true, "400 Bad Request", "Unexpected EOF")
454 end
455 return data
456 end
457 local function read_eof(...)
458 local data, status = socket:read_yield(...)
459 if data == nil then
460 request_error(true, "400 Bad Request", "Read error")
461 end
462 if status == "eof" then
463 if data == "" then
464 return nil
465 else
466 request_error(true, "400 Bad Request", "Unexpected EOF")
467 end
468 end
469 return data
470 end
471 -- reads a number of bytes from the socket,
472 -- optionally feeding these bytes chunk-wise
473 -- into a callback function:
474 local function read_body_bytes(remaining)
475 while remaining > 0 do
476 local limit
477 if remaining > input_chunk_size then
478 limit = input_chunk_size
479 else
480 limit = remaining
481 end
482 local chunk = read(limit)
483 remaining = remaining - limit
484 if process_body_chunk then
485 process_body_chunk(chunk)
486 end
487 end
488 end
489 -- coroutine for request body processing:
490 local function read_body()
491 if request.headers_flags["Transfer-Encoding"]["chunked"] then
492 while true do
493 local line = read(32 + remaining_body_size_limit, "\n")
494 local zeros, lenstr = string.match(line, "^(0*)([1-9A-Fa-f]+[0-9A-Fa-f]*)\r?\n$")
495 local chunkext
496 if lenstr then
497 chunkext = ""
498 else
499 zeros, lenstr, chunkext = string.match(line, "^(0*)([1-9A-Fa-f]+[0-9A-Fa-f]*)([ \t;].-)\r?\n$")
500 end
501 if not lenstr or #lenstr > 13 then
502 request_error(true, "400 Bad Request", "Encoding error while reading chunk of request body")
503 end
504 local len = tonumber("0x" .. lenstr)
505 remaining_body_size_limit = remaining_body_size_limit - (#zeros + #chunkext + len)
506 if remaining_body_size_limit < 0 then
507 request_error(true, "413 Request Entity Too Large", "Request body size limit exceeded")
508 end
509 if len == 0 then break end
510 read_body_bytes(len)
511 local term = read(2, "\n")
512 if term ~= "\r\n" and term ~= "\n" then
513 request_error(true, "400 Bad Request", "Encoding error while reading chunk of request body")
514 end
515 end
516 while true do
517 local line = read(2 + remaining_body_size_limit, "\n")
518 if line == "\r\n" or line == "\n" then break end
519 remaining_body_size_limit = remaining_body_size_limit - #line
520 if remaining_body_size_limit < 0 then
521 request_error(true, "413 Request Entity Too Large", "Request body size limit exceeded while reading trailer section of chunked request body")
522 end
523 end
524 elseif request_body_content_length then
525 read_body_bytes(request_body_content_length)
526 end
527 end
528 -- function to setup default request body handling:
529 local function default_request_body_handling()
530 local post_params_list, post_params = new_params_list()
531 local content_type = request.headers_value["Content-Type"]
532 if content_type then
533 if
534 content_type == "application/x-www-form-urlencoded" or
535 string.match(content_type, "^application/x%-www%-form%-urlencoded *;")
536 then
537 read_urlencoded_form(post_params_list, request.body)
538 request.post_params_list, request.post_params = post_params_list, post_params
539 else
540 local boundary = string.match(
541 content_type,
542 '^multipart/form%-data[ \t]*[;,][ \t]*boundary="([^"]+)"$'
543 ) or string.match(
544 content_type,
545 '^multipart/form%-data[ \t]*[;,][ \t]*boundary=([^"; \t]+)$'
546 )
547 if boundary then
548 local post_metadata_list, post_metadata = new_params_list()
549 boundary = "--" .. boundary
550 local headerdata = ""
551 local streamer
552 local field_name
553 local metadata = {}
554 local value_parts
555 local function default_streamer(chunk)
556 value_parts[#value_parts+1] = chunk
557 end
558 local function stream_part_finish()
559 if streamer == default_streamer then
560 local value = table.concat(value_parts)
561 value_parts = nil
562 if field_name then
563 local values = post_params_list[field_name]
564 values[#values+1] = value
565 local metadata_entries = post_metadata_list[field_name]
566 metadata_entries[#metadata_entries+1] = metadata
567 end
568 else
569 streamer()
570 end
571 headerdata = ""
572 streamer = nil
573 field_name = nil
574 metadata = {}
575 end
576 local function stream_part_chunk(chunk)
577 if streamer then
578 streamer(chunk)
579 else
580 headerdata = headerdata .. chunk
581 while true do
582 local line, remaining = string.match(headerdata, "^(.-)\r?\n(.*)$")
583 if not line then
584 break
585 end
586 if line == "" then
587 streamer = streamed_post_params[field_name]
588 if not streamer then
589 for i, rule in ipairs(streamed_post_param_patterns) do
590 if string.match(field_name, rule[1]) then
591 streamer = rule[2]
592 break
593 end
594 end
595 end
596 if not streamer then
597 value_parts = {}
598 streamer = default_streamer
599 end
600 streamer(remaining, field_name, metadata)
601 return
602 end
603 headerdata = remaining
604 local header_key, header_value = string.match(line, "^([^:]*):[ \t]*(.-)[ \t]*$")
605 if not header_key then
606 request_error(true, "400 Bad Request", "Invalid header in multipart/form-data part")
607 end
608 header_key = string.lower(header_key)
609 if header_key == "content-disposition" then
610 local escaped_header_value = string.gsub(header_value, '"[^"]*"', function(str)
611 return string.gsub(str, "=", "==")
612 end)
613 field_name = string.match(escaped_header_value, ';[ \t]*name="([^"]*)"')
614 if field_name then
615 field_name = string.gsub(field_name, "==", "=")
616 else
617 field_name = string.match(header_value, ';[ \t]*name=([^"; \t]+)')
618 end
619 metadata.file_name = string.match(escaped_header_value, ';[ \t]*filename="([^"]*)"')
620 if metadata.file_name then
621 metadata.file_name = string.gsub(metadata.file_name, "==", "=")
622 else
623 string.match(header_value, ';[ \t]*filename=([^"; \t]+)')
624 end
625 elseif header_key == "content-type" then
626 metadata.content_type = header_value
627 elseif header_key == "content-transfer-encoding" then
628 request_error(true, "400 Bad Request", "Content-transfer-encoding not supported by multipart/form-data parser")
629 end
630 end
631 end
632 end
633 local skippart = true -- ignore data until first boundary
634 local afterbound = false -- interpret 2 bytes after boundary ("\r\n" or "--")
635 local terminated = false -- final boundary read
636 local bigchunk = ""
637 request:set_request_body_streamer(function(chunk)
638 if chunk == nil then
639 if not terminated then
640 request_error(true, "400 Bad Request", "Premature end of multipart/form-data request body")
641 end
642 request.post_metadata_list, request.post_metadata = post_metadata_list, post_metadata
643 end
644 if terminated then
645 return
646 end
647 bigchunk = bigchunk .. chunk
648 while true do
649 if afterbound then
650 if #bigchunk <= 2 then
651 return
652 end
653 local terminator = string.sub(bigchunk, 1, 2)
654 if terminator == "\r\n" then
655 afterbound = false
656 bigchunk = string.sub(bigchunk, 3)
657 elseif terminator == "--" then
658 terminated = true
659 bigchunk = nil
660 return
661 else
662 request_error(true, "400 Bad Request", "Error while parsing multipart body (expected CRLF or double minus)")
663 end
664 end
665 local pos1, pos2 = string.find(bigchunk, boundary, 1, true)
666 if not pos1 then
667 if not skippart then
668 local safe = #bigchunk-#boundary
669 if safe > 0 then
670 stream_part_chunk(string.sub(bigchunk, 1, safe))
671 bigchunk = string.sub(bigchunk, safe+1)
672 end
673 end
674 return
675 end
676 if not skippart then
677 stream_part_chunk(string.sub(bigchunk, 1, pos1 - 1))
678 stream_part_finish()
679 else
680 boundary = "\r\n" .. boundary
681 skippart = false
682 end
683 bigchunk = string.sub(bigchunk, pos2 + 1)
684 afterbound = true
685 end
686 end)
687 else
688 request_error(true, "415 Unsupported Media Type", "Unknown Content-Type of request body")
689 end
690 end
691 end
692 end
693 -- function to prepare body processing:
694 local function prepare()
695 assert_not_faulty()
696 if process_body_chunk == nil then
697 default_request_body_handling()
698 end
699 if state ~= "init" then
700 return
701 end
702 consume = coroutine.wrap(read_body)
703 state = "no_status_sent"
704 if request.headers_flags["Expect"]["100-continue"] then
705 request:send_status("100 Continue")
706 request:finish_headers()
707 end
708 end
709 -- method to ignore input and close connection after response:
710 function request:monologue()
711 assert_not_faulty()
712 if
713 state == "headers_sent" or
714 state == "finished"
715 then
716 error("All HTTP headers have already been sent")
717 end
718 local old_state = state
719 state = "faulty"
720 consume = drain
721 close_requested = true
722 if old_state == "init" then
723 state = "no_status_sent"
724 else
725 state = old_state
726 end
727 end
728 --
729 -- method to send a HTTP response status (e.g. "200 OK"):
730 function request:send_status(status)
731 prepare()
732 local old_state = state
733 state = "faulty"
734 if old_state == "info_status_sent" then
735 send_flush("\r\n")
736 elseif old_state ~= "no_status_sent" then
737 error("HTTP status has already been sent")
738 end
739 local status1 = string.sub(status, 1, 1)
740 local status3 = string.sub(status, 1, 3)
741 send("HTTP/1.1 ", status, "\r\n", preamble)
742 local wrb = status_without_response_body[status3]
743 if wrb then
744 state = "bodyless_status_sent"
745 if wrb == "zero_content_length" then
746 request:send_header("Content-Length", 0)
747 end
748 elseif status1 == "1" then
749 state = "info_status_sent"
750 else
751 state = "status_sent"
752 end
753 end
754 -- method to send a HTTP response header:
755 -- (key and value must be provided as separate args)
756 function request:send_header(key, value)
757 assert_not_faulty()
758 if state == "init" or state == "no_status_sent" then
759 error("HTTP status has not been sent yet")
760 elseif
761 state == "headers_sent" or
762 state == "finished"
763 then
764 error("All HTTP headers have already been sent")
765 end
766 local key_lower = string.lower(key)
767 if key_lower == "content-length" then
768 if state == "info_status_sent" then
769 error("Cannot set Content-Length for informational status response")
770 end
771 local cl = assert(tonumber(value), "Invalid content-length")
772 if content_length == nil then
773 content_length = cl
774 elseif content_length == cl then
775 return
776 else
777 error("Content-Length has been set multiple times with different values")
778 end
779 elseif key_lower == "connection" then
780 for entry in string.gmatch(string.lower(value), "[^,]+") do
781 if string.match(entry, "^[ \t]*close[ \t]*$") then
782 if state == "info_status_sent" then
783 error("Cannot set \"Connection: close\" for informational status response")
784 end
785 close_responded = true
786 break
787 end
788 end
789 end
790 assert_output(socket:write(key, ": ", value, "\r\n"))
791 end
792 -- function to terminate header section in response, optionally flushing:
793 -- (may be called multiple times unless response is finished)
794 local function finish_headers(with_flush)
795 if state == "finished" then
796 error("Response has already been finished")
797 elseif state == "info_status_sent" then
798 send_flush("\r\n")
799 state = "no_status_sent"
800 elseif state == "bodyless_status_sent" then
801 if close_requested and not close_responded then
802 request:send_header("Connection", "close")
803 end
804 send("\r\n")
805 finish()
806 state = "finished"
807 elseif state == "status_sent" then
808 if not content_length then
809 request:send_header("Transfer-Encoding", "chunked")
810 end
811 if close_requested and not close_responded then
812 request:send_header("Connection", "close")
813 end
814 send("\r\n")
815 if request.method == "HEAD" then
816 finish()
817 elseif with_flush then
818 send_flush()
819 end
820 state = "headers_sent"
821 elseif state ~= "headers_sent" then
822 error("HTTP status has not been sent yet")
823 end
824 end
825 -- method to finish and flush headers:
826 function request:finish_headers()
827 assert_not_faulty()
828 finish_headers(true)
829 end
830 -- method to send body data:
831 function request:send_data(...)
832 assert_not_faulty()
833 if output_state == "info_status_sent" then
834 error("No (non-informational) HTTP status has been sent yet")
835 elseif output_state == "bodyless_status_sent" then
836 error("Cannot send response data for body-less status message")
837 end
838 finish_headers(false)
839 if output_state ~= "headers_sent" then
840 error("Unexpected internal status in HTTP engine")
841 end
842 if request.method == "HEAD" then
843 return
844 end
845 for i = 1, select("#", ...) do
846 local str = tostring(select(i, ...))
847 if #str > 0 then
848 if content_length then
849 local bytes_to_send = #str
850 if bytes_sent + bytes_to_send > content_length then
851 error("Content length exceeded")
852 else
853 send(str)
854 bytes_sent = bytes_sent + bytes_to_send
855 end
856 else
857 chunk_bytes = chunk_bytes + #str
858 chunk_parts[#chunk_parts+1] = str
859 end
860 end
861 end
862 if chunk_bytes >= output_chunk_size then
863 send_chunk()
864 end
865 end
866 -- method to flush output buffer:
867 function request:flush()
868 assert_not_faulty()
869 send_chunk()
870 send_flush()
871 end
872 -- method to finish response:
873 function request:finish()
874 assert_not_faulty()
875 if state == "finished" then
876 return
877 elseif state == "info_status_sent" then
878 error("Informational HTTP response can be finished with :finish_headers() method")
879 end
880 finish_headers(false)
881 if state == "headers_sent" then
882 if request.method ~= "HEAD" then
883 state = "faulty"
884 if content_length then
885 if bytes_sent ~= content_length then
886 error("Content length not used")
887 end
888 else
889 send_chunk()
890 send("0\r\n\r\n")
891 end
892 finish()
893 end
894 state = "finished"
895 elseif state ~= "finished" then
896 error("Unexpected internal status in HTTP engine")
897 end
898 end
899 -- method to register POST param stream handler for a single field name:
900 function request:stream_post_param(field_name, callback)
901 if state ~= "init" then
902 error("Cannot setup request body streamer at this stage")
903 end
904 streamed_post_params[field_name] = callback
905 end
906 -- method to register POST param stream handler for a field name pattern:
907 function request:stream_post_params(pattern, callback)
908 if state ~= "init" then
909 error("Cannot setup request body streamer at this stage")
910 end
911 streamed_post_param_patterns[#streamed_post_param_patterns+1] = {pattern, callback}
912 end
913 -- method to register request body stream handler
914 function request:set_request_body_streamer(callback)
915 if state ~= "init" then
916 error("Cannot setup request body streamer at this stage")
917 end
918 local inprogress = false
919 local buffer = {}
920 process_body_chunk = function(chunk)
921 if inprogress then
922 buffer[#buffer+1] = chunk
923 else
924 inprogress = true
925 callback(chunk)
926 while #buffer > 0 do
927 chunk = table.concat(buffer)
928 buffer = {}
929 callback(chunk)
930 end
931 inprogress = false
932 end
933 end
934 end
935 -- method to start reading request body
936 function request:consume_input()
937 prepare()
938 consume_all()
939 end
940 -- method to stream request body
941 function request:stream_request_body(callback)
942 request:set_request_body_streamer(function(chunk)
943 if chunk ~= nil then
944 callback(chunk)
945 end
946 end)
947 request:consume_input()
948 end
949 -- metamethod to read special attibutes of request object:
950 function request_mt:__index(key, value)
951 if key == "body" then
952 local chunks = {}
953 request:stream_request_body(function(chunk)
954 chunks[#chunks+1] = chunk
955 end)
956 self.body = table.concat(chunks)
957 return self.body
958 elseif
959 key == "post_params_list" or key == "post_params" or
960 key == "post_metadata_list" or key == "post_metadata"
961 then
962 prepare()
963 consume_all()
964 return self[key]
965 end
966 end
967 -- coroutine for reading headers:
968 local function read_headers()
969 -- read and parse request line:
970 local line = read_eof(remaining_header_size_limit, "\n")
971 if not line then
972 return false, survive
973 end
974 remaining_header_size_limit = remaining_header_size_limit - #line
975 if remaining_header_size_limit == 0 then
976 return false, request_error(false, "414 Request-URI Too Long")
977 end
978 local target, proto
979 request.method, target, proto =
980 line:match("^([^ \t\r]+)[ \t]+([^ \t\r]+)[ \t]*([^ \t\r]*)[ \t]*\r?\n$")
981 if not request.method then
982 return false, request_error(false, "400 Bad Request")
983 elseif proto ~= "HTTP/1.1" then
984 return false, request_error(false, "505 HTTP Version Not Supported")
985 end
986 -- read and parse headers:
987 while true do
988 local line = read(remaining_header_size_limit, "\n");
989 remaining_header_size_limit = remaining_header_size_limit - #line
990 if line == "\r\n" or line == "\n" then
991 break
992 end
993 if remaining_header_size_limit == 0 then
994 return false, request_error(false, "431 Request Header Fields Too Large")
995 end
996 local key, value = string.match(line, "^([^ \t\r]+):[ \t]*(.-)[ \t]*\r?\n$")
997 if not key then
998 return false, request_error(false, "400 Bad Request")
999 end
1000 local values = request.headers[key]
1001 values[#values+1] = value
1002 end
1003 return true -- success
1004 end
1005 -- wait for input:
1006 if not poll(socket_set, nil, request_idle_timeout) then
1007 return request_error(false, "408 Request Timeout", "Idle connection timed out")
1008 end
1009 -- read headers (with timeout):
1010 do
1011 local coro = coroutine.wrap(read_headers)
1012 local starttime = request_header_timeout and moonbridge_io.timeref()
1013 while true do
1014 local status, retval = coro()
1015 if status == nil then
1016 local remaining
1017 if request_header_timeout then
1018 remaining = request_header_timeout - moonbridge_io.timeref(starttime)
1019 end
1020 if not poll(socket_set, nil, remaining) then
1021 return request_error(false, "408 Request Timeout", "Timeout while receiving headers")
1022 end
1023 elseif status == false then
1024 return retval
1025 elseif status == true then
1026 break
1027 else
1028 error("Unexpected yield value")
1029 end
1030 end
1031 end
1032 -- process "Connection: close" header if existent:
1033 connection_close_requested = request.headers_flags["Connection"]["close"]
1034 -- process "Content-Length" header if existent:
1035 do
1036 local values = request.headers_csv_table["Content-Length"]
1037 if #values > 0 then
1038 request_body_content_length = tonumber(values[1])
1039 local proper_value = tostring(request_body_content_length)
1040 for i, value in ipairs(values) do
1041 value = string.match(value, "^0*(.*)")
1042 if value ~= proper_value then
1043 return request_error(false, "400 Bad Request", "Content-Length header(s) invalid")
1044 end
1045 end
1046 if request_body_content_length > remaining_body_size_limit then
1047 return request_error(false, "413 Request Entity Too Large", "Announced request body size is too big")
1048 end
1049 end
1050 end
1051 -- process "Transfer-Encoding" header if existent:
1052 do
1053 local flag = request.headers_flags["Transfer-Encoding"]["chunked"]
1054 local list = request.headers_csv_table["Transfer-Encoding"]
1055 if (flag and #list ~= 1) or (not flag and #list ~= 0) then
1056 return request_error(false, "400 Bad Request", "Unexpected Transfer-Encoding")
1057 end
1058 end
1059 -- process "Expect" header if existent:
1060 for i, value in ipairs(request.headers_csv_table["Expect"]) do
1061 if string.lower(value) ~= "100-continue" then
1062 return request_error(false, "417 Expectation Failed", "Unexpected Expect header")
1063 end
1064 end
1065 -- get mandatory Host header according to RFC 7230:
1066 request.host = request.headers_value["Host"]
1067 if not request.host then
1068 return request_error(false, "400 Bad Request", "No valid host header")
1069 end
1070 -- parse request target:
1071 request.path, request.query = string.match(target, "^/([^?]*)(.*)$")
1072 if not request.path then
1073 local host2
1074 host2, request.path, request.query = string.match(target, "^[Hh][Tt][Tt][Pp]://([^/?]+)/?([^?]*)(.*)$")
1075 if host2 then
1076 if request.host ~= host2 then
1077 return request_error(false, "400 Bad Request", "No valid host header")
1078 end
1079 elseif not (target == "*" and request.method == "OPTIONS") then
1080 return request_error(false, "400 Bad Request", "Invalid request target")
1081 end
1082 end
1083 -- parse GET params:
1084 if request.query then
1085 read_urlencoded_form(request.get_params_list, request.query)
1086 end
1087 -- parse cookies:
1088 for i, line in ipairs(request.headers["Cookie"]) do
1089 for rawkey, rawvalue in
1090 string.gmatch(line, "([^=; ]*)=([^=; ]*)")
1091 do
1092 request.cookies[decode_uri(rawkey)] = decode_uri(rawvalue)
1093 end
1094 end
1095 -- (re)set timeout for handler:
1096 timeout(response_timeout or 0)
1097 -- call underlying handler and remember boolean result:
1098 if handler(request) ~= true then survive = false end
1099 -- finish request (unless already done by underlying handler):
1100 request:finish()
1101 -- stop timeout timer:
1102 timeout(0)
1103 until close_responded
1104 return survive
1105 end
1106 end
1108 return _M

Impressum / About Us