src/network/access/qnetworkaccessbackend.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtNetwork module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qnetworkaccessbackend_p.h"
       
    43 #include "qnetworkaccessmanager_p.h"
       
    44 #include "qnetworkrequest.h"
       
    45 #include "qnetworkreply.h"
       
    46 #include "qnetworkreply_p.h"
       
    47 #include "QtCore/qhash.h"
       
    48 #include "QtCore/qmutex.h"
       
    49 
       
    50 #include "qnetworkaccesscachebackend_p.h"
       
    51 #include "qabstractnetworkcache.h"
       
    52 
       
    53 #include "private/qnoncontiguousbytedevice_p.h"
       
    54 
       
    55 QT_BEGIN_NAMESPACE
       
    56 
       
    57 static bool factoryDataShutdown = false;
       
    58 class QNetworkAccessBackendFactoryData: public QList<QNetworkAccessBackendFactory *>
       
    59 {
       
    60 public:
       
    61     QNetworkAccessBackendFactoryData() : mutex(QMutex::Recursive) { }
       
    62     ~QNetworkAccessBackendFactoryData()
       
    63     {
       
    64         QMutexLocker locker(&mutex); // why do we need to lock?
       
    65         factoryDataShutdown = true;
       
    66     }
       
    67 
       
    68     QMutex mutex;
       
    69 };
       
    70 Q_GLOBAL_STATIC(QNetworkAccessBackendFactoryData, factoryData)
       
    71 
       
    72 QNetworkAccessBackendFactory::QNetworkAccessBackendFactory()
       
    73 {
       
    74     QMutexLocker locker(&factoryData()->mutex);
       
    75     factoryData()->prepend(this);
       
    76 }
       
    77 
       
    78 QNetworkAccessBackendFactory::~QNetworkAccessBackendFactory()
       
    79 {
       
    80     if (!factoryDataShutdown) {
       
    81         QMutexLocker locker(&factoryData()->mutex);
       
    82         factoryData()->removeAll(this);
       
    83     }
       
    84 }
       
    85 
       
    86 QNetworkAccessBackend *QNetworkAccessManagerPrivate::findBackend(QNetworkAccessManager::Operation op,
       
    87                                                                  const QNetworkRequest &request)
       
    88 {
       
    89     QNetworkRequest::CacheLoadControl mode =
       
    90         static_cast<QNetworkRequest::CacheLoadControl>(
       
    91             request.attribute(QNetworkRequest::CacheLoadControlAttribute,
       
    92                               QNetworkRequest::PreferNetwork).toInt());
       
    93     if (mode == QNetworkRequest::AlwaysCache
       
    94         && (op == QNetworkAccessManager::GetOperation
       
    95         || op == QNetworkAccessManager::HeadOperation)) {
       
    96         QNetworkAccessBackend *backend = new QNetworkAccessCacheBackend;
       
    97         backend->manager = this;
       
    98         return backend;
       
    99     }
       
   100 
       
   101     if (!factoryDataShutdown) {
       
   102         QMutexLocker locker(&factoryData()->mutex);
       
   103         QNetworkAccessBackendFactoryData::ConstIterator it = factoryData()->constBegin(),
       
   104                                                            end = factoryData()->constEnd();
       
   105         while (it != end) {
       
   106             QNetworkAccessBackend *backend = (*it)->create(op, request);
       
   107             if (backend) {
       
   108                 backend->manager = this;
       
   109                 return backend; // found a factory that handled our request
       
   110             }
       
   111             ++it;
       
   112         }
       
   113     }
       
   114     return 0;
       
   115 }
       
   116 
       
   117 QNonContiguousByteDevice* QNetworkAccessBackend::createUploadByteDevice()
       
   118 {
       
   119     QNonContiguousByteDevice* device = 0;
       
   120 
       
   121     if (reply->outgoingDataBuffer)
       
   122         device = QNonContiguousByteDeviceFactory::create(reply->outgoingDataBuffer);
       
   123     else
       
   124         device = QNonContiguousByteDeviceFactory::create(reply->outgoingData);
       
   125 
       
   126     bool bufferDisallowed =
       
   127             reply->request.attribute(QNetworkRequest::DoNotBufferUploadDataAttribute,
       
   128                           QVariant(false)) == QVariant(true);
       
   129     if (bufferDisallowed)
       
   130         device->disableReset();
       
   131 
       
   132     // make sure we delete this later
       
   133     device->setParent(this);
       
   134 
       
   135     connect(device, SIGNAL(readProgress(qint64,qint64)), this, SLOT(emitReplyUploadProgress(qint64,qint64)));
       
   136 
       
   137     return device;
       
   138 }
       
   139 
       
   140 // need to have this function since the reply is a private member variable
       
   141 // and the special backends need to access this.
       
   142 void QNetworkAccessBackend::emitReplyUploadProgress(qint64 bytesSent, qint64 bytesTotal)
       
   143 {
       
   144     reply->emitUploadProgress(bytesSent, bytesTotal);
       
   145 }
       
   146 
       
   147 QNetworkAccessBackend::QNetworkAccessBackend()
       
   148     : manager(0)
       
   149     , reply(0)
       
   150 {
       
   151 }
       
   152 
       
   153 QNetworkAccessBackend::~QNetworkAccessBackend()
       
   154 {
       
   155 }
       
   156 
       
   157 void QNetworkAccessBackend::downstreamReadyWrite()
       
   158 {
       
   159     // do nothing
       
   160 }
       
   161 
       
   162 void QNetworkAccessBackend::copyFinished(QIODevice *)
       
   163 {
       
   164     // do nothing
       
   165 }
       
   166 
       
   167 void QNetworkAccessBackend::ignoreSslErrors()
       
   168 {
       
   169     // do nothing
       
   170 }
       
   171 
       
   172 void QNetworkAccessBackend::ignoreSslErrors(const QList<QSslError> &errors)
       
   173 {
       
   174     Q_UNUSED(errors);
       
   175     // do nothing
       
   176 }
       
   177 
       
   178 void QNetworkAccessBackend::fetchSslConfiguration(QSslConfiguration &) const
       
   179 {
       
   180     // do nothing
       
   181 }
       
   182 
       
   183 void QNetworkAccessBackend::setSslConfiguration(const QSslConfiguration &)
       
   184 {
       
   185     // do nothing
       
   186 }
       
   187 
       
   188 QNetworkCacheMetaData QNetworkAccessBackend::fetchCacheMetaData(const QNetworkCacheMetaData &) const
       
   189 {
       
   190     return QNetworkCacheMetaData();
       
   191 }
       
   192 
       
   193 QNetworkAccessManager::Operation QNetworkAccessBackend::operation() const
       
   194 {
       
   195     return reply->operation;
       
   196 }
       
   197 
       
   198 QNetworkRequest QNetworkAccessBackend::request() const
       
   199 {
       
   200     return reply->request;
       
   201 }
       
   202 
       
   203 #ifndef QT_NO_NETWORKPROXY
       
   204 QList<QNetworkProxy> QNetworkAccessBackend::proxyList() const
       
   205 {
       
   206     return reply->proxyList;
       
   207 }
       
   208 #endif
       
   209 
       
   210 QAbstractNetworkCache *QNetworkAccessBackend::networkCache() const
       
   211 {
       
   212     if (!manager)
       
   213         return 0;
       
   214     return manager->networkCache;
       
   215 }
       
   216 
       
   217 void QNetworkAccessBackend::setCachingEnabled(bool enable)
       
   218 {
       
   219     reply->setCachingEnabled(enable);
       
   220 }
       
   221 
       
   222 bool QNetworkAccessBackend::isCachingEnabled() const
       
   223 {
       
   224     return reply->isCachingEnabled();
       
   225 }
       
   226 
       
   227 qint64 QNetworkAccessBackend::nextDownstreamBlockSize() const
       
   228 {
       
   229     return reply->nextDownstreamBlockSize();
       
   230 }
       
   231 
       
   232 void QNetworkAccessBackend::writeDownstreamData(QByteDataBuffer &list)
       
   233 {
       
   234     reply->appendDownstreamData(list);
       
   235 }
       
   236 
       
   237 void QNetworkAccessBackend::writeDownstreamData(QIODevice *data)
       
   238 {
       
   239     reply->appendDownstreamData(data);
       
   240 }
       
   241 
       
   242 QVariant QNetworkAccessBackend::header(QNetworkRequest::KnownHeaders header) const
       
   243 {
       
   244     return reply->q_func()->header(header);
       
   245 }
       
   246 
       
   247 void QNetworkAccessBackend::setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value)
       
   248 {
       
   249     reply->setCookedHeader(header, value);
       
   250 }
       
   251 
       
   252 bool QNetworkAccessBackend::hasRawHeader(const QByteArray &headerName) const
       
   253 {
       
   254     return reply->q_func()->hasRawHeader(headerName);
       
   255 }
       
   256 
       
   257 QByteArray QNetworkAccessBackend::rawHeader(const QByteArray &headerName) const
       
   258 {
       
   259     return reply->q_func()->rawHeader(headerName);
       
   260 }
       
   261 
       
   262 QList<QByteArray> QNetworkAccessBackend::rawHeaderList() const
       
   263 {
       
   264     return reply->q_func()->rawHeaderList();
       
   265 }
       
   266 
       
   267 void QNetworkAccessBackend::setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)
       
   268 {
       
   269     reply->setRawHeader(headerName, headerValue);
       
   270 }
       
   271 
       
   272 QVariant QNetworkAccessBackend::attribute(QNetworkRequest::Attribute code) const
       
   273 {
       
   274     return reply->q_func()->attribute(code);
       
   275 }
       
   276 
       
   277 void QNetworkAccessBackend::setAttribute(QNetworkRequest::Attribute code, const QVariant &value)
       
   278 {
       
   279     if (value.isValid())
       
   280         reply->attributes.insert(code, value);
       
   281     else
       
   282         reply->attributes.remove(code);
       
   283 }
       
   284 QUrl QNetworkAccessBackend::url() const
       
   285 {
       
   286     return reply->url;
       
   287 }
       
   288 
       
   289 void QNetworkAccessBackend::setUrl(const QUrl &url)
       
   290 {
       
   291     reply->url = url;
       
   292 }
       
   293 
       
   294 void QNetworkAccessBackend::finished()
       
   295 {
       
   296     reply->finished();
       
   297 }
       
   298 
       
   299 void QNetworkAccessBackend::error(QNetworkReply::NetworkError code, const QString &errorString)
       
   300 {
       
   301     reply->error(code, errorString);
       
   302 }
       
   303 
       
   304 #ifndef QT_NO_NETWORKPROXY
       
   305 void QNetworkAccessBackend::proxyAuthenticationRequired(const QNetworkProxy &proxy,
       
   306                                                         QAuthenticator *authenticator)
       
   307 {
       
   308     manager->proxyAuthenticationRequired(this, proxy, authenticator);
       
   309 }
       
   310 #endif
       
   311 
       
   312 void QNetworkAccessBackend::authenticationRequired(QAuthenticator *authenticator)
       
   313 {
       
   314     manager->authenticationRequired(this, authenticator);
       
   315 }
       
   316 
       
   317 void QNetworkAccessBackend::metaDataChanged()
       
   318 {
       
   319     reply->metaDataChanged();
       
   320 }
       
   321 
       
   322 void QNetworkAccessBackend::redirectionRequested(const QUrl &target)
       
   323 {
       
   324     reply->redirectionRequested(target);
       
   325 }
       
   326 
       
   327 void QNetworkAccessBackend::sslErrors(const QList<QSslError> &errors)
       
   328 {
       
   329 #ifndef QT_NO_OPENSSL
       
   330     reply->sslErrors(errors);
       
   331 #else
       
   332     Q_UNUSED(errors);
       
   333 #endif
       
   334 }
       
   335 
       
   336 QT_END_NAMESPACE