Annotate

net/httpclient_listener.lua @ 4352:912a49b1c4e3

net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
author Matthew Wild <mwild1@gmail.com>
date Sat, 20 Aug 2011 15:10:04 -0400
parent 2925:692b3c6c5bd2
child 4354:502876d94363
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: 1227
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2812
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2812
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: 1227
diff changeset
4 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1227
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: 1227
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: 1227
diff changeset
7 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1227
diff changeset
8
1227
6a587ca99109 httpclient_listener: Don't use print()...
Matthew Wild <mwild1@gmail.com>
parents: 737
diff changeset
9 local log = require "util.logger".init("httpclient_listener");
4352
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
10 local t_concat = table.concat;
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local connlisteners_register = require "net.connlisteners".register;
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 requests = {}; -- Open requests
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local buffers = {}; -- Buffers of partial lines
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local httpclient = { default_port = 80, default_mode = "*a" };
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
4352
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
19 function httpclient.onconnect(conn)
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
20 local req = requests[conn];
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
21 -- Send the request
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
22 local request_line = { req.method or "GET", " ", req.path, " HTTP/1.1\r\n" };
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
23 if req.query then
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
24 t_insert(request_line, 4, "?"..req.query);
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
25 end
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
26
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
27 conn:write(t_concat(request_line));
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
28 local t = { [2] = ": ", [4] = "\r\n" };
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
29 for k, v in pairs(req.headers) do
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
30 t[1], t[3] = k, v;
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
31 conn:write(t_concat(t));
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
32 end
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
33 conn:write("\r\n");
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
34
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
35 if body then
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
36 conn:write(body);
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
37 end
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
38 end
912a49b1c4e3 net.http, httpclient_listener: Move request sending from net.http to onconnect() handler, and add support for HTTPS requests to net.http
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
39
2129
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
40 function httpclient.onincoming(conn, data)
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local request = requests[conn];
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 if not request then
1227
6a587ca99109 httpclient_listener: Don't use print()...
Matthew Wild <mwild1@gmail.com>
parents: 737
diff changeset
44 log("warn", "Received response from connection %s with no request attached!", tostring(conn));
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 return;
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
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 if data and request.reader then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 request:reader(data);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
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
2129
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
53 function httpclient.ondisconnect(conn, err)
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local request = requests[conn];
2812
496a80322a51 net.httpclient_listener: Don't notify request of closed connection if the close was initiated by us
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
55 if request and err ~= "closed" then
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 request:reader(nil);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
737
ade262a8da7f net.http: Remove request from conn->request table when conn closed
Matthew Wild <mwild1@gmail.com>
parents: 616
diff changeset
58 requests[conn] = nil;
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 function httpclient.register_request(conn, req)
1227
6a587ca99109 httpclient_listener: Don't use print()...
Matthew Wild <mwild1@gmail.com>
parents: 737
diff changeset
62 log("debug", "Attaching request %s to connection %s", tostring(req.id or req), tostring(conn));
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 requests[conn] = req;
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
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 connlisteners_register("httpclient", httpclient);