Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: manually add http date header for windows #1721

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions engine/controllers/configs.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "configs.h"
#include "utils/cortex_utils.h"

void Configs::GetConfigurations(
const HttpRequestPtr& req,
Expand All @@ -7,13 +8,13 @@ void Configs::GetConfigurations(
if (get_config_result.has_error()) {
Json::Value error_json;
error_json["message"] = get_config_result.error();
auto resp = drogon::HttpResponse::newHttpJsonResponse(error_json);
auto resp = cortex_utils::CreateCortexHttpJsonResponse(error_json);
resp->setStatusCode(drogon::k400BadRequest);
callback(resp);
return;
}

auto resp = drogon::HttpResponse::newHttpJsonResponse(
auto resp = cortex_utils::CreateCortexHttpJsonResponse(
get_config_result.value().ToJson());
resp->setStatusCode(drogon::k200OK);
callback(resp);
Expand All @@ -27,7 +28,7 @@ void Configs::UpdateConfigurations(
if (json_body == nullptr) {
Json::Value error_json;
error_json["message"] = "Configuration must be provided via JSON body";
auto resp = drogon::HttpResponse::newHttpJsonResponse(error_json);
auto resp = cortex_utils::CreateCortexHttpJsonResponse(error_json);
resp->setStatusCode(drogon::k400BadRequest);
callback(resp);
return;
Expand All @@ -37,7 +38,7 @@ void Configs::UpdateConfigurations(
if (update_config_result.has_error()) {
Json::Value error_json;
error_json["message"] = update_config_result.error();
auto resp = drogon::HttpResponse::newHttpJsonResponse(error_json);
auto resp = cortex_utils::CreateCortexHttpJsonResponse(error_json);
resp->setStatusCode(drogon::k400BadRequest);
callback(resp);
return;
Expand All @@ -46,7 +47,7 @@ void Configs::UpdateConfigurations(
Json::Value root;
root["message"] = "Configuration updated successfully";
root["config"] = update_config_result.value().ToJson();
auto resp = drogon::HttpResponse::newHttpJsonResponse(root);
auto resp = cortex_utils::CreateCortexHttpJsonResponse(root);
resp->setStatusCode(drogon::k200OK);
callback(resp);
return;
Expand Down
5 changes: 3 additions & 2 deletions engine/controllers/swagger.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "swagger.h"
#include "cortex_openapi.h"
#include "utils/cortex_utils.h"

constexpr auto ScalarUi = R"(
<!doctype html>
Expand Down Expand Up @@ -31,7 +32,7 @@ Json::Value SwaggerController::generateOpenAPISpec() {
void SwaggerController::serveSwaggerUI(
const drogon::HttpRequestPtr& req,
std::function<void(const drogon::HttpResponsePtr&)>&& callback) const {
auto resp = drogon::HttpResponse::newHttpResponse();
auto resp = cortex_utils::CreateCortexHttpResponse();
resp->setBody(ScalarUi);
resp->setContentTypeCode(drogon::CT_TEXT_HTML);
callback(resp);
Expand All @@ -41,6 +42,6 @@ void SwaggerController::serveOpenAPISpec(
const drogon::HttpRequestPtr& req,
std::function<void(const drogon::HttpResponsePtr&)>&& callback) const {
Json::Value spec = generateOpenAPISpec();
auto resp = drogon::HttpResponse::newHttpJsonResponse(spec);
auto resp = cortex_utils::CreateCortexHttpJsonResponse(spec);
callback(resp);
}
2 changes: 2 additions & 0 deletions engine/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ void RunServer(std::optional<int> port, bool ignore_cout) {
LOG_INFO << "Please load your model";
#ifndef _WIN32
drogon::app().enableReusePort();
#else
drogon::app().enableDateHeader(false);
vansangpfiev marked this conversation as resolved.
Show resolved Hide resolved
#endif
drogon::app().addListener(config.apiServerHost,
std::stoi(config.apiServerPort));
Expand Down
29 changes: 26 additions & 3 deletions engine/utils/cortex_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <drogon/HttpResponse.h>
#include <sys/stat.h>
#include <algorithm>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <random>
Expand All @@ -24,20 +26,41 @@ inline std::string logs_folder = "./logs";
inline std::string logs_base_name = "./logs/cortex.log";
inline std::string logs_cli_base_name = "./logs/cortex-cli.log";

// example: Mon, 25 Nov 2024 09:57:03 GMT
inline std::string GetDateRFC1123() {
std::time_t now = std::time(nullptr);
std::tm* gmt_time = std::gmtime(&now);
std::ostringstream oss;
oss << std::put_time(gmt_time, "%a, %d %b %Y %H:%M:%S GMT");
return oss.str();
}

inline drogon::HttpResponsePtr CreateCortexHttpResponse() {
return drogon::HttpResponse::newHttpResponse();
auto res = drogon::HttpResponse::newHttpResponse();
#if defined(_WIN32)
res->addHeader("date", GetDateRFC1123());
#endif
return res;
}

inline drogon::HttpResponsePtr CreateCortexHttpJsonResponse(
const Json::Value& data) {
return drogon::HttpResponse::newHttpJsonResponse(data);
auto res = drogon::HttpResponse::newHttpJsonResponse(data);
#if defined(_WIN32)
res->addHeader("date", GetDateRFC1123());
#endif
return res;
};

inline drogon::HttpResponsePtr CreateCortexStreamResponse(
const std::function<std::size_t(char*, std::size_t)>& callback,
const std::string& attachmentFileName = "") {
return drogon::HttpResponse::newStreamResponse(
auto res = drogon::HttpResponse::newStreamResponse(
callback, attachmentFileName, drogon::CT_NONE, "text/event-stream");
#if defined(_WIN32)
res->addHeader("date", GetDateRFC1123());
#endif
return res;
}

#if defined(_WIN32)
Expand Down
Loading