summaryrefslogtreecommitdiffhomepage
path: root/plugins/webbox/webbox.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-05-14 18:21:04 +0200
committerRoland Reichwein <mail@reichwein.it>2020-05-14 18:21:04 +0200
commitdef52539028cb024b4e9e9767796face08d645f6 (patch)
tree184797e14fc0bb1d625bd336094ade543d7df05e /plugins/webbox/webbox.cpp
parent72fbf82cd8ca1794fde0b1348956892fd87f2f12 (diff)
Webbox: Support subdirectory via URL. This makes webbox viable as index page
Diffstat (limited to 'plugins/webbox/webbox.cpp')
-rw-r--r--plugins/webbox/webbox.cpp31
1 files changed, 22 insertions, 9 deletions
diff --git a/plugins/webbox/webbox.cpp b/plugins/webbox/webbox.cpp
index 4abb2bf..09ac7c5 100644
--- a/plugins/webbox/webbox.cpp
+++ b/plugins/webbox/webbox.cpp
@@ -32,7 +32,7 @@ namespace {
static const std::string DOWNLOAD_FILENAME{"webbox-download.zip"};
- // STATIC_HTML_TARGET: no leading slash because comparision is done with relative path;
+ // STATIC_HTML_TARGET: no leading slash because comparison is done with relative path;
// trailing slash because of path comparison
static const std::string STATIC_HTML_TARGET{"webbox-html/"};
static const fs::path STATIC_HTML_DOC_ROOT{"/usr/lib/webbox/html"};
@@ -75,7 +75,7 @@ namespace {
return result;
}
- std::unordered_map<std::string, std::string> ParseQueryString(std::string s)
+ std::unordered_map<std::string, std::string> ParseQueryString(const std::string& s, const std::string& webboxPath)
{
std::unordered_map<std::string, std::string> result;
@@ -88,7 +88,10 @@ namespace {
result[urlDecode(i.substr(0, pos))] = urlDecode(i.substr(pos + 1));
}
}
- } else if (s == "/" || s == "" || boost::algorithm::starts_with(s, STATIC_HTML_TARGET)) {
+ } else if (s.empty() || s.back() == '/' || boost::algorithm::starts_with(s, STATIC_HTML_TARGET) ||
+ boost::algorithm::contains(s, "/"s + STATIC_HTML_TARGET) ||
+ (s.back() != '/' && fs::exists(webboxPath + "/" + s) && fs::is_directory(webboxPath + "/" + s)))
+ {
result["command"] = "static-html";
}
@@ -104,12 +107,13 @@ namespace {
std::string m_pathInfo; // path inside webbox, derived from request
fs::path m_path; // local filesystem path
- std::unordered_map<std::string, std::string> paramHash;
-
std::string webboxPath;
std::string webboxName;
bool webboxReadOnly;
fs::path webboxStaticHtml;
+ std::string m_rootdir;
+
+ std::unordered_map<std::string, std::string> paramHash;
CommandParameters(
std::function<std::string(const std::string& key)>& GetServerParam,
@@ -119,11 +123,12 @@ namespace {
: m_GetServerParam(GetServerParam)
, m_GetRequestParam(GetRequestParam)
, m_SetResponseHeader(SetResponseHeader)
- , paramHash(ParseQueryString(GetRequestParam("rel_target"))) // rel_target contains query string
, webboxPath(m_GetRequestParam("doc_root"))
, webboxName(m_GetRequestParam("WEBBOX_NAME"))
, webboxReadOnly(m_GetRequestParam("WEBBOX_READONLY") == "1")
, webboxStaticHtml(m_GetRequestParam("WEBBOX_STATIC_HTML"))
+ , m_rootdir(m_GetRequestParam("plugin_path"))
+ , paramHash(ParseQueryString(GetRequestParam("rel_target"), webboxPath)) // rel_target contains query string
{
if (webboxStaticHtml == "")
webboxStaticHtml = STATIC_HTML_DOC_ROOT;
@@ -327,6 +332,7 @@ protected:
// Retrieve from Server:
// Title
// ReadOnly flag
+// Root directory: Where in the HTTP URL is the webbox root located, e.g. /webbox1
class ServerInfoCommand: public GetCommand
{
public:
@@ -344,6 +350,8 @@ protected:
pt::ptree tree;
tree.put("serverinfo.title", p.webboxName);
tree.put("serverinfo.readonly", p.webboxReadOnly ? "1" : "0");
+ tree.put("serverinfo.rootdir", p.m_rootdir);
+ tree.put("serverinfo.currentdir", p.m_pathInfo);
std::ostringstream ss;
pt::xml_parser::write_xml(ss, tree);
return ss.str();
@@ -400,6 +408,7 @@ protected:
}
};
+// Info about single or multiple files
class InfoCommand: public PostCommand
{
public:
@@ -838,8 +847,10 @@ protected:
virtual std::string start(CommandParameters& p)
{
// redirect to xyz/ if xyz was requested
- std::string target = p.m_GetRequestParam("target");
- if (p.m_pathInfo == "" && !target.empty() && target.back() != '/') {
+ std::string target { p.m_GetRequestParam("target") };
+ if (!target.empty() && target.back() != '/' &&
+ (p.m_pathInfo.empty() || (fs::exists(p.webboxPath + "/" + p.m_pathInfo) && fs::is_directory(p.webboxPath + "/" + p.m_pathInfo))))
+ {
p.m_SetResponseHeader("location", target + "/");
return HttpStatus("301", "Use correct index: /"s, p);
}
@@ -847,10 +858,12 @@ protected:
try {
fs::path file_path;
- if (p.m_pathInfo == "/" || p.m_pathInfo == "") {
+ if (p.m_pathInfo.empty() || p.m_pathInfo.back() == '/') {
file_path = p.webboxStaticHtml / "index.html";
} else if (boost::algorithm::starts_with(p.m_pathInfo, STATIC_HTML_TARGET)) {
file_path = p.webboxStaticHtml / p.m_pathInfo.substr(STATIC_HTML_TARGET.size());
+ } else if (boost::algorithm::contains(p.m_pathInfo, "/"s + STATIC_HTML_TARGET)) {
+ file_path = p.webboxStaticHtml / p.m_pathInfo.substr(p.m_pathInfo.find("/"s + STATIC_HTML_TARGET) + STATIC_HTML_TARGET.size() + 1);
} else {
return HttpStatus("500", "Bad request: "s + p.m_pathInfo, p);
}