moonbridge

annotate moonbridge_http.lua @ 164:27dc025e76cc

Work on new HTTP module implementation
author jbe
date Sat Jun 13 00:55:01 2015 +0200 (2015-06-13)
parents 80266ee1593f
children 00f01d945e13
rev   line source
jbe@0 1 #!/usr/bin/env lua
jbe@0 2
jbe@0 3 -- module preamble
jbe@0 4 local _G, _M = _ENV, {}
jbe@0 5 _ENV = setmetatable({}, {
jbe@0 6 __index = function(self, key)
jbe@0 7 local value = _M[key]; if value ~= nil then return value end
jbe@0 8 return _G[key]
jbe@0 9 end,
jbe@63 10 __newindex = _M
jbe@0 11 })
jbe@0 12
jbe@0 13 -- function that encodes certain HTML entities:
jbe@0 14 -- (not used by the library itself)
jbe@0 15 function encode_html(text)
jbe@0 16 return (
jbe@0 17 string.gsub(
jbe@0 18 text, '[<>&"]',
jbe@0 19 function(char)
jbe@0 20 if char == '<' then
jbe@0 21 return "&lt;"
jbe@0 22 elseif char == '>' then
jbe@0 23 return "&gt;"
jbe@0 24 elseif char == '&' then
jbe@0 25 return "&amp;"
jbe@0 26 elseif char == '"' then
jbe@0 27 return "&quot;"
jbe@0 28 end
jbe@0 29 end
jbe@0 30 )
jbe@0 31 )
jbe@0 32
jbe@0 33 end
jbe@0 34
jbe@0 35 -- function that encodes special characters for URIs:
jbe@0 36 -- (not used by the library itself)
jbe@0 37 function encode_uri(text)
jbe@0 38 return (
jbe@0 39 string.gsub(text, "[^0-9A-Za-z_%.~-]",
jbe@0 40 function (char)
jbe@0 41 return string.format("%%%02x", string.byte(char))
jbe@0 42 end
jbe@0 43 )
jbe@0 44 )
jbe@0 45 end
jbe@0 46
jbe@0 47 -- function undoing URL encoding:
jbe@0 48 do
jbe@0 49 local b0 = string.byte("0")
jbe@0 50 local b9 = string.byte("9")
jbe@0 51 local bA = string.byte("A")
jbe@0 52 local bF = string.byte("F")
jbe@0 53 local ba = string.byte("a")
jbe@0 54 local bf = string.byte("f")
jbe@0 55 function decode_uri(str)
jbe@0 56 return (
jbe@0 57 string.gsub(
jbe@0 58 string.gsub(str, "%+", " "),
jbe@0 59 "%%([0-9A-Fa-f][0-9A-Fa-f])",
jbe@0 60 function(hex)
jbe@0 61 local n1, n2 = string.byte(hex, 1, 2)
jbe@0 62 if n1 >= b0 and n1 <= b9 then n1 = n1 - b0
jbe@0 63 elseif n1 >= bA and n1 <= bF then n1 = n1 - bA + 10
jbe@0 64 elseif n1 >= ba and n1 <= bf then n1 = n1 - ba + 10
jbe@0 65 else error("Assertion failed") end
jbe@0 66 if n2 >= b0 and n2 <= b9 then n2 = n2 - b0
jbe@0 67 elseif n2 >= bA and n2 <= bF then n2 = n2 - bA + 10
jbe@0 68 elseif n2 >= ba and n2 <= bf then n2 = n2 - ba + 10
jbe@0 69 else error("Assertion failed") end
jbe@0 70 return string.char(n1 * 16 + n2)
jbe@0 71 end
jbe@0 72 )
jbe@0 73 )
jbe@0 74 end
jbe@0 75 end
jbe@0 76
jbe@0 77 -- status codes that carry no response body (in addition to 1xx):
jbe@0 78 -- (set to "zero_content_length" if Content-Length header is required)
jbe@0 79 status_without_response_body = {
jbe@5 80 ["101"] = true, -- list 101 to allow protocol switch
jbe@0 81 ["204"] = true,
jbe@0 82 ["205"] = "zero_content_length",
jbe@0 83 ["304"] = true
jbe@0 84 }
jbe@0 85
jbe@154 86 -- parses URL encoded form data:
jbe@154 87 local function read_urlencoded_form(data)
jbe@154 88 local tbl = {}
jbe@154 89 for rawkey, rawvalue in string.gmatch(data, "([^?=&]*)=([^?=&]*)") do
jbe@154 90 local key = decode_uri(rawkey)
jbe@154 91 local value = decode_uri(rawvalue)
jbe@154 92 local subtbl = tbl[key]
jbe@154 93 if subtbl then
jbe@154 94 subtbl[#subtbl+1] = value
jbe@154 95 else
jbe@154 96 tbl[key] = {value}
jbe@35 97 end
jbe@35 98 end
jbe@154 99 return tbl
jbe@0 100 end
jbe@0 101
jbe@154 102 -- extracts first value from each subtable:
jbe@154 103 local function get_first_values(tbl)
jbe@154 104 local newtbl = {}
jbe@154 105 for key, subtbl in pairs(tbl) do
jbe@154 106 newtbl[key] = subtbl[1]
jbe@0 107 end
jbe@154 108 return newtbl
jbe@154 109 end
jbe@154 110
jbe@0 111 function generate_handler(handler, options)
jbe@0 112 -- swap arguments if necessary (for convenience):
jbe@0 113 if type(handler) ~= "function" and type(options) == "function" then
jbe@0 114 handler, options = options, handler
jbe@0 115 end
jbe@160 116 -- helper function to process options:
jbe@160 117 local function default(name, default_value)
jbe@160 118 local value = options[name]
jbe@160 119 if value == nil then
jbe@160 120 return default_value
jbe@160 121 else
jbe@160 122 return value or nil
jbe@159 123 end
jbe@160 124 end
jbe@0 125 -- process options:
jbe@0 126 options = options or {}
jbe@0 127 local preamble = "" -- preamble sent with every(!) HTTP response
jbe@0 128 do
jbe@0 129 -- named arg "static_headers" is used to create the preamble:
jbe@0 130 local s = options.static_headers
jbe@0 131 local t = {}
jbe@0 132 if s then
jbe@0 133 if type(s) == "string" then
jbe@0 134 for line in string.gmatch(s, "[^\r\n]+") do
jbe@0 135 t[#t+1] = line
jbe@0 136 end
jbe@0 137 else
jbe@0 138 for i, kv in ipairs(options.static_headers) do
jbe@0 139 if type(kv) == "string" then
jbe@0 140 t[#t+1] = kv
jbe@0 141 else
jbe@0 142 t[#t+1] = kv[1] .. ": " .. kv[2]
jbe@0 143 end
jbe@0 144 end
jbe@0 145 end
jbe@0 146 end
jbe@0 147 t[#t+1] = ""
jbe@0 148 preamble = table.concat(t, "\r\n")
jbe@0 149 end
jbe@160 150 local input_chunk_size = options.maximum_input_chunk_size or options.chunk_size or 16384
jbe@44 151 local output_chunk_size = options.minimum_output_chunk_size or options.chunk_size or 1024
jbe@160 152 local header_size_limit = options.header_size_limit or 1024*1024
jbe@160 153 local body_size_limit = options.body_size_limit or 64*1024*1024
jbe@160 154 local request_idle_timeout = default("request_idle_timeout", 330)
jbe@160 155 local request_header_timeout = default("request_header_timeout", 30)
jbe@160 156 local request_body_timeout = default("request_body_timeout", 60)
jbe@160 157 local request_response_timeout = default("request_response_timeout", 1800)
jbe@160 158 local poll = options.poll_function or moonbridge_io.poll
jbe@160 159 -- return socket handler:
jbe@0 160 return function(socket)
jbe@160 161 local socket_set = {[socket] = true} -- used for poll function
jbe@0 162 local survive = true -- set to false if process shall be terminated later
jbe@160 163 local consume -- function that reads some input if possible
jbe@160 164 -- function that drains some input if possible:
jbe@160 165 local function drain()
jbe@163 166 local bytes, status = socket:drain_nb(input_chunk_size)
jbe@163 167 if not bytes or status == "eof" then
jbe@160 168 consume = nil
jbe@50 169 end
jbe@159 170 end
jbe@163 171 -- function trying to unblock socket by reading:
jbe@160 172 local function unblock()
jbe@160 173 if consume then
jbe@160 174 poll(socket_set, socket_set)
jbe@160 175 consume()
jbe@160 176 else
jbe@160 177 poll(nil, socket_set)
jbe@0 178 end
jbe@154 179 end
jbe@163 180 -- function that enforces consumption of all input:
jbe@162 181 local function consume_all()
jbe@162 182 while consume do
jbe@163 183 poll(socket_set, nil)
jbe@162 184 consume()
jbe@162 185 end
jbe@162 186 end
jbe@163 187 -- handle requests in a loop:
jbe@160 188 repeat
jbe@162 189 -- create a new request object:
jbe@160 190 local request = {
jbe@0 191 socket = socket,
jbe@160 192 cookies = {}
jbe@0 193 }
jbe@162 194 -- local variables to track the state:
jbe@162 195 local state = "init" -- one of:
jbe@162 196 -- "init" (initial state)
jbe@163 197 -- "prepare" (configureation in preparation)
jbe@162 198 -- "no_status_sent" (configuration complete)
jbe@162 199 -- "info_status_sent" (1xx status code has been sent)
jbe@162 200 -- "bodyless_status_sent" (204/304 status code has been sent)
jbe@162 201 -- "status_sent" (regular status code has been sent)
jbe@162 202 -- "headers_sent" (headers have been terminated)
jbe@162 203 -- "finished" (request has been answered completely)
jbe@163 204 -- "faulty" (I/O or protocaol error)
jbe@162 205 local close_requested = false -- "Connection: close" requested
jbe@162 206 local close_responded = false -- "Connection: close" sent
jbe@162 207 local content_length = nil -- value of Content-Length header sent
jbe@164 208 local chunk_parts = {} -- list of chunks to send
jbe@164 209 local chunk_bytes = 0 -- sum of lengths of chunks to send
jbe@163 210 -- functions to assert proper output/closing:
jbe@163 211 local function assert_output(...)
jbe@163 212 local retval, errmsg = ...
jbe@163 213 if retval then return ... end
jbe@163 214 state = "faulty"
jbe@163 215 socket:reset()
jbe@163 216 error("Could not send data to client: " .. errmsg)
jbe@163 217 end
jbe@163 218 local function assert_close(...)
jbe@163 219 local retval, errmsg = ...
jbe@163 220 if retval then return ... end
jbe@163 221 state = "faulty"
jbe@163 222 error("Could not finish sending data to client: " .. errmsg)
jbe@163 223 end
jbe@164 224 -- function to assert non-faulty handle:
jbe@164 225 local function assert_not_faulty()
jbe@164 226 assert(state ~= "faulty", "Tried to use faulty request handle")
jbe@164 227 end
jbe@162 228 -- functions to send data to the browser:
jbe@160 229 local function send(...)
jbe@163 230 assert_output(socket:write_call(unblock, ...))
jbe@38 231 end
jbe@162 232 local function send_flush(...)
jbe@163 233 assert_output(socket:flush_call(unblock, ...))
jbe@162 234 end
jbe@163 235 -- function to finish request:
jbe@163 236 local function finish()
jbe@163 237 if close_responded then
jbe@163 238 -- discard any input:
jbe@163 239 consume = drain
jbe@163 240 -- close output stream:
jbe@163 241 send_flush()
jbe@163 242 assert_close(socket:finish())
jbe@163 243 -- wait for EOF of peer to avoid immediate TCP RST condition:
jbe@163 244 consume_all()
jbe@163 245 -- fully close socket:
jbe@163 246 assert_close(socket:close())
jbe@163 247 else
jbe@163 248 send_flush()
jbe@163 249 consume_all()
jbe@163 250 end
jbe@163 251 end
jbe@164 252 -- function that writes out buffered chunks (without flushing the socket):
jbe@164 253 function send_chunk()
jbe@164 254 if chunk_bytes > 0 then
jbe@164 255 assert_output(socket:write(string.format("%x\r\n", chunk_bytes)))
jbe@164 256 for i = 1, #chunk_parts do -- TODO: evaluated only once?
jbe@164 257 send(chunk_parts[i])
jbe@164 258 chunk_parts[i] = nil
jbe@164 259 end
jbe@164 260 chunk_bytes = 0
jbe@164 261 send("\r\n")
jbe@164 262 end
jbe@164 263 end
jbe@164 264 -- function to prepare (or skip) body processing:
jbe@162 265 local function prepare()
jbe@164 266 assert_not_faulty()
jbe@162 267 if state == "prepare" then
jbe@164 268 error("Unexpected internal status in HTTP engine (recursive prepare)")
jbe@162 269 elseif state ~= "init" then
jbe@162 270 return
jbe@162 271 end
jbe@162 272 state = "prepare"
jbe@162 273 -- TODO
jbe@162 274 state = "no_status_sent"
jbe@162 275 end
jbe@163 276 -- method to ignore input and close connection after response:
jbe@163 277 function request:monologue()
jbe@164 278 assert_not_faulty()
jbe@163 279 if
jbe@163 280 state == "headers_sent" or
jbe@163 281 state == "finished"
jbe@163 282 then
jbe@163 283 error("All HTTP headers have already been sent")
jbe@163 284 end
jbe@164 285 local old_state = state
jbe@164 286 state = "faulty"
jbe@163 287 consume = drain
jbe@163 288 close_requested = true
jbe@164 289 if old_state == "init" or old_state == "prepare" then -- TODO: ok?
jbe@163 290 state = "no_status_sent"
jbe@164 291 else
jbe@164 292 state = old_state
jbe@162 293 end
jbe@162 294 end
jbe@163 295 --
jbe@162 296 -- method to send a HTTP response status (e.g. "200 OK"):
jbe@162 297 function request:send_status(status)
jbe@162 298 prepare()
jbe@164 299 local old_state = state
jbe@164 300 state = "faulty"
jbe@164 301 if old_state == "info_status_sent" then
jbe@162 302 send_flush("\r\n")
jbe@164 303 elseif old_state ~= "no_status_sent" then
jbe@162 304 error("HTTP status has already been sent")
jbe@162 305 end
jbe@162 306 local status1 = string.sub(status, 1, 1)
jbe@162 307 local status3 = string.sub(status, 1, 3)
jbe@162 308 send("HTTP/1.1 ", status, "\r\n", preamble)
jbe@162 309 local wrb = status_without_response_body[status3]
jbe@162 310 if wrb then
jbe@162 311 state = "bodyless_status_sent"
jbe@162 312 if wrb == "zero_content_length" then
jbe@162 313 request:send_header("Content-Length", 0)
jbe@162 314 end
jbe@162 315 elseif status1 == "1" then
jbe@162 316 state = "info_status_sent"
jbe@162 317 else
jbe@162 318 state = "status_sent"
jbe@162 319 end
jbe@162 320 end
jbe@162 321 -- method to send a HTTP response header:
jbe@162 322 -- (key and value must be provided as separate args)
jbe@162 323 function request:send_header(key, value)
jbe@164 324 assert_not_faulty()
jbe@164 325 if
jbe@164 326 state == "init" or
jbe@164 327 state == "prepare" or
jbe@164 328 state == "no_status_sent"
jbe@164 329 then
jbe@162 330 error("HTTP status has not been sent yet")
jbe@162 331 elseif
jbe@164 332 state == "headers_sent" or
jbe@164 333 state == "finished"
jbe@162 334 then
jbe@162 335 error("All HTTP headers have already been sent")
jbe@162 336 end
jbe@162 337 local key_lower = string.lower(key)
jbe@162 338 if key_lower == "content-length" then
jbe@162 339 if state == "info_status_sent" then
jbe@162 340 error("Cannot set Content-Length for informational status response")
jbe@162 341 end
jbe@162 342 local cl = assert(tonumber(value), "Invalid content-length")
jbe@162 343 if content_length == nil then
jbe@162 344 content_length = cl
jbe@162 345 elseif content_length == cl then
jbe@162 346 return
jbe@162 347 else
jbe@162 348 error("Content-Length has been set multiple times with different values")
jbe@162 349 end
jbe@162 350 elseif key_lower == "connection" then
jbe@162 351 for entry in string.gmatch(string.lower(value), "[^,]+") do
jbe@162 352 if string.match(entry, "^[ \t]*close[ \t]*$") then
jbe@162 353 if state == "info_status_sent" then
jbe@162 354 error("Cannot set \"Connection: close\" for informational status response")
jbe@162 355 end
jbe@162 356 close_responded = true
jbe@162 357 break
jbe@162 358 end
jbe@162 359 end
jbe@162 360 end
jbe@162 361 assert_output(socket:write(key, ": ", value, "\r\n"))
jbe@162 362 end
jbe@162 363 -- function to terminate header section in response, optionally flushing:
jbe@162 364 -- (may be called multiple times unless response is finished)
jbe@162 365 local function finish_headers(with_flush)
jbe@162 366 if state == "finished" then
jbe@162 367 error("Response has already been finished")
jbe@162 368 elseif state == "info_status_sent" then
jbe@162 369 send_flush("\r\n")
jbe@162 370 state = "no_status_sent"
jbe@162 371 elseif state == "bodyless_status_sent" then
jbe@162 372 if close_requested and not close_responded then
jbe@162 373 request:send_header("Connection", "close")
jbe@162 374 end
jbe@162 375 send("\r\n")
jbe@163 376 finish()
jbe@162 377 state = "finished"
jbe@162 378 elseif state == "status_sent" then
jbe@162 379 if not content_length then
jbe@162 380 request:send_header("Transfer-Encoding", "chunked")
jbe@162 381 end
jbe@162 382 if close_requested and not close_responded then
jbe@162 383 request:send_header("Connection", "close")
jbe@162 384 end
jbe@162 385 send("\r\n")
jbe@162 386 if request.method == "HEAD" then
jbe@163 387 finish()
jbe@162 388 elseif with_flush then
jbe@162 389 send_flush()
jbe@162 390 end
jbe@162 391 state = "headers_sent"
jbe@162 392 elseif state ~= "headers_sent" then
jbe@162 393 error("HTTP status has not been sent yet")
jbe@162 394 end
jbe@162 395 end
jbe@162 396 -- method to finish and flush headers:
jbe@162 397 function request:finish_headers()
jbe@164 398 assert_not_faulty()
jbe@162 399 finish_headers(true)
jbe@162 400 end
jbe@164 401 -- method to send body data:
jbe@164 402 function request:send_data(...)
jbe@164 403 assert_not_faulty()
jbe@164 404 if output_state == "info_status_sent" then
jbe@164 405 error("No (non-informational) HTTP status has been sent yet")
jbe@164 406 elseif output_state == "bodyless_status_sent" then
jbe@164 407 error("Cannot send response data for body-less status message")
jbe@164 408 end
jbe@164 409 finish_headers(false)
jbe@164 410 if output_state ~= "headers_sent" then
jbe@164 411 error("Unexpected internal status in HTTP engine")
jbe@164 412 end
jbe@164 413 if request.method == "HEAD" then
jbe@164 414 return
jbe@164 415 end
jbe@164 416 for i = 1, select("#", ...) do
jbe@164 417 local str = tostring(select(i, ...))
jbe@164 418 if #str > 0 then
jbe@164 419 if content_length then
jbe@164 420 local bytes_to_send = #str
jbe@164 421 if bytes_sent + bytes_to_send > content_length then
jbe@164 422 error("Content length exceeded")
jbe@164 423 else
jbe@164 424 send(str)
jbe@164 425 bytes_sent = bytes_sent + bytes_to_send
jbe@164 426 end
jbe@164 427 else
jbe@164 428 chunk_bytes = chunk_bytes + #str
jbe@164 429 chunk_parts[#chunk_parts+1] = str
jbe@164 430 end
jbe@164 431 end
jbe@164 432 end
jbe@164 433 if chunk_bytes >= output_chunk_size then
jbe@164 434 send_chunk()
jbe@164 435 end
jbe@164 436 end
jbe@163 437 -- function to report an error:
jbe@163 438 local function request_error(throw_error, status, text)
jbe@163 439 local errmsg = "Error while reading request from client. Error response: " .. status
jbe@163 440 if text then
jbe@163 441 errmsg = errmsg .. " (" .. text .. ")"
jbe@163 442 end
jbe@163 443 if
jbe@163 444 state == "init" or
jbe@163 445 state == "prepare" or
jbe@163 446 state == "no_status_sent" or
jbe@163 447 state == "info_status_sent"
jbe@163 448 then
jbe@163 449 local error_response_status, errmsg2 = pcall(function()
jbe@163 450 request:monologue()
jbe@163 451 request:send_status(status)
jbe@163 452 request:send_header("Content-Type", "text/plain")
jbe@163 453 request:send_data(status, "\n")
jbe@163 454 if text then
jbe@163 455 request:send_data("\n", text, "\n")
jbe@163 456 end
jbe@163 457 request:finish()
jbe@163 458 end)
jbe@163 459 if not error_response_status then
jbe@163 460 error("Unexpected error while sending error response: " .. errmsg2)
jbe@163 461 end
jbe@163 462 elseif state ~= "faulty" then
jbe@163 463 assert_close(socket:reset())
jbe@163 464 end
jbe@163 465 if throw_error then
jbe@163 466 error(errmsg)
jbe@163 467 else
jbe@163 468 return survive
jbe@163 469 end
jbe@163 470 end
jbe@160 471 -- wait for input:
jbe@160 472 if not poll(socket_set, nil, request_idle_timeout) then
jbe@163 473 return request_error(false, "408 Request Timeout", "Idle connection timed out")
jbe@38 474 end
jbe@162 475 until close_responded
jbe@0 476 return survive
jbe@0 477 end
jbe@0 478 end
jbe@0 479
jbe@0 480 return _M
jbe@0 481

Impressum / About Us