Annotate

net/httpclient_listener.lua @ 1015:9e31e9397cff

util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
author Matthew Wild <mwild1@gmail.com>
date Tue, 21 Apr 2009 03:18:13 +0100
parent 737:ade262a8da7f
child 1227:6a587ca99109
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local connlisteners_register = require "net.connlisteners".register;
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
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
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 print("NO REQUEST!! for "..tostring(conn));
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)
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 print("Registering a request for "..tostring(conn));
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);