From fead940d102d1ea3a2c437f5b8f46846796be305 Mon Sep 17 00:00:00 2001 From: hagen Date: Wed, 27 Apr 2016 00:00:00 +0000 Subject: [PATCH] * HTTPServer.{cpp,h}: * move query parsing code to one place * use /?cmd=X instead /?X * unified handler signatures --- HTTPServer.cpp | 108 ++++++++++++++++++------------------------------- HTTPServer.h | 33 ++++++++------- 2 files changed, 55 insertions(+), 86 deletions(-) diff --git a/HTTPServer.cpp b/HTTPServer.cpp index 8b9066f3..0f8eacc0 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -254,26 +254,9 @@ namespace http { } if (ret == 0) return; /* need more data */ - HandleRequest (request.uri); + HandleRequest (request); } - void HTTPConnection::ExtractParams (const std::string& str, std::map& params) - { - if (str[0] != '&') return; - size_t pos = 1, end; - do - { - end = str.find ('&', pos); - std::string param = str.substr (pos, end - pos); - LogPrint (eLogDebug, "HTTPServer: extracted parameters: ", param); - size_t e = param.find ('='); - if (e != std::string::npos) - params[param.substr(0, e)] = param.substr(e+1); - pos = end + 1; - } - while (end != std::string::npos); - } - void HTTPConnection::HandleWriteReply (const boost::system::error_code& ecode) { if (ecode != boost::asio::error::operation_aborted) @@ -295,7 +278,7 @@ namespace http { AsyncStreamReceive (); } - void HTTPConnection::HandleRequest (const std::string& address) + void HTTPConnection::HandleRequest (const HTTPReq &request) { std::stringstream s; // Html5 head start @@ -322,22 +305,22 @@ namespace http { s << "
"; s << "
\r\n"; s << "Main page
\r\n
\r\n"; - s << "Local destinations
\r\n"; - s << "Tunnels
\r\n"; - s << "Transit tunnels
\r\n"; - s << "Transports
\r\n
\r\n"; - s << "I2P tunnels
\r\n"; + s << "Local destinations
\r\n"; + s << "Tunnels
\r\n"; + s << "Transit tunnels
\r\n"; + s << "Transports
\r\n
\r\n"; + s << "I2P tunnels
\r\n"; if (i2p::client::context.GetSAMBridge ()) - s << "SAM sessions
\r\n
\r\n"; + s << "SAM sessions
\r\n
\r\n"; if (i2p::context.AcceptsTunnels ()) - s << "Stop accepting tunnels
\r\n
\r\n"; + s << "Stop accepting tunnels
\r\n
\r\n"; else - s << "Start accepting tunnels
\r\n
\r\n"; - s << "Run peer test
\r\n
\r\n"; - s << "Jump services
\r\n
\r\n"; + s << "Start accepting tunnels
\r\n
\r\n"; + s << "Run peer test
\r\n
\r\n"; + s << "Jump services
\r\n
\r\n"; s << "
"; - if (address.length () > 1) - HandleCommand (address.substr (2), s); + if (request.uri.find("cmd=") != std::string::npos) + HandleCommand (s, request.uri); else FillContent (s); s << "
\r\n\r\n"; @@ -413,21 +396,23 @@ namespace http { s << "Transit Tunnels: " << std::to_string(transitTunnelCount) << "
\r\n"; } - void HTTPConnection::HandleCommand (const std::string& command, std::stringstream& s) + void HTTPConnection::HandleCommand (std::stringstream& s, const std::string & uri) { - size_t paramsPos = command.find('&'); - std::string cmd = command.substr (0, paramsPos); + std::map params; + std::string cmd(""); + URL url; + + url.parse(uri); + url.parse_query(params); + cmd = params["cmd"]; + if (cmd == HTTP_COMMAND_TRANSPORTS) ShowTransports (s); else if (cmd == HTTP_COMMAND_TUNNELS) ShowTunnels (s); else if (cmd == HTTP_COMMAND_JUMPSERVICES) - { - std::map params; - ExtractParams (command.substr (paramsPos), params); - auto address = params[HTTP_PARAM_ADDRESS]; - ShowJumpServices (address, s); - } else if (cmd == HTTP_COMMAND_TRANSIT_TUNNELS) + ShowJumpServices (s, params["address"]); + else if (cmd == HTTP_COMMAND_TRANSIT_TUNNELS) ShowTransitTunnels (s); else if (cmd == HTTP_COMMAND_START_ACCEPTING_TUNNELS) StartAcceptingTunnels (s); @@ -438,26 +423,16 @@ namespace http { else if (cmd == HTTP_COMMAND_LOCAL_DESTINATIONS) ShowLocalDestinations (s); else if (cmd == HTTP_COMMAND_LOCAL_DESTINATION) - { - std::map params; - ExtractParams (command.substr (paramsPos), params); - auto b32 = params[HTTP_PARAM_BASE32_ADDRESS]; - ShowLocalDestination (b32, s); - } + ShowLocalDestination (s, params["b32"]); else if (cmd == HTTP_COMMAND_SAM_SESSIONS) ShowSAMSessions (s); else if (cmd == HTTP_COMMAND_SAM_SESSION) - { - std::map params; - ExtractParams (command.substr (paramsPos), params); - auto id = params[HTTP_PARAM_SAM_SESSION_ID]; - ShowSAMSession (id, s); - } + ShowSAMSession (s, params["sam_id"]); else if (cmd == HTTP_COMMAND_I2P_TUNNELS) ShowI2PTunnels (s); } - void HTTPConnection::ShowJumpServices (const std::string& address, std::stringstream& s) + void HTTPConnection::ShowJumpServices (std::stringstream& s, const std::string& address) { s << "
"; s << ""; @@ -465,7 +440,7 @@ namespace http { s << "Jump services for " << address << ""; s << ""; - } + } void HTTPConnection::ShowLocalDestinations (std::stringstream& s) { @@ -473,13 +448,12 @@ namespace http { for (auto& it: i2p::client::context.GetDestinations ()) { auto ident = it.second->GetIdentHash ();; - s << ""; + s << ""; s << i2p::client::context.GetAddressBook ().ToAddress(ident) << "
\r\n" << std::endl; } } - void HTTPConnection::ShowLocalDestination (const std::string& b32, std::stringstream& s) + void HTTPConnection::ShowLocalDestination (std::stringstream& s, const std::string& b32) { s << "Local Destination:
\r\n
\r\n"; i2p::data::IdentHash ident; @@ -666,14 +640,13 @@ namespace http { { for (auto& it: sam->GetSessions ()) { - s << ""; + s << ""; s << it.first << "
\r\n" << std::endl; } } } - void HTTPConnection::ShowSAMSession (const std::string& id, std::stringstream& s) + void HTTPConnection::ShowSAMSession (std::stringstream& s, const std::string& id) { s << "SAM Session:
\r\n
\r\n"; auto sam = i2p::client::context.GetSAMBridge (); @@ -683,8 +656,7 @@ namespace http { if (session) { auto& ident = session->localDestination->GetIdentHash(); - s << ""; + s << ""; s << i2p::client::context.GetAddressBook ().ToAddress(ident) << "
\r\n" << std::endl; s << "Streams:
\r\n"; for (auto it: session->ListSockets()) @@ -716,8 +688,7 @@ namespace http { for (auto& it: i2p::client::context.GetClientTunnels ()) { auto& ident = it.second->GetLocalDestination ()->GetIdentHash(); - s << ""; + s << ""; s << it.second->GetName () << " ⇐ "; s << i2p::client::context.GetAddressBook ().ToAddress(ident); s << "
\r\n"<< std::endl; @@ -726,8 +697,7 @@ namespace http { for (auto& it: i2p::client::context.GetServerTunnels ()) { auto& ident = it.second->GetLocalDestination ()->GetIdentHash(); - s << ""; + s << ""; s << it.second->GetName () << " ⇒ "; s << i2p::client::context.GetAddressBook ().ToAddress(ident); s << ":" << it.second->GetLocalPort (); @@ -781,14 +751,14 @@ namespace http { } } - void HTTPConnection::SendReply (const std::string& content, int status) + void HTTPConnection::SendReply (const std::string& content, int code) { std::time_t time_now = std::time(nullptr); char time_buff[128]; std::strftime(time_buff, sizeof(time_buff), "%a, %d %b %Y %H:%M:%S GMT", std::gmtime(&time_now)); HTTPRes reply; - reply.code = status; - reply.status = HTTPCodeToStatus(status); + reply.code = code; + reply.status = HTTPCodeToStatus(code); reply.headers.insert(std::pair("Date", time_buff)); reply.headers.insert(std::pair("Content-Type", "text/html")); reply.headers.insert(std::pair("Content-Length", std::to_string(content.size()))); diff --git a/HTTPServer.h b/HTTPServer.h index 1aa781de..89df8fb5 100644 --- a/HTTPServer.h +++ b/HTTPServer.h @@ -25,27 +25,26 @@ namespace http { void HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); void HandleWriteReply(const boost::system::error_code& ecode); void HandleWrite (const boost::system::error_code& ecode); - void SendReply (const std::string& content, int status = 200); + void SendReply (const std::string& content, int code = 200); void SendError (const std::string& message); - void HandleRequest (const std::string& address); - void HandleCommand (const std::string& command, std::stringstream& s); - void ShowJumpServices (const std::string& address, std::stringstream& s); - void ShowTransports (std::stringstream& s); - void ShowTunnels (std::stringstream& s); - void ShowTransitTunnels (std::stringstream& s); + void HandleRequest (const HTTPReq & request); + void HandleCommand (std::stringstream& s, const std::string& request); + + void ShowJumpServices (std::stringstream& s, const std::string& address); + void ShowTransports (std::stringstream& s); + void ShowTunnels (std::stringstream& s); + void ShowTransitTunnels (std::stringstream& s); void ShowLocalDestinations (std::stringstream& s); - void ShowLocalDestination (const std::string& b32, std::stringstream& s); - void ShowSAMSessions (std::stringstream& s); - void ShowSAMSession (const std::string& id, std::stringstream& s); - void ShowI2PTunnels (std::stringstream& s); + void ShowLocalDestination (std::stringstream& s, const std::string& b32); + void ShowSAMSessions (std::stringstream& s); + void ShowSAMSession (std::stringstream& s, const std::string& id); + void ShowI2PTunnels (std::stringstream& s); void StartAcceptingTunnels (std::stringstream& s); - void StopAcceptingTunnels (std::stringstream& s); - void RunPeerTest (std::stringstream& s); - void FillContent (std::stringstream& s); - std::string ExtractAddress (); - void ExtractParams (const std::string& str, std::map& params); - + void StopAcceptingTunnels (std::stringstream& s); + void RunPeerTest (std::stringstream& s); + void FillContent (std::stringstream& s); + protected: std::shared_ptr m_Socket;