Archived
1
0
This commit is contained in:
Jeff Becker 2018-05-03 13:38:35 -04:00
parent 4df3bc0672
commit 0c41298fe0
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
9 changed files with 39 additions and 43 deletions

View File

@ -47,7 +47,7 @@ public:
private: private:
size_t conns; size_t conns;
int epollfd; int epollfd;
char readbuf[1024]; char readbuf[2048];
}; };
} }

View File

@ -2,6 +2,8 @@
#define NNTPCHAN_LINE_HPP #define NNTPCHAN_LINE_HPP
#include "server.hpp" #include "server.hpp"
#include <stdint.h> #include <stdint.h>
#include <sstream>
namespace nntpchan namespace nntpchan
{ {
@ -19,11 +21,12 @@ public:
protected: protected:
/** @brief handle a line from the client */ /** @brief handle a line from the client */
virtual void HandleLine(const std::string &line) = 0; virtual void HandleLine(const std::string line) = 0;
private: 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; bool m_close;
const size_t lineLimit; const size_t lineLimit;
}; };

View File

@ -35,7 +35,7 @@ protected:
void SetStream(std::istream *i); void SetStream(std::istream *i);
std::string Hash(const std::string &data, const std::string &salt); std::string Hash(const std::string &data, const std::string &salt);
void HandleLine(const std::string &line); void HandleLine(const std::string line);
private: private:
bool ProcessLine(const std::string &line); bool ProcessLine(const std::string &line);

View File

@ -11,7 +11,7 @@ namespace nntpchan
class NNTPServerHandler : public LineReader, public IConnHandler class NNTPServerHandler : public LineReader, public IConnHandler
{ {
public: public:
NNTPServerHandler(const fs::path &storage); NNTPServerHandler(fs::path storage);
~NNTPServerHandler(); ~NNTPServerHandler();
virtual bool ShouldClose(); virtual bool ShouldClose();
@ -23,7 +23,7 @@ public:
void Greet(); void Greet();
protected: protected:
void HandleLine(const std::string &line); void HandleLine(const std::string line);
void HandleCommand(const std::deque<std::string> &command); void HandleCommand(const std::deque<std::string> &command);
private: private:
@ -51,7 +51,7 @@ private:
std::string m_articleName; std::string m_articleName;
FileHandle_ptr m_article; FileHandle_ptr m_article;
CredDB_ptr m_auth; CredDB_ptr m_auth;
ArticleStorage_ptr m_store; ArticleStorage m_store;
std::string m_mode; std::string m_mode;
bool m_authed; bool m_authed;
State m_state; State m_state;

View File

@ -107,6 +107,7 @@ void Mainloop::Run()
idx = 0; idx = 0;
while(idx < res) while(idx < res)
{ {
errno = 0;
ev = &evs[idx++]; ev = &evs[idx++];
if(ev->data.fd == sfd) if(ev->data.fd == sfd)
{ {

View File

@ -9,37 +9,20 @@ void LineReader::Data(const char *data, ssize_t l)
{ {
if (l <= 0) if (l <= 0)
return; return;
// process leftovers m_line << m_leftover;
std::size_t idx = 0; m_leftover = "";
std::size_t pos = 0; m_line << std::string(data, l);
while (l-- > 0)
{
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++;
}
}
}
void LineReader::OnLine(const char *d, const size_t l) for(std::string line; std::getline(m_line, line); )
{ {
std::string line; line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
line += std::string(d, l);
HandleLine(line); HandleLine(line);
} }
if(m_line)
m_leftover = m_line.str();
m_line.clear();
}
bool LineReader::ShouldClose() { return m_close; } bool LineReader::ShouldClose() { return m_close; }
} }

View File

@ -55,7 +55,7 @@ bool HashedCredDB::ProcessLine(const std::string &line)
return Hash(m_passwd, salt) == cred; return Hash(m_passwd, salt) == cred;
} }
void HashedCredDB::HandleLine(const std::string &line) void HashedCredDB::HandleLine(const std::string line)
{ {
if (m_found) if (m_found)
return; return;

View File

@ -9,15 +9,15 @@
namespace nntpchan namespace nntpchan
{ {
NNTPServerHandler::NNTPServerHandler(const fs::path &storage) NNTPServerHandler::NNTPServerHandler(fs::path storage)
: LineReader(1024), m_article(nullptr), m_auth(nullptr), m_store(std::make_unique<ArticleStorage>(storage)), : LineReader(1024), m_article(nullptr), m_auth(nullptr), m_store(storage),
m_authed(false), m_state(eStateReadCommand) m_authed(false), m_state(eStateReadCommand)
{ {
} }
NNTPServerHandler::~NNTPServerHandler() {} NNTPServerHandler::~NNTPServerHandler() {}
void NNTPServerHandler::HandleLine(const std::string &line) void NNTPServerHandler::HandleLine(const std::string line)
{ {
if (m_state == eStateReadCommand) if (m_state == eStateReadCommand)
{ {
@ -51,6 +51,12 @@ void NNTPServerHandler::OnData(const char *data, ssize_t l)
return; return;
if (m_state == eStateStoreArticle) 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"); const char *end = strstr(data, "\r\n.\r\n");
if (end) if (end)
{ {
@ -124,7 +130,7 @@ void NNTPServerHandler::HandleCommand(const std::deque<std::string> &command)
if (cmdlen >= 2) if (cmdlen >= 2)
{ {
const std::string &msgid = command[1]; const std::string &msgid = command[1];
if (IsValidMessageID(msgid) && m_store->Accept(msgid)) if (IsValidMessageID(msgid) && m_store.Accept(msgid))
{ {
QueueLine("238 " + msgid); QueueLine("238 " + msgid);
} }
@ -139,9 +145,9 @@ void NNTPServerHandler::HandleCommand(const std::deque<std::string> &command)
if (cmdlen >= 2) if (cmdlen >= 2)
{ {
const std::string &msgid = command[1]; 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; m_articleName = msgid;
EnterState(eStateStoreArticle); EnterState(eStateStoreArticle);

View File

@ -20,6 +20,7 @@ void ArticleStorage::SetPath(const fs::path &fpath)
fs::create_directories(basedir); fs::create_directories(basedir);
assert(init_skiplist(posts_skiplist_dir)); assert(init_skiplist(posts_skiplist_dir));
assert(init_skiplist(threads_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)) if (!IsValidMessageID(msgid))
return false; return false;
auto p = MessagePath(msgid); 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; } fs::path ArticleStorage::MessagePath(const std::string &msgid) const { return basedir / msgid; }