From 527e6d6086ee82b15e50564dda5249aa9734ce49 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 21 Apr 2017 16:41:27 +0100 Subject: net.http: Allow creation of http client objects, with custom options --- net/http.lua | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'net/http.lua') diff --git a/net/http.lua b/net/http.lua index 2fd46db9..986b6dc4 100644 --- a/net/http.lua +++ b/net/http.lua @@ -10,6 +10,7 @@ local b64 = require "util.encodings".base64.encode; local url = require "socket.url" local httpstream_new = require "net.http.parser".new; local util_http = require "util.http"; +local events = require "util.events"; local ssl_available = pcall(require, "ssl"); @@ -122,7 +123,7 @@ local function log_if_failed(id, ret, ...) return ...; end -local function request(u, ex, callback) +local function request(self, u, ex, callback) local req = url.parse(u); if not (req and req.host) then @@ -207,9 +208,27 @@ local function request(u, ex, callback) return req; end -return { - request = request; +local function new(options) + local http = { + options = options; + request = request; + new = options and function (new_options) + return new(setmetatable(new_options, { __index = options })); + end or new; + events = events.new(); + request = request; + }; + return http; +end +local default_http = new(); + +return { + request = function (u, ex, callback) + return default_http:request(u, ex, callback); + end; + new = new; + events = default_http.events; -- COMPAT urlencode = util_http.urlencode; urldecode = util_http.urldecode; -- cgit v1.2.3