Software /
code /
prosody
Comparison
net/http.lua @ 8201:a0ad62a269df
Merge 0.10->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 28 Aug 2017 21:05:12 +0200 |
parent | 8121:a33a87f13155 |
parent | 8200:e92585ab4998 |
child | 8534:c071cabfa066 |
comparison
equal
deleted
inserted
replaced
8185:e89320b8a789 | 8201:a0ad62a269df |
---|---|
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 local events = require "util.events"; |
14 local verify_identity = require"util.x509".verify_identity; | |
14 | 15 |
15 local ssl_available = pcall(require, "ssl"); | 16 local ssl_available = pcall(require, "ssl"); |
16 | 17 |
17 local server = require "net.server" | 18 local server = require "net.server" |
18 | 19 |
32 | 33 |
33 local listener = { default_port = 80, default_mode = "*a" }; | 34 local listener = { default_port = 80, default_mode = "*a" }; |
34 | 35 |
35 function listener.onconnect(conn) | 36 function listener.onconnect(conn) |
36 local req = requests[conn]; | 37 local req = requests[conn]; |
38 | |
39 -- Validate certificate | |
40 if not req.insecure and conn:ssl() then | |
41 local sock = conn:socket(); | |
42 local chain_valid = sock.getpeerverification and sock:getpeerverification(); | |
43 if not chain_valid then | |
44 req.callback("certificate-chain-invalid", 0, req); | |
45 req.callback = nil; | |
46 conn:close(); | |
47 return; | |
48 end | |
49 local cert = sock.getpeercertificate and sock:getpeercertificate(); | |
50 if not cert or not verify_identity(req.host, false, cert) then | |
51 req.callback("certificate-verify-failed", 0, req); | |
52 req.callback = nil; | |
53 conn:close(); | |
54 return; | |
55 end | |
56 end | |
57 | |
37 -- Send the request | 58 -- Send the request |
38 local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" }; | 59 local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" }; |
39 if req.query then | 60 if req.query then |
40 t_insert(request_line, 4, "?"..req.query); | 61 t_insert(request_line, 4, "?"..req.query); |
41 end | 62 end |
179 if ex.headers then | 200 if ex.headers then |
180 for k, v in pairs(ex.headers) do | 201 for k, v in pairs(ex.headers) do |
181 headers[k] = v; | 202 headers[k] = v; |
182 end | 203 end |
183 end | 204 end |
205 req.insecure = ex.insecure; | |
184 end | 206 end |
185 | 207 |
186 log("debug", "Making %s %s request '%s' to %s", req.scheme:upper(), method or "GET", req.id, (ex and ex.suppress_url and host_header) or u); | 208 log("debug", "Making %s %s request '%s' to %s", req.scheme:upper(), method or "GET", req.id, (ex and ex.suppress_url and host_header) or u); |
187 | 209 |
188 -- Attach to request object | 210 -- Attach to request object |
194 end | 216 end |
195 local port_number = port and tonumber(port) or (using_https and 443 or 80); | 217 local port_number = port and tonumber(port) or (using_https and 443 or 80); |
196 | 218 |
197 local sslctx = false; | 219 local sslctx = false; |
198 if using_https then | 220 if using_https then |
199 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } }; | 221 sslctx = ex and ex.sslctx or self.options and self.options.sslctx; |
200 end | 222 end |
201 | 223 |
202 local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx) | 224 local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx) |
203 if not handler then | 225 if not handler then |
204 self.events.fire_event("request-connection-error", { http = self, request = req, url = u, err = conn }); | 226 self.events.fire_event("request-connection-error", { http = self, request = req, url = u, err = conn }); |
233 request = request; | 255 request = request; |
234 new = options and function (new_options) | 256 new = options and function (new_options) |
235 return new(setmetatable(new_options, { __index = options })); | 257 return new(setmetatable(new_options, { __index = options })); |
236 end or new; | 258 end or new; |
237 events = events.new(); | 259 events = events.new(); |
238 request = request; | |
239 }; | 260 }; |
240 return http; | 261 return http; |
241 end | 262 end |
242 | 263 |
243 local default_http = new(); | 264 local default_http = new({ |
265 sslctx = { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } }; | |
266 }); | |
244 | 267 |
245 return { | 268 return { |
246 request = function (u, ex, callback) | 269 request = function (u, ex, callback) |
247 return default_http:request(u, ex, callback); | 270 return default_http:request(u, ex, callback); |
248 end; | 271 end; |
272 default = default_http; | |
249 new = new; | 273 new = new; |
250 events = default_http.events; | 274 events = default_http.events; |
251 -- COMPAT | 275 -- COMPAT |
252 urlencode = util_http.urlencode; | 276 urlencode = util_http.urlencode; |
253 urldecode = util_http.urldecode; | 277 urldecode = util_http.urldecode; |