net/httpserver_listener.lua
author Matthew Wild <mwild1@gmail.com>
Tue, 05 Apr 2011 14:34:48 +0100
changeset 4241 49b9e73e31ef
parent 3047 820ae39e06de
permissions -rw-r--r--
tools/migration/migrator/prosody_files.lua: Fix for previous commit
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: 634
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1540
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1540
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: 634
diff changeset
     4
-- 
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 634
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: 634
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: 634
diff changeset
     7
--
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 634
diff changeset
     8
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local connlisteners_register = require "net.connlisteners".register;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
local new_request = require "net.httpserver".new_request;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
local request_reader = require "net.httpserver".request_reader;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
local requests = {}; -- Open requests
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
local httpserver = { default_port = 80, default_mode = "*a" };
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
2129
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 1540
diff changeset
    19
function httpserver.onincoming(conn, data)
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
	local request = requests[conn];
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
	if not request then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
		request = new_request(conn);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
		requests[conn] = request;
1540
19fb86c19a59 net.httpserver: Mark a request as secure if it is made using HTTPS
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
    25
		
19fb86c19a59 net.httpserver: Mark a request as secure if it is made using HTTPS
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
    26
		-- If using HTTPS, request is secure
2372
bb88b76c21d0 httpserver_listener: Update for new connection API
Matthew Wild <mwild1@gmail.com>
parents: 2129
diff changeset
    27
		if conn:ssl() then
1540
19fb86c19a59 net.httpserver: Mark a request as secure if it is made using HTTPS
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
    28
			request.secure = true;
19fb86c19a59 net.httpserver: Mark a request as secure if it is made using HTTPS
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
    29
		end
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
	end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
3046
92173d8a35d3 net/httpserver_listener: Ignore empty strings on incoming data.
Brian Cully <bjc@kublai.com>
parents: 2923
diff changeset
    32
	if data and data ~= "" then
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
		request_reader(request, data);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
	end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
2129
fcdcdf00787c *_listener: Update for new net.server API, specifically .listener -> .onincoming, .disconnect -> .ondisconnect
Matthew Wild <mwild1@gmail.com>
parents: 1540
diff changeset
    37
function httpserver.ondisconnect(conn, err)
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
	local request = requests[conn];
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
	if request and not request.destroyed then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
		request.conn = nil;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
		request_reader(request, nil);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
	end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
	requests[conn] = nil;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
connlisteners_register("httpserver", httpserver);