Software / code / prosody
Comparison
plugins/mod_httpserver.lua @ 3353:cd3cbf361f8f
mod_httpserver: Serve index.html if a request is made for a directory and it contains one (thanks Brian Cully)
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Tue, 13 Jul 2010 09:25:45 +0100 |
| parent | 2925:692b3c6c5bd2 |
| child | 4670:bd5e5e23942a |
comparison
equal
deleted
inserted
replaced
| 3352:770e27bf11ea | 3353:cd3cbf361f8f |
|---|---|
| 6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
| 7 -- | 7 -- |
| 8 | 8 |
| 9 | 9 |
| 10 local httpserver = require "net.httpserver"; | 10 local httpserver = require "net.httpserver"; |
| 11 local lfs = require "lfs"; | |
| 11 | 12 |
| 12 local open = io.open; | 13 local open = io.open; |
| 13 local t_concat = table.concat; | 14 local t_concat = table.concat; |
| 15 local stat = lfs.attributes; | |
| 14 | 16 |
| 15 local http_base = config.get("*", "core", "http_path") or "www_files"; | 17 local http_base = config.get("*", "core", "http_path") or "www_files"; |
| 16 | 18 |
| 17 local response_400 = { status = "400 Bad Request", body = "<h1>Bad Request</h1>Sorry, we didn't understand your request :(" }; | 19 local response_400 = { status = "400 Bad Request", body = "<h1>Bad Request</h1>Sorry, we didn't understand your request :(" }; |
| 18 local response_403 = { status = "403 Forbidden", body = "<h1>Forbidden</h1>You don't have permission to view the contents of this directory :(" }; | 20 local response_403 = { status = "403 Forbidden", body = "<h1>Forbidden</h1>You don't have permission to view the contents of this directory :(" }; |
| 46 end | 48 end |
| 47 return path; | 49 return path; |
| 48 end | 50 end |
| 49 | 51 |
| 50 function serve_file(path) | 52 function serve_file(path) |
| 51 local f, err = open(http_base..path, "rb"); | 53 local full_path = http_base..path; |
| 54 if stat(full_path, "mode") == "directory" then | |
| 55 if stat(full_path.."/index.html", "mode") == "file" then | |
| 56 return serve_file(path.."/index.html"); | |
| 57 end | |
| 58 return response_403; | |
| 59 end | |
| 60 local f, err = open(full_path, "rb"); | |
| 52 if not f then return response_404; end | 61 if not f then return response_404; end |
| 53 local data = f:read("*a"); | 62 local data = f:read("*a"); |
| 54 f:close(); | 63 f:close(); |
| 55 if not data then | 64 if not data then |
| 56 return response_403; | 65 return response_403; |