Software /
code /
prosody
Annotate
net/http.lua @ 8535:b6f3b34ecc03
net.http: Rename handler to conn, for consistency with all our other code
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 23 Feb 2018 16:21:40 +0000 |
parent | 8534:c071cabfa066 |
child | 8551:2bd2e94a0496 |
rev | line source |
---|---|
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"; |
8113
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
13 local events = require "util.events"; |
8199
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
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 |
5448
cbe9fa2d3787
net.http: Throw error when connecting to a http:// URL without LuaSec available
Matthew Wild <mwild1@gmail.com>
parents:
5354
diff
changeset
|
16 local ssl_available = pcall(require, "ssl"); |
cbe9fa2d3787
net.http: Throw error when connecting to a http:// URL without LuaSec available
Matthew Wild <mwild1@gmail.com>
parents:
5354
diff
changeset
|
17 |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local server = require "net.server" |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 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
|
21 local pairs = pairs; |
7792
0bc6c3704973
net.http: Remove unused imports [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7520
diff
changeset
|
22 local tonumber, tostring, xpcall, traceback = |
0bc6c3704973
net.http: Remove unused imports [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7520
diff
changeset
|
23 tonumber, tostring, xpcall, debug.traceback; |
0bc6c3704973
net.http: Remove unused imports [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7520
diff
changeset
|
24 local error = error |
678
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
25 |
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
26 local log = require "util.logger".init("http"); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
6780
647adfd8f738
net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6501
diff
changeset
|
28 local _ENV = nil; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
4557
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
30 local requests = {}; -- Open requests |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
31 |
7463
3b6e7ce9431f
net.http: Add request.id to every request object (can be overridden by providing ex.id)
Matthew Wild <mwild1@gmail.com>
parents:
6823
diff
changeset
|
32 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:
6823
diff
changeset
|
33 |
4557
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
34 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
|
35 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
36 function listener.onconnect(conn) |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
37 local req = requests[conn]; |
8199
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
38 |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
39 -- Validate certificate |
8200
e92585ab4998
net.http: Add option for disabling TLS certifictate validation
Kim Alvefur <zash@zash.se>
parents:
8199
diff
changeset
|
40 if not req.insecure and conn:ssl() then |
8199
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
41 local sock = conn:socket(); |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
42 local chain_valid = sock.getpeerverification and sock:getpeerverification(); |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
43 if not chain_valid then |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
44 req.callback("certificate-chain-invalid", 0, req); |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
45 req.callback = nil; |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
46 conn:close(); |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
47 return; |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
48 end |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
49 local cert = sock.getpeercertificate and sock:getpeercertificate(); |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
50 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:
8197
diff
changeset
|
51 req.callback("certificate-verify-failed", 0, req); |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
52 req.callback = nil; |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
53 conn:close(); |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
54 return; |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
55 end |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
56 end |
8f82d3cd0631
net.http: Validate HTTPS certificates (fixes #659)
Kim Alvefur <zash@zash.se>
parents:
8197
diff
changeset
|
57 |
4557
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
58 -- Send the request |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
59 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
|
60 if req.query then |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
61 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
|
62 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
63 |
4557
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
64 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
|
65 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
|
66 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
|
67 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
|
68 conn:write(t_concat(t)); |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
69 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
70 conn:write("\r\n"); |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
71 |
4557
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
72 if req.body then |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
73 conn:write(req.body); |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
74 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
75 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
76 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
77 function listener.onincoming(conn, data) |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
78 local request = requests[conn]; |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
79 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
80 if not request then |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
81 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
|
82 return; |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
83 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
84 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
85 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
|
86 request:reader(data); |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
87 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
88 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
89 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
90 function listener.ondisconnect(conn, err) |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
91 local request = requests[conn]; |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
92 if request and request.conn then |
8045
55a56dc935f2
net.http: Pass error all the way to callback
Kim Alvefur <zash@zash.se>
parents:
7793
diff
changeset
|
93 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
|
94 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
95 requests[conn] = nil; |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
96 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
97 |
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
|
98 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
|
99 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
|
100 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
|
101 |
6780
647adfd8f738
net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6501
diff
changeset
|
102 local function destroy_request(request) |
8534
c071cabfa066
net.http: Remove use of 'conn' (raw socket object returned by addclient)
Matthew Wild <mwild1@gmail.com>
parents:
8201
diff
changeset
|
103 local conn = request.conn; |
c071cabfa066
net.http: Remove use of 'conn' (raw socket object returned by addclient)
Matthew Wild <mwild1@gmail.com>
parents:
8201
diff
changeset
|
104 if conn then |
6780
647adfd8f738
net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6501
diff
changeset
|
105 request.conn = nil; |
8534
c071cabfa066
net.http: Remove use of 'conn' (raw socket object returned by addclient)
Matthew Wild <mwild1@gmail.com>
parents:
8201
diff
changeset
|
106 conn:close() |
6780
647adfd8f738
net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6501
diff
changeset
|
107 end |
647adfd8f738
net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6501
diff
changeset
|
108 end |
647adfd8f738
net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6501
diff
changeset
|
109 |
5488
0880a079d830
net.http: When HTTP request fails due to a network or SSL error, call the callback to let it know
Matthew Wild <mwild1@gmail.com>
parents:
5466
diff
changeset
|
110 local function request_reader(request, data, err) |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
111 if not request.parser then |
5488
0880a079d830
net.http: When HTTP request fails due to a network or SSL error, call the callback to let it know
Matthew Wild <mwild1@gmail.com>
parents:
5466
diff
changeset
|
112 local function error_cb(reason) |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 if request.callback then |
5488
0880a079d830
net.http: When HTTP request fails due to a network or SSL error, call the callback to let it know
Matthew Wild <mwild1@gmail.com>
parents:
5466
diff
changeset
|
114 request.callback(reason or "connection-closed", 0, request); |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
115 request.callback = nil; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 destroy_request(request); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
119 |
5488
0880a079d830
net.http: When HTTP request fails due to a network or SSL error, call the callback to let it know
Matthew Wild <mwild1@gmail.com>
parents:
5466
diff
changeset
|
120 if not data then |
0880a079d830
net.http: When HTTP request fails due to a network or SSL error, call the callback to let it know
Matthew Wild <mwild1@gmail.com>
parents:
5466
diff
changeset
|
121 error_cb(err); |
0880a079d830
net.http: When HTTP request fails due to a network or SSL error, call the callback to let it know
Matthew Wild <mwild1@gmail.com>
parents:
5466
diff
changeset
|
122 return; |
0880a079d830
net.http: When HTTP request fails due to a network or SSL error, call the callback to let it know
Matthew Wild <mwild1@gmail.com>
parents:
5466
diff
changeset
|
123 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
124 |
5488
0880a079d830
net.http: When HTTP request fails due to a network or SSL error, call the callback to let it know
Matthew Wild <mwild1@gmail.com>
parents:
5466
diff
changeset
|
125 local function success_cb(r) |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
126 if request.callback then |
5488
0880a079d830
net.http: When HTTP request fails due to a network or SSL error, call the callback to let it know
Matthew Wild <mwild1@gmail.com>
parents:
5466
diff
changeset
|
127 request.callback(r.body, r.code, r, request); |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
128 request.callback = nil; |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
129 end |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
130 destroy_request(request); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 end |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
132 local function options_cb() |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
133 return request; |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
134 end |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
135 request.parser = httpstream_new(success_cb, error_cb, "client", options_cb); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 end |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
137 request.parser:feed(data); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 |
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
|
140 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end |
7464
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:
7463
diff
changeset
|
141 local function log_if_failed(id, ret, ...) |
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:
7463
diff
changeset
|
142 if not ret then |
7520
fc6c24cb3599
net.http: Add quotes around ids in log messages
Matthew Wild <mwild1@gmail.com>
parents:
7464
diff
changeset
|
143 log("error", "Request '%s': error in callback: %s", id, tostring((...))); |
7464
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:
7463
diff
changeset
|
144 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:
7463
diff
changeset
|
145 return ...; |
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:
7463
diff
changeset
|
146 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:
7463
diff
changeset
|
147 |
8113
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
148 local function request(self, u, ex, callback) |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 local req = url.parse(u); |
8115
375cf924fce1
net.http: Add request.url, which is the original full URL as a string
Matthew Wild <mwild1@gmail.com>
parents:
8114
diff
changeset
|
150 req.url = u; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
151 |
903
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
152 if not (req and req.host) then |
8045
55a56dc935f2
net.http: Pass error all the way to callback
Kim Alvefur <zash@zash.se>
parents:
7793
diff
changeset
|
153 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
|
154 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
|
155 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
156 |
903
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
157 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
|
158 req.path = "/"; |
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
159 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
160 |
7463
3b6e7ce9431f
net.http: Add request.id to every request object (can be overridden by providing ex.id)
Matthew Wild <mwild1@gmail.com>
parents:
6823
diff
changeset
|
161 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:
6823
diff
changeset
|
162 |
8114
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
163 do |
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
164 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:
8113
diff
changeset
|
165 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:
8113
diff
changeset
|
166 if ret then |
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
167 return ret; |
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
168 end |
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
169 req, u, ex, callback = event.request, event.url, event.options, event.callback; |
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
170 end |
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
171 |
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
|
172 local method, headers, body; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
173 |
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
|
174 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
|
175 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
|
176 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
|
177 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
|
178 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
|
179 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
|
180 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
|
181 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
|
182 |
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
|
183 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
|
184 ["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
|
185 ["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
|
186 }; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
187 |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 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
|
189 headers["Authorization"] = "Basic "..b64(req.userinfo); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 end |
4351
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
191 |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 if ex then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 req.onlystatus = ex.onlystatus; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 body = ex.body; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 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
|
196 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
|
197 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
|
198 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
|
199 end |
4351
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
200 if ex.method then method = ex.method; end |
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
201 if ex.headers then |
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
202 for k, v in pairs(ex.headers) do |
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
203 headers[k] = v; |
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
204 end |
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
205 end |
8200
e92585ab4998
net.http: Add option for disabling TLS certifictate validation
Kim Alvefur <zash@zash.se>
parents:
8199
diff
changeset
|
206 req.insecure = ex.insecure; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
208 |
7520
fc6c24cb3599
net.http: Add quotes around ids in log messages
Matthew Wild <mwild1@gmail.com>
parents:
7464
diff
changeset
|
209 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); |
7464
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:
7463
diff
changeset
|
210 |
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
|
211 -- 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
|
212 req.method, req.headers, req.body = method, headers, body; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
213 |
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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 |
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
|
220 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
|
221 if using_https then |
8197
55826e29c719
net.http: Move default SSL/TLS settings into options, allowing them to be overriden in new()
Kim Alvefur <zash@zash.se>
parents:
8196
diff
changeset
|
222 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
|
223 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
|
224 |
8535
b6f3b34ecc03
net.http: Rename handler to conn, for consistency with all our other code
Matthew Wild <mwild1@gmail.com>
parents:
8534
diff
changeset
|
225 local conn, ret = server.addclient(host, port_number, listener, "*a", sslctx) |
b6f3b34ecc03
net.http: Rename handler to conn, for consistency with all our other code
Matthew Wild <mwild1@gmail.com>
parents:
8534
diff
changeset
|
226 if not conn then |
8534
c071cabfa066
net.http: Remove use of 'conn' (raw socket object returned by addclient)
Matthew Wild <mwild1@gmail.com>
parents:
8201
diff
changeset
|
227 self.events.fire_event("request-connection-error", { http = self, request = req, url = u, err = ret }); |
c071cabfa066
net.http: Remove use of 'conn' (raw socket object returned by addclient)
Matthew Wild <mwild1@gmail.com>
parents:
8201
diff
changeset
|
228 callback(ret, 0, req); |
c071cabfa066
net.http: Remove use of 'conn' (raw socket object returned by addclient)
Matthew Wild <mwild1@gmail.com>
parents:
8201
diff
changeset
|
229 return nil, ret; |
6823
3b07f38d70f5
net/http: Use server.addclient
daurnimator <quae@daurnimator.com>
parents:
6780
diff
changeset
|
230 end |
8535
b6f3b34ecc03
net.http: Rename handler to conn, for consistency with all our other code
Matthew Wild <mwild1@gmail.com>
parents:
8534
diff
changeset
|
231 req.conn = conn |
8534
c071cabfa066
net.http: Remove use of 'conn' (raw socket object returned by addclient)
Matthew Wild <mwild1@gmail.com>
parents:
8201
diff
changeset
|
232 req.write = function (...) return req.conn:write(...); end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
233 |
8114
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
234 req.callback = function (content, code, response, request) |
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
235 do |
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
236 local event = { http = self, url = u, request = req, response = response, content = content, code = code, callback = callback }; |
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
237 self.events.fire_event("response", event); |
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
238 content, code, response = event.content, event.code, event.response; |
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
239 end |
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
240 |
7520
fc6c24cb3599
net.http: Add quotes around ids in log messages
Matthew Wild <mwild1@gmail.com>
parents:
7464
diff
changeset
|
241 log("debug", "Request '%s': Calling callback, status %s", req.id, code or "---"); |
7464
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:
7463
diff
changeset
|
242 return log_if_failed(req.id, xpcall(function () return callback(content, code, request, response) end, handleerr)); |
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:
7463
diff
changeset
|
243 end |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
244 req.reader = request_reader; |
678
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
245 req.state = "status"; |
4351
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
246 |
8534
c071cabfa066
net.http: Remove use of 'conn' (raw socket object returned by addclient)
Matthew Wild <mwild1@gmail.com>
parents:
8201
diff
changeset
|
247 requests[req.conn] = req; |
8114
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
248 |
12df41a5a4b1
net.http: Fire new events: pre-request, request-connection-error, request, response
Matthew Wild <mwild1@gmail.com>
parents:
8113
diff
changeset
|
249 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
|
250 return req; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
251 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
252 |
8113
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
253 local function new(options) |
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
254 local http = { |
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
255 options = options; |
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
256 request = request; |
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
257 new = options and function (new_options) |
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
258 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:
8045
diff
changeset
|
259 end or new; |
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
260 events = events.new(); |
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
261 }; |
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
262 return http; |
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
263 end |
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
264 |
8197
55826e29c719
net.http: Move default SSL/TLS settings into options, allowing them to be overriden in new()
Kim Alvefur <zash@zash.se>
parents:
8196
diff
changeset
|
265 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:
8196
diff
changeset
|
266 sslctx = { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } }; |
55826e29c719
net.http: Move default SSL/TLS settings into options, allowing them to be overriden in new()
Kim Alvefur <zash@zash.se>
parents:
8196
diff
changeset
|
267 }); |
8113
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
268 |
6780
647adfd8f738
net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6501
diff
changeset
|
269 return { |
8113
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
270 request = function (u, ex, callback) |
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
271 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:
8045
diff
changeset
|
272 end; |
8196 | 273 default = default_http; |
8113
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
274 new = new; |
cfb5ab763384
net.http: Allow creation of http client objects, with custom options
Matthew Wild <mwild1@gmail.com>
parents:
8045
diff
changeset
|
275 events = default_http.events; |
6780
647adfd8f738
net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6501
diff
changeset
|
276 -- COMPAT |
647adfd8f738
net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6501
diff
changeset
|
277 urlencode = util_http.urlencode; |
647adfd8f738
net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6501
diff
changeset
|
278 urldecode = util_http.urldecode; |
647adfd8f738
net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6501
diff
changeset
|
279 formencode = util_http.formencode; |
647adfd8f738
net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6501
diff
changeset
|
280 formdecode = util_http.formdecode; |
647adfd8f738
net.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6501
diff
changeset
|
281 }; |