1
- // CorsFilter.cpp
2
1
#include " filters/CorsFilter.h"
3
2
#include " utils/Logger.h"
4
3
@@ -8,32 +7,46 @@ namespace api {
8
7
void CorsFilter::doFilter (const drogon::HttpRequestPtr& req,
9
8
drogon::FilterCallback&& fcb,
10
9
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
+ }
12
15
16
+ // Handle preflight OPTIONS request
13
17
if (req->getMethodString () == " OPTIONS" ) {
14
- // Handle preflight request
15
- API_LOG_DEBUG (" Handling OPTIONS preflight request" );
18
+ API_LOG_DEBUG (" Processing OPTIONS preflight request" );
16
19
auto resp = drogon::HttpResponse::newHttpResponse ();
17
20
18
21
// Add CORS headers for preflight
19
22
resp->addHeader (" Access-Control-Allow-Origin" , " http://localhost:3000" );
20
23
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 " );
23
26
24
- // Important: Set status 200 for OPTIONS
27
+ // Set 200 OK status
25
28
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
+
26
35
fcb (resp);
27
36
return ;
28
37
}
29
38
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" );
35
41
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
+
37
50
fccb ();
38
51
}
39
52
0 commit comments