moonbridge

view moonbridge_http.lua @ 282:850f5c8fec37

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

Impressum / About Us