Qt 4.8
qnetworkaccesscachebackend.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 //#define QNETWORKACCESSCACHEBACKEND_DEBUG
43 
45 #include "qabstractnetworkcache.h"
46 #include "qfileinfo.h"
47 #include "qurlinfo.h"
48 #include "qdir.h"
49 #include "qcoreapplication.h"
50 
52 
55  , device(0)
56 {
57 }
58 
60 {
61 }
62 
64 {
66  QString msg = QCoreApplication::translate("QNetworkAccessCacheBackend", "Error opening %1")
67  .arg(this->url().toString());
69  } else {
71  }
72  finished();
73 }
74 
76 {
77  setCachingEnabled(false);
79  if (!nc)
80  return false;
81 
82  QNetworkCacheMetaData item = nc->metaData(url());
83  if (!item.isValid())
84  return false;
85 
89 
90  // set the raw headers
93  end = rawHeaders.constEnd();
94  for ( ; it != end; ++it) {
95  if (it->first.toLower() == "cache-control" &&
96  it->second.toLower().contains("must-revalidate")) {
97  return false;
98  }
99  setRawHeader(it->first, it->second);
100  }
101 
102  // handle a possible redirect
103  QVariant redirectionTarget = attributes.value(QNetworkRequest::RedirectionTargetAttribute);
104  if (redirectionTarget.isValid()) {
106  redirectionRequested(redirectionTarget.toUrl());
107  }
108 
109  // signal we're open
110  metaDataChanged();
111 
113  QIODevice *contents = nc->data(url());
114  if (!contents)
115  return false;
116  contents->setParent(this);
117  writeDownstreamData(contents);
118  }
119 
120 #if defined(QNETWORKACCESSCACHEBACKEND_DEBUG)
121  qDebug() << "Successfully sent cache:" << url();
122 #endif
123  return true;
124 }
125 
127 {
129  device->close();
130  delete device;
131  device = 0;
132  }
133 }
134 
136 {
137  Q_ASSERT_X(false, Q_FUNC_INFO, "This function show not have been called!");
138 }
139 
141 {
142  Q_ASSERT_X(false, Q_FUNC_INFO, "This function show not have been called!");
143 }
144 
146 {
147  Q_ASSERT_X(false, Q_FUNC_INFO, "This function show not have been called!");
148 }
149 
151 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setAttribute(QNetworkRequest::Attribute code, const QVariant &value)
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
Definition: qiodevice.cpp:590
#define it(className, varName)
bool isValid() const
Returns true if this network cache meta data has attributes that have been set otherwise false...
The QString class provides a Unicode character string.
Definition: qstring.h:83
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
void setParent(QObject *)
Makes the object a child of parent.
Definition: qobject.cpp:1950
static QString translate(const char *context, const char *key, const char *disambiguation=0, Encoding encoding=CodecForTr)
virtual QIODevice * data(const QUrl &url)=0
Returns the data associated with url.
RawHeaderList rawHeaders() const
Returns a list of all raw headers that are set in this meta data.
Q_CORE_EXPORT void qDebug(const char *,...)
static QString toString(Register *reg, int type, bool *ok=0)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void setRawHeader(const QByteArray &headerName, const QByteArray &value)
AttributesMap attributes() const
Returns all the attributes stored with this cache item.
QNetworkAccessManager::Operation operation() const
QUrl toUrl() const
Returns the variant as a QUrl if the variant has type() Url ; otherwise returns an invalid QUrl...
Definition: qvariant.cpp:2528
QAbstractNetworkCache * networkCache() const
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
void error(QNetworkReply::NetworkError code, const QString &errorString)
const_iterator ConstIterator
Qt-style synonym for QList::const_iterator.
Definition: qlist.h:279
virtual QNetworkCacheMetaData metaData(const QUrl &url)=0
Returns the meta data for the url url.
void writeDownstreamData(QByteDataBuffer &list)
void setCachingEnabled(bool enable)
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
static const KeyPair *const end
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
The QNetworkCacheMetaData class provides cache information.
void redirectionRequested(const QUrl &destination)
The QAbstractNetworkCache class provides the interface for cache implementations. ...
#define Q_FUNC_INFO
Definition: qglobal.h:1871