Annotate

net/http.lua @ 3506:0f46acca11cc

MUC: Fixed traceback on presence errors lacking a condition.
author Waqas Hussain <waqas20@gmail.com>
date Mon, 27 Sep 2010 19:00:11 +0500
parent 3470:0e59b5cdd57b
child 3540:bc139431830b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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"
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;
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
20 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
21 local tonumber, tostring, xpcall, select, debug_traceback, char, format =
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 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
23
1859edec2237 Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents: 677
diff changeset
24 local log = require "util.logger".init("http");
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
1331
4443309b5528 net.http: (Re-)add url[en|de]code functions
Matthew Wild <mwild1@gmail.com>
parents: 1112
diff changeset
26 module "http"
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
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
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
31 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
32 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
33 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
34 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
35 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
36 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
37 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
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 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
41 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
42 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
43 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
44 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
45 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
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
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local function expectbody(reqt, code)
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 if reqt.method == "HEAD" then return nil end
2777
a0ea72846b46 net.http: Don't expect the body on redirects
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
50 if code == 204 or code == 304 or code == 301 then return nil end
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 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
52 return 1
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local function request_reader(request, data, startpos)
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 if not data then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 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
58 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
59 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
60 elseif request.state ~= "completed" then
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 -- 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
62 request.callback("connection-closed", 0, request);
2808
23bb7e29819e net.http: Don't re-destroy a request when the connection is closed
Matthew Wild <mwild1@gmail.com>
parents: 2807
diff changeset
63 return;
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 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
66 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
67 request.state = "completed";
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 return;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 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
70 if request.state == "body" and request.state ~= "completed" then
2675
ab643a77da2d net.http: Update print()s to log()s - don't ask how this came to be, I have no idea :)
Matthew Wild <mwild1@gmail.com>
parents: 2673
diff changeset
71 log("debug", "Reading body...")
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 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
73 if startpos then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 data = data:sub(startpos, -1)
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 t_insert(request.body, data);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 if request.bodylength then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 request.havebodylength = request.havebodylength + #data;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 if request.havebodylength >= request.bodylength then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 -- 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
81 log("debug", "Have full body, calling callback");
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 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
83 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
84 end
677
93e5309c5430 Fix to prevent calling HTTP request callback twice with the same data
Matthew Wild <mwild1@gmail.com>
parents: 646
diff changeset
85 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
86 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
87 else
2675
ab643a77da2d net.http: Update print()s to log()s - don't ask how this came to be, I have no idea :)
Matthew Wild <mwild1@gmail.com>
parents: 2673
diff changeset
88 log("debug", "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
89 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 elseif request.state == "headers" then
2675
ab643a77da2d net.http: Update print()s to log()s - don't ask how this came to be, I have no idea :)
Matthew Wild <mwild1@gmail.com>
parents: 2673
diff changeset
92 log("debug", "Reading headers...")
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 local pos = startpos;
2807
8e2dba8904a7 net.http: Port commit 2f235c57d713 to net.http to fix headers in responses (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents: 2777
diff changeset
94 local headers, headers_complete = request.responseheaders;
8e2dba8904a7 net.http: Port commit 2f235c57d713 to net.http to fix headers in responses (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents: 2777
diff changeset
95 if not headers then
8e2dba8904a7 net.http: Port commit 2f235c57d713 to net.http to fix headers in responses (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents: 2777
diff changeset
96 headers = {};
8e2dba8904a7 net.http: Port commit 2f235c57d713 to net.http to fix headers in responses (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents: 2777
diff changeset
97 request.responseheaders = headers;
8e2dba8904a7 net.http: Port commit 2f235c57d713 to net.http to fix headers in responses (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents: 2777
diff changeset
98 end
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 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
100 startpos = startpos + #line + 2;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 local k, v = line:match("(%S+): (.+)");
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 if k and v then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 headers[k:lower()] = v;
2675
ab643a77da2d net.http: Update print()s to log()s - don't ask how this came to be, I have no idea :)
Matthew Wild <mwild1@gmail.com>
parents: 2673
diff changeset
104 --log("debug", "Header: "..k:lower().." = "..v);
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 elseif #line == 0 then
2807
8e2dba8904a7 net.http: Port commit 2f235c57d713 to net.http to fix headers in responses (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents: 2777
diff changeset
106 headers_complete = true;
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 break;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 else
2675
ab643a77da2d net.http: Update print()s to log()s - don't ask how this came to be, I have no idea :)
Matthew Wild <mwild1@gmail.com>
parents: 2673
diff changeset
109 log("warn", "Unhandled header line: "..line);
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 end
2807
8e2dba8904a7 net.http: Port commit 2f235c57d713 to net.http to fix headers in responses (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents: 2777
diff changeset
112 if not headers_complete then return; end
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 -- Reached the end of the headers
2807
8e2dba8904a7 net.http: Port commit 2f235c57d713 to net.http to fix headers in responses (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents: 2777
diff changeset
114 if not expectbody(request, request.code) then
8e2dba8904a7 net.http: Port commit 2f235c57d713 to net.http to fix headers in responses (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents: 2777
diff changeset
115 request.callback(nil, request.code, request);
8e2dba8904a7 net.http: Port commit 2f235c57d713 to net.http to fix headers in responses (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents: 2777
diff changeset
116 return;
8e2dba8904a7 net.http: Port commit 2f235c57d713 to net.http to fix headers in responses (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents: 2777
diff changeset
117 end
8e2dba8904a7 net.http: Port commit 2f235c57d713 to net.http to fix headers in responses (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents: 2777
diff changeset
118 request.state = "body";
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 if #data > startpos then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 return request_reader(request, data, startpos);
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 elseif request.state == "status" then
2675
ab643a77da2d net.http: Update print()s to log()s - don't ask how this came to be, I have no idea :)
Matthew Wild <mwild1@gmail.com>
parents: 2673
diff changeset
123 log("debug", "Reading status...")
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 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
125 code = tonumber(code);
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 if not code then
2809
1a7b5b775275 net.http: Close connection when invalid status line is received from the server
Matthew Wild <mwild1@gmail.com>
parents: 2808
diff changeset
127 log("warn", "Invalid HTTP status line, telling callback then closing");
1a7b5b775275 net.http: Close connection when invalid status line is received from the server
Matthew Wild <mwild1@gmail.com>
parents: 2808
diff changeset
128 local ret = request.callback("invalid-status-line", 0, request);
1a7b5b775275 net.http: Close connection when invalid status line is received from the server
Matthew Wild <mwild1@gmail.com>
parents: 2808
diff changeset
129 destroy_request(request);
1a7b5b775275 net.http: Close connection when invalid status line is received from the server
Matthew Wild <mwild1@gmail.com>
parents: 2808
diff changeset
130 return ret;
616
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
633
fe1e01a06729 Fix nil status code for http request callbacks
Matthew Wild <mwild1@gmail.com>
parents: 628
diff changeset
133 request.code, request.responseversion = code, http;
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134
2807
8e2dba8904a7 net.http: Port commit 2f235c57d713 to net.http to fix headers in responses (thanks dersd)
Matthew Wild <mwild1@gmail.com>
parents: 2777
diff changeset
135 if request.onlystatus then
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 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
137 request.callback(nil, code, request);
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 destroy_request(request);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 return;
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 request.state = "headers";
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 if #data > linelen then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 return request_reader(request, data, linelen);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150
678
1859edec2237 Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents: 677
diff changeset
151 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
152 function request(u, ex, callback)
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 local req = url.parse(u);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154
903
6737d005a84a net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
155 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
156 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
157 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
158 end
6737d005a84a net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
159
6737d005a84a net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
160 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
161 req.path = "/";
6737d005a84a net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
162 end
6737d005a84a net.http: Don't throw error on invalid URLs. Fixes #56.
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
163
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 local custom_headers, body;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 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
166
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 if req.userinfo then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 default_headers["Authorization"] = "Basic "..mime.b64(req.userinfo);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 if ex then
738
cf70342985df net.http: custom_headers -> headers
Matthew Wild <mwild1@gmail.com>
parents: 720
diff changeset
173 custom_headers = ex.headers;
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 req.onlystatus = ex.onlystatus;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 body = ex.body;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 if body then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 req.method = "POST ";
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 default_headers["Content-Length"] = tostring(#body);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 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
180 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 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
182 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183
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
184 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
185 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
186 req.conn:settimeout(0);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 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
188 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
189 callback(nil, 0, req);
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 return nil, err;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192
719
b1eb112478b8 net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents: 714
diff changeset
193 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
194
b1eb112478b8 net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents: 714
diff changeset
195 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
196 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
197 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
198 end
b1eb112478b8 net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents: 714
diff changeset
199
b1eb112478b8 net.http: Fix to send query part of URL to server
Matthew Wild <mwild1@gmail.com>
parents: 714
diff changeset
200 req.write(t_concat(request_line));
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 local t = { [2] = ": ", [4] = "\r\n" };
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 if custom_headers then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 for k, v in pairs(custom_headers) do
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 t[1], t[3] = k, v;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 req.write(t_concat(t));
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 default_headers[k] = nil;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 for k, v in pairs(default_headers) do
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 t[1], t[3] = k, v;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 req.write(t_concat(t));
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 default_headers[k] = nil;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 req.write("\r\n");
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 if body then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 req.write(body);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220
720
8f22e9fb2291 net.http: Don't log content from server
Matthew Wild <mwild1@gmail.com>
parents: 719
diff changeset
221 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
222 req.reader = request_reader;
678
1859edec2237 Protected call for HTTP request callbacks, to catch errors
Matthew Wild <mwild1@gmail.com>
parents: 677
diff changeset
223 req.state = "status";
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 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
226
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 return req;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 function destroy_request(request)
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 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
232 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
233 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
234 listener.ondisconnect(request.handler, "closed");
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 _M.urlencode = urlencode;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 return _M;