2020-09-24 18:54:34 +02:00
|
|
|
#include "HttpServer.h"
|
2020-09-24 01:16:44 +02:00
|
|
|
|
|
|
|
using namespace Logging;
|
2020-09-24 14:31:30 +02:00
|
|
|
using namespace Rest;
|
|
|
|
using namespace JasonPP;
|
2020-09-24 01:16:44 +02:00
|
|
|
|
2020-09-24 18:54:34 +02:00
|
|
|
HttpServer::HttpServer()
|
2020-09-24 01:16:44 +02:00
|
|
|
{
|
|
|
|
pMgr = new mg_mgr();
|
|
|
|
pNc = nullptr;
|
2020-09-24 19:00:01 +02:00
|
|
|
log = new Logger("HttpServer");
|
2020-09-24 01:16:44 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:54:34 +02:00
|
|
|
HttpServer::~HttpServer()
|
2020-09-24 01:16:44 +02:00
|
|
|
{
|
|
|
|
delete pMgr;
|
|
|
|
delete log;
|
|
|
|
|
|
|
|
log = nullptr;
|
|
|
|
pMgr = nullptr;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:54:34 +02:00
|
|
|
void HttpServer::PostInit()
|
2020-09-24 01:16:44 +02:00
|
|
|
{
|
|
|
|
isBootedSuccessfully = InitWebServer();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:54:34 +02:00
|
|
|
bool HttpServer::InitWebServer()
|
2020-09-24 01:16:44 +02:00
|
|
|
{
|
|
|
|
mg_mgr_init(pMgr, NULL);
|
|
|
|
|
2020-09-24 19:00:01 +02:00
|
|
|
log->cout << "Starting http-server on port " << XGConfig::httpServer.port << "...";
|
2020-09-24 01:16:44 +02:00
|
|
|
log->Flush();
|
|
|
|
|
2020-09-24 18:54:34 +02:00
|
|
|
pNc = mg_bind(pMgr, XGConfig::httpServer.port.c_str(), this->EventHandler);
|
2020-09-24 01:16:44 +02:00
|
|
|
|
|
|
|
if (pNc == NULL)
|
|
|
|
{
|
2020-09-24 19:00:01 +02:00
|
|
|
log->cout << log->Err() << "Failed to boot the http-server! - Unable to bind listener! (port: " << XGConfig::httpServer.port << ")";
|
2020-09-24 01:16:44 +02:00
|
|
|
log->Flush();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mg_set_protocol_http_websocket(pNc);
|
2020-09-24 18:54:34 +02:00
|
|
|
frontend_serve_opts.document_root = XGConfig::httpServer.rootdir.c_str();
|
2020-09-24 15:35:07 +02:00
|
|
|
frontend_serve_opts.enable_directory_listing = "no";
|
2020-09-24 01:16:44 +02:00
|
|
|
|
2020-09-24 19:00:01 +02:00
|
|
|
log->cout << "Started http-server successfully!";
|
2020-09-24 01:16:44 +02:00
|
|
|
log->Flush();
|
|
|
|
isBootedSuccessfully = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:54:34 +02:00
|
|
|
void HttpServer::Update()
|
2020-09-24 01:16:44 +02:00
|
|
|
{
|
2020-09-27 13:06:04 +02:00
|
|
|
mg_mgr_poll(pMgr, XGConfig::httpServer.polling_rate);
|
2020-09-24 01:16:44 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:54:34 +02:00
|
|
|
void HttpServer::ServeStringToConnection(struct mg_connection* c, std::string str, int httpStatusCode)
|
2020-09-24 01:16:44 +02:00
|
|
|
{
|
2020-09-24 14:31:30 +02:00
|
|
|
mg_send_head(c, httpStatusCode, str.length(), "content-type: application/json\nAccess-Control-Allow-Origin: *");
|
2021-03-03 19:08:06 +01:00
|
|
|
mg_printf(c, "%s", str.c_str());
|
2020-09-24 01:16:44 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:54:34 +02:00
|
|
|
void HttpServer::EventHandler(mg_connection* pNc, int ev, void* p)
|
2020-09-24 01:16:44 +02:00
|
|
|
{
|
2020-09-24 14:31:30 +02:00
|
|
|
switch (ev)
|
2020-09-24 01:16:44 +02:00
|
|
|
{
|
|
|
|
case MG_EV_HTTP_REQUEST:
|
2020-11-15 22:30:50 +01:00
|
|
|
// Reset standby timer
|
|
|
|
XGControl::last_query_time = time(0);
|
2020-09-24 01:16:44 +02:00
|
|
|
|
2020-09-24 14:31:30 +02:00
|
|
|
http_message* hpm = (http_message*)p;
|
2020-09-24 15:35:07 +02:00
|
|
|
std::string requestedUri = FixUnterminatedString(hpm->uri.p, hpm->uri.len);
|
2020-09-30 22:41:00 +02:00
|
|
|
|
2020-10-02 13:16:57 +02:00
|
|
|
// Get clients ip address
|
2020-09-30 22:41:00 +02:00
|
|
|
std::string peer_addr;
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
mg_sock_addr_to_str(&pNc->sa, buf, sizeof(buf), MG_SOCK_STRINGIFY_IP);
|
|
|
|
peer_addr = buf;
|
|
|
|
}
|
|
|
|
|
2020-10-02 18:09:51 +02:00
|
|
|
std::string denialReason;
|
|
|
|
if (IsConnectionAllowed(peer_addr, denialReason))
|
2020-09-30 22:41:00 +02:00
|
|
|
{
|
|
|
|
try
|
2020-09-25 02:04:30 +02:00
|
|
|
{
|
2020-09-30 22:41:00 +02:00
|
|
|
if (requestedUri == "/api")
|
|
|
|
{
|
|
|
|
ProcessAPIRequest(pNc, ev, p, peer_addr);
|
|
|
|
}
|
|
|
|
else if (requestedUri.substr(0, 9) == "/download")
|
|
|
|
{
|
|
|
|
ServeDownloadeableResource(pNc, ev, p, requestedUri);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Just serve the files requested
|
|
|
|
mg_serve_http(pNc, (struct http_message*)p, frontend_serve_opts);
|
|
|
|
}
|
2020-09-25 02:04:30 +02:00
|
|
|
}
|
2020-09-30 22:41:00 +02:00
|
|
|
catch (std::exception& e)
|
2020-09-27 18:51:53 +02:00
|
|
|
{
|
2020-09-30 22:41:00 +02:00
|
|
|
Json j;
|
2021-03-03 19:08:06 +01:00
|
|
|
j.CloneFrom(RestResponseTemplates::GetByCode(HTTP_STATUS_CODE::INTERNAL_SERVER_ERROR, e.what()));
|
|
|
|
ServeStringToConnection(pNc, j.Render(), (int)HTTP_STATUS_CODE::INTERNAL_SERVER_ERROR);
|
2020-09-27 18:51:53 +02:00
|
|
|
}
|
2020-09-30 22:41:00 +02:00
|
|
|
catch (...)
|
2020-09-25 02:04:30 +02:00
|
|
|
{
|
2020-09-30 22:41:00 +02:00
|
|
|
Json j;
|
2021-03-03 19:08:06 +01:00
|
|
|
j.CloneFrom(RestResponseTemplates::GetByCode(HTTP_STATUS_CODE::INTERNAL_SERVER_ERROR, "Das not good"));
|
|
|
|
ServeStringToConnection(pNc, j.Render(), (int)HTTP_STATUS_CODE::INTERNAL_SERVER_ERROR);
|
2020-09-25 02:04:30 +02:00
|
|
|
}
|
2020-09-24 01:16:44 +02:00
|
|
|
|
2020-09-30 22:41:00 +02:00
|
|
|
break;
|
|
|
|
}
|
2020-10-02 13:16:57 +02:00
|
|
|
else // Client is not allowed, serve error json
|
|
|
|
{
|
|
|
|
Json j;
|
2021-03-03 19:08:06 +01:00
|
|
|
j.CloneFrom(RestResponseTemplates::GetByCode(HTTP_STATUS_CODE::UNAUTHORIZED, denialReason));
|
|
|
|
ServeStringToConnection(pNc, j.Render(), (int)HTTP_STATUS_CODE::UNAUTHORIZED);
|
2020-10-02 13:16:57 +02:00
|
|
|
}
|
2020-09-24 01:16:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-30 22:41:00 +02:00
|
|
|
void HttpServer::ProcessAPIRequest(mg_connection* pNc, int ev, void* p, std::string peerAddress)
|
2020-09-24 15:35:07 +02:00
|
|
|
{
|
|
|
|
// Get struct with http message informations
|
|
|
|
http_message* hpm = (http_message*)p;
|
|
|
|
|
|
|
|
// Get the transmitted message body
|
|
|
|
std::string requestBodyRaw = FixUnterminatedString(hpm->body.p, hpm->body.len);
|
|
|
|
|
|
|
|
// Check for the body being valid json
|
|
|
|
if (IsJsonValid(requestBodyRaw))
|
|
|
|
{
|
|
|
|
Json requestBody;
|
|
|
|
requestBody.Parse(requestBodyRaw);
|
|
|
|
|
2020-09-30 22:41:00 +02:00
|
|
|
|
2020-09-24 15:35:07 +02:00
|
|
|
|
|
|
|
JsonBlock responseBody;
|
|
|
|
HTTP_STATUS_CODE returnCode;
|
2020-09-30 22:41:00 +02:00
|
|
|
RestQueryHandler::ProcessQuery(peerAddress, requestBody, responseBody, returnCode);
|
2020-09-24 15:35:07 +02:00
|
|
|
|
|
|
|
Json response(responseBody);
|
2021-03-03 19:08:06 +01:00
|
|
|
ServeStringToConnection(pNc, response.Render(), (int)returnCode);
|
2020-09-24 15:35:07 +02:00
|
|
|
}
|
|
|
|
else // return error message for invalid json
|
|
|
|
{
|
2020-09-25 02:04:30 +02:00
|
|
|
Json errorJson;
|
|
|
|
errorJson.CloneFrom(RestResponseTemplates::GetByCode(HTTP_STATUS_CODE::BAD_REQUEST, "Received json is fucked"));
|
2021-03-03 19:08:06 +01:00
|
|
|
ServeStringToConnection(pNc, errorJson.Render(), (int)HTTP_STATUS_CODE::BAD_REQUEST);
|
2020-09-24 15:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-27 19:14:35 +02:00
|
|
|
void HttpServer::ServeDownloadeableResource(mg_connection* pNc, int ev, void* p, std::string uri)
|
2020-09-27 18:51:53 +02:00
|
|
|
{
|
|
|
|
std::string fileId = uri.substr(10, uri.length() - 10);
|
|
|
|
|
|
|
|
if (Downloader::DownloadManager::DoesTubioIDExist(fileId))
|
|
|
|
{
|
|
|
|
Downloader::DownloadEntry& entry = Downloader::DownloadManager::GetDownloadEntryByTubioID(fileId);
|
|
|
|
|
|
|
|
if (entry.status == Downloader::DOWNLOAD_STATUS::FINISHED)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
std::string downloadedFilename = entry.title + (entry.mode == Downloader::DOWNLOAD_MODE::AUDIO ? ".mp3" : ".mp4");
|
|
|
|
|
|
|
|
ss << "Access-Control-Allow-Origin: *\nContent-Disposition: attachment; filename=\"" << downloadedFilename << "\"\nPragma: public\nCache-Control: must-revalidate, post-check=0, pre-check=0";
|
|
|
|
mg_http_serve_file(pNc, (http_message*)p, entry.downloaded_filename.c_str(), mg_mk_str("application/octet-stream"), mg_mk_str(ss.str().c_str()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Json j;
|
2021-03-03 19:08:06 +01:00
|
|
|
j.CloneFrom(RestResponseTemplates::GetByCode(HTTP_STATUS_CODE::BAD_REQUEST, "File download not ready!"));
|
|
|
|
ServeStringToConnection(pNc, j.Render(), (int)HTTP_STATUS_CODE::BAD_REQUEST);
|
2020-09-27 18:51:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Json j;
|
2021-03-03 19:08:06 +01:00
|
|
|
j.CloneFrom(RestResponseTemplates::GetByCode(HTTP_STATUS_CODE::BAD_REQUEST, "Invalid tubio id!"));
|
|
|
|
ServeStringToConnection(pNc, j.Render(), (int)HTTP_STATUS_CODE::BAD_REQUEST);
|
2020-09-27 18:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-02 18:09:51 +02:00
|
|
|
bool HttpServer::IsConnectionAllowed(std::string peer_address, std::string& denialReason)
|
2020-10-02 13:16:57 +02:00
|
|
|
{
|
|
|
|
// Localhost is always allowed!
|
|
|
|
if (peer_address == "127.0.0.1") return true;
|
|
|
|
|
|
|
|
// Peer is not localhost, but only localhost is allowed!
|
2020-10-02 18:09:51 +02:00
|
|
|
else if (XGConfig::access.only_allow_localhost)
|
|
|
|
{
|
|
|
|
denialReason = "Only localhost allowed!";
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-02 13:16:57 +02:00
|
|
|
|
|
|
|
// Let's check if the whitelist is active
|
|
|
|
else if (XGConfig::access.enable_whitelist)
|
|
|
|
{
|
|
|
|
// It is. Let's check if our peer is whitelisted
|
|
|
|
for (std::size_t i = 0; i < XGConfig::access.whitelist.size(); i++)
|
|
|
|
{
|
|
|
|
// Whitelist is enabled, and peer is whitelisted
|
|
|
|
if (XGConfig::access.whitelist[i] == peer_address) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Whitelist is enabled, but peer is NOT whitelisted
|
2020-11-15 22:30:50 +01:00
|
|
|
denialReason = std::string("Not whitelisted! Ask your tubio administrator to whitelist '") + peer_address + "' in order to gain access.";
|
2020-10-02 13:16:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else // Whitelist is NOT enabled and only_allow_localhost is FALSE!
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should never come to this point
|
2020-10-02 18:09:51 +02:00
|
|
|
denialReason = "Access denied";
|
2020-10-02 13:16:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:54:34 +02:00
|
|
|
void HttpServer::OnExit()
|
2020-09-24 01:16:44 +02:00
|
|
|
{
|
2020-09-24 19:00:01 +02:00
|
|
|
log->cout << "Shutting down http-server...";
|
2020-09-24 15:15:18 +02:00
|
|
|
log->Flush();
|
|
|
|
|
2020-09-24 01:16:44 +02:00
|
|
|
mg_mgr_free(pMgr);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2020-09-24 14:31:30 +02:00
|
|
|
|
|
|
|
|
2020-09-24 18:54:34 +02:00
|
|
|
std::string HttpServer::FixUnterminatedString(const char* cstr, const std::size_t len)
|
2020-09-24 14:31:30 +02:00
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
for (std::size_t i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
ss << *(cstr + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
2020-09-24 15:35:07 +02:00
|
|
|
|
2020-09-24 18:54:34 +02:00
|
|
|
mg_serve_http_opts HttpServer::frontend_serve_opts;
|