Annotate

plugins/mod_httpserver.lua @ 1092:b547967d87fc

mod_posix: Don't let the server run as root without the magic run_as_root in config
author Matthew Wild <mwild1@gmail.com>
date Sat, 02 May 2009 20:32:43 +0100
parent 696:b35faad717f2
child 1384:2f7403d47cf1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
25f1117d7886 Add initial mod_httpserver for serving static content
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 httpserver.new{ port = 5280, base = "files", handler = handle_request, ssl = false}