File tree Expand file tree Collapse file tree 2 files changed +29
-5
lines changed Expand file tree Collapse file tree 2 files changed +29
-5
lines changed Original file line number Diff line number Diff line change 1
1
#pragma once
2
+ #include " crow/common.h"
2
3
#include " crow/http_request.h"
3
4
#include " crow/http_response.h"
4
5
#include " crow/routing.h"
@@ -126,12 +127,25 @@ namespace crow
126
127
set_header_no_override (" Access-Control-Allow-Headers" , headers_, res);
127
128
set_header_no_override (" Access-Control-Expose-Headers" , exposed_headers_, res);
128
129
set_header_no_override (" Access-Control-Max-Age" , max_age_, res);
129
- if (allow_credentials_) set_header_no_override (" Access-Control-Allow-Credentials" , " true" , res);
130
130
131
- if (allow_credentials_ && origin_ == " *" )
132
- set_header_no_override (" Access-Control-Allow-Origin" , req.get_header_value (" Origin" ), res);
133
- else
131
+ bool origin_set = false ;
132
+
133
+ if (req.method != HTTPMethod::Options)
134
+ {
135
+ if (allow_credentials_)
136
+ {
137
+ set_header_no_override (" Access-Control-Allow-Credentials" , " true" , res);
138
+ if (origin_ == " *" )
139
+ {
140
+ set_header_no_override (" Access-Control-Allow-Origin" , req.get_header_value (" Origin" ), res);
141
+ origin_set = true ;
142
+ }
143
+ }
144
+ }
145
+
146
+ if ( !origin_set){
134
147
set_header_no_override (" Access-Control-Allow-Origin" , origin_, res);
148
+ }
135
149
}
136
150
137
151
bool ignore_ = false ;
Original file line number Diff line number Diff line change @@ -1997,6 +1997,10 @@ TEST_CASE("middleware_cors")
1997
1997
return " -" ;
1998
1998
});
1999
1999
2000
+ CROW_ROUTE (app, " /auth-origin" ).methods (crow::HTTPMethod::Post)([&](const request&) {
2001
+ return " -" ;
2002
+ });
2003
+
2000
2004
CROW_ROUTE (app, " /expose" )
2001
2005
([&](const request&) {
2002
2006
return " -" ;
@@ -2024,8 +2028,14 @@ TEST_CASE("middleware_cors")
2024
2028
CHECK (resp.find (" Access-Control-Allow-Origin: test.test" ) != std::string::npos);
2025
2029
2026
2030
resp = HttpClient::request (LOCALHOST_ADDRESS, port,
2027
- " GET /auth-origin\r\n Origin: test-client\r\n\r\n " );
2031
+ " GET /auth-origin\r\n Origin: test-client\r\n\r\n " );
2028
2032
CHECK (resp.find (" Access-Control-Allow-Origin: test-client" ) != std::string::npos);
2033
+ CHECK (resp.find (" Access-Control-Allow-Credentials: true" ) != std::string::npos);
2034
+
2035
+ resp = HttpClient::request (LOCALHOST_ADDRESS, port,
2036
+ " OPTIONS /auth-origin / HTTP/1.1 \r\n\r\n " );
2037
+ CHECK (resp.find (" Access-Control-Allow-Origin: *" ) != std::string::npos);
2038
+ CHECK (resp.find (" Access-Control-Allow-Credentials: true" ) == std::string::npos);
2029
2039
2030
2040
resp = HttpClient::request (LOCALHOST_ADDRESS, port,
2031
2041
" GET /expose\r\n\r\n " );
You can’t perform that action at this time.
0 commit comments