summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-04-13 19:36:15 +0200
committerRoland Reichwein <mail@reichwein.it>2020-04-13 19:36:15 +0200
commit6b91026eddfa739b6637b18285041063e0fd0348 (patch)
tree728375b29278bd1a1e71464d7818f69c38ecea1f
parent5b3022c4a0e81ff23ce4ebc2ec7b03e32f7a719e (diff)
Fix webbox (WIP)
-rw-r--r--Makefile2
-rw-r--r--TODO1
-rw-r--r--plugins/static-files/static-files.cpp55
-rw-r--r--response.cpp13
-rw-r--r--webserver.conf4
5 files changed, 65 insertions, 10 deletions
diff --git a/Makefile b/Makefile
index 78cfa40..bb1c26b 100644
--- a/Makefile
+++ b/Makefile
@@ -76,7 +76,7 @@ TESTSRC=\
SRC=$(PROGSRC) webserver.cpp
build: $(PROJECTNAME) test-$(PROJECTNAME)
- for i in $(PLUGINS) ; do make -C plugins/$$i ; done
+ set -e ; for i in $(PLUGINS) ; do make -C plugins/$$i ; done
./test-$(PROJECTNAME)
all: build
diff --git a/TODO b/TODO
index 66f23c3..a8405aa 100644
--- a/TODO
+++ b/TODO
@@ -6,3 +6,4 @@ Request properties: Remote Address, e.g. [::1]:8081 -> ipv6 / ipv4
Speed up config.GetPath
read: The socket was closed due to a timeout
statistics
+index page
diff --git a/plugins/static-files/static-files.cpp b/plugins/static-files/static-files.cpp
index b137cb8..011e37f 100644
--- a/plugins/static-files/static-files.cpp
+++ b/plugins/static-files/static-files.cpp
@@ -1,5 +1,7 @@
#include "static-files.h"
+#include <boost/algorithm/string/predicate.hpp>
+
#include <filesystem>
#include <fstream>
#include <iostream>
@@ -10,6 +12,42 @@ namespace fs = std::filesystem;
namespace {
+// Return a reasonable mime type based on the extension of a file.
+std::string
+mime_type(fs::path path)
+{
+ using boost::algorithm::iequals;
+ auto const ext = [&path]
+ {
+ size_t pos = path.string().rfind(".");
+ if (pos == std::string::npos)
+ return std::string{};
+ return path.string().substr(pos);
+ }();
+ if(iequals(ext, ".htm")) return "text/html"; // TODO: unordered_map
+ if(iequals(ext, ".html")) return "text/html";
+ if(iequals(ext, ".php")) return "text/html";
+ if(iequals(ext, ".css")) return "text/css";
+ if(iequals(ext, ".txt")) return "text/plain";
+ if(iequals(ext, ".js")) return "application/javascript";
+ if(iequals(ext, ".json")) return "application/json";
+ if(iequals(ext, ".xml")) return "application/xml";
+ if(iequals(ext, ".swf")) return "application/x-shockwave-flash";
+ if(iequals(ext, ".flv")) return "video/x-flv";
+ if(iequals(ext, ".png")) return "image/png";
+ if(iequals(ext, ".jpe")) return "image/jpeg";
+ if(iequals(ext, ".jpeg")) return "image/jpeg";
+ if(iequals(ext, ".jpg")) return "image/jpeg";
+ if(iequals(ext, ".gif")) return "image/gif";
+ if(iequals(ext, ".bmp")) return "image/bmp";
+ if(iequals(ext, ".ico")) return "image/vnd.microsoft.icon";
+ if(iequals(ext, ".tiff")) return "image/tiff";
+ if(iequals(ext, ".tif")) return "image/tiff";
+ if(iequals(ext, ".svg")) return "image/svg+xml";
+ if(iequals(ext, ".svgz")) return "image/svg+xml";
+ return "application/text";
+}
+
std::string getFile(const fs::path& filename)
{
std::ifstream file(filename.string(), std::ios::in | std::ios::binary | std::ios::ate);
@@ -28,10 +66,10 @@ std::string getFile(const fs::path& filename)
}
}
-std::string extend_index_html(std::string path)
+fs::path extend_index_html(fs::path path)
{
- if (path.size() == 0 || (path.size() && path.back() == '/'))
- path.append("index.html");
+ if (path.string().size() == 0 || path.string().back() == '/')
+ return path / "index.html";
return path;
}
@@ -74,14 +112,21 @@ std::string static_files_plugin::generate_page(
// Request path must not contain "..".
std::string rel_target{GetRequestParam("rel_target")};
+ std::string target{GetRequestParam("target")};
if (rel_target.find("..") != std::string::npos) {
- std::string target{GetRequestParam("target")};
return HttpStatus("400", "Illegal request: "s + target, SetResponseHeader);
}
// Build the path to the requested file
std::string doc_root{GetRequestParam("doc_root")};
- std::string path {fs::path{doc_root} / extend_index_html(rel_target)};
+ fs::path path {fs::path{doc_root} / rel_target};
+ if (target.size() && target.back() != '/' && fs::is_directory(path)) {
+ std::string location{GetRequestParam("location") + "/"s};
+ SetResponseHeader("location", location);
+ return HttpStatus("301", "Correcting directory path", SetResponseHeader);
+ }
+ path = {extend_index_html(path)};
+ SetResponseHeader("content_type", mime_type(path));
try {
return getFile(path);
diff --git a/response.cpp b/response.cpp
index a70a694..5c68d51 100644
--- a/response.cpp
+++ b/response.cpp
@@ -42,7 +42,10 @@ public:
std::string GetRelativePath() const { // can throw std::runtime_error
if (!boost::starts_with(m_target, m_path.requested))
throw std::runtime_error("Mismatch of target ("s + m_target + ") and plugin path(" + m_path.requested + ")"s);
- return m_target.substr(m_path.requested.size());
+ if (m_target.size() > m_path.requested.size() && m_target[m_path.requested.size()] == '/')
+ return m_target.substr(m_path.requested.size() + 1);
+ else
+ return m_target.substr(m_path.requested.size());
}
std::string GetPluginParam(const std::string& key) const {return m_path.params.at(key);} // can throw std::out_of_range
@@ -52,11 +55,13 @@ public:
request_type& GetReq() const {return m_req;}
std::string GetTarget() const {return m_target;}
+
+ std::string GetHost() const {return m_host;}
};
std::string extend_index_html(std::string path)
{
- if (path.size() && path.back() == '/')
+ if (path.size() == 0 || path.back() == '/')
path.append("index.html");
return path;
}
@@ -83,6 +88,8 @@ std::unordered_map<std::string, std::function<std::string(RequestContext&)>> Get
{"content_type", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq()["content_type"]}; }}, // TODO: does this work?
{"method", [](RequestContext& req_ctx) { return std::string{req_ctx.GetReq().method_string()};}},
+
+ {"location", [](RequestContext& req_ctx) { return req_ctx.GetTarget(); }},
};
std::string GetRequestParam(const std::string& key, RequestContext& req_ctx)
@@ -129,6 +136,8 @@ void SetResponseHeader(const std::string& key, const std::string& value, respons
res.set(http::field::content_type, value);
} else if (key == "content_disposition") { // e.g. attachment; ...
res.set(http::field::content_disposition, value);
+ } else if (key == "location") { // e.g. 301 Moved Permanently: new Location
+ res.set(http::field::location, value);
} else
throw std::runtime_error("Unsupported response field: "s + key);
}
diff --git a/webserver.conf b/webserver.conf
index 76f2591..35ff2f6 100644
--- a/webserver.conf
+++ b/webserver.conf
@@ -21,11 +21,11 @@
<plugin>static-files</plugin>
<target>/home/ernie/homepage/test</target>
</path>
- <path requested="/webbox">
+ <path requested="/webbox1">
<plugin>static-files</plugin>
<target>/home/ernie/code/webbox/html</target>
</path>
- <path requested="/webbox/bin">
+ <path requested="/webbox1/bin">
<plugin>webbox</plugin>
<target>/home/ernie/testbox</target>
<WEBBOX_NAME>Testbox1</WEBBOX_NAME>