moonbridge

view moonbridge_http.lua @ 202:2ed3d94a0eb7

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

Impressum / About Us