Software /
code /
prosody
Annotate
net/http.lua @ 5448:cbe9fa2d3787
net.http: Throw error when connecting to a http:// URL without LuaSec available
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 08 Apr 2013 16:40:27 +0100 |
parent | 5354:18ebc3874364 |
child | 5458:84162b81c863 |
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 |
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1331
diff
changeset
|
4 -- |
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 |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local socket = require "socket" |
4972
1777271a1ec0
net.http: Use base64 from util.encodings instead of luasocket
Kim Alvefur <zash@zash.se>
parents:
4865
diff
changeset
|
10 local b64 = require "util.encodings".base64.encode; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local url = require "socket.url" |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
12 local httpstream_new = require "util.httpstream".new; |
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; |
3470
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
19 local pairs, ipairs = pairs, ipairs; |
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
20 local tonumber, tostring, xpcall, select, debug_traceback, char, format = |
3540
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
3470
diff
changeset
|
21 tonumber, tostring, xpcall, select, debug.traceback, string.char, string.format; |
678
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
22 |
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
23 local log = require "util.logger".init("http"); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 |
1331
4443309b5528
net.http: (Re-)add url[en|de]code functions
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
25 module "http" |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
4557
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
27 local requests = {}; -- Open requests |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
28 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
29 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
|
30 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
31 function listener.onconnect(conn) |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
32 local req = requests[conn]; |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
33 -- Send the request |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
34 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
|
35 if req.query then |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
36 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
|
37 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
38 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
39 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
|
40 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
|
41 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
|
42 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
|
43 conn:write(t_concat(t)); |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
44 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
45 conn:write("\r\n"); |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
46 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
47 if req.body then |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
48 conn:write(req.body); |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
49 end |
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 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
52 function listener.onincoming(conn, data) |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
53 local request = requests[conn]; |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
54 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
55 if not request then |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
56 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
|
57 return; |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
58 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
59 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
60 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
|
61 request:reader(data); |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
62 end |
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 |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
65 function listener.ondisconnect(conn, err) |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
66 local request = requests[conn]; |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
67 if request and request.conn then |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
68 request:reader(nil); |
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 requests[conn] = nil; |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
71 end |
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
72 |
4865
9171dc2357e0
net.http: Fix urlencode to not encode unreserved characters, so I can guiltlessly rant about people who do.
Matthew Wild <mwild1@gmail.com>
parents:
4557
diff
changeset
|
73 function urlencode(s) return s and (s:gsub("[^a-zA-Z0-9.~_-]", function (c) return format("%%%02x", c:byte()); end)); end |
1331
4443309b5528
net.http: (Re-)add url[en|de]code functions
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
74 function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end)); end |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 |
3470
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
76 local function _formencodepart(s) |
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
77 return s and (s:gsub("%W", function (c) |
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
78 if c ~= " " then |
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
79 return format("%%%02x", c:byte()); |
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
80 else |
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
81 return "+"; |
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
82 end |
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
83 end)); |
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
84 end |
4338
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
85 |
3470
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
86 function formencode(form) |
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
87 local result = {}; |
4355
9e2a841739b5
net.http: Support for k->v maps in http.formencode
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
88 if form[1] then -- Array of ordered { name, value } |
9e2a841739b5
net.http: Support for k->v maps in http.formencode
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
89 for _, field in ipairs(form) do |
9e2a841739b5
net.http: Support for k->v maps in http.formencode
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
90 t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value)); |
9e2a841739b5
net.http: Support for k->v maps in http.formencode
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
91 end |
9e2a841739b5
net.http: Support for k->v maps in http.formencode
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
92 else -- Unordered map of name -> value |
9e2a841739b5
net.http: Support for k->v maps in http.formencode
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
93 for name, value in pairs(form) do |
9e2a841739b5
net.http: Support for k->v maps in http.formencode
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
94 t_insert(result, _formencodepart(name).."=".._formencodepart(value)); |
9e2a841739b5
net.http: Support for k->v maps in http.formencode
Matthew Wild <mwild1@gmail.com>
parents:
4352
diff
changeset
|
95 end |
3470
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
96 end |
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
97 return t_concat(result, "&"); |
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
98 end |
0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
99 |
4338
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
100 function formdecode(s) |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
101 if not s:match("=") then return urldecode(s); end |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
102 local r = {}; |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
103 for k, v in s:gmatch("([^=&]*)=([^&]*)") do |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
104 k, v = k:gsub("%+", "%%20"), v:gsub("%+", "%%20"); |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
105 k, v = urldecode(k), urldecode(v); |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
106 t_insert(r, { name = k, value = v }); |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
107 r[k] = v; |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
108 end |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
109 return r; |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
110 end |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
111 |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 local function request_reader(request, data, startpos) |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
113 if not request.parser then |
4350
0b9ed126286e
net.http: Small fix to prevent traceback when connection fails before sending the request
Matthew Wild <mwild1@gmail.com>
parents:
4338
diff
changeset
|
114 if not data then return; end |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
115 local function success_cb(r) |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 if request.callback then |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
117 for k,v in pairs(r) do request[k] = v; end |
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
|
118 request.callback(r.body, r.code, request, r); |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
119 request.callback = nil; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 destroy_request(request); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 end |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
123 local function error_cb(r) |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
124 if request.callback then |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
125 request.callback(r or "connection-closed", 0, request); |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
126 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
|
127 end |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
128 destroy_request(request); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 end |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
130 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
|
131 return request; |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
132 end |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
133 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
|
134 end |
3569
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:feed(data); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 |
678
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
138 local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug_traceback()); 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
|
139 function request(u, ex, callback) |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 local req = url.parse(u); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 |
903
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
142 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
|
143 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
|
144 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
|
145 end |
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
146 |
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
147 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
|
148 req.path = "/"; |
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
149 end |
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
150 |
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
|
151 local method, headers, body; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 |
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
|
153 headers = { |
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
|
154 ["Host"] = req.host; |
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
|
155 ["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
|
156 }; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 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
|
159 headers["Authorization"] = "Basic "..b64(req.userinfo); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 end |
4351
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
161 |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 if ex then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 req.onlystatus = ex.onlystatus; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 body = ex.body; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 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
|
166 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
|
167 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
|
168 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
|
169 end |
4351
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
170 if ex.method then method = ex.method; end |
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
171 if ex.headers then |
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
172 for k, v in pairs(ex.headers) do |
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
173 headers[k] = v; |
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
174 end |
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
175 end |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 |
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
|
178 -- 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
|
179 req.method, req.headers, req.body = method, headers, 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
|
180 |
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
|
181 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
|
182 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
|
183 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
|
184 end |
4356
10a4f3b081a7
net.http: Convert port to a number (for custom port in URL)
Matthew Wild <mwild1@gmail.com>
parents:
4355
diff
changeset
|
185 local port = tonumber(req.port) or (using_https and 443 or 80); |
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
|
186 |
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
|
187 -- Connect the socket, and wrap it with net.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
|
188 local conn = socket.tcp(); |
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
|
189 conn:settimeout(10); |
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
|
190 local ok, err = conn:connect(req.host, port); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 if not ok and err ~= "timeout" then |
903
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
192 callback(nil, 0, req); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 return nil, err; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 |
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
|
196 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
|
197 if using_https then |
5354
18ebc3874364
net.http: Disable SSLv2 support for HTTPS connections
Matthew Wild <mwild1@gmail.com>
parents:
5353
diff
changeset
|
198 sslctx = ex and ex.sslctx or { mode = "client", protocol = "sslv23", options = { "no_sslv2" } }; |
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
|
199 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
|
200 |
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
|
201 req.handler, req.conn = server.wrapclient(conn, req.host, port, listener, "*a", sslctx); |
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
|
202 req.write = function (...) return req.handler:write(...); end |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 |
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
|
204 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
|
205 req.reader = request_reader; |
678
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
206 req.state = "status"; |
4351
3f414091a008
net.http: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
4350
diff
changeset
|
207 |
4557
2abe4e541d52
net.http, httpclient_listener: Merge listener into net.http
Matthew Wild <mwild1@gmail.com>
parents:
4471
diff
changeset
|
208 requests[req.handler] = req; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
209 return req; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
210 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
211 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
212 function destroy_request(request) |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
213 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
|
214 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
|
215 request.handler:close() |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
216 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
217 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
219 _M.urlencode = urlencode; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
221 return _M; |