1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qnetworkreplyfileimpl_p.h"
44 #include "QtCore/qdatetime.h"
45 #include <QtCore/QCoreApplication>
46 #include <QtCore/QFileInfo>
51 QNetworkReplyFileImplPrivate::QNetworkReplyFileImplPrivate()
52 : QNetworkReplyPrivate(), realFileSize(0)
56 QNetworkReplyFileImpl::~QNetworkReplyFileImpl()
60 QNetworkReplyFileImpl::QNetworkReplyFileImpl(QObject *parent, const QNetworkRequest &req, const QNetworkAccessManager::Operation op)
61 : QNetworkReply(*new QNetworkReplyFileImplPrivate(), parent)
67 QNetworkReply::open(QIODevice::ReadOnly);
69 QNetworkReplyFileImplPrivate *d = (QNetworkReplyFileImplPrivate*) d_func();
72 if (url.host() == QLatin1String("localhost"))
73 url.setHost(QString());
75 #if !defined(Q_OS_WIN)
76 // do not allow UNC paths on Unix
77 if (!url.host().isEmpty()) {
78 // we handle only local files
79 QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Request for opening non-local file %1").arg(url.toString());
80 setError(QNetworkReply::ProtocolInvalidOperationError, msg);
81 QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
82 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ProtocolInvalidOperationError));
83 QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
87 if (url.path().isEmpty())
88 url.setPath(QLatin1String("/"));
92 QString fileName = url.toLocalFile();
93 if (fileName.isEmpty()) {
94 if (url.scheme() == QLatin1String("qrc"))
95 fileName = QLatin1Char(':') + url.path();
97 fileName = url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery);
100 QFileInfo fi(fileName);
102 QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Cannot open %1: Path is a directory").arg(url.toString());
103 setError(QNetworkReply::ContentOperationNotPermittedError, msg);
104 QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
105 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentOperationNotPermittedError));
106 QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
110 d->realFile.setFileName(fileName);
111 bool opened = d->realFile.open(QIODevice::ReadOnly | QIODevice::Unbuffered);
113 // could we open the file?
115 QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Error opening %1: %2")
116 .arg(d->realFile.fileName(), d->realFile.errorString());
118 if (d->realFile.exists()) {
119 setError(QNetworkReply::ContentAccessDenied, msg);
120 QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
121 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentAccessDenied));
123 setError(QNetworkReply::ContentNotFoundError, msg);
124 QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
125 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentNotFoundError));
127 QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
131 setHeader(QNetworkRequest::LastModifiedHeader, fi.lastModified());
132 d->realFileSize = fi.size();
133 setHeader(QNetworkRequest::ContentLengthHeader, d->realFileSize);
135 QMetaObject::invokeMethod(this, "metaDataChanged", Qt::QueuedConnection);
136 QMetaObject::invokeMethod(this, "downloadProgress", Qt::QueuedConnection,
137 Q_ARG(qint64, d->realFileSize), Q_ARG(qint64, d->realFileSize));
138 QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
139 QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
141 void QNetworkReplyFileImpl::close()
143 Q_D(QNetworkReplyFileImpl);
144 QNetworkReply::close();
148 void QNetworkReplyFileImpl::abort()
150 Q_D(QNetworkReplyFileImpl);
151 QNetworkReply::close();
155 qint64 QNetworkReplyFileImpl::bytesAvailable() const
157 Q_D(const QNetworkReplyFileImpl);
158 return QNetworkReply::bytesAvailable() + d->realFile.bytesAvailable();
161 bool QNetworkReplyFileImpl::isSequential () const
166 qint64 QNetworkReplyFileImpl::size() const
168 Q_D(const QNetworkReplyFileImpl);
169 return d->realFileSize;
175 qint64 QNetworkReplyFileImpl::readData(char *data, qint64 maxlen)
177 Q_D(QNetworkReplyFileImpl);
178 qint64 ret = d->realFile.read(data, maxlen);
179 if (ret == 0 && bytesAvailable() == 0)
188 #include "moc_qnetworkreplyfileimpl_p.cpp"