net/httpserver.lua
author Matthew Wild <mwild1@gmail.com>
Thu, 17 Dec 2009 20:10:11 +0000
changeset 2371 7ddb8ce0cd0b
parent 2360 f1832bf27f84
child 2399 0325f241a26c
permissions -rw-r--r--
net.httpserver: Fix usage of old connection API syntax
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: 1435
diff changeset
     1
-- Prosody IM
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1435
diff changeset
     2
-- Copyright (C) 2008-2009 Matthew Wild
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1435
diff changeset
     3
-- Copyright (C) 2008-2009 Waqas Hussain
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1435
diff changeset
     4
-- 
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1435
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: 1435
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: 1435
diff changeset
     7
--
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1435
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
local socket = require "socket"
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local server = require "net.server"
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
local url_parse = require "socket.url".parse;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
local connlisteners_start = require "net.connlisteners".start;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
local connlisteners_get = require "net.connlisteners".get;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
local listener;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
local t_insert, t_concat = table.insert, table.concat;
1111
a1cf1d623695 net.http, net.httpserver: Remove urlcodes table... it really isn't needed (thanks Jan Harkes)
Matthew Wild <mwild1@gmail.com>
parents: 1110
diff changeset
    19
local s_match, s_gmatch = string.match, string.gmatch;
1536
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
    20
local tonumber, tostring, pairs, ipairs, type = tonumber, tostring, pairs, ipairs, type;
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
1113
89ac8e9e1426 net.httpserver: Fix for urlencoding to always produce 2 digits
Matthew Wild <mwild1@gmail.com>
parents: 1111
diff changeset
    22
local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
local log = require "util.logger".init("httpserver");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
local http_servers = {};
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
module "httpserver"
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
local default_handler;
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
local function expectbody(reqt)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
    return reqt.method == "POST";
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
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
local function send_response(request, response)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
	-- Write status line
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
	local resp;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
	if response.body then
1632
4a70cbb0fad7 net.httpserver: Allow response.body to be a non-string
Matthew Wild <mwild1@gmail.com>
parents: 1608
diff changeset
    40
		local body = tostring(response.body);
1052
a3429542631d net.httpserver: Don't log the response body (can be binary data...)
Matthew Wild <mwild1@gmail.com>
parents: 958
diff changeset
    41
		log("debug", "Sending response to %s", request.id);
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
		resp = { "HTTP/1.0 ", response.status or "200 OK", "\r\n"};
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
		local h = response.headers;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
		if h then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
			for k, v in pairs(h) do
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
				t_insert(resp, k);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
				t_insert(resp, ": ");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
				t_insert(resp, v);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
				t_insert(resp, "\r\n");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
			end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
		end
1632
4a70cbb0fad7 net.httpserver: Allow response.body to be a non-string
Matthew Wild <mwild1@gmail.com>
parents: 1608
diff changeset
    52
		if not (h and h["Content-Length"]) then
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
			t_insert(resp, "Content-Length: ");
1632
4a70cbb0fad7 net.httpserver: Allow response.body to be a non-string
Matthew Wild <mwild1@gmail.com>
parents: 1608
diff changeset
    54
			t_insert(resp, #body);
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
			t_insert(resp, "\r\n");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
		end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
		t_insert(resp, "\r\n");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
		
1632
4a70cbb0fad7 net.httpserver: Allow response.body to be a non-string
Matthew Wild <mwild1@gmail.com>
parents: 1608
diff changeset
    59
		if request.method ~= "HEAD" then
4a70cbb0fad7 net.httpserver: Allow response.body to be a non-string
Matthew Wild <mwild1@gmail.com>
parents: 1608
diff changeset
    60
			t_insert(resp, body);
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
		end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
	else
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
		-- Response we have is just a string (the body)
1661
33b1aee4b77f net.httpserver: Don't log response bodies!
Matthew Wild <mwild1@gmail.com>
parents: 1632
diff changeset
    64
		log("debug", "Sending 200 response to %s", request.id or "<none>");
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
		
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
		resp = { "HTTP/1.0 200 OK\r\n" };
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
		t_insert(resp, "Connection: close\r\n");
2360
f1832bf27f84 net.httpserver: Removed mime handling (now in mod_httpserver). Unknown dynamic content is now always served as text/html.
Waqas Hussain <waqas20@gmail.com>
parents: 2321
diff changeset
    68
		t_insert(resp, "Content-Type: text/html\r\n");
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
		t_insert(resp, "Content-Length: ");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
		t_insert(resp, #response);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
		t_insert(resp, "\r\n\r\n");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
		
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
		t_insert(resp, response);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
	end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
	request.write(t_concat(resp));
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
	if not request.stayopen then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
		request:destroy();
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
	end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
local function call_callback(request, err)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
	if request.handled then return; end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
	request.handled = true;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
	local callback = request.callback;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
	if not callback and request.path then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
		local path = request.url.path;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
		local base = path:match("^/([^/?]+)");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
		if not base then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
			base = path:match("^http://[^/?]+/([^/?]+)");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
		end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
		
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
		callback = (request.server and request.server.handlers[base]) or default_handler;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
	end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
	if callback then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
		if err then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
			log("debug", "Request error: "..err);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
			if not callback(nil, err, request) then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
				destroy_request(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
			end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
			return;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
		end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
		
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
		local response = callback(request.method, request.body and t_concat(request.body), request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
		if response then
958
172fb9a73017 net.httpserver: Don't log that a request has been left open if it is destroyed
Matthew Wild <mwild1@gmail.com>
parents: 697
diff changeset
   105
			if response == true and not request.destroyed then
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
				-- Keep connection open, we will reply later
1608
fb53fe17af36 net.httpserver: Reduce log level of 'request left open' message
Matthew Wild <mwild1@gmail.com>
parents: 1550
diff changeset
   107
				log("debug", "Request %s left open, on_destroy is %s", request.id, tostring(request.on_destroy));
1053
c04b40a0740b net.httpserver: Fix traceback when sending response to a destroyed request
Matthew Wild <mwild1@gmail.com>
parents: 1052
diff changeset
   108
			elseif response ~= true then
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
				-- Assume response
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
				send_response(request, response);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
				destroy_request(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   112
			end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   113
		else
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   114
			log("debug", "Request handler provided no response, destroying request...");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   115
			-- No response, close connection
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   116
			destroy_request(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
		end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   118
	end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   119
end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
local function request_reader(request, data, startpos)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
	if not data then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
		if request.body then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
			call_callback(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
		else
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
			-- Error.. connection was closed prematurely
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
			call_callback(request, "connection-closed");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   128
		end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   129
		-- Here we force a destroy... the connection is gone, so we can't reply later
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
		destroy_request(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
		return;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
	end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
	if request.state == "body" then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   134
		log("debug", "Reading body...")
697
8ddc85fa7602 core.httpserver: Rename request.responseheaders to the more logical request.headers
Matthew Wild <mwild1@gmail.com>
parents: 634
diff changeset
   135
		if not request.body then request.body = {}; request.havebodylength, request.bodylength = 0, tonumber(request.headers["content-length"]); end
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   136
		if startpos then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   137
			data = data:sub(startpos, -1)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   138
		end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   139
		t_insert(request.body, data);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   140
		if request.bodylength then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
			request.havebodylength = request.havebodylength + #data;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
			if request.havebodylength >= request.bodylength then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
				-- We have the body
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   144
				call_callback(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
			end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
		end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   147
	elseif request.state == "headers" then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
		log("debug", "Reading headers...")
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
		local pos = startpos;
697
8ddc85fa7602 core.httpserver: Rename request.responseheaders to the more logical request.headers
Matthew Wild <mwild1@gmail.com>
parents: 634
diff changeset
   150
		local headers = request.headers or {};
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   151
		for line in data:gmatch("(.-)\r\n") do
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   152
			startpos = (startpos or 1) + #line + 2;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   153
			local k, v = line:match("(%S+): (.+)");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
			if k and v then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
				headers[k:lower()] = v;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   156
--				log("debug", "Header: "..k:lower().." = "..v);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   157
			elseif #line == 0 then
697
8ddc85fa7602 core.httpserver: Rename request.responseheaders to the more logical request.headers
Matthew Wild <mwild1@gmail.com>
parents: 634
diff changeset
   158
				request.headers = headers;
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   159
				break;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   160
			else
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   161
				log("debug", "Unhandled header line: "..line);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
			end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   163
		end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   164
		
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   165
		if not expectbody(request) then 
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   166
			call_callback(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
			return;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   168
		end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   169
		
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   170
		-- Reached the end of the headers
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   171
		request.state = "body";
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   172
		if #data > startpos then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   173
			return request_reader(request, data:sub(startpos, -1));
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   174
		end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   175
	elseif request.state == "request" then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   176
		log("debug", "Reading request line...")
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   177
		local method, path, http, linelen = data:match("^(%S+) (%S+) HTTP/(%S+)\r\n()", startpos);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   178
		if not method then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   179
			return call_callback(request, "invalid-status-line");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   180
		end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   181
		
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   182
		request.method, request.path, request.httpversion = method, path, http;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   183
		
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   184
		request.url = url_parse(request.path);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   185
		
2371
7ddb8ce0cd0b net.httpserver: Fix usage of old connection API syntax
Matthew Wild <mwild1@gmail.com>
parents: 2360
diff changeset
   186
		log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler:serverport());
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   187
		
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   188
		if request.onlystatus then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   189
			if not call_callback(request) then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   190
				return;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   191
			end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   192
		end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   193
		
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   194
		request.state = "headers";
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   195
		
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   196
		if #data > linelen then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   197
			return request_reader(request, data:sub(linelen, -1));
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   198
		end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   199
	end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   200
end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   201
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   202
-- The default handler for requests
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   203
default_handler = function (method, body, request)
2371
7ddb8ce0cd0b net.httpserver: Fix usage of old connection API syntax
Matthew Wild <mwild1@gmail.com>
parents: 2360
diff changeset
   204
	log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler:serverport());
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   205
	return { status = "404 Not Found", 
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   206
			headers = { ["Content-Type"] = "text/html" },
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   207
			body = "<html><head><title>Page Not Found</title></head><body>Not here :(</body></html>" };
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   208
end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   209
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   210
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   211
function new_request(handler)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   212
	return { handler = handler, conn = handler.socket, 
2130
828e161cdfc7 net.httpserver, net.http: Update for new net.server API (untested)
Matthew Wild <mwild1@gmail.com>
parents: 2066
diff changeset
   213
			write = function (...) return handler:write(...); end, state = "request", 
2275
dd344b94d088 net.httpserver: Fix to work with new server API
Matthew Wild <mwild1@gmail.com>
parents: 2274
diff changeset
   214
			server = http_servers[handler:serverport()],
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   215
			send = send_response,
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   216
			destroy = destroy_request,
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   217
			id = tostring{}:match("%x+$")
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   218
			 };
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   219
end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   220
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   221
function destroy_request(request)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   222
	log("debug", "Destroying request %s", request.id);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   223
	listener = listener or connlisteners_get("httpserver");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   224
	if not request.destroyed then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   225
		request.destroyed = true;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   226
		if request.on_destroy then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   227
			log("debug", "Request has destroy callback");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   228
			request.on_destroy(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   229
		else
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   230
			log("debug", "Request has no destroy callback");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   231
		end
2371
7ddb8ce0cd0b net.httpserver: Fix usage of old connection API syntax
Matthew Wild <mwild1@gmail.com>
parents: 2360
diff changeset
   232
		request.handler:close()
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   233
		if request.conn then
2130
828e161cdfc7 net.httpserver, net.http: Update for new net.server API (untested)
Matthew Wild <mwild1@gmail.com>
parents: 2066
diff changeset
   234
			listener.ondisconnect(request.handler, "closed");
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   235
		end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   236
	end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   237
end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   238
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   239
function new(params)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   240
	local http_server = http_servers[params.port];
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   241
	if not http_server then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   242
		http_server = { handlers = {} };
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   243
		http_servers[params.port] = http_server;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   244
		-- We weren't already listening on this port, so start now
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   245
		connlisteners_start("httpserver", params);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   246
	end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   247
	if params.base then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   248
		http_server.handlers[params.base] = params.handler;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   249
	end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   250
end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   251
1666
d1243b321c45 net.httpserver: Allow overriding default request handler
Matthew Wild <mwild1@gmail.com>
parents: 1661
diff changeset
   252
function set_default_handler(handler)
d1243b321c45 net.httpserver: Allow overriding default request handler
Matthew Wild <mwild1@gmail.com>
parents: 1661
diff changeset
   253
	default_handler = handler;
d1243b321c45 net.httpserver: Allow overriding default request handler
Matthew Wild <mwild1@gmail.com>
parents: 1661
diff changeset
   254
end
d1243b321c45 net.httpserver: Allow overriding default request handler
Matthew Wild <mwild1@gmail.com>
parents: 1661
diff changeset
   255
1868
36e1238db2f2 net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents: 1666
diff changeset
   256
function new_from_config(ports, handle_request, default_options)
36e1238db2f2 net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents: 1666
diff changeset
   257
	if type(handle_request) == "string" then -- COMPAT with old plugins
36e1238db2f2 net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents: 1666
diff changeset
   258
		log("warn", "Old syntax of httpserver.new_from_config being used to register %s", handle_request);
36e1238db2f2 net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents: 1666
diff changeset
   259
		handle_request, default_options = default_options, { base = handle_request };
36e1238db2f2 net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents: 1666
diff changeset
   260
	end
1536
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   261
	for _, options in ipairs(ports) do
1868
36e1238db2f2 net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents: 1666
diff changeset
   262
		local port = default_options.port or 5280;
36e1238db2f2 net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents: 1666
diff changeset
   263
		local base = default_options.base;
36e1238db2f2 net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents: 1666
diff changeset
   264
		local ssl = default_options.ssl or false;
36e1238db2f2 net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents: 1666
diff changeset
   265
		local interface = default_options.interface;
1536
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   266
		if type(options) == "number" then
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   267
			port = options;
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   268
		elseif type(options) == "table" then
1868
36e1238db2f2 net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents: 1666
diff changeset
   269
			port = options.port or port;
36e1238db2f2 net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents: 1666
diff changeset
   270
			base = options.path or base;
36e1238db2f2 net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents: 1666
diff changeset
   271
			ssl = options.ssl or ssl;
36e1238db2f2 net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents: 1666
diff changeset
   272
			interface = options.interface or interface;
1536
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   273
		elseif type(options) == "string" then
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   274
			base = options;
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   275
		end
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   276
		
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   277
		if ssl then
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   278
			ssl.mode = "server";
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   279
			ssl.protocol = "sslv23";
2321
7e7484a4e821 Disable SSLv2 by default, it's known to be insecure.
Paul Aurich <paul@darkrain42.org>
parents: 2275
diff changeset
   280
			ssl.options = "no_sslv2";
1536
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   281
		end
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   282
		
2021
43b7c0980d23 net.httpserver: Pass on interface option from new_from_config() (thanks Chris)
Matthew Wild <mwild1@gmail.com>
parents: 1868
diff changeset
   283
		new{ port = port, interface = interface, 
43b7c0980d23 net.httpserver: Pass on interface option from new_from_config() (thanks Chris)
Matthew Wild <mwild1@gmail.com>
parents: 1868
diff changeset
   284
			base = base, handler = handle_request, 
43b7c0980d23 net.httpserver: Pass on interface option from new_from_config() (thanks Chris)
Matthew Wild <mwild1@gmail.com>
parents: 1868
diff changeset
   285
			ssl = ssl, type = (ssl and "ssl") or "tcp" };
1536
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   286
	end
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   287
end
8214458eacc8 net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   288
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   289
_M.request_reader = request_reader;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   290
_M.send_response = send_response;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   291
_M.urlencode = urlencode;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   292
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   293
return _M;