Hobrasoft httpd server
Embedded HTTP server for Qt and C++
Public Member Functions | Protected Member Functions | Private Slots | Private Attributes | List of all members
HobrasoftHttpd::HttpServer Class Reference

General single-threaded, event-driven HTTP server. More...

#include <httpserver.h>

Inheritance diagram for HobrasoftHttpd::HttpServer:

Public Member Functions

 HttpServer (QObject *parent)
 Constructor using default HttpSettings object.
 HttpServer (const HttpSettings *settings, QObject *parent)
 Constructor using your own HttpSettings. More...
void start ()
 Starts of restart HttpServer with new parameters.
virtual HttpRequestHandlerrequestHandler (HttpConnection *)
 Creates new request handler and returs pointer to it. More...
const HttpSettingssettings () const
 Returs pointer to HttpSettings used in the HttpServer.
QVariant webStatus () const
QList< QPointer< HobrasoftHttpd::HttpConnection > > connections () const

Protected Member Functions

void close ()
 Closes the QTcpServer bind with your HttpServer.

Private Slots

void slotNewConnection ()
 Slot is invoked when QTcpServer::newConnection() signal arrived. More...
void slotConnectionClosed (QObject *)

Private Attributes


Detailed Description

General single-threaded, event-driven HTTP server.

Static content server

The class can be used as a static content server without extending. The .html and .shtml formats are recognized. Html files are returned without any change. In shtml file all occurences of #include pragma is found and replaced with requested file. Only one pragma on line is possible. No other strings are allowed on line with #include pragma:

<!-- #include "file.html" -->

Extending server

If you want to use some dynamic content you have to reimplement the Http::requestHandler() method. The method is called for every request and it must return new instance of your own HttpRequestHandler derived class:

HttpRequestHandler *Httpd::requestHandler(HttpConnection *connection) {
return new MyOwnRequestHandler(connection);

The class MyOwnRequestHandler() is derived from HttpRequestHandler where the HttpRequestHandler::service() is reimplemented:

void RequestMapper::service(HttpRequest *request, HttpResponse *response) {
QString path = request->path();
#define ROUTER(address, hclass) \
if (path.startsWith(address)) { \
HttpRequestHandler *controller = new hclass (connection()); \
controller->service(request, response); \
return; \
ROUTER("/translations", ControllerTranslations);
ROUTER("/errorevents", ControllerErrorEvents);
ROUTER("/callqueue", ControllerCallQueue);
HttpRequestHandler::service(request, response);


HttpServer class has two constructors:

When the first constructor is used then the default QSettings is used to read the configuration. In the second constructor you can use your own instance of HttpSettings class or your own derived class. Using different configuration classes you can start multiple instances of HttpServer in your application each listening on different addresses or ports.

Definition at line 88 of file httpserver.h.

Constructor & Destructor Documentation

◆ HttpServer()

HttpServer::HttpServer ( const HttpSettings settings,
QObject *  parent 

Constructor using your own HttpSettings.

When using different settings it is possible to start multiple instances of the HttpServer.

Definition at line 26 of file httpserver.cpp.

Member Function Documentation

◆ requestHandler()

HttpRequestHandler * HttpServer::requestHandler ( HttpConnection parent)

Creates new request handler and returs pointer to it.

This method should be reimplemented in derived classes

Reimplemented in Example::Httpd::Httpd.

Definition at line 71 of file httpserver.cpp.

◆ slotNewConnection

void HttpServer::slotNewConnection ( )

Slot is invoked when QTcpServer::newConnection() signal arrived.

The method processes new request.

Definition at line 76 of file httpserver.cpp.

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