From 7ca291e03f0485ce227715ce00b787e1586098c9 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 23 Apr 2012 23:37:43 +0200 Subject: mod_httpserver: Rename to mod_http_files --- plugins/mod_http_files.lua | 86 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 plugins/mod_http_files.lua (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua new file mode 100644 index 00000000..bc9cbcd3 --- /dev/null +++ b/plugins/mod_http_files.lua @@ -0,0 +1,86 @@ +-- Prosody IM +-- Copyright (C) 2008-2010 Matthew Wild +-- Copyright (C) 2008-2010 Waqas Hussain +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- + +module:depends("http"); +local lfs = require "lfs"; + +local open = io.open; +local stat = lfs.attributes; + +local http_base = module:get_option_string("http_path", "www_files"); + +local response_400 = "

Bad Request

Sorry, we didn't understand your request :("; +local response_403 = "

Forbidden

You don't have permission to view the contents of this directory :("; +local response_404 = "

Page Not Found

Sorry, we couldn't find what you were looking for :("; + +-- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?) +local mime_map = { + html = "text/html"; + htm = "text/html"; + xml = "text/xml"; + xsl = "text/xml"; + txt = "text/plain; charset=utf-8"; + js = "text/javascript"; + css = "text/css"; +}; + +local function preprocess_path(path) + if path:sub(1,1) ~= "/" then + path = "/"..path; + end + local level = 0; + for component in path:gmatch("([^/]+)/") do + if component == ".." then + level = level - 1; + elseif component ~= "." then + level = level + 1; + end + if level < 0 then + return nil; + end + end + return path; +end + +function serve_file(request, path) + local response = request.response; + path = path and preprocess_path(path); + if not path then + response.status = 400; + return response:send(response_400); + end + local full_path = http_base..path; + if stat(full_path, "mode") == "directory" then + if stat(full_path.."/index.html", "mode") == "file" then + return serve_file(request, path.."/index.html"); + end + response.status = 403; + return response:send(response_403); + end + local f, err = open(full_path, "rb"); + if not f then + response.status = 404; + return response:send(response_404.."
"..tostring(err)); + end + local data = f:read("*a"); + f:close(); + if not data then + response.status = 403; + return response:send(response_403); + end + local ext = path:match("%.([^.]*)$"); + response.headers.content_type = mime_map[ext]; -- Content-Type should be nil when not known + return response:send(data); +end + +module:provides("http", { + route = { + ["/*"] = serve_file; + }; +}); + -- cgit v1.2.3 From 3fd98627c00a102fc7cbb4657be7e872f953b869 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 24 Apr 2012 00:17:15 +0200 Subject: mod_http_files: Rename argument to reflect what it actually is --- plugins/mod_http_files.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index bc9cbcd3..437633e7 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -47,8 +47,8 @@ local function preprocess_path(path) return path; end -function serve_file(request, path) - local response = request.response; +function serve_file(event, path) + local response = event.response; path = path and preprocess_path(path); if not path then response.status = 400; @@ -57,7 +57,7 @@ function serve_file(request, path) local full_path = http_base..path; if stat(full_path, "mode") == "directory" then if stat(full_path.."/index.html", "mode") == "file" then - return serve_file(request, path.."/index.html"); + return serve_file(event, path.."/index.html"); end response.status = 403; return response:send(response_403); -- cgit v1.2.3 From eb427566464aa721caa9a482d01f59e460de2f51 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 26 Apr 2012 06:10:14 +0100 Subject: mod_http_files: Return numeric error codes instead of custom error responses --- plugins/mod_http_files.lua | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index 437633e7..c5859283 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -14,10 +14,6 @@ local stat = lfs.attributes; local http_base = module:get_option_string("http_path", "www_files"); -local response_400 = "

Bad Request

Sorry, we didn't understand your request :("; -local response_403 = "

Forbidden

You don't have permission to view the contents of this directory :("; -local response_404 = "

Page Not Found

Sorry, we couldn't find what you were looking for :("; - -- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?) local mime_map = { html = "text/html"; @@ -51,27 +47,23 @@ function serve_file(event, path) local response = event.response; path = path and preprocess_path(path); if not path then - response.status = 400; - return response:send(response_400); + return 400; end local full_path = http_base..path; if stat(full_path, "mode") == "directory" then if stat(full_path.."/index.html", "mode") == "file" then return serve_file(event, path.."/index.html"); end - response.status = 403; - return response:send(response_403); + return 403; end local f, err = open(full_path, "rb"); if not f then - response.status = 404; - return response:send(response_404.."
"..tostring(err)); + return 404; end local data = f:read("*a"); f:close(); if not data then - response.status = 403; - return response:send(response_403); + return 403; end local ext = path:match("%.([^.]*)$"); response.headers.content_type = mime_map[ext]; -- Content-Type should be nil when not known -- cgit v1.2.3 From 603f08ca8419b8deff0334e206442f0c01bbd2f3 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 26 Apr 2012 06:30:29 +0100 Subject: mod_http_files: Log 404 failure reason --- plugins/mod_http_files.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index c5859283..9d5aa252 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -58,6 +58,7 @@ function serve_file(event, path) end local f, err = open(full_path, "rb"); if not f then + module:log("warn", "Failed to open file: %s", err); return 404; end local data = f:read("*a"); -- cgit v1.2.3 From 9b7e2f6d4b643bf24fb2adac75d2129dfb68bcd0 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 26 Apr 2012 06:42:02 +0100 Subject: mod_http_files: Change option name from http_path to http_files_dir --- plugins/mod_http_files.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index 9d5aa252..932d2547 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -12,7 +12,7 @@ local lfs = require "lfs"; local open = io.open; local stat = lfs.attributes; -local http_base = module:get_option_string("http_path", "www_files"); +local http_base = module:get_option_string("http_files_dir", module:get_option_string("http_path", "www_files")); -- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?) local mime_map = { -- cgit v1.2.3 From e64c982be472e25835cf9bf9f14efb131b73aece Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 26 Apr 2012 16:48:16 +0100 Subject: mod_http_files, net.http.parser: Move path normalization to net.http.parser so that all modules can benefit --- plugins/mod_http_files.lua | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index 932d2547..9be735b7 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -25,31 +25,9 @@ local mime_map = { css = "text/css"; }; -local function preprocess_path(path) - if path:sub(1,1) ~= "/" then - path = "/"..path; - end - local level = 0; - for component in path:gmatch("([^/]+)/") do - if component == ".." then - level = level - 1; - elseif component ~= "." then - level = level + 1; - end - if level < 0 then - return nil; - end - end - return path; -end - function serve_file(event, path) local response = event.response; - path = path and preprocess_path(path); - if not path then - return 400; - end - local full_path = http_base..path; + local full_path = http_base.."/"..path; if stat(full_path, "mode") == "directory" then if stat(full_path.."/index.html", "mode") == "file" then return serve_file(event, path.."/index.html"); -- cgit v1.2.3 From aa1d8dd97b673e7fdd4c72fd4216b593dbb79f87 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 27 Apr 2012 18:40:44 +0100 Subject: mod_http_files: Specify method in HTTP route --- plugins/mod_http_files.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/mod_http_files.lua') diff --git a/plugins/mod_http_files.lua b/plugins/mod_http_files.lua index 9be735b7..dc58ff5d 100644 --- a/plugins/mod_http_files.lua +++ b/plugins/mod_http_files.lua @@ -51,7 +51,7 @@ end module:provides("http", { route = { - ["/*"] = serve_file; + ["GET /*"] = serve_file; }; }); -- cgit v1.2.3