Changeset

8113:cfb5ab763384

net.http: Allow creation of http client objects, with custom options
author Matthew Wild <mwild1@gmail.com>
date Fri, 21 Apr 2017 16:41:27 +0100
parents 8111:3cbb311f8468
children 8114:12df41a5a4b1
files net/http.lua
diffstat 1 files changed, 22 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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;