Skip to content

Commit e6564cd

Browse files
committed
Updated CorsFilter.cpp
1 parent a024cd0 commit e6564cd

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

api/src/filters/CorsFilter.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// CorsFilter.cpp
21
#include "filters/CorsFilter.h"
32
#include "utils/Logger.h"
43

@@ -8,32 +7,46 @@ namespace api {
87
void CorsFilter::doFilter(const drogon::HttpRequestPtr& req,
98
drogon::FilterCallback&& fcb,
109
drogon::FilterChainCallback&& fccb) {
11-
API_LOG_DEBUG("Processing CORS request for path: {}", req->getPath());
10+
API_LOG_DEBUG("Starting CORS filter for path: {} method: {}", req->getPath(), req->getMethodString());
11+
API_LOG_DEBUG("Request headers:");
12+
for (const auto& header : req->getHeaders()) {
13+
API_LOG_DEBUG(" {}: {}", header.first, header.second);
14+
}
1215

16+
// Handle preflight OPTIONS request
1317
if (req->getMethodString() == "OPTIONS") {
14-
// Handle preflight request
15-
API_LOG_DEBUG("Handling OPTIONS preflight request");
18+
API_LOG_DEBUG("Processing OPTIONS preflight request");
1619
auto resp = drogon::HttpResponse::newHttpResponse();
1720

1821
// Add CORS headers for preflight
1922
resp->addHeader("Access-Control-Allow-Origin", "http://localhost:3000");
2023
resp->addHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
21-
resp->addHeader("Access-Control-Allow-Headers", "Content-Type");
22-
resp->addHeader("Access-Control-Max-Age", "86400");
24+
resp->addHeader("Access-Control-Allow-Headers", "*");
25+
resp->addHeader("Access-Control-Max-Age", "3600");
2326

24-
// Important: Set status 200 for OPTIONS
27+
// Set 200 OK status
2528
resp->setStatusCode(drogon::k200OK);
29+
30+
API_LOG_DEBUG("Sending preflight response with headers:");
31+
for (const auto& header : resp->getHeaders()) {
32+
API_LOG_DEBUG(" {}: {}", header.first, header.second);
33+
}
34+
2635
fcb(resp);
2736
return;
2837
}
2938

30-
// For non-OPTIONS requests
31-
auto resp = drogon::HttpResponse::newHttpResponse();
32-
resp->addHeader("Access-Control-Allow-Origin", "http://localhost:3000");
33-
resp->addHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
34-
resp->addHeader("Access-Control-Allow-Headers", "Content-Type");
39+
// For actual request
40+
API_LOG_DEBUG("Processing actual request");
3541

36-
// Continue with the request chain
42+
// Continue with request chain and add CORS headers to the final response
43+
auto modCallback = [fcb](const drogon::HttpResponsePtr& responsePtr) {
44+
responsePtr->addHeader("Access-Control-Allow-Origin", "http://localhost:3000");
45+
responsePtr->addHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
46+
responsePtr->addHeader("Access-Control-Allow-Headers", "*");
47+
fcb(responsePtr);
48+
};
49+
3750
fccb();
3851
}
3952

0 commit comments

Comments
 (0)