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:
size_t conns;
int epollfd;
char readbuf[1024];
char readbuf[2048];
};
}

View File

@ -2,6 +2,8 @@
#define NNTPCHAN_LINE_HPP
#include "server.hpp"
#include <stdint.h>
#include <sstream>
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;
};

View File

@ -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);

View File

@ -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<std::string> &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;

View File

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

View File

@ -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; }
}

View File

@ -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;

View File

@ -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<ArticleStorage>(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<std::string> &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<std::string> &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);

View File

@ -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; }