net/http.lua
author Matthew Wild <mwild1@gmail.com>
Wed, 04 Apr 2018 18:42:05 +0100
changeset 8710 fd39c44c0113
parent 8693 0f6623712239
parent 8709 e2919978673e
child 8734 6063f9211fd1
permissions -rw-r--r--
Merge 0.10 -> trunk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1331
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2810
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2810
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5714
diff changeset
     4
--
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1331
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1331
diff changeset
     6
-- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1331
diff changeset
     7
--
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1331
diff changeset
     8
4972
1777271a1ec0 net.http: Use base64 from util.encodings instead of luasocket
Kim Alvefur <zash@zash.se>
parents: 4865
diff changeset
     9
local b64 = require "util.encodings".base64.encode;
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
local url = require "socket.url"
5464
712dbe1a0146 net.http: Switch from util.httpstream to net.http.parser, introduces small but backwards-incompatible API changes - see http://prosody.im/doc/developers/http
Matthew Wild <mwild1@gmail.com>
parents: 5458
diff changeset
    11
local httpstream_new = require "net.http.parser".new;
5458
84162b81c863 net.http, util.http: Move definitions of urlencode/decode and formencode/decode to util.http (possible to use them without unnecessary network-related dependencies)
Matthew Wild <mwild1@gmail.com>
parents: 5448
diff changeset
    12
local util_http = require "util.http";
8116
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
    13
local events = require "util.events";
8202
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
    14
local verify_identity = require"util.x509".verify_identity;
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
8554
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    16
local basic_resolver = require "net.resolvers.basic";
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    17
local connect = require "net.connect".connect;
5448
cbe9fa2d3787 net.http: Throw error when connecting to a http:// URL without LuaSec available
Matthew Wild <mwild1@gmail.com>
parents: 5354
diff changeset
    18
8554
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    19
local ssl_available = pcall(require, "ssl");
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
local t_insert, t_concat = table.insert, table.concat;
5505
0b6a99e6c1b1 mod_c2s, mod_s2s, net.http, net.http.server: Improve tracebacks (omit traceback function), to make it clearer where an error occured
Matthew Wild <mwild1@gmail.com>
parents: 5488
diff changeset
    22
local pairs = pairs;
7795
0bc6c3704973 net.http: Remove unused imports [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7523
diff changeset
    23
local tonumber, tostring, xpcall, traceback =
0bc6c3704973 net.http: Remove unused imports [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7523
diff changeset
    24
      tonumber, tostring, xpcall, debug.traceback;
0bc6c3704973 net.http: Remove unused imports [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7523
diff changeset
    25
local error = error
678
1859edec2237 Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents: 677
diff changeset
    26
1859edec2237 Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents: 677
diff changeset
    27
local log = require "util.logger".init("http");
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
6783
647adfd8f738 net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6504
diff changeset
    29
local _ENV = nil;
8558
4f0f5b49bb03 vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8554
diff changeset
    30
-- luacheck: std none
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
4557
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
    32
local requests = {}; -- Open requests
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
    33
7466
3b6e7ce9431f net.http: Add request.id to every request object (can be overridden by providing ex.id)
Matthew Wild <mwild1@gmail.com>
parents: 6826
diff changeset
    34
local function make_id(req) return (tostring(req):match("%x+$")); end
3b6e7ce9431f net.http: Add request.id to every request object (can be overridden by providing ex.id)
Matthew Wild <mwild1@gmail.com>
parents: 6826
diff changeset
    35
4557
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
    36
local listener = { default_port = 80, default_mode = "*a" };
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
    37
8554
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    38
-- Request-related helper functions
8692
c7122fbe1600 net.http: Fix for Lua 5.2: return error from error handler (xpcall changed)
Matthew Wild <mwild1@gmail.com>
parents: 8558
diff changeset
    39
local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); return err; end
8693
0f6623712239 net.http: Allow enabling/disabling error suppression, useful for tests
Matthew Wild <mwild1@gmail.com>
parents: 8692
diff changeset
    40
local function log_if_failed(req, ret, ...)
8554
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    41
	if not ret then
8693
0f6623712239 net.http: Allow enabling/disabling error suppression, useful for tests
Matthew Wild <mwild1@gmail.com>
parents: 8692
diff changeset
    42
		log("error", "Request '%s': error in callback: %s", req.id, tostring((...)));
0f6623712239 net.http: Allow enabling/disabling error suppression, useful for tests
Matthew Wild <mwild1@gmail.com>
parents: 8692
diff changeset
    43
		if not req.suppress_errors then
0f6623712239 net.http: Allow enabling/disabling error suppression, useful for tests
Matthew Wild <mwild1@gmail.com>
parents: 8692
diff changeset
    44
			error(...);
0f6623712239 net.http: Allow enabling/disabling error suppression, useful for tests
Matthew Wild <mwild1@gmail.com>
parents: 8692
diff changeset
    45
		end
8554
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    46
	end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    47
	return ...;
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    48
end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    49
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    50
local function destroy_request(request)
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    51
	local conn = request.conn;
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    52
	if conn then
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    53
		request.conn = nil;
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    54
		conn:close()
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    55
	end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    56
end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    57
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    58
local function request_reader(request, data, err)
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    59
	if not request.parser then
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    60
		local function error_cb(reason)
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    61
			if request.callback then
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    62
				request.callback(reason or "connection-closed", 0, request);
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    63
				request.callback = nil;
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    64
			end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    65
			destroy_request(request);
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    66
		end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    67
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    68
		if not data then
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    69
			error_cb(err);
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    70
			return;
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    71
		end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    72
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    73
		local function success_cb(r)
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    74
			if request.callback then
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    75
				request.callback(r.body, r.code, r, request);
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    76
				request.callback = nil;
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    77
			end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    78
			destroy_request(request);
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    79
		end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    80
		local function options_cb()
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    81
			return request;
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    82
		end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    83
		request.parser = httpstream_new(success_cb, error_cb, "client", options_cb);
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    84
	end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    85
	request.parser:feed(data);
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    86
end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    87
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    88
-- Connection listener callbacks
4557
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
    89
function listener.onconnect(conn)
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
    90
	local req = requests[conn];
8202
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
    91
8554
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    92
	-- Initialize request object
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    93
	req.write = function (...) return req.conn:write(...); end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    94
	local callback = req.callback;
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    95
	req.callback = function (content, code, response, request)
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    96
		do
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    97
			local event = { http = req.http, url = req.url, request = req, response = response, content = content, code = code, callback = req.callback };
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    98
			req.http.events.fire_event("response", event);
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
    99
			content, code, response = event.content, event.code, event.response;
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   100
		end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   101
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   102
		log("debug", "Request '%s': Calling callback, status %s", req.id, code or "---");
8710
fd39c44c0113 Merge 0.10 -> trunk
Matthew Wild <mwild1@gmail.com>
parents: 8693 8709
diff changeset
   103
		return log_if_failed(req.id, xpcall(function () return callback(content, code, response, request) end, handleerr));
8554
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   104
	end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   105
	req.reader = request_reader;
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   106
	req.state = "status";
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   107
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   108
	requests[req.conn] = req;
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   109
8202
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   110
	-- Validate certificate
8203
e92585ab4998 net.http: Add option for disabling TLS certifictate validation
Kim Alvefur <zash@zash.se>
parents: 8202
diff changeset
   111
	if not req.insecure and conn:ssl() then
8202
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   112
		local sock = conn:socket();
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   113
		local chain_valid = sock.getpeerverification and sock:getpeerverification();
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   114
		if not chain_valid then
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   115
			req.callback("certificate-chain-invalid", 0, req);
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   116
			req.callback = nil;
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   117
			conn:close();
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   118
			return;
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   119
		end
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   120
		local cert = sock.getpeercertificate and sock:getpeercertificate();
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   121
		if not cert or not verify_identity(req.host, false, cert) then
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   122
			req.callback("certificate-verify-failed", 0, req);
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   123
			req.callback = nil;
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   124
			conn:close();
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   125
			return;
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   126
		end
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   127
	end
8f82d3cd0631 net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents: 8200
diff changeset
   128
4557
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   129
	-- Send the request
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   130
	local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" };
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   131
	if req.query then
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   132
		t_insert(request_line, 4, "?"..req.query);
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   133
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5714
diff changeset
   134
4557
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   135
	conn:write(t_concat(request_line));
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   136
	local t = { [2] = ": ", [4] = "\r\n" };
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   137
	for k, v in pairs(req.headers) do
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   138
		t[1], t[3] = k, v;
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   139
		conn:write(t_concat(t));
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   140
	end
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   141
	conn:write("\r\n");
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5714
diff changeset
   142
4557
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   143
	if req.body then
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   144
		conn:write(req.body);
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   145
	end
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   146
end
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   147
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   148
function listener.onincoming(conn, data)
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   149
	local request = requests[conn];
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   150
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   151
	if not request then
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   152
		log("warn", "Received response from connection %s with no request attached!", tostring(conn));
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   153
		return;
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   154
	end
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   155
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   156
	if data and request.reader then
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   157
		request:reader(data);
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   158
	end
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   159
end
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   160
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   161
function listener.ondisconnect(conn, err)
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   162
	local request = requests[conn];
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   163
	if request and request.conn then
8048
55a56dc935f2 net.http: Pass error all the way to callback
Kim Alvefur <zash@zash.se>
parents: 7796
diff changeset
   164
		request:reader(nil, err or "closed");
4557
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   165
	end
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   166
	requests[conn] = nil;
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   167
end
2abe4e541d52 net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents: 4471
diff changeset
   168
8554
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   169
function listener.onattach(conn, req)
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   170
	requests[conn] = req;
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   171
	req.conn = conn;
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   172
end
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   173
6380
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 5948
diff changeset
   174
function listener.ondetach(conn)
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 5948
diff changeset
   175
	requests[conn] = nil;
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 5948
diff changeset
   176
end
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 5948
diff changeset
   177
8554
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   178
function listener.onfail(req, reason)
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   179
	req.http.events.fire_event("request-connection-error", { http = req.http, request = req, url = req.url, err = reason });
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   180
	req.callback(reason or "connection failed", 0, req);
7467
3b7de72e58a9 net.http: Add log messages for requests, including their id (so "calling callback" and tracebacks can be traced back to their initial request)
Matthew Wild <mwild1@gmail.com>
parents: 7466
diff changeset
   181
end
3b7de72e58a9 net.http: Add log messages for requests, including their id (so "calling callback" and tracebacks can be traced back to their initial request)
Matthew Wild <mwild1@gmail.com>
parents: 7466
diff changeset
   182
8116
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   183
local function request(self, u, ex, callback)
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   184
	local req = url.parse(u);
8118
375cf924fce1 net.http: Add request.url, which is the original full URL as a string
Matthew Wild <mwild1@gmail.com>
parents: 8117
diff changeset
   185
	req.url = u;
8554
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   186
	req.http = self;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5714
diff changeset
   187
903
6737d005a84a net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
   188
	if not (req and req.host) then
8048
55a56dc935f2 net.http: Pass error all the way to callback
Kim Alvefur <zash@zash.se>
parents: 7796
diff changeset
   189
		callback("invalid-url", 0, req);
903
6737d005a84a net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
   190
		return nil, "invalid-url";
6737d005a84a net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
   191
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5714
diff changeset
   192
903
6737d005a84a net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
   193
	if not req.path then
6737d005a84a net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
   194
		req.path = "/";
6737d005a84a net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
   195
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5714
diff changeset
   196
7466
3b6e7ce9431f net.http: Add request.id to every request object (can be overridden by providing ex.id)
Matthew Wild <mwild1@gmail.com>
parents: 6826
diff changeset
   197
	req.id = ex and ex.id or make_id(req);
3b6e7ce9431f net.http: Add request.id to every request object (can be overridden by providing ex.id)
Matthew Wild <mwild1@gmail.com>
parents: 6826
diff changeset
   198
8117
12df41a5a4b1 net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents: 8116
diff changeset
   199
	do
12df41a5a4b1 net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents: 8116
diff changeset
   200
		local event = { http = self, url = u, request = req, options = ex, callback = callback };
12df41a5a4b1 net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents: 8116
diff changeset
   201
		local ret = self.events.fire_event("pre-request", event);
12df41a5a4b1 net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents: 8116
diff changeset
   202
		if ret then
12df41a5a4b1 net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents: 8116
diff changeset
   203
			return ret;
12df41a5a4b1 net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents: 8116
diff changeset
   204
		end
8554
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   205
		req, u, ex, req.callback = event.request, event.url, event.options, event.callback;
8117
12df41a5a4b1 net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents: 8116
diff changeset
   206
	end
12df41a5a4b1 net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents: 8116
diff changeset
   207
4352
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 4351
diff changeset
   208
	local method, headers, body;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5714
diff changeset
   209
5714
520671c3159c net.http: Include port number (when non-standard) in the Host header of outgoing requests, as per the HTTP RFC
Matthew Wild <mwild1@gmail.com>
parents: 5505
diff changeset
   210
	local host, port = req.host, req.port;
520671c3159c net.http: Include port number (when non-standard) in the Host header of outgoing requests, as per the HTTP RFC
Matthew Wild <mwild1@gmail.com>
parents: 5505
diff changeset
   211
	local host_header = host;
520671c3159c net.http: Include port number (when non-standard) in the Host header of outgoing requests, as per the HTTP RFC
Matthew Wild <mwild1@gmail.com>
parents: 5505
diff changeset
   212
	if (port == "80" and req.scheme == "http")
520671c3159c net.http: Include port number (when non-standard) in the Host header of outgoing requests, as per the HTTP RFC
Matthew Wild <mwild1@gmail.com>
parents: 5505
diff changeset
   213
	or (port == "443" and req.scheme == "https") then
520671c3159c net.http: Include port number (when non-standard) in the Host header of outgoing requests, as per the HTTP RFC
Matthew Wild <mwild1@gmail.com>
parents: 5505
diff changeset
   214
		port = nil;
520671c3159c net.http: Include port number (when non-standard) in the Host header of outgoing requests, as per the HTTP RFC
Matthew Wild <mwild1@gmail.com>
parents: 5505
diff changeset
   215
	elseif port then
520671c3159c net.http: Include port number (when non-standard) in the Host header of outgoing requests, as per the HTTP RFC
Matthew Wild <mwild1@gmail.com>
parents: 5505
diff changeset
   216
		host_header = host_header..":"..port;
520671c3159c net.http: Include port number (when non-standard) in the Host header of outgoing requests, as per the HTTP RFC
Matthew Wild <mwild1@gmail.com>
parents: 5505
diff changeset
   217
	end
520671c3159c net.http: Include port number (when non-standard) in the Host header of outgoing requests, as per the HTTP RFC
Matthew Wild <mwild1@gmail.com>
parents: 5505
diff changeset
   218
4352
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 4351
diff changeset
   219
	headers = {
5714
520671c3159c net.http: Include port number (when non-standard) in the Host header of outgoing requests, as per the HTTP RFC
Matthew Wild <mwild1@gmail.com>
parents: 5505
diff changeset
   220
		["Host"] = host_header;
4352
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 4351
diff changeset
   221
		["User-Agent"] = "Prosody XMPP Server";
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 4351
diff changeset
   222
	};
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5714
diff changeset
   223
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   224
	if req.userinfo then
4972
1777271a1ec0 net.http: Use base64 from util.encodings instead of luasocket
Kim Alvefur <zash@zash.se>
parents: 4865
diff changeset
   225
		headers["Authorization"] = "Basic "..b64(req.userinfo);
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   226
	end
4351
3f414091a008 net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 4350
diff changeset
   227
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   228
	if ex then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   229
		req.onlystatus = ex.onlystatus;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   230
		body = ex.body;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   231
		if body then
4369
3578ff5d3674 net.http: Remove extra space after method in request status line for POST.
Waqas Hussain <waqas20@gmail.com>
parents: 4356
diff changeset
   232
			method = "POST";
4352
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 4351
diff changeset
   233
			headers["Content-Length"] = tostring(#body);
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 4351
diff changeset
   234
			headers["Content-Type"] = "application/x-www-form-urlencoded";
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   235
		end
4351
3f414091a008 net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 4350
diff changeset
   236
		if ex.method then method = ex.method; end
3f414091a008 net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 4350
diff changeset
   237
		if ex.headers then
3f414091a008 net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 4350
diff changeset
   238
			for k, v in pairs(ex.headers) do
3f414091a008 net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 4350
diff changeset
   239
				headers[k] = v;
3f414091a008 net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 4350
diff changeset
   240
			end
3f414091a008 net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 4350
diff changeset
   241
		end
8203
e92585ab4998 net.http: Add option for disabling TLS certifictate validation
Kim Alvefur <zash@zash.se>
parents: 8202
diff changeset
   242
		req.insecure = ex.insecure;
8693
0f6623712239 net.http: Allow enabling/disabling error suppression, useful for tests
Matthew Wild <mwild1@gmail.com>
parents: 8692
diff changeset
   243
		req.suppress_errors = ex.suppress_errors;
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   244
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5714
diff changeset
   245
7523
fc6c24cb3599 net.http: Add quotes around ids in log messages
Matthew Wild <mwild1@gmail.com>
parents: 7467
diff changeset
   246
	log("debug", "Making %s %s request '%s' to %s", req.scheme:upper(), method or "GET", req.id, (ex and ex.suppress_url and host_header) or u);
7467
3b7de72e58a9 net.http: Add log messages for requests, including their id (so "calling callback" and tracebacks can be traced back to their initial request)
Matthew Wild <mwild1@gmail.com>
parents: 7466
diff changeset
   247
4352
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 4351
diff changeset
   248
	-- Attach to request object
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 4351
diff changeset
   249
	req.method, req.headers, req.body = method, headers, body;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5714
diff changeset
   250
4352
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 4351
diff changeset
   251
	local using_https = req.scheme == "https";
5448
cbe9fa2d3787 net.http: Throw error when connecting to a http:// URL without LuaSec available
Matthew Wild <mwild1@gmail.com>
parents: 5354
diff changeset
   252
	if using_https and not ssl_available then
cbe9fa2d3787 net.http: Throw error when connecting to a http:// URL without LuaSec available
Matthew Wild <mwild1@gmail.com>
parents: 5354
diff changeset
   253
		error("SSL not available, unable to contact https URL");
cbe9fa2d3787 net.http: Throw error when connecting to a http:// URL without LuaSec available
Matthew Wild <mwild1@gmail.com>
parents: 5354
diff changeset
   254
	end
5714
520671c3159c net.http: Include port number (when non-standard) in the Host header of outgoing requests, as per the HTTP RFC
Matthew Wild <mwild1@gmail.com>
parents: 5505
diff changeset
   255
	local port_number = port and tonumber(port) or (using_https and 443 or 80);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5714
diff changeset
   256
5353
8c3f28f5c1c1 net.http: Allow passing an SSL context or options table to be used for HTTPS requests (thanks daurnimator)
Matthew Wild <mwild1@gmail.com>
parents: 4977
diff changeset
   257
	local sslctx = false;
8c3f28f5c1c1 net.http: Allow passing an SSL context or options table to be used for HTTPS requests (thanks daurnimator)
Matthew Wild <mwild1@gmail.com>
parents: 4977
diff changeset
   258
	if using_https then
8200
55826e29c719 net.http: Move default SSL/TLS settings into options, allowing them to be overriden in new()
Kim Alvefur <zash@zash.se>
parents: 8199
diff changeset
   259
		sslctx = ex and ex.sslctx or self.options and self.options.sslctx;
5353
8c3f28f5c1c1 net.http: Allow passing an SSL context or options table to be used for HTTPS requests (thanks daurnimator)
Matthew Wild <mwild1@gmail.com>
parents: 4977
diff changeset
   260
	end
8c3f28f5c1c1 net.http: Allow passing an SSL context or options table to be used for HTTPS requests (thanks daurnimator)
Matthew Wild <mwild1@gmail.com>
parents: 4977
diff changeset
   261
8554
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   262
	local http_service = basic_resolver.new(host, port_number);
2bd2e94a0496 net.http: Refactor to use new net.connect API, brings support for async DNS
Matthew Wild <mwild1@gmail.com>
parents: 8538
diff changeset
   263
	connect(http_service, listener, { sslctx = sslctx }, req);
8117
12df41a5a4b1 net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents: 8116
diff changeset
   264
12df41a5a4b1 net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents: 8116
diff changeset
   265
	self.events.fire_event("request", { http = self, request = req, url = u });
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   266
	return req;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   267
end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   268
8116
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   269
local function new(options)
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   270
	local http = {
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   271
		options = options;
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   272
		request = request;
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   273
		new = options and function (new_options)
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   274
			return new(setmetatable(new_options, { __index = options }));
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   275
		end or new;
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   276
		events = events.new();
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   277
	};
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   278
	return http;
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   279
end
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   280
8200
55826e29c719 net.http: Move default SSL/TLS settings into options, allowing them to be overriden in new()
Kim Alvefur <zash@zash.se>
parents: 8199
diff changeset
   281
local default_http = new({
55826e29c719 net.http: Move default SSL/TLS settings into options, allowing them to be overriden in new()
Kim Alvefur <zash@zash.se>
parents: 8199
diff changeset
   282
	sslctx = { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } };
8693
0f6623712239 net.http: Allow enabling/disabling error suppression, useful for tests
Matthew Wild <mwild1@gmail.com>
parents: 8692
diff changeset
   283
	suppress_errors = true;
8200
55826e29c719 net.http: Move default SSL/TLS settings into options, allowing them to be overriden in new()
Kim Alvefur <zash@zash.se>
parents: 8199
diff changeset
   284
});
8116
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   285
6783
647adfd8f738 net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6504
diff changeset
   286
return {
8116
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   287
	request = function (u, ex, callback)
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   288
		return default_http:request(u, ex, callback);
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   289
	end;
8199
bc2bcfa63b43 net.http: Expose defaults
Kim Alvefur <zash@zash.se>
parents: 8198
diff changeset
   290
	default = default_http;
8116
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   291
	new = new;
cfb5ab763384 net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents: 8048
diff changeset
   292
	events = default_http.events;
6783
647adfd8f738 net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6504
diff changeset
   293
	-- COMPAT
647adfd8f738 net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6504
diff changeset
   294
	urlencode = util_http.urlencode;
647adfd8f738 net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6504
diff changeset
   295
	urldecode = util_http.urldecode;
647adfd8f738 net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6504
diff changeset
   296
	formencode = util_http.formencode;
647adfd8f738 net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6504
diff changeset
   297
	formdecode = util_http.formdecode;
647adfd8f738 net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6504
diff changeset
   298
};