Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSslSocket: how to know if the client use http or https
Forum Updated to NodeBB v4.3 + New Features

QSslSocket: how to know if the client use http or https

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.0k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Snyfir
    wrote on 27 Feb 2020, 07:15 last edited by
    #1

    Hello everybody,

    I created a server web in qt with QTcpServer . I would like the server be accesible with http and https (ssl). I can configure my server to use one or other but i don't know how to do both.

    void HttpServer::incomingConnection(qintptr socketDescriptor)
    {
        bool sslEnable = // how to know what the client want ?
    
        if (!sslEnable) {
            QTcpSocket *socket = new QTcpSocket(this);
            socket->setSocketDescriptor(socketDescriptor);
            addPendingConnection(socket);
        } else {
            QSslSocket *socket = new QSslSocket(this);
    
            if (socket->setSocketDescriptor(socketDescriptor)) {
                connect(socket, &QSslSocket::encrypted, this, &HttpServer::encrypted);
                connect(socket,
                        SIGNAL(sslErrors(QList<QSslError>)),
                        this,
                        SLOT(sslErrors(QList<QSslError>)));
                connect(socket, &QSslSocket::peerVerifyError, this, &HttpServer::peerVerifyError);
    
                socket->setProtocol(QSsl::TlsV1_2);
                socket->setLocalCertificate(certificate);
                socket->setPrivateKey(key);
                socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
                socket->setPeerVerifyMode(QSslSocket::QueryPeer);
                socket->startServerEncryption();
    
                addPendingConnection(socket);
            } else {
                socket->deleteLater();
            }
        }
    }
    

    thank you for your help

    K 1 Reply Last reply 27 Feb 2020, 07:50
    0
    • S Snyfir
      27 Feb 2020, 07:15

      Hello everybody,

      I created a server web in qt with QTcpServer . I would like the server be accesible with http and https (ssl). I can configure my server to use one or other but i don't know how to do both.

      void HttpServer::incomingConnection(qintptr socketDescriptor)
      {
          bool sslEnable = // how to know what the client want ?
      
          if (!sslEnable) {
              QTcpSocket *socket = new QTcpSocket(this);
              socket->setSocketDescriptor(socketDescriptor);
              addPendingConnection(socket);
          } else {
              QSslSocket *socket = new QSslSocket(this);
      
              if (socket->setSocketDescriptor(socketDescriptor)) {
                  connect(socket, &QSslSocket::encrypted, this, &HttpServer::encrypted);
                  connect(socket,
                          SIGNAL(sslErrors(QList<QSslError>)),
                          this,
                          SLOT(sslErrors(QList<QSslError>)));
                  connect(socket, &QSslSocket::peerVerifyError, this, &HttpServer::peerVerifyError);
      
                  socket->setProtocol(QSsl::TlsV1_2);
                  socket->setLocalCertificate(certificate);
                  socket->setPrivateKey(key);
                  socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
                  socket->setPeerVerifyMode(QSslSocket::QueryPeer);
                  socket->startServerEncryption();
      
                  addPendingConnection(socket);
              } else {
                  socket->deleteLater();
              }
          }
      }
      

      thank you for your help

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 27 Feb 2020, 07:50 last edited by
      #2

      You're half way there. You need to exchange a couple of message over plain TCP with the client to negotiate if SSL is supported, and which protocols can be used, and then you can configure the ssl socket (if it is) and call startServerEncryption. Otherwise you just use the QSslSocket as a plain QTcpSocket.

      Read and abide by the Qt Code of Conduct

      S 1 Reply Last reply 27 Feb 2020, 08:04
      1
      • K kshegunov
        27 Feb 2020, 07:50

        You're half way there. You need to exchange a couple of message over plain TCP with the client to negotiate if SSL is supported, and which protocols can be used, and then you can configure the ssl socket (if it is) and call startServerEncryption. Otherwise you just use the QSslSocket as a plain QTcpSocket.

        S Offline
        S Offline
        Snyfir
        wrote on 27 Feb 2020, 08:04 last edited by
        #3

        @kshegunov do you have an example?

        K 1 Reply Last reply 27 Feb 2020, 08:10
        0
        • S Snyfir
          27 Feb 2020, 08:04

          @kshegunov do you have an example?

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 27 Feb 2020, 08:10 last edited by
          #4

          @Snyfir said in QSslSocket: how to know if the client use http or https:

          @kshegunov do you have an example?

          I haven't implemented the HTTP protocol before, so no, I don't. Here's an rfc, you can check out, however.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 27 Feb 2020, 08:46 last edited by
            #5

            Hi,

            You might want to check the cutelyst project for inspiration.

            On a side note, it usually boils down to:

            • Connect on port 80 -> http
            • Connect on port 443 -> https

            But in the actual date and time, you should just redirect port 80 to 443 and to https communication only.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1

            4/5

            27 Feb 2020, 08:10

            • Login

            • Login or register to search.
            4 out of 5
            • First post
              4/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved