-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
allow custom OPTIONS handling via middleware flagging #2444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow custom OPTIONS handling via middleware flagging #2444
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This pull request introduces an opt-out mechanism for Drogon's built-in OPTIONS/CORS preflight request handling, allowing applications to implement custom CORS policies through their own route handlers. The default behavior remains unchanged, ensuring full backward compatibility.
Changes:
- Added conditional logic in HttpServer to skip internal OPTIONS handling when a custom flag is set
- Introduced HttpOptionsMiddleware classes that tag OPTIONS requests with a
customCORShandlingattribute - Provided comprehensive documentation for the new middleware usage patterns
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/src/HttpServer.cc | Adds conditional check to skip internal OPTIONS handling when customCORShandling attribute is true |
| lib/inc/drogon/HttpMiddleware.h | Introduces HttpOptionsMiddleware template classes and implementations for flagging OPTIONS requests |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
I just realized that the custom Options handler needs to know the registered methods for the path of the request, so I also added them as attribute when custom handling is requested |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
lib/inc/drogon/HttpMiddleware.h
Outdated
| * 1. globally via | ||
| * app().registerMiddleware(std::make_shared<drogon::HttpOptionsMiddleware>()) | ||
| * 2. on every path handlers that need non-default handling, with | ||
| * ADD_METHOD_TO(... , "drogon::HttpOptionsMiddleware") |
Copilot
AI
Jan 22, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation example is incorrect. When using ADD_METHOD_TO with a string reference (line 164), the middleware must have AutoCreation set to true so the framework can automatically instantiate it. However, HttpOptionsMiddleware has AutoCreation set to false (line 189).
The example should use "drogon::HttpOptionsMiddlewareAuto" instead, which has AutoCreation set to true (line 183-184). Alternatively, document that only option 1 (manual registration via registerMiddleware) is supported.
| * ADD_METHOD_TO(... , "drogon::HttpOptionsMiddleware") | |
| * ADD_METHOD_TO(... , "drogon::HttpOptionsMiddlewareAuto") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not what I understood (and use).
I always register middleware with auto-creation set to false, and then use their name in paths registration.
Is this the wrong way?
|
@Greisby Thanks so much for your patch. Is it more lightweight and efficient if we use configuration items instead of middleware to achieve this effect? |
|
@an-tao For info, the use would be similar to this: |
You are right, the middleware solution is more flexible, if we use an option in the configuration file, it changes global behavior of all option requests. |
|
Ah ok, I understand now. You meant an cmake configuration option. I think that the extra test in HttpServer::requestPreHandling won't impact the performance, and it remains flexible as it is now. |
I mean an option in the config.json file that affects the entire application. |
|
Ok, got it. |
Summary
This PR adds an opt-out mechanism for Drogon’s built-in handling of OPTIONS (CORS preflight) requests, enabling applications to implement custom preflight logic in their own route handlers when needed.
Concretely:
drogon.customCORShandling).Motivation / Why this is useful
Drogon’s default shortcut responds with permissive CORS headers based on the request headers.
This is convenient for many APIs, but it can be limiting when an application needs a non-default CORS policy that depends on runtime context or route-specific rules.
Typical cases include:
Today, the internal OPTIONS shortcut prevents those applications from routing preflight to handlers in a straightforward way.
This PR provides a minimal and explicit opt-out, without changing default behavior.
Design
drogon.customCORShandlingis absent/false, Drogon continues to handle OPTIONS internally as before.Backward compatibility
drogon.customCORShandlinganddrogon.corsMethodsattributes.drogon.customCORShandlingattribute, so performance impact is negligible.Notes
The current implementation keeps the change minimal.
Possible follow-up (not part of this PR)
Provide small helper utilities to build consistent OPTIONS / CORS preflight responses (e.g. isCorsPreflightRequest(), createOptionsResponse()), so applications can implement custom policies more easily.
II’m keeping this PR focused on the opt-out mechanism to minimize API surface and review scope.