X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;ds=sidebyside;f=net%2Fhttp.lua;h=b7d2beb99417949c425c26133f13d64e76f23b95;hb=84c934632367eac822fb6af2663032b67fd0a596;hp=4eb4a2ac261b8d37af973106b08ed73a3e62a050;hpb=85c6fd1f0a5397e3277183ea115c15ad56bf25e1;p=prosody.git diff --git a/net/http.lua b/net/http.lua index 4eb4a2ac..b7d2beb9 100644 --- a/net/http.lua +++ b/net/http.lua @@ -17,9 +17,9 @@ local ssl_available = pcall(require, "ssl"); local server = require "net.server" local t_insert, t_concat = table.insert, table.concat; -local pairs, ipairs = pairs, ipairs; -local tonumber, tostring, xpcall, select, debug_traceback, char, format = - tonumber, tostring, xpcall, select, debug.traceback, string.char, string.format; +local pairs = pairs; +local tonumber, tostring, xpcall, select, traceback = + tonumber, tostring, xpcall, select, debug.traceback; local log = require "util.logger".init("http"); @@ -66,24 +66,29 @@ end function listener.ondisconnect(conn, err) local request = requests[conn]; if request and request.conn then - request:reader(nil); + request:reader(nil, err); end requests[conn] = nil; end -local function request_reader(request, data) +local function request_reader(request, data, err) if not request.parser then - if not data then return; end - local function success_cb(r) + local function error_cb(reason) if request.callback then - request.callback(r.body, r.code, r, request); + request.callback(reason or "connection-closed", 0, request); request.callback = nil; end destroy_request(request); end - local function error_cb(r) + + if not data then + error_cb(err); + return; + end + + local function success_cb(r) if request.callback then - request.callback(r or "connection-closed", 0, request); + request.callback(r.body, r.code, r, request); request.callback = nil; end destroy_request(request); @@ -96,7 +101,7 @@ local function request_reader(request, data) request.parser:feed(data); end -local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug_traceback()); end +local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end function request(u, ex, callback) local req = url.parse(u); @@ -111,8 +116,17 @@ function request(u, ex, callback) local method, headers, body; + local host, port = req.host, req.port; + local host_header = host; + if (port == "80" and req.scheme == "http") + or (port == "443" and req.scheme == "https") then + port = nil; + elseif port then + host_header = host_header..":"..port; + end + headers = { - ["Host"] = req.host; + ["Host"] = host_header; ["User-Agent"] = "Prosody XMPP Server"; }; @@ -143,12 +157,12 @@ function request(u, ex, callback) if using_https and not ssl_available then error("SSL not available, unable to contact https URL"); end - local port = tonumber(req.port) or (using_https and 443 or 80); + local port_number = port and tonumber(port) or (using_https and 443 or 80); -- Connect the socket, and wrap it with net.server local conn = socket.tcp(); conn:settimeout(10); - local ok, err = conn:connect(req.host, port); + local ok, err = conn:connect(host, port_number); if not ok and err ~= "timeout" then callback(nil, 0, req); return nil, err; @@ -159,7 +173,7 @@ function request(u, ex, callback) sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2" } }; end - req.handler, req.conn = server.wrapclient(conn, req.host, port, listener, "*a", sslctx); + req.handler, req.conn = server.wrapclient(conn, host, port_number, listener, "*a", sslctx); req.write = function (...) return req.handler:write(...); end req.callback = function (content, code, request, response) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request, response) end, handleerr)); end