moonbridge

view moonbridge_http.lua @ 161:d5b8295d035e

Discard minimal changes to HTTP module in favor of new reimplementation
author jbe
date Thu Jun 11 00:54:18 2015 +0200 (2015-06-11)
parents 573995950b0b 8ab33bfb47e7
children 6c1561547f79
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 -- parses URL encoded form data:
87 local function read_urlencoded_form(data)
88 local tbl = {}
89 for rawkey, rawvalue in string.gmatch(data, "([^?=&]*)=([^?=&]*)") do
90 local key = decode_uri(rawkey)
91 local value = decode_uri(rawvalue)
92 local subtbl = tbl[key]
93 if subtbl then
94 subtbl[#subtbl+1] = value
95 else
96 tbl[key] = {value}
97 end
98 end
99 return tbl
100 end
102 -- extracts first value from each subtable:
103 local function get_first_values(tbl)
104 local newtbl = {}
105 for key, subtbl in pairs(tbl) do
106 newtbl[key] = subtbl[1]
107 end
108 return newtbl
109 end
111 function generate_handler(handler, options)
112 -- swap arguments if necessary (for convenience):
113 if type(handler) ~= "function" and type(options) == "function" then
114 handler, options = options, handler
115 end
116 -- helper function to process options:
117 local function default(name, default_value)
118 local value = options[name]
119 if value == nil then
120 return default_value
121 else
122 return value or nil
123 end
124 end
125 -- process options:
126 options = options or {}
127 local preamble = "" -- preamble sent with every(!) HTTP response
128 do
129 -- named arg "static_headers" is used to create the preamble:
130 local s = options.static_headers
131 local t = {}
132 if s then
133 if type(s) == "string" then
134 for line in string.gmatch(s, "[^\r\n]+") do
135 t[#t+1] = line
136 end
137 else
138 for i, kv in ipairs(options.static_headers) do
139 if type(kv) == "string" then
140 t[#t+1] = kv
141 else
142 t[#t+1] = kv[1] .. ": " .. kv[2]
143 end
144 end
145 end
146 end
147 t[#t+1] = ""
148 preamble = table.concat(t, "\r\n")
149 end
150 local input_chunk_size = options.maximum_input_chunk_size or options.chunk_size or 16384
151 local output_chunk_size = options.minimum_output_chunk_size or options.chunk_size or 1024
152 local header_size_limit = options.header_size_limit or 1024*1024
153 local body_size_limit = options.body_size_limit or 64*1024*1024
154 local request_idle_timeout = default("request_idle_timeout", 330)
155 local request_header_timeout = default("request_header_timeout", 30)
156 local request_body_timeout = default("request_body_timeout", 60)
157 local request_response_timeout = default("request_response_timeout", 1800)
158 local poll = options.poll_function or moonbridge_io.poll
159 -- return socket handler:
160 return function(socket)
161 local socket_set = {[socket] = true} -- used for poll function
162 local survive = true -- set to false if process shall be terminated later
163 local consume -- function that reads some input if possible
164 -- function that drains some input if possible:
165 local function drain()
166 local bytes, status = assert(socket:drain_nb(input_chunk_size))
167 if status == "eof" then
168 consume = nil
169 end
170 end
171 local function unblock()
172 if consume then
173 poll(socket_set, socket_set)
174 consume()
175 else
176 poll(nil, socket_set)
177 end
178 end
179 repeat
180 local request = {
181 socket = socket,
182 cookies = {}
183 }
184 local function send(...)
185 assert(socket:write_call(unblock, ...))
186 end
187 -- wait for input:
188 if not poll(socket_set, nil, request_idle_timeout) then
189 -- TODO: send error
190 return survive
191 end
192 until connection_close_responded
193 return survive
194 end
195 end
197 return _M

Impressum / About Us