X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=net%2Fhttp.lua;h=9b1954da5dc113b87e8c45ee2fe7a377d7f90b28;hb=409ea7826d085d86bce956ec741bc3df62feda02;hp=1ecbf41079f4ee6837e2178cb675b933efa74fbe;hpb=61fbebe4017d142510fbcfce61714ecb48b99503;p=prosody.git diff --git a/net/http.lua b/net/http.lua index 1ecbf410..9b1954da 100644 --- a/net/http.lua +++ b/net/http.lua @@ -1,3 +1,11 @@ +-- Prosody IM +-- Copyright (C) 2008-2009 Matthew Wild +-- Copyright (C) 2008-2009 Waqas Hussain +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- + local socket = require "socket" local mime = require "mime" @@ -9,20 +17,20 @@ local connlisteners_get = require "net.connlisteners".get; local listener = connlisteners_get("httpclient") or error("No httpclient listener!"); local t_insert, t_concat = table.insert, table.concat; -local tonumber, tostring, pairs, xpcall, select, debug_traceback = - tonumber, tostring, pairs, xpcall, select, debug.traceback; +local tonumber, tostring, pairs, xpcall, select, debug_traceback, char, format = + tonumber, tostring, pairs, xpcall, select, debug.traceback, string.char, string.format; local log = require "util.logger".init("http"); local print = function () end -local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end }); -local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end - module "http" +function urlencode(s) return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); end +function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end)); end + local function expectbody(reqt, code) if reqt.method == "HEAD" then return nil end - if code == 204 or code == 304 then return nil end + if code == 204 or code == 304 or code == 301 then return nil end if code >= 100 and code < 200 then return nil end return 1 end @@ -114,6 +122,15 @@ local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(e function request(u, ex, callback) local req = url.parse(u); + if not (req and req.host) then + callback(nil, 0, req); + return nil, "invalid-url"; + end + + if not req.path then + req.path = "/"; + end + local custom_headers, body; local default_headers = { ["Host"] = req.host, ["User-Agent"] = "Prosody XMPP Server" } @@ -135,10 +152,11 @@ function request(u, ex, callback) end req.handler, req.conn = server.wrapclient(socket.tcp(), req.host, req.port or 80, listener, "*a"); - req.write = req.handler.write; + req.write = function (...) return req.handler:write(...); end req.conn:settimeout(0); local ok, err = req.conn:connect(req.host, req.port or 80); if not ok and err ~= "timeout" then + callback(nil, 0, req); return nil, err; end @@ -182,7 +200,7 @@ end function destroy_request(request) if request.conn then request.handler.close() - listener.disconnect(request.conn, "closed"); + listener.ondisconnect(request.conn, "closed"); end end