# HG changeset patch # User jbe # Date 1434667441 -7200 # Node ID 31820816f55431bc59676d767c74c6670d4434ce # Parent 77d8fdd124e6276eb91e682da7ac0b8c3e7ae9d5 Code cleanup and added "request_body_content_length" declaration in HTTP module diff -r 77d8fdd124e6 -r 31820816f554 moonbridge_http.lua --- a/moonbridge_http.lua Thu Jun 18 22:38:53 2015 +0200 +++ b/moonbridge_http.lua Fri Jun 19 00:44:01 2015 +0200 @@ -246,9 +246,7 @@ -- (raw access, but case-insensitive): headers = setmetatable({}, { __index = function(self, key) - if type(key) ~= "string" then - error("Attempted to index headers table with a non-string key") - end + assert(type(key) == "string", "Attempted to index headers table with a non-string key") local lowerkey = string.lower(key) local result = rawget(self, lowerkey) if result == nil then @@ -296,17 +294,19 @@ if headers_value_nil[key] then return nil end - local result = nil local values = request.headers_csv_table[key] if #values == 0 then headers_value_nil[key] = true - elseif #values == 1 then - result = values[1] else - result = false + local result + if #values == 1 then + result = values[1] + else + result = false + end + self[key] = result + return result end - self[key] = result - return result end }), -- table mapping header field names to a flag table, @@ -315,6 +315,7 @@ __index = function(self, key) local result = setmetatable({}, { __index = function(self, key) + assert(type(key) == "string", "Attempted to index header flag table with a non-string key") local lowerkey = string.lower(key) local result = rawget(self, lowerkey) or false self[lowerkey] = result @@ -338,18 +339,19 @@ -- local variables to track the state: local state = "init" -- one of: -- "init" (initial state) - -- "no_status_sent" (configuration complete) + -- "no_status_sent" (request body streaming config complete) -- "info_status_sent" (1xx status code has been sent) -- "bodyless_status_sent" (204/304 status code has been sent) -- "status_sent" (regular status code has been sent) -- "headers_sent" (headers have been terminated) -- "finished" (request has been answered completely) -- "faulty" (I/O or protocaol error) + local request_body_content_length -- Content-Length of request body local close_requested = false -- "Connection: close" requested local close_responded = false -- "Connection: close" sent - local content_length = nil -- value of Content-Length header sent - local chunk_parts = {} -- list of chunks to send - local chunk_bytes = 0 -- sum of lengths of chunks to send + local content_length = nil -- value of Content-Length header sent + local chunk_parts = {} -- list of chunks to send + local chunk_bytes = 0 -- sum of lengths of chunks to send local streamed_post_params = {} -- mapping from POST field name to stream function local streamed_post_param_patterns = {} -- list of POST field pattern and stream function pairs -- functions to assert proper output/closing: