diff options
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | base64.cpp | 23 | ||||
-rw-r--r-- | base64.h | 7 | ||||
-rw-r--r-- | os.cpp | 78 | ||||
-rw-r--r-- | os.h | 10 | ||||
-rw-r--r-- | response.cpp | 12 | ||||
-rw-r--r-- | tests/Makefile | 2 |
7 files changed, 8 insertions, 131 deletions
@@ -15,6 +15,7 @@ PLUGINS= \ weblog CXXFLAGS+=-fPIE +CXXFLAGS+=-gdwarf-4 LDLIBS+=\ -lreichwein \ @@ -36,11 +37,9 @@ LDFLAGS+=-pie PROGSRC=\ auth.cpp \ - base64.cpp \ config.cpp \ http.cpp \ https.cpp \ - os.cpp \ plugin.cpp \ privileges.cpp \ response.cpp \ @@ -123,8 +122,6 @@ DISTFILES= \ archive.h \ auth.cpp \ auth.h \ - base64.cpp \ - base64.h \ config.cpp \ config.h \ http.cpp \ @@ -132,8 +129,6 @@ DISTFILES= \ https.cpp \ https.h \ main.cpp \ - os.cpp \ - os.h \ plugin.cpp \ plugin.h \ plugin_interface.h \ diff --git a/base64.cpp b/base64.cpp deleted file mode 100644 index 3847f0a..0000000 --- a/base64.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "base64.h" - -#include <boost/archive/iterators/binary_from_base64.hpp> -#include <boost/archive/iterators/base64_from_binary.hpp> -#include <boost/archive/iterators/transform_width.hpp> -#include <boost/algorithm/string.hpp> - -std::string decode64(const std::string &val) -{ - using namespace boost::archive::iterators; - using It = transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>; - return boost::algorithm::trim_right_copy_if(std::string(It(std::begin(val)), It(std::end(val))), [](char c) { - return c == '\0'; - }); -} - -std::string encode64(const std::string &val) -{ - using namespace boost::archive::iterators; - using It = base64_from_binary<transform_width<std::string::const_iterator, 6, 8>>; - auto tmp = std::string(It(std::begin(val)), It(std::end(val))); - return tmp.append((3 - val.size() % 3) % 3, '='); -} diff --git a/base64.h b/base64.h deleted file mode 100644 index 6a3f2c3..0000000 --- a/base64.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include <string> - -std::string decode64(const std::string &val); -std::string encode64(const std::string &val); - @@ -1,78 +0,0 @@ -#include "os.h" - -#include <boost/algorithm/string/split.hpp> - -#include <unistd.h> - -#include <fstream> -#include <vector> - -using namespace std::string_literals; - -namespace { - - std::string to_string(uint32_t v, size_t size) - { - std::string result{std::to_string(v)}; - - return (size > result.size() ? std::string(size - result.size(), char('0')) : ""s) + result; - } - - std::string to_time_string(uint32_t sec) - { - uint32_t days = sec / (24 * 3600); - uint32_t hours = (sec % (24 * 3600)) / 3600; - uint32_t minutes = (sec % 3600) / 60; - uint32_t seconds = (sec % 60); - - return std::to_string(days) + " days, "s + to_string(hours, 2) + ":"s + to_string(minutes, 2) + ":"s + to_string(seconds, 2); - } - - uint64_t uptime() - { - double uptime_seconds{}; - if (std::ifstream("/proc/uptime", std::ios::in) >> uptime_seconds) - { - return static_cast<uint64_t>(uptime_seconds); - } - - return 0; - } - -} // anonymous namespace - -std::string OS::uptime_host() -{ - return to_time_string(uptime()); -} - -std::string OS::uptime_process() -{ - std::string filepath{"/proc/self/stat"}; - std::ifstream f(filepath, std::ios::in); - if (f.is_open()) { - std::string line; - std::getline(f, line); - std::vector<std::string> elements; - boost::algorithm::split(elements, line, [](char c){ return c == ' '; }); - if (elements.size() < 22) - throw std::runtime_error("Bad contents of /proc/self/stat"); - - long jiffies_per_second {sysconf(_SC_CLK_TCK)}; - if (jiffies_per_second == 0) - throw std::runtime_error("Jiffies per second is 0"); - - try { - unsigned long starttime { std::stoul(elements[21])}; - - unsigned long runtime = uptime() - starttime / jiffies_per_second; - - return to_time_string(runtime); - } catch (const std::exception& ex) { - throw std::runtime_error("Bad value in /proc/self/stat: "s + ex.what()); - } - - } else - throw std::runtime_error("Reading /proc/self/stat"); -} - @@ -1,10 +0,0 @@ -#pragma once - -#include <string> - -namespace OS { - - std::string uptime_host(); - std::string uptime_process(); - -} // namespace OS diff --git a/response.cpp b/response.cpp index 92390cd..17d3e48 100644 --- a/response.cpp +++ b/response.cpp @@ -1,11 +1,11 @@ #include "response.h" #include "auth.h" -#include "base64.h" -#include "os.h" -#include "libreichwein/mime.h" +#include "libreichwein/base64.h" #include "libreichwein/file.h" +#include "libreichwein/mime.h" +#include "libreichwein/os.h" #include <boost/algorithm/string/predicate.hpp> @@ -16,6 +16,8 @@ using namespace std::placeholders; using namespace Reichwein::Mime; +using namespace Reichwein::OS; +using namespace Reichwein::Base64; namespace { @@ -122,8 +124,8 @@ std::unordered_map<std::string, std::function<std::string(Server&)>> GetServerPa {"ipv6", [](Server& server) { return is_ipv6_address(server.GetSocket().address) ? "yes" : "no"; }}, {"port", [](Server& server) { return server.GetSocket().port; }}, {"statistics", [](Server& server) { return server.GetStatistics().getValues(); }}, - {"uptime_host", [](Server& server) { return OS::uptime_host(); }}, - {"uptime_webserver", [](Server& server) { return OS::uptime_process(); }}, + {"uptime_host", [](Server& server) { return uptime_host(); }}, + {"uptime_webserver", [](Server& server) { return uptime_process(); }}, {"version", [](Server& server) { return Server::VersionString; }}, }; diff --git a/tests/Makefile b/tests/Makefile index 033ba95..4522952 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -33,11 +33,9 @@ LDFLAGS+=-pie UNITS=\ auth.cpp \ - base64.cpp \ config.cpp \ http.cpp \ https.cpp \ - os.cpp \ plugin.cpp \ privileges.cpp \ response.cpp \ |