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

Redirects are not cached, hence QNetworkRequest::AlwaysCache is useless

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P3: Somewhat important
    • Resolution: Unresolved
    • Affects Version/s: 4.8.5, 5.3.1
    • Fix Version/s: Some future release
    • Component/s: Network: Cache
    • Labels:
      None
    • Environment:
      Ubuntu 13.04

      Description

      I use a QWebView with a custom QNetworkAccessManager and QNetworkDiskCache like that:

      NetworkAccessManager::NetworkAccessManager()
          : QNetworkAccessManager(),
            m_cacheControl(QNetworkRequest::PreferNetwork)
      {
          QNetworkDiskCache *cache = new QNetworkDiskCache(this);
      
          // 100 Mb
          cache->setMaximumCacheSize(100*1024*1024);
          cache->setCacheDirectory("cacheDir");
      
          setCache(cache);
      
          connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
      }
      
      QNetworkReply *NetworkAccessManager::createRequest(QNetworkAccessManager::Operation op,
                                                           const QNetworkRequest &req,
                                                           QIODevice *outgoingData)
      {
          QNetworkReply *reply = 0;
      
           if(cache())
           {
               QNetworkRequest request(req);
      
               request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, m_cacheControl);
               request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, true);
      
               qDebug("Request with cache %d", request.attribute(QNetworkRequest::CacheLoadControlAttribute).toInt());
      
               reply = QNetworkAccessManager::createRequest(op, request, outgoingData);
           }
           else
               reply = QNetworkAccessManager::createRequest(op, req, outgoingData);
      
           return reply;
      }
      
      void NetworkAccessManager::requestFinished(QNetworkReply *reply)
      {
          if(!reply)
          {
              qWarning("Reply is null");
              return;
          }
      
          qDebug("Finished. Error: %d", reply->error());
      }
      
      /**************************************************/
      
      Widget::Widget(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::Widget)
      {
          ui->setupUi(this);
      
          m_manager = new NetworkAccessManager;
      
          ui->webView->page()->setNetworkAccessManager(m_manager);
      
          new QShortcut(QKeySequence::HelpContents, this, SLOT(slotHelp()));
      }
      
      Widget::~Widget()
      {
          delete ui;
      }
      
      void Widget::slotGet()
      {
          qDebug("GET");
          ui->webView->setUrl(QUrl("http://cnn.com"));
      }
      
      void Widget::slotPrefer()
      {
          qDebug("Prefer network");
          m_manager->setCacheControl(QNetworkRequest::PreferNetwork);
      }
      
      void Widget::slotAlways()
      {
          qDebug("Always cache");
          m_manager->setCacheControl(QNetworkRequest::AlwaysCache);
      }

      I have three buttons on the form. The first button "PreferNetwork" sets the network caching mode to PreferNetwork (default mode). The second button "AlwaysCache" sets the network caching mode to AlwaysCache. The third button "Get" loads the url "http://cnn.com" into the webview.

      Steps to reproduce:

      1) Press "Get", wait for the content
      2) disconnect the network (I use "ifconfig eth0 down")
      3) Press "AlwaysCache"
      4) Press "Get"

      Result: empty page. In the console I see "Finished. Error: 203". Error 203 is QNetworkReply::ContentNotFoundError.

      Further debugging (network is still disconnected):

      1) Change the source code of widget.cpp at line #82, change "cnn.com" to "edition.cnn.com"
      2) Compile and run
      3) Press "AlwaysCache"
      4) Press "Get"

      Result: I see the cached content of the CNN website.

      Conslusion: Many sites use redirections - CNN, Yandex, Google etc. QWebPage doesn't cache the redirections, and hence AlwaysCache is useless.

      Source code is attached.

        Attachments

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

          Activity

            People

            • Assignee:
              peter-har Peter Hartmann
              Reporter:
              dmitry64 Dmitry
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:

                Gerrit Reviews

                There are no open Gerrit changes