Software /
code /
prosody
Annotate
net/httpclient_listener.lua @ 1503:5970e06d9335
mod_console: Add quit and exit as aliases for 'bye' command
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 09 Jul 2009 04:37:08 +0100 |
parent | 1227:6a587ca99109 |
child | 1522:569d58d21612 |
rev | line source |
---|---|
1227
6a587ca99109
httpclient_listener: Don't use print()...
Matthew Wild <mwild1@gmail.com>
parents:
737
diff
changeset
|
1 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
|
2 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local connlisteners_register = require "net.connlisteners".register; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local requests = {}; -- Open requests |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local buffers = {}; -- Buffers of partial lines |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local httpclient = { default_port = 80, default_mode = "*a" }; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 function httpclient.listener(conn, data) |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local request = requests[conn]; |
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 if not request then |
1227
6a587ca99109
httpclient_listener: Don't use print()...
Matthew Wild <mwild1@gmail.com>
parents:
737
diff
changeset
|
14 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
|
15 return; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 end |
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 if data and request.reader then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 request:reader(data); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 function httpclient.disconnect(conn, err) |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 local request = requests[conn]; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 if request then |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 request:reader(nil); |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 end |
737
ade262a8da7f
net.http: Remove request from conn->request table when conn closed
Matthew Wild <mwild1@gmail.com>
parents:
616
diff
changeset
|
28 requests[conn] = nil; |
616
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.register_request(conn, req) |
1227
6a587ca99109
httpclient_listener: Don't use print()...
Matthew Wild <mwild1@gmail.com>
parents:
737
diff
changeset
|
32 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
|
33 requests[conn] = req; |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 end |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 |
69bc5782b25e
Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 connlisteners_register("httpclient", httpclient); |