All Projects → azadkuh → Qhttp

azadkuh / Qhttp

Licence: mit
a light-weight and asynchronous HTTP library (both server & client) in Qt5 and c++14

Labels

Projects that are alternatives of or similar to Qhttp

tenplayer
Modern Music Player for Linux
Stars: ✭ 18 (-95.43%)
Mutual labels:  qt, qt5
Heimer
Heimer is a simple cross-platform mind map, diagram, and note-taking tool written in Qt.
Stars: ✭ 380 (-3.55%)
Mutual labels:  qt, qt5
asn1scc.IDE
Qt Creator plugin for asn1scc - ASN.1/ACN compiler for embedded systems
Stars: ✭ 15 (-96.19%)
Mutual labels:  qt, qt5
qSIP
VoIP/SIP client (softphone)
Stars: ✭ 20 (-94.92%)
Mutual labels:  qt, qt5
Raspberry Pi Cross Compilers
Latest GCC Cross Compiler & Native (ARM & ARM64) CI generated precompiled standalone toolchains for all Raspberry Pis. 🍇
Stars: ✭ 261 (-33.76%)
Mutual labels:  qt, qt5
screenshotgun
Open cross-platform screenshoter with cloud support and server part
Stars: ✭ 23 (-94.16%)
Mutual labels:  qt, qt5
QDashBoard
Sample dashboard developed with QML. Login, plots and several screens.
Stars: ✭ 34 (-91.37%)
Mutual labels:  qt, qt5
Jkqtplotter
an extensive Qt5 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
Stars: ✭ 246 (-37.56%)
Mutual labels:  qt, qt5
Project lemonlime
为了 OI 比赛而生的基于 Lemon + LemonPlus 的轻量评测系统 | 三大桌面系统支持
Stars: ✭ 255 (-35.28%)
Mutual labels:  qt, qt5
freeLib
freeLib - каталогизатор для библиотек LibRusEc и Flibusta
Stars: ✭ 19 (-95.18%)
Mutual labels:  qt, qt5
dannyAVgleDownloader
知名網站avgle下載器
Stars: ✭ 27 (-93.15%)
Mutual labels:  qt, qt5
Lan Share
Cross platform LAN File transfer application built with Qt C++ framework
Stars: ✭ 317 (-19.54%)
Mutual labels:  qt, qt5
Swiftly
Swiftly is an easy to use Qt/C++ web framework
Stars: ✭ 20 (-94.92%)
Mutual labels:  qt, qt5
QtExamples
Translations of the official Qt examples into PyQt5 (also PySide2) and more.
Stars: ✭ 39 (-90.1%)
Mutual labels:  qt, qt5
QUaServer
Qt C++ wrapper for open62541 server stack
Stars: ✭ 78 (-80.2%)
Mutual labels:  qt, qt5
chatRoom
从零开始实现一个聊天室客户端(用qt实现,在Windows和Linux环境下都行),和在Linux下实现服务器后台
Stars: ✭ 146 (-62.94%)
Mutual labels:  qt, qt5
Cqtdeployer
This project is used to deploy applications written using QML, qt or other С / С++ frameworks.
Stars: ✭ 225 (-42.89%)
Mutual labels:  qt, qt5
Qtusb
A cross-platform USB Module for Qt.
Stars: ✭ 245 (-37.82%)
Mutual labels:  qt, qt5
QtDemos
This is the collection of Qt demos to solve the problem from StackOverflow or I faced.
Stars: ✭ 23 (-94.16%)
Mutual labels:  qt, qt5
Qxmpp
Cross-platform C++ XMPP client and server library
Stars: ✭ 300 (-23.86%)
Mutual labels:  qt, qt5

QHttp

Table of contents

About

TOC

QHttp is a lightweight, asynchronous and fast HTTP library in c++14 / Qt5, containing both server and client side classes for managing connections, parsing and building HTTP requests and responses.

  • the objective of QHttp is being light weight with a simple API for Qt developers to implement RESTful web services in private (internal) zones. more
  • by using std::function and c++14 generic lambda, the API is intentionally similar to the Node.js' http module. Asynchronous and non-blocking HTTP programming is quite easy with QHttp. have a look at sample codes.
  • the fantastic nodejs/http-parser (which is a single pair of *.h/*.c files) is the only dependency of the QHttp.

attention: c++14 is the minimum requirement for version 3.0+ please see releases

This project was inspired by nikhilm/qhttpserver effort to implement a Qt HTTP server. QHttp pushes the idea further by implementing client side classes, better memory management, a lot more Node.js-like API, ...

Sample codes

TOC

a HelloWorld HTTP server by QHttp looks like:

int main(int argc, char** argv) {
    QCoreApplication app(argc, argv);

    using namespace qhttp::server;
    QHttpServer server(&app);
    server.listen( // listening on 0.0.0.0:8080
        QHostAddress::Any, 8080,
        [](QHttpRequest* req, QHttpResponse* res) {
            // http status 200
            res->setStatusCode(qhttp::ESTATUS_OK);
            // the response body data
            res->end("Hello World!\n");
            // automatic memory management for req/res
    });

    if ( !server.isListening() ) {
        qDebug("failed to listen");
        return -1;
    }

    return app.exec();
}

to request weather information by HTTP client:

int main(int argc, char** argv) {
    QCoreApplication app(argc, argv);

    using namespace qhttp::client;
    QHttpClient client(&app);
    QUrl        weatherUrl("http://wttr.in/tehran");

    client.request(qhttp::EHTTP_GET, weatherUrl, [](QHttpResponse* res) {
        // response handler, called when the incoming HTTP headers are ready

        // gather HTTP response data (HTTP body)
        res->collectData();

        // when all data in HTTP response have been read:
        res->onEnd([&]() {
            writeTo("weather.html", res->collectedData());

            // done! now quit the application
            qApp->quit();
        });

        // just for fun! print incoming headers:
        qDebug("\n[Headers:]");
        res->headers().forEach([](auto cit) {
            qDebug("%s : %s", cit.key().constData(), cit.value().constData());
        });
    });

    // set a timeout for the http connection
    client.setConnectingTimeOut(10000, []{
        qDebug("connecting to HTTP server timed out!");
        qApp->quit();
    });

    return app.exec();
}

Features

TOC

  • the only dependencies are: Qt5, c++14 and the http-parser
  • both TCP and UNIX (local) sockets are supported as backend.
  • separate namespaces for server and client classes.
  • HTTP server classes: QHttpServer, QHttpConnection, QHttpRequest and QHttpResponse.
  • optional HTTP client classes: QHttpClient, QHttpRequest and QHttpResponse. the client classes can be disabled at build time by commenting QHTTP_HAS_CLIENT in common.dir
  • automatic memory management of objects. Instances of connections, requests and replies will be deleted automatically when socket drops or disconnected.
  • PIMPL (Private implementaion) to achieve better ABI compatibility and cleaner API and faster compile time.
  • Asynchronous and non-blocking. You can handle thousands of concurrent HTTP connections efficiently by a single thread, although a multi-threaded HTTP server is easy to implement.
  • high throughput, I have tried the QHttp and gason++ to implement a REST/Json web service on an Ubuntu VPS (dual core + 512MB ram) with more than 5800 connections per second (stress test). On a MacBook Pro (i5 4258U, 8GB ram), QHttp easily reaches to more than 11700 connections / second. Generally QHttp is 1.5x ~ 3x faster than Node.js depending on your machine / OS.
  • Easily portable where ever Qt5 / c++14 works. Tested under:
    • Linux Ubuntu 12.04 ~ 16.04 LTS, g++ 5.3+
    • OS X 10.9+, clang 3.7+
    • Windows 7/8.1, msvs2015 / mingw (g++ 6.1)

Setup

TOC

instructions:

# first clone this repository:
$> git clone https://github.com/azadkuh/qhttp.git
$> cd qhttp

# prepare dependencies:
$qhttp/> ./update-dependencies.sh

# now build the library and the examples
$qhttp/> qmake -r qhttp.pro
$qhttp/> make -j 8

Multi-threading

TOC

As QHttp is asynchronous and non-blocking, your app can handle thousands of concurrent HTTP connections by a single thread.

in some rare scenarios you may want to use multiple handler threads (although it's not always the best solution):

  • there are some blocking APIs (QSql, system calls, ...) in your connection handler (adopting asynchronous layer over the blocking API is a better approach).
  • the hardware has lots of free cores and the measurement shows that the load on the main QHttp thread is close to highest limit. There you can spawn some other handler threads.

Source tree

TOC

  • src/: holds the source code of QHttp. server classes are prefixed by qhttpserver* and client classes by qhttpclient*.
    • private/: Private classes of the library.
  • 3rdparty/: will contain http-parser source tree as the only dependency. this directory is created by setup. see also: setup.
  • example/: contains some sample applications representing the QHttp usage:
    • helloworld/: the HelloWorld example of QHttp, both server + client are represented. see: [email protected]
    • basic-server/: a basic HTTP server shows how to collect the request body, and respond to the clients. see: [email protected]
    • keep-alive: shows how to keep an http connection open and transmitting many requests/responses. see: [email protected]
    • post-collector: another server example shows how to collect large data by POST requests. see: [email protected]
  • tmp/: a temporary directory which is created while makeing the library and holds all the .o, moc files, etc.
  • xbin/: all the executable and libraries will be placed on this folder by build system.

Disclaimer

TOC

  • Implementing a lightweight and simple HTTP server/client in Qt with Node.js like API, is the main purpose of QHttp.
  • There are lots of features in a full blown HTTP server which are out of scope of this small library, although those can be added on top of QHttp.
  • The client classes are by no mean designed as a QNetworkAccessManager replacement. QHttpClient is simpler and lighter, for serious scenarios just use QNetworkAccessManager which supports proxy, redirections, authentication, cookie jar, ssl, ...
  • I'm a busy person.

If you have any ideas, critiques, suggestions or whatever you want to call it, please open an issue. I'll be happy to hear different ideas, will think about them, and I try to add those that make sense.

License

TOC

Distributed under the MIT license. Copyright (c) 2014, Amir Zamani.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].