moonbridge

view moonbridge_http.lua @ 122:3bbcd75eefcd

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

Impressum / About Us