Comparison

net/http.lua @ 10803:71d04bd6cadd

net.http: Return a Promise if no callback is given
author Kim Alvefur <zash@zash.se>
date Wed, 06 May 2020 18:03:20 +0200
parent 10464:8d3acf16c404
child 10995:e18a913aed2d
comparison
equal deleted inserted replaced
10802:c11f9cd6c761 10803:71d04bd6cadd
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 errors = require "util.error";
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");
268 end 270 end
269 271
270 local function new(options) 272 local function new(options)
271 local http = { 273 local http = {
272 options = options; 274 options = options;
273 request = request; 275 request = function (self, u, ex, callback)
276 if callback ~= nil then
277 return request(self, u, ex, callback);
278 else
279 return promise.new(function (resolve, reject)
280 request(self, u, ex, function (body, code, a, b)
281 if code == 0 then
282 reject(errors.new(body, { request = a }));
283 else
284 resolve({ request = b, response = a });
285 end
286 end);
287 end);
288 end
289 end;
274 new = options and function (new_options) 290 new = options and function (new_options)
275 local final_options = {}; 291 local final_options = {};
276 for k, v in pairs(options) do final_options[k] = v; end 292 for k, v in pairs(options) do final_options[k] = v; end
277 if new_options then 293 if new_options then
278 for k, v in pairs(new_options) do final_options[k] = v; end 294 for k, v in pairs(new_options) do final_options[k] = v; end