Sunday, August 15, 2010

Qt4 Http Request Parser

Qt 4.4 introduces QNetworkRequest and QNetworkAccessManager to help you with your HTTP client request. But if you want parse an HTTP Request, because you're writing an HTTP server, it seems that there's nothing already done (comment here, if I've missed it).

So, this morning I've written a little class that help you with HTTP Request parse:

  • static HttpRequest fromData (const QByteArray& data);
  • static HttpRequest fromStream (QIODevice *buffer);

There's a couple of method that you can use to parse your request data, and some others method to retrieve headers, method type, url and body data.

Qt is used only for QHash, QByteArray and QIODevice classes, you've to implement all the "socket" logic, to handle your client request and response.

The Source code is available here: Qt4 Http Request Parser Source Code.


  1. Well it's not exactly the same but do you know if it's possible to write a webservice with Qt?

    The idea it's to replace some .NET or PHP webservices with a Qt application to boost the performance and to have just one binary to install.

    In the same idea is that possible to write a full website with Qt?

  2. Yep, why not... Tornado ( does it, but it's written in python. But can be a great starting point.

  3. @zelegolas: you can look at this project, too:

  4. @Matteo
    loll :) I like python too but I want to replace all a bunch of crap by just one single and clean binary for all platform. Without nothing to install on the server except the binary.
    With python I will have the same pb that php or perl.

    Thanks I will take a look...

  5. Python, Perl, PHP are good when they are used to develop internal tools. When you want to sale a solution, because the solution may be need a couple of extension, it's will become difficult to have the right environment. Each client have is own specific context and some time it's a lot of troubles to run your application on their environment.
    With Qt we can create a single executable easier to install. Because it's compile you don't have to expose your code. And because it's in C++ you can optimize what you generate.