summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/anope.h6
-rw-r--r--modules/extra/httpd.h19
-rw-r--r--modules/extra/m_httpd.cpp176
3 files changed, 94 insertions, 107 deletions
diff --git a/include/anope.h b/include/anope.h
index 2dda6cf76..0ba950d9b 100644
--- a/include/anope.h
+++ b/include/anope.h
@@ -48,9 +48,10 @@ namespace Anope
string(char chr) : _string() { _string = chr; }
string(size_type n, char chr) : _string(n, chr) { }
string(const char *_str) : _string(_str) { }
+ string(const char *_str, size_type n) : _string(_str, n) { }
string(const std::string &_str) : _string(_str) { }
string(const ci::string &_str) : _string(_str.c_str()) { }
- string(const string &_str, size_type pos = 0, size_type n = npos) : _string(_str._string, pos, n) { }
+ string(const string &_str, size_type pos, size_type n = npos) : _string(_str._string, pos, n) { }
template <class InputIterator> string(InputIterator first, InputIterator last) : _string(first, last) { }
/**
@@ -152,12 +153,13 @@ namespace Anope
/**
* Trim leading and trailing white spaces from the string.
*/
- inline void trim()
+ inline string& trim()
{
while (!this->_string.empty() && isspace(this->_string[0]))
this->_string.erase(this->_string.begin());
while (!this->_string.empty() && isspace(this->_string[this->_string.length() - 1]))
this->_string.erase(this->_string.length() - 1);
+ return *this;
}
/**
diff --git a/modules/extra/httpd.h b/modules/extra/httpd.h
index bf6a9838d..015dacc28 100644
--- a/modules/extra/httpd.h
+++ b/modules/extra/httpd.h
@@ -107,7 +107,7 @@ class HTTPPage : public Base
virtual bool OnRequest(HTTPProvider *, const Anope::string &, HTTPClient *, HTTPMessage &, HTTPReply &) = 0;
};
-class HTTPClient : public ClientSocket, public BufferedSocket, public BinarySocket, public Base
+class HTTPClient : public ClientSocket, public BinarySocket, public Base
{
protected:
void WriteClient(const Anope::string &message)
@@ -116,28 +116,13 @@ class HTTPClient : public ClientSocket, public BufferedSocket, public BinarySock
}
public:
- HTTPClient(ListenSocket *l, int f, const sockaddrs &a) : ClientSocket(l, a), BufferedSocket(), BinarySocket() { }
+ HTTPClient(ListenSocket *l, int f, const sockaddrs &a) : ClientSocket(l, a), BinarySocket() { }
virtual const Anope::string GetIP()
{
return this->clientaddr.addr();
}
- bool ProcessRead() anope_override
- {
- return BufferedSocket::ProcessRead();
- }
-
- bool ProcessWrite() anope_override
- {
- return !BinarySocket::ProcessWrite() || BinarySocket::write_buffer.empty() ? false : true;
- }
-
- void Write(const char *buffer, size_t l) anope_override
- {
- BinarySocket::Write(buffer, l);
- }
-
virtual void SendError(HTTPError err, const Anope::string &msg) = 0;
virtual void SendReply(HTTPReply *) = 0;
};
diff --git a/modules/extra/m_httpd.cpp b/modules/extra/m_httpd.cpp
index 0176b955a..a44c6a28e 100644
--- a/modules/extra/m_httpd.cpp
+++ b/modules/extra/m_httpd.cpp
@@ -45,6 +45,9 @@ class MyHTTPClient : public HTTPClient
Reference<HTTPPage> page;
Anope::string ip;
+ unsigned content_length;
+ Anope::string post_data;
+
enum
{
ACTION_NONE,
@@ -91,7 +94,7 @@ class MyHTTPClient : public HTTPClient
public:
time_t created;
- MyHTTPClient(HTTPProvider *l, int f, const sockaddrs &a) : Socket(f, l->IsIPv6()), HTTPClient(l, f, a), provider(l), header_done(false), served(false), ip(a.addr()), action(ACTION_NONE), created(Anope::CurTime)
+ MyHTTPClient(HTTPProvider *l, int f, const sockaddrs &a) : Socket(f, l->IsIPv6()), HTTPClient(l, f, a), provider(l), header_done(false), served(false), ip(a.addr()), content_length(0), action(ACTION_NONE), created(Anope::CurTime)
{
Log(LOG_DEBUG, "httpd") << "Accepted connection " << f << " from " << a.addr();
}
@@ -106,119 +109,116 @@ class MyHTTPClient : public HTTPClient
return this->ip;
}
- bool Read(const Anope::string &buf) anope_override
+ bool Read(const char *buffer, size_t l) anope_override
{
- Log(LOG_DEBUG_2) << "HTTP from " << this->clientaddr.addr() << ": " << buf;
+ Anope::string buf(buffer, l);
if (!this->header_done)
{
- if (this->action == ACTION_NONE)
- {
- std::vector<Anope::string> params;
- spacesepstream(buf).GetTokens(params);
+ Anope::string token;
+ sepstream sep(buf, '\n');
+ while (sep.GetToken(token) && !token.trim().empty())
+ this->Read(token);
+ this->header_done = true;
+ buf = sep.GetRemaining();
+ }
- if (params.empty() || (params[0] != "GET" && params[0] != "POST"))
- {
- this->SendError(HTTP_BAD_REQUEST, "Unknown operation");
- return true;
- }
+ this->post_data += buf;
- if (params.size() != 3)
- {
- this->SendError(HTTP_BAD_REQUEST, "Invalid parameters");
- return true;
- }
+ if (this->post_data.length() >= this->content_length)
+ {
+ sepstream sep(this->post_data, '&');
+ Anope::string token;
+
+ while (sep.GetToken(token))
+ {
+ size_t sz = token.find('=');
+ if (sz == Anope::string::npos || !sz || sz + 1 >= token.length())
+ continue;
+ this->header.post_data[token.substr(0, sz)] = HTTPUtils::URLDecode(token.substr(sz + 1));
+ }
- if (params[0] == "GET")
- this->action = ACTION_GET;
- else if (params[0] == "POST")
- this->action = ACTION_POST;
+ this->Serve();
+ }
+
+ return true;
+ }
- Anope::string targ = params[1];
- size_t q = targ.find('?');
- if (q != Anope::string::npos)
- {
- sepstream sep(targ.substr(q + 1), '&');
- targ = targ.substr(0, q);
+ bool Read(const Anope::string &buf)
+ {
+ Log(LOG_DEBUG_2) << "HTTP from " << this->clientaddr.addr() << ": " << buf;
- Anope::string token;
- while (sep.GetToken(token))
- {
- size_t sz = token.find('=');
- if (sz == Anope::string::npos || !sz || sz + 1 >= token.length())
- continue;
- this->header.get_data[token.substr(0, sz)] = HTTPUtils::URLDecode(token.substr(sz + 1));
- }
- }
+ if (this->action == ACTION_NONE)
+ {
+ std::vector<Anope::string> params;
+ spacesepstream(buf).GetTokens(params);
- this->page = this->provider->FindPage(targ);
- this->page_name = targ;
+ if (params.empty() || (params[0] != "GET" && params[0] != "POST"))
+ {
+ this->SendError(HTTP_BAD_REQUEST, "Unknown operation");
+ return true;
}
- else if (buf.find("Cookie: ") == 0)
+
+ if (params.size() != 3)
{
- spacesepstream sep(buf.substr(8));
- Anope::string token;
+ this->SendError(HTTP_BAD_REQUEST, "Invalid parameters");
+ return true;
+ }
+
+ if (params[0] == "GET")
+ this->action = ACTION_GET;
+ else if (params[0] == "POST")
+ this->action = ACTION_POST;
+ Anope::string targ = params[1];
+ size_t q = targ.find('?');
+ if (q != Anope::string::npos)
+ {
+ sepstream sep(targ.substr(q + 1), '&');
+ targ = targ.substr(0, q);
+
+ Anope::string token;
while (sep.GetToken(token))
{
size_t sz = token.find('=');
if (sz == Anope::string::npos || !sz || sz + 1 >= token.length())
continue;
- size_t end = token.length() - (sz + 1);
- if (!sep.StreamEnd())
- --end; // Remove trailing ;
- this->header.cookies[token.substr(0, sz)] = token.substr(sz + 1, end);
+ this->header.get_data[token.substr(0, sz)] = HTTPUtils::URLDecode(token.substr(sz + 1));
}
}
- else if (buf.find(':') != Anope::string::npos)
+
+ this->page = this->provider->FindPage(targ);
+ this->page_name = targ;
+ }
+ else if (buf.find("Cookie: ") == 0)
+ {
+ spacesepstream sep(buf.substr(8));
+ Anope::string token;
+
+ while (sep.GetToken(token))
{
- size_t sz = buf.find(':');
- if (sz + 2 < buf.length())
- this->header.headers[buf.substr(0, sz)] = buf.substr(sz + 2);
+ size_t sz = token.find('=');
+ if (sz == Anope::string::npos || !sz || sz + 1 >= token.length())
+ continue;
+ size_t end = token.length() - (sz + 1);
+ if (!sep.StreamEnd())
+ --end; // Remove trailing ;
+ this->header.cookies[token.substr(0, sz)] = token.substr(sz + 1, end);
}
- else if (buf.empty())
+ }
+ else if (buf.find("Content-Length: ") == 0)
+ {
+ try
{
- this->header_done = true;
-
- if (this->action == ACTION_POST)
- {
- unsigned content_length = 0;
- if (this->header.headers.count("Content-Length"))
- try
- {
- content_length = convertTo<unsigned>(this->header.headers["Content-Length"]);
- }
- catch (const ConvertException &) { }
-
- if (this->extra_buf.length() == content_length)
- {
- Log(LOG_DEBUG_2) << "HTTP POST from " << this->clientaddr.addr() << ": " << this->extra_buf;
-
- sepstream sep(this->extra_buf, '&');
- Anope::string token;
-
- while (sep.GetToken(token))
- {
- size_t sz = token.find('=');
- if (sz == Anope::string::npos || !sz || sz + 1 >= token.length())
- continue;
- this->header.post_data[token.substr(0, sz)] = HTTPUtils::URLDecode(token.substr(sz + 1));
- }
-
- this->header.content = this->extra_buf;
- this->Serve();
- }
- }
- else
- this->Serve();
+ this->content_length = convertTo<unsigned>(buf.substr(16));
}
+ catch (const ConvertException &ex) { }
}
- else if (this->action == ACTION_POST)
+ else if (buf.find(':') != Anope::string::npos)
{
- if (buf.empty())
- this->Serve();
- else
- this->header.content += buf;
+ size_t sz = buf.find(':');
+ if (sz + 2 < buf.length())
+ this->header.headers[buf.substr(0, sz)] = buf.substr(sz + 2);
}
return true;