$darkmode
Kourier 1.0.0
Kourier::HttpServer Class Reference

The HttpServer class provides the fastest HTTP server ever built. More...

Public Types

enum class  ServerError {
  NoError ,
  MalformedRequest ,
  TooBigRequest ,
  RequestTimeout
}
 This enum describes the server error that can occur while parsing an HTTP request. More...
 
enum class  ServerOption {
  WorkerCount ,
  TcpServerBacklogSize ,
  IdleTimeoutInSecs ,
  RequestTimeoutInSecs ,
  MaxUrlSize ,
  MaxHeaderNameSize ,
  MaxHeaderValueSize ,
  MaxHeaderLineCount ,
  MaxTrailerNameSize ,
  MaxTrailerValueSize ,
  MaxTrailerLineCount ,
  MaxChunkMetadataSize ,
  MaxRequestSize ,
  MaxBodySize ,
  MaxConnectionCount
}
 This enum describes the available server options that you can configure. See Configuring Server for further details. More...
 

Public Slots

void start (QHostAddress address, quint16 port)
 
void stop ()
 

Signals

void failed ()
 
void started ()
 
void stopped ()
 

Public Member Functions

 HttpServer ()
 
bool addRoute (HttpRequest::Method method, std::string_view path, void(*pFcn)(const HttpRequest &, HttpBroker &))
 
std::string_view errorMessage () const
 
int64_t serverOption (ServerOption option) const
 
void setErrorHandler (std::shared_ptr< ErrorHandler > pErrorHandler)
 
bool setServerOption (ServerOption option, int64_t value)
 
bool setTlsConfiguration (const TlsConfiguration &tlsConfiguration)
 

Detailed Description

The HttpServer class provides the fastest HTTP server ever built.

HttpServer streamlines the creation of REST-based services. With Kourier, you only have to configure and add handlers to an HttpServer instance before starting it to create a REST service.

HttpServer emits the started signal when all workers initialize successfully. Otherwise, if any worker fails while the server starts, HttpServer stops all workers and emits the failed signal after the last running worker stops. You can call errorMessage to get a description of the last error that occurred.

You can call stop to stop a running server. HttpServer emits the stopped signal after the last running worker stops.

To create a reliable service, you must be able to act whenever an error occurs. HttpServer provides the setErrorHandler method that you can use to set an error handler that HttpServer calls when an error occurs. HttpServer uses the set error handler to report what prevented it from calling a mapped handler. You can use the HttpServer::ServerError argument that HttpServer passes to the error handler to know why the server failed to call a mapped handler.

Member Enumeration Documentation

◆ ServerError

This enum describes the server error that can occur while parsing an HTTP request.

Enumerator
NoError 

No error has happened.

MalformedRequest 

An invalid HTTP request or a valid request with no handler mapped to its method/path.

TooBigRequest 

The request is larger than the server is allowed to support.

RequestTimeout 

Either the request was not parsed within RequestTimeoutInSecs or, after processing a request, no bytes from the next request were received within IdleTimeoutInSecs.

◆ ServerOption

This enum describes the available server options that you can configure. See Configuring Server for further details.

Enumerator
WorkerCount 

The number of workers the server should use to handle incoming requests. By default, HttpServer uses as many workers as available cores.

TcpServerBacklogSize 

The size of the backlog used to keep accepted connections.

IdleTimeoutInSecs 

The number of seconds a connection can idle before the server closes it.

RequestTimeoutInSecs 

The number of seconds the server can wait until the request is fully received.

MaxUrlSize 

Maximum size of request URL.

MaxHeaderNameSize 

Maximum size of header name.

MaxHeaderValueSize 

Maximum size of header value.

MaxHeaderLineCount 

Maximum number of field lines in the header block.

MaxTrailerNameSize 

Maximum size of trailer name.

MaxTrailerValueSize 

Maximum size of trailer value.

MaxTrailerLineCount 

Maximum number of field lines in the trailer section.

MaxChunkMetadataSize 

Maximum size of the chunk metadata.

MaxRequestSize 

Maximum request size.

MaxBodySize 

Maximum request body size.

MaxConnectionCount 

Maximum number of connections the server can keep.

Constructor & Destructor Documentation

◆ HttpServer()

Kourier::HttpServer::HttpServer ( )

Creates a HttpServer. You can call addRoute to map paths to request handlers and setServerOption to configure the server.

Member Function Documentation

◆ addRoute()

bool Kourier::HttpServer::addRoute ( HttpRequest::Method  method,
std::string_view  path,
void(*)(const HttpRequest &, HttpBroker &)  pFcn 
)

Maps the handler identified by pFcn to the given path for requests containing the given method. HttpServer always picks the most specific path for handling a given request. You can call HttpBroker::setQObject on the HttpBroker object that HttpServer passes to the handler function to postpone responding until after the handler finishes. HttpServer monitors responses through the given broker, and after you write a complete response for the current request, HttpServer destroys any QObject you set on the broker and processes the next request. See Adding Handlers for more details.

◆ errorMessage()

std::string_view Kourier::HttpServer::errorMessage ( ) const

Returns a textual description for the last error that occurred. If no error has occurred, HttpServer returns an empty string view.

◆ failed

Kourier::HttpServer::failed ( )
signal

HttpServer emits this signal if any worker fails to start.

◆ serverOption()

int64_t Kourier::HttpServer::serverOption ( ServerOption  option) const

Returns the set value for the given option. See Configuring Server for more details.

◆ setErrorHandler()

void Kourier::HttpServer::setErrorHandler ( std::shared_ptr< ErrorHandler pErrorHandler)

Sets pErrorHandler as the error handler. HttpServer does not serialize access to the given error handler.

◆ setServerOption()

bool Kourier::HttpServer::setServerOption ( ServerOption  option,
int64_t  value 
)

Sets the value for the given option. See Configuring Server for more details.

◆ setTlsConfiguration()

bool Kourier::HttpServer::setTlsConfiguration ( const TlsConfiguration tlsConfiguration)

Makes HttpServer encrypt connections according to the given tlsConfiguration.

◆ start

void Kourier::HttpServer::start ( QHostAddress  address,
quint16  port 
)
slot

Starts HttpServer. HttpServer creates as many workers as set in the worker count option value and makes them listen to the given address and port. HttpServer emits started when all workers start, or emits failed if any error occurs. If the server fails to start, you can call errorMessage to get a textual description of the last error that occurred.

◆ started

Kourier::HttpServer::started ( )
signal

HttpServer emits this signal when all workers finish starting.

◆ stop

void Kourier::HttpServer::stop ( )
slot

Stops HttpServer. HttpServer emits stopped when all workers stop.

◆ stopped

Kourier::HttpServer::stopped ( )
signal

HttpServer emits this signal when all workers stop.