| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | //#define QNETWORKACCESSCACHEBACKEND_DEBUG |
| 41 | |
| 42 | #include "qnetworkaccesscachebackend_p.h" |
| 43 | #include "qabstractnetworkcache.h" |
| 44 | #include "qfileinfo.h" |
| 45 | #if QT_CONFIG(ftp) |
| 46 | #include "qurlinfo_p.h" |
| 47 | #endif |
| 48 | #include "qdir.h" |
| 49 | #include "qcoreapplication.h" |
| 50 | |
| 51 | QT_BEGIN_NAMESPACE |
| 52 | |
| 53 | QNetworkAccessCacheBackend::QNetworkAccessCacheBackend() |
| 54 | : QNetworkAccessBackend() |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | QNetworkAccessCacheBackend::~QNetworkAccessCacheBackend() |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | void QNetworkAccessCacheBackend::open() |
| 63 | { |
| 64 | if (operation() != QNetworkAccessManager::GetOperation || !sendCacheContents()) { |
| 65 | QString msg = QCoreApplication::translate(context: "QNetworkAccessCacheBackend" , key: "Error opening %1" ) |
| 66 | .arg(a: this->url().toString()); |
| 67 | error(code: QNetworkReply::ContentNotFoundError, errorString: msg); |
| 68 | } else { |
| 69 | setAttribute(code: QNetworkRequest::SourceIsFromCacheAttribute, value: true); |
| 70 | } |
| 71 | finished(); |
| 72 | } |
| 73 | |
| 74 | bool QNetworkAccessCacheBackend::sendCacheContents() |
| 75 | { |
| 76 | setCachingEnabled(false); |
| 77 | QAbstractNetworkCache *nc = networkCache(); |
| 78 | if (!nc) |
| 79 | return false; |
| 80 | |
| 81 | QNetworkCacheMetaData item = nc->metaData(url: url()); |
| 82 | if (!item.isValid()) |
| 83 | return false; |
| 84 | |
| 85 | QNetworkCacheMetaData::AttributesMap attributes = item.attributes(); |
| 86 | setAttribute(code: QNetworkRequest::HttpStatusCodeAttribute, value: attributes.value(akey: QNetworkRequest::HttpStatusCodeAttribute)); |
| 87 | setAttribute(code: QNetworkRequest::HttpReasonPhraseAttribute, value: attributes.value(akey: QNetworkRequest::HttpReasonPhraseAttribute)); |
| 88 | |
| 89 | // set the raw headers |
| 90 | const QNetworkCacheMetaData::RawHeaderList = item.rawHeaders(); |
| 91 | for (const auto & : rawHeaders) { |
| 92 | if (header.first.toLower() == "cache-control" ) { |
| 93 | const QByteArray cacheControlValue = header.second.toLower(); |
| 94 | if (cacheControlValue.contains(c: "must-revalidate" ) |
| 95 | || cacheControlValue.contains(c: "no-cache" )) { |
| 96 | return false; |
| 97 | } |
| 98 | } |
| 99 | setRawHeader(headerName: header.first, value: header.second); |
| 100 | } |
| 101 | |
| 102 | // handle a possible redirect |
| 103 | QVariant redirectionTarget = attributes.value(akey: QNetworkRequest::RedirectionTargetAttribute); |
| 104 | if (redirectionTarget.isValid()) { |
| 105 | setAttribute(code: QNetworkRequest::RedirectionTargetAttribute, value: redirectionTarget); |
| 106 | redirectionRequested(destination: redirectionTarget.toUrl()); |
| 107 | } |
| 108 | |
| 109 | // signal we're open |
| 110 | metaDataChanged(); |
| 111 | |
| 112 | if (operation() == QNetworkAccessManager::GetOperation) { |
| 113 | QIODevice *contents = nc->data(url: url()); |
| 114 | if (!contents) |
| 115 | return false; |
| 116 | contents->setParent(this); |
| 117 | writeDownstreamData(data: contents); |
| 118 | } |
| 119 | |
| 120 | #if defined(QNETWORKACCESSCACHEBACKEND_DEBUG) |
| 121 | qDebug() << "Successfully sent cache:" << url(); |
| 122 | #endif |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | bool QNetworkAccessCacheBackend::start() |
| 127 | { |
| 128 | open(); |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | void QNetworkAccessCacheBackend::closeDownstreamChannel() |
| 133 | { |
| 134 | } |
| 135 | |
| 136 | void QNetworkAccessCacheBackend::closeUpstreamChannel() |
| 137 | { |
| 138 | Q_ASSERT_X(false, Q_FUNC_INFO, "This function show not have been called!" ); |
| 139 | } |
| 140 | |
| 141 | void QNetworkAccessCacheBackend::upstreamReadyRead() |
| 142 | { |
| 143 | Q_ASSERT_X(false, Q_FUNC_INFO, "This function show not have been called!" ); |
| 144 | } |
| 145 | |
| 146 | void QNetworkAccessCacheBackend::downstreamReadyWrite() |
| 147 | { |
| 148 | Q_ASSERT_X(false, Q_FUNC_INFO, "This function show not have been called!" ); |
| 149 | } |
| 150 | |
| 151 | QT_END_NAMESPACE |
| 152 | |
| 153 | |