Software /
code /
prosody
Comparison
net/http.lua @ 678:1859edec2237
Protected call for HTTP request callbacks, to catch errors
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 08 Jan 2009 02:04:06 +0000 |
parent | 677:93e5309c5430 |
child | 714:ab3c47f4fe1d |
comparison
equal
deleted
inserted
replaced
677:93e5309c5430 | 678:1859edec2237 |
---|---|
7 | 7 |
8 local connlisteners_get = require "net.connlisteners".get; | 8 local connlisteners_get = require "net.connlisteners".get; |
9 local listener = connlisteners_get("httpclient") or error("No httpclient listener!"); | 9 local listener = connlisteners_get("httpclient") or error("No httpclient listener!"); |
10 | 10 |
11 local t_insert, t_concat = table.insert, table.concat; | 11 local t_insert, t_concat = table.insert, table.concat; |
12 local tonumber, tostring, pairs = tonumber, tostring, pairs; | 12 local tonumber, tostring, pairs, xpcall, select, debug_traceback = |
13 tonumber, tostring, pairs, xpcall, select, debug.traceback; | |
14 | |
15 local log = require "util.logger".init("http"); | |
13 local print = function () end | 16 local print = function () end |
14 | 17 |
15 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end }); | 18 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end }); |
16 local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end | 19 local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end |
17 | 20 |
103 return request_reader(request, data, linelen); | 106 return request_reader(request, data, linelen); |
104 end | 107 end |
105 end | 108 end |
106 end | 109 end |
107 | 110 |
111 local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug_traceback()); end | |
108 function request(u, ex, callback) | 112 function request(u, ex, callback) |
109 local req = url.parse(u); | 113 local req = url.parse(u); |
110 | 114 |
111 local custom_headers, body; | 115 local custom_headers, body; |
112 local default_headers = { ["Host"] = req.host, ["User-Agent"] = "Prosody XMPP Server" } | 116 local default_headers = { ["Host"] = req.host, ["User-Agent"] = "Prosody XMPP Server" } |
155 | 159 |
156 if body then | 160 if body then |
157 req.write(body); | 161 req.write(body); |
158 end | 162 end |
159 | 163 |
160 req.callback = callback; | 164 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 |
161 req.reader = request_reader; | 165 req.reader = request_reader; |
162 req.state = "status" | 166 req.state = "status"; |
163 | 167 |
164 listener.register_request(req.handler, req); | 168 listener.register_request(req.handler, req); |
165 | 169 |
166 return req; | 170 return req; |
167 end | 171 end |