diff --git a/engine/controllers/engines.cc b/engine/controllers/engines.cc index e01abf9ab..c5e816d66 100644 --- a/engine/controllers/engines.cc +++ b/engine/controllers/engines.cc @@ -49,10 +49,8 @@ void Engines::TestJinja( callback(resp); return; } - // TODO: namh recheck all the api using this. because we have an issue with Germany locale before. - auto resp = HttpResponse::newHttpResponse(); - resp->setBody(rendered_data.value()); - resp->setContentTypeCode(drogon::CT_TEXT_PLAIN); + + auto resp = cortex_utils::CreateTextPlainResponse(rendered_data.value()); callback(resp); } diff --git a/engine/controllers/files.cc b/engine/controllers/files.cc index e0cd502f4..ed37967b2 100644 --- a/engine/controllers/files.cc +++ b/engine/controllers/files.cc @@ -216,10 +216,8 @@ void Files::RetrieveFileContent( return; } - auto [buffer, size] = std::move(res.value()); - auto resp = HttpResponse::newHttpResponse(); - resp->setBody(std::string(buffer.get(), size)); - resp->setContentTypeCode(CT_APPLICATION_OCTET_STREAM); + auto resp = + cortex_utils::CreateCortexContentResponse(std::move(res.value())); callback(resp); } else { if (!msg_res->rel_path.has_value()) { @@ -243,10 +241,8 @@ void Files::RetrieveFileContent( return; } - auto [buffer, size] = std::move(content_res.value()); - auto resp = HttpResponse::newHttpResponse(); - resp->setBody(std::string(buffer.get(), size)); - resp->setContentTypeCode(CT_APPLICATION_OCTET_STREAM); + auto resp = cortex_utils::CreateCortexContentResponse( + std::move(content_res.value())); callback(resp); } } @@ -261,9 +257,6 @@ void Files::RetrieveFileContent( return; } - auto [buffer, size] = std::move(res.value()); - auto resp = HttpResponse::newHttpResponse(); - resp->setBody(std::string(buffer.get(), size)); - resp->setContentTypeCode(CT_APPLICATION_OCTET_STREAM); + auto resp = cortex_utils::CreateCortexContentResponse(std::move(res.value())); callback(resp); } diff --git a/engine/utils/cortex_utils.h b/engine/utils/cortex_utils.h index 4d0a956a9..f58fcfe8f 100644 --- a/engine/utils/cortex_utils.h +++ b/engine/utils/cortex_utils.h @@ -2,16 +2,10 @@ #include #include #include -#include #include -#include -#include #include -#include -#include -#include -#include #include +#include #if defined(__linux__) #include #include @@ -69,6 +63,30 @@ inline drogon::HttpResponsePtr CreateCortexHttpJsonResponse( return res; }; +inline drogon::HttpResponsePtr CreateCortexContentResponse( + std::pair, size_t> content) { + auto [buffer, size] = std::move(content); + auto resp = drogon::HttpResponse::newHttpResponse(); + resp->setBody(std::string(buffer.get(), size)); + resp->setContentTypeCode(drogon::CT_APPLICATION_OCTET_STREAM); + +#if defined(_WIN32) + resp->addHeader("date", GetDateRFC1123()); +#endif + return resp; +} + +inline drogon::HttpResponsePtr CreateTextPlainResponse( + const std::string& text) { + auto resp = drogon::HttpResponse::newHttpResponse(); + resp->setBody(text); + resp->setContentTypeCode(drogon::CT_TEXT_PLAIN); +#if defined(_WIN32) + resp->addHeader("date", GetDateRFC1123()); +#endif + return resp; +} + inline drogon::HttpResponsePtr CreateCortexStreamResponse( const std::function& callback, const std::string& attachmentFileName = "") { @@ -80,8 +98,6 @@ inline drogon::HttpResponsePtr CreateCortexStreamResponse( return res; } - - #if defined(_WIN32) inline std::string GetCurrentPath() { char path[MAX_PATH];