Comments (5)
To insecurely resolve all CORS issues, you can echo back everything the client asks for:
drogon::app()
.registerSyncAdvice([](const HttpRequestPtr& req) -> HttpResponsePtr
{
if(req->method() == drogon::HttpMethod::Options)
{
auto resp = HttpResponse::newHttpResponse();
{
const auto& val = req->getHeader("Origin");
if(!val.empty())
resp->addHeader("Access-Control-Allow-Origin", val);
}
{
const auto& val = req->getHeader("Access-Control-Request-Method");
if(!val.empty())
resp->addHeader("Access-Control-Allow-Methods", val);
}
resp->addHeader("Access-Control-Allow-Credentials", "true");
{
const auto& val = req->getHeader("Access-Control-Request-Headers");
if(!val.empty())
resp->addHeader("Access-Control-Allow-Headers", val);
}
return std::move(resp);
}
return {};
})
.registerPostHandlingAdvice([](const HttpRequestPtr& req, const HttpResponsePtr& resp) -> void
{
{
const auto& val = req->getHeader("Origin");
if(!val.empty())
resp->addHeader("Access-Control-Allow-Origin", val);
}
{
const auto& val = req->getHeader("Access-Control-Request-Method");
if(!val.empty())
resp->addHeader("Access-Control-Allow-Methods", val);
}
resp->addHeader("Access-Control-Allow-Credentials", "true");
{
const auto& val = req->getHeader("Access-Control-Request-Headers");
if(!val.empty())
resp->addHeader("Access-Control-Allow-Headers", val);
}
})
.run();
from drogon.
Options is a preflight, it asks what it can do before actually doing it, that's why we respond separately for OPTION requests
from drogon.
If you do that then none of your API endpoints will be hit and the sync advice ends up responding to every request
from drogon.
Thanks @Mis1eader-dev it works.
Is there any reason not to set headers for all OPTIONS POST GET requests (not just OPTIONS) inside registerSyncAdvice?
from drogon.
@Mis1eader-dev I mean, why not respond to the POST GET in the registerSyncAdvice hook too?
from drogon.
Related Issues (20)
- 内存泄漏 HOT 38
- forward cant works HOT 1
- Hodor plugin add exempt IPs HOT 1
- gcc 14.1 HOT 3
- Let drogon_ctl generate systemd unit file
- drogon_ctl create model treats charvar[] (arrays) as std::string. HOT 2
- Provide capability to serve files without extension HOT 2
- HttpMiddleware not found in the latest tag HOT 2
- error on compiling source code HOT 3
- Support C++20 modules.
- MSSQL support in ORM
- It's time to create a middleware for CORS, I will create it when I have time. HOT 1
- 进程已结束,退出代码为 139 (interrupted by signal 11:SIGSEGV) HOT 18
- HTTP/2 compatiablity tracker HOT 4
- Protection against SQL injections HOT 2
- ORM Mapper getDbClient return NULL HOT 7
- Aligned Static File Routes HOT 1
- HttpRequestPtr reset interface HOT 1
- [HttpAppFramework / HttpClientPtr ] can't change/set (ssl key, cert, & configuration) after/on plugin HOT 2
- new json HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from drogon.