Orbit Framework
An ultra-modern, asynchronous, cross-platform C++ Web Framework.
Loading...
Searching...
No Matches
Middlewares

Built-in middleware components for Orbit HTTP server. More...

Classes

struct  middleware::CorsOptions
 Options for configuring CORS (Cross-Origin Resource Sharing). More...
 
class  middleware::Csrf
 Cross-Site Request Forgery (CSRF) protection middleware. More...
 
class  middleware::DistributedRateLimiter
 Distributed rate limiter middleware using Redis. More...
 
class  middleware::Metrics
 Middleware for tracking HTTP request metrics. More...
 
struct  middleware::OAuth2Config
 Configuration for OAuth2 middleware. More...
 
class  middleware::OAuth2
 Middleware for handling OAuth2 authentication. More...
 
struct  middleware::ProxyOptions
 Options for Proxy middleware. More...
 
struct  middleware::TargetNode
 A target node for the load balancer. More...
 
struct  middleware::LoadBalancerOptions
 Options for LoadBalancer middleware. More...
 
class  middleware::RateLimiter
 Middleware for in-memory rate limiting. More...
 
class  middleware::SessionManager
 Middleware for managing sessions using Redis. More...
 
struct  middleware::SchemaField
 Represents a field in a validation schema. More...
 

Enumerations

enum class  middleware::JsonType {
  middleware::JsonType::STRING , middleware::JsonType::NUMBER , middleware::JsonType::BOOLEAN , middleware::JsonType::OBJECT ,
  middleware::JsonType::ARRAY
}
 Enumeration of JSON data types. More...
 

Functions

routing::Middleware middleware::compress ()
 Returns a middleware that automatically compresses the HTTP response body using GZIP.
 
routing::Middleware middleware::cors (CorsOptions options=CorsOptions{})
 Returns a middleware that handles CORS.
 
auto middleware::csrf_protection (const std::string &cookie_name="csrf_token", const std::string &header_name="X-CSRF-Token")
 Helper to create a CSRF protection middleware handler.
 
auto middleware::distributed_rate_limit (const std::string &redis_host, int redis_port, size_t max_requests, std::chrono::seconds window)
 Helper to create a distributed rate limiting middleware handler.
 
routing::Middleware middleware::jwt_auth (const std::string &secret_key)
 JWT Authentication middleware using HMAC-SHA256 (HS256).
 
routing::Middleware middleware::proxy (ProxyOptions options)
 Returns a middleware that proxies requests to a single target URL (ip/hostname and port).
 
routing::Middleware middleware::proxy (const std::string &target_host, int target_port)
 Returns a middleware that proxies requests to a single target URL.
 
routing::Middleware middleware::load_balancer (LoadBalancerOptions options)
 Returns a middleware that load-balances requests across multiple upstream nodes.
 
routing::Middleware middleware::load_balancer (const std::vector< TargetNode > &nodes)
 Returns a middleware that load-balances requests across multiple upstream nodes.
 
auto middleware::rate_limit (size_t max_requests, std::chrono::seconds window)
 Helper function to easily register rate limiting middleware.
 
auto middleware::session (const std::string &redis_host, int redis_port)
 Helper to create a session management middleware handler.
 
routing::Middleware middleware::static_files (const std::string &directory)
 Returns a middleware that serves static files from a directory.
 
routing::Middleware middleware::validate_json (const std::vector< SchemaField > &schema)
 Returns a middleware that parses and validates a JSON request body.
 

Detailed Description

Built-in middleware components for Orbit HTTP server.

Enumeration Type Documentation

◆ JsonType

enum class middleware::JsonType
strong

Enumeration of JSON data types.

Enumerator
STRING 
NUMBER 
BOOLEAN 
OBJECT 
ARRAY 

Function Documentation

◆ compress()

routing::Middleware middleware::compress ( )

Returns a middleware that automatically compresses the HTTP response body using GZIP.

Compresses the response if the client sent the Accept-Encoding: gzip header.

Returns
routing::Middleware The compression middleware handler.

◆ cors()

routing::Middleware middleware::cors ( CorsOptions  options = CorsOptions{})

Returns a middleware that handles CORS.

Parameters
optionsThe CORS configuration options.
Returns
routing::Middleware The CORS middleware handler.

◆ csrf_protection()

auto middleware::csrf_protection ( const std::string &  cookie_name = "csrf_token",
const std::string &  header_name = "X-CSRF-Token" 
)
inline

Helper to create a CSRF protection middleware handler.

Parameters
cookie_nameThe name of the cookie to store the CSRF token.
header_nameThe name of the header expected in requests.
Returns
A middleware handler function.

◆ distributed_rate_limit()

auto middleware::distributed_rate_limit ( const std::string &  redis_host,
int  redis_port,
size_t  max_requests,
std::chrono::seconds  window 
)
inline

Helper to create a distributed rate limiting middleware handler.

Parameters
redis_hostThe Redis server host.
redis_portThe Redis server port.
max_requestsMaximum number of requests allowed in the time window.
windowThe time window for rate limiting.
Returns
A middleware handler function.

◆ jwt_auth()

routing::Middleware middleware::jwt_auth ( const std::string &  secret_key)

JWT Authentication middleware using HMAC-SHA256 (HS256).

Verifies the "Authorization: Bearer <token>" header against the secret key. If valid, the decoded JSON payload is placed into HttpRequest::user. If invalid or missing, it responds with a 401 Unauthorized and stops the pipeline.

app.use(middleware::jwt_auth("my_super_secret_key"));
routing::Middleware jwt_auth(const std::string &secret_key)
JWT Authentication middleware using HMAC-SHA256 (HS256).
Definition JwtAuth.cpp:65
Parameters
secret_keyThe secret key used for HMAC-SHA256 verification.
Returns
routing::Middleware The JWT authentication middleware handler.

◆ load_balancer() [1/2]

routing::Middleware middleware::load_balancer ( const std::vector< TargetNode > &  nodes)

Returns a middleware that load-balances requests across multiple upstream nodes.

Parameters
nodesA vector of target nodes.
Returns
routing::Middleware The load balancer middleware handler.

◆ load_balancer() [2/2]

routing::Middleware middleware::load_balancer ( LoadBalancerOptions  options)

Returns a middleware that load-balances requests across multiple upstream nodes.

Currently implements a Round-Robin algorithm.

Parameters
optionsThe load balancer options.
Returns
routing::Middleware The load balancer middleware handler.

◆ proxy() [1/2]

routing::Middleware middleware::proxy ( const std::string &  target_host,
int  target_port 
)

Returns a middleware that proxies requests to a single target URL.

Parameters
target_hostThe target host.
target_portThe target port.
Returns
routing::Middleware The proxy middleware handler.

◆ proxy() [2/2]

routing::Middleware middleware::proxy ( ProxyOptions  options)

Returns a middleware that proxies requests to a single target URL (ip/hostname and port).

Parameters
optionsThe proxy configuration options.
Returns
routing::Middleware The proxy middleware handler.

◆ rate_limit()

auto middleware::rate_limit ( size_t  max_requests,
std::chrono::seconds  window 
)
inline

Helper function to easily register rate limiting middleware.

Parameters
max_requestsMaximum number of requests allowed in the time window.
windowThe time window for rate limiting.
Returns
A middleware handler function.

◆ session()

auto middleware::session ( const std::string &  redis_host,
int  redis_port 
)
inline

Helper to create a session management middleware handler.

Parameters
redis_hostThe Redis server host.
redis_portThe Redis server port.
Returns
A middleware handler function.

◆ static_files()

routing::Middleware middleware::static_files ( const std::string &  directory)

Returns a middleware that serves static files from a directory.

If a file is found, it is served and the pipeline is stopped. If a file is not found, the pipeline continues.

Parameters
directoryThe directory containing static files.
Returns
routing::Middleware The static files middleware handler.

◆ validate_json()

routing::Middleware middleware::validate_json ( const std::vector< SchemaField > &  schema)

Returns a middleware that parses and validates a JSON request body.

If valid, the parsed JSON is available in req.json_body. If invalid, it intercepts the request and responds with a 422 Unprocessable Entity.

Parameters
schemaThe schema to validate against.
Returns
routing::Middleware The validation middleware handler.