Software /
code /
prosody
Annotate
net/http.lua @ 1869:7456bb2a3458
mod_bosh: Update to use new httpserver.new_from_config syntax
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 03 Oct 2009 00:56:45 +0100 |
parent | 1522:569d58d21612 |
child | 2130:828e161cdfc7 |
child | 2777:a0ea72846b46 |
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 |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1331
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1331
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
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" |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local server = require "net.server" |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local connlisteners_get = require "net.connlisteners".get; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 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
|
18 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 local t_insert, t_concat = table.insert, table.concat; |
1331
4443309b5528
net.http: (Re-)add url[en|de]code functions
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
20 local tonumber, tostring, pairs, xpcall, select, debug_traceback, char, format = |
4443309b5528
net.http: (Re-)add url[en|de]code functions
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
21 tonumber, tostring, pairs, 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 local print = function () end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
1331
4443309b5528
net.http: (Re-)add url[en|de]code functions
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
26 module "http" |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
1331
4443309b5528
net.http: (Re-)add url[en|de]code functions
Matthew Wild <mwild1@gmail.com>
parents:
1112
diff
changeset
|
28 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
|
29 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
|
30 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 local function expectbody(reqt, code) |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 if reqt.method == "HEAD" then return nil end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 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
|
34 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
|
35 return 1 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 local function request_reader(request, data, startpos) |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 if not data then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 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
|
41 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
|
42 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
|
43 elseif request.state ~= "completed" then |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 -- 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
|
45 request.callback("connection-closed", 0, request); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 destroy_request(request); |
714
ab3c47f4fe1d
net.http: Fix for callbacks being triggered multiple times for the same request
Matthew Wild <mwild1@gmail.com>
parents:
678
diff
changeset
|
48 request.body = nil; |
ab3c47f4fe1d
net.http: Fix for callbacks being triggered multiple times for the same request
Matthew Wild <mwild1@gmail.com>
parents:
678
diff
changeset
|
49 request.state = "completed"; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 return; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 end |
714
ab3c47f4fe1d
net.http: Fix for callbacks being triggered multiple times for the same request
Matthew Wild <mwild1@gmail.com>
parents:
678
diff
changeset
|
52 if request.state == "body" and request.state ~= "completed" then |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 print("Reading body...") |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 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
|
55 if startpos then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 data = data:sub(startpos, -1) |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 t_insert(request.body, data); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 if request.bodylength then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 request.havebodylength = request.havebodylength + #data; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 if request.havebodylength >= request.bodylength then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 -- 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
|
63 log("debug", "Have full body, calling callback"); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 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
|
65 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
|
66 end |
677
93e5309c5430
Fix to prevent calling HTTP request callback twice with the same data
Matthew Wild <mwild1@gmail.com>
parents:
646
diff
changeset
|
67 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
|
68 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
|
69 else |
93e5309c5430
Fix to prevent calling HTTP request callback twice with the same data
Matthew Wild <mwild1@gmail.com>
parents:
646
diff
changeset
|
70 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
|
71 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 elseif request.state == "headers" then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 print("Reading headers...") |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 local pos = startpos; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 local headers = request.responseheaders or {}; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 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
|
78 startpos = startpos + #line + 2; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 local k, v = line:match("(%S+): (.+)"); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 if k and v then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 headers[k:lower()] = v; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 print("Header: "..k:lower().." = "..v); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 elseif #line == 0 then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 request.responseheaders = headers; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 break; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 else |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 print("Unhandled header line: "..line); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 -- Reached the end of the headers |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 request.state = "body"; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 if #data > startpos then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 return request_reader(request, data, startpos); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 elseif request.state == "status" then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 print("Reading status...") |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 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
|
98 code = tonumber(code); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 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
|
100 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
|
101 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 |
633
fe1e01a06729
Fix nil status code for http request callbacks
Matthew Wild <mwild1@gmail.com>
parents:
628
diff
changeset
|
103 request.code, request.responseversion = code, http; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 |
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
|
105 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
|
106 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
|
107 request.callback(nil, code, request); |
616
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 destroy_request(request); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 return; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 request.state = "headers"; |
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 if #data > linelen then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 return request_reader(request, data, linelen); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 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 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 |
678
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
121 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
|
122 function request(u, ex, callback) |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 local req = url.parse(u); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 |
903
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
125 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
|
126 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
|
127 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
|
128 end |
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
129 |
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
130 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
|
131 req.path = "/"; |
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
132 end |
6737d005a84a
net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents:
739
diff
changeset
|
133 |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 local custom_headers, body; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 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
|
136 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 if req.userinfo then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 default_headers["Authorization"] = "Basic "..mime.b64(req.userinfo); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 if ex then |
738
cf70342985df
net.http: custom_headers -> headers
Matthew Wild <mwild1@gmail.com>
parents:
720
diff
changeset
|
143 custom_headers = ex.headers; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 req.onlystatus = ex.onlystatus; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 body = ex.body; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 if body then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 req.method = "POST "; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 default_headers["Content-Length"] = tostring(#body); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 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
|
150 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 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
|
152 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 |
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
|
154 req.handler, req.conn = server.wrapclient(socket.tcp(), req.host, req.port or 80, listener, "*a"); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 req.write = req.handler.write; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 req.conn:settimeout(0); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 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
|
158 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
|
159 callback(nil, 0, req); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 return nil, err; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 |
719
b1eb112478b8
net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents:
714
diff
changeset
|
163 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
|
164 |
b1eb112478b8
net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents:
714
diff
changeset
|
165 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
|
166 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
|
167 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
|
168 end |
b1eb112478b8
net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents:
714
diff
changeset
|
169 |
b1eb112478b8
net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents:
714
diff
changeset
|
170 req.write(t_concat(request_line)); |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 local t = { [2] = ": ", [4] = "\r\n" }; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 if custom_headers then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 for k, v in pairs(custom_headers) do |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 t[1], t[3] = k, v; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 req.write(t_concat(t)); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 default_headers[k] = nil; |
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 for k, v in pairs(default_headers) do |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 t[1], t[3] = k, v; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 req.write(t_concat(t)); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 default_headers[k] = nil; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 req.write("\r\n"); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 if body then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 req.write(body); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 |
720
8f22e9fb2291
net.http: Don't log content from server
Matthew Wild <mwild1@gmail.com>
parents:
719
diff
changeset
|
191 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
|
192 req.reader = request_reader; |
678
1859edec2237
Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents:
677
diff
changeset
|
193 req.state = "status"; |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 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
|
196 |
616
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 return req; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 function destroy_request(request) |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 if request.conn then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 request.handler.close() |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 listener.disconnect(request.conn, "closed"); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
204 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 _M.urlencode = urlencode; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
208 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
209 return _M; |