diff options
author | Kim Alvefur <zash@zash.se> | 2017-04-24 14:19:49 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2017-04-24 14:19:49 +0200 |
commit | 706849aa1ef9f5f2f6494d14d7de0ac9e1647190 (patch) | |
tree | 13ca49cac213b410c52092725470206f070d7450 | |
parent | 708f82e198006129be93ea2d60504c30d200ee06 (diff) | |
parent | 04abad10a7f83c64a2bafdcb2888e9cad815f47d (diff) | |
download | prosody-706849aa1ef9f5f2f6494d14d7de0ac9e1647190.tar.gz prosody-706849aa1ef9f5f2f6494d14d7de0ac9e1647190.zip |
Merge 0.10->trunk
-rw-r--r-- | net/http.lua | 46 | ||||
-rw-r--r-- | plugins/mod_posix.lua | 2 | ||||
-rwxr-xr-x | prosodyctl | 16 |
3 files changed, 57 insertions, 7 deletions
diff --git a/net/http.lua b/net/http.lua index 2fd46db9..d820e471 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,8 +123,9 @@ 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); + req.url = u; if not (req and req.host) then callback("invalid-url", 0, req); @@ -136,6 +138,15 @@ local function request(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; @@ -190,13 +201,20 @@ local function request(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 @@ -204,12 +222,32 @@ local function request(u, ex, callback) req.state = "status"; requests[req.handler] = req; + + self.events.fire_event("request", { http = self, request = req, url = u }); 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; diff --git a/plugins/mod_posix.lua b/plugins/mod_posix.lua index e4c8ef31..3fc57afc 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 https://prosody.im/doc/root"); prosody.shutdown("Refusing to run as root"); @@ -873,19 +873,21 @@ 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 + -- TODO Say where we looked show_warning("No certificate for host "..host.." found :("); end -- TODO Additional checks @@ -893,6 +895,16 @@ 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, ", ")); + 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; + end end function commands.cert(arg) |