diff options
Diffstat (limited to 'src/network/access/qnetworkreplyimpl.cpp')
-rw-r--r-- | src/network/access/qnetworkreplyimpl.cpp | 113 |
1 files changed, 42 insertions, 71 deletions
diff --git a/src/network/access/qnetworkreplyimpl.cpp b/src/network/access/qnetworkreplyimpl.cpp index 61745155f5..b90ec1cc4c 100644 --- a/src/network/access/qnetworkreplyimpl.cpp +++ b/src/network/access/qnetworkreplyimpl.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtNetwork module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qnetworkreplyimpl_p.h" #include "qnetworkaccessbackend_p.h" @@ -51,6 +15,8 @@ QT_BEGIN_NAMESPACE +QT_IMPL_METATYPE_EXTERN_TAGGED(QSharedPointer<char>, QSharedPointer_char) + inline QNetworkReplyImplPrivate::QNetworkReplyImplPrivate() : backend(nullptr), outgoingData(nullptr), copyDevice(nullptr), @@ -152,15 +118,16 @@ void QNetworkReplyImplPrivate::_q_copyReadyRead() return; } - QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader); + const auto totalSizeOpt = QNetworkHeadersPrivate::toInt( + headers().value(QHttpHeaders::WellKnownHeader::ContentLength)); + pauseNotificationHandling(); - // emit readyRead before downloadProgress incase this will cause events to be + // emit readyRead before downloadProgress in case this will cause events to be // processed and we get into a recursive call (as in QProgressDialog). emit q->readyRead(); if (downloadProgressSignalChoke.elapsed() >= progressSignalInterval) { downloadProgressSignalChoke.restart(); - emit q->downloadProgress(bytesDownloaded, - totalSize.isNull() ? Q_INT64_C(-1) : totalSize.toLongLong()); + emit q->downloadProgress(bytesDownloaded, totalSizeOpt.value_or(-1)); } resumeNotificationHandling(); } @@ -193,7 +160,7 @@ void QNetworkReplyImplPrivate::_q_bufferOutgoingData() if (!outgoingDataBuffer) { // first call, create our buffer - outgoingDataBuffer = QSharedPointer<QRingBuffer>::create(); + outgoingDataBuffer = std::make_shared<QRingBuffer>(); QObject::connect(outgoingData, SIGNAL(readyRead()), q, SLOT(_q_bufferOutgoingData())); QObject::connect(outgoingData, SIGNAL(readChannelFinished()), q, SLOT(_q_bufferOutgoingDataFinished())); @@ -249,7 +216,7 @@ void QNetworkReplyImplPrivate::setup(QNetworkAccessManager::Operation op, const // The synchronous HTTP is a corner case, we will put all upload data in one big QByteArray in the outgoingDataBuffer. // Yes, this is not the most efficient thing to do, but on the other hand synchronous XHR needs to die anyway. if (synchronousHttpAttribute.toBool() && outgoingData) { - outgoingDataBuffer = QSharedPointer<QRingBuffer>::create(); + outgoingDataBuffer = std::make_shared<QRingBuffer>(); qint64 previousDataSize = 0; do { previousDataSize = outgoingDataBuffer->size(); @@ -277,7 +244,10 @@ void QNetworkReplyImplPrivate::setup(QNetworkAccessManager::Operation op, const if (bufferingDisallowed) { // if a valid content-length header for the request was supplied, we can disable buffering // if not, we will buffer anyway - if (req.header(QNetworkRequest::ContentLengthHeader).isValid()) { + const auto sizeOpt = QNetworkHeadersPrivate::toInt( + headers().value(QHttpHeaders::WellKnownHeader::ContentLength)); + + if (sizeOpt) { QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection); } else { state = Buffering; @@ -315,7 +285,7 @@ void QNetworkReplyImplPrivate::handleNotifications() if (notificationHandlingPaused) return; - for (InternalNotifications notification : qExchange(pendingNotifications, {})) { + for (InternalNotifications notification : std::exchange(pendingNotifications, {})) { if (state != Working) return; switch (notification) { @@ -492,7 +462,7 @@ void QNetworkReplyImplPrivate::appendDownstreamData(QByteDataBuffer &data) } qint64 bytesWritten = 0; - for (int i = 0; i < data.bufferCount(); i++) { + for (qsizetype i = 0; i < data.bufferCount(); ++i) { QByteArray const &item = data[i]; if (cacheSaveDevice) @@ -512,17 +482,17 @@ void QNetworkReplyImplPrivate::appendDownstreamDataSignalEmissions() { Q_Q(QNetworkReplyImpl); - QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader); + const auto totalSizeOpt = QNetworkHeadersPrivate::toInt( + headers().value(QHttpHeaders::WellKnownHeader::ContentLength)); pauseNotificationHandling(); // important: At the point of this readyRead(), the data parameter list must be empty, // else implicit sharing will trigger memcpy when the user is reading data! emit q->readyRead(); - // emit readyRead before downloadProgress incase this will cause events to be + // emit readyRead before downloadProgress in case this will cause events to be // processed and we get into a recursive call (as in QProgressDialog). if (downloadProgressSignalChoke.elapsed() >= progressSignalInterval) { downloadProgressSignalChoke.restart(); - emit q->downloadProgress(bytesDownloaded, - totalSize.isNull() ? Q_INT64_C(-1) : totalSize.toLongLong()); + emit q->downloadProgress(bytesDownloaded, totalSizeOpt.value_or(-1)); } resumeNotificationHandling(); @@ -541,7 +511,7 @@ void QNetworkReplyImplPrivate::appendDownstreamData(QIODevice *data) // read until EOF from data if (Q_UNLIKELY(copyDevice)) { qCritical("QNetworkReplyImpl: copy from QIODevice already in progress -- " - "backend probly needs to be fixed"); + "backend probably needs to be fixed"); return; } @@ -553,11 +523,6 @@ void QNetworkReplyImplPrivate::appendDownstreamData(QIODevice *data) _q_copyReadyRead(); } -static void downloadBufferDeleter(char *ptr) -{ - delete[] ptr; -} - char* QNetworkReplyImplPrivate::getDownloadBuffer(qint64 size) { Q_Q(QNetworkReplyImpl); @@ -570,7 +535,7 @@ char* QNetworkReplyImplPrivate::getDownloadBuffer(qint64 size) downloadBufferCurrentSize = 0; downloadBufferMaximumSize = size; downloadBuffer = new char[downloadBufferMaximumSize]; // throws if allocation fails - downloadBufferPointer = QSharedPointer<char>(downloadBuffer, downloadBufferDeleter); + downloadBufferPointer = QSharedPointer<char>(downloadBuffer, [](auto p) { delete[] p; }); q->setAttribute(QNetworkRequest::DownloadBufferAttribute, QVariant::fromValue<QSharedPointer<char> > (downloadBufferPointer)); } @@ -610,7 +575,7 @@ void QNetworkReplyImplPrivate::appendDownstreamDataDownloadBuffer(qint64 bytesRe downloadBufferCurrentSize = bytesReceived; // Only emit readyRead when actual data is there - // emit readyRead before downloadProgress incase this will cause events to be + // emit readyRead before downloadProgress in case this will cause events to be // processed and we get into a recursive call (as in QProgressDialog). if (bytesDownloaded > 0) emit q->readyRead(); @@ -628,7 +593,9 @@ void QNetworkReplyImplPrivate::finished() return; pauseNotificationHandling(); - QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader); + const auto totalSizeOpt = QNetworkHeadersPrivate::toInt( + headers().value(QHttpHeaders::WellKnownHeader::ContentLength)); + const auto totalSize = totalSizeOpt.value_or(-1); resumeNotificationHandling(); @@ -638,10 +605,10 @@ void QNetworkReplyImplPrivate::finished() pendingNotifications.clear(); pauseNotificationHandling(); - if (totalSize.isNull() || totalSize == -1) { + if (totalSize == -1) { emit q->downloadProgress(bytesDownloaded, bytesDownloaded); } else { - emit q->downloadProgress(bytesDownloaded, totalSize.toLongLong()); + emit q->downloadProgress(bytesDownloaded, totalSize); } if (bytesUploaded == -1 && (outgoingData || outgoingDataBuffer)) @@ -649,7 +616,7 @@ void QNetworkReplyImplPrivate::finished() resumeNotificationHandling(); // if we don't know the total size of or we received everything save the cache - if (totalSize.isNull() || totalSize == -1 || bytesDownloaded == totalSize) + if (totalSize == -1 || bytesDownloaded == totalSize) completeCacheSave(); // note: might not be a good idea, since users could decide to delete us @@ -685,14 +652,14 @@ void QNetworkReplyImplPrivate::metaDataChanged() // 1. do we have cookies? // 2. are we allowed to set them? if (!manager.isNull()) { - const auto it = cookedHeaders.constFind(QNetworkRequest::SetCookieHeader); - if (it != cookedHeaders.cend() + const auto cookiesOpt = QNetworkHeadersPrivate::toSetCookieList( + headers().values(QHttpHeaders::WellKnownHeader::SetCookie)); + const auto cookies = cookiesOpt.value_or(QList<QNetworkCookie>()); + if (!cookies.empty() && request.attribute(QNetworkRequest::CookieSaveControlAttribute, QNetworkRequest::Automatic).toInt() == QNetworkRequest::Automatic) { QNetworkCookieJar *jar = manager->cookieJar(); if (jar) { - QList<QNetworkCookie> cookies = - qvariant_cast<QList<QNetworkCookie> >(it.value()); jar->setCookiesFromUrl(cookies, url); } } @@ -734,6 +701,7 @@ void QNetworkReplyImplPrivate::readFromBackend() if (backend->bytesAvailable()) emit q->readyRead(); } else { + bool anyBytesRead = false; while (backend->bytesAvailable() && (!readBufferMaxSize || buffer.size() < readBufferMaxSize)) { qint64 toRead = qMin(nextDownstreamBlockSize(), backend->bytesAvailable()); @@ -743,8 +711,10 @@ void QNetworkReplyImplPrivate::readFromBackend() qint64 bytesRead = backend->read(data, toRead); Q_ASSERT(bytesRead <= toRead); buffer.chop(toRead - bytesRead); - emit q->readyRead(); + anyBytesRead |= bytesRead > 0; } + if (anyBytesRead) + emit q->readyRead(); } } @@ -893,9 +863,10 @@ qint64 QNetworkReplyImpl::readData(char *data, qint64 maxlen) break; } } - QVariant totalSize = d->cookedHeaders.value(QNetworkRequest::ContentLengthHeader); - emit downloadProgress(bytesRead, - totalSize.isNull() ? Q_INT64_C(-1) : totalSize.toLongLong()); + + const auto totalSizeOpt = QNetworkHeadersPrivate::toInt( + headers().value(QHttpHeaders::WellKnownHeader::ContentLength)); + emit downloadProgress(bytesRead, totalSizeOpt.value_or(-1)); return bytesRead; } else if (d->backend && d->backend->bytesAvailable()) { return d->backend->read(data, maxlen); |