Tue, 13 Jan 2009 22:37:07 +0500
modulemanager: Added reload support, with callbacks for saving and restoring state
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | local socket = require "socket" |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | local mime = require "mime" |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | local url = require "socket.url" |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 | local server = require "net.server" |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | local connlisteners_get = require "net.connlisteners".get; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | 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
|
10 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | local t_insert, t_concat = table.insert, table.concat; |
678
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
12 | local tonumber, tostring, pairs, xpcall, select, debug_traceback = |
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
13 | tonumber, tostring, pairs, xpcall, select, debug.traceback; |
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
14 | |
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
15 | local log = require "util.logger".init("http"); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | local print = function () end |
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 urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end }); |
627
3712d36b6d25
Fixed URL encoding to generate %0x instead of %x
Waqas Hussain <waqas20@gmail.com>
parents:
619
diff
changeset
|
19 | local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | module "http" |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | local function expectbody(reqt, code) |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | if reqt.method == "HEAD" then return nil end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | if code == 204 or code == 304 then return nil end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 | if code >= 100 and code < 200 then return nil end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | return 1 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | local function request_reader(request, data, startpos) |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | if not data then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 | if request.body then |
677
93e5309c5430
Fix to prevent calling HTTP request callback twice with the same data
Matthew Wild <mwild1@gmail.com>
parents:
646
diff
changeset
|
33 | log("debug", "Connection closed, but we have data, calling callback..."); |
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
|
34 | request.callback(t_concat(request.body), request.code, request); |
677
93e5309c5430
Fix to prevent calling HTTP request callback twice with the same data
Matthew Wild <mwild1@gmail.com>
parents:
646
diff
changeset
|
35 | elseif request.state ~= "completed" then |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 | -- Error.. connection was closed prematurely |
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
|
37 | request.callback("connection-closed", 0, request); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 | destroy_request(request); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | return; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 | if request.state == "body" then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | print("Reading body...") |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 | if not request.body then request.body = {}; request.havebodylength, request.bodylength = 0, tonumber(request.responseheaders["content-length"]); end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | if startpos then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | data = data:sub(startpos, -1) |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 | t_insert(request.body, data); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 | if request.bodylength then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 | request.havebodylength = request.havebodylength + #data; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 | if request.havebodylength >= request.bodylength then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 | -- We have the body |
677
93e5309c5430
Fix to prevent calling HTTP request callback twice with the same data
Matthew Wild <mwild1@gmail.com>
parents:
646
diff
changeset
|
53 | log("debug", "Have full body, calling callback"); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 | if request.callback then |
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
|
55 | request.callback(t_concat(request.body), request.code, request); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 | end |
677
93e5309c5430
Fix to prevent calling HTTP request callback twice with the same data
Matthew Wild <mwild1@gmail.com>
parents:
646
diff
changeset
|
57 | request.body = nil; |
93e5309c5430
Fix to prevent calling HTTP request callback twice with the same data
Matthew Wild <mwild1@gmail.com>
parents:
646
diff
changeset
|
58 | request.state = "completed"; |
93e5309c5430
Fix to prevent calling HTTP request callback twice with the same data
Matthew Wild <mwild1@gmail.com>
parents:
646
diff
changeset
|
59 | else |
93e5309c5430
Fix to prevent calling HTTP request callback twice with the same data
Matthew Wild <mwild1@gmail.com>
parents:
646
diff
changeset
|
60 | print("", "Have "..request.havebodylength.." bytes out of "..request.bodylength); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 | elseif request.state == "headers" then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 | print("Reading headers...") |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 | local pos = startpos; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 | local headers = request.responseheaders or {}; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 | for line in data:sub(startpos, -1):gmatch("(.-)\r\n") do |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 | startpos = startpos + #line + 2; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 | local k, v = line:match("(%S+): (.+)"); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 | if k and v then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 | headers[k:lower()] = v; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 | print("Header: "..k:lower().." = "..v); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 | elseif #line == 0 then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 | request.responseheaders = headers; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 | break; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 | else |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 | print("Unhandled header line: "..line); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 | -- Reached the end of the headers |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 | request.state = "body"; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 | if #data > startpos then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 | return request_reader(request, data, startpos); |
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 | elseif request.state == "status" then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 | print("Reading status...") |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 | local http, code, text, linelen = data:match("^HTTP/(%S+) (%d+) (.-)\r\n()", startpos); |
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 | code = tonumber(code); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 | if not code then |
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
|
90 | return request.callback("invalid-status-line", 0, request); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 | |
633
fe1e01a06729
Fix nil status code for http request callbacks
Matthew Wild <mwild1@gmail.com>
parents:
627
diff
changeset
|
93 | request.code, request.responseversion = code, http; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 | |
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
|
95 | if request.onlystatus or not expectbody(request, code) then |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 | if request.callback then |
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
|
97 | request.callback(nil, code, request); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 | destroy_request(request); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 | return; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 | end |
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 | request.state = "headers"; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 | if #data > linelen then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 | return request_reader(request, data, linelen); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 | |
678
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
111 | 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
|
112 | function request(u, ex, callback) |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 | local req = url.parse(u); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 | local custom_headers, body; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 | 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
|
117 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 | if req.userinfo then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 | default_headers["Authorization"] = "Basic "..mime.b64(req.userinfo); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 | if ex then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 | custom_headers = ex.custom_headers; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 | req.onlystatus = ex.onlystatus; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 | body = ex.body; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 | if body then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 | req.method = "POST "; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 | default_headers["Content-Length"] = tostring(#body); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 | 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
|
131 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 | 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
|
133 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 | req.handler, req.conn = server.wraptcpclient(listener, socket.tcp(), req.host, req.port or 80, 0, "*a"); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 | req.write = req.handler.write; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 | req.conn:settimeout(0); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 | 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
|
139 | if not ok and err ~= "timeout" then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 | return nil, err; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 | req.write((req.method or "GET ")..req.path.." HTTP/1.0\r\n"); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 | local t = { [2] = ": ", [4] = "\r\n" }; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 | if custom_headers then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 | for k, v in pairs(custom_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 | end |
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 | for k, v in pairs(default_headers) do |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 | t[1], t[3] = k, v; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 | req.write(t_concat(t)); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 | default_headers[k] = nil; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 | req.write("\r\n"); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 | if body then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 | req.write(body); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 | |
678
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
164 | req.callback = function (content, code, request) log("debug", "Calling callback, code %s content: %s", code or "---", content 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
|
165 | req.reader = request_reader; |
678
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
166 | req.state = "status"; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 | 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
|
169 | |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 | return req; |
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 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 | function destroy_request(request) |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 | if request.conn then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 | request.handler.close() |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 | listener.disconnect(request.conn, "closed"); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 | end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 | _M.urlencode = urlencode; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 | |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 | return _M; |