Annotate

net/http.lua @ 628:3712d36b6d25

Fixed URL encoding to generate %0x instead of %x
author Waqas Hussain <waqas20@gmail.com>
date Tue, 16 Dec 2008 03:46:17 +0500
parent 619:6d720aba51cb
child 633:fe1e01a06729
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local tonumber, tostring, pairs = tonumber, tostring, pairs;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local print = function () end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
628
3712d36b6d25 Fixed URL encoding to generate %0x instead of %x
Waqas Hussain <waqas20@gmail.com>
parents: 619
diff changeset
16 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
17
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 module "http"
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local function expectbody(reqt, code)
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if reqt.method == "HEAD" then return nil end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 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
23 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
24 return 1
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local function request_reader(request, data, startpos)
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 if not data then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 if request.body then
618
69de678792d6 Pass HTTP request object to callback
Matthew Wild <mwild1@gmail.com>
parents: 617
diff changeset
30 request.callback(request.code, t_concat(request.body), request);
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 else
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 -- Error.. connection was closed prematurely
618
69de678792d6 Pass HTTP request object to callback
Matthew Wild <mwild1@gmail.com>
parents: 617
diff changeset
33 request.callback(0, "connection-closed", request);
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 destroy_request(request);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 return;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 if request.state == "body" then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 print("Reading body...")
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 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
41 if startpos then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 data = data:sub(startpos, -1)
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 t_insert(request.body, data);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 if request.bodylength then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 request.havebodylength = request.havebodylength + #data;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if request.havebodylength >= request.bodylength then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 -- We have the body
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 if request.callback then
618
69de678792d6 Pass HTTP request object to callback
Matthew Wild <mwild1@gmail.com>
parents: 617
diff changeset
50 request.callback(request.code, t_concat(request.body), request);
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 print("", "Have "..request.havebodylength.." bytes out of "..request.bodylength);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 elseif request.state == "headers" then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 print("Reading headers...")
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 local pos = startpos;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 local headers = request.responseheaders or {};
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 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
60 startpos = startpos + #line + 2;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local k, v = line:match("(%S+): (.+)");
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 if k and v then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 headers[k:lower()] = v;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 print("Header: "..k:lower().." = "..v);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 elseif #line == 0 then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 request.responseheaders = headers;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 break;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 else
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 print("Unhandled header line: "..line);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
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 -- Reached the end of the headers
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 request.state = "body";
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 if #data > startpos then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 return request_reader(request, data, startpos);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 elseif request.state == "status" then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 print("Reading status...")
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 local http, code, text, linelen = data:match("^HTTP/(%S+) (%d+) (.-)\r\n()", startpos);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 if not code then
618
69de678792d6 Pass HTTP request object to callback
Matthew Wild <mwild1@gmail.com>
parents: 617
diff changeset
81 return request.callback(0, "invalid-status-line", request);
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 request.responsecode, request.responseversion = code, http;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 if request.onlystatus or not expectbody(request, tonumber(code)) then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 if request.callback then
618
69de678792d6 Pass HTTP request object to callback
Matthew Wild <mwild1@gmail.com>
parents: 617
diff changeset
88 request.callback(code, nil, request);
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 destroy_request(request);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 return;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 request.state = "headers";
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 if #data > linelen then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 return request_reader(request, data, linelen);
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 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 function request(u, callback, ex)
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 local req = url.parse(u);
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 local custom_headers, body;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 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
107
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 if req.userinfo then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 default_headers["Authorization"] = "Basic "..mime.b64(req.userinfo);
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 if ex then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 custom_headers = ex.custom_headers;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 req.onlystatus = ex.onlystatus;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 body = ex.body;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 if body then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 req.method = "POST ";
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 default_headers["Content-Length"] = tostring(#body);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 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
121 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 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
123 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 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
126 req.write = req.handler.write;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 req.conn:settimeout(0);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 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
129 if not ok and err ~= "timeout" then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 return nil, err;
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
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 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
134 local t = { [2] = ": ", [4] = "\r\n" };
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 if custom_headers then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 for k, v in pairs(custom_headers) do
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 t[1], t[3] = k, v;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 req.write(t_concat(t));
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 default_headers[k] = nil;
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 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 for k, v in pairs(default_headers) do
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 t[1], t[3] = k, v;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 req.write(t_concat(t));
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 default_headers[k] = nil;
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 req.write("\r\n");
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 if body then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 req.write(body);
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
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 req.callback = callback;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 req.reader = request_reader;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 req.state = "status"
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 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
159
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 return req;
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
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 function destroy_request(request)
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 if request.conn then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 request.handler.close()
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 listener.disconnect(request.conn, "closed");
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 _M.urlencode = urlencode;
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 return _M;