From 0c41298fe0173533406649970d7e2ac817f152e5 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 3 May 2018 13:38:35 -0400 Subject: [PATCH] more --- .../include/nntpchan/event.hpp | 2 +- .../nntpchan-daemon/include/nntpchan/line.hpp | 9 +++-- .../include/nntpchan/nntp_auth.hpp | 2 +- .../include/nntpchan/nntp_handler.hpp | 6 +-- .../nntpchan-daemon/libnntpchan/event.cpp | 1 + .../nntpchan-daemon/libnntpchan/line.cpp | 37 +++++-------------- .../nntpchan-daemon/libnntpchan/nntp_auth.cpp | 2 +- .../libnntpchan/nntp_handler.cpp | 18 ++++++--- .../nntpchan-daemon/libnntpchan/storage.cpp | 5 ++- 9 files changed, 39 insertions(+), 43 deletions(-) diff --git a/contrib/backends/nntpchan-daemon/include/nntpchan/event.hpp b/contrib/backends/nntpchan-daemon/include/nntpchan/event.hpp index 9634825..dfa592e 100644 --- a/contrib/backends/nntpchan-daemon/include/nntpchan/event.hpp +++ b/contrib/backends/nntpchan-daemon/include/nntpchan/event.hpp @@ -47,7 +47,7 @@ public: private: size_t conns; int epollfd; - char readbuf[1024]; + char readbuf[2048]; }; } diff --git a/contrib/backends/nntpchan-daemon/include/nntpchan/line.hpp b/contrib/backends/nntpchan-daemon/include/nntpchan/line.hpp index ab013f7..3535785 100644 --- a/contrib/backends/nntpchan-daemon/include/nntpchan/line.hpp +++ b/contrib/backends/nntpchan-daemon/include/nntpchan/line.hpp @@ -2,6 +2,8 @@ #define NNTPCHAN_LINE_HPP #include "server.hpp" #include +#include + namespace nntpchan { @@ -19,11 +21,12 @@ public: protected: /** @brief handle a line from the client */ - virtual void HandleLine(const std::string &line) = 0; + virtual void HandleLine(const std::string line) = 0; private: - void OnLine(const char *d, const size_t l); - std::string m_leftovers; + + std::stringstream m_line; + std::string m_leftover; bool m_close; const size_t lineLimit; }; diff --git a/contrib/backends/nntpchan-daemon/include/nntpchan/nntp_auth.hpp b/contrib/backends/nntpchan-daemon/include/nntpchan/nntp_auth.hpp index dea5d0f..7b4246e 100644 --- a/contrib/backends/nntpchan-daemon/include/nntpchan/nntp_auth.hpp +++ b/contrib/backends/nntpchan-daemon/include/nntpchan/nntp_auth.hpp @@ -35,7 +35,7 @@ protected: void SetStream(std::istream *i); std::string Hash(const std::string &data, const std::string &salt); - void HandleLine(const std::string &line); + void HandleLine(const std::string line); private: bool ProcessLine(const std::string &line); diff --git a/contrib/backends/nntpchan-daemon/include/nntpchan/nntp_handler.hpp b/contrib/backends/nntpchan-daemon/include/nntpchan/nntp_handler.hpp index d8608d9..e08054c 100644 --- a/contrib/backends/nntpchan-daemon/include/nntpchan/nntp_handler.hpp +++ b/contrib/backends/nntpchan-daemon/include/nntpchan/nntp_handler.hpp @@ -11,7 +11,7 @@ namespace nntpchan class NNTPServerHandler : public LineReader, public IConnHandler { public: - NNTPServerHandler(const fs::path &storage); + NNTPServerHandler(fs::path storage); ~NNTPServerHandler(); virtual bool ShouldClose(); @@ -23,7 +23,7 @@ public: void Greet(); protected: - void HandleLine(const std::string &line); + void HandleLine(const std::string line); void HandleCommand(const std::deque &command); private: @@ -51,7 +51,7 @@ private: std::string m_articleName; FileHandle_ptr m_article; CredDB_ptr m_auth; - ArticleStorage_ptr m_store; + ArticleStorage m_store; std::string m_mode; bool m_authed; State m_state; diff --git a/contrib/backends/nntpchan-daemon/libnntpchan/event.cpp b/contrib/backends/nntpchan-daemon/libnntpchan/event.cpp index ae13777..7aabbe9 100644 --- a/contrib/backends/nntpchan-daemon/libnntpchan/event.cpp +++ b/contrib/backends/nntpchan-daemon/libnntpchan/event.cpp @@ -107,6 +107,7 @@ void Mainloop::Run() idx = 0; while(idx < res) { + errno = 0; ev = &evs[idx++]; if(ev->data.fd == sfd) { diff --git a/contrib/backends/nntpchan-daemon/libnntpchan/line.cpp b/contrib/backends/nntpchan-daemon/libnntpchan/line.cpp index d3b8022..39ad5ff 100644 --- a/contrib/backends/nntpchan-daemon/libnntpchan/line.cpp +++ b/contrib/backends/nntpchan-daemon/libnntpchan/line.cpp @@ -9,37 +9,20 @@ void LineReader::Data(const char *data, ssize_t l) { if (l <= 0) return; - // process leftovers - std::size_t idx = 0; - std::size_t pos = 0; - while (l-- > 0) + m_line << m_leftover; + m_leftover = ""; + m_line << std::string(data, l); + + for(std::string line; std::getline(m_line, line); ) { - char c = data[idx++]; - if (c == '\n') - { - OnLine(data, pos); - pos = 0; - data += idx; - } - else if (c == '\r' && data[idx] == '\n') - { - OnLine(data, pos); - data += idx + 1; - pos = 0; - } - else - { - pos++; - } + line.erase(std::remove(line.begin(), line.end(), '\r'), line.end()); + HandleLine(line); } + if(m_line) + m_leftover = m_line.str(); + m_line.clear(); } -void LineReader::OnLine(const char *d, const size_t l) -{ - std::string line; - line += std::string(d, l); - HandleLine(line); -} bool LineReader::ShouldClose() { return m_close; } } diff --git a/contrib/backends/nntpchan-daemon/libnntpchan/nntp_auth.cpp b/contrib/backends/nntpchan-daemon/libnntpchan/nntp_auth.cpp index 3339678..d48697e 100644 --- a/contrib/backends/nntpchan-daemon/libnntpchan/nntp_auth.cpp +++ b/contrib/backends/nntpchan-daemon/libnntpchan/nntp_auth.cpp @@ -55,7 +55,7 @@ bool HashedCredDB::ProcessLine(const std::string &line) return Hash(m_passwd, salt) == cred; } -void HashedCredDB::HandleLine(const std::string &line) +void HashedCredDB::HandleLine(const std::string line) { if (m_found) return; diff --git a/contrib/backends/nntpchan-daemon/libnntpchan/nntp_handler.cpp b/contrib/backends/nntpchan-daemon/libnntpchan/nntp_handler.cpp index 2a0d83d..c83d1b1 100644 --- a/contrib/backends/nntpchan-daemon/libnntpchan/nntp_handler.cpp +++ b/contrib/backends/nntpchan-daemon/libnntpchan/nntp_handler.cpp @@ -9,15 +9,15 @@ namespace nntpchan { -NNTPServerHandler::NNTPServerHandler(const fs::path &storage) - : LineReader(1024), m_article(nullptr), m_auth(nullptr), m_store(std::make_unique(storage)), +NNTPServerHandler::NNTPServerHandler(fs::path storage) + : LineReader(1024), m_article(nullptr), m_auth(nullptr), m_store(storage), m_authed(false), m_state(eStateReadCommand) { } NNTPServerHandler::~NNTPServerHandler() {} -void NNTPServerHandler::HandleLine(const std::string &line) +void NNTPServerHandler::HandleLine(const std::string line) { if (m_state == eStateReadCommand) { @@ -51,6 +51,12 @@ void NNTPServerHandler::OnData(const char *data, ssize_t l) return; if (m_state == eStateStoreArticle) { + std::cerr << "storing " << l << " bytes" << std::endl; + if(strncmp(data, ".\r\n", l) == 0) + { + ArticleObtained(); + return; + } const char *end = strstr(data, "\r\n.\r\n"); if (end) { @@ -124,7 +130,7 @@ void NNTPServerHandler::HandleCommand(const std::deque &command) if (cmdlen >= 2) { const std::string &msgid = command[1]; - if (IsValidMessageID(msgid) && m_store->Accept(msgid)) + if (IsValidMessageID(msgid) && m_store.Accept(msgid)) { QueueLine("238 " + msgid); } @@ -139,9 +145,9 @@ void NNTPServerHandler::HandleCommand(const std::deque &command) if (cmdlen >= 2) { const std::string &msgid = command[1]; - if (m_store->Accept(msgid)) + if (m_store.Accept(msgid)) { - m_article = m_store->OpenWrite(msgid); + m_article = m_store.OpenWrite(msgid); } m_articleName = msgid; EnterState(eStateStoreArticle); diff --git a/contrib/backends/nntpchan-daemon/libnntpchan/storage.cpp b/contrib/backends/nntpchan-daemon/libnntpchan/storage.cpp index f389f61..7f41343 100644 --- a/contrib/backends/nntpchan-daemon/libnntpchan/storage.cpp +++ b/contrib/backends/nntpchan-daemon/libnntpchan/storage.cpp @@ -20,6 +20,7 @@ void ArticleStorage::SetPath(const fs::path &fpath) fs::create_directories(basedir); assert(init_skiplist(posts_skiplist_dir)); assert(init_skiplist(threads_skiplist_dir)); + errno = 0; } @@ -41,7 +42,9 @@ bool ArticleStorage::Accept(const std::string &msgid) const if (!IsValidMessageID(msgid)) return false; auto p = MessagePath(msgid); - return !fs::exists(p); + bool ret = !fs::exists(p); + errno = 0; + return ret; } fs::path ArticleStorage::MessagePath(const std::string &msgid) const { return basedir / msgid; }