net/httpclient_listener.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 03 Jun 2011 05:34:22 +0100
changeset 4297 3421dfaa8188
parent 2925 692b3c6c5bd2
child 4352 912a49b1c4e3
permissions -rw-r--r--
mod_storage_sql: Log error when failing to update MySQL schema
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1227
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2812
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2812
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
1522
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
2129
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
    18
function httpclient.onincoming(conn, data)
616
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
2129
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
    31
function httpclient.ondisconnect(conn, err)
616
69bc5782b25e Non-blocking HTTP requests (adding net.http)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
	local request = requests[conn];
2812
496a80322a51 net.httpclient_listener: Don't notify request of closed connection if the close was initiated by us
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
    33
	if request and err ~= "closed" then
616
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);