libs/httpclient_listener.lua

Sat, 02 Jan 2010 05:46:52 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 02 Jan 2010 05:46:52 +0000
changeset 0
6e60da4625db
permissions
-rw-r--r--

Initial commit

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

mercurial