Software / code / prosody
Comparison
net/http.lua @ 8121:a33a87f13155
Merge 0.10->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Mon, 24 Apr 2017 14:19:49 +0200 |
| parent | 8046:1bf47706aefb |
| parent | 8115:375cf924fce1 |
| child | 8201:a0ad62a269df |
comparison
equal
deleted
inserted
replaced
| 8112:d8ecefcb7c97 | 8121:a33a87f13155 |
|---|---|
| 8 | 8 |
| 9 local b64 = require "util.encodings".base64.encode; | 9 local b64 = require "util.encodings".base64.encode; |
| 10 local url = require "socket.url" | 10 local url = require "socket.url" |
| 11 local httpstream_new = require "net.http.parser".new; | 11 local httpstream_new = require "net.http.parser".new; |
| 12 local util_http = require "util.http"; | 12 local util_http = require "util.http"; |
| 13 local events = require "util.events"; | |
| 13 | 14 |
| 14 local ssl_available = pcall(require, "ssl"); | 15 local ssl_available = pcall(require, "ssl"); |
| 15 | 16 |
| 16 local server = require "net.server" | 17 local server = require "net.server" |
| 17 | 18 |
| 120 log("error", "Request '%s': error in callback: %s", id, tostring((...))); | 121 log("error", "Request '%s': error in callback: %s", id, tostring((...))); |
| 121 end | 122 end |
| 122 return ...; | 123 return ...; |
| 123 end | 124 end |
| 124 | 125 |
| 125 local function request(u, ex, callback) | 126 local function request(self, u, ex, callback) |
| 126 local req = url.parse(u); | 127 local req = url.parse(u); |
| 128 req.url = u; | |
| 127 | 129 |
| 128 if not (req and req.host) then | 130 if not (req and req.host) then |
| 129 callback("invalid-url", 0, req); | 131 callback("invalid-url", 0, req); |
| 130 return nil, "invalid-url"; | 132 return nil, "invalid-url"; |
| 131 end | 133 end |
| 133 if not req.path then | 135 if not req.path then |
| 134 req.path = "/"; | 136 req.path = "/"; |
| 135 end | 137 end |
| 136 | 138 |
| 137 req.id = ex and ex.id or make_id(req); | 139 req.id = ex and ex.id or make_id(req); |
| 140 | |
| 141 do | |
| 142 local event = { http = self, url = u, request = req, options = ex, callback = callback }; | |
| 143 local ret = self.events.fire_event("pre-request", event); | |
| 144 if ret then | |
| 145 return ret; | |
| 146 end | |
| 147 req, u, ex, callback = event.request, event.url, event.options, event.callback; | |
| 148 end | |
| 138 | 149 |
| 139 local method, headers, body; | 150 local method, headers, body; |
| 140 | 151 |
| 141 local host, port = req.host, req.port; | 152 local host, port = req.host, req.port; |
| 142 local host_header = host; | 153 local host_header = host; |
| 188 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } }; | 199 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } }; |
| 189 end | 200 end |
| 190 | 201 |
| 191 local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx) | 202 local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx) |
| 192 if not handler then | 203 if not handler then |
| 204 self.events.fire_event("request-connection-error", { http = self, request = req, url = u, err = conn }); | |
| 193 callback(conn, 0, req); | 205 callback(conn, 0, req); |
| 194 return nil, conn; | 206 return nil, conn; |
| 195 end | 207 end |
| 196 req.handler, req.conn = handler, conn | 208 req.handler, req.conn = handler, conn |
| 197 req.write = function (...) return req.handler:write(...); end | 209 req.write = function (...) return req.handler:write(...); end |
| 198 | 210 |
| 199 req.callback = function (content, code, request, response) | 211 req.callback = function (content, code, response, request) |
| 212 do | |
| 213 local event = { http = self, url = u, request = req, response = response, content = content, code = code, callback = callback }; | |
| 214 self.events.fire_event("response", event); | |
| 215 content, code, response = event.content, event.code, event.response; | |
| 216 end | |
| 217 | |
| 200 log("debug", "Request '%s': Calling callback, status %s", req.id, code or "---"); | 218 log("debug", "Request '%s': Calling callback, status %s", req.id, code or "---"); |
| 201 return log_if_failed(req.id, xpcall(function () return callback(content, code, request, response) end, handleerr)); | 219 return log_if_failed(req.id, xpcall(function () return callback(content, code, request, response) end, handleerr)); |
| 202 end | 220 end |
| 203 req.reader = request_reader; | 221 req.reader = request_reader; |
| 204 req.state = "status"; | 222 req.state = "status"; |
| 205 | 223 |
| 206 requests[req.handler] = req; | 224 requests[req.handler] = req; |
| 225 | |
| 226 self.events.fire_event("request", { http = self, request = req, url = u }); | |
| 207 return req; | 227 return req; |
| 208 end | 228 end |
| 209 | 229 |
| 230 local function new(options) | |
| 231 local http = { | |
| 232 options = options; | |
| 233 request = request; | |
| 234 new = options and function (new_options) | |
| 235 return new(setmetatable(new_options, { __index = options })); | |
| 236 end or new; | |
| 237 events = events.new(); | |
| 238 request = request; | |
| 239 }; | |
| 240 return http; | |
| 241 end | |
| 242 | |
| 243 local default_http = new(); | |
| 244 | |
| 210 return { | 245 return { |
| 211 request = request; | 246 request = function (u, ex, callback) |
| 212 | 247 return default_http:request(u, ex, callback); |
| 248 end; | |
| 249 new = new; | |
| 250 events = default_http.events; | |
| 213 -- COMPAT | 251 -- COMPAT |
| 214 urlencode = util_http.urlencode; | 252 urlencode = util_http.urlencode; |
| 215 urldecode = util_http.urldecode; | 253 urldecode = util_http.urldecode; |
| 216 formencode = util_http.formencode; | 254 formencode = util_http.formencode; |
| 217 formdecode = util_http.formdecode; | 255 formdecode = util_http.formdecode; |