mirror of
https://github.com/anope/anope.git
synced 2026-07-03 09:13:12 +02:00
Rewrite/simplify some of m_httpd
This commit is contained in:
+4
-2
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+2
-17
@@ -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;
|
||||
};
|
||||
|
||||
+96
-96
@@ -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)
|
||||
Anope::string token;
|
||||
sepstream sep(buf, '\n');
|
||||
while (sep.GetToken(token) && !token.trim().empty())
|
||||
this->Read(token);
|
||||
this->header_done = true;
|
||||
buf = sep.GetRemaining();
|
||||
}
|
||||
|
||||
this->post_data += buf;
|
||||
|
||||
if (this->post_data.length() >= this->content_length)
|
||||
{
|
||||
sepstream sep(this->post_data, '&');
|
||||
Anope::string token;
|
||||
|
||||
while (sep.GetToken(token))
|
||||
{
|
||||
std::vector<Anope::string> params;
|
||||
spacesepstream(buf).GetTokens(params);
|
||||
|
||||
if (params.empty() || (params[0] != "GET" && params[0] != "POST"))
|
||||
{
|
||||
this->SendError(HTTP_BAD_REQUEST, "Unknown operation");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (params.size() != 3)
|
||||
{
|
||||
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;
|
||||
this->header.get_data[token.substr(0, sz)] = HTTPUtils::URLDecode(token.substr(sz + 1));
|
||||
}
|
||||
}
|
||||
|
||||
this->page = this->provider->FindPage(targ);
|
||||
this->page_name = targ;
|
||||
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));
|
||||
}
|
||||
else if (buf.find("Cookie: ") == 0)
|
||||
{
|
||||
spacesepstream sep(buf.substr(8));
|
||||
Anope::string token;
|
||||
|
||||
this->Serve();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Read(const Anope::string &buf)
|
||||
{
|
||||
Log(LOG_DEBUG_2) << "HTTP from " << this->clientaddr.addr() << ": " << buf;
|
||||
|
||||
if (this->action == ACTION_NONE)
|
||||
{
|
||||
std::vector<Anope::string> params;
|
||||
spacesepstream(buf).GetTokens(params);
|
||||
|
||||
if (params.empty() || (params[0] != "GET" && params[0] != "POST"))
|
||||
{
|
||||
this->SendError(HTTP_BAD_REQUEST, "Unknown operation");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (params.size() != 3)
|
||||
{
|
||||
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);
|
||||
}
|
||||
else if (buf.empty())
|
||||
{
|
||||
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();
|
||||
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 (this->action == ACTION_POST)
|
||||
else if (buf.find("Content-Length: ") == 0)
|
||||
{
|
||||
if (buf.empty())
|
||||
this->Serve();
|
||||
else
|
||||
this->header.content += buf;
|
||||
try
|
||||
{
|
||||
this->content_length = convertTo<unsigned>(buf.substr(16));
|
||||
}
|
||||
catch (const ConvertException &ex) { }
|
||||
}
|
||||
else if (buf.find(':') != Anope::string::npos)
|
||||
{
|
||||
size_t sz = buf.find(':');
|
||||
if (sz + 2 < buf.length())
|
||||
this->header.headers[buf.substr(0, sz)] = buf.substr(sz + 2);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user