moonbridge

view moonbridge_http.lua @ 52:042bb4854aa6

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

Impressum / About Us