mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-04-30 04:37:50 +02:00
Added basic webui (layout from l-n-s).
This commit is contained in:
parent
e7350a3af4
commit
719bfbc89b
8 changed files with 512 additions and 16 deletions
|
@ -35,6 +35,14 @@ const char HTTP_COMMAND_SAM_SESSIONS[] = "sam_sessions";
|
|||
const char HTTP_COMMAND_SAM_SESSION[] = "sam_session";
|
||||
const char HTTP_PARAM_SAM_SESSION_ID[] = "id";
|
||||
|
||||
HTTPConnection::HTTPConnection(boost::asio::ip::tcp::socket* socket,
|
||||
std::shared_ptr<i2p::client::I2PControlSession> session)
|
||||
: m_Socket(socket), m_Timer(socket->get_io_service()),
|
||||
m_BufferLen(0), m_Session(session)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void HTTPConnection::Terminate()
|
||||
{
|
||||
m_Socket->close();
|
||||
|
@ -62,8 +70,18 @@ void HTTPConnection::HandleReceive(const boost::system::error_code& e, std::size
|
|||
|
||||
void HTTPConnection::RunRequest()
|
||||
{
|
||||
m_Request = i2p::util::http::Request(std::string(m_Buffer, m_Buffer + m_BufferLen));
|
||||
HandleRequest();
|
||||
try {
|
||||
m_Request = i2p::util::http::Request(std::string(m_Buffer, m_Buffer + m_BufferLen));
|
||||
if(m_Request.getMethod() == "GET")
|
||||
return HandleRequest();
|
||||
if(m_Request.getHeader("Content-Type").find("application/json") != std::string::npos)
|
||||
return HandleI2PControlRequest();
|
||||
} catch(...) {
|
||||
// Ignore the error for now, probably Content-Type doesn't exist
|
||||
}
|
||||
// Unsupported method
|
||||
m_Reply = i2p::util::http::Response(502, "");
|
||||
SendReply();
|
||||
}
|
||||
|
||||
void HTTPConnection::ExtractParams(const std::string& str, std::map<std::string, std::string>& params)
|
||||
|
@ -119,6 +137,23 @@ void HTTPConnection::HandleRequest()
|
|||
ifs.close();
|
||||
|
||||
m_Reply = i2p::util::http::Response(200, str);
|
||||
|
||||
// TODO: get rid of this hack, actually determine the MIME type
|
||||
if(address.substr(address.find_last_of(".")) == ".css")
|
||||
m_Reply.setHeader("Content-Type", "text/css");
|
||||
else if(address.substr(address.find_last_of(".")) == ".js")
|
||||
m_Reply.setHeader("Content-Type", "text/javascript");
|
||||
else
|
||||
m_Reply.setHeader("Content-Type", "text/html");
|
||||
SendReply();
|
||||
}
|
||||
|
||||
void HTTPConnection::HandleI2PControlRequest()
|
||||
{
|
||||
std::stringstream ss(m_Request.getContent());
|
||||
const client::I2PControlSession::Response rsp = m_Session->handleRequest(ss);
|
||||
m_Reply = i2p::util::http::Response(200, rsp.toJsonString());
|
||||
m_Reply.setHeader("Content-Type", "application/json");
|
||||
SendReply();
|
||||
}
|
||||
|
||||
|
@ -141,7 +176,6 @@ void HTTPConnection::SendReply()
|
|||
if(std::strftime(time_buff, sizeof(time_buff), "%a, %d %b %Y %H:%M:%S GMT", std::gmtime(&time_now)) ) {
|
||||
m_Reply.setHeader("Date", std::string(time_buff));
|
||||
m_Reply.setContentLength();
|
||||
m_Reply.setHeader("Content-Type", "text/html");
|
||||
}
|
||||
boost::asio::async_write(
|
||||
*m_Socket, boost::asio::buffer(m_Reply.toString()),
|
||||
|
@ -153,7 +187,9 @@ HTTPServer::HTTPServer(const std::string& address, int port):
|
|||
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_NewSocket(nullptr)
|
||||
),
|
||||
m_NewSocket(nullptr),
|
||||
m_Session(std::make_shared<i2p::client::I2PControlSession>(m_Service))
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -167,11 +203,13 @@ void HTTPServer::Start()
|
|||
{
|
||||
m_Thread = new std::thread(std::bind(&HTTPServer::Run, this));
|
||||
m_Acceptor.listen();
|
||||
m_Session->start();
|
||||
Accept();
|
||||
}
|
||||
|
||||
void HTTPServer::Stop()
|
||||
{
|
||||
m_Session->stop();
|
||||
m_Acceptor.close();
|
||||
m_Service.stop();
|
||||
if(m_Thread)
|
||||
|
@ -196,18 +234,17 @@ void HTTPServer::Accept()
|
|||
|
||||
void HTTPServer::HandleAccept(const boost::system::error_code& ecode)
|
||||
{
|
||||
if(!ecode)
|
||||
{
|
||||
if(!ecode) {
|
||||
CreateConnection(m_NewSocket);
|
||||
Accept();
|
||||
}
|
||||
}
|
||||
|
||||
void HTTPServer::CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket)
|
||||
void HTTPServer::CreateConnection(boost::asio::ip::tcp::socket* m_NewSocket)
|
||||
{
|
||||
auto conn = std::make_shared<HTTPConnection>(m_NewSocket);
|
||||
auto conn = std::make_shared<HTTPConnection>(m_NewSocket, m_Session);
|
||||
conn->Receive();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue