net/httpclient_listener.lua

Wed, 13 Jan 2010 00:04:38 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 13 Jan 2010 00:04:38 +0000
changeset 2796
1e287badd033
parent 1522
569d58d21612
child 2126
fcdcdf00787c
child 2812
496a80322a51
permissions
-rw-r--r--

prosodyctl: Use mode r+ for opening the file so 1) it fails if the file doesn't exist 2) we have write access to lock it

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
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 function httpclient.listener(conn, data)
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
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 function httpclient.disconnect(conn, err)
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);

mercurial