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
QtWS25 Last Chance

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
  • 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 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

    kshegunovK 1 Reply Last reply
    0
    • S Snyfir

      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

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on 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
      1
      • kshegunovK kshegunov

        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 last edited by
        #3

        @kshegunov do you have an example?

        kshegunovK 1 Reply Last reply
        0
        • S Snyfir

          @kshegunov do you have an example?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on 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
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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

            • Login

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