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. QWebSocket refuses to follow HTTP 301 Redirect

QWebSocket refuses to follow HTTP 301 Redirect

Scheduled Pinned Locked Moved Solved General and Desktop
qwebsocketredirect
3 Posts 2 Posters 1.2k 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.
  • J Offline
    J Offline
    JustASimpleCoder
    wrote on last edited by
    #1

    Hello,

    I am trying to use QWebSockets to connect to a service that is located behind an nginx balancer.
    Nginx is returning HTTP 301 redirects during the HTTP handshake, and this is breaking the QWebSocket connection.

    I would like to have my QWebSocket automatically follow the redirect so that it will be able to connect with the service; however, I can't seem to get it working, or find any documentation on how to accomplish this.

    The QNetworkRequest allows you to follow redirects, so I attempted to create a QWebSocket based on this, but was unsuccessful.

    QWebSocket *socket = new QWebSocket;
    std::string url = "someUrlThatGoesThroughNGINX";
    
    QNetworkRequest nr(QUrl::fromUserInput(QString::fromStdString("ws://" + url)));
    nr.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
    nr.setRawHeader(QByteArray("Connection"), QByteArray("Upgrade"));
    nr.setRawHeader(QByteArray("Upgrade"), QByteArray("WebSocket"));
    
    socket->open(nr); // This throws QWebSocket::error with socket->errorString() = QWebSocketPrivate:processHandshake: Unhandled http status code: 301 (Moved Permanently).
    

    I know nginx is working properly because normal HTTP requests work fine, I just can't seem to get it working with a QWebSocket.

    Any ideas?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JustASimpleCoder
      wrote on last edited by
      #3

      It turned out that nginx was returning 301 while trying to redirect from http to https (it had nothing to do with the endpoint itself thankfully).

      I changed my websocket URL to "wss://" + url and that seemed to resolve the issue for me.

      For future readers, if you are still experiencing errors with a Secure WebSocket you may want to adjust the SSL configuration of the QWebSocket.

      QSslConfiguration sslConfig;
      
      sslConfig.setProtocol(whateverProtocolYouDesire);
      // Set additional properties of sslConfig here...
      
      socket->setSslConfiguration(sslConfig);
      
      1 Reply Last reply
      0
      • AlveinA Offline
        AlveinA Offline
        Alvein
        wrote on last edited by
        #2

        Some thoughts.

        QWebSocket::open() is able to do the handshake from a supplied QUrl. No need to handle the connection upgrade yourself.

        In case you supply a QUrl and still get the unexpected HTTP 301, can't you get the Location response header from the previous QNetworkRequest's reply and use that URL instead? It's even permanent. You don't need to figure it out on each run.

        1 Reply Last reply
        2
        • J Offline
          J Offline
          JustASimpleCoder
          wrote on last edited by
          #3

          It turned out that nginx was returning 301 while trying to redirect from http to https (it had nothing to do with the endpoint itself thankfully).

          I changed my websocket URL to "wss://" + url and that seemed to resolve the issue for me.

          For future readers, if you are still experiencing errors with a Secure WebSocket you may want to adjust the SSL configuration of the QWebSocket.

          QSslConfiguration sslConfig;
          
          sslConfig.setProtocol(whateverProtocolYouDesire);
          // Set additional properties of sslConfig here...
          
          socket->setSslConfiguration(sslConfig);
          
          1 Reply Last reply
          0

          • Login

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