X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=net%2Fhttp.lua;h=67da59defebfb6a45f335ad37568d30b3de752ed;hb=23509c31f55c062f70eff2acbf46ee19ef3e7628;hp=da5b3c84a7e80fd497b02d14100b1733b0309b38;hpb=c486936898887aab61e79d090f8eeedbbc0a6935;p=prosody.git diff --git a/net/http.lua b/net/http.lua index da5b3c84..67da59de 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" } @@ -123,7 +140,7 @@ function request(u, ex, callback) end if ex then - custom_headers = ex.custom_headers; + custom_headers = ex.headers; req.onlystatus = ex.onlystatus; body = ex.body; if body then @@ -134,15 +151,23 @@ function request(u, ex, callback) if ex.method then req.method = ex.method; end end - req.handler, req.conn = server.wraptcpclient(listener, socket.tcp(), req.host, req.port or 80, 0, "*a"); + req.handler, req.conn = server.wrapclient(socket.tcp(), req.host, req.port or 80, listener, "*a"); req.write = req.handler.write; 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 - req.write((req.method or "GET ")..req.path.." HTTP/1.0\r\n"); + local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" }; + + if req.query then + t_insert(request_line, 4, "?"); + t_insert(request_line, 5, req.query); + end + + req.write(t_concat(request_line)); local t = { [2] = ": ", [4] = "\r\n" }; if custom_headers then for k, v in pairs(custom_headers) do @@ -163,7 +188,7 @@ function request(u, ex, callback) req.write(body); end - req.callback = function (content, code, request) log("debug", "Calling callback, code %s content: %s", code or "---", content or "---"); return select(2, xpcall(function () return callback(content, code, request) end, handleerr)); end + req.callback = function (content, code, request) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request) end, handleerr)); end req.reader = request_reader; req.state = "status";