Links

Download source code
Online class documentation
QT Framework
LGPL License

Last changes

1.3.6 16.09.2014
Fixed DualFileLogger produces no output.

1.3.5 11.06.2014
Fixed a multi-threading issue with race condition in StaticFileController.

1.3.4 04.06.2014
Fixed wrong content type when the StaticFileController returns a cached index.html.

1.3.3 17.03.2014
Improved security of StaticFileController by denying "/.." in any position in request path.
Improved performance of StaticFileController a little.
New method HttpResponse::redirect(url).
Fixed a missing return in StaticFileController.

1.3.2 08.01.2014
Fixed HTTP Server ignoring URL parameters when the request contains POST parameters.

1.3.1 15.08.2013
Fixed HTTP server not accepting connections on 64bit OS with QT 5.

1.3.0 20.04.2013
Updated for compatibility with QT 5.x. You may still use QT 4.7 or 4.8, if you like.
Also added support for logging source file name, line number and function name, which is new in QT 5.

1.2.13 03.03.2013
Fixed Logger writing wrong timestamp for buffered messages.
Improved shutdown procedure. The webserver now processes all final signals before the destructor finishes.

1.2.12 01.03.2013
Fixed HttpResponse sending first part of data repeatedly when the amount of data is larger than the available memory for I/O buffer.

...

QtWebApp HTTP Server in C++

Deutsche Version dieser Seite

Develop web-server applications for Linux, Windows, Mac OS or Android in C++ with QtWebApp. The API is similar to the Java Servlet API. A very small example:

// The controller responds to HTTP requests
void MyController::service(HttpRequest& request, HttpResponse& response) {

    // Get some request parameters
    QByteArray path=request.getPath();
    QByteArray username=request.getParameter("username");

    // Set some headers
    response.setHeader("Content-Type", "text/html; charset=ISO-8859-1");
    response.setCookie(HttpCookie("myCookie","any value",600));

    // Generate a website as response
    response.write("<html><body>");
    response.write("Hello ");
    response.write(username);
    response.write("</body></html>");
}

// The main program starts a listener that uses the controller from above
int main(int argc, char *argv[]) {
    QCoreApplication* app=new QCoreApplication(argc,argv);
    
    QSettings* settings=new QSettings("configfile.ini",QSettings::IniFormat,app);
    MyController* controller=new MyController(app);
    HttpListener* listener=new HttpListener(settings,controller,app);

    return app->exec();
}

//configfile.ini
port=8080
minThreads=1
maxThreads=10
cleanupInterval=1000
readTimeout=60000
maxRequestSize=16000
maxMultiPartSize=1000000

QtWebApp contains:

The HTTP server processes incoming requests in multiple threads, that are managed in a dynamic pool. It supports persistent connections, sessions, and file-uploads. The example above answers all requests in a single controller. However real applications would branch to more specific classes depending on the requested path, as the demo application does.

A simple multi-language capable template engine is included, that fills placeholders in HTML files with dynamic values at runtime. The template engine also supports conditional output and loops. Other larger template engines, such as ClearSilver, may be used as an alternative.

The logger plugs into QT and redirects log messages from qDebug(...) to qFatal(...) to files, while they are enriched with additional attributes like timestamp, thread ID, session ID and some more. Changes to the configuration file of the logger become active automatically without program restart.

The QtService library from Nokia allows you to start the application as a Linux Daemon or Windows Service. But you may also embed the webserver in your existing program.

The small memory requirement of about 2MB qualifies the web server to be used in embedded systems and Android.

You may download it for free and use the program under the conditions of the LGPL License.

Thanks

The current high quality of the program is not only the result of my own work. Some people tested the program in production environments and suggested bug fixes. I'm thankful for their help.