STTNet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Member Functions | List of all members
stt::network::HttpServer Class Reference

Http/HttpServer server operation class. More...

#include <sttnet_English.h>

Inheritance diagram for stt::network::HttpServer:
stt::network::TcpServer

Public Member Functions

void putTask (const std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)> &fun, HttpServerFDHandler &k, HttpRequestInformation &inf)
 Submit a task to the worker thread pool. More...
 
 HttpServer (const unsigned long long &maxFD=1000000, const int &buffer_size=256, const bool &security_open=true, const int &connectionNumLimit=10, const int &connectionSecs=1, const int &connectionTimes=3, const int &requestSecs=1, const int &requestTimes=20, const int &checkFrequency=30, const int &connectionTimeout=30)
 Constructor. More...
 
void setSecuritySendBackFun (std::function< void(HttpServerFDHandler &k, HttpRequestInformation &inf)> fc)
 Set the callback invoked when an information security policy is violated. More...
 
void setGlobalSolveFunction (std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)> fc)
 Sets a global backup function. More...
 
void setFunction (const std::string &key, std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)> fc)
 Register a callback function for a specific key. More...
 
void setGetKeyFunction (std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)> parseKeyFun)
 Set the callback function used to parse the key. More...
 
bool startListen (const int &port, const int &threads=8)
 Start the HTTP server listening loop. More...
 
 ~HttpServer ()
 Destructor. More...
 
- Public Member Functions inherited from stt::network::TcpServer
void putTask (const std::function< int(TcpFDHandler &k, TcpInformation &inf)> &fun, TcpFDHandler &k, TcpInformation &inf)
 Add a task to a worker thread pool and have it completed by worker threads. More...
 
 TcpServer (const unsigned long long &maxFD=1000000, const int &buffer_size=256, const bool &security_open=true, const int &connectionNumLimit=20, const int &connectionSecs=1, const int &connectionTimes=6, const int &requestSecs=1, const int &requestTimes=40, const int &checkFrequency=60, const int &connectionTimeout=60)
 Constructor. More...
 
bool startListen (const int &port, const int &threads=8)
 Start the TCP server listening program. More...
 
bool setTLS (const char *cert, const char *key, const char *passwd, const char *ca)
 Enable TLS encryption and configure server-side certificate and key. More...
 
void redrawTLS ()
 Revoke TLS encryption, CA certificate, etc. More...
 
void setSecuritySendBackFun (std::function< void(TcpFDHandler &k, TcpInformation &inf)> fc)
 Set the callback invoked when an information security policy is violated. More...
 
void setGlobalSolveFunction (std::function< bool(TcpFDHandler &k, TcpInformation &inf)> fc)
 Set global fallback function. More...
 
void setFunction (const std::string &key, std::function< int(TcpFDHandler &k, TcpInformation &inf)> fc)
 Register a callback function for a specific key. More...
 
void setGetKeyFunction (std::function< int(TcpFDHandler &k, TcpInformation &inf)> parseKeyFun)
 Set the callback function used to parse the key. More...
 
bool stopListen ()
 Stop listening. More...
 
bool close ()
 Close listening and all connected sockets. More...
 
virtual bool close (const int &fd)
 Close the connection of a specific socket. More...
 
void setCloseFun (std::function< void(const int &fd)> closeFun)
 set function after tcp connection close More...
 
bool isListen ()
 Return the listening status of the object. More...
 
SSL * getSSL (const int &fd)
 Query the connection with the server, pass in the socket, and return the encrypted SSL handle. More...
 
 ~TcpServer ()
 Destructor of TcpServer class. More...
 

Additional Inherited Members

- Protected Attributes inherited from stt::network::TcpServer
std::queue< WorkerMessagefinishQueue
 
stt::system::WorkerPoolworkpool
 
unsigned long buffer_size
 
unsigned long long maxFD
 
security::ConnectionLimiter connectionLimiter
 
TcpFDInfclientfd
 
int flag1 =true
 
bool unblock
 
SSL_CTX * ctx =nullptr
 
bool TLS =false
 
bool security_open
 
int workerEventFD
 
int serverType
 
int connectionSecs
 
int connectionTimes
 
int requestSecs
 
int requestTimes
 
int checkFrequency
 

Detailed Description

Http/HttpServer server operation class.

Note
support http/1.0 1.1

Constructor & Destructor Documentation

stt::network::HttpServer::HttpServer ( const unsigned long long &  maxFD = 1000000,
const int &  buffer_size = 256,
const bool &  security_open = true,
const int &  connectionNumLimit = 10,
const int &  connectionSecs = 1,
const int &  connectionTimes = 3,
const int &  requestSecs = 1,
const int &  requestTimes = 20,
const int &  checkFrequency = 30,
const int &  connectionTimeout = 30 
)
inline

Constructor.

By default, allows up to 1,000,000 concurrent connections, allocates a maximum receive buffer of 256 KB per connection, and enables the security module.

Note
Enabling the security module introduces additional overhead and may impact performance.
Parameters
maxFDMaximum number of connections this server instance can accept. Default: 1,000,000.
buffer_sizeMaximum amount of data a single connection is allowed to receive, in kilobytes (KB). Default: 256 KB.
security_openWhether to enable the security module.
  • true : enable security checks (default)
  • false : disable security checks
connectionNumLimitMaximum number of concurrent connections allowed per IP. Default: 10.
connectionSecsTime window (in seconds) for connection rate limiting. Default: 1 second.
connectionTimesMaximum number of connection attempts allowed within connectionSecs seconds. Default: 3.
requestSecsTime window (in seconds) for request rate limiting. Default: 1 second.
requestTimesMaximum number of requests allowed within requestSecs seconds. Default: 20.
checkFrequencyFrequency (in seconds) for checking zombie/idle connections. -1 disables zombie connection detection. Default: 30 seconds.
connectionTimeoutIf a connection has no activity for this many seconds, it is considered a zombie connection. -1 means no timeout (infinite). Default: 30 seconds.
stt::network::HttpServer::~HttpServer ( )
inline

Destructor.

Member Function Documentation

void stt::network::HttpServer::putTask ( const std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)> &  fun,
HttpServerFDHandler k,
HttpRequestInformation inf 
)

Submit a task to the worker thread pool.

Note
Slow or potentially blocking I/O operations should be dispatched to the worker thread pool.
Warning
The callable object must strictly follow the required return value conventions.
Parameters
funThe callable object to be executed by the worker thread pool.
  • Parameters: HttpServerFDHandler &k - Reference to the socket operation object associated with the client connection. HttpRequestInformation &inf - Client request information, including data, processing progress, and state machine information.
  • Return value: -2 : Processing failed and the connection must be closed. -1 : Processing failed but the connection should remain open. 1 : Processing succeeded.
kReference to the socket operation object associated with the client connection.
infReference to the client request information.
void stt::network::HttpServer::setFunction ( const std::string &  key,
std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)>  fc 
)
inline

Register a callback function for a specific key.

Note
Multiple callbacks can be registered for the same key. The framework executes them sequentially in the order they are registered. Processing can also be dispatched to the worker thread pool by returning appropriate values.
Warning
The callable object must strictly follow the required return value conventions.
Parameters
keyThe key used to locate the corresponding callback functions.
fcA function or function object that defines the logic for handling client requests.
  • Parameters: HttpServerFDHandler &k - Reference to the socket operation object associated with the client connection. HttpRequestInformation &inf - Client request information, including data, processing progress, and state machine information.
  • Return value: -2 : Processing failed and the connection must be closed. -1 : Processing failed but the connection should remain open. 0 : Processing has been dispatched to the worker thread pool and the framework should wait for completion. 1 : Processing succeeded.
httpserver->setFunction("/ping",
[](HttpServerFDHandler &k, HttpRequestInformation &inf) -> int {
k.sendBack("pong");
return 1;
});
httpserver->setFunction("/ping",
[](HttpServerFDHandler &k, HttpRequestInformation &inf) -> int {
httpserver->putTask(
[](HttpServerFDHandler &k, HttpRequestInformation &inf) -> int {
k.sendBack("pong");
return 1;
},
k,
inf
);
return 0;
});
void stt::network::HttpServer::setGetKeyFunction ( std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)>  parseKeyFun)
inline

Set the callback function used to parse the key.

Note
This function extracts the key from the input parameters and stores it in the ctx hash table of HttpRequestInformation. The framework uses this key to locate the registered processing callbacks.
Warning
The callable object must strictly follow the required return value conventions.
Parameters
parseKeyFunThe callback function used to parse the key.
  • Parameters: HttpServerFDHandler &k - Reference to the socket operation object associated with the client connection. HttpRequestInformation &inf - Client request information, including data, processing progress, and state machine information.
  • Return value: -2 : Processing failed and the connection must be closed. -1 : Processing failed but the connection should remain open. 0 : Processing has been dispatched to the worker thread pool and the framework should wait for completion. 1 : Processing succeeded.
httpserver->setGetKeyFunction(
[](HttpServerFDHandler &k, HttpRequestInformation &inf) -> int {
inf.ctx["key"] = inf.loc;
return 1;
});
void stt::network::HttpServer::setGlobalSolveFunction ( std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)>  fc)
inline

Sets a global backup function.

Note
The global backup function will be called when the corresponding callback function is not found. Multiple backup functions can be set, and the framework will execute them in the order they are set. You can also set the process of throwing the data into the worker thread pool; just be sure to set different return values.
Parameters
fcA function or function object used for processing logic after receiving a message from the client.
  • Parameters: HttpServerFDHandler &k - A reference to the operation object of the socket connected to the client.

HttpRequestInformation &inf - Client information, saved data, processing progress, state machine information, etc.

  • - Return value: -2: Processing failed and the connection needs to be closed -1: Processing failed but the connection does not need to be closed 0: The processing flow has been thrown into the worker thread pool and needs to wait for processing to complete 1: Processing succeeded
void stt::network::HttpServer::setSecuritySendBackFun ( std::function< void(HttpServerFDHandler &k, HttpRequestInformation &inf)>  fc)
inline

Set the callback invoked when an information security policy is violated.

This callback is executed when a client violates a security rule (e.g. rate limiting, abnormal request behavior, or other security checks). After the callback is executed, the connection will be closed.

Note
The callback is invoked once per security violation. The connection is closed unconditionally after the callback returns.
Parameters
fcA function or function object used to handle the response sent back to the client upon a security violation.

Callback parameters:

  • HttpServerFDHandler &k Reference to the handler object that operates on the client connection/socket.
  • HttpRequestInformation &inf Client request information, including buffered data, processing progress, and HTTP state/context.
Note
The callback does not return a value. The connection will be closed regardless of the outcome of the callback.
bool stt::network::HttpServer::startListen ( const int &  port,
const int &  threads = 8 
)
inline

Start the HTTP server listening loop.

Parameters
portThe port to listen on.
threadsNumber of worker/consumer threads (default: 8).
Returns
true if the server starts listening successfully, false otherwise.

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