moonbridge

view moonbridge_http.lua @ 59:fd0fe0adb203

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

Impressum / About Us