Comparison

net/http.lua @ 11120:b2331f3dfeea

Merge 0.11->trunk
author Matthew Wild <mwild1@gmail.com>
date Wed, 30 Sep 2020 09:50:33 +0100
parent 11068:988ddd57e851
child 11185:409ce7686c11
comparison
equal deleted inserted replaced
11119:68df52bf08d5 11120:b2331f3dfeea
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 local verify_identity = require"util.x509".verify_identity;
15 local promise = require "util.promise";
16 local http_errors = require "net.http.errors";
15 17
16 local basic_resolver = require "net.resolvers.basic"; 18 local basic_resolver = require "net.resolvers.basic";
17 local connect = require "net.connect".connect; 19 local connect = require "net.connect".connect;
18 20
19 local ssl_available = pcall(require, "ssl"); 21 local ssl_available = pcall(require, "ssl");
38 40
39 -- Request-related helper functions 41 -- Request-related helper functions
40 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); return err; end 42 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); return err; end
41 local function log_if_failed(req, ret, ...) 43 local function log_if_failed(req, ret, ...)
42 if not ret then 44 if not ret then
43 log("error", "Request '%s': error in callback: %s", req.id, tostring((...))); 45 log("error", "Request '%s': error in callback: %s", req.id, (...));
44 if not req.suppress_errors then 46 if not req.suppress_errors then
45 error(...); 47 error(...);
46 end 48 end
47 end 49 end
48 return ...; 50 return ...;
159 161
160 function listener.onincoming(conn, data) 162 function listener.onincoming(conn, data)
161 local request = requests[conn]; 163 local request = requests[conn];
162 164
163 if not request then 165 if not request then
164 log("warn", "Received response from connection %s with no request attached!", tostring(conn)); 166 log("warn", "Received response from connection %s with no request attached!", conn);
165 return; 167 return;
166 end 168 end
167 169
168 if data and request.reader then 170 if data and request.reader then
169 request:reader(data); 171 request:reader(data);
280 end 282 end
281 283
282 local function new(options) 284 local function new(options)
283 local http = { 285 local http = {
284 options = options; 286 options = options;
285 request = request; 287 request = function (self, u, ex, callback)
288 if callback ~= nil then
289 return request(self, u, ex, callback);
290 else
291 return promise.new(function (resolve, reject)
292 request(self, u, ex, function (body, code, a, b)
293 if code == 0 then
294 reject(http_errors.new(body, { request = a }));
295 else
296 a.request = b;
297 resolve(a);
298 end
299 end);
300 end);
301 end
302 end;
286 new = options and function (new_options) 303 new = options and function (new_options)
287 local final_options = {}; 304 local final_options = {};
288 for k, v in pairs(options) do final_options[k] = v; end 305 for k, v in pairs(options) do final_options[k] = v; end
289 if new_options then 306 if new_options then
290 for k, v in pairs(new_options) do final_options[k] = v; end 307 for k, v in pairs(new_options) do final_options[k] = v; end
295 }; 312 };
296 return http; 313 return http;
297 end 314 end
298 315
299 local default_http = new({ 316 local default_http = new({
300 sslctx = { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } }; 317 sslctx = { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" }, alpn = "http/1.1" };
301 suppress_errors = true; 318 suppress_errors = true;
302 }); 319 });
303 320
304 return { 321 return {
305 request = function (u, ex, callback) 322 request = function (u, ex, callback)