From ae290f388b3a2c8aed0d1dfa632b5f6396bdd064 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20=C5=A0pa=C4=8Dek?= Date: Thu, 18 Apr 2019 11:18:53 +0200 Subject: [PATCH] http: assorted fixed to make luacheck happy --- modules/http/http.lua.in | 51 +++++++++++++++--------------- modules/http/http.test.lua | 2 +- modules/http/http_doh.test.lua | 2 +- modules/http/test_tls/tls.test.lua | 2 +- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/modules/http/http.lua.in b/modules/http/http.lua.in index cdd57f374..34c457b49 100644 --- a/modules/http/http.lua.in +++ b/modules/http/http.lua.in @@ -2,8 +2,8 @@ if not stats then modules.load('stats') end if not bogus_log then modules.load('bogus_log') end -ffi = require('ffi') -cqueues = require('cqueues') +local ffi = require('ffi') +local cqueues = require('cqueues') cqueues.socket = require('cqueues.socket') assert(cqueues.VERSION >= 20150112) -- fdopen changed semantics @@ -320,7 +320,7 @@ end -- @function Merge dictionaries. -- Values from right-hand side dictionaries take precedence. -function mergeconf(...) +local function mergeconf(...) local merged = {} for _, intable in ipairs({...}) do assert(type(intable) == 'table', 'cannot merge non-tables') @@ -331,12 +331,12 @@ function mergeconf(...) return merged end -function load_cert(certname, keyname) +local function load_cert(certname, keyname) local f, err = io.open(certname, 'r') if not f then - panic('[http] unable to read TLS certificate file %s: %s', certname, err) + panic('[http] unable to read TLS certificate file: %s', err) end - crt = x509.new(f:read('*all')) + local crt = x509.new(f:read('*all')) f:close() if not crt then panic('[http] unable to parse TLS certificate file %s', certname) @@ -344,9 +344,9 @@ function load_cert(certname, keyname) f, err = io.open(keyname, 'r') if not f then - panic('[http] unable to open TLS key file %s: %s', keyname, err) + panic('[http] unable to open TLS key file: %s', err) end - key = pkey.new(f:read('*all')) + local key = pkey.new(f:read('*all')) f:close() if not key then panic('[http] unable to parse TLS key file %s', keyname) @@ -356,7 +356,7 @@ end -- @function Listen on given socket -- using configuration for specific "kind" of HTTP server -function add_socket(fd, kind) +local function add_socket(fd, kind, addr_str) assert(M.servers[fd] == nil, 'socket is already served by an HTTP instance') local conf, crt, key conf = mergeconf(M.templates._builtin, M.templates._default, M.templates[kind] or {}) @@ -377,12 +377,11 @@ function add_socket(fd, kind) -- Compose server handler local routes = route(conf.endpoints) -- Enable SO_REUSEPORT by default (unless explicitly turned off) - if not reuseport and worker.id > 0 then + if not conf.reuseport and worker.id > 0 then warn('[http] the "reuseport" option is disabled and multiple forks are used, ' .. 'port binding will fail on some instances') end - -- Check if UNIX socket path is used - local addr_str -- TODO + -- Check if UNIX socket path is used TODO conf.ctx = crt and tlscontext(crt, key) conf.onstream = routes -- Create TLS context and start listening @@ -409,7 +408,7 @@ function add_socket(fd, kind) end -- @function Stop listening on given socket -function remove_socket(fd) +local function remove_socket(fd) local instance = M.servers[fd] assert(instance, 'HTTP module is not listening on given socket') @@ -418,6 +417,19 @@ function remove_socket(fd) -- TODO stop refresh timer end +-- @function Listen for config changes from net.listen()/net.close() +local function cb_socket(...) + local added, endpoint, addr_str = unpack({...}) + endpoint = ffi.cast('struct endpoint **', endpoint)[0] + local kind = ffi.string(endpoint.flags.kind) + local socket = endpoint.fd + if added then + return add_socket(socket, kind, addr_str) + else + return remove_socket(socket) + end +end + -- @function Init module function M.init() -- collect and merge metrics only on leader @@ -438,19 +450,6 @@ function M.deinit() net.register_endpoint_kind('webmgmt') end --- @function Listen for config changes from net.listen()/net.close() -function cb_socket(...) - local added, endpoint, _ = unpack({...}) - endpoint = ffi.cast('struct endpoint **', endpoint)[0] - local kind = ffi.string(endpoint.flags.kind) - local socket = endpoint.fd - if added then - return add_socket(socket, kind) - else - return remove_socket(socket) - end -end - -- @function Configure module, i.e. store new configuration template -- kind = socket type (doh/webmgmt) function M.config(conf, kind) diff --git a/modules/http/http.test.lua b/modules/http/http.test.lua index bc765f58b..ed8f510c5 100644 --- a/modules/http/http.test.lua +++ b/modules/http/http.test.lua @@ -19,7 +19,7 @@ else }, 'webtest') local bound - for i = 1,1000 do + for _ = 1,1000 do bound = net.listen('127.0.0.1', math.random(1025,65535), { kind = 'webtest'} ) if bound then break diff --git a/modules/http/http_doh.test.lua b/modules/http/http_doh.test.lua index f9cd26b32..74e1d4357 100644 --- a/modules/http/http_doh.test.lua +++ b/modules/http/http_doh.test.lua @@ -88,7 +88,7 @@ else }, 'doh') local bound - for i = 1,1000 do + for _ = 1,1000 do bound = net.listen('127.0.0.1', math.random(1025,65535), { kind = 'doh'} ) if bound then break diff --git a/modules/http/test_tls/tls.test.lua b/modules/http/test_tls/tls.test.lua index 0a9696453..5327e193e 100644 --- a/modules/http/test_tls/tls.test.lua +++ b/modules/http/test_tls/tls.test.lua @@ -15,7 +15,7 @@ else same(http.config(config), nil, desc .. ' can be configured') local bound - for i = 1,1000 do + for _ = 1,1000 do bound = net.listen('127.0.0.1', math.random(1025,65535), { kind = 'webmgmt'} ) if bound then break -- 2.47.2