| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 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 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qfilenetworkreply_p.h"
|
|---|
| 43 |
|
|---|
| 44 | #include "QtCore/qdatetime.h"
|
|---|
| 45 | #include <QtCore/QCoreApplication>
|
|---|
| 46 | #include <QtCore/QFileInfo>
|
|---|
| 47 | #include <QDebug>
|
|---|
| 48 |
|
|---|
| 49 | QT_BEGIN_NAMESPACE
|
|---|
| 50 |
|
|---|
| 51 | QFileNetworkReplyPrivate::QFileNetworkReplyPrivate()
|
|---|
| 52 | : QNetworkReplyPrivate(), fileEngine(0), fileSize(0), filePos(0)
|
|---|
| 53 | {
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | QFileNetworkReplyPrivate::~QFileNetworkReplyPrivate()
|
|---|
| 57 | {
|
|---|
| 58 | delete fileEngine;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | QFileNetworkReply::~QFileNetworkReply()
|
|---|
| 62 | {
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | QFileNetworkReply::QFileNetworkReply(QObject *parent, const QNetworkRequest &req, const QNetworkAccessManager::Operation op)
|
|---|
| 66 | : QNetworkReply(*new QFileNetworkReplyPrivate(), parent)
|
|---|
| 67 | {
|
|---|
| 68 | setRequest(req);
|
|---|
| 69 | setUrl(req.url());
|
|---|
| 70 | setOperation(op);
|
|---|
| 71 | QNetworkReply::open(QIODevice::ReadOnly);
|
|---|
| 72 |
|
|---|
| 73 | qRegisterMetaType<QNetworkReply::NetworkError>("QNetworkReply::NetworkError");
|
|---|
| 74 |
|
|---|
| 75 | QFileNetworkReplyPrivate *d = (QFileNetworkReplyPrivate*) d_func();
|
|---|
| 76 |
|
|---|
| 77 | QUrl url = req.url();
|
|---|
| 78 | if (url.host() == QLatin1String("localhost"))
|
|---|
| 79 | url.setHost(QString());
|
|---|
| 80 |
|
|---|
| 81 | #if !defined(Q_OS_WIN)
|
|---|
| 82 | // do not allow UNC paths on Unix
|
|---|
| 83 | if (!url.host().isEmpty()) {
|
|---|
| 84 | // we handle only local files
|
|---|
| 85 | QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Request for opening non-local file %1").arg(url.toString());
|
|---|
| 86 | setError(QNetworkReply::ProtocolInvalidOperationError, msg);
|
|---|
| 87 | QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
|
|---|
| 88 | Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ProtocolInvalidOperationError));
|
|---|
| 89 | QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
|
|---|
| 90 | return;
|
|---|
| 91 | }
|
|---|
| 92 | #endif
|
|---|
| 93 | if (url.path().isEmpty())
|
|---|
| 94 | url.setPath(QLatin1String("/"));
|
|---|
| 95 | setUrl(url);
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 | QString fileName = url.toLocalFile();
|
|---|
| 99 | if (fileName.isEmpty()) {
|
|---|
| 100 | if (url.scheme() == QLatin1String("qrc"))
|
|---|
| 101 | fileName = QLatin1Char(':') + url.path();
|
|---|
| 102 | else
|
|---|
| 103 | fileName = url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | QFileInfo fi(fileName);
|
|---|
| 107 | if (fi.isDir()) {
|
|---|
| 108 | QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Cannot open %1: Path is a directory").arg(url.toString());
|
|---|
| 109 | setError(QNetworkReply::ContentOperationNotPermittedError, msg);
|
|---|
| 110 | QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
|
|---|
| 111 | Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentOperationNotPermittedError));
|
|---|
| 112 | QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
|
|---|
| 113 | return;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | d->fileEngine = QAbstractFileEngine::create(fileName);
|
|---|
| 117 | bool opened = d->fileEngine->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
|
|---|
| 118 |
|
|---|
| 119 | // could we open the file?
|
|---|
| 120 | if (!opened) {
|
|---|
| 121 | QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Error opening %1: %2")
|
|---|
| 122 | .arg(fileName, d->fileEngine->errorString());
|
|---|
| 123 |
|
|---|
| 124 | if (fi.exists()) {
|
|---|
| 125 | setError(QNetworkReply::ContentAccessDenied, msg);
|
|---|
| 126 | QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
|
|---|
| 127 | Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentAccessDenied));
|
|---|
| 128 | } else {
|
|---|
| 129 | setError(QNetworkReply::ContentNotFoundError, msg);
|
|---|
| 130 | QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
|
|---|
| 131 | Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentNotFoundError));
|
|---|
| 132 | }
|
|---|
| 133 | QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
|
|---|
| 134 | return;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | d->fileSize = fi.size();
|
|---|
| 138 | setHeader(QNetworkRequest::LastModifiedHeader, fi.lastModified());
|
|---|
| 139 | setHeader(QNetworkRequest::ContentLengthHeader, d->fileSize);
|
|---|
| 140 |
|
|---|
| 141 | QMetaObject::invokeMethod(this, "metaDataChanged", Qt::QueuedConnection);
|
|---|
| 142 | QMetaObject::invokeMethod(this, "downloadProgress", Qt::QueuedConnection,
|
|---|
| 143 | Q_ARG(qint64, d->fileSize), Q_ARG(qint64, d->fileSize));
|
|---|
| 144 | QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
|
|---|
| 145 | QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | bool QFileNetworkReplyPrivate::isFinished() const
|
|---|
| 149 | {
|
|---|
| 150 | return true;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | void QFileNetworkReply::close()
|
|---|
| 154 | {
|
|---|
| 155 | Q_D(QFileNetworkReply);
|
|---|
| 156 | QNetworkReply::close();
|
|---|
| 157 | if (d->fileEngine)
|
|---|
| 158 | d->fileEngine->close();
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | void QFileNetworkReply::abort()
|
|---|
| 162 | {
|
|---|
| 163 | Q_D(QFileNetworkReply);
|
|---|
| 164 | QNetworkReply::close();
|
|---|
| 165 | if (d->fileEngine)
|
|---|
| 166 | d->fileEngine->close();
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | qint64 QFileNetworkReply::bytesAvailable() const
|
|---|
| 170 | {
|
|---|
| 171 | Q_D(const QFileNetworkReply);
|
|---|
| 172 | if (!d->fileEngine)
|
|---|
| 173 | return 0;
|
|---|
| 174 |
|
|---|
| 175 | return QNetworkReply::bytesAvailable() + d->fileSize - d->filePos;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | bool QFileNetworkReply::isSequential () const
|
|---|
| 179 | {
|
|---|
| 180 | return true;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | qint64 QFileNetworkReply::size() const
|
|---|
| 184 | {
|
|---|
| 185 | Q_D(const QFileNetworkReply);
|
|---|
| 186 | return d->fileSize;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | /*!
|
|---|
| 190 | \internal
|
|---|
| 191 | */
|
|---|
| 192 | qint64 QFileNetworkReply::readData(char *data, qint64 maxlen)
|
|---|
| 193 | {
|
|---|
| 194 | Q_D(QFileNetworkReply);
|
|---|
| 195 | if (!d->fileEngine)
|
|---|
| 196 | return -1;
|
|---|
| 197 |
|
|---|
| 198 | qint64 ret = d->fileEngine->read(data, maxlen);
|
|---|
| 199 | if (ret == 0 && bytesAvailable() == 0) {
|
|---|
| 200 | return -1; // everything had been read
|
|---|
| 201 | } else if (ret > 0) {
|
|---|
| 202 | d->filePos += ret;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | return ret;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 | QT_END_NAMESPACE
|
|---|
| 210 |
|
|---|
| 211 | #include "moc_qfilenetworkreply_p.cpp"
|
|---|
| 212 |
|
|---|