diff options
author | Kai Koehne <kai.koehne@nokia.com> | 2011-05-10 13:14:16 (GMT) |
---|---|---|
committer | Kai Koehne <kai.koehne@nokia.com> | 2011-05-10 13:14:16 (GMT) |
commit | baecfbc7f7e0ec7edddb74a2e760c685977e9084 (patch) | |
tree | f9071063e3b8dfcc1b2fcb3236be84fce5b17091 /src/network/access | |
parent | bde58ad1e7d2b38d2882aaf869e93b0415128836 (diff) | |
parent | f3ddd2d995b7485cdc1c3420d254499904ff9dd9 (diff) | |
download | Qt-baecfbc7f7e0ec7edddb74a2e760c685977e9084.zip Qt-baecfbc7f7e0ec7edddb74a2e760c685977e9084.tar.gz Qt-baecfbc7f7e0ec7edddb74a2e760c685977e9084.tar.bz2 |
Merge remote branch 'qt/4.8' into master-qml-staging
Conflicts:
src/declarative/debugger/qdeclarativedebugserver.cpp
src/plugins/qmltooling/qmltooling.pro
Diffstat (limited to 'src/network/access')
28 files changed, 1274 insertions, 83 deletions
diff --git a/src/network/access/access.pri b/src/network/access/access.pri index 57a79b3..5ead3ad 100644 --- a/src/network/access/access.pri +++ b/src/network/access/access.pri @@ -34,7 +34,9 @@ HEADERS += \ access/qabstractnetworkcache.h \ access/qnetworkdiskcache_p.h \ access/qnetworkdiskcache.h \ - access/qhttpthreaddelegate_p.h + access/qhttpthreaddelegate_p.h \ + access/qhttpmultipart.h \ + access/qhttpmultipart_p.h SOURCES += \ access/qftp.cpp \ @@ -62,6 +64,7 @@ SOURCES += \ access/qnetworkreplyfileimpl.cpp \ access/qabstractnetworkcache.cpp \ access/qnetworkdiskcache.cpp \ - access/qhttpthreaddelegate.cpp + access/qhttpthreaddelegate.cpp \ + access/qhttpmultipart.cpp include($$PWD/../../3rdparty/zlib_dependency.pri) diff --git a/src/network/access/qftp.cpp b/src/network/access/qftp.cpp index 69e59d1..4ff45ba 100644 --- a/src/network/access/qftp.cpp +++ b/src/network/access/qftp.cpp @@ -319,6 +319,10 @@ void QFtpDTP::connectToHost(const QString & host, quint16 port) socket = 0; } socket = new QTcpSocket(this); +#ifndef QT_NO_BEARERMANAGEMENT + //copy network session down to the socket + socket->setProperty("_q_networksession", property("_q_networksession")); +#endif socket->setObjectName(QLatin1String("QFtpDTP Passive state socket")); connect(socket, SIGNAL(connected()), SLOT(socketConnected())); connect(socket, SIGNAL(readyRead()), SLOT(socketReadyRead())); @@ -331,6 +335,10 @@ void QFtpDTP::connectToHost(const QString & host, quint16 port) int QFtpDTP::setupListener(const QHostAddress &address) { +#ifndef QT_NO_BEARERMANAGEMENT + //copy network session down to the socket + listener.setProperty("_q_networksession", property("_q_networksession")); +#endif if (!listener.isListening() && !listener.listen(address, 0)) return -1; return listener.serverPort(); @@ -808,6 +816,11 @@ QFtpPI::QFtpPI(QObject *parent) : void QFtpPI::connectToHost(const QString &host, quint16 port) { emit connectState(QFtp::HostLookup); +#ifndef QT_NO_BEARERMANAGEMENT + //copy network session down to the socket & DTP + commandSocket.setProperty("_q_networksession", property("_q_networksession")); + dtp.setProperty("_q_networksession", property("_q_networksession")); +#endif commandSocket.connectToHost(host, port); } @@ -2240,6 +2253,10 @@ void QFtpPrivate::_q_startNextCommand() c->rawCmds.clear(); _q_piFinished(QLatin1String("Proxy set to ") + proxyHost + QLatin1Char(':') + QString::number(proxyPort)); } else if (c->command == QFtp::ConnectToHost) { +#ifndef QT_NO_BEARERMANAGEMENT + //copy network session down to the PI + pi.setProperty("_q_networksession", q->property("_q_networksession")); +#endif if (!proxyHost.isEmpty()) { host = c->rawCmds[0]; port = c->rawCmds[1].toUInt(); diff --git a/src/network/access/qhttpmultipart.cpp b/src/network/access/qhttpmultipart.cpp new file mode 100644 index 0000000..640f9ea --- /dev/null +++ b/src/network/access/qhttpmultipart.cpp @@ -0,0 +1,548 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qhttpmultipart.h" +#include "qhttpmultipart_p.h" +#include "QtCore/qdatetime.h" // for initializing the random number generator with QTime +#include "QtCore/qmutex.h" +#include "QtCore/qthreadstorage.h" + +QT_BEGIN_NAMESPACE + +/*! + \class QHttpPart + \brief The QHttpPart class holds a body part to be used inside a + HTTP multipart MIME message. + \since 4.8 + + \ingroup network + \inmodule QtNetwork + + The QHttpPart class holds a body part to be used inside a HTTP + multipart MIME message (which is represented by the QHttpMultiPart class). + A QHttpPart consists of a header block + and a data block, which are separated by each other by two + consecutive new lines. An example for one part would be: + + \snippet doc/src/snippets/code/src_network_access_qhttppart.cpp 0 + + For setting headers, use setHeader() and setRawHeader(), which behave + exactly like QNetworkRequest::setHeader() and QNetworkRequest::setRawHeader(). + + For reading small pieces of data, use setBody(); for larger data blocks + like e.g. images, use setBodyDevice(). The latter method saves memory by + not copying the data internally, but reading directly from the device. + This means that the device must be opened and readable at the moment when + the multipart message containing the body part is sent on the network via + QNetworkAccessManager::post(). + + To construct a QHttpPart with a small body, consider the following snippet + (this produces the data shown in the example above): + + \snippet doc/src/snippets/code/src_network_access_qhttppart.cpp 1 + + To construct a QHttpPart reading from a device (e.g. a file), the following + can be applied: + + \snippet doc/src/snippets/code/src_network_access_qhttppart.cpp 2 + + Be aware that QHttpPart does not take ownership of the device when set, so + it is the developer's responsibility to destroy it when it is not needed anymore. + A good idea might be to set the multipart message as parent object for the device, + as documented at the documentation for QHttpMultiPart. + + \sa QHttpMultiPart, QNetworkAccessManager +*/ + + +/*! + Constructs an empty QHttpPart object. +*/ +QHttpPart::QHttpPart() : d(new QHttpPartPrivate) +{ +} + +/*! + Creates a copy of \a other. +*/ +QHttpPart::QHttpPart(const QHttpPart &other) : d(other.d) +{ +} + +/*! + Destroys this QHttpPart. +*/ +QHttpPart::~QHttpPart() +{ + d = 0; +} + +/*! + Creates a copy of \a other. +*/ +QHttpPart &QHttpPart::operator=(const QHttpPart &other) +{ + d = other.d; + return *this; +} + +/*! + Returns true if this object is the same as \a other (i.e., if they + have the same headers and body). + + \sa operator!=() +*/ +bool QHttpPart::operator==(const QHttpPart &other) const +{ + return d == other.d || *d == *other.d; +} + +/*! + \fn bool QHttpPart::operator!=(const QHttpPart &other) const + + Returns true if this object is not the same as \a other. + + \sa operator==() +*/ + +/*! + Sets the value of the known header \a header to be \a value, + overriding any previously set headers. + + \sa QNetworkRequest::KnownHeaders, setRawHeader(), QNetworkRequest::setHeader() +*/ +void QHttpPart::setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value) +{ + d->setCookedHeader(header, value); +} + +/*! + Sets the header \a headerName to be of value \a headerValue. If \a + headerName corresponds to a known header (see + QNetworkRequest::KnownHeaders), the raw format will be parsed and + the corresponding "cooked" header will be set as well. + + Note: setting the same header twice overrides the previous + setting. To accomplish the behaviour of multiple HTTP headers of + the same name, you should concatenate the two values, separating + them with a comma (",") and set one single raw header. + + \sa QNetworkRequest::KnownHeaders, setHeader(), QNetworkRequest::setRawHeader() +*/ +void QHttpPart::setRawHeader(const QByteArray &headerName, const QByteArray &headerValue) +{ + d->setRawHeader(headerName, headerValue); +} + +/*! + Sets the body of this MIME part to \a body. The body set with this method + will be used unless the device is set via setBodyDevice(). For a large + amount of data (e.g. an image), use setBodyDevice(), which will not copy + the data internally. + + \sa setBodyDevice() +*/ +void QHttpPart::setBody(const QByteArray &body) +{ + d->setBody(body); +} + +/*! + Sets the device to read the content from to \a device. For large amounts of data + this method should be preferred over setBody(), + because the content is not copied when using this method, but read + directly from the device. + \a device must be open and readable. QHttpPart does not take ownership + of \a device, i.e. the device must be closed and destroyed if necessary. + if \a device is sequential (e.g. sockets, but not files), + QNetworkAccessManager::post() should be called after \a device has + emitted finished(). + For unsetting the device and using data set via setBody(), use + "setBodyDevice(0)". + + \sa setBody(), QNetworkAccessManager::post() + */ +void QHttpPart::setBodyDevice(QIODevice *device) +{ + d->setBodyDevice(device); +} + + + +/*! + \class QHttpMultiPart + \brief The QHttpMultiPart class resembles a MIME multipart message to be sent over HTTP. + \since 4.8 + + \ingroup network + \inmodule QtNetwork + + The QHttpMultiPart resembles a MIME multipart message, as described in RFC 2046, + which is to be sent over HTTP. + A multipart message consists of an arbitrary number of body parts (see QHttpPart), + which are separated by a unique boundary. The boundary of the QHttpMultiPart is + constructed with the string "boundary_.oOo._" followed by random characters, + and provides enough uniqueness to make sure it does not occur inside the parts itself. + If desired, the boundary can still be set via setBoundary(). + + As an example, consider the following code snippet, which constructs a multipart + message containing a text part followed by an image part: + + \snippet doc/src/snippets/code/src_network_access_qhttpmultipart.cpp 0 + + \sa QHttpPart, QNetworkAccessManager::post() +*/ + +/*! + \enum QHttpMultiPart::ContentType + + List of known content types for a multipart subtype as described + in RFC 2046 and others. + + \value MixedType corresponds to the "multipart/mixed" subtype, + meaning the body parts are independent of each other, as described + in RFC 2046. + + \value RelatedType corresponds to the "multipart/related" subtype, + meaning the body parts are related to each other, as described in RFC 2387. + + \value FormDataType corresponds to the "multipart/form-data" + subtype, meaning the body parts contain form elements, as described in RFC 2388. + + \value AlternativeType corresponds to the "multipart/alternative" + subtype, meaning the body parts are alternative representations of + the same information, as described in RFC 2046. + + \sa setContentType() +*/ + +/*! + Constructs a QHttpMultiPart with content type MixedType and sets + parent as the parent object. + + \sa QHttpMultiPart::ContentType +*/ +QHttpMultiPart::QHttpMultiPart(QObject *parent) : QObject(*new QHttpMultiPartPrivate, parent) +{ + Q_D(QHttpMultiPart); + d->contentType = MixedType; +} + +/*! + Constructs a QHttpMultiPart with content type \a contentType and + sets parent as the parent object. + + \sa QHttpMultiPart::ContentType +*/ +QHttpMultiPart::QHttpMultiPart(QHttpMultiPart::ContentType contentType, QObject *parent) : QObject(*new QHttpMultiPartPrivate, parent) +{ + Q_D(QHttpMultiPart); + d->contentType = contentType; +} + +/*! + Destroys the multipart. +*/ +QHttpMultiPart::~QHttpMultiPart() +{ +} + +/*! + Appends \a httpPart to this multipart. +*/ +void QHttpMultiPart::append(const QHttpPart &httpPart) +{ + d_func()->parts.append(httpPart); +} + +/*! + Sets the content type to \a contentType. The content type will be used + in the HTTP header section when sending the multipart message via + QNetworkAccessManager::post(). + In case you want to use a multipart subtype not contained in + QHttpMultiPart::ContentType, + you can add the "Content-Type" header field to the QNetworkRequest + by hand, and then use this request together with the multipart + message for posting. + + \sa QHttpMultiPart::ContentType, QNetworkAccessManager::post() +*/ +void QHttpMultiPart::setContentType(QHttpMultiPart::ContentType contentType) +{ + d_func()->contentType = contentType; +} + +/*! + returns the boundary. + + \sa setBoundary() +*/ +QByteArray QHttpMultiPart::boundary() const +{ + return d_func()->boundary; +} + +/*! + Sets the boundary to \a boundary. + + Usually, you do not need to generate a boundary yourself; upon construction + the boundary is initiated with the string "boundary_.oOo._" followed by random + characters, and provides enough uniqueness to make sure it does not occur + inside the parts itself. + + \sa boundary() +*/ +void QHttpMultiPart::setBoundary(const QByteArray &boundary) +{ + d_func()->boundary = boundary; +} + + + +// ------------------------------------------------------------------ +// ----------- implementations of private classes: ------------------ +// ------------------------------------------------------------------ + + + +qint64 QHttpPartPrivate::bytesAvailable() const +{ + checkHeaderCreated(); + qint64 bytesAvailable = header.count(); + if (bodyDevice) { + bytesAvailable += bodyDevice->bytesAvailable() - readPointer; + } else { + bytesAvailable += body.count() - readPointer; + } + // the device might have closed etc., so make sure we do not return a negative value + return qMax(bytesAvailable, (qint64) 0); +} + +qint64 QHttpPartPrivate::readData(char *data, qint64 maxSize) +{ + checkHeaderCreated(); + qint64 bytesRead = 0; + qint64 headerDataCount = header.count(); + + // read header if it has not been read yet + if (readPointer < headerDataCount) { + bytesRead = qMin(headerDataCount - readPointer, maxSize); + const char *headerData = header.constData(); + memcpy(data, headerData + readPointer, bytesRead); + readPointer += bytesRead; + } + // read content if there is still space + if (bytesRead < maxSize) { + if (bodyDevice) { + qint64 dataBytesRead = bodyDevice->read(data + bytesRead, maxSize - bytesRead); + if (dataBytesRead == -1) + return -1; + bytesRead += dataBytesRead; + readPointer += dataBytesRead; + } else { + qint64 contentBytesRead = qMin(body.count() - readPointer + headerDataCount, maxSize - bytesRead); + const char *contentData = body.constData(); + // if this method is called several times, we need to find the + // right offset in the content ourselves: + memcpy(data + bytesRead, contentData + readPointer - headerDataCount, contentBytesRead); + bytesRead += contentBytesRead; + readPointer += contentBytesRead; + } + } + return bytesRead; +} + +qint64 QHttpPartPrivate::size() const +{ + checkHeaderCreated(); + qint64 size = header.count(); + if (bodyDevice) { + size += bodyDevice->size(); + } else { + size += body.count(); + } + return size; +} + +bool QHttpPartPrivate::reset() +{ + bool ret = true; + if (bodyDevice) + if (!bodyDevice->reset()) + ret = false; + readPointer = 0; + return ret; +} +void QHttpPartPrivate::checkHeaderCreated() const +{ + if (!headerCreated) { + // copied from QHttpNetworkRequestPrivate::header() and adapted + QList<QPair<QByteArray, QByteArray> > fields = allRawHeaders(); + QList<QPair<QByteArray, QByteArray> >::const_iterator it = fields.constBegin(); + for (; it != fields.constEnd(); ++it) + header += it->first + ": " + it->second + "\r\n"; + header += "\r\n"; + headerCreated = true; + } +} + +Q_GLOBAL_STATIC(QThreadStorage<bool *>, seedCreatedStorage); + +QHttpMultiPartPrivate::QHttpMultiPartPrivate() : contentType(QHttpMultiPart::MixedType), device(new QHttpMultiPartIODevice(this)) +{ + if (!seedCreatedStorage()->hasLocalData()) { + qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()) ^ reinterpret_cast<quintptr>(this)); + seedCreatedStorage()->setLocalData(new bool(true)); + } + + boundary = QByteArray("boundary_.oOo._") + + QByteArray::number(qrand()).toBase64() + + QByteArray::number(qrand()).toBase64() + + QByteArray::number(qrand()).toBase64(); + + // boundary must not be longer than 70 characters, see RFC 2046, section 5.1.1 + if (boundary.count() > 70) + boundary = boundary.left(70); +} + +qint64 QHttpMultiPartIODevice::size() const +{ + // if not done yet, we calculate the size and the offsets of each part, + // including boundary (needed later in readData) + if (deviceSize == -1) { + qint64 currentSize = 0; + qint64 boundaryCount = multiPart->boundary.count(); + for (int a = 0; a < multiPart->parts.count(); a++) { + partOffsets.append(currentSize); + // 4 additional bytes for the "--" before and the "\r\n" after the boundary, + // and 2 bytes for the "\r\n" after the content + currentSize += boundaryCount + 4 + multiPart->parts.at(a).d->size() + 2; + } + currentSize += boundaryCount + 4; // size for ending boundary and 2 beginning and ending dashes + deviceSize = currentSize; + } + return deviceSize; +} + +bool QHttpMultiPartIODevice::isSequential() const +{ + for (int a = 0; a < multiPart->parts.count(); a++) { + QIODevice *device = multiPart->parts.at(a).d->bodyDevice; + // we are sequential if any of the bodyDevices of our parts are sequential; + // when reading from a byte array, we are not sequential + if (device && device->isSequential()) + return true; + } + return false; +} + +bool QHttpMultiPartIODevice::reset() +{ + for (int a = 0; a < multiPart->parts.count(); a++) + if (!multiPart->parts[a].d->reset()) + return false; + return true; +} +qint64 QHttpMultiPartIODevice::readData(char *data, qint64 maxSize) +{ + qint64 bytesRead = 0, index = 0; + + // skip the parts we have already read + while (index < multiPart->parts.count() && + readPointer >= partOffsets.at(index) + multiPart->parts.at(index).d->size()) + index++; + + // read the data + while (bytesRead < maxSize && index < multiPart->parts.count()) { + + // check whether we need to read the boundary of the current part + QByteArray boundaryData = "--" + multiPart->boundary + "\r\n"; + qint64 boundaryCount = boundaryData.count(); + qint64 partIndex = readPointer - partOffsets.at(index); + if (partIndex < boundaryCount) { + qint64 boundaryBytesRead = qMin(boundaryCount - partIndex, maxSize - bytesRead); + memcpy(data + bytesRead, boundaryData.constData() + partIndex, boundaryBytesRead); + bytesRead += boundaryBytesRead; + readPointer += boundaryBytesRead; + partIndex += boundaryBytesRead; + } + + // check whether we need to read the data of the current part + if (bytesRead < maxSize && partIndex >= boundaryCount && partIndex < boundaryCount + multiPart->parts.at(index).d->size()) { + qint64 dataBytesRead = multiPart->parts[index].d->readData(data + bytesRead, maxSize - bytesRead); + if (dataBytesRead == -1) + return -1; + bytesRead += dataBytesRead; + readPointer += dataBytesRead; + partIndex += dataBytesRead; + } + + // check whether we need to read the ending CRLF of the current part + if (bytesRead < maxSize && partIndex >= boundaryCount + multiPart->parts.at(index).d->size()) { + if (bytesRead == maxSize - 1) + return bytesRead; + memcpy(data + bytesRead, "\r\n", 2); + bytesRead += 2; + readPointer += 2; + index++; + } + } + // check whether we need to return the final boundary + if (bytesRead < maxSize && index == multiPart->parts.count()) { + QByteArray finalBoundary = "--" + multiPart->boundary + "--"; + qint64 boundaryIndex = readPointer + finalBoundary.count() - size(); + qint64 lastBoundaryBytesRead = qMin(finalBoundary.count() - boundaryIndex, maxSize - bytesRead); + memcpy(data + bytesRead, finalBoundary.constData() + boundaryIndex, lastBoundaryBytesRead); + bytesRead += lastBoundaryBytesRead; + readPointer += lastBoundaryBytesRead; + } + return bytesRead; +} + +qint64 QHttpMultiPartIODevice::writeData(const char *data, qint64 maxSize) +{ + Q_UNUSED(data); + Q_UNUSED(maxSize); + return -1; +} + + +QT_END_NAMESPACE diff --git a/src/network/access/qhttpmultipart.h b/src/network/access/qhttpmultipart.h new file mode 100644 index 0000000..0a3342c --- /dev/null +++ b/src/network/access/qhttpmultipart.h @@ -0,0 +1,119 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QHTTPMULTIPART_H +#define QHTTPMULTIPART_H + +#include <QtCore/QSharedDataPointer> +#include <QtCore/QByteArray> +#include <QtNetwork/QNetworkRequest> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Network) + +class QHttpPartPrivate; +class QHttpMultiPart; + +class Q_NETWORK_EXPORT QHttpPart +{ +public: + QHttpPart(); + QHttpPart(const QHttpPart &other); + ~QHttpPart(); + QHttpPart &operator=(const QHttpPart &other); + bool operator==(const QHttpPart &other) const; + inline bool operator!=(const QHttpPart &other) const + { return !operator==(other); } + + void setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value); + void setRawHeader(const QByteArray &headerName, const QByteArray &headerValue); + + void setBody(const QByteArray &body); + void setBodyDevice(QIODevice *device); + +private: + QSharedDataPointer<QHttpPartPrivate> d; + + friend class QHttpMultiPartIODevice; +}; + +class QHttpMultiPartPrivate; + +class Q_NETWORK_EXPORT QHttpMultiPart : public QObject +{ + Q_OBJECT + +public: + + enum ContentType { + MixedType, + RelatedType, + FormDataType, + AlternativeType + }; + + QHttpMultiPart(QObject *parent = 0); + QHttpMultiPart(ContentType contentType, QObject *parent = 0); + ~QHttpMultiPart(); + + void append(const QHttpPart &httpPart); + + void setContentType(ContentType contentType); + + QByteArray boundary() const; + void setBoundary(const QByteArray &boundary); + +private: + Q_DECLARE_PRIVATE(QHttpMultiPart) + Q_DISABLE_COPY(QHttpMultiPart) + + friend class QNetworkAccessManager; + friend class QNetworkAccessManagerPrivate; +}; + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif // QHTTPMULTIPART_H diff --git a/src/network/access/qhttpmultipart_p.h b/src/network/access/qhttpmultipart_p.h new file mode 100644 index 0000000..7dc13e9 --- /dev/null +++ b/src/network/access/qhttpmultipart_p.h @@ -0,0 +1,182 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QHTTPMULTIPART_P_H +#define QHTTPMULTIPART_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of the Network Access API. This header file may change from +// version to version without notice, or even be removed. +// +// We mean it. +// + +#include "QtCore/qshareddata.h" +#include "qnetworkrequest_p.h" // for deriving QHttpPartPrivate from QNetworkHeadersPrivate +#include "private/qobject_p.h" + +QT_BEGIN_NAMESPACE + + +class QHttpPartPrivate: public QSharedData, public QNetworkHeadersPrivate +{ +public: + inline QHttpPartPrivate() : bodyDevice(0), headerCreated(false), readPointer(0) + { + } + ~QHttpPartPrivate() + { + } + + + QHttpPartPrivate(const QHttpPartPrivate &other) + : QSharedData(other), QNetworkHeadersPrivate(other), body(other.body), + header(other.header), headerCreated(other.headerCreated), readPointer(other.readPointer) + { + bodyDevice = other.bodyDevice; + } + + inline bool operator==(const QHttpPartPrivate &other) const + { + return rawHeaders == other.rawHeaders && body == other.body && + bodyDevice == other.bodyDevice && readPointer == other.readPointer; + } + + void setBodyDevice(QIODevice *device) { + bodyDevice = device; + readPointer = 0; + } + void setBody(const QByteArray &newBody) { + body = newBody; + readPointer = 0; + } + + // QIODevice-style methods called by QHttpMultiPartIODevice (but this class is + // not a QIODevice): + qint64 bytesAvailable() const; + qint64 readData(char *data, qint64 maxSize); + qint64 size() const; + bool reset(); + + QByteArray body; + QIODevice *bodyDevice; + +private: + void checkHeaderCreated() const; + + mutable QByteArray header; + mutable bool headerCreated; + qint64 readPointer; +}; + + + +class QHttpMultiPartPrivate; + +class Q_AUTOTEST_EXPORT QHttpMultiPartIODevice : public QIODevice +{ +public: + QHttpMultiPartIODevice(QHttpMultiPartPrivate *parentMultiPart) : + QIODevice(), multiPart(parentMultiPart), readPointer(0), deviceSize(-1) { + } + + ~QHttpMultiPartIODevice() { + } + + virtual bool atEnd() const { + return readPointer == size(); + } + + virtual qint64 bytesAvailable() const { + return size() - readPointer; + } + + virtual void close() { + readPointer = 0; + partOffsets.clear(); + deviceSize = -1; + QIODevice::close(); + } + + virtual qint64 bytesToWrite() const { + return 0; + } + + virtual qint64 size() const; + virtual bool isSequential() const; + virtual bool reset(); + virtual qint64 readData(char *data, qint64 maxSize); + virtual qint64 writeData(const char *data, qint64 maxSize); + + QHttpMultiPartPrivate *multiPart; + qint64 readPointer; + mutable QList<qint64> partOffsets; + mutable qint64 deviceSize; +}; + + + +class QHttpMultiPartPrivate: public QObjectPrivate +{ +public: + + QHttpMultiPartPrivate(); + + ~QHttpMultiPartPrivate() + { + delete device; + } + + QList<QHttpPart> parts; + QByteArray boundary; + QHttpMultiPart::ContentType contentType; + QHttpMultiPartIODevice *device; + +}; + +QT_END_NAMESPACE + + +#endif // QHTTPMULTIPART_P_H diff --git a/src/network/access/qhttpnetworkconnection.cpp b/src/network/access/qhttpnetworkconnection.cpp index 29ae5b0..07dd729 100644 --- a/src/network/access/qhttpnetworkconnection.cpp +++ b/src/network/access/qhttpnetworkconnection.cpp @@ -120,6 +120,10 @@ void QHttpNetworkConnectionPrivate::init() for (int i = 0; i < channelCount; i++) { channels[i].setConnection(this->q_func()); channels[i].ssl = encrypt; +#ifndef QT_NO_BEARERMANAGEMENT + //push session down to channels + channels[i].networkSession = networkSession; +#endif channels[i].init(); } } @@ -830,6 +834,23 @@ void QHttpNetworkConnectionPrivate::readMoreLater(QHttpNetworkReply *reply) } } +#ifndef QT_NO_BEARERMANAGEMENT +QHttpNetworkConnection::QHttpNetworkConnection(const QString &hostName, quint16 port, bool encrypt, QObject *parent, QSharedPointer<QNetworkSession> networkSession) + : QObject(*(new QHttpNetworkConnectionPrivate(hostName, port, encrypt)), parent) +{ + Q_D(QHttpNetworkConnection); + d->networkSession = networkSession; + d->init(); +} + +QHttpNetworkConnection::QHttpNetworkConnection(quint16 connectionCount, const QString &hostName, quint16 port, bool encrypt, QObject *parent, QSharedPointer<QNetworkSession> networkSession) + : QObject(*(new QHttpNetworkConnectionPrivate(connectionCount, hostName, port, encrypt)), parent) +{ + Q_D(QHttpNetworkConnection); + d->networkSession = networkSession; + d->init(); +} +#else QHttpNetworkConnection::QHttpNetworkConnection(const QString &hostName, quint16 port, bool encrypt, QObject *parent) : QObject(*(new QHttpNetworkConnectionPrivate(hostName, port, encrypt)), parent) { @@ -843,6 +864,7 @@ QHttpNetworkConnection::QHttpNetworkConnection(quint16 connectionCount, const QS Q_D(QHttpNetworkConnection); d->init(); } +#endif QHttpNetworkConnection::~QHttpNetworkConnection() { diff --git a/src/network/access/qhttpnetworkconnection_p.h b/src/network/access/qhttpnetworkconnection_p.h index 874ea22..adb779f4 100644 --- a/src/network/access/qhttpnetworkconnection_p.h +++ b/src/network/access/qhttpnetworkconnection_p.h @@ -55,6 +55,7 @@ #include <QtNetwork/qnetworkrequest.h> #include <QtNetwork/qnetworkreply.h> #include <QtNetwork/qabstractsocket.h> +#include <QtNetwork/qnetworksession.h> #include <private/qobject_p.h> #include <qauthenticator.h> @@ -88,8 +89,13 @@ class Q_AUTOTEST_EXPORT QHttpNetworkConnection : public QObject Q_OBJECT public: +#ifndef QT_NO_BEARERMANAGEMENT + QHttpNetworkConnection(const QString &hostName, quint16 port = 80, bool encrypt = false, QObject *parent = 0, QSharedPointer<QNetworkSession> networkSession = QSharedPointer<QNetworkSession>()); + QHttpNetworkConnection(quint16 channelCount, const QString &hostName, quint16 port = 80, bool encrypt = false, QObject *parent = 0, QSharedPointer<QNetworkSession> networkSession = QSharedPointer<QNetworkSession>()); +#else QHttpNetworkConnection(const QString &hostName, quint16 port = 80, bool encrypt = false, QObject *parent = 0); QHttpNetworkConnection(quint16 channelCount, const QString &hostName, quint16 port = 80, bool encrypt = false, QObject *parent = 0); +#endif ~QHttpNetworkConnection(); //The hostname to which this is connected to. @@ -208,6 +214,10 @@ public: QList<HttpMessagePair> highPriorityQueue; QList<HttpMessagePair> lowPriorityQueue; +#ifndef QT_NO_BEARERMANAGEMENT + QSharedPointer<QNetworkSession> networkSession; +#endif + friend class QHttpNetworkConnectionChannel; }; diff --git a/src/network/access/qhttpnetworkconnectionchannel.cpp b/src/network/access/qhttpnetworkconnectionchannel.cpp index 6564b4b..6fbc6f8 100644 --- a/src/network/access/qhttpnetworkconnectionchannel.cpp +++ b/src/network/access/qhttpnetworkconnectionchannel.cpp @@ -54,6 +54,10 @@ # include <QtNetwork/qsslconfiguration.h> #endif +#ifndef QT_NO_BEARERMANAGEMENT +#include "private/qnetworksession_p.h" +#endif + QT_BEGIN_NAMESPACE // TODO: Put channel specific stuff here so it does not polute qhttpnetworkconnection.cpp @@ -91,6 +95,11 @@ void QHttpNetworkConnectionChannel::init() #else socket = new QTcpSocket; #endif +#ifndef QT_NO_BEARERMANAGEMENT + //push session down to socket + if (networkSession) + socket->setProperty("_q_networksession", QVariant::fromValue(networkSession)); +#endif #ifndef QT_NO_NETWORKPROXY // Set by QNAM anyway, but let's be safe here socket->setProxy(QNetworkProxy::NoProxy); @@ -833,7 +842,10 @@ void QHttpNetworkConnectionChannel::handleStatus() bool QHttpNetworkConnectionChannel::resetUploadData() { - Q_ASSERT(reply); + if (!reply) { + //this happens if server closes connection while QHttpNetworkConnectionPrivate::_q_startNextRequest is pending + return false; + } QNonContiguousByteDevice* uploadByteDevice = request.uploadByteDevice(); if (!uploadByteDevice) return true; diff --git a/src/network/access/qhttpnetworkconnectionchannel_p.h b/src/network/access/qhttpnetworkconnectionchannel_p.h index 893d75e..f27c6f5 100644 --- a/src/network/access/qhttpnetworkconnectionchannel_p.h +++ b/src/network/access/qhttpnetworkconnectionchannel_p.h @@ -117,6 +117,9 @@ public: bool ignoreAllSslErrors; QList<QSslError> ignoreSslErrorsList; #endif +#ifndef QT_NO_BEARERMANAGEMENT + QSharedPointer<QNetworkSession> networkSession; +#endif // HTTP pipelining -> http://en.wikipedia.org/wiki/Http_pipelining enum PipeliningSupport { diff --git a/src/network/access/qhttpnetworkreply.cpp b/src/network/access/qhttpnetworkreply.cpp index 704cf3a..cb6c09f 100644 --- a/src/network/access/qhttpnetworkreply.cpp +++ b/src/network/access/qhttpnetworkreply.cpp @@ -886,8 +886,14 @@ bool QHttpNetworkReplyPrivate::expectContent() return false; if (request.operation() == QHttpNetworkRequest::Head) return !shouldEmitSignals(); - if (contentLength() == 0) + qint64 expectedContentLength = contentLength(); + if (expectedContentLength == 0) return false; + if (expectedContentLength == -1 && bodyLength == 0) { + // The content-length header was stripped, but its value was 0. + // This would be the case for an explicitly zero-length compressed response. + return false; + } return true; } diff --git a/src/network/access/qhttpnetworkrequest.cpp b/src/network/access/qhttpnetworkrequest.cpp index 8573364..2c67e87 100644 --- a/src/network/access/qhttpnetworkrequest.cpp +++ b/src/network/access/qhttpnetworkrequest.cpp @@ -158,8 +158,10 @@ QByteArray QHttpNetworkRequestPrivate::header(const QHttpNetworkRequest &request } if (request.d->operation == QHttpNetworkRequest::Post) { // add content type, if not set in the request - if (request.headerField("content-type").isEmpty()) - ba += "Content-Type: application/x-www-form-urlencoded\r\n"; + if (request.headerField("content-type").isEmpty()) { + qWarning("content-type missing in HTTP POST, defaulting to application/octet-stream"); + ba += "Content-Type: application/octet-stream\r\n"; + } if (!request.d->uploadByteDevice && request.d->url.hasQuery()) { QByteArray query = request.d->url.encodedQuery(); ba += "Content-Length: "; diff --git a/src/network/access/qhttpthreaddelegate.cpp b/src/network/access/qhttpthreaddelegate.cpp index 16fd9bb..0364dac 100644 --- a/src/network/access/qhttpthreaddelegate.cpp +++ b/src/network/access/qhttpthreaddelegate.cpp @@ -39,6 +39,7 @@ ** ****************************************************************************/ +//#define QHTTPTHREADDELEGATE_DEBUG #include "qhttpthreaddelegate_p.h" #include <QThread> @@ -50,6 +51,7 @@ #include "private/qnetworkaccesscache_p.h" #include "private/qnoncontiguousbytedevice_p.h" +#ifndef QT_NO_HTTP QT_BEGIN_NAMESPACE @@ -136,6 +138,8 @@ static QByteArray makeCacheKey(QUrl &url, QNetworkProxy *proxy) result = key.toEncoded(); } } +#else + Q_UNUSED(proxy) #endif return "http-connection:" + result; @@ -146,8 +150,13 @@ class QNetworkAccessCachedHttpConnection: public QHttpNetworkConnection, { // Q_OBJECT public: +#ifdef QT_NO_BEARERMANAGEMENT QNetworkAccessCachedHttpConnection(const QString &hostName, quint16 port, bool encrypt) : QHttpNetworkConnection(hostName, port, encrypt) +#else + QNetworkAccessCachedHttpConnection(const QString &hostName, quint16 port, bool encrypt, QSharedPointer<QNetworkSession> networkSession) + : QHttpNetworkConnection(hostName, port, encrypt, /*parent=*/0, networkSession) +#endif { setExpires(true); setShareable(true); @@ -195,12 +204,16 @@ QHttpThreadDelegate::QHttpThreadDelegate(QObject *parent) : , downloadBuffer(0) , httpConnection(0) , httpReply(0) + , synchronousRequestLoop(0) { } // This is invoked as BlockingQueuedConnection from QNetworkAccessHttpBackend in the user thread void QHttpThreadDelegate::startRequestSynchronously() { +#ifdef QHTTPTHREADDELEGATE_DEBUG + qDebug() << "QHttpThreadDelegate::startRequestSynchronously() thread=" << QThread::currentThreadId(); +#endif synchronous = true; QEventLoop synchronousRequestLoop; @@ -215,12 +228,18 @@ void QHttpThreadDelegate::startRequestSynchronously() connections.localData()->releaseEntry(cacheKey); connections.setLocalData(0); +#ifdef QHTTPTHREADDELEGATE_DEBUG + qDebug() << "QHttpThreadDelegate::startRequestSynchronously() thread=" << QThread::currentThreadId() << "finished"; +#endif } // This is invoked as QueuedConnection from QNetworkAccessHttpBackend in the user thread void QHttpThreadDelegate::startRequest() { +#ifdef QHTTPTHREADDELEGATE_DEBUG + qDebug() << "QHttpThreadDelegate::startRequest() thread=" << QThread::currentThreadId(); +#endif // Check QThreadStorage for the QNetworkAccessCache // If not there, create this connection cache if (!connections.hasLocalData()) { @@ -231,11 +250,13 @@ void QHttpThreadDelegate::startRequest() QUrl urlCopy = httpRequest.url(); urlCopy.setPort(urlCopy.port(ssl ? 443 : 80)); +#ifndef QT_NO_NETWORKPROXY if (transparentProxy.type() != QNetworkProxy::NoProxy) cacheKey = makeCacheKey(urlCopy, &transparentProxy); else if (cacheProxy.type() != QNetworkProxy::NoProxy) cacheKey = makeCacheKey(urlCopy, &cacheProxy); else +#endif cacheKey = makeCacheKey(urlCopy, 0); @@ -244,10 +265,14 @@ void QHttpThreadDelegate::startRequest() if (httpConnection == 0) { // no entry in cache; create an object // the http object is actually a QHttpNetworkConnection +#ifdef QT_NO_BEARERMANAGEMENT httpConnection = new QNetworkAccessCachedHttpConnection(urlCopy.host(), urlCopy.port(), ssl); +#else + httpConnection = new QNetworkAccessCachedHttpConnection(urlCopy.host(), urlCopy.port(), ssl, networkSession); +#endif #ifndef QT_NO_OPENSSL // Set the QSslConfiguration from this QNetworkRequest. - if (ssl) { + if (ssl && incomingSslConfiguration != QSslConfiguration::defaultConfiguration()) { httpConnection->setSslConfiguration(incomingSslConfiguration); } #endif @@ -305,15 +330,22 @@ void QHttpThreadDelegate::startRequest() // This gets called from the user thread or by the synchronous HTTP timeout timer void QHttpThreadDelegate::abortRequest() { +#ifdef QHTTPTHREADDELEGATE_DEBUG + qDebug() << "QHttpThreadDelegate::abortRequest() thread=" << QThread::currentThreadId() << "sync=" << synchronous; +#endif if (httpReply) { delete httpReply; httpReply = 0; - this->deleteLater(); } // Got aborted by the timeout timer - if (synchronous) + if (synchronous) { incomingErrorCode = QNetworkReply::TimeoutError; + QMetaObject::invokeMethod(synchronousRequestLoop, "quit", Qt::QueuedConnection); + } else { + //only delete this for asynchronous mode or QNetworkAccessHttpBackend will crash - see QNetworkAccessHttpBackend::postRequest() + this->deleteLater(); + } } void QHttpThreadDelegate::readyReadSlot() @@ -334,6 +366,9 @@ void QHttpThreadDelegate::finishedSlot() qWarning() << "QHttpThreadDelegate::finishedSlot: HTTP reply had already been deleted, internal problem. Please report."; return; } +#ifdef QHTTPTHREADDELEGATE_DEBUG + qDebug() << "QHttpThreadDelegate::finishedSlot() thread=" << QThread::currentThreadId() << "result=" << httpReply->statusCode(); +#endif // If there is still some data left emit that now while (httpReply->readAnyAvailable()) { @@ -363,6 +398,9 @@ void QHttpThreadDelegate::finishedSlot() void QHttpThreadDelegate::synchronousFinishedSlot() { +#ifdef QHTTPTHREADDELEGATE_DEBUG + qDebug() << "QHttpThreadDelegate::synchronousFinishedSlot() thread=" << QThread::currentThreadId() << "result=" << httpReply->statusCode(); +#endif if (httpReply->statusCode() >= 400) { // it's an error reply QString msg = QLatin1String(QT_TRANSLATE_NOOP("QNetworkReply", @@ -384,6 +422,9 @@ void QHttpThreadDelegate::finishedWithErrorSlot(QNetworkReply::NetworkError erro qWarning() << "QHttpThreadDelegate::finishedWithErrorSlot: HTTP reply had already been deleted, internal problem. Please report."; return; } +#ifdef QHTTPTHREADDELEGATE_DEBUG + qDebug() << "QHttpThreadDelegate::finishedWithErrorSlot() thread=" << QThread::currentThreadId() << "error=" << errorCode << detail; +#endif #ifndef QT_NO_OPENSSL if (ssl) @@ -401,6 +442,9 @@ void QHttpThreadDelegate::finishedWithErrorSlot(QNetworkReply::NetworkError erro void QHttpThreadDelegate::synchronousFinishedWithErrorSlot(QNetworkReply::NetworkError errorCode, const QString &detail) { +#ifdef QHTTPTHREADDELEGATE_DEBUG + qDebug() << "QHttpThreadDelegate::synchronousFinishedWithErrorSlot() thread=" << QThread::currentThreadId() << "error=" << errorCode << detail; +#endif incomingErrorCode = errorCode; incomingErrorDetail = detail; @@ -416,6 +460,10 @@ static void downloadBufferDeleter(char *ptr) void QHttpThreadDelegate::headerChangedSlot() { +#ifdef QHTTPTHREADDELEGATE_DEBUG + qDebug() << "QHttpThreadDelegate::headerChangedSlot() thread=" << QThread::currentThreadId(); +#endif + #ifndef QT_NO_OPENSSL if (ssl) emit sslConfigurationChanged(httpReply->sslConfiguration()); @@ -448,6 +496,9 @@ void QHttpThreadDelegate::headerChangedSlot() void QHttpThreadDelegate::synchronousHeaderChangedSlot() { +#ifdef QHTTPTHREADDELEGATE_DEBUG + qDebug() << "QHttpThreadDelegate::synchronousHeaderChangedSlot() thread=" << QThread::currentThreadId(); +#endif // Store the information we need in this object, the QNetworkAccessHttpBackend will later read it incomingHeaders = httpReply->header(); incomingStatusCode = httpReply->statusCode(); @@ -492,6 +543,9 @@ void QHttpThreadDelegate::sslErrorsSlot(const QList<QSslError> &errors) void QHttpThreadDelegate::synchronousAuthenticationRequiredSlot(const QHttpNetworkRequest &request, QAuthenticator *a) { Q_UNUSED(request); +#ifdef QHTTPTHREADDELEGATE_DEBUG + qDebug() << "QHttpThreadDelegate::synchronousAuthenticationRequiredSlot() thread=" << QThread::currentThreadId(); +#endif // Ask the credential cache QNetworkAuthenticationCredential credential = authenticationManager->fetchCachedCredentials(httpRequest.url(), a); @@ -507,6 +561,9 @@ void QHttpThreadDelegate::synchronousAuthenticationRequiredSlot(const QHttpNetwo #ifndef QT_NO_NETWORKPROXY void QHttpThreadDelegate::synchronousProxyAuthenticationRequiredSlot(const QNetworkProxy &p, QAuthenticator *a) { +#ifdef QHTTPTHREADDELEGATE_DEBUG + qDebug() << "QHttpThreadDelegate::synchronousProxyAuthenticationRequiredSlot() thread=" << QThread::currentThreadId(); +#endif // Ask the credential cache QNetworkAuthenticationCredential credential = authenticationManager->fetchCachedProxyCredentials(p, a); if (!credential.isNull()) { @@ -520,4 +577,6 @@ void QHttpThreadDelegate::synchronousProxyAuthenticationRequiredSlot(const QNet #endif +#endif // QT_NO_HTTP + QT_END_NAMESPACE diff --git a/src/network/access/qhttpthreaddelegate_p.h b/src/network/access/qhttpthreaddelegate_p.h index 3b598aa..c0f2077 100644 --- a/src/network/access/qhttpthreaddelegate_p.h +++ b/src/network/access/qhttpthreaddelegate_p.h @@ -68,6 +68,8 @@ #include "private/qnoncontiguousbytedevice_p.h" #include "qnetworkaccessauthenticationmanager_p.h" +#ifndef QT_NO_HTTP + QT_BEGIN_NAMESPACE class QAuthenticator; @@ -110,6 +112,9 @@ public: qint64 incomingContentLength; QNetworkReply::NetworkError incomingErrorCode; QString incomingErrorDetail; +#ifndef QT_NO_BEARERMANAGEMENT + QSharedPointer<QNetworkSession> networkSession; +#endif protected: // The zerocopy download buffer, if used: @@ -282,4 +287,6 @@ signals: QT_END_NAMESPACE +#endif // QT_NO_HTTP + #endif // QHTTPTHREADDELEGATE_H diff --git a/src/network/access/qnetworkaccessbackend.cpp b/src/network/access/qnetworkaccessbackend.cpp index 5aedac9..6220abe 100644 --- a/src/network/access/qnetworkaccessbackend.cpp +++ b/src/network/access/qnetworkaccessbackend.cpp @@ -46,7 +46,7 @@ #include "qnetworkreply_p.h" #include "QtCore/qhash.h" #include "QtCore/qmutex.h" -#include "QtNetwork/qnetworksession.h" +#include "QtNetwork/private/qnetworksession_p.h" #include "qnetworkaccesscachebackend_p.h" #include "qabstractnetworkcache.h" @@ -369,6 +369,8 @@ bool QNetworkAccessBackend::start() if (manager->networkSession->isOpen() && manager->networkSession->state() == QNetworkSession::Connected) { + //copy network session down to the backend + setProperty("_q_networksession", QVariant::fromValue(manager->networkSession)); open(); return true; } diff --git a/src/network/access/qnetworkaccesscachebackend.cpp b/src/network/access/qnetworkaccesscachebackend.cpp index 13f4cd9..c585848 100644 --- a/src/network/access/qnetworkaccesscachebackend.cpp +++ b/src/network/access/qnetworkaccesscachebackend.cpp @@ -66,6 +66,7 @@ void QNetworkAccessCacheBackend::open() QString msg = QCoreApplication::translate("QNetworkAccessCacheBackend", "Error opening %1") .arg(this->url().toString()); error(QNetworkReply::ContentNotFoundError, msg); + } else { setAttribute(QNetworkRequest::SourceIsFromCacheAttribute, true); } finished(); @@ -85,14 +86,18 @@ bool QNetworkAccessCacheBackend::sendCacheContents() QNetworkCacheMetaData::AttributesMap attributes = item.attributes(); setAttribute(QNetworkRequest::HttpStatusCodeAttribute, attributes.value(QNetworkRequest::HttpStatusCodeAttribute)); setAttribute(QNetworkRequest::HttpReasonPhraseAttribute, attributes.value(QNetworkRequest::HttpReasonPhraseAttribute)); - setAttribute(QNetworkRequest::SourceIsFromCacheAttribute, true); // set the raw headers QNetworkCacheMetaData::RawHeaderList rawHeaders = item.rawHeaders(); QNetworkCacheMetaData::RawHeaderList::ConstIterator it = rawHeaders.constBegin(), end = rawHeaders.constEnd(); - for ( ; it != end; ++it) + for ( ; it != end; ++it) { + if (it->first.toLower() == "cache-control" && + it->second.toLower().contains("must-revalidate")) { + return false; + } setRawHeader(it->first, it->second); + } // handle a possible redirect QVariant redirectionTarget = attributes.value(QNetworkRequest::RedirectionTargetAttribute); diff --git a/src/network/access/qnetworkaccessftpbackend.cpp b/src/network/access/qnetworkaccessftpbackend.cpp index e34e6bb..3ad1961 100644 --- a/src/network/access/qnetworkaccessftpbackend.cpp +++ b/src/network/access/qnetworkaccessftpbackend.cpp @@ -153,6 +153,10 @@ void QNetworkAccessFtpBackend::open() if (!objectCache->requestEntry(cacheKey, this, SLOT(ftpConnectionReady(QNetworkAccessCache::CacheableObject*)))) { ftp = new QNetworkAccessCachedFtpConnection; +#ifndef QT_NO_BEARERMANAGEMENT + //copy network session down to the QFtp + ftp->setProperty("_q_networksession", property("_q_networksession")); +#endif #ifndef QT_NO_NETWORKPROXY if (proxy.type() == QNetworkProxy::FtpCachingProxy) ftp->setProxy(proxy.hostName(), proxy.port()); diff --git a/src/network/access/qnetworkaccesshttpbackend.cpp b/src/network/access/qnetworkaccesshttpbackend.cpp index cc1248b..a45c2de 100644 --- a/src/network/access/qnetworkaccesshttpbackend.cpp +++ b/src/network/access/qnetworkaccesshttpbackend.cpp @@ -47,6 +47,7 @@ #include "qabstractnetworkcache.h" #include "qnetworkrequest.h" #include "qnetworkreply.h" +#include "QtNetwork/private/qnetworksession_p.h" #include "qnetworkrequest_p.h" #include "qnetworkcookie_p.h" #include "QtCore/qdatetime.h" @@ -219,7 +220,7 @@ QNetworkAccessHttpBackend::~QNetworkAccessHttpBackend() 2) If we have a cache entry for this url populate headers so the server can return 304 3) Calculate if response_is_fresh and if so send the cache and set loadedFromCache to true */ -void QNetworkAccessHttpBackend::validateCache(QHttpNetworkRequest &httpRequest, bool &loadedFromCache) +bool QNetworkAccessHttpBackend::loadFromCacheIfAllowed(QHttpNetworkRequest &httpRequest) { QNetworkRequest::CacheLoadControl CacheLoadControlAttribute = (QNetworkRequest::CacheLoadControl)request().attribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork).toInt(); @@ -230,24 +231,24 @@ void QNetworkAccessHttpBackend::validateCache(QHttpNetworkRequest &httpRequest, httpRequest.setHeaderField("Cache-Control", "no-cache"); httpRequest.setHeaderField("Pragma", "no-cache"); } - return; + return false; } // The disk cache API does not currently support partial content retrieval. // That is why we don't use the disk cache for any such requests. if (request().hasRawHeader("Range")) - return; + return false; QAbstractNetworkCache *nc = networkCache(); if (!nc) - return; // no local cache + return false; // no local cache QNetworkCacheMetaData metaData = nc->metaData(url()); if (!metaData.isValid()) - return; // not in cache + return false; // not in cache if (!metaData.saveToDisk()) - return; + return false; QNetworkHeadersPrivate cacheHeaders; QNetworkHeadersPrivate::RawHeadersList::ConstIterator it; @@ -261,13 +262,11 @@ void QNetworkAccessHttpBackend::validateCache(QHttpNetworkRequest &httpRequest, if (lastModified.isValid()) httpRequest.setHeaderField("If-Modified-Since", QNetworkHeadersPrivate::toHttpDate(lastModified)); - if (CacheLoadControlAttribute == QNetworkRequest::PreferNetwork) { - it = cacheHeaders.findRawHeader("Cache-Control"); - if (it != cacheHeaders.rawHeaders.constEnd()) { - QHash<QByteArray, QByteArray> cacheControl = parseHttpOptionHeader(it->second); - if (cacheControl.contains("must-revalidate")) - return; - } + it = cacheHeaders.findRawHeader("Cache-Control"); + if (it != cacheHeaders.rawHeaders.constEnd()) { + QHash<QByteArray, QByteArray> cacheControl = parseHttpOptionHeader(it->second); + if (cacheControl.contains("must-revalidate")) + return false; } QDateTime currentDateTime = QDateTime::currentDateTime(); @@ -338,14 +337,12 @@ void QNetworkAccessHttpBackend::validateCache(QHttpNetworkRequest &httpRequest, #endif if (!response_is_fresh) - return; + return false; - loadedFromCache = true; #if defined(QNETWORKACCESSHTTPBACKEND_DEBUG) qDebug() << "response_is_fresh" << CacheLoadControlAttribute; #endif - if (!sendCacheContents(metaData)) - loadedFromCache = false; + return sendCacheContents(metaData); } static QHttpNetworkRequest::Priority convert(const QNetworkRequest::Priority& prio) @@ -442,12 +439,12 @@ void QNetworkAccessHttpBackend::postRequest() switch (operation()) { case QNetworkAccessManager::GetOperation: httpRequest.setOperation(QHttpNetworkRequest::Get); - validateCache(httpRequest, loadedFromCache); + loadedFromCache = loadFromCacheIfAllowed(httpRequest); break; case QNetworkAccessManager::HeadOperation: httpRequest.setOperation(QHttpNetworkRequest::Head); - validateCache(httpRequest, loadedFromCache); + loadedFromCache = loadFromCacheIfAllowed(httpRequest); break; case QNetworkAccessManager::PostOperation: @@ -479,6 +476,13 @@ void QNetworkAccessHttpBackend::postRequest() break; // can't happen } + if (loadedFromCache) { + // commented this out since it will be called later anyway + // by copyFinished() + //QNetworkAccessBackend::finished(); + return; // no need to send the request! :) + } + QList<QByteArray> headers = request().rawHeaderList(); if (resumeOffset != 0) { if (headers.contains("Range")) { @@ -506,13 +510,6 @@ void QNetworkAccessHttpBackend::postRequest() foreach (const QByteArray &header, headers) httpRequest.setHeaderField(header, request().rawHeader(header)); - if (loadedFromCache) { - // commented this out since it will be called later anyway - // by copyFinished() - //QNetworkAccessBackend::finished(); - return; // no need to send the request! :) - } - if (request().attribute(QNetworkRequest::HttpPipeliningAllowedAttribute).toBool() == true) httpRequest.setPipeliningAllowed(true); @@ -524,6 +521,11 @@ void QNetworkAccessHttpBackend::postRequest() // Create the HTTP thread delegate QHttpThreadDelegate *delegate = new QHttpThreadDelegate; +#ifndef Q_NO_BEARERMANAGEMENT + QVariant v(property("_q_networksession")); + if (v.isValid()) + delegate->networkSession = qvariant_cast<QSharedPointer<QNetworkSession> >(v); +#endif // For the synchronous HTTP, this is the normal way the delegate gets deleted // For the asynchronous HTTP this is a safety measure, the delegate deletes itself when HTTP is finished @@ -716,8 +718,13 @@ void QNetworkAccessHttpBackend::replyDownloadData(QByteArray d) pendingDownloadData.append(d); d.clear(); - writeDownstreamData(pendingDownloadData); + // We need to usa a copy for calling writeDownstreamData as we could + // possibly recurse into this this function when we call + // appendDownstreamDataSignalEmissions because the user might call + // processEvents() or spin an event loop when this occur. + QByteDataBuffer pendingDownloadDataCopy = pendingDownloadData; pendingDownloadData.clear(); + writeDownstreamData(pendingDownloadDataCopy); } void QNetworkAccessHttpBackend::replyFinished() diff --git a/src/network/access/qnetworkaccesshttpbackend_p.h b/src/network/access/qnetworkaccesshttpbackend_p.h index 712dd2f..4778bd0 100644 --- a/src/network/access/qnetworkaccesshttpbackend_p.h +++ b/src/network/access/qnetworkaccesshttpbackend_p.h @@ -148,7 +148,7 @@ private: quint64 resumeOffset; - void validateCache(QHttpNetworkRequest &httpRequest, bool &loadedFromCache); + bool loadFromCacheIfAllowed(QHttpNetworkRequest &httpRequest); void invalidateCache(); void postRequest(); void readFromHttp(); diff --git a/src/network/access/qnetworkaccessmanager.cpp b/src/network/access/qnetworkaccessmanager.cpp index 7e75045..0d7b52a 100644 --- a/src/network/access/qnetworkaccessmanager.cpp +++ b/src/network/access/qnetworkaccessmanager.cpp @@ -64,6 +64,8 @@ #include "QtNetwork/qauthenticator.h" #include "QtNetwork/qsslconfiguration.h" #include "QtNetwork/qnetworkconfigmanager.h" +#include "QtNetwork/qhttpmultipart.h" +#include "qhttpmultipart_p.h" #include "qthread.h" @@ -629,6 +631,46 @@ QNetworkReply *QNetworkAccessManager::post(const QNetworkRequest &request, const } /*! + \since 4.8 + + \overload + + Sends the contents of the \a multiPart message to the destination + specified by \a request. + + This can be used for sending MIME multipart messages over HTTP. + + \sa QHttpMultiPart, QHttpPart, put() +*/ +QNetworkReply *QNetworkAccessManager::post(const QNetworkRequest &request, QHttpMultiPart *multiPart) +{ + QNetworkRequest newRequest = d_func()->prepareMultipart(request, multiPart); + QIODevice *device = multiPart->d_func()->device; + QNetworkReply *reply = post(newRequest, device); + return reply; +} + +/*! + \since 4.8 + + \overload + + Sends the contents of the \a multiPart message to the destination + specified by \a request. + + This can be used for sending MIME multipart messages over HTTP. + + \sa QHttpMultiPart, QHttpPart, post() +*/ +QNetworkReply *QNetworkAccessManager::put(const QNetworkRequest &request, QHttpMultiPart *multiPart) +{ + QNetworkRequest newRequest = d_func()->prepareMultipart(request, multiPart); + QIODevice *device = multiPart->d_func()->device; + QNetworkReply *reply = put(newRequest, device); + return reply; +} + +/*! Uploads the contents of \a data to the destination \a request and returnes a new QNetworkReply object that will be open for reply. @@ -654,7 +696,8 @@ QNetworkReply *QNetworkAccessManager::put(const QNetworkRequest &request, QIODev /*! \overload - Sends the contents of the \a data byte array to the destination + + Sends the contents of the \a data byte array to the destination specified by \a request. */ QNetworkReply *QNetworkAccessManager::put(const QNetworkRequest &request, const QByteArray &data) @@ -818,7 +861,7 @@ QNetworkAccessManager::NetworkAccessibility QNetworkAccessManager::networkAccess device will be uploaded to the server; in that case, data must be open for reading and must remain valid until the finished() signal is emitted for this reply. - \note This feature is currently available for HTTP only. + \note This feature is currently available for HTTP(S) only. \sa get(), post(), put(), deleteResource() */ @@ -1125,8 +1168,25 @@ void QNetworkAccessManagerPrivate::createSession(const QNetworkConfiguration &co initializeSession = false; - if (!config.isValid()) { - networkSession.clear(); + QSharedPointer<QNetworkSession> newSession; + if (config.isValid()) + newSession = QSharedNetworkSessionManager::getSession(config); + + if (networkSession) { + //do nothing if new and old session are the same + if (networkSession == newSession) + return; + //disconnect from old session + QObject::disconnect(networkSession.data(), SIGNAL(opened()), q, SIGNAL(networkSessionConnected())); + QObject::disconnect(networkSession.data(), SIGNAL(closed()), q, SLOT(_q_networkSessionClosed())); + QObject::disconnect(networkSession.data(), SIGNAL(stateChanged(QNetworkSession::State)), + q, SLOT(_q_networkSessionStateChanged(QNetworkSession::State))); + } + + //switch to new session (null if config was invalid) + networkSession = newSession; + + if (!networkSession) { online = false; if (networkAccessible == QNetworkAccessManager::NotAccessible) @@ -1137,8 +1197,7 @@ void QNetworkAccessManagerPrivate::createSession(const QNetworkConfiguration &co return; } - networkSession = QSharedNetworkSessionManager::getSession(config); - + //connect to new session QObject::connect(networkSession.data(), SIGNAL(opened()), q, SIGNAL(networkSessionConnected()), Qt::QueuedConnection); //QueuedConnection is used to avoid deleting the networkSession inside its closed signal QObject::connect(networkSession.data(), SIGNAL(closed()), q, SLOT(_q_networkSessionClosed()), Qt::QueuedConnection); @@ -1150,9 +1209,15 @@ void QNetworkAccessManagerPrivate::createSession(const QNetworkConfiguration &co void QNetworkAccessManagerPrivate::_q_networkSessionClosed() { + Q_Q(QNetworkAccessManager); if (networkSession) { networkConfiguration = networkSession->configuration().identifier(); + //disconnect from old session + QObject::disconnect(networkSession.data(), SIGNAL(opened()), q, SIGNAL(networkSessionConnected())); + QObject::disconnect(networkSession.data(), SIGNAL(closed()), q, SLOT(_q_networkSessionClosed())); + QObject::disconnect(networkSession.data(), SIGNAL(stateChanged(QNetworkSession::State)), + q, SLOT(_q_networkSessionStateChanged(QNetworkSession::State))); networkSession.clear(); } } @@ -1161,8 +1226,12 @@ void QNetworkAccessManagerPrivate::_q_networkSessionStateChanged(QNetworkSession { Q_Q(QNetworkAccessManager); - if (state == QNetworkSession::Connected) + //Do not emit the networkSessionConnected signal here, except for roaming -> connected + //transition, otherwise it is emitted twice in a row when opening a connection. + if (state == QNetworkSession::Connected && lastSessionState == QNetworkSession::Roaming) emit q->networkSessionConnected(); + lastSessionState = state; + if (online) { if (state != QNetworkSession::Connected && state != QNetworkSession::Roaming) { online = false; @@ -1177,6 +1246,54 @@ void QNetworkAccessManagerPrivate::_q_networkSessionStateChanged(QNetworkSession } #endif // QT_NO_BEARERMANAGEMENT +QNetworkRequest QNetworkAccessManagerPrivate::prepareMultipart(const QNetworkRequest &request, QHttpMultiPart *multiPart) +{ + // copy the request, we probably need to add some headers + QNetworkRequest newRequest(request); + + // add Content-Type header if not there already + if (!request.header(QNetworkRequest::ContentTypeHeader).isValid()) { + QByteArray contentType; + contentType.reserve(34 + multiPart->d_func()->boundary.count()); + contentType += "multipart/"; + switch (multiPart->d_func()->contentType) { + case QHttpMultiPart::RelatedType: + contentType += "related"; + break; + case QHttpMultiPart::FormDataType: + contentType += "form-data"; + break; + case QHttpMultiPart::AlternativeType: + contentType += "alternative"; + break; + default: + contentType += "mixed"; + break; + } + // putting the boundary into quotes, recommended in RFC 2046 section 5.1.1 + contentType += "; boundary=\"" + multiPart->d_func()->boundary + "\""; + newRequest.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(contentType)); + } + + // add MIME-Version header if not there already (we must include the header + // if the message conforms to RFC 2045, see section 4 of that RFC) + QByteArray mimeHeader("MIME-Version"); + if (!request.hasRawHeader(mimeHeader)) + newRequest.setRawHeader(mimeHeader, QByteArray("1.0")); + + QIODevice *device = multiPart->d_func()->device; + if (!device->isReadable()) { + if (!device->isOpen()) { + if (!device->open(QIODevice::ReadOnly)) + qWarning("could not open device for reading"); + } else { + qWarning("device is not readable"); + } + } + + return newRequest; +} + QT_END_NAMESPACE #include "moc_qnetworkaccessmanager.cpp" diff --git a/src/network/access/qnetworkaccessmanager.h b/src/network/access/qnetworkaccessmanager.h index d67b8ac..47760b2 100644 --- a/src/network/access/qnetworkaccessmanager.h +++ b/src/network/access/qnetworkaccessmanager.h @@ -65,6 +65,7 @@ class QSslError; #if !defined(QT_NO_BEARERMANAGEMENT) && !defined(QT_MOBILITY_BEARER) class QNetworkConfiguration; #endif +class QHttpMultiPart; class QNetworkReplyImplPrivate; class QNetworkAccessManagerPrivate; @@ -116,8 +117,10 @@ public: QNetworkReply *get(const QNetworkRequest &request); QNetworkReply *post(const QNetworkRequest &request, QIODevice *data); QNetworkReply *post(const QNetworkRequest &request, const QByteArray &data); + QNetworkReply *post(const QNetworkRequest &request, QHttpMultiPart *multiPart); QNetworkReply *put(const QNetworkRequest &request, QIODevice *data); QNetworkReply *put(const QNetworkRequest &request, const QByteArray &data); + QNetworkReply *put(const QNetworkRequest &request, QHttpMultiPart *multiPart); QNetworkReply *deleteResource(const QNetworkRequest &request); QNetworkReply *sendCustomRequest(const QNetworkRequest &request, const QByteArray &verb, QIODevice *data = 0); diff --git a/src/network/access/qnetworkaccessmanager_p.h b/src/network/access/qnetworkaccessmanager_p.h index ee6ad70..f64cc4d 100644 --- a/src/network/access/qnetworkaccessmanager_p.h +++ b/src/network/access/qnetworkaccessmanager_p.h @@ -79,6 +79,7 @@ public: #endif #ifndef QT_NO_BEARERMANAGEMENT networkSession(0), + lastSessionState(QNetworkSession::Invalid), networkAccessible(QNetworkAccessManager::Accessible), online(false), initializeSession(true), @@ -119,6 +120,8 @@ public: void _q_networkSessionStateChanged(QNetworkSession::State state); #endif + QNetworkRequest prepareMultipart(const QNetworkRequest &request, QHttpMultiPart *multiPart); + // this is the cache for storing downloaded files QAbstractNetworkCache *networkCache; @@ -134,6 +137,7 @@ public: #ifndef QT_NO_BEARERMANAGEMENT QSharedPointer<QNetworkSession> networkSession; + QNetworkSession::State lastSessionState; QString networkConfiguration; QNetworkAccessManager::NetworkAccessibility networkAccessible; bool online; diff --git a/src/network/access/qnetworkcookie.cpp b/src/network/access/qnetworkcookie.cpp index c2a6925..eec8507 100644 --- a/src/network/access/qnetworkcookie.cpp +++ b/src/network/access/qnetworkcookie.cpp @@ -395,8 +395,8 @@ static QPair<QByteArray, QByteArray> nextField(const QByteArray &text, int &posi // qdtext = <any TEXT except <">> // quoted-pair = "\" CHAR - // If its NAME=VALUE, retain the value as is - // refer to ttp://bugreports.qt.nokia.com/browse/QTBUG-17746 + // If it is NAME=VALUE, retain the value as is + // refer to http://bugreports.qt.nokia.com/browse/QTBUG-17746 if (isNameValue) second += '"'; ++i; @@ -432,7 +432,9 @@ static QPair<QByteArray, QByteArray> nextField(const QByteArray &text, int &posi position = i; for ( ; i < length; ++i) { register char c = text.at(i); - if (c == ',' || c == ';' || isLWS(c)) + // for name value pairs, we want to parse until reaching the next ';' + // and not break when reaching a space char + if (c == ',' || c == ';' || ((isNameValue && (c == '\n' || c == '\r')) || (!isNameValue && isLWS(c)))) break; } @@ -487,7 +489,6 @@ QByteArray QNetworkCookie::toRawForm(RawForm form) const result += '='; if ((d->value.contains(';') || d->value.contains(',') || - d->value.contains(' ') || d->value.contains('"')) && (!d->value.startsWith('"') && !d->value.endsWith('"'))) { @@ -737,7 +738,7 @@ static QDateTime parseDateString(const QByteArray &dateString) // 4 digit Year if (isNum && year == -1 - && dateString.length() >= at + 3) { + && dateString.length() > at + 3) { if (isNumber(dateString[at + 1]) && isNumber(dateString[at + 2]) && isNumber(dateString[at + 3])) { diff --git a/src/network/access/qnetworkdiskcache.cpp b/src/network/access/qnetworkdiskcache.cpp index 2040b01..271494d 100644 --- a/src/network/access/qnetworkdiskcache.cpp +++ b/src/network/access/qnetworkdiskcache.cpp @@ -50,13 +50,15 @@ #include <qdir.h> #include <qdatetime.h> #include <qdiriterator.h> -#include <qcryptographichash.h> #include <qurl.h> - +#include <qcryptographichash.h> #include <qdebug.h> -#define CACHE_PREFIX QLatin1String("cache_") -#define CACHE_POSTFIX QLatin1String(".cache") +#define CACHE_POSTFIX QLatin1String(".d") +#define PREPARED_SLASH QLatin1String("prepared/") +#define CACHE_VERSION 7 +#define DATA_DIR QLatin1String("data") + #define MAX_COMPRESSION_SIZE (1024 * 1024 * 3) #ifndef QT_NO_NETWORKDISKCACHE @@ -153,6 +155,9 @@ void QNetworkDiskCache::setCacheDirectory(const QString &cacheDir) d->cacheDirectory = dir.absolutePath(); if (!d->cacheDirectory.endsWith(QLatin1Char('/'))) d->cacheDirectory += QLatin1Char('/'); + + d->dataDirectory = d->cacheDirectory + DATA_DIR + QString::number(CACHE_VERSION) + QLatin1Char('/'); + d->prepareLayout(); } /*! @@ -244,6 +249,26 @@ void QNetworkDiskCache::insert(QIODevice *device) d->inserting.erase(it); } + +/*! + Create subdirectories and other housekeeping on the filesystem. + Prevents too many files from being present in any single directory. +*/ +void QNetworkDiskCachePrivate::prepareLayout() +{ + QDir helper; + helper.mkpath(cacheDirectory + PREPARED_SLASH); + + //Create directory and subdirectories 0-F + helper.mkpath(dataDirectory); + for (uint i = 0; i < 16 ; i++) { + QString str = QString::number(i, 16); + QString subdir = dataDirectory + str; + helper.mkdir(subdir); + } +} + + void QNetworkDiskCachePrivate::storeItem(QCacheItem *cacheItem) { Q_Q(QNetworkDiskCache); @@ -324,7 +349,7 @@ bool QNetworkDiskCachePrivate::removeFile(const QString &file) return false; QFileInfo info(file); QString fileName = info.fileName(); - if (!fileName.endsWith(CACHE_POSTFIX) || !fileName.startsWith(CACHE_PREFIX)) + if (!fileName.endsWith(CACHE_POSTFIX)) return false; qint64 size = info.size(); if (QFile::remove(file)) { @@ -508,6 +533,9 @@ qint64 QNetworkDiskCache::expire() return 0; } + // close file handle to prevent "in use" error when QFile::remove() is called + d->lastItem.reset(); + QDir::Filters filters = QDir::AllDirs | QDir:: Files | QDir::NoDotAndDotDot; QDirIterator it(cacheDirectory(), filters, QDirIterator::Subdirectories); @@ -517,7 +545,7 @@ qint64 QNetworkDiskCache::expire() QString path = it.next(); QFileInfo info = it.fileInfo(); QString fileName = info.fileName(); - if (fileName.endsWith(CACHE_POSTFIX) && fileName.startsWith(CACHE_PREFIX)) { + if (fileName.endsWith(CACHE_POSTFIX)) { cacheItems.insert(info.created(), path); totalSize += info.size(); } @@ -544,8 +572,6 @@ qint64 QNetworkDiskCache::expire() << "Kept:" << cacheItems.count() - removedFiles; } #endif - if (removedFiles > 0) - d->lastItem.reset(); return totalSize; } @@ -564,7 +590,10 @@ void QNetworkDiskCache::clear() d->maximumCacheSize = size; } -QByteArray QNetworkDiskCachePrivate::generateId(const QUrl &url) const +/*! + Given a URL, generates a unique enough filename (and subdirectory) + */ +QString QNetworkDiskCachePrivate::uniqueFileName(const QUrl &url) { QUrl cleanUrl = url; cleanUrl.setPassword(QString()); @@ -572,29 +601,32 @@ QByteArray QNetworkDiskCachePrivate::generateId(const QUrl &url) const QCryptographicHash hash(QCryptographicHash::Sha1); hash.addData(cleanUrl.toEncoded()); - return hash.result().toHex(); + // convert sha1 to base36 form and return first 8 bytes for use as string + QByteArray id = QByteArray::number(*(qlonglong*)hash.result().data(), 36).left(8); + // generates <one-char subdir>/<8-char filname.d> + uint code = (uint)id.at(id.length()-1) % 16; + QString pathFragment = QString::number(code, 16) + QLatin1Char('/') + + QLatin1String(id) + CACHE_POSTFIX; + + return pathFragment; } QString QNetworkDiskCachePrivate::tmpCacheFileName() const { - QDir dir; - dir.mkpath(cacheDirectory + QLatin1String("prepared/")); - return cacheDirectory + QLatin1String("prepared/") + CACHE_PREFIX + QLatin1String("XXXXXX") + CACHE_POSTFIX; + //The subdirectory is presumed to be already read for use. + return cacheDirectory + PREPARED_SLASH + QLatin1String("XXXXXX") + CACHE_POSTFIX; } +/*! + Generates fully qualified path of cached resource from a URL. + */ QString QNetworkDiskCachePrivate::cacheFileName(const QUrl &url) const { if (!url.isValid()) return QString(); - QString directory = cacheDirectory + url.scheme() + QLatin1Char('/'); - if (!QFile::exists(directory)) { - // ### make a static QDir function for this... - QDir dir; - dir.mkpath(directory); - } - QString fileName = CACHE_PREFIX + QLatin1String(generateId(url)) + CACHE_POSTFIX; - return directory + fileName; + QString fullpath = dataDirectory + uniqueFileName(url); + return fullpath; } /*! @@ -631,7 +663,7 @@ bool QCacheItem::canCompress() const enum { CacheMagic = 0xe8, - CurrentCacheVersion = 7 + CurrentCacheVersion = CACHE_VERSION }; void QCacheItem::writeHeader(QFile *device) const @@ -682,6 +714,12 @@ bool QCacheItem::read(QFile *device, bool readData) data.setData(qUncompress(dataBA)); data.open(QBuffer::ReadOnly); } + + // quick and dirty check if metadata's URL field and the file's name are in synch + QString expectedFilename = QNetworkDiskCachePrivate::uniqueFileName(metaData.url()); + if (!device->fileName().endsWith(expectedFilename)) + return false; + return metaData.isValid(); } diff --git a/src/network/access/qnetworkdiskcache_p.h b/src/network/access/qnetworkdiskcache_p.h index 8659066..13db04f 100644 --- a/src/network/access/qnetworkdiskcache_p.h +++ b/src/network/access/qnetworkdiskcache_p.h @@ -104,14 +104,17 @@ public: , currentCacheSize(-1) {} - QByteArray generateId(const QUrl &url) const; + static QString uniqueFileName(const QUrl &url); QString cacheFileName(const QUrl &url) const; QString tmpCacheFileName() const; bool removeFile(const QString &file); void storeItem(QCacheItem *item); + void prepareLayout(); + static quint32 crc32(const char *data, uint len); mutable QCacheItem lastItem; QString cacheDirectory; + QString dataDirectory; qint64 maximumCacheSize; qint64 currentCacheSize; diff --git a/src/network/access/qnetworkreplyimpl.cpp b/src/network/access/qnetworkreplyimpl.cpp index 1c9fa3e..9eb505d 100644 --- a/src/network/access/qnetworkreplyimpl.cpp +++ b/src/network/access/qnetworkreplyimpl.cpp @@ -74,7 +74,7 @@ inline QNetworkReplyImplPrivate::QNetworkReplyImplPrivate() void QNetworkReplyImplPrivate::_q_startOperation() { // ensure this function is only being called once - if (state == Working) { + if (state == Working || state == Finished) { qDebug("QNetworkReplyImpl::_q_startOperation was called more than once"); return; } @@ -178,9 +178,11 @@ void QNetworkReplyImplPrivate::_q_copyReadyRead() if (preMigrationDownloaded != Q_INT64_C(-1)) totalSize = totalSize.toLongLong() + preMigrationDownloaded; pauseNotificationHandling(); + // emit readyRead before downloadProgress incase this will cause events to be + // processed and we get into a recursive call (as in QProgressDialog). + emit q->readyRead(); emit q->downloadProgress(bytesDownloaded, totalSize.isNull() ? Q_INT64_C(-1) : totalSize.toLongLong()); - emit q->readyRead(); resumeNotificationHandling(); } @@ -586,11 +588,13 @@ void QNetworkReplyImplPrivate::appendDownstreamDataSignalEmissions() if (preMigrationDownloaded != Q_INT64_C(-1)) totalSize = totalSize.toLongLong() + preMigrationDownloaded; pauseNotificationHandling(); - emit q->downloadProgress(bytesDownloaded, - totalSize.isNull() ? Q_INT64_C(-1) : totalSize.toLongLong()); // 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 + // processed and we get into a recursive call (as in QProgressDialog). + emit q->downloadProgress(bytesDownloaded, + totalSize.isNull() ? Q_INT64_C(-1) : totalSize.toLongLong()); resumeNotificationHandling(); // do we still have room in the buffer? @@ -691,10 +695,12 @@ void QNetworkReplyImplPrivate::appendDownstreamDataDownloadBuffer(qint64 bytesRe downloadBufferCurrentSize = bytesReceived; - emit q->downloadProgress(bytesDownloaded, bytesTotal); // Only emit readyRead when actual data is there + // emit readyRead before downloadProgress incase this will cause events to be + // processed and we get into a recursive call (as in QProgressDialog). if (bytesDownloaded > 0) emit q->readyRead(); + emit q->downloadProgress(bytesDownloaded, bytesTotal); } void QNetworkReplyImplPrivate::finished() diff --git a/src/network/access/qnetworkrequest.cpp b/src/network/access/qnetworkrequest.cpp index a48a26f..338969a 100644 --- a/src/network/access/qnetworkrequest.cpp +++ b/src/network/access/qnetworkrequest.cpp @@ -524,7 +524,7 @@ void QNetworkRequest::setAttribute(Attribute code, const QVariant &value) QSslConfiguration QNetworkRequest::sslConfiguration() const { if (!d->sslConfiguration) - d->sslConfiguration = new QSslConfiguration; + d->sslConfiguration = new QSslConfiguration(QSslConfiguration::defaultConfiguration()); return *d->sslConfiguration; } @@ -639,6 +639,9 @@ static QByteArray headerName(QNetworkRequest::KnownHeaders header) case QNetworkRequest::SetCookieHeader: return "Set-Cookie"; + case QNetworkRequest::ContentDispositionHeader: + return "Content-Disposition"; + // no default: // if new values are added, this will generate a compiler warning } @@ -651,6 +654,7 @@ static QByteArray headerValue(QNetworkRequest::KnownHeaders header, const QVaria switch (header) { case QNetworkRequest::ContentTypeHeader: case QNetworkRequest::ContentLengthHeader: + case QNetworkRequest::ContentDispositionHeader: return value.toByteArray(); case QNetworkRequest::LocationHeader: @@ -812,6 +816,11 @@ QNetworkHeadersPrivate::findRawHeader(const QByteArray &key) const return end; // not found } +QNetworkHeadersPrivate::RawHeadersList QNetworkHeadersPrivate::allRawHeaders() const +{ + return rawHeaders; +} + QList<QByteArray> QNetworkHeadersPrivate::rawHeadersKeys() const { QList<QByteArray> result; diff --git a/src/network/access/qnetworkrequest.h b/src/network/access/qnetworkrequest.h index b5ef109..d3bbba7 100644 --- a/src/network/access/qnetworkrequest.h +++ b/src/network/access/qnetworkrequest.h @@ -65,7 +65,8 @@ public: LocationHeader, LastModifiedHeader, CookieHeader, - SetCookieHeader + SetCookieHeader, + ContentDispositionHeader // added for QMultipartMessage }; enum Attribute { HttpStatusCodeAttribute, diff --git a/src/network/access/qnetworkrequest_p.h b/src/network/access/qnetworkrequest_p.h index 23705f5..ea8c56f 100644 --- a/src/network/access/qnetworkrequest_p.h +++ b/src/network/access/qnetworkrequest_p.h @@ -62,7 +62,7 @@ QT_BEGIN_NAMESPACE -// this is the common part between QNetworkRequestPrivate and QNetworkReplyPrivate +// this is the common part between QNetworkRequestPrivate, QNetworkReplyPrivate and QHttpPartPrivate class QNetworkHeadersPrivate { public: @@ -77,6 +77,7 @@ public: QWeakPointer<QObject> originatingObject; RawHeadersList::ConstIterator findRawHeader(const QByteArray &key) const; + RawHeadersList allRawHeaders() const; QList<QByteArray> rawHeadersKeys() const; void setRawHeader(const QByteArray &key, const QByteArray &value); void setAllRawHeaders(const RawHeadersList &list); |