moonbridge

view moonbridge_http.lua @ 173:6e80bcf89bd5

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

Impressum / About Us