net/httpserver_listener.lua
author Matthew Wild <mwild1@gmail.com>
Wed, 01 May 2013 13:54:31 +0100
branchsasl
changeset 5555 70a7ef4b6aaa
parent 1540 19fb86c19a59
child 2129 fcdcdf00787c
child 2923 b7049746bd29
permissions -rw-r--r--
Close 'sasl' branch
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
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 634
diff changeset
     2
-- Copyright (C) 2008-2009 Matthew Wild
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 634
diff changeset
     3
-- Copyright (C) 2008-2009 Waqas Hussain
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
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
function httpserver.listener(conn, data)
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
19fb86c19a59 net.httpserver: Mark a request as secure if it is made using HTTPS
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
    27
		if conn.ssl() then
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
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
	if data then
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
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
function httpserver.disconnect(conn, err)
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);