src/network/access/qnetworkreplyimpl.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 "qnetworkreplyimpl_p.h"
       
    43 #include "qnetworkaccessbackend_p.h"
       
    44 #include "qnetworkcookie.h"
       
    45 #include "qabstractnetworkcache.h"
       
    46 #include "QtCore/qcoreapplication.h"
       
    47 #include "QtCore/qdatetime.h"
       
    48 #include "QtNetwork/qsslconfiguration.h"
       
    49 #include "qnetworkaccesshttpbackend_p.h"
       
    50 
       
    51 #include <QtCore/QCoreApplication>
       
    52 
       
    53 QT_BEGIN_NAMESPACE
       
    54 
       
    55 inline QNetworkReplyImplPrivate::QNetworkReplyImplPrivate()
       
    56     : backend(0), outgoingData(0), outgoingDataBuffer(0),
       
    57       copyDevice(0),
       
    58       cacheEnabled(false), cacheSaveDevice(0),
       
    59       notificationHandlingPaused(false),
       
    60       bytesDownloaded(0), lastBytesDownloaded(-1), bytesUploaded(-1),
       
    61       httpStatusCode(0),
       
    62       state(Idle)
       
    63 {
       
    64 }
       
    65 
       
    66 void QNetworkReplyImplPrivate::_q_startOperation()
       
    67 {
       
    68     // ensure this function is only being called once
       
    69     if (state == Working) {
       
    70         qDebug("QNetworkReplyImpl::_q_startOperation was called more than once");
       
    71         return;
       
    72     }
       
    73     state = Working;
       
    74 
       
    75     if (!backend) {
       
    76         error(QNetworkReplyImpl::ProtocolUnknownError,
       
    77               QCoreApplication::translate("QNetworkReply", "Protocol \"%1\" is unknown").arg(url.scheme())); // not really true!;
       
    78         finished();
       
    79         return;
       
    80     }
       
    81 
       
    82     backend->open();
       
    83     if (state != Finished) {
       
    84         if (operation == QNetworkAccessManager::GetOperation)
       
    85             pendingNotifications.append(NotifyDownstreamReadyWrite);
       
    86 
       
    87         handleNotifications();
       
    88     }
       
    89 }
       
    90 
       
    91 void QNetworkReplyImplPrivate::_q_copyReadyRead()
       
    92 {
       
    93     Q_Q(QNetworkReplyImpl);
       
    94     if (state != Working)
       
    95         return;
       
    96     if (!copyDevice || !q->isOpen())
       
    97         return;
       
    98 
       
    99     forever {
       
   100         qint64 bytesToRead = nextDownstreamBlockSize();
       
   101         if (bytesToRead == 0)
       
   102             // we'll be called again, eventually
       
   103             break;
       
   104 
       
   105         bytesToRead = qBound<qint64>(1, bytesToRead, copyDevice->bytesAvailable());
       
   106         QByteArray byteData;
       
   107         byteData.resize(bytesToRead);
       
   108         qint64 bytesActuallyRead = copyDevice->read(byteData.data(), byteData.size());
       
   109         if (bytesActuallyRead == -1) {
       
   110             byteData.clear();
       
   111             backendNotify(NotifyCopyFinished);
       
   112             break;
       
   113         }
       
   114 
       
   115         byteData.resize(bytesActuallyRead);
       
   116         readBuffer.append(byteData);
       
   117 
       
   118         if (!copyDevice->isSequential() && copyDevice->atEnd()) {
       
   119             backendNotify(NotifyCopyFinished);
       
   120             bytesDownloaded += bytesActuallyRead;
       
   121             break;
       
   122         }
       
   123 
       
   124         bytesDownloaded += bytesActuallyRead;
       
   125     }
       
   126 
       
   127     if (bytesDownloaded == lastBytesDownloaded) {
       
   128         // we didn't read anything
       
   129         return;
       
   130     }
       
   131 
       
   132     lastBytesDownloaded = bytesDownloaded;
       
   133     QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader);
       
   134     pauseNotificationHandling();
       
   135     emit q->downloadProgress(bytesDownloaded,
       
   136                              totalSize.isNull() ? Q_INT64_C(-1) : totalSize.toLongLong());
       
   137     emit q->readyRead();
       
   138     resumeNotificationHandling();
       
   139 }
       
   140 
       
   141 void QNetworkReplyImplPrivate::_q_copyReadChannelFinished()
       
   142 {
       
   143     _q_copyReadyRead();
       
   144 }
       
   145 
       
   146 void QNetworkReplyImplPrivate::_q_bufferOutgoingDataFinished()
       
   147 {
       
   148     Q_Q(QNetworkReplyImpl);
       
   149 
       
   150     // make sure this is only called once, ever.
       
   151     //_q_bufferOutgoingData may call it or the readChannelFinished emission
       
   152     if (state != Buffering)
       
   153         return;
       
   154 
       
   155     // disconnect signals
       
   156     QObject::disconnect(outgoingData, SIGNAL(readyRead()), q, SLOT(_q_bufferOutgoingData()));
       
   157     QObject::disconnect(outgoingData, SIGNAL(readChannelFinished()), q, SLOT(_q_bufferOutgoingDataFinished()));
       
   158 
       
   159     // finally, start the request
       
   160     QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection);
       
   161 }
       
   162 
       
   163 void QNetworkReplyImplPrivate::_q_bufferOutgoingData()
       
   164 {
       
   165     Q_Q(QNetworkReplyImpl);
       
   166 
       
   167     if (!outgoingDataBuffer) {
       
   168         // first call, create our buffer
       
   169         outgoingDataBuffer = new QRingBuffer();
       
   170 
       
   171         QObject::connect(outgoingData, SIGNAL(readyRead()), q, SLOT(_q_bufferOutgoingData()));
       
   172         QObject::connect(outgoingData, SIGNAL(readChannelFinished()), q, SLOT(_q_bufferOutgoingDataFinished()));
       
   173     }
       
   174 
       
   175     qint64 bytesBuffered = 0;
       
   176     qint64 bytesToBuffer = 0;
       
   177 
       
   178     // read data into our buffer
       
   179     forever {
       
   180         bytesToBuffer = outgoingData->bytesAvailable();
       
   181         // unknown? just try 2 kB, this also ensures we always try to read the EOF
       
   182         if (bytesToBuffer <= 0)
       
   183             bytesToBuffer = 2*1024;
       
   184 
       
   185         char *dst = outgoingDataBuffer->reserve(bytesToBuffer);
       
   186         bytesBuffered = outgoingData->read(dst, bytesToBuffer);
       
   187 
       
   188         if (bytesBuffered == -1) {
       
   189             // EOF has been reached.
       
   190             outgoingDataBuffer->chop(bytesToBuffer);
       
   191 
       
   192             _q_bufferOutgoingDataFinished();
       
   193             break;
       
   194         } else if (bytesBuffered == 0) {
       
   195             // nothing read right now, just wait until we get called again
       
   196             outgoingDataBuffer->chop(bytesToBuffer);
       
   197 
       
   198             break;
       
   199         } else {
       
   200             // don't break, try to read() again
       
   201             outgoingDataBuffer->chop(bytesToBuffer - bytesBuffered);
       
   202         }
       
   203     }
       
   204 }
       
   205 
       
   206 
       
   207 void QNetworkReplyImplPrivate::setup(QNetworkAccessManager::Operation op, const QNetworkRequest &req,
       
   208                                      QIODevice *data)
       
   209 {
       
   210     Q_Q(QNetworkReplyImpl);
       
   211 
       
   212     outgoingData = data;
       
   213     request = req;
       
   214     url = request.url();
       
   215     operation = op;
       
   216 
       
   217     if (outgoingData && backend) {
       
   218         // there is data to be uploaded, e.g. HTTP POST.
       
   219 
       
   220         if (!backend->needsResetableUploadData() || !outgoingData->isSequential()) {
       
   221             // backend does not need upload buffering or
       
   222             // fixed size non-sequential
       
   223             // just start the operation
       
   224             QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection);
       
   225         } else {
       
   226             bool bufferingDisallowed =
       
   227                     req.attribute(QNetworkRequest::DoNotBufferUploadDataAttribute,
       
   228                                              false).toBool();
       
   229 
       
   230             if (bufferingDisallowed) {
       
   231                 // if a valid content-length header for the request was supplied, we can disable buffering
       
   232                 // if not, we will buffer anyway
       
   233                 if (req.header(QNetworkRequest::ContentLengthHeader).isValid()) {
       
   234                     QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection);
       
   235                 } else {
       
   236                     state = Buffering;
       
   237                     QMetaObject::invokeMethod(q, "_q_bufferOutgoingData", Qt::QueuedConnection);
       
   238                 }
       
   239             } else {
       
   240                 // _q_startOperation will be called when the buffering has finished.
       
   241                 state = Buffering;
       
   242                 QMetaObject::invokeMethod(q, "_q_bufferOutgoingData", Qt::QueuedConnection);
       
   243             }
       
   244         }
       
   245     } else {
       
   246         // No outgoing data (e.g. HTTP GET request)
       
   247         // or no backend
       
   248         // if no backend, _q_startOperation will handle the error of this
       
   249         QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection);
       
   250     }
       
   251 
       
   252     q->QIODevice::open(QIODevice::ReadOnly);
       
   253 }
       
   254 
       
   255 void QNetworkReplyImplPrivate::backendNotify(InternalNotifications notification)
       
   256 {
       
   257     Q_Q(QNetworkReplyImpl);
       
   258     if (!pendingNotifications.contains(notification))
       
   259         pendingNotifications.enqueue(notification);
       
   260 
       
   261     if (pendingNotifications.size() == 1)
       
   262         QCoreApplication::postEvent(q, new QEvent(QEvent::NetworkReplyUpdated));
       
   263 }
       
   264 
       
   265 void QNetworkReplyImplPrivate::handleNotifications()
       
   266 {
       
   267     if (notificationHandlingPaused)
       
   268         return;
       
   269 
       
   270     NotificationQueue current = pendingNotifications;
       
   271     pendingNotifications.clear();
       
   272 
       
   273     if (state != Working)
       
   274         return;
       
   275 
       
   276     while (!current.isEmpty()) {
       
   277         InternalNotifications notification = current.dequeue();
       
   278         switch (notification) {
       
   279         case NotifyDownstreamReadyWrite:
       
   280             if (copyDevice)
       
   281                 _q_copyReadyRead();
       
   282             else
       
   283                 backend->downstreamReadyWrite();
       
   284             break;
       
   285 
       
   286         case NotifyCloseDownstreamChannel:
       
   287             backend->closeDownstreamChannel();
       
   288             break;
       
   289 
       
   290         case NotifyCopyFinished: {
       
   291             QIODevice *dev = copyDevice;
       
   292             copyDevice = 0;
       
   293             backend->copyFinished(dev);
       
   294             break;
       
   295         }
       
   296         }
       
   297     }
       
   298 }
       
   299 
       
   300 // Do not handle the notifications while we are emitting downloadProgress
       
   301 // or readyRead
       
   302 void QNetworkReplyImplPrivate::pauseNotificationHandling()
       
   303 {
       
   304     notificationHandlingPaused = true;
       
   305 }
       
   306 
       
   307 // Resume notification handling
       
   308 void QNetworkReplyImplPrivate::resumeNotificationHandling()
       
   309 {
       
   310     Q_Q(QNetworkReplyImpl);
       
   311     notificationHandlingPaused = false;
       
   312     if (pendingNotifications.size() >= 1)
       
   313         QCoreApplication::postEvent(q, new QEvent(QEvent::NetworkReplyUpdated));
       
   314 }
       
   315 
       
   316 QAbstractNetworkCache *QNetworkReplyImplPrivate::networkCache() const
       
   317 {
       
   318     if (!backend)
       
   319         return 0;
       
   320     return backend->networkCache();
       
   321 }
       
   322 
       
   323 void QNetworkReplyImplPrivate::createCache()
       
   324 {
       
   325     // check if we can save and if we're allowed to
       
   326     if (!networkCache()
       
   327         || !request.attribute(QNetworkRequest::CacheSaveControlAttribute, true).toBool()
       
   328         || request.attribute(QNetworkRequest::CacheLoadControlAttribute,
       
   329                              QNetworkRequest::PreferNetwork).toInt()
       
   330             == QNetworkRequest::AlwaysNetwork)
       
   331         return;
       
   332     cacheEnabled = true;
       
   333 }
       
   334 
       
   335 bool QNetworkReplyImplPrivate::isCachingEnabled() const
       
   336 {
       
   337     return (cacheEnabled && networkCache() != 0);
       
   338 }
       
   339 
       
   340 void QNetworkReplyImplPrivate::setCachingEnabled(bool enable)
       
   341 {
       
   342     if (!enable && !cacheEnabled)
       
   343         return;                 // nothing to do
       
   344     if (enable && cacheEnabled)
       
   345         return;                 // nothing to do either!
       
   346 
       
   347     if (enable) {
       
   348         if (bytesDownloaded) {
       
   349             // refuse to enable in this case
       
   350             qCritical("QNetworkReplyImpl: backend error: caching was enabled after some bytes had been written");
       
   351             return;
       
   352         }
       
   353 
       
   354         createCache();
       
   355     } else {
       
   356         // someone told us to turn on, then back off?
       
   357         // ok... but you should make up your mind
       
   358         qDebug("QNetworkReplyImpl: setCachingEnabled(true) called after setCachingEnabled(false) -- "
       
   359                "backend %s probably needs to be fixed",
       
   360                backend->metaObject()->className());
       
   361         networkCache()->remove(url);
       
   362         cacheSaveDevice = 0;
       
   363         cacheEnabled = false;
       
   364     }
       
   365 }
       
   366 
       
   367 void QNetworkReplyImplPrivate::completeCacheSave()
       
   368 {
       
   369     if (cacheEnabled && errorCode != QNetworkReplyImpl::NoError) {
       
   370         networkCache()->remove(url);
       
   371     } else if (cacheEnabled && cacheSaveDevice) {
       
   372         networkCache()->insert(cacheSaveDevice);
       
   373     }
       
   374     cacheSaveDevice = 0;
       
   375     cacheEnabled = false;
       
   376 }
       
   377 
       
   378 void QNetworkReplyImplPrivate::emitUploadProgress(qint64 bytesSent, qint64 bytesTotal)
       
   379 {
       
   380     Q_Q(QNetworkReplyImpl);
       
   381     bytesUploaded = bytesSent;
       
   382     pauseNotificationHandling();
       
   383     emit q->uploadProgress(bytesSent, bytesTotal);
       
   384     resumeNotificationHandling();
       
   385 }
       
   386 
       
   387 
       
   388 qint64 QNetworkReplyImplPrivate::nextDownstreamBlockSize() const
       
   389 {
       
   390     enum { DesiredBufferSize = 32 * 1024 };
       
   391     if (readBufferMaxSize == 0)
       
   392         return DesiredBufferSize;
       
   393 
       
   394     return qMax<qint64>(0, readBufferMaxSize - readBuffer.byteAmount());
       
   395 }
       
   396 
       
   397 // we received downstream data and send this to the cache
       
   398 // and to our readBuffer (which in turn gets read by the user of QNetworkReply)
       
   399 void QNetworkReplyImplPrivate::appendDownstreamData(QByteDataBuffer &data)
       
   400 {
       
   401     Q_Q(QNetworkReplyImpl);
       
   402     if (!q->isOpen())
       
   403         return;
       
   404 
       
   405     if (cacheEnabled && !cacheSaveDevice) {
       
   406         // save the meta data
       
   407         QNetworkCacheMetaData metaData;
       
   408         metaData.setUrl(url);
       
   409         metaData = backend->fetchCacheMetaData(metaData);
       
   410 
       
   411         // save the redirect request also in the cache
       
   412         QVariant redirectionTarget = q->attribute(QNetworkRequest::RedirectionTargetAttribute);
       
   413         if (redirectionTarget.isValid()) {
       
   414             QNetworkCacheMetaData::AttributesMap attributes = metaData.attributes();
       
   415             attributes.insert(QNetworkRequest::RedirectionTargetAttribute, redirectionTarget);
       
   416             metaData.setAttributes(attributes);
       
   417         }
       
   418 
       
   419         cacheSaveDevice = networkCache()->prepare(metaData);
       
   420 
       
   421         if (!cacheSaveDevice || (cacheSaveDevice && !cacheSaveDevice->isOpen())) {
       
   422             if (cacheSaveDevice && !cacheSaveDevice->isOpen())
       
   423                 qCritical("QNetworkReplyImpl: network cache returned a device that is not open -- "
       
   424                       "class %s probably needs to be fixed",
       
   425                       networkCache()->metaObject()->className());
       
   426 
       
   427             networkCache()->remove(url);
       
   428             cacheSaveDevice = 0;
       
   429             cacheEnabled = false;
       
   430         }
       
   431     }
       
   432 
       
   433     qint64 bytesWritten = 0;
       
   434     for (int i = 0; i < data.bufferCount(); i++) {
       
   435         QByteArray item = data[i];
       
   436 
       
   437         if (cacheSaveDevice)
       
   438             cacheSaveDevice->write(item.constData(), item.size());
       
   439         readBuffer.append(item);
       
   440 
       
   441         bytesWritten += item.size();
       
   442     }
       
   443     data.clear();
       
   444 
       
   445     bytesDownloaded += bytesWritten;
       
   446     lastBytesDownloaded = bytesDownloaded;
       
   447 
       
   448     QPointer<QNetworkReplyImpl> qq = q;
       
   449 
       
   450     QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader);
       
   451     pauseNotificationHandling();
       
   452     emit q->downloadProgress(bytesDownloaded,
       
   453                              totalSize.isNull() ? Q_INT64_C(-1) : totalSize.toLongLong());
       
   454     // important: At the point of this readyRead(), the data parameter list must be empty,
       
   455     // else implicit sharing will trigger memcpy when the user is reading data!
       
   456     emit q->readyRead();
       
   457 
       
   458     // hopefully we haven't been deleted here
       
   459     if (!qq.isNull()) {
       
   460         resumeNotificationHandling();
       
   461         // do we still have room in the buffer?
       
   462         if (nextDownstreamBlockSize() > 0)
       
   463             backendNotify(QNetworkReplyImplPrivate::NotifyDownstreamReadyWrite);
       
   464     }
       
   465 }
       
   466 
       
   467 // this is used when it was fetched from the cache, right?
       
   468 void QNetworkReplyImplPrivate::appendDownstreamData(QIODevice *data)
       
   469 {
       
   470     Q_Q(QNetworkReplyImpl);
       
   471     if (!q->isOpen())
       
   472         return;
       
   473 
       
   474     // read until EOF from data
       
   475     if (copyDevice) {
       
   476         qCritical("QNetworkReplyImpl: copy from QIODevice already in progress -- "
       
   477                   "backend probly needs to be fixed");
       
   478         return;
       
   479     }
       
   480 
       
   481     copyDevice = data;
       
   482     q->connect(copyDevice, SIGNAL(readyRead()), SLOT(_q_copyReadyRead()));
       
   483     q->connect(copyDevice, SIGNAL(readChannelFinished()), SLOT(_q_copyReadChannelFinished()));
       
   484 
       
   485     // start the copy:
       
   486     _q_copyReadyRead();
       
   487 }
       
   488 
       
   489 void QNetworkReplyImplPrivate::finished()
       
   490 {
       
   491     Q_Q(QNetworkReplyImpl);
       
   492     if (state == Finished || state == Aborted)
       
   493         return;
       
   494 
       
   495     state = Finished;
       
   496     pendingNotifications.clear();
       
   497 
       
   498     pauseNotificationHandling();
       
   499     QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader);
       
   500     if (totalSize.isNull() || totalSize == -1) {
       
   501         emit q->downloadProgress(bytesDownloaded, bytesDownloaded);
       
   502     }
       
   503 
       
   504     if (bytesUploaded == -1 && (outgoingData || outgoingDataBuffer))
       
   505         emit q->uploadProgress(0, 0);
       
   506     resumeNotificationHandling();
       
   507 
       
   508     completeCacheSave();
       
   509 
       
   510     // note: might not be a good idea, since users could decide to delete us
       
   511     // which would delete the backend too...
       
   512     // maybe we should protect the backend
       
   513     pauseNotificationHandling();
       
   514     emit q->readChannelFinished();
       
   515     emit q->finished();
       
   516     resumeNotificationHandling();
       
   517 }
       
   518 
       
   519 void QNetworkReplyImplPrivate::error(QNetworkReplyImpl::NetworkError code, const QString &errorMessage)
       
   520 {
       
   521     Q_Q(QNetworkReplyImpl);
       
   522 
       
   523     errorCode = code;
       
   524     q->setErrorString(errorMessage);
       
   525 
       
   526     // note: might not be a good idea, since users could decide to delete us
       
   527     // which would delete the backend too...
       
   528     // maybe we should protect the backend
       
   529     emit q->error(code);
       
   530 }
       
   531 
       
   532 void QNetworkReplyImplPrivate::metaDataChanged()
       
   533 {
       
   534     Q_Q(QNetworkReplyImpl);
       
   535     // do we have cookies?
       
   536     if (cookedHeaders.contains(QNetworkRequest::SetCookieHeader) && !manager.isNull()) {
       
   537         QList<QNetworkCookie> cookies =
       
   538             qvariant_cast<QList<QNetworkCookie> >(cookedHeaders.value(QNetworkRequest::SetCookieHeader));
       
   539         QNetworkCookieJar *jar = manager->cookieJar();
       
   540         if (jar)
       
   541             jar->setCookiesFromUrl(cookies, url);
       
   542     }
       
   543     emit q->metaDataChanged();
       
   544 }
       
   545 
       
   546 void QNetworkReplyImplPrivate::redirectionRequested(const QUrl &target)
       
   547 {
       
   548     attributes.insert(QNetworkRequest::RedirectionTargetAttribute, target);
       
   549 }
       
   550 
       
   551 void QNetworkReplyImplPrivate::sslErrors(const QList<QSslError> &errors)
       
   552 {
       
   553 #ifndef QT_NO_OPENSSL
       
   554     Q_Q(QNetworkReplyImpl);
       
   555     emit q->sslErrors(errors);
       
   556 #else
       
   557     Q_UNUSED(errors);
       
   558 #endif
       
   559 }
       
   560 
       
   561 bool QNetworkReplyImplPrivate::isFinished() const
       
   562 {
       
   563     return (state == Finished || state == Aborted);
       
   564 }
       
   565 
       
   566 QNetworkReplyImpl::QNetworkReplyImpl(QObject *parent)
       
   567     : QNetworkReply(*new QNetworkReplyImplPrivate, parent)
       
   568 {
       
   569 }
       
   570 
       
   571 QNetworkReplyImpl::~QNetworkReplyImpl()
       
   572 {
       
   573     Q_D(QNetworkReplyImpl);
       
   574     if (d->isCachingEnabled())
       
   575         d->networkCache()->remove(url());
       
   576     if (d->outgoingDataBuffer)
       
   577         delete d->outgoingDataBuffer;
       
   578 }
       
   579 
       
   580 void QNetworkReplyImpl::abort()
       
   581 {
       
   582     Q_D(QNetworkReplyImpl);
       
   583     if (d->state == QNetworkReplyImplPrivate::Aborted)
       
   584         return;
       
   585 
       
   586     // stop both upload and download
       
   587     if (d->outgoingData)
       
   588         disconnect(d->outgoingData, 0, this, 0);
       
   589     if (d->copyDevice)
       
   590         disconnect(d->copyDevice, 0, this, 0);
       
   591 
       
   592     QNetworkReply::close();
       
   593 
       
   594     if (d->state != QNetworkReplyImplPrivate::Finished) {
       
   595         // emit signals
       
   596         d->error(OperationCanceledError, tr("Operation canceled"));
       
   597         d->finished();
       
   598     }
       
   599     d->state = QNetworkReplyImplPrivate::Aborted;
       
   600 
       
   601     // finished may access the backend
       
   602     if (d->backend) {
       
   603         d->backend->deleteLater();
       
   604         d->backend = 0;
       
   605     }
       
   606 }
       
   607 
       
   608 void QNetworkReplyImpl::close()
       
   609 {
       
   610     Q_D(QNetworkReplyImpl);
       
   611     if (d->state == QNetworkReplyImplPrivate::Aborted ||
       
   612         d->state == QNetworkReplyImplPrivate::Finished)
       
   613         return;
       
   614 
       
   615     // stop the download
       
   616     if (d->backend)
       
   617         d->backend->closeDownstreamChannel();
       
   618     if (d->copyDevice)
       
   619         disconnect(d->copyDevice, 0, this, 0);
       
   620 
       
   621     QNetworkReply::close();
       
   622 
       
   623     // emit signals
       
   624     d->error(OperationCanceledError, tr("Operation canceled"));
       
   625     d->finished();
       
   626 }
       
   627 
       
   628 /*!
       
   629     Returns the number of bytes available for reading with
       
   630     QIODevice::read(). The number of bytes available may grow until
       
   631     the finished() signal is emitted.
       
   632 */
       
   633 qint64 QNetworkReplyImpl::bytesAvailable() const
       
   634 {
       
   635     return QNetworkReply::bytesAvailable() + d_func()->readBuffer.byteAmount();
       
   636 }
       
   637 
       
   638 void QNetworkReplyImpl::setReadBufferSize(qint64 size)
       
   639 {
       
   640     Q_D(QNetworkReplyImpl);
       
   641     if (size > d->readBufferMaxSize &&
       
   642         size > d->readBuffer.byteAmount())
       
   643         d->backendNotify(QNetworkReplyImplPrivate::NotifyDownstreamReadyWrite);
       
   644 
       
   645     QNetworkReply::setReadBufferSize(size);
       
   646 }
       
   647 
       
   648 #ifndef QT_NO_OPENSSL
       
   649 QSslConfiguration QNetworkReplyImpl::sslConfigurationImplementation() const
       
   650 {
       
   651     Q_D(const QNetworkReplyImpl);
       
   652     QSslConfiguration config;
       
   653     if (d->backend)
       
   654         d->backend->fetchSslConfiguration(config);
       
   655     return config;
       
   656 }
       
   657 
       
   658 void QNetworkReplyImpl::setSslConfigurationImplementation(const QSslConfiguration &config)
       
   659 {
       
   660     Q_D(QNetworkReplyImpl);
       
   661     if (d->backend && !config.isNull())
       
   662         d->backend->setSslConfiguration(config);
       
   663 }
       
   664 
       
   665 void QNetworkReplyImpl::ignoreSslErrors()
       
   666 {
       
   667     Q_D(QNetworkReplyImpl);
       
   668     if (d->backend)
       
   669         d->backend->ignoreSslErrors();
       
   670 }
       
   671 
       
   672 void QNetworkReplyImpl::ignoreSslErrorsImplementation(const QList<QSslError> &errors)
       
   673 {
       
   674     Q_D(QNetworkReplyImpl);
       
   675     if (d->backend)
       
   676         d->backend->ignoreSslErrors(errors);
       
   677 }
       
   678 #endif  // QT_NO_OPENSSL
       
   679 
       
   680 /*!
       
   681     \internal
       
   682 */
       
   683 qint64 QNetworkReplyImpl::readData(char *data, qint64 maxlen)
       
   684 {
       
   685     Q_D(QNetworkReplyImpl);
       
   686     if (d->readBuffer.isEmpty())
       
   687         return d->state == QNetworkReplyImplPrivate::Finished ? -1 : 0;
       
   688 
       
   689     d->backendNotify(QNetworkReplyImplPrivate::NotifyDownstreamReadyWrite);
       
   690     if (maxlen == 1) {
       
   691         // optimization for getChar()
       
   692         *data = d->readBuffer.getChar();
       
   693         return 1;
       
   694     }
       
   695 
       
   696     maxlen = qMin<qint64>(maxlen, d->readBuffer.byteAmount());
       
   697     return d->readBuffer.read(data, maxlen);
       
   698 }
       
   699 
       
   700 /*!
       
   701    \internal Reimplemented for internal purposes
       
   702 */
       
   703 bool QNetworkReplyImpl::event(QEvent *e)
       
   704 {
       
   705     if (e->type() == QEvent::NetworkReplyUpdated) {
       
   706         d_func()->handleNotifications();
       
   707         return true;
       
   708     }
       
   709 
       
   710     return QObject::event(e);
       
   711 }
       
   712 
       
   713 QT_END_NAMESPACE
       
   714 
       
   715 #include "moc_qnetworkreplyimpl_p.cpp"
       
   716