From 92a14d375c8cd9dabc32ccb6dcbdf83321af535f Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 11 Feb 2023 16:04:35 +0100 Subject: Added config --- Makefile | 6 ++--- config.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ config.h | 25 +++++++++++++++++++++ main.cpp | 7 ++++++ weblog.conf | 10 +++++---- weblog.cpp | 49 +++++++---------------------------------- weblog.h | 21 ++++-------------- 7 files changed, 125 insertions(+), 66 deletions(-) create mode 100644 config.cpp create mode 100644 config.h create mode 100644 main.cpp diff --git a/Makefile b/Makefile index 30f0335..bbe1cd0 100644 --- a/Makefile +++ b/Makefile @@ -15,10 +15,8 @@ LDLIBS=\ -lssl -lcrypto \ -ldl -PROGSRC=\ - weblog.cpp - -SRC=$(PROGSRC) +SRC=\ + weblog.cpp config.cpp main.cpp all: $(PROJECTNAME) diff --git a/config.cpp b/config.cpp new file mode 100644 index 0000000..44e0d36 --- /dev/null +++ b/config.cpp @@ -0,0 +1,73 @@ +#include "config.h" + +#include +#include + +#include +#include + +namespace pt = boost::property_tree; +using namespace std::string_literals; + +namespace { + const std::string default_datapath {"/var/lib/blog"}; + const std::string default_listen {"::1:9000"}; + const std::string default_name{"blog"}; + const std::string default_keywords{"blog, reichwein.it, weblog"}; +} + +Config::Config(const std::string& config_filename): + m_dataPath{default_datapath}, + m_listenAddress{"::1"}, + m_listenPort{9000}, + m_name{default_name}, + m_keywords{default_keywords} +{ + + try { + pt::ptree tree; + + pt::read_xml(config_filename, tree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace); + + m_dataPath = tree.get("config.datapath", default_datapath); + m_name = tree.get("config.name", default_name); + std::string listen {tree.get("config.port", default_listen)}; + auto pos{listen.find_last_of(':')}; + if (pos == std::string::npos) + throw std::runtime_error("Bad port address: "s + listen); + + m_listenAddress = listen.substr(0, pos); + m_listenPort = std::stoi(listen.substr(pos + 1)); + if (m_listenPort < 0 || m_listenPort > 65535) + throw std::runtime_error("Bad listen port: "s + std::to_string(m_listenPort)); + + m_keywords = tree.get("config.keywords", default_keywords); + } catch (const std::exception& ex) { + std::cerr << "Error reading config file " << config_filename << ". Using defaults." << std::endl; + } +} + +std::string Config::getDataPath() const +{ + return m_dataPath; +} + +std::string Config::getListenAddress() const +{ + return m_listenAddress; +} + +int Config::getListenPort() const +{ + return m_listenPort; +} + +std::string Config::getName() const +{ + return m_name; +} + +std::string Config::getKeywords() const +{ + return m_keywords; +} diff --git a/config.h b/config.h new file mode 100644 index 0000000..fff7f9d --- /dev/null +++ b/config.h @@ -0,0 +1,25 @@ +#pragma once + +#include + +const std::string default_config_filename{"/etc/weblog.conf"}; + +class Config +{ +private: + std::string m_dataPath; + std::string m_listenAddress; // ip address v4/v6 + int m_listenPort; + std::string m_name; + std::string m_keywords; + +public: + Config(const std::string& config_filename = default_config_filename); + std::string getDataPath() const; + + std::string getListenAddress() const; + int getListenPort() const; + + std::string getName() const; + std::string getKeywords() const; +}; diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..84cec43 --- /dev/null +++ b/main.cpp @@ -0,0 +1,7 @@ +#include "weblog.h" + +int main(int argc, char* argv[]) +{ + Weblog weblog; + return weblog.run(argc, argv); +} diff --git a/weblog.conf b/weblog.conf index 80a5ec6..6dd9ab7 100644 --- a/weblog.conf +++ b/weblog.conf @@ -1,4 +1,6 @@ -::1:9019 -/home/ernie/testblog -name Roland Reichweins Blog -keywords Roland Reichwein, Blog + + ::1:9019 + /home/ernie/testblog + Roland Reichweins Blog + Roland Reichwein, Blog + diff --git a/weblog.cpp b/weblog.cpp index cc2be34..7842ac3 100644 --- a/weblog.cpp +++ b/weblog.cpp @@ -2,6 +2,7 @@ #include "libreichwein/mime.h" #include "libreichwein/stringhelper.h" +#include "libreichwein/url.h" #include #include @@ -322,36 +323,6 @@ namespace { } } - std::string urlDecode(std::string s) - { - std::string result; - - size_t pos = 0; - while (pos < s.size()) { - char c {s[pos]}; - if (c == '+') { - result += ' '; - } else if (c == '%' && pos + 2 < s.size()) { - try { - int i = stoi(s.substr(pos + 1, 2), 0, 16); - if (i < 0 || i > 255) - return result; - - result += static_cast(i); - } catch (...) { - return result; - } - - pos += 2; - } else { - result += c; - } - pos++; - } - - return result; - } - std::unordered_map SplitQueryString(std::string& s) { std::unordered_map result; @@ -362,7 +333,7 @@ namespace { for (auto i: list) { size_t apos = i.find('='); if (apos != i.npos) { - result[urlDecode(i.substr(0, apos))] = urlDecode(i.substr(apos + 1)); + result[Reichwein::URL::urlDecode(i.substr(0, apos))] = Reichwein::URL::urlDecode(i.substr(apos + 1)); } } } @@ -374,22 +345,17 @@ namespace { } // anonymous namespace -std::string weblog_plugin::name() -{ - return "weblog"; -} - -weblog_plugin::weblog_plugin() +Weblog::Weblog() { //std::cout << "Plugin constructor" << std::endl; } -weblog_plugin::~weblog_plugin() +Weblog::~Weblog() { //std::cout << "Plugin destructor" << std::endl; } -std::string weblog_plugin::generate_page( +std::string Weblog::generate_page( std::function& GetServerParam, std::function& GetRequestParam, // request including body (POST...) std::function& SetResponseHeader // to be added to result string @@ -450,7 +416,8 @@ std::string weblog_plugin::generate_page( } } -bool weblog_plugin::has_own_authentication() +void Weblog::run(int argc, char* argv[]) { - return false; + return 0; } + diff --git a/weblog.h b/weblog.h index 0994b91..85eb5b1 100644 --- a/weblog.h +++ b/weblog.h @@ -1,23 +1,10 @@ #pragma once -#include "../../plugin_interface.h" - -class weblog_plugin: public webserver_plugin_interface +class weblog { public: - weblog_plugin(); - ~weblog_plugin(); + weblog(); + ~weblog(); - std::string name() override; - - std::string generate_page( - std::function& GetServerParam, - std::function& GetRequestParam, // request including body (POST...) - std::function& SetResponseHeader // to be added to result string - ) override; - - bool has_own_authentication() override; + void run(int argc, char* argv[]); }; - -extern "C" BOOST_SYMBOL_EXPORT weblog_plugin webserver_plugin; -weblog_plugin webserver_plugin; -- cgit v1.2.3