From cbfc28a946ded64e9402e1e6d32511150339ec72 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 4 Apr 2020 20:09:04 +0200 Subject: Prepare DocRoot(), WIP --- Makefile | 3 ++- config.cpp | 28 +++++++++++++++++++++++++ config.h | 3 +++ http.cpp | 2 +- stringutil.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ stringutil.h | 10 +++++++++ 6 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 stringutil.cpp create mode 100644 stringutil.h 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 #include #include @@ -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& Sites() const; const std::vector& 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 +#include + +#include + +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 split(std::string value, const std::string separators) +{ + std::vector 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 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 &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 +#include + +std::string strfmt(const char* fmt, ...); + +std::vector split(std::string value, const std::string separators = "\r\n "); +std::string join(std::vector vs, std::string separator = "\n"); +bool startsWithAnyOfLower(const std::string &s, const std::vector &list); -- cgit v1.2.3