Annotate

net/httpclient_listener.lua @ 2421:83773aed9437

s2smanager: Stamp a 'to' on the reply stream:stream. This makes prosody happy.
author Paul Aurich <paul@darkrain42.org>
date Sun, 03 Jan 2010 11:26:36 -0800
parent 2129:fcdcdf00787c
child 2674:a1fdfd7318df
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
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1227
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1227
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
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");
616
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 connlisteners_register = require "net.connlisteners".register;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local requests = {}; -- Open requests
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local buffers = {}; -- Buffers of partial lines
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 httpclient = { default_port = 80, default_mode = "*a" };
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
2129
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
18 function httpclient.onincoming(conn, data)
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local request = requests[conn];
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if not request then
1227
6a587ca99109 httpclient_listener: Don't use print()...
Matthew Wild <mwild1@gmail.com>
parents: 737
diff changeset
22 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
23 return;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if data and request.reader then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 request:reader(data);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
2129
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
31 function httpclient.ondisconnect(conn, err)
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local request = requests[conn];
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if request then
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 request:reader(nil);
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end
737
ade262a8da7f net.http: Remove request from conn->request table when conn closed
Matthew Wild <mwild1@gmail.com>
parents: 616
diff changeset
36 requests[conn] = nil;
616
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
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 function httpclient.register_request(conn, req)
1227
6a587ca99109 httpclient_listener: Don't use print()...
Matthew Wild <mwild1@gmail.com>
parents: 737
diff changeset
40 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
41 requests[conn] = req;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 connlisteners_register("httpclient", httpclient);