plugins/mod_httpserver.lua

Mon, 22 Jun 2009 16:16:08 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 22 Jun 2009 16:16:08 +0100
changeset 1384
2f7403d47cf1
parent 696
b35faad717f2
child 1522
569d58d21612
permissions
-rw-r--r--

mod_httpserver: Allow configuration of ports and base path, like mod_bosh

696
b35faad717f2 mod_httpserver: Add require 'net.httpserver'
Matthew Wild <mwild1@gmail.com>
parents: 635
diff changeset
1
b35faad717f2 mod_httpserver: Add require 'net.httpserver'
Matthew Wild <mwild1@gmail.com>
parents: 635
diff changeset
2 local httpserver = require "net.httpserver";
635
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local open = io.open;
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local t_concat = table.concat;
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local http_base = "www_files";
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local response_404 = { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" };
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local http_path = { http_base };
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local function handle_request(method, body, request)
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local path = request.url.path:gsub("%.%.%/", ""):gsub("^/[^/]+", "");
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 http_path[2] = path;
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local f, err = open(t_concat(http_path), "r");
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 if not f then return response_404; end
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local data = f:read("*a");
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 f:close();
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 return data;
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
1384
2f7403d47cf1 mod_httpserver: Allow configuration of ports and base path, like mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 696
diff changeset
22 local ports = config.get(module.host, "core", "http_ports") or { 5280 };
2f7403d47cf1 mod_httpserver: Allow configuration of ports and base path, like mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 696
diff changeset
23 for _, options in ipairs(ports) do
2f7403d47cf1 mod_httpserver: Allow configuration of ports and base path, like mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 696
diff changeset
24 local port, base, ssl, interface = 5280, "files", false, nil;
2f7403d47cf1 mod_httpserver: Allow configuration of ports and base path, like mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 696
diff changeset
25 if type(options) == "number" then
2f7403d47cf1 mod_httpserver: Allow configuration of ports and base path, like mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 696
diff changeset
26 port = options;
2f7403d47cf1 mod_httpserver: Allow configuration of ports and base path, like mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 696
diff changeset
27 elseif type(options) == "table" then
2f7403d47cf1 mod_httpserver: Allow configuration of ports and base path, like mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 696
diff changeset
28 port, base, ssl, interface = options.port or 5280, options.path or "files", options.ssl or false, options.interface;
2f7403d47cf1 mod_httpserver: Allow configuration of ports and base path, like mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 696
diff changeset
29 elseif type(options) == "string" then
2f7403d47cf1 mod_httpserver: Allow configuration of ports and base path, like mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 696
diff changeset
30 base = options;
2f7403d47cf1 mod_httpserver: Allow configuration of ports and base path, like mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 696
diff changeset
31 end
2f7403d47cf1 mod_httpserver: Allow configuration of ports and base path, like mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 696
diff changeset
32 httpserver.new{ port = port, base = base, handler = handle_request, ssl = ssl }
2f7403d47cf1 mod_httpserver: Allow configuration of ports and base path, like mod_bosh
Matthew Wild <mwild1@gmail.com>
parents: 696
diff changeset
33 end

mercurial