# HG changeset patch # User Matthew Wild # Date 1492789287 -3600 # Node ID cfb5ab7633840575bef3cd08ffe60b653e9ffac5 # Parent 3cbb311f8468b64b1eb6429bc58a5b0317bf1583 net.http: Allow creation of http client objects, with custom options diff -r 3cbb311f8468 -r cfb5ab763384 net/http.lua --- a/net/http.lua Fri Apr 21 15:11:25 2017 +0200 +++ b/net/http.lua Fri Apr 21 16:41:27 2017 +0100 @@ -10,6 +10,7 @@ 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 @@ 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 @@ return req; end +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 = request; - + 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;