Software /
code /
prosody
Annotate
net/http.lua @ 6745:6728ad041761
Merge 0.10->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 25 Jun 2015 18:57:43 +0200 |
parent | 6502:8fed6ea12098 |
child | 6791:e813e8cf6046 |
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"; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
5448
cbe9fa2d3787
net.http: Throw error when connecting to a http:// URL without LuaSec available
Matthew Wild <mwild1@gmail.com>
parents:
5354
diff
changeset
|
14 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
|
15 |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local server = require "net.server" |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 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
|
19 local pairs = pairs; |
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
|
20 local tonumber, tostring, xpcall, select, traceback = |
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 tonumber, tostring, xpcall, select, debug.traceback; |
5948
1341384628ec
net.http: assert() for socket creation success so it doesn't silently fail (thanks daurnimator)
Matthew Wild <mwild1@gmail.com>
parents:
5714
diff
changeset
|
22 local assert, error = assert, error |
678
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
23 |
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
24 local log = require "util.logger".init("http"); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
1331
4443309b5528
net.http: (Re-)add url[en|de]code functions
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
26 module "http" |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
4557
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
28 local requests = {}; -- Open requests |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
29 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
30 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
|
31 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
32 function listener.onconnect(conn) |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
33 local req = requests[conn]; |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
34 -- Send the request |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
35 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
|
36 if req.query then |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
37 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
|
38 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
39 |
4557
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
40 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
|
41 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
|
42 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
|
43 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
|
44 conn:write(t_concat(t)); |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
45 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
46 conn:write("\r\n"); |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
47 |
4557
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
48 if req.body then |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
49 conn:write(req.body); |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
50 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
51 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
52 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
53 function listener.onincoming(conn, data) |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
54 local request = requests[conn]; |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
55 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
56 if not request then |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
57 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
|
58 return; |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
59 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
60 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
61 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
|
62 request:reader(data); |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
63 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
64 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
65 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
66 function listener.ondisconnect(conn, err) |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
67 local request = requests[conn]; |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
68 if request and request.conn 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
|
69 request:reader(nil, err); |
4557
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
70 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
71 requests[conn] = nil; |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
72 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
73 |
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
|
74 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
|
75 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
|
76 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
|
77 |
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
|
78 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
|
79 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
|
80 local function error_cb(reason) |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 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
|
82 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
|
83 request.callback = nil; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 destroy_request(request); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
87 |
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
|
88 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
|
89 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
|
90 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
|
91 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
92 |
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
|
93 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
|
94 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
|
95 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
|
96 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
|
97 end |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
98 destroy_request(request); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 end |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
100 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
|
101 return request; |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
102 end |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
103 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
|
104 end |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
105 request.parser:feed(data); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 |
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
|
108 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end |
646
90da4c9b34b5
HTTP requests now have status code as a number instead of a string. Switched parameters on both http.request() and the callback to better match LuaSocket's http module
Matthew Wild <mwild1@gmail.com>
parents:
633
diff
changeset
|
109 function request(u, ex, callback) |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 local req = url.parse(u); |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
111 |
903
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
112 if not (req and req.host) then |
923
c63f9bc45a85
Fixed: net/http.lua: HTTP request callback wasn't being called on some errors
Waqas Hussain <waqas20@gmail.com>
parents:
903
diff
changeset
|
113 callback(nil, 0, req); |
903
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
114 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
|
115 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
116 |
903
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
117 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
|
118 req.path = "/"; |
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
119 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
120 |
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
|
121 local method, headers, body; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
122 |
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
|
123 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
|
124 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
|
125 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
|
126 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
|
127 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
|
128 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
|
129 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
|
130 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
|
131 |
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
|
132 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
|
133 ["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
|
134 ["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
|
135 }; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
136 |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 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
|
138 headers["Authorization"] = "Basic "..b64(req.userinfo); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 end |
4351
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
140 |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 if ex then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 req.onlystatus = ex.onlystatus; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 body = ex.body; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 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
|
145 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
|
146 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
|
147 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
|
148 end |
4351
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
149 if ex.method then method = ex.method; end |
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
150 if ex.headers then |
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
151 for k, v in pairs(ex.headers) do |
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
152 headers[k] = v; |
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
153 end |
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
154 end |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
156 |
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
|
157 -- 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
|
158 req.method, req.headers, req.body = method, headers, body; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
159 |
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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 |
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
|
166 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
|
167 if using_https then |
6496
e4b998ffc922
certmanager, net.http: Disable SSLv3 by default
Matthew Wild <mwild1@gmail.com>
parents:
6380
diff
changeset
|
168 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2", "no_sslv3" } }; |
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
|
169 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
|
170 |
5964
ad04170d6533
net/http: Use server.addclient
daurnimator <quae@daurnimator.com>
parents:
5950
diff
changeset
|
171 local handler, conn = server.addclient(host, port_number, listener, "*a", sslctx) |
ad04170d6533
net/http: Use server.addclient
daurnimator <quae@daurnimator.com>
parents:
5950
diff
changeset
|
172 if not handler then |
ad04170d6533
net/http: Use server.addclient
daurnimator <quae@daurnimator.com>
parents:
5950
diff
changeset
|
173 callback(nil, 0, req); |
ad04170d6533
net/http: Use server.addclient
daurnimator <quae@daurnimator.com>
parents:
5950
diff
changeset
|
174 return nil, conn; |
ad04170d6533
net/http: Use server.addclient
daurnimator <quae@daurnimator.com>
parents:
5950
diff
changeset
|
175 end |
ad04170d6533
net/http: Use server.addclient
daurnimator <quae@daurnimator.com>
parents:
5950
diff
changeset
|
176 req.handler, req.conn = handler, conn |
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
|
177 req.write = function (...) return req.handler:write(...); end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5714
diff
changeset
|
178 |
4471
ea24d73e6c94
net.http: Pass response object to callbacks (feels hacky, should this be passed *instead of* the request?)
Matthew Wild <mwild1@gmail.com>
parents:
4369
diff
changeset
|
179 req.callback = function (content, code, request, response) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request, response) end, handleerr)); end |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 req.reader = request_reader; |
678
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
181 req.state = "status"; |
4351
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
182 |
4557
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
183 requests[req.handler] = req; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 return req; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 function destroy_request(request) |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 if request.conn then |
2673
61ae351c19b5
net.http: destroy_request(): Remove update for new server API, pass request.handler instead of request.conn to ondisconnect
Matthew Wild <mwild1@gmail.com>
parents:
2672
diff
changeset
|
189 request.conn = nil; |
61ae351c19b5
net.http: destroy_request(): Remove update for new server API, pass request.handler instead of request.conn to ondisconnect
Matthew Wild <mwild1@gmail.com>
parents:
2672
diff
changeset
|
190 request.handler:close() |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 |
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
|
194 local urlencode, urldecode = util_http.urlencode, util_http.urldecode; |
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
|
195 local formencode, formdecode = util_http.formencode, util_http.formdecode; |
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
|
196 |
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
|
197 _M.urlencode, _M.urldecode = urlencode, urldecode; |
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
|
198 _M.formencode, _M.formdecode = formencode, formdecode; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 return _M; |