From 04d3816b69a847be365f41d5a625e224108b2c76 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 21 Apr 2017 16:41:27 +0100 Subject: net.http: Allow creation of http client objects, with custom options --- net/http.lua | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/net/http.lua b/net/http.lua index 2fd46db9..986b6dc4 100644 --- a/net/http.lua +++ b/net/http.lua @@ -10,6 +10,7 @@ local b64 = require "util.encodings".base64.encode; local url = require "socket.url" local httpstream_new = require "net.http.parser".new; local util_http = require "util.http"; +local events = require "util.events"; local ssl_available = pcall(require, "ssl"); @@ -122,7 +123,7 @@ local function log_if_failed(id, ret, ...) return ...; end -local function request(u, ex, callback) +local function request(self, u, ex, callback) local req = url.parse(u); if not (req and req.host) then @@ -207,9 +208,27 @@ local function request(u, ex, callback) return req; end -return { - request = request; +local function new(options) + local http = { + options = options; + request = request; + new = options and function (new_options) + return new(setmetatable(new_options, { __index = options })); + end or new; + events = events.new(); + request = request; + }; + return http; +end +local default_http = new(); + +return { + request = function (u, ex, callback) + return default_http:request(u, ex, callback); + end; + new = new; + events = default_http.events; -- COMPAT urlencode = util_http.urlencode; urldecode = util_http.urldecode; -- cgit v1.2.3 From 66a3d68d562fb51a8b5b8918bf2e4d6a76e0519b Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 21 Apr 2017 16:42:06 +0100 Subject: net.http: Fire new events: pre-request, request-connection-error, request, response --- net/http.lua | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/net/http.lua b/net/http.lua index 986b6dc4..6d32b110 100644 --- a/net/http.lua +++ b/net/http.lua @@ -137,6 +137,15 @@ local function request(self, u, ex, callback) req.id = ex and ex.id or make_id(req); + do + local event = { http = self, url = u, request = req, options = ex, callback = callback }; + local ret = self.events.fire_event("pre-request", event); + if ret then + return ret; + end + req, u, ex, callback = event.request, event.url, event.options, event.callback; + end + local method, headers, body; local host, port = req.host, req.port; @@ -191,13 +200,20 @@ local function request(self, u, ex, callback) local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx) if not handler then + self.events.fire_event("request-connection-error", { http = self, request = req, url = u, err = conn }); callback(conn, 0, req); return nil, conn; end req.handler, req.conn = handler, conn req.write = function (...) return req.handler:write(...); end - req.callback = function (content, code, request, response) + req.callback = function (content, code, response, request) + do + local event = { http = self, url = u, request = req, response = response, content = content, code = code, callback = callback }; + self.events.fire_event("response", event); + content, code, response = event.content, event.code, event.response; + end + log("debug", "Request '%s': Calling callback, status %s", req.id, code or "---"); return log_if_failed(req.id, xpcall(function () return callback(content, code, request, response) end, handleerr)); end @@ -205,6 +221,8 @@ local function request(self, u, ex, callback) req.state = "status"; requests[req.handler] = req; + + self.events.fire_event("request", { http = self, request = req, url = u }); return req; end -- cgit v1.2.3 From 3371e3eae3f1dc9db621519c8ccf045b1738d030 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 21 Apr 2017 16:42:25 +0100 Subject: net.http: Add request.url, which is the original full URL as a string --- net/http.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/net/http.lua b/net/http.lua index 6d32b110..d820e471 100644 --- a/net/http.lua +++ b/net/http.lua @@ -125,6 +125,7 @@ end local function request(self, u, ex, callback) local req = url.parse(u); + req.url = u; if not (req and req.host) then callback("invalid-url", 0, req); -- cgit v1.2.3 From b9401b9307b94c8b115257835392624df74e4012 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 22 Apr 2017 16:41:44 +0200 Subject: mod_posix: Use typed config API --- plugins/mod_posix.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/mod_posix.lua b/plugins/mod_posix.lua index 5a6cd0c2..bf8b5cc5 100644 --- a/plugins/mod_posix.lua +++ b/plugins/mod_posix.lua @@ -57,7 +57,7 @@ end); if not prosody.start_time then -- server-starting local suid = module:get_option("setuid"); if not suid or suid == 0 or suid == "root" then - if pposix.getuid() == 0 and not module:get_option("run_as_root") then + if pposix.getuid() == 0 and not module:get_option_boolean("run_as_root") then module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!"); module:log("error", "For more information on running Prosody as root, see http://prosody.im/doc/root"); prosody.shutdown("Refusing to run as root"); -- cgit v1.2.3 From eae8bd26c27278ea7a7b8bc015b93a3761750e89 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 22 Apr 2017 19:11:26 +0200 Subject: prosodyctl: Delay reporting of successful certificate imports until all done --- prosodyctl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/prosodyctl b/prosodyctl index cc410f5a..e2f71223 100755 --- a/prosodyctl +++ b/prosodyctl @@ -873,18 +873,19 @@ function cert_commands.import(arg) owner = config.get("*", "prosody_user") or "prosody"; group = config.get("*", "prosody_group") or owner; end + local imported = {}; for _, host in ipairs(hostnames) do for _, dir in ipairs(arg) do if lfs.attributes(dir .. "/" .. host .. "/fullchain.pem") and lfs.attributes(dir .. "/" .. host .. "/privkey.pem") then copy(dir .. "/" .. host .. "/fullchain.pem", cert_basedir .. "/" .. host .. ".crt", nil, owner, group); copy(dir .. "/" .. host .. "/privkey.pem", cert_basedir .. "/" .. host .. ".key", "0377", owner, group); - show_message("Imported certificate and key for "..host); + table.insert(imported, host); elseif lfs.attributes(dir .. "/" .. host .. ".crt") and lfs.attributes(dir .. "/" .. host .. ".key") then copy(dir .. "/" .. host .. ".crt", cert_basedir .. "/" .. host .. ".crt", nil, owner, group); copy(dir .. "/" .. host .. ".key", cert_basedir .. "/" .. host .. ".key", "0377", owner, group); - show_message("Imported certificate and key for "..host); + table.insert(imported, host); else show_warning("No certificate for host "..host.." found :("); end @@ -893,6 +894,9 @@ function cert_commands.import(arg) -- Private key matches public key in certificate end end + if imported[1] then + show_message("Imported certificate and key for hosts "..table.concat(imported, ", ")); + end end function commands.cert(arg) -- cgit v1.2.3 From 934e6627ecf290b8428dea164e6c504623bee6f7 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 22 Apr 2017 19:12:04 +0200 Subject: prosodyctl: Return non-zero exit code from cert import if no certificates imported --- prosodyctl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/prosodyctl b/prosodyctl index e2f71223..4b29c406 100755 --- a/prosodyctl +++ b/prosodyctl @@ -896,6 +896,9 @@ function cert_commands.import(arg) end if imported[1] then show_message("Imported certificate and key for hosts "..table.concat(imported, ", ")); + else + show_warning("No certificates imported :("); + return 1; end end -- cgit v1.2.3 From dbb0ad8e7ed9dd71011d0c5f5203781cdf9566ad Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 22 Apr 2017 19:12:26 +0200 Subject: prosodyctl: Make note about reporting where certificates are searched for --- prosodyctl | 1 + 1 file changed, 1 insertion(+) diff --git a/prosodyctl b/prosodyctl index 4b29c406..bee90665 100755 --- a/prosodyctl +++ b/prosodyctl @@ -887,6 +887,7 @@ function cert_commands.import(arg) copy(dir .. "/" .. host .. ".key", cert_basedir .. "/" .. host .. ".key", "0377", owner, group); table.insert(imported, host); else + -- TODO Say where we looked show_warning("No certificate for host "..host.." found :("); end -- TODO Additional checks -- cgit v1.2.3 From 04abad10a7f83c64a2bafdcb2888e9cad815f47d Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 22 Apr 2017 19:12:51 +0200 Subject: prosodyctl: Attempt to reload prosody after importing certificates --- prosodyctl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/prosodyctl b/prosodyctl index bee90665..dcc98a79 100755 --- a/prosodyctl +++ b/prosodyctl @@ -897,6 +897,10 @@ function cert_commands.import(arg) end if imported[1] then show_message("Imported certificate and key for hosts "..table.concat(imported, ", ")); + local ok, err = prosodyctl.reload(); + if not ok and err ~= "not-running" then + show_message(error_messages[err]); + end else show_warning("No certificates imported :("); return 1; -- cgit v1.2.3