diff options
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | config.cpp | 28 | ||||
-rw-r--r-- | config.h | 3 | ||||
-rw-r--r-- | http.cpp | 2 | ||||
-rw-r--r-- | stringutil.cpp | 66 | ||||
-rw-r--r-- | stringutil.h | 10 |
6 files changed, 110 insertions, 2 deletions
@@ -63,7 +63,8 @@ PROGSRC=\ https.cpp \ http_debian10.cpp \ plugin.cpp \ - server.cpp + server.cpp \ + stringutil.cpp TESTSRC=\ test-webserver.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 ""; +} + @@ -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; }; @@ -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); |