From 5bc1f7bed536e0e936fd13fad45c49392b0bfff4 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 11 May 2020 12:24:04 +0200 Subject: Separated out routines to libcommon --- Makefile | 15 ++++------ file.cpp | 46 ------------------------------ file.h | 15 ---------- https.cpp | 3 +- libcommon/Makefile | 5 +++- libcommon/file.cpp | 46 ++++++++++++++++++++++++++++++ libcommon/file.h | 15 ++++++++++ libcommon/stringutil.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++ libcommon/stringutil.h | 10 +++++++ plugins/webbox/Makefile | 2 -- plugins/webbox/file.cpp | 46 ------------------------------ plugins/webbox/file.h | 15 ---------- plugins/webbox/stringutil.cpp | 66 ------------------------------------------- plugins/webbox/stringutil.h | 10 ------- plugins/webbox/webbox.cpp | 35 +++-------------------- plugins/weblog/Makefile | 1 - plugins/weblog/stringutil.cpp | 66 ------------------------------------------- plugins/weblog/stringutil.h | 10 ------- plugins/weblog/weblog.cpp | 3 +- response.cpp | 2 +- 20 files changed, 155 insertions(+), 322 deletions(-) delete mode 100644 file.cpp delete mode 100644 file.h create mode 100644 libcommon/file.cpp create mode 100644 libcommon/file.h create mode 100644 libcommon/stringutil.cpp create mode 100644 libcommon/stringutil.h delete mode 100644 plugins/webbox/file.cpp delete mode 100644 plugins/webbox/file.h delete mode 100644 plugins/webbox/stringutil.cpp delete mode 100644 plugins/webbox/stringutil.h delete mode 100644 plugins/weblog/stringutil.cpp delete mode 100644 plugins/weblog/stringutil.h diff --git a/Makefile b/Makefile index b9fb155..edadb60 100644 --- a/Makefile +++ b/Makefile @@ -78,7 +78,6 @@ PROGSRC=\ auth.cpp \ base64.cpp \ config.cpp \ - file.cpp \ http.cpp \ https.cpp \ os.cpp \ @@ -171,8 +170,6 @@ DISTFILES= \ base64.h \ config.cpp \ config.h \ - file.cpp \ - file.h \ http.cpp \ http.h \ https.cpp \ @@ -214,6 +211,12 @@ DISTFILES= \ libcommon/Makefile \ libcommon/mime.h \ libcommon/mime.cpp \ + libcommon/file.h \ + libcommon/file.cpp \ + libcommon/stringutil.h \ + libcommon/stringutil.cpp \ + libcommon/tempfile.h \ + libcommon/tempfile.cpp \ plugins/cgi/cgi.h \ plugins/cgi/Makefile \ plugins/cgi/cgi.cpp \ @@ -225,10 +228,6 @@ DISTFILES= \ plugins/fcgi/fcgiid.cpp \ plugins/fcgi/socket.h \ plugins/fcgi/socket.cpp \ - plugins/webbox/file.h \ - plugins/webbox/file.cpp \ - plugins/webbox/stringutil.cpp \ - plugins/webbox/stringutil.h \ plugins/webbox/webbox.h \ plugins/webbox/html/refresh-inverted.png \ plugins/webbox/html/directory.png \ @@ -243,8 +242,6 @@ DISTFILES= \ plugins/weblog/weblog.h \ plugins/weblog/procmail/procmailrc \ plugins/weblog/procmail/procmail.sh \ - plugins/weblog/stringutil.h \ - plugins/weblog/stringutil.cpp \ plugins/weblog/Makefile \ plugins/weblog/weblog.cpp \ plugins/redirect/Makefile \ diff --git a/file.cpp b/file.cpp deleted file mode 100644 index 47ab8be..0000000 --- a/file.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "file.h" - -#include - -namespace fs = std::filesystem; - -using namespace std::string_literals; - -std::string File::getFile(const fs::path& filename) -{ - std::ifstream file(filename.string(), std::ios::in | std::ios::binary | std::ios::ate); - - if (file.is_open()) { - std::ifstream::pos_type fileSize = file.tellg(); - file.seekg(0, std::ios::beg); - - std::string bytes(fileSize, ' '); - file.read(reinterpret_cast(bytes.data()), fileSize); - - return bytes; - - } else { - throw std::runtime_error("Opening "s + filename.string() + " for reading"); - } -} - -void File::setFile(const fs::path& filename, const std::string& s) -{ - File::setFile(filename, s.data(), s.size()); -} - -void File::setFile(const fs::path& filename, const char* data, size_t size) -{ - std::ofstream file(filename.string(), std::ios::out | std::ios::binary); - if (file.is_open()) { - file.write(data, size); - } else { - throw std::runtime_error("Opening "s + filename.string() + " for writing"); - } -} - -void File::setFile(const fs::path& filename, const std::vector& data) -{ - File::setFile(filename, reinterpret_cast(data.data()), data.size()); -} - diff --git a/file.h b/file.h deleted file mode 100644 index e7e4cf6..0000000 --- a/file.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace File { - -std::string getFile(const std::filesystem::path& filename); -void setFile(const std::filesystem::path& filename, const std::string& s); -void setFile(const std::filesystem::path& filename, const char* data, size_t size); -void setFile(const std::filesystem::path& filename, const std::vector& data); - -} diff --git a/https.cpp b/https.cpp index cec0e4b..6ca27f0 100644 --- a/https.cpp +++ b/https.cpp @@ -1,10 +1,11 @@ #include "https.h" #include "config.h" -#include "file.h" #include "server.h" #include "response.h" +#include "libcommon/file.h" + #include #include diff --git a/libcommon/Makefile b/libcommon/Makefile index 3314549..932ee90 100644 --- a/libcommon/Makefile +++ b/libcommon/Makefile @@ -59,7 +59,10 @@ LIBS+= \ endif PROGSRC=\ - mime.cpp + file.cpp \ + mime.cpp \ + stringutil.cpp \ + tempfile.cpp TESTSRC=\ test-webserver.cpp \ diff --git a/libcommon/file.cpp b/libcommon/file.cpp new file mode 100644 index 0000000..47ab8be --- /dev/null +++ b/libcommon/file.cpp @@ -0,0 +1,46 @@ +#include "file.h" + +#include + +namespace fs = std::filesystem; + +using namespace std::string_literals; + +std::string File::getFile(const fs::path& filename) +{ + std::ifstream file(filename.string(), std::ios::in | std::ios::binary | std::ios::ate); + + if (file.is_open()) { + std::ifstream::pos_type fileSize = file.tellg(); + file.seekg(0, std::ios::beg); + + std::string bytes(fileSize, ' '); + file.read(reinterpret_cast(bytes.data()), fileSize); + + return bytes; + + } else { + throw std::runtime_error("Opening "s + filename.string() + " for reading"); + } +} + +void File::setFile(const fs::path& filename, const std::string& s) +{ + File::setFile(filename, s.data(), s.size()); +} + +void File::setFile(const fs::path& filename, const char* data, size_t size) +{ + std::ofstream file(filename.string(), std::ios::out | std::ios::binary); + if (file.is_open()) { + file.write(data, size); + } else { + throw std::runtime_error("Opening "s + filename.string() + " for writing"); + } +} + +void File::setFile(const fs::path& filename, const std::vector& data) +{ + File::setFile(filename, reinterpret_cast(data.data()), data.size()); +} + diff --git a/libcommon/file.h b/libcommon/file.h new file mode 100644 index 0000000..e7e4cf6 --- /dev/null +++ b/libcommon/file.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#include +#include + +namespace File { + +std::string getFile(const std::filesystem::path& filename); +void setFile(const std::filesystem::path& filename, const std::string& s); +void setFile(const std::filesystem::path& filename, const char* data, size_t size); +void setFile(const std::filesystem::path& filename, const std::vector& data); + +} diff --git a/libcommon/stringutil.cpp b/libcommon/stringutil.cpp new file mode 100644 index 0000000..f87fa00 --- /dev/null +++ b/libcommon/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/libcommon/stringutil.h b/libcommon/stringutil.h new file mode 100644 index 0000000..5110e2e --- /dev/null +++ b/libcommon/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); diff --git a/plugins/webbox/Makefile b/plugins/webbox/Makefile index 1c49fda..4794e72 100644 --- a/plugins/webbox/Makefile +++ b/plugins/webbox/Makefile @@ -60,8 +60,6 @@ endif LDFLAGS+=-L../../libcommon PROGSRC=\ - file.cpp \ - stringutil.cpp \ webbox.cpp TESTSRC=\ diff --git a/plugins/webbox/file.cpp b/plugins/webbox/file.cpp deleted file mode 100644 index 47ab8be..0000000 --- a/plugins/webbox/file.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "file.h" - -#include - -namespace fs = std::filesystem; - -using namespace std::string_literals; - -std::string File::getFile(const fs::path& filename) -{ - std::ifstream file(filename.string(), std::ios::in | std::ios::binary | std::ios::ate); - - if (file.is_open()) { - std::ifstream::pos_type fileSize = file.tellg(); - file.seekg(0, std::ios::beg); - - std::string bytes(fileSize, ' '); - file.read(reinterpret_cast(bytes.data()), fileSize); - - return bytes; - - } else { - throw std::runtime_error("Opening "s + filename.string() + " for reading"); - } -} - -void File::setFile(const fs::path& filename, const std::string& s) -{ - File::setFile(filename, s.data(), s.size()); -} - -void File::setFile(const fs::path& filename, const char* data, size_t size) -{ - std::ofstream file(filename.string(), std::ios::out | std::ios::binary); - if (file.is_open()) { - file.write(data, size); - } else { - throw std::runtime_error("Opening "s + filename.string() + " for writing"); - } -} - -void File::setFile(const fs::path& filename, const std::vector& data) -{ - File::setFile(filename, reinterpret_cast(data.data()), data.size()); -} - diff --git a/plugins/webbox/file.h b/plugins/webbox/file.h deleted file mode 100644 index e7e4cf6..0000000 --- a/plugins/webbox/file.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace File { - -std::string getFile(const std::filesystem::path& filename); -void setFile(const std::filesystem::path& filename, const std::string& s); -void setFile(const std::filesystem::path& filename, const char* data, size_t size); -void setFile(const std::filesystem::path& filename, const std::vector& data); - -} diff --git a/plugins/webbox/stringutil.cpp b/plugins/webbox/stringutil.cpp deleted file mode 100644 index f87fa00..0000000 --- a/plugins/webbox/stringutil.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#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/plugins/webbox/stringutil.h b/plugins/webbox/stringutil.h deleted file mode 100644 index 5110e2e..0000000 --- a/plugins/webbox/stringutil.h +++ /dev/null @@ -1,10 +0,0 @@ -#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); diff --git a/plugins/webbox/webbox.cpp b/plugins/webbox/webbox.cpp index 7de431a..01241a9 100644 --- a/plugins/webbox/webbox.cpp +++ b/plugins/webbox/webbox.cpp @@ -1,9 +1,10 @@ #include "webbox.h" -#include "file.h" -#include "stringutil.h" - #include "libcommon/mime.h" +#include "libcommon/tempfile.h" +#include "libcommon/file.h" +#include "libcommon/stringutil.h" + #include #include @@ -36,34 +37,6 @@ namespace { static const std::string STATIC_HTML_TARGET{"webbox-html/"}; static const fs::path STATIC_HTML_DOC_ROOT{"/usr/lib/webbox/html"}; - // TODO: separate out - class Tempfile - { - fs::path m_path; - - public: - fs::path GetPath() const - { - return m_path; - } - - Tempfile() { - try { - m_path = std::string{tmpnam(NULL)} + ".zip"s; - } catch (const std::exception& ex) { - throw std::runtime_error("Tempfile error: "s + ex.what()); - } - } - - ~Tempfile() { - try { - fs::remove_all(m_path); - } catch (const std::exception& ex) { - std::cerr << "Warning: Couldn't remove temporary file " << m_path << std::endl; - } - } - }; - std::unordered_map status_map { { "301", "Moved Permanently" }, { "400", "Bad Request"}, diff --git a/plugins/weblog/Makefile b/plugins/weblog/Makefile index b9f278a..b660995 100644 --- a/plugins/weblog/Makefile +++ b/plugins/weblog/Makefile @@ -60,7 +60,6 @@ endif LDFLAGS=-L../../libcommon PROGSRC=\ - stringutil.cpp \ weblog.cpp TESTSRC=\ diff --git a/plugins/weblog/stringutil.cpp b/plugins/weblog/stringutil.cpp deleted file mode 100644 index f87fa00..0000000 --- a/plugins/weblog/stringutil.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#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/plugins/weblog/stringutil.h b/plugins/weblog/stringutil.h deleted file mode 100644 index 5110e2e..0000000 --- a/plugins/weblog/stringutil.h +++ /dev/null @@ -1,10 +0,0 @@ -#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); diff --git a/plugins/weblog/weblog.cpp b/plugins/weblog/weblog.cpp index 4a14799..eb02bb2 100644 --- a/plugins/weblog/weblog.cpp +++ b/plugins/weblog/weblog.cpp @@ -1,8 +1,7 @@ #include "weblog.h" -#include "stringutil.h" - #include "libcommon/mime.h" +#include "libcommon/stringutil.h" #include #include diff --git a/response.cpp b/response.cpp index 4c76bb3..a6f55e2 100644 --- a/response.cpp +++ b/response.cpp @@ -2,10 +2,10 @@ #include "auth.h" #include "base64.h" -#include "file.h" #include "os.h" #include "libcommon/mime.h" +#include "libcommon/file.h" #include -- cgit v1.2.3