Software /
code /
prosody
Annotate
net/http.lua @ 4338:5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 10 Aug 2011 17:49:10 -0400 |
parent | 3569:f30da46e0add |
child | 4350:0b9ed126286e |
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 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local socket = require "socket" |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local mime = require "mime" |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 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
|
13 local httpstream_new = require "util.httpstream".new; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local server = require "net.server" |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 local connlisteners_get = require "net.connlisteners".get; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local listener = connlisteners_get("httpclient") or error("No httpclient listener!"); |
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; |
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
|
21 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
|
22 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
|
23 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
|
24 |
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
25 local log = require "util.logger".init("http"); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
1331
4443309b5528
net.http: (Re-)add url[en|de]code functions
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
27 module "http" |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 |
1331
4443309b5528
net.http: (Re-)add url[en|de]code functions
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
29 function urlencode(s) return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); end |
4443309b5528
net.http: (Re-)add url[en|de]code functions
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
30 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
|
31 |
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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 end |
4338
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
41 |
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
|
42 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
|
43 local 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
|
44 for _, field in ipairs(form) do |
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
|
45 t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value)); |
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
|
46 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
|
47 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
|
48 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
|
49 |
4338
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
50 function formdecode(s) |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
51 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
|
52 local r = {}; |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
53 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
|
54 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
|
55 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
|
56 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
|
57 r[k] = v; |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
58 end |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
59 return r; |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
60 end |
5d5d6c6d121a
net.http: Add formdecode to decode an urlencoded form
Matthew Wild <mwild1@gmail.com>
parents:
3569
diff
changeset
|
61 |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 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
|
63 if not request.parser then |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
64 local function success_cb(r) |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 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
|
66 for k,v in pairs(r) do request[k] = v; end |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
67 request.callback(r.body, r.code, request); |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
68 request.callback = nil; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 destroy_request(request); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 end |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 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
|
76 end |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
77 destroy_request(request); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 end |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
79 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
|
80 return request; |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
81 end |
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
82 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
|
83 end |
3569
f30da46e0add
net.http: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3540
diff
changeset
|
84 request.parser:feed(data); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 |
678
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
87 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
|
88 function request(u, ex, callback) |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 local req = url.parse(u); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 |
903
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
91 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
|
92 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
|
93 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
|
94 end |
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
95 |
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
96 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
|
97 req.path = "/"; |
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
98 end |
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
99 |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 local custom_headers, body; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 local default_headers = { ["Host"] = req.host, ["User-Agent"] = "Prosody XMPP Server" } |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 if req.userinfo then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 default_headers["Authorization"] = "Basic "..mime.b64(req.userinfo); |
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 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 if ex then |
738
cf70342985df
net.http: custom_headers -> headers
Matthew Wild <mwild1@gmail.com>
parents:
720
diff
changeset
|
109 custom_headers = ex.headers; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 req.onlystatus = ex.onlystatus; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 body = ex.body; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 if body then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 req.method = "POST "; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 default_headers["Content-Length"] = tostring(#body); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 default_headers["Content-Type"] = "application/x-www-form-urlencoded"; |
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 if ex.method then req.method = ex.method; end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 |
739
1def06cd9311
Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents:
738
diff
changeset
|
120 req.handler, req.conn = server.wrapclient(socket.tcp(), req.host, req.port or 80, listener, "*a"); |
2130
828e161cdfc7
net.httpserver, net.http: Update for new net.server API (untested)
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
121 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
|
122 req.conn:settimeout(0); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 local ok, err = req.conn:connect(req.host, req.port or 80); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 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
|
125 callback(nil, 0, req); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 return nil, err; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 |
719
b1eb112478b8
net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents:
714
diff
changeset
|
129 local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" }; |
b1eb112478b8
net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents:
714
diff
changeset
|
130 |
b1eb112478b8
net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents:
714
diff
changeset
|
131 if req.query then |
b1eb112478b8
net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents:
714
diff
changeset
|
132 t_insert(request_line, 4, "?"); |
b1eb112478b8
net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents:
714
diff
changeset
|
133 t_insert(request_line, 5, req.query); |
b1eb112478b8
net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents:
714
diff
changeset
|
134 end |
b1eb112478b8
net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents:
714
diff
changeset
|
135 |
b1eb112478b8
net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents:
714
diff
changeset
|
136 req.write(t_concat(request_line)); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 local t = { [2] = ": ", [4] = "\r\n" }; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 if custom_headers then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 for k, v in pairs(custom_headers) do |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 t[1], t[3] = k, v; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 req.write(t_concat(t)); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 default_headers[k] = nil; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 for k, v in pairs(default_headers) do |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 t[1], t[3] = k, v; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 req.write(t_concat(t)); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 default_headers[k] = nil; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 req.write("\r\n"); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 if body then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 req.write(body); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 |
720
8f22e9fb2291
net.http: Don't log content from server
Matthew Wild <mwild1@gmail.com>
parents:
719
diff
changeset
|
157 req.callback = function (content, code, request) log("debug", "Calling callback, status %s", code or "---"); return select(2, xpcall(function () return callback(content, code, request) end, handleerr)); end |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 req.reader = request_reader; |
678
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
159 req.state = "status"; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 listener.register_request(req.handler, req); |
619
6d720aba51cb
Oops, don't call server.loop() because we'll be running inside the server
Matthew Wild <mwild1@gmail.com>
parents:
618
diff
changeset
|
162 |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 return req; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 function destroy_request(request) |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 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
|
168 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
|
169 request.handler:close() |
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
|
170 listener.ondisconnect(request.handler, "closed"); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 _M.urlencode = urlencode; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 return _M; |