Sat, 02 Jan 2010 05:46:52 +0000
Initial commit
0 | 1 | -- Prosody IM |
2 | -- Copyright (C) 2008-2009 Matthew Wild | |
3 | -- Copyright (C) 2008-2009 Waqas Hussain | |
4 | -- | |
5 | -- This project is MIT/X11 licensed. Please see the | |
6 | -- COPYING file in the source package for more information. | |
7 | -- | |
8 | ||
9 | local log = require "util.logger".init("httpclient_listener"); | |
10 | ||
11 | local connlisteners_register = require "net.connlisteners".register; | |
12 | ||
13 | local requests = {}; -- Open requests | |
14 | local buffers = {}; -- Buffers of partial lines | |
15 | ||
16 | local httpclient = { default_port = 80, default_mode = "*a" }; | |
17 | ||
18 | function httpclient.onincoming(conn, data) | |
19 | local request = requests[conn]; | |
20 | ||
21 | if not request then | |
22 | log("warn", "Received response from connection %s with no request attached!", tostring(conn)); | |
23 | return; | |
24 | end | |
25 | ||
26 | if data and request.reader then | |
27 | request:reader(data); | |
28 | end | |
29 | end | |
30 | ||
31 | function httpclient.ondisconnect(conn, err) | |
32 | local request = requests[conn]; | |
33 | if request then | |
34 | request:reader(nil); | |
35 | end | |
36 | requests[conn] = nil; | |
37 | end | |
38 | ||
39 | function httpclient.register_request(conn, req) | |
40 | log("debug", "Attaching request %s to connection %s", tostring(req.id or req), tostring(conn)); | |
41 | requests[conn] = req; | |
42 | end | |
43 | ||
44 | connlisteners_register("httpclient", httpclient); |