From b6863ac2e7230394cf8982992674d04c8a025f3d Mon Sep 17 00:00:00 2001 From: Leon Etienne Date: Mon, 18 Apr 2022 14:36:26 +0200 Subject: [PATCH] Server backend now implements a request to get the raw download log for a given download-entity --- Tubio/HttpServer.cpp | 29 +++++++++++++++++++++++++++++ Tubio/HttpServer.h | 1 + Tubio/Version.h | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Tubio/HttpServer.cpp b/Tubio/HttpServer.cpp index eee915e..8f50c9b 100644 --- a/Tubio/HttpServer.cpp +++ b/Tubio/HttpServer.cpp @@ -102,6 +102,10 @@ void HttpServer::EventHandler(mg_connection* pNc, int ev, void* p) { ProcessAPIRequest(pNc, ev, p, peer_addr); } + else if (requestedUri.substr(0, 12) == "/downloadlog") + { + ServeDownloadLog(pNc, ev, p, requestedUri); + } else if (requestedUri.substr(0, 9) == "/download") { ServeDownloadeableResource(pNc, ev, p, requestedUri); @@ -216,6 +220,31 @@ void HttpServer::ServeDownloadeableResource(mg_connection* pNc, int ev, void* p, return; } +void HttpServer::ServeDownloadLog(mg_connection* pNc, int ev, void* p, std::string uri) +{ + std::string fileId = uri.substr(13, uri.length() - 13); + + if (Downloader::DownloadManager::DoesTubioIDExist(fileId)) + { + Downloader::DownloadEntry& entry = Downloader::DownloadManager::GetDownloadEntryByTubioID(fileId); + + std::stringstream ss; + std::string logFilename = std::string("./dlcache/dlprogbuf/") + fileId + ".buf"; + + ss << "Access-Control-Allow-Origin: *\nContent-Type: text/plain; Cache-Control: must-revalidate, post-check=0, pre-check=0"; + mg_http_serve_file(pNc, (http_message*)p, logFilename.c_str(), mg_mk_str("text/plain"), mg_mk_str(ss.str().c_str())); + } + else + { + Json j; + std::stringstream ss; + j.CloneFrom(RestResponseTemplates::GetByCode(HTTP_STATUS_CODE::BAD_REQUEST, "Invalid tubio id!")); + ServeStringToConnection(pNc, j.Render(), (int)HTTP_STATUS_CODE::BAD_REQUEST); + } + + return; +} + bool HttpServer::IsConnectionAllowed(std::string peer_address, std::string& denialReason) { // Localhost is always allowed! diff --git a/Tubio/HttpServer.h b/Tubio/HttpServer.h index 0f95a43..7ba650f 100644 --- a/Tubio/HttpServer.h +++ b/Tubio/HttpServer.h @@ -24,6 +24,7 @@ namespace Rest bool InitWebServer(); static void ProcessAPIRequest(struct mg_connection* pNc, int ev, void* p, std::string peerAddress); static void ServeDownloadeableResource(struct mg_connection* pNc, int ev, void* p, std::string uri); + static void ServeDownloadLog(struct mg_connection* pNc, int ev, void* p, std::string uri); static void EventHandler(struct mg_connection* pNc, int ev, void* p); static void ServeStringToConnection(struct mg_connection* c, std::string str, int httpStatusCode = 200); diff --git a/Tubio/Version.h b/Tubio/Version.h index 147f94f..057a7fc 100644 --- a/Tubio/Version.h +++ b/Tubio/Version.h @@ -1,2 +1,2 @@ #pragma once -#define TUBIO_SERVER_VERSION (0.65) +#define TUBIO_SERVER_VERSION (0.66)