moonbridge

view moonbridge_http.lua @ 201:4e72725118d0

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

Impressum / About Us