Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 13d8341

Browse files
committed
fix: better handler
1 parent dce8663 commit 13d8341

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

engine/main.cc

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,17 +322,44 @@ void RunServer(std::optional<std::string> host, std::optional<int> port,
322322
// Handle OPTIONS preflight requests
323323
if (req->method() == drogon::HttpMethod::Options) {
324324
auto resp = HttpResponse::newHttpResponse();
325+
auto handlers = drogon::app().getHandlersInfo();
326+
bool has_ep = [req, &handlers]() {
327+
for (auto const& h : handlers) {
328+
if (req->path() == std::get<0>(h))
329+
return true;
330+
}
331+
return false;
332+
}();
333+
if (!has_ep) {
334+
resp->setStatusCode(drogon::HttpStatusCode::k404NotFound);
335+
stop(resp);
336+
return;
337+
}
338+
325339
handle_cors(req, resp);
340+
std::string supported_methods = [req, &handlers]() {
341+
std::string methods;
342+
for (auto const& h : handlers) {
343+
if (req->path() == std::get<0>(h)) {
344+
methods += drogon::to_string_view(std::get<1>(h));
345+
methods += ", ";
346+
}
347+
}
348+
if (methods.size() < 2)
349+
return std::string();
350+
return methods.substr(0, methods.size() - 2);
351+
}();
326352

327353
// Add more info to header
354+
resp->addHeader("Access-Control-Allow-Methods", supported_methods);
328355
{
329356
const auto& val = req->getHeader("Access-Control-Request-Headers");
330357
if (!val.empty())
331358
resp->addHeader("Access-Control-Allow-Headers", val);
332359
}
333360
// Set Access-Control-Max-Age
334361
resp->addHeader("Access-Control-Max-Age",
335-
"3600"); // Cache for 60 minutes
362+
"600"); // Cache for 10 minutes
336363
stop(resp);
337364
return;
338365
}

0 commit comments

Comments
 (0)