QtWebApp
stefanfrings::HttpListener Class Reference

Listens for incoming TCP connections and and passes all incoming HTTP requests to your implementation of HttpRequestHandler, which processes the request and generates the response (usually a HTML document). More...

#include <httplistener.h>

Inheritance diagram for stefanfrings::HttpListener:
Collaboration diagram for stefanfrings::HttpListener:

Signals

void handleConnection (tSocketDescriptor socketDescriptor)
 Sent to the connection handler to process a new incoming connection. More...
 

Public Member Functions

 HttpListener (const QSettings *settings, HttpRequestHandler *requestHandler, QObject *parent=nullptr)
 Constructor. More...
 
virtual ~HttpListener ()
 Destructor.
 
void listen ()
 Restart listeing after close().
 
void close ()
 Closes the listener, waits until all pending requests are processed, then closes the connection pool.
 

Protected Member Functions

void incomingConnection (tSocketDescriptor socketDescriptor)
 Serves new incoming connection requests.
 

Detailed Description

Listens for incoming TCP connections and and passes all incoming HTTP requests to your implementation of HttpRequestHandler, which processes the request and generates the response (usually a HTML document).

Example for the required settings in the config file:

;host=192.168.0.100
port=8080

readTimeout=60000
maxRequestSize=16000
maxMultiPartSize=1000000

minThreads=1
maxThreads=10
cleanupInterval=1000

;sslKeyFile=ssl/server.key
;sslCertFile=ssl/server.crt
;caCertFile=ssl/ca.crt
;verifyPeer=false

The optional host parameter binds the listener to a specific network interface, otherwise the server accepts connections from any network interface on the given port.

The readTimeout value defines the maximum time to wait for a complete HTTP request.

MaxRequestSize is the maximum size of a HTTP request. In case of multipart/form-data requests (also known as file-upload), the maximum size of the body must not exceed maxMultiPartSize.

After server start, the size of the thread pool is always 0. Threads are started on demand when requests come in. The cleanup timer reduces the number of idle threads slowly by closing one thread in each interval. But the configured minimum number of threads are kept running.

See also
HttpConnectionHandlerPool for description of the optional ssl settings

Definition at line 57 of file httplistener.h.

Constructor & Destructor Documentation

◆ HttpListener()

HttpListener::HttpListener ( const QSettings *  settings,
HttpRequestHandler requestHandler,
QObject *  parent = nullptr 
)

Constructor.

Creates a connection pool and starts listening on the configured host and port.

Parameters
settingsConfiguration settings, usually stored in an INI file. Must not be 0. Settings are read from the current group, so the caller must have called settings->beginGroup(). Because the group must not change during runtime, it is recommended to provide a separate QSettings instance that is not used by other parts of the program. The HttpListener does not take over ownership of the QSettings instance, so the caller should destroy it during shutdown.
requestHandlerProcesses each received HTTP request, usually by dispatching to controller classes.
parentParent object.
Warning
Ensure to close or delete the listener before deleting the request handler.

Definition at line 13 of file httplistener.cpp.

Member Function Documentation

◆ handleConnection

void stefanfrings::HttpListener::handleConnection ( tSocketDescriptor  socketDescriptor)
signal

Sent to the connection handler to process a new incoming connection.

Parameters
socketDescriptorreferences the accepted connection.

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