Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-22041

QNetworkReply returns corrupted "Location" header and QNetworkRequest::RedirectionTargetAttribute for some URLs

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Invalid
    • Icon: P2: Important P2: Important
    • Some future release
    • 4.7.4
    • Core: URL Handling
    • None
    • OS X, Qt SDK 1.1.2

      Do a GET request to the following URL with QNetworkRequest and QNetworkAccessManager:

      http://dw.com.com/redir/cnet_2011-10-14-204205.2500.mp4?destUrl=http%3A%2F%2Fcnet.co%2Fognfq4%3FADPARAMS%3DBRAND%3D47%7CSITE%3D53%7CSP%3D181%7CPOS%3D100%7CNCAT%3D13482%3A19029%3A&ontId=14106&siteId=145&edId=3&asid=50113231&pid=&astId=31&ltype=dl_dlnow
      
         QString downloadUrlFixed = "http://dw.com.com/redir/cnet_2011-10-14-204205.2500.mp4?destUrl=http%3A%2F%2Fcnet.co%2Fognfq4%3FADPARAMS%3DBRAND%3D47%7CSITE%3D53%7CSP%3D181%7CPOS%3D100%7CNCAT%3D13482%3A19029%3A&ontId=14106&siteId=145&edId=3&asid=50113231&pid=&astId=31&ltype=dl_dlnow";
      
          QUrl downloadUrl(downloadUrlFixed);
          if (!downloadUrl.isValid()) {
              qWarning() << "URL is not valid.";
              return;
          }
      
          QNetworkRequest request;
          request.setUrl(downloadUrl);
      
          QNetworkReply *m_currentDownload = m_dlNetworkManager->get(request);
      
          connect(m_currentDownload, SIGNAL(finished()),
                  this, SLOT(onDownloadCompleted()));
      

      Then in onDownloadCompleted:

      void MyObject::onDownloadCompleted()
      {
          QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
          qDebug() << "HTTP Status: " << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
          
          QVariant possibleRedirectUrl =
                           reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
      
          qDebug() << "Redirect URL: " << possibleRedirectUrl;
      
          qDebug() << "Location HTTP header:" << reply->rawHeader("Location");
      }
      

      This is the output:

      HTTP Status:  QVariant(int, 302) 
      Redirect URL:  QVariant(QUrl, QUrl("http:") ) 
      Location HTTP header: "http://dw.com.comhttp%3A%2F%2Fcnet.co%2Fognfq4%3FADPARAMS%3DBRAND%3D47%7CSITE%3D53%7CSP%3D181%7CPOS%3D100%7CNCAT%3D13482%3A19029%3A" 
      

      Note how the HTTP status says it's 302 (Moved), reply->attribute(QNetworkRequest::RedirectionTargetAttribute) does not return any URL to redirect to and especially how the HTTP header "Location" is corrupted.

      For the reference, this is how 'wget' shows the headers for the same URL and note what the "Location" header should be:

      kypeli@QPowerhouse ~ $ wget 'http://dw.com.com/redir/cnet_2011-10-14-204205.2500.mp4?destUrl=http%3A%2F%2Fcnet.co%2Fognfq4%3FADPARAMS%3DBRAND%3D47%7CSITE%3D53%7CSP%3D181%7CPOS%3D100%7CNCAT%3D13482%3A19029%3A&ontId=14106&siteId=145&edId=3&asid=50113231&pid=&astId=31&ltype=dl_dlnow' --server-response
      --2011-10-14 21:53:58--  http://dw.com.com/redir/cnet_2011-10-14-204205.2500.mp4?destUrl=http%3A%2F%2Fcnet.co%2Fognfq4%3FADPARAMS%3DBRAND%3D47%7CSITE%3D53%7CSP%3D181%7CPOS%3D100%7CNCAT%3D13482%3A19029%3A&ontId=14106&siteId=145&edId=3&asid=50113231&pid=&astId=31&ltype=dl_dlnow
      Resolving dw.com.com... 64.30.224.42
      Connecting to dw.com.com|64.30.224.42|:80... connected.
      HTTP request sent, awaiting response... 
        HTTP/1.1 302 Found
        Date: Sat, 15 Oct 2011 04:53:58 GMT
        Server: Apache/2.0
        Pragma: no-cache
        Cache-control: no-cache, must-revalidate, no-transform
        Vary: *
        Expires: Fri, 23 Jan 1970 12:12:12 GMT
        Location: http://cnet.co/ognfq4?ADPARAMS=BRAND=47|SITE=53|SP=181|POS=100|NCAT=13482:19029:
        Content-Length: 0
        P3P: CP="CAO DSP COR CURa ADMa DEVa PSAa PSDa IVAi IVDi CONi OUR OTRi IND PHY ONL UNI FIN COM NAV INT DEM STA"
        Keep-Alive: timeout=363, max=620
        Connection: Keep-Alive
        Content-Type: text/plain
      Location: http://cnet.co/ognfq4?ADPARAMS=BRAND=47|SITE=53|SP=181|POS=100|NCAT=13482:19029: [following]
      --2011-10-14 21:53:58--  http://cnet.co/ognfq4?ADPARAMS=BRAND=47%7CSITE=53%7CSP=181%7CPOS=100%7CNCAT=13482:19029:
      Resolving cnet.co... 168.143.174.97
      Connecting to cnet.co|168.143.174.97|:80... connected.
      HTTP request sent, awaiting response... 
      

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            earthdomain Earth Domain (Inactive)
            kypeli Johan Paul
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes