i2pd/client/HTTPServer.cpp

265 lines
7.4 KiB
C++
Raw Normal View History

2013-12-10 17:03:22 +04:00
#include <boost/bind.hpp>
2015-08-11 23:55:55 +03:00
#include <ctime>
2015-09-06 21:34:50 +03:00
#include <fstream>
2015-07-30 16:34:56 +03:00
#include "util/Log.h"
2015-09-06 21:34:50 +03:00
#include "util/util.h"
2015-07-28 23:48:38 +03:00
#include "util/I2PEndian.h"
2014-09-30 21:34:29 +04:00
#include "HTTPServer.h"
2013-12-10 17:03:22 +04:00
2015-09-06 21:34:50 +03:00
namespace i2p {
namespace util {
2015-09-07 13:31:57 +03:00
HTTPConnection::HTTPConnection(boost::asio::ip::tcp::socket* socket,
2015-09-17 17:49:55 +03:00
std::shared_ptr<client::i2pcontrol::I2PControlSession> session)
: m_Socket(socket), m_BufferLen(0), m_Session(session)
2015-09-07 13:31:57 +03:00
{
}
2015-09-06 21:34:50 +03:00
void HTTPConnection::Terminate()
2013-12-10 17:03:22 +04:00
{
2015-09-06 21:34:50 +03:00
m_Socket->close();
}
2014-09-29 00:12:25 +04:00
2015-09-06 21:34:50 +03:00
void HTTPConnection::Receive()
{
m_Socket->async_read_some(
boost::asio::buffer(m_Buffer, HTTP_CONNECTION_BUFFER_SIZE), std::bind(
&HTTPConnection::HandleReceive, shared_from_this(),
std::placeholders::_1, std::placeholders::_2
)
);
}
2014-09-29 00:12:25 +04:00
2015-09-06 21:34:50 +03:00
void HTTPConnection::HandleReceive(const boost::system::error_code& e, std::size_t nb_bytes)
{
if(!e) {
m_Buffer[nb_bytes] = 0;
m_BufferLen = nb_bytes;
2015-09-17 12:47:16 +03:00
const std::string data = std::string(m_Buffer, m_Buffer + m_BufferLen);
if(!m_Request.hasData()) // New request
m_Request = i2p::util::http::Request(data);
else
m_Request.update(data);
if(m_Request.isComplete()) {
RunRequest();
m_Request.clear();
} else {
Receive();
}
2015-09-06 21:34:50 +03:00
} else if(e != boost::asio::error::operation_aborted)
Terminate();
}
2015-02-04 00:45:19 +03:00
2015-09-06 21:34:50 +03:00
void HTTPConnection::RunRequest()
{
2015-09-07 13:31:57 +03:00
try {
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
2015-09-17 12:47:16 +03:00
// Could also be invalid json data
2015-09-07 13:31:57 +03:00
}
// Unsupported method
m_Reply = i2p::util::http::Response(502, "");
SendReply();
2015-09-06 21:34:50 +03:00
}
2013-12-10 17:03:22 +04:00
2015-09-06 21:34:50 +03:00
void HTTPConnection::ExtractParams(const std::string& str, std::map<std::string, std::string>& params)
{
if(str[0] != '&') return;
size_t pos = 1, end;
do
{
end = str.find('&', pos);
std::string param = str.substr(pos, end - pos);
LogPrint(param);
size_t e = param.find('=');
if(e != std::string::npos)
params[param.substr(0, e)] = param.substr(e+1);
pos = end + 1;
}
2015-09-06 21:34:50 +03:00
while(end != std::string::npos);
}
2014-07-16 20:41:40 +04:00
2015-09-06 21:34:50 +03:00
void HTTPConnection::HandleWriteReply(const boost::system::error_code& e)
{
if(e != boost::asio::error::operation_aborted) {
boost::system::error_code ignored_ec;
m_Socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
Terminate();
}
2015-09-06 21:34:50 +03:00
}
2014-09-29 03:15:04 +04:00
2015-09-18 12:52:09 +03:00
void HTTPConnection::Send404Reply()
2015-09-06 21:34:50 +03:00
{
2015-09-18 12:52:09 +03:00
try {
const std::string error_page = "404.html";
m_Reply = i2p::util::http::Response(404, GetFileContents(error_page, true));
m_Reply.setHeader("Content-Type", i2p::util::http::getMimeType(error_page));
} catch(const std::runtime_error&) {
// Failed to load 404.html, assume the webui is incorrectly installed
m_Reply = i2p::util::http::Response(404,
"<!DOCTYPE HTML><html>"
"<head><title>Error: 404 - webui not installed</title></head><body>"
"<p>It looks like your webui installation is broken.</p>"
"<p>Run the following command to (re)install it:</p>"
2015-09-18 15:19:06 +03:00
"<pre>./i2pd --install=/path/to/webui</pre>"
"<p>Or from a directory containing a folder named webui:</p>"
"<pre>./i2pd --install</pre>"
2015-09-18 12:52:09 +03:00
"<p>The webui folder should come with the binaries.</p>"
"</body></html>"
);
}
SendReply();
}
2015-09-18 12:52:09 +03:00
std::string HTTPConnection::GetFileContents(const std::string& filename, bool preprocess) const
{
boost::system::error_code e;
2015-09-18 12:52:09 +03:00
// Use canonical to avoid .. or . in path
2015-09-17 12:47:16 +03:00
const boost::filesystem::path address = boost::filesystem::canonical(
2015-09-18 12:52:09 +03:00
i2p::util::filesystem::GetWebuiDataDir() / filename, e
2015-09-17 12:47:16 +03:00
);
const std::string address_str = address.string();
2015-09-17 12:47:16 +03:00
std::ifstream ifs(address_str);
2015-09-18 12:52:09 +03:00
if(e || !ifs || !isAllowed(address_str))
throw std::runtime_error("Cannot load " + address_str + ".");
2015-09-06 21:34:50 +03:00
std::string str;
ifs.seekg(0, ifs.end);
str.resize(ifs.tellg());
ifs.seekg(0, ifs.beg);
ifs.read(&str[0], str.size());
ifs.close();
2015-09-18 12:52:09 +03:00
if(preprocess)
return i2p::util::http::preprocessContent(str, address.parent_path().string());
else
return str;
}
2015-09-07 13:31:57 +03:00
2015-09-18 12:52:09 +03:00
void HTTPConnection::HandleRequest()
{
std::string uri = m_Request.getUri();
if(uri == "/")
uri = "index.html";
try {
m_Reply = i2p::util::http::Response(200, GetFileContents(uri, true));
m_Reply.setHeader("Content-Type", i2p::util::http::getMimeType(uri));
SendReply();
} catch(const std::runtime_error&) {
// Cannot open the file for some reason, send 404
Send404Reply();
}
2015-09-07 13:31:57 +03:00
}
void HTTPConnection::HandleI2PControlRequest()
{
std::stringstream ss(m_Request.getContent());
2015-09-17 17:49:55 +03:00
const client::i2pcontrol::I2PControlSession::Response rsp = m_Session->handleRequest(ss);
2015-09-07 13:31:57 +03:00
m_Reply = i2p::util::http::Response(200, rsp.toJsonString());
m_Reply.setHeader("Content-Type", "application/json");
2015-09-06 21:34:50 +03:00
SendReply();
}
2014-09-30 21:34:29 +04:00
2015-09-18 12:52:09 +03:00
bool HTTPConnection::isAllowed(const std::string& address) const
2015-09-06 21:34:50 +03:00
{
const std::size_t pos_dot = address.find_last_of('.');
const std::size_t pos_slash = address.find_last_of('/');
if(pos_dot == std::string::npos || pos_dot == address.size() - 1)
return false;
if(pos_slash != std::string::npos && pos_dot < pos_slash)
return false;
return true;
}
2014-09-30 21:34:29 +04:00
2015-09-06 21:34:50 +03:00
void HTTPConnection::SendReply()
{
// we need the date header to be compliant with HTTP 1.1
std::time_t time_now = std::time(nullptr);
char time_buff[128];
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();
}
boost::asio::async_write(
*m_Socket, boost::asio::buffer(m_Reply.toString()),
std::bind(&HTTPConnection::HandleWriteReply, shared_from_this(), std::placeholders::_1)
);
}
2014-07-16 20:41:40 +04:00
2015-09-06 21:34:50 +03:00
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)
2015-09-07 13:31:57 +03:00
),
m_NewSocket(nullptr),
2015-09-17 17:49:55 +03:00
m_Session(std::make_shared<client::i2pcontrol::I2PControlSession>(m_Service))
2015-09-06 21:34:50 +03:00
{
2014-07-16 20:41:40 +04:00
2015-09-06 21:34:50 +03:00
}
2013-12-10 17:03:22 +04:00
2015-09-06 21:34:50 +03:00
HTTPServer::~HTTPServer()
{
Stop();
}
2013-12-10 17:03:22 +04:00
2015-09-06 21:34:50 +03:00
void HTTPServer::Start()
{
m_Thread = new std::thread(std::bind(&HTTPServer::Run, this));
m_Acceptor.listen();
2015-09-07 13:31:57 +03:00
m_Session->start();
2015-09-06 21:34:50 +03:00
Accept();
}
2013-12-10 17:03:22 +04:00
2015-09-06 21:34:50 +03:00
void HTTPServer::Stop()
{
2015-09-07 13:31:57 +03:00
m_Session->stop();
2015-09-06 21:34:50 +03:00
m_Acceptor.close();
m_Service.stop();
if(m_Thread)
{
2015-09-06 21:34:50 +03:00
m_Thread->join();
delete m_Thread;
m_Thread = nullptr;
}
2015-09-06 21:34:50 +03:00
}
2013-12-10 17:03:22 +04:00
2015-09-06 21:34:50 +03:00
void HTTPServer::Run()
{
m_Service.run();
}
2013-12-10 17:03:22 +04:00
2015-09-06 21:34:50 +03:00
void HTTPServer::Accept()
{
m_NewSocket = new boost::asio::ip::tcp::socket(m_Service);
m_Acceptor.async_accept(*m_NewSocket, boost::bind(&HTTPServer::HandleAccept, this,
boost::asio::placeholders::error));
}
2013-12-10 17:03:22 +04:00
2015-09-06 21:34:50 +03:00
void HTTPServer::HandleAccept(const boost::system::error_code& ecode)
{
2015-09-07 13:31:57 +03:00
if(!ecode) {
2015-09-06 21:34:50 +03:00
CreateConnection(m_NewSocket);
Accept();
}
2015-09-06 21:34:50 +03:00
}
2015-09-07 13:31:57 +03:00
void HTTPServer::CreateConnection(boost::asio::ip::tcp::socket* m_NewSocket)
2015-09-06 21:34:50 +03:00
{
2015-09-07 13:31:57 +03:00
auto conn = std::make_shared<HTTPConnection>(m_NewSocket, m_Session);
2015-09-06 21:34:50 +03:00
conn->Receive();
}
2015-09-07 13:31:57 +03:00
2013-12-10 17:03:22 +04:00
}
}