summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-04-04 20:09:04 +0200
committerRoland Reichwein <mail@reichwein.it>2020-04-04 20:09:04 +0200
commitcbfc28a946ded64e9402e1e6d32511150339ec72 (patch)
treebec1e30fdd97e99e88a7b168f3cad7278b59157e
parent1fcaed7a34cce8e55bb071d503bb583f715e7d37 (diff)
Prepare DocRoot(), WIP
-rw-r--r--Makefile3
-rw-r--r--config.cpp28
-rw-r--r--config.h3
-rw-r--r--http.cpp2
-rw-r--r--stringutil.cpp66
-rw-r--r--stringutil.h10
6 files changed, 110 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index ed35360..1021231 100644
--- a/Makefile
+++ b/Makefile
@@ -63,7 +63,8 @@ PROGSRC=\
https.cpp \
http_debian10.cpp \
plugin.cpp \
- server.cpp
+ server.cpp \
+ stringutil.cpp
TESTSRC=\
test-webserver.cpp \
diff --git a/config.cpp b/config.cpp
index c478265..4e33dfc 100644
--- a/config.cpp
+++ b/config.cpp
@@ -1,5 +1,6 @@
#include "config.h"
+#include <boost/algorithm/string/predicate.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
@@ -181,3 +182,30 @@ void Config::dump() const
std::cout << "=============================================" << std::endl;
}
+std::string Config::DocRoot(const Socket& socket, const std::string& requested_host, const std::string& requested_path)
+{
+ // TODO: speed this up
+ std::string host{requested_host};
+
+ auto pos {host.find(':')};
+ if (pos != host.npos) {
+ host = host.substr(0, pos);
+ }
+
+ for (const auto& site: m_sites) {
+ if (std::find(socket.serve_sites.begin(), socket.serve_sites.end(), site.name) != socket.serve_sites.end()) {
+ for (const auto& m_host: site.hosts) {
+ if (m_host == host) {
+ for (const auto& path: site.paths) {
+ if (boost::starts_with(requested_path, path.requested)) {
+ return path.params.at("target");
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return "";
+}
+
diff --git a/config.h b/config.h
index a8d6a03..0f5ec78 100644
--- a/config.h
+++ b/config.h
@@ -70,6 +70,9 @@ class Config
const std::vector<Site>& Sites() const;
const std::vector<Socket>& Sockets() const;
+ /// param[in] requested_host e.g. www.domain.com:8080 or www.domain.com
+ std::string DocRoot(const Socket& socket, const std::string& requested_host, const std::string& requested_path);
+
void dump() const;
};
diff --git a/http.cpp b/http.cpp
index aea4e07..eb4dc8c 100644
--- a/http.cpp
+++ b/http.cpp
@@ -153,7 +153,7 @@ handle_request(
// Build the path to the requested file
std::string path = path_cat(doc_root, req.target());
- std::cout << "DEBUG: " << req["host"] << std::endl;
+ std::cout << "DEBUG: " << req["host"] << "|" << req.target() << std::endl;
if(req.target().back() == '/')
path.append("index.html");
diff --git a/stringutil.cpp b/stringutil.cpp
new file mode 100644
index 0000000..f87fa00
--- /dev/null
+++ b/stringutil.cpp
@@ -0,0 +1,66 @@
+#include "stringutil.h"
+
+#include <boost/algorithm/string/predicate.hpp>
+#include <boost/algorithm/string/case_conv.hpp>
+
+#include <cstdarg>
+
+std::string strfmt(const char* fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ int size = std::vsnprintf(nullptr, 0, fmt, args);
+ va_end(args);
+
+ std::string result(size, ' ');
+
+ va_start(args, fmt);
+ std::vsnprintf(result.data(), size + 1, fmt, args);
+ va_end(args);
+
+ return result;
+}
+
+std::vector<std::string> split(std::string value, const std::string separators)
+{
+ std::vector<std::string> result;
+
+ size_t pos0 = 0;
+ size_t pos1 = 0;
+ while (pos0 < value.size()) {
+ pos1 = value.find_first_of(separators, pos0);
+ if (pos1 == std::string::npos)
+ pos1 = value.size();
+ std::string part = value.substr(pos0, pos1 - pos0);
+ //std::cout << "DEBUG: " << part << std::endl << std::flush;
+ if (part != "")
+ result.push_back(part);
+ pos0 = value.find_first_not_of(separators, pos1);
+ if (pos0 == std::string::npos)
+ pos0 = value.size();
+ }
+
+ return result;
+}
+
+std::string join(std::vector<std::string> vs, std::string separator)
+{
+ std::string s;
+ for (const auto& line : vs) {
+ if (s.size() > 0)
+ s += separator;
+ s += line;
+ }
+
+ return s;
+}
+
+bool startsWithAnyOfLower(const std::string &s, const std::vector<std::string> &list) {
+ for (const std::string& element : list) {
+ if (boost::algorithm::starts_with(boost::algorithm::to_lower_copy(s), boost::algorithm::to_lower_copy(element)))
+ return true;
+ }
+ return false;
+}
+
diff --git a/stringutil.h b/stringutil.h
new file mode 100644
index 0000000..5110e2e
--- /dev/null
+++ b/stringutil.h
@@ -0,0 +1,10 @@
+#pragma once
+
+#include <string>
+#include <vector>
+
+std::string strfmt(const char* fmt, ...);
+
+std::vector<std::string> split(std::string value, const std::string separators = "\r\n ");
+std::string join(std::vector<std::string> vs, std::string separator = "\n");
+bool startsWithAnyOfLower(const std::string &s, const std::vector<std::string> &list);