moonbridge

view moonbridge_http.lua @ 50:0dd15d642124

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

Impressum / About Us