Introducing the Qt Http Server

January 25, 2019 Jesús Fernández

First things first, Happy new year dear Qt users!

I’m sorry, I’m really late 🙁

Some time ago we decided to avoid repeating the same tedious code to write our own “Simple” Http Servers. So we started writing a Qt HTTP server with several essential features. The word simple is quoted because something planned to be “simple” evolved into a piece of code hard to maintain.
To give few numbers during the last two years, I wrote three times the same code to parse request headers, write response headers, deal with connections, … And I know, I’m not the only one who did the same (just check GitHub).

To take a look into the code, you can clone it with:

git clone --recursive https://codereview.qt-project.org/qt-labs/qthttpserver

You can manually build it using the last Qt release.
Before listing the features let’s show the mandatory hello world example:

#include <QtCore>
#include <QtHttpServer>

int main(int argc, char **argv) {
  QCoreApplication app(argc, argv);
  QHttpServer httpServer;
  httpServer.route("/", []() {
    return "Hello world";
  });
  httpServer.listen(QHostAddress::Any);
  return app.exec();
}

At this moment it’s a qt-labs project, and it’s not officially part of the Qt distribution, but at some point, we’ll add it.

Some features already implemented are:

  • HTTP 1.1
  • HTTPS but not yet merged
  • Customizable Routing
  • WebSockets integration
  • Error handling

I have planned some use-cases for this module:

  • Change the current embedded web server (and WebSockets) in the WebGL plugin to make it easy to create your own custom solutions based on the plugin.
  • Provide a way to serve an application built with WebAssamebly with live communication with the server via WebSockets and maybe using QtWebChannel.
  • Embed a web server into your application running on your desktop machine or embedded device.
  • Create a REST API
  • HTTP Server for QML

If you’re interested in this project, don’t forget to clone it and maybe if you’re concerned you can consider contributing or provide some feedback in QTBUG-60105.

Next week Mikhail Svetkin will write more about the routing API. Stay tuned 😀

The post Introducing the Qt Http Server appeared first on Qt Blog.

Previous
Qt Virtual Tech Summit Registration is Open!
Qt Virtual Tech Summit Registration is Open!

Did you miss Qt World Summit 2018? Fear not. On 12-14 February, the Qt Virtual Tech Summit and Qt experts w...

Next Article
Qt Visual Studio Tools 2.3.1 Released
Qt Visual Studio Tools 2.3.1 Released

The Qt VS Tools version 2.3.1 has now been released to the Visual Studio Marketplace. Important changes inc...