1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qnetworkaccessbackend_p.h"
|
---|
43 | #include "qnetworkaccessmanager_p.h"
|
---|
44 | #include "qnetworkrequest.h"
|
---|
45 | #include "qnetworkreply.h"
|
---|
46 | #include "qnetworkreply_p.h"
|
---|
47 | #include "QtCore/qhash.h"
|
---|
48 | #include "QtCore/qmutex.h"
|
---|
49 |
|
---|
50 | #include "qnetworkaccesscachebackend_p.h"
|
---|
51 | #include "qabstractnetworkcache.h"
|
---|
52 |
|
---|
53 | QT_BEGIN_NAMESPACE
|
---|
54 |
|
---|
55 | static bool factoryDataShutdown = false;
|
---|
56 | class QNetworkAccessBackendFactoryData: public QList<QNetworkAccessBackendFactory *>
|
---|
57 | {
|
---|
58 | public:
|
---|
59 | QNetworkAccessBackendFactoryData() : mutex(QMutex::Recursive) { }
|
---|
60 | ~QNetworkAccessBackendFactoryData()
|
---|
61 | {
|
---|
62 | QMutexLocker locker(&mutex); // why do we need to lock?
|
---|
63 | factoryDataShutdown = true;
|
---|
64 | }
|
---|
65 |
|
---|
66 | QMutex mutex;
|
---|
67 | };
|
---|
68 | Q_GLOBAL_STATIC(QNetworkAccessBackendFactoryData, factoryData)
|
---|
69 |
|
---|
70 | QNetworkAccessBackendFactory::QNetworkAccessBackendFactory()
|
---|
71 | {
|
---|
72 | QMutexLocker locker(&factoryData()->mutex);
|
---|
73 | factoryData()->prepend(this);
|
---|
74 | }
|
---|
75 |
|
---|
76 | QNetworkAccessBackendFactory::~QNetworkAccessBackendFactory()
|
---|
77 | {
|
---|
78 | if (!factoryDataShutdown) {
|
---|
79 | QMutexLocker locker(&factoryData()->mutex);
|
---|
80 | factoryData()->removeAll(this);
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | QNetworkAccessBackend *QNetworkAccessManagerPrivate::findBackend(QNetworkAccessManager::Operation op,
|
---|
85 | const QNetworkRequest &request)
|
---|
86 | {
|
---|
87 | QNetworkRequest::CacheLoadControl mode =
|
---|
88 | static_cast<QNetworkRequest::CacheLoadControl>(
|
---|
89 | request.attribute(QNetworkRequest::CacheLoadControlAttribute,
|
---|
90 | QNetworkRequest::PreferNetwork).toInt());
|
---|
91 | if (mode == QNetworkRequest::AlwaysCache
|
---|
92 | && (op == QNetworkAccessManager::GetOperation
|
---|
93 | || op == QNetworkAccessManager::HeadOperation))
|
---|
94 | return new QNetworkAccessCacheBackend;
|
---|
95 |
|
---|
96 | if (!factoryDataShutdown) {
|
---|
97 | QMutexLocker locker(&factoryData()->mutex);
|
---|
98 | QNetworkAccessBackendFactoryData::ConstIterator it = factoryData()->constBegin(),
|
---|
99 | end = factoryData()->constEnd();
|
---|
100 | while (it != end) {
|
---|
101 | QNetworkAccessBackend *backend = (*it)->create(op, request);
|
---|
102 | if (backend) {
|
---|
103 | backend->manager = this;
|
---|
104 | return backend; // found a factory that handled our request
|
---|
105 | }
|
---|
106 | ++it;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | return 0;
|
---|
110 | }
|
---|
111 |
|
---|
112 | QNetworkAccessBackend::QNetworkAccessBackend()
|
---|
113 | {
|
---|
114 | }
|
---|
115 |
|
---|
116 | QNetworkAccessBackend::~QNetworkAccessBackend()
|
---|
117 | {
|
---|
118 | }
|
---|
119 |
|
---|
120 | void QNetworkAccessBackend::upstreamReadyRead()
|
---|
121 | {
|
---|
122 | // do nothing
|
---|
123 | }
|
---|
124 |
|
---|
125 | void QNetworkAccessBackend::downstreamReadyWrite()
|
---|
126 | {
|
---|
127 | // do nothing
|
---|
128 | }
|
---|
129 |
|
---|
130 | void QNetworkAccessBackend::copyFinished(QIODevice *)
|
---|
131 | {
|
---|
132 | // do nothing
|
---|
133 | }
|
---|
134 |
|
---|
135 | void QNetworkAccessBackend::ignoreSslErrors()
|
---|
136 | {
|
---|
137 | // do nothing
|
---|
138 | }
|
---|
139 |
|
---|
140 | void QNetworkAccessBackend::fetchSslConfiguration(QSslConfiguration &) const
|
---|
141 | {
|
---|
142 | // do nothing
|
---|
143 | }
|
---|
144 |
|
---|
145 | void QNetworkAccessBackend::setSslConfiguration(const QSslConfiguration &)
|
---|
146 | {
|
---|
147 | // do nothing
|
---|
148 | }
|
---|
149 |
|
---|
150 | QNetworkCacheMetaData QNetworkAccessBackend::fetchCacheMetaData(const QNetworkCacheMetaData &) const
|
---|
151 | {
|
---|
152 | return QNetworkCacheMetaData();
|
---|
153 | }
|
---|
154 |
|
---|
155 | QNetworkAccessManager::Operation QNetworkAccessBackend::operation() const
|
---|
156 | {
|
---|
157 | return reply->operation;
|
---|
158 | }
|
---|
159 |
|
---|
160 | QNetworkRequest QNetworkAccessBackend::request() const
|
---|
161 | {
|
---|
162 | return reply->request;
|
---|
163 | }
|
---|
164 |
|
---|
165 | #ifndef QT_NO_NETWORKPROXY
|
---|
166 | QList<QNetworkProxy> QNetworkAccessBackend::proxyList() const
|
---|
167 | {
|
---|
168 | return reply->proxyList;
|
---|
169 | }
|
---|
170 | #endif
|
---|
171 |
|
---|
172 | QAbstractNetworkCache *QNetworkAccessBackend::networkCache() const
|
---|
173 | {
|
---|
174 | return reply->networkCache; // should be the same as manager->networkCache
|
---|
175 | }
|
---|
176 |
|
---|
177 | void QNetworkAccessBackend::setCachingEnabled(bool enable)
|
---|
178 | {
|
---|
179 | reply->setCachingEnabled(enable);
|
---|
180 | }
|
---|
181 |
|
---|
182 | bool QNetworkAccessBackend::isCachingEnabled() const
|
---|
183 | {
|
---|
184 | return reply->isCachingEnabled();
|
---|
185 | }
|
---|
186 |
|
---|
187 | qint64 QNetworkAccessBackend::upstreamBytesAvailable() const
|
---|
188 | {
|
---|
189 | return reply->writeBuffer.size();
|
---|
190 | }
|
---|
191 |
|
---|
192 | void QNetworkAccessBackend::upstreamBytesConsumed(qint64 count)
|
---|
193 | {
|
---|
194 | // remove count bytes from the write buffer
|
---|
195 | reply->consume(count);
|
---|
196 | }
|
---|
197 |
|
---|
198 | QByteArray QNetworkAccessBackend::readUpstream()
|
---|
199 | {
|
---|
200 | // ### this is expensive. Consider making QRingBuffer::peekAll keep the buffer it returns
|
---|
201 | return reply->writeBuffer.peek(upstreamBytesAvailable());
|
---|
202 | }
|
---|
203 |
|
---|
204 | qint64 QNetworkAccessBackend::nextDownstreamBlockSize() const
|
---|
205 | {
|
---|
206 | return reply->nextDownstreamBlockSize();
|
---|
207 | }
|
---|
208 |
|
---|
209 | qint64 QNetworkAccessBackend::downstreamBytesToConsume() const
|
---|
210 | {
|
---|
211 | return reply->writeBuffer.size();
|
---|
212 | }
|
---|
213 |
|
---|
214 | void QNetworkAccessBackend::writeDownstreamData(const QByteArray &data)
|
---|
215 | {
|
---|
216 | reply->feed(data);
|
---|
217 | }
|
---|
218 |
|
---|
219 | void QNetworkAccessBackend::writeDownstreamData(QIODevice *data)
|
---|
220 | {
|
---|
221 | reply->feed(data);
|
---|
222 | }
|
---|
223 |
|
---|
224 | QVariant QNetworkAccessBackend::header(QNetworkRequest::KnownHeaders header) const
|
---|
225 | {
|
---|
226 | return reply->q_func()->header(header);
|
---|
227 | }
|
---|
228 |
|
---|
229 | void QNetworkAccessBackend::setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value)
|
---|
230 | {
|
---|
231 | reply->setCookedHeader(header, value);
|
---|
232 | }
|
---|
233 |
|
---|
234 | bool QNetworkAccessBackend::hasRawHeader(const QByteArray &headerName) const
|
---|
235 | {
|
---|
236 | return reply->q_func()->hasRawHeader(headerName);
|
---|
237 | }
|
---|
238 |
|
---|
239 | QByteArray QNetworkAccessBackend::rawHeader(const QByteArray &headerName) const
|
---|
240 | {
|
---|
241 | return reply->q_func()->rawHeader(headerName);
|
---|
242 | }
|
---|
243 |
|
---|
244 | QList<QByteArray> QNetworkAccessBackend::rawHeaderList() const
|
---|
245 | {
|
---|
246 | return reply->q_func()->rawHeaderList();
|
---|
247 | }
|
---|
248 |
|
---|
249 | void QNetworkAccessBackend::setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)
|
---|
250 | {
|
---|
251 | reply->setRawHeader(headerName, headerValue);
|
---|
252 | }
|
---|
253 |
|
---|
254 | QVariant QNetworkAccessBackend::attribute(QNetworkRequest::Attribute code) const
|
---|
255 | {
|
---|
256 | return reply->q_func()->attribute(code);
|
---|
257 | }
|
---|
258 |
|
---|
259 | void QNetworkAccessBackend::setAttribute(QNetworkRequest::Attribute code, const QVariant &value)
|
---|
260 | {
|
---|
261 | if (value.isValid())
|
---|
262 | reply->attributes.insert(code, value);
|
---|
263 | else
|
---|
264 | reply->attributes.remove(code);
|
---|
265 | }
|
---|
266 | QUrl QNetworkAccessBackend::url() const
|
---|
267 | {
|
---|
268 | return reply->url;
|
---|
269 | }
|
---|
270 |
|
---|
271 | void QNetworkAccessBackend::setUrl(const QUrl &url)
|
---|
272 | {
|
---|
273 | reply->url = url;
|
---|
274 | }
|
---|
275 |
|
---|
276 | void QNetworkAccessBackend::finished()
|
---|
277 | {
|
---|
278 | reply->finished();
|
---|
279 | }
|
---|
280 |
|
---|
281 | void QNetworkAccessBackend::error(QNetworkReply::NetworkError code, const QString &errorString)
|
---|
282 | {
|
---|
283 | reply->error(code, errorString);
|
---|
284 | }
|
---|
285 |
|
---|
286 | #ifndef QT_NO_NETWORKPROXY
|
---|
287 | void QNetworkAccessBackend::proxyAuthenticationRequired(const QNetworkProxy &proxy,
|
---|
288 | QAuthenticator *authenticator)
|
---|
289 | {
|
---|
290 | manager->proxyAuthenticationRequired(this, proxy, authenticator);
|
---|
291 | }
|
---|
292 | #endif
|
---|
293 |
|
---|
294 | void QNetworkAccessBackend::authenticationRequired(QAuthenticator *authenticator)
|
---|
295 | {
|
---|
296 | manager->authenticationRequired(this, authenticator);
|
---|
297 | }
|
---|
298 |
|
---|
299 | void QNetworkAccessBackend::metaDataChanged()
|
---|
300 | {
|
---|
301 | reply->metaDataChanged();
|
---|
302 | }
|
---|
303 |
|
---|
304 | void QNetworkAccessBackend::redirectionRequested(const QUrl &target)
|
---|
305 | {
|
---|
306 | reply->redirectionRequested(target);
|
---|
307 | }
|
---|
308 |
|
---|
309 | void QNetworkAccessBackend::sslErrors(const QList<QSslError> &errors)
|
---|
310 | {
|
---|
311 | #ifndef QT_NO_OPENSSL
|
---|
312 | reply->sslErrors(errors);
|
---|
313 | #else
|
---|
314 | Q_UNUSED(errors);
|
---|
315 | #endif
|
---|
316 | }
|
---|
317 |
|
---|
318 | QT_END_NAMESPACE
|
---|