IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Simple HTTP Server

Simple example of how to set up an HTTP server.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

Simple HTTP Server▲

Image non disponible

This example shows how to set up a server using the https:// class. The server is bound to an incoming port using the listen() function, and the route() function is used to add a handler for each of several different incoming URLs. For one of the URLs, "/auth", https:// is used.

 
Sélectionnez
const auto sslCertificateChain =
        QSslCertificate::fromPath(QStringLiteral(":/assets/certificate.crt"));
if (sslCertificateChain.empty()) {
    qWarning() << QCoreApplication::translate("QHttpServerExample",
                                              "Couldn't retrieve SSL certificate from file.");
    return -1;
}
QFile privateKeyFile(QStringLiteral(":/assets/private.key"));
if (!privateKeyFile.open(QIODevice::ReadOnly)) {
    qWarning() << QCoreApplication::translate("QHttpServerExample",
                                              "Couldn't open file for reading: %1")
                  .arg(privateKeyFile.errorString());
    return -1;
}
httpServer.sslSetup(sslCertificateChain.front(), QSslKey(&privateKeyFile, QSsl::Rsa));
privateKeyFile.close();

const auto sslPort = httpServer.listen(QHostAddress::Any);
if (!sslPort) {
    qWarning() << QCoreApplication::translate("QHttpServerExample",
                                              "Server failed to listen on a port.");
    return -1;
}

In the above example QSslConfiguration is used to show how to create an SSL configuration for a https:// to serve HTTPS traffic.

 
Sélectionnez
httpServer.afterRequest([](QHttpServerResponse &&resp) {
    resp.setHeader("Server", "Qt HTTP Server");
    return std::move(resp);
});

The above example shows how to use the afterRequest() function of the https:// to change the https:// object after it has been handled by the route() function. It demonstrates how HTTP headers can be added to the response.

List of Files▲

Files:

List of Files▲

  • simple/CMakeLists.txt

  • simple/main.cpp

  • simple/simple.pro

List of Files▲

Images:

List of Files▲
  • simple/assets/qt-logo.png

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Ce document est disponible sous la licence https:// telle que publiée par la Free Software Foundation.