Software /
code /
prosody
Comparison
net/http.lua @ 8114:12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 21 Apr 2017 16:42:06 +0100 |
parent | 8113:cfb5ab763384 |
child | 8115:375cf924fce1 |
comparison
equal
deleted
inserted
replaced
8113:cfb5ab763384 | 8114:12df41a5a4b1 |
---|---|
135 req.path = "/"; | 135 req.path = "/"; |
136 end | 136 end |
137 | 137 |
138 req.id = ex and ex.id or make_id(req); | 138 req.id = ex and ex.id or make_id(req); |
139 | 139 |
140 do | |
141 local event = { http = self, url = u, request = req, options = ex, callback = callback }; | |
142 local ret = self.events.fire_event("pre-request", event); | |
143 if ret then | |
144 return ret; | |
145 end | |
146 req, u, ex, callback = event.request, event.url, event.options, event.callback; | |
147 end | |
148 | |
140 local method, headers, body; | 149 local method, headers, body; |
141 | 150 |
142 local host, port = req.host, req.port; | 151 local host, port = req.host, req.port; |
143 local host_header = host; | 152 local host_header = host; |
144 if (port == "80" and req.scheme == "http") | 153 if (port == "80" and req.scheme == "http") |
189 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } }; | 198 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } }; |
190 end | 199 end |
191 | 200 |
192 local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx) | 201 local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx) |
193 if not handler then | 202 if not handler then |
203 self.events.fire_event("request-connection-error", { http = self, request = req, url = u, err = conn }); | |
194 callback(conn, 0, req); | 204 callback(conn, 0, req); |
195 return nil, conn; | 205 return nil, conn; |
196 end | 206 end |
197 req.handler, req.conn = handler, conn | 207 req.handler, req.conn = handler, conn |
198 req.write = function (...) return req.handler:write(...); end | 208 req.write = function (...) return req.handler:write(...); end |
199 | 209 |
200 req.callback = function (content, code, request, response) | 210 req.callback = function (content, code, response, request) |
211 do | |
212 local event = { http = self, url = u, request = req, response = response, content = content, code = code, callback = callback }; | |
213 self.events.fire_event("response", event); | |
214 content, code, response = event.content, event.code, event.response; | |
215 end | |
216 | |
201 log("debug", "Request '%s': Calling callback, status %s", req.id, code or "---"); | 217 log("debug", "Request '%s': Calling callback, status %s", req.id, code or "---"); |
202 return log_if_failed(req.id, xpcall(function () return callback(content, code, request, response) end, handleerr)); | 218 return log_if_failed(req.id, xpcall(function () return callback(content, code, request, response) end, handleerr)); |
203 end | 219 end |
204 req.reader = request_reader; | 220 req.reader = request_reader; |
205 req.state = "status"; | 221 req.state = "status"; |
206 | 222 |
207 requests[req.handler] = req; | 223 requests[req.handler] = req; |
224 | |
225 self.events.fire_event("request", { http = self, request = req, url = u }); | |
208 return req; | 226 return req; |
209 end | 227 end |
210 | 228 |
211 local function new(options) | 229 local function new(options) |
212 local http = { | 230 local http = { |