drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
drogon::HttpOptionsMiddlewareImpl< Derived, AutoCreation > Class Template Reference

Simple middleware that tags OPTIONS requests. More...

#include <drogon/HttpMiddleware.h>

Inheritance diagram for drogon::HttpOptionsMiddlewareImpl< Derived, AutoCreation >:
Collaboration diagram for drogon::HttpOptionsMiddlewareImpl< Derived, AutoCreation >:

Public Member Functions

void invoke (const HttpRequestPtr &req, MiddlewareNextCallback &&nextCb, MiddlewareCallback &&mcb) override
Public Member Functions inherited from drogon::DrObject< Derived >
const std::string & className () const override
 Get the class name.
bool isClass (const std::string &class_name) const override
 Return true if the class name is 'class_name'.

Additional Inherited Members

Static Public Member Functions inherited from drogon::DrObject< Derived >
static const std::string & classTypeName ()
Static Public Attributes inherited from drogon::HttpMiddleware< Derived, true >
static constexpr bool isAutoCreation

Detailed Description

template<class Derived, bool AutoCreation = true>
class drogon::HttpOptionsMiddlewareImpl< Derived, AutoCreation >

Simple middleware that tags OPTIONS requests.

It adds the attribute "drogon.customCORShandling" to the request, so that HttpServer does not handle CORS for them internally.

This allows custom CORS handling via the path handlers. For example to restrict the origins, headers allowed, specify a max age to avoid OPTIONS on every request, etc.

Just register it:

  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::Options, "drogon::HttpOptionsMiddleware")

Member Function Documentation

◆ invoke()

template<class Derived, bool AutoCreation = true>
void drogon::HttpOptionsMiddlewareImpl< Derived, AutoCreation >::invoke ( const HttpRequestPtr & req,
MiddlewareNextCallback && nextCb,
MiddlewareCallback && mcb )
inlineoverridevirtual

This virtual function should be overridden in subclasses.

Example:

void invoke(const HttpRequestPtr &req,
MiddlewareNextCallback &&nextCb,
MiddlewareCallback &&mcb) override
{
if (req->path() == "/some/path") {
// intercept directly
return;
}
// Do something before calling the next middleware
nextCb([mcb = std::move(mcb)](const HttpResponsePtr &resp) {
// Do something after the next middleware returns
mcb(resp);
});
}
void invoke(const HttpRequestPtr &req, MiddlewareNextCallback &&nextCb, MiddlewareCallback &&mcb) override
Definition HttpMiddleware.h:171
static HttpResponsePtr newNotFoundResponse(const HttpRequestPtr &req=HttpRequestPtr())
Create a response which returns a 404 page.

Implements drogon::HttpMiddlewareBase.


The documentation for this class was generated from the following file: