Browse Source

* HTTPServer.{cpp,h}: change page/cmd processing flow

pull/489/head
hagen 9 years ago
parent
commit
80e37df012
  1. 1
      Daemon.cpp
  2. 55
      HTTPServer.cpp
  3. 10
      HTTPServer.h

1
Daemon.cpp

@ -13,6 +13,7 @@
#include "RouterInfo.h" #include "RouterInfo.h"
#include "RouterContext.h" #include "RouterContext.h"
#include "Tunnel.h" #include "Tunnel.h"
#include "HTTP.h"
#include "NetDb.h" #include "NetDb.h"
#include "Garlic.h" #include "Garlic.h"
#include "Streaming.h" #include "Streaming.h"

55
HTTPServer.cpp

@ -302,6 +302,11 @@ namespace http {
"</html>\r\n"; "</html>\r\n";
} }
void ShowError(std::stringstream& s, const std::string& string)
{
s << "<b>ERROR:</b>&nbsp;" << string << "<br>\r\n";
}
void ShowStatus (std::stringstream& s) void ShowStatus (std::stringstream& s)
{ {
s << "<b>Uptime:</b> "; s << "<b>Uptime:</b> ";
@ -670,7 +675,7 @@ namespace http {
} }
if (ret == 0) if (ret == 0)
return; /* need more data */ return; /* need more data */
HandleRequest (request.uri); HandleRequest (request);
} }
void HTTPConnection::Terminate (const boost::system::error_code& ecode) void HTTPConnection::Terminate (const boost::system::error_code& ecode)
@ -682,28 +687,31 @@ namespace http {
m_Socket->close (); m_Socket->close ();
} }
void HTTPConnection::HandleRequest (const std::string &uri) void HTTPConnection::HandleRequest (const HTTPReq & req)
{ {
std::stringstream s; std::stringstream s;
std::string content;
HTTPRes res;
// Html5 head start // Html5 head start
ShowPageHead (s); ShowPageHead (s);
if (uri.find("page=") != std::string::npos) if (req.uri.find("page=") != std::string::npos)
HandlePage (s, uri); HandlePage (req, res, s);
else if (uri.find("cmd=") != std::string::npos) else if (req.uri.find("cmd=") != std::string::npos)
HandleCommand (s, uri); HandleCommand (req, res, s);
else else
ShowStatus (s); ShowStatus (s);
ShowPageTail (s); ShowPageTail (s);
SendReply (s.str ()); content = s.str ();
SendReply (res, content);
} }
void HTTPConnection::HandlePage (std::stringstream& s, const std::string & uri) void HTTPConnection::HandlePage (const HTTPReq& req, HTTPRes& res, std::stringstream& s)
{ {
std::map<std::string, std::string> params; std::map<std::string, std::string> params;
std::string page(""); std::string page("");
URL url; URL url;
url.parse(uri); url.parse(req.uri);
url.parse_query(params); url.parse_query(params);
page = params["page"]; page = params["page"];
@ -727,17 +735,20 @@ namespace http {
ShowSAMSession (s, params["sam_id"]); ShowSAMSession (s, params["sam_id"]);
else if (page == HTTP_PAGE_I2P_TUNNELS) else if (page == HTTP_PAGE_I2P_TUNNELS)
ShowI2PTunnels (s); ShowI2PTunnels (s);
else else {
SendError("Unknown page: " + page); res.code = 400;
ShowError(s, "Unknown page: " + page);
return;
}
} }
void HTTPConnection::HandleCommand (std::stringstream& s, const std::string & uri) void HTTPConnection::HandleCommand (const HTTPReq& req, HTTPRes& res, std::stringstream& s)
{ {
std::map<std::string, std::string> params; std::map<std::string, std::string> params;
std::string cmd(""); std::string cmd("");
URL url; URL url;
url.parse(uri); url.parse(req.uri);
url.parse_query(params); url.parse_query(params);
cmd = params["cmd"]; cmd = params["cmd"];
@ -748,21 +759,20 @@ namespace http {
else if (cmd == HTTP_COMMAND_STOP_ACCEPTING_TUNNELS) else if (cmd == HTTP_COMMAND_STOP_ACCEPTING_TUNNELS)
i2p::context.SetAcceptsTunnels (false); i2p::context.SetAcceptsTunnels (false);
else { else {
SendError("Unknown command: " + cmd); res.code = 400;
ShowError(s, "Unknown command: " + cmd);
return; return;
} }
s << "<b>Command accepted</b><br><br>\r\n"; s << "<b>SUCCESS</b>:&nbsp;Command accepted<br><br>\r\n";
s << "<a href=\"/?page=commands\">Back to commands list</a>"; s << "<a href=\"/?page=commands\">Back to commands list</a>";
} }
void HTTPConnection::SendReply (const std::string& content, int code) void HTTPConnection::SendReply (HTTPRes& reply, std::string& content)
{ {
std::time_t time_now = std::time(nullptr); std::time_t time_now = std::time(nullptr);
char time_buff[128]; char time_buff[128];
std::strftime(time_buff, sizeof(time_buff), "%a, %d %b %Y %H:%M:%S GMT", std::gmtime(&time_now)); std::strftime(time_buff, sizeof(time_buff), "%a, %d %b %Y %H:%M:%S GMT", std::gmtime(&time_now));
HTTPRes reply; reply.status = HTTPCodeToStatus(reply.code);
reply.code = code;
reply.status = HTTPCodeToStatus(code);
reply.headers.insert(std::pair<std::string, std::string>("Date", time_buff)); reply.headers.insert(std::pair<std::string, std::string>("Date", time_buff));
reply.headers.insert(std::pair<std::string, std::string>("Content-Type", "text/html")); reply.headers.insert(std::pair<std::string, std::string>("Content-Type", "text/html"));
reply.headers.insert(std::pair<std::string, std::string>("Content-Length", std::to_string(content.size()))); reply.headers.insert(std::pair<std::string, std::string>("Content-Length", std::to_string(content.size())));
@ -777,13 +787,6 @@ namespace http {
std::bind (&HTTPConnection::Terminate, shared_from_this (), std::placeholders::_1)); std::bind (&HTTPConnection::Terminate, shared_from_this (), std::placeholders::_1));
} }
void HTTPConnection::SendError(const std::string& content)
{
std::stringstream ss;
ss << "<html>" << itoopieImage << "<br>\r\n" << content << "</html>";
SendReply (ss.str(), 504);
}
HTTPServer::HTTPServer (const std::string& address, int port): HTTPServer::HTTPServer (const std::string& address, int port):
m_Thread (nullptr), m_Work (m_Service), m_Thread (nullptr), m_Work (m_Service),
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::address::from_string(address), port)) m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::address::from_string(address), port))

10
HTTPServer.h

@ -21,13 +21,11 @@ namespace http {
void HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); void HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
void Terminate (const boost::system::error_code& ecode); void Terminate (const boost::system::error_code& ecode);
void SendReply (const std::string& content, int code = 200);
void SendError (const std::string& message);
void RunRequest (); void RunRequest ();
void HandleRequest (const std::string& uri); void HandleRequest (const HTTPReq & req);
void HandlePage (std::stringstream& s, const std::string& request); void HandlePage (const HTTPReq & req, HTTPRes & res, std::stringstream& data);
void HandleCommand (std::stringstream& s, const std::string& request); void HandleCommand (const HTTPReq & req, HTTPRes & res, std::stringstream& data);
void SendReply (HTTPRes & res, std::string & content);
private: private:

Loading…
Cancel
Save