author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 26 Jan 2010 12:42:25 +0200 | |
changeset 2 | 56cd8111b7f7 |
parent 1 | ae9c8dab0e3e |
child 3 | 41300fa6a67c |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) |
|
3 |
Copyright (C) 2007 Staikos Computing Services Inc. <info@staikos.net> |
|
4 |
Copyright (C) 2008 Holger Hans Peter Freyther |
|
5 |
||
6 |
This library is free software; you can redistribute it and/or |
|
7 |
modify it under the terms of the GNU Library General Public |
|
8 |
License as published by the Free Software Foundation; either |
|
9 |
version 2 of the License, or (at your option) any later version. |
|
10 |
||
11 |
This library is distributed in the hope that it will be useful, |
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
Library General Public License for more details. |
|
15 |
||
16 |
You should have received a copy of the GNU Library General Public License |
|
17 |
along with this library; see the file COPYING.LIB. If not, write to |
|
18 |
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
19 |
Boston, MA 02110-1301, USA. |
|
20 |
*/ |
|
21 |
#include "config.h" |
|
22 |
#include "QNetworkReplyHandler.h" |
|
23 |
||
24 |
#if QT_VERSION >= 0x040400 |
|
25 |
||
26 |
#include "HTTPParsers.h" |
|
27 |
#include "MIMETypeRegistry.h" |
|
28 |
#include "ResourceHandle.h" |
|
29 |
#include "ResourceHandleClient.h" |
|
30 |
#include "ResourceHandleInternal.h" |
|
31 |
#include "ResourceResponse.h" |
|
32 |
#include "ResourceRequest.h" |
|
33 |
#include <QDateTime> |
|
34 |
#include <QFile> |
|
35 |
#include <QNetworkReply> |
|
36 |
#include <QNetworkCookie> |
|
37 |
#include <qwebframe.h> |
|
38 |
#include <qwebpage.h> |
|
39 |
||
40 |
#include <QDebug> |
|
41 |
#include <QCoreApplication> |
|
42 |
||
43 |
namespace WebCore { |
|
44 |
||
45 |
// Take a deep copy of the FormDataElement |
|
46 |
FormDataIODevice::FormDataIODevice(FormData* data) |
|
47 |
: m_formElements(data ? data->elements() : Vector<FormDataElement>()) |
|
48 |
, m_currentFile(0) |
|
49 |
, m_currentDelta(0) |
|
50 |
{ |
|
51 |
setOpenMode(FormDataIODevice::ReadOnly); |
|
52 |
} |
|
53 |
||
54 |
FormDataIODevice::~FormDataIODevice() |
|
55 |
{ |
|
56 |
delete m_currentFile; |
|
57 |
} |
|
58 |
||
59 |
void FormDataIODevice::moveToNextElement() |
|
60 |
{ |
|
61 |
if (m_currentFile) |
|
62 |
m_currentFile->close(); |
|
63 |
m_currentDelta = 0; |
|
64 |
||
65 |
m_formElements.remove(0); |
|
66 |
||
67 |
if (m_formElements.isEmpty() || m_formElements[0].m_type == FormDataElement::data) |
|
68 |
return; |
|
69 |
||
70 |
if (!m_currentFile) |
|
71 |
m_currentFile = new QFile; |
|
72 |
||
73 |
m_currentFile->setFileName(m_formElements[0].m_filename); |
|
74 |
m_currentFile->open(QFile::ReadOnly); |
|
75 |
} |
|
76 |
||
77 |
// m_formElements[0] is the current item. If the destination buffer is |
|
78 |
// big enough we are going to read from more than one FormDataElement |
|
79 |
qint64 FormDataIODevice::readData(char* destination, qint64 size) |
|
80 |
{ |
|
81 |
if (m_formElements.isEmpty()) |
|
82 |
return -1; |
|
83 |
||
84 |
qint64 copied = 0; |
|
85 |
while (copied < size && !m_formElements.isEmpty()) { |
|
86 |
const FormDataElement& element = m_formElements[0]; |
|
87 |
const qint64 available = size-copied; |
|
88 |
||
89 |
if (element.m_type == FormDataElement::data) { |
|
90 |
const qint64 toCopy = qMin<qint64>(available, element.m_data.size() - m_currentDelta); |
|
91 |
memcpy(destination+copied, element.m_data.data()+m_currentDelta, toCopy); |
|
92 |
m_currentDelta += toCopy; |
|
93 |
copied += toCopy; |
|
94 |
||
95 |
if (m_currentDelta == element.m_data.size()) |
|
96 |
moveToNextElement(); |
|
97 |
} else { |
|
98 |
const QByteArray data = m_currentFile->read(available); |
|
99 |
memcpy(destination+copied, data.constData(), data.size()); |
|
100 |
copied += data.size(); |
|
101 |
||
102 |
if (m_currentFile->atEnd() || !m_currentFile->isOpen()) |
|
103 |
moveToNextElement(); |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
return copied; |
|
108 |
} |
|
109 |
||
110 |
qint64 FormDataIODevice::writeData(const char*, qint64) |
|
111 |
{ |
|
112 |
return -1; |
|
113 |
} |
|
114 |
||
115 |
bool FormDataIODevice::isSequential() const |
|
116 |
{ |
|
117 |
return true; |
|
118 |
} |
|
119 |
||
120 |
QNetworkReplyHandler::QNetworkReplyHandler(ResourceHandle* handle, LoadMode loadMode) |
|
121 |
: QObject(0) |
|
122 |
, m_reply(0) |
|
123 |
, m_resourceHandle(handle) |
|
124 |
, m_redirected(false) |
|
125 |
, m_responseSent(false) |
|
126 |
, m_responseDataSent(false) |
|
127 |
, m_loadMode(loadMode) |
|
128 |
, m_shouldStart(true) |
|
129 |
, m_shouldFinish(false) |
|
130 |
, m_shouldSendResponse(false) |
|
131 |
, m_shouldForwardData(false) |
|
132 |
{ |
|
133 |
const ResourceRequest &r = m_resourceHandle->request(); |
|
134 |
||
135 |
if (r.httpMethod() == "GET") |
|
136 |
m_method = QNetworkAccessManager::GetOperation; |
|
137 |
else if (r.httpMethod() == "HEAD") |
|
138 |
m_method = QNetworkAccessManager::HeadOperation; |
|
139 |
else if (r.httpMethod() == "POST") |
|
140 |
m_method = QNetworkAccessManager::PostOperation; |
|
141 |
else if (r.httpMethod() == "PUT") |
|
142 |
m_method = QNetworkAccessManager::PutOperation; |
|
143 |
else |
|
144 |
m_method = QNetworkAccessManager::UnknownOperation; |
|
145 |
||
146 |
m_request = r.toNetworkRequest(); |
|
147 |
||
148 |
if (m_loadMode == LoadNormal) |
|
149 |
start(); |
|
150 |
} |
|
151 |
||
152 |
void QNetworkReplyHandler::setLoadMode(LoadMode mode) |
|
153 |
{ |
|
154 |
// https://bugs.webkit.org/show_bug.cgi?id=26556 |
|
155 |
// We cannot call sendQueuedItems() from here, because the signal that |
|
156 |
// caused us to get into deferred mode, might not be processed yet. |
|
157 |
switch (mode) { |
|
158 |
case LoadNormal: |
|
159 |
m_loadMode = LoadResuming; |
|
160 |
emit processQueuedItems(); |
|
161 |
break; |
|
162 |
case LoadDeferred: |
|
163 |
m_loadMode = LoadDeferred; |
|
164 |
break; |
|
165 |
case LoadResuming: |
|
166 |
Q_ASSERT(0); // should never happen |
|
167 |
break; |
|
168 |
}; |
|
169 |
} |
|
170 |
||
171 |
void QNetworkReplyHandler::abort() |
|
172 |
{ |
|
173 |
m_resourceHandle = 0; |
|
174 |
if (m_reply) { |
|
175 |
QNetworkReply* reply = release(); |
|
176 |
reply->abort(); |
|
177 |
reply->deleteLater(); |
|
178 |
deleteLater(); |
|
179 |
} |
|
180 |
} |
|
181 |
||
182 |
QNetworkReply* QNetworkReplyHandler::release() |
|
183 |
{ |
|
184 |
QNetworkReply* reply = m_reply; |
|
185 |
if (m_reply) { |
|
186 |
disconnect(m_reply, 0, this, 0); |
|
187 |
// We have queued connections to the QNetworkReply. Make sure any |
|
188 |
// posted meta call events that were the result of a signal emission |
|
189 |
// don't reach the slots in our instance. |
|
190 |
QCoreApplication::removePostedEvents(this, QEvent::MetaCall); |
|
191 |
m_reply = 0; |
|
192 |
} |
|
193 |
return reply; |
|
194 |
} |
|
195 |
||
196 |
static bool ignoreHttpError(QNetworkReply* reply, bool receivedData) |
|
197 |
{ |
|
198 |
int httpStatusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); |
|
199 |
||
200 |
if (httpStatusCode == 401 || httpStatusCode == 407) |
|
201 |
return true; |
|
202 |
||
203 |
if (receivedData && (httpStatusCode >= 400 && httpStatusCode < 600)) |
|
204 |
return true; |
|
205 |
||
206 |
return false; |
|
207 |
} |
|
208 |
||
209 |
void QNetworkReplyHandler::finish() |
|
210 |
{ |
|
211 |
m_shouldFinish = (m_loadMode != LoadNormal); |
|
212 |
if (m_shouldFinish) |
|
213 |
return; |
|
214 |
||
215 |
sendResponseIfNeeded(); |
|
216 |
||
217 |
if (!m_resourceHandle) |
|
218 |
return; |
|
219 |
ResourceHandleClient* client = m_resourceHandle->client(); |
|
220 |
if (!client) { |
|
221 |
m_reply->deleteLater(); |
|
222 |
m_reply = 0; |
|
223 |
return; |
|
224 |
} |
|
225 |
||
226 |
QNetworkReply* oldReply = m_reply; |
|
227 |
||
228 |
if (m_redirected) { |
|
229 |
resetState(); |
|
230 |
start(); |
|
231 |
} else if (!m_reply->error() || ignoreHttpError(m_reply, m_responseDataSent)) { |
|
232 |
client->didFinishLoading(m_resourceHandle); |
|
233 |
} else { |
|
234 |
QUrl url = m_reply->url(); |
|
235 |
int httpStatusCode = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); |
|
236 |
||
237 |
if (httpStatusCode) { |
|
238 |
ResourceError error("HTTP", httpStatusCode, url.toString(), m_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString()); |
|
239 |
client->didFail(m_resourceHandle, error); |
|
240 |
} else { |
|
241 |
ResourceError error("QtNetwork", m_reply->error(), url.toString(), m_reply->errorString()); |
|
242 |
client->didFail(m_resourceHandle, error); |
|
243 |
} |
|
244 |
} |
|
245 |
||
246 |
oldReply->deleteLater(); |
|
247 |
if (oldReply == m_reply) |
|
248 |
m_reply = 0; |
|
249 |
} |
|
250 |
||
251 |
void QNetworkReplyHandler::sendResponseIfNeeded() |
|
252 |
{ |
|
253 |
m_shouldSendResponse = (m_loadMode != LoadNormal); |
|
254 |
if (m_shouldSendResponse) |
|
255 |
return; |
|
256 |
||
2
56cd8111b7f7
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
1
diff
changeset
|
257 |
if (m_reply->error()) |
0 | 258 |
return; |
259 |
||
260 |
if (m_responseSent || !m_resourceHandle) |
|
261 |
return; |
|
262 |
m_responseSent = true; |
|
263 |
||
264 |
ResourceHandleClient* client = m_resourceHandle->client(); |
|
265 |
if (!client) |
|
266 |
return; |
|
267 |
||
268 |
WebCore::String contentType = m_reply->header(QNetworkRequest::ContentTypeHeader).toString(); |
|
269 |
WebCore::String encoding = extractCharsetFromMediaType(contentType); |
|
270 |
WebCore::String mimeType = extractMIMETypeFromMediaType(contentType); |
|
271 |
||
272 |
if (mimeType.isEmpty()) { |
|
273 |
// let's try to guess from the extension |
|
274 |
QString extension = m_reply->url().path(); |
|
275 |
int index = extension.lastIndexOf(QLatin1Char('.')); |
|
276 |
if (index > 0) { |
|
277 |
extension = extension.mid(index + 1); |
|
278 |
mimeType = MIMETypeRegistry::getMIMETypeForExtension(extension); |
|
279 |
} |
|
280 |
} |
|
281 |
||
282 |
KURL url(m_reply->url()); |
|
283 |
ResourceResponse response(url, mimeType, |
|
284 |
m_reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), |
|
285 |
encoding, String()); |
|
286 |
||
287 |
if (url.isLocalFile()) { |
|
288 |
client->didReceiveResponse(m_resourceHandle, response); |
|
289 |
return; |
|
290 |
} |
|
291 |
||
292 |
// The status code is equal to 0 for protocols not in the HTTP family. |
|
293 |
int statusCode = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); |
|
294 |
||
295 |
if (url.protocolInHTTPFamily()) { |
|
296 |
String suggestedFilename = filenameFromHTTPContentDisposition(QString::fromAscii(m_reply->rawHeader("Content-Disposition"))); |
|
297 |
||
298 |
if (!suggestedFilename.isEmpty()) |
|
299 |
response.setSuggestedFilename(suggestedFilename); |
|
300 |
else |
|
301 |
response.setSuggestedFilename(url.lastPathComponent()); |
|
302 |
||
303 |
response.setHTTPStatusCode(statusCode); |
|
304 |
response.setHTTPStatusText(m_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray().constData()); |
|
305 |
||
306 |
// Add remaining headers. |
|
307 |
foreach (const QByteArray& headerName, m_reply->rawHeaderList()) { |
|
308 |
response.setHTTPHeaderField(QString::fromAscii(headerName), QString::fromAscii(m_reply->rawHeader(headerName))); |
|
309 |
} |
|
310 |
} |
|
311 |
||
312 |
QUrl redirection = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); |
|
313 |
if (redirection.isValid()) { |
|
314 |
QUrl newUrl = m_reply->url().resolved(redirection); |
|
315 |
ResourceRequest newRequest = m_resourceHandle->request(); |
|
316 |
newRequest.setURL(newUrl); |
|
317 |
||
318 |
if (((statusCode >= 301 && statusCode <= 303) || statusCode == 307) && m_method == QNetworkAccessManager::PostOperation) { |
|
319 |
m_method = QNetworkAccessManager::GetOperation; |
|
320 |
newRequest.setHTTPMethod("GET"); |
|
321 |
} |
|
322 |
||
323 |
client->willSendRequest(m_resourceHandle, newRequest, response); |
|
324 |
m_redirected = true; |
|
325 |
m_request = newRequest.toNetworkRequest(); |
|
326 |
||
327 |
ResourceHandleInternal* d = m_resourceHandle->getInternal(); |
|
328 |
emit d->m_frame->page()->networkRequestStarted(d->m_frame, &m_request); |
|
329 |
return; |
|
330 |
} |
|
331 |
||
332 |
client->didReceiveResponse(m_resourceHandle, response); |
|
333 |
} |
|
334 |
||
335 |
void QNetworkReplyHandler::forwardData() |
|
336 |
{ |
|
337 |
m_shouldForwardData = (m_loadMode != LoadNormal); |
|
338 |
if (m_shouldForwardData) |
|
339 |
return; |
|
340 |
||
341 |
sendResponseIfNeeded(); |
|
342 |
||
343 |
// don't emit the "Document has moved here" type of HTML |
|
344 |
if (m_redirected) |
|
345 |
return; |
|
346 |
||
347 |
if (!m_resourceHandle) |
|
348 |
return; |
|
349 |
||
350 |
QByteArray data = m_reply->read(m_reply->bytesAvailable()); |
|
351 |
||
352 |
ResourceHandleClient* client = m_resourceHandle->client(); |
|
353 |
if (!client) |
|
354 |
return; |
|
355 |
||
356 |
if (!data.isEmpty()) { |
|
357 |
m_responseDataSent = true; |
|
358 |
client->didReceiveData(m_resourceHandle, data.constData(), data.length(), data.length() /*FixMe*/); |
|
359 |
} |
|
360 |
} |
|
361 |
||
362 |
void QNetworkReplyHandler::start() |
|
363 |
{ |
|
364 |
m_shouldStart = false; |
|
365 |
||
366 |
ResourceHandleInternal* d = m_resourceHandle->getInternal(); |
|
367 |
||
368 |
QNetworkAccessManager* manager = d->m_frame->page()->networkAccessManager(); |
|
369 |
||
370 |
emit d->m_frame->page()->networkRequestStarted(d->m_frame, &m_request); |
|
371 |
||
372 |
const QUrl url = m_request.url(); |
|
373 |
const QString scheme = url.scheme(); |
|
374 |
// Post requests on files and data don't really make sense, but for |
|
375 |
// fast/forms/form-post-urlencoded.html and for fast/forms/button-state-restore.html |
|
376 |
// we still need to retrieve the file/data, which means we map it to a Get instead. |
|
377 |
if (m_method == QNetworkAccessManager::PostOperation |
|
378 |
&& (!url.toLocalFile().isEmpty() || url.scheme() == QLatin1String("data"))) |
|
379 |
m_method = QNetworkAccessManager::GetOperation; |
|
380 |
||
381 |
switch (m_method) { |
|
382 |
case QNetworkAccessManager::GetOperation: |
|
383 |
m_reply = manager->get(m_request); |
|
384 |
break; |
|
385 |
case QNetworkAccessManager::PostOperation: { |
|
386 |
FormDataIODevice* postDevice = new FormDataIODevice(d->m_request.httpBody()); |
|
387 |
m_reply = manager->post(m_request, postDevice); |
|
388 |
postDevice->setParent(m_reply); |
|
389 |
break; |
|
390 |
} |
|
391 |
case QNetworkAccessManager::HeadOperation: |
|
392 |
m_reply = manager->head(m_request); |
|
393 |
break; |
|
394 |
case QNetworkAccessManager::PutOperation: { |
|
395 |
FormDataIODevice* putDevice = new FormDataIODevice(d->m_request.httpBody()); |
|
396 |
m_reply = manager->put(m_request, putDevice); |
|
397 |
putDevice->setParent(m_reply); |
|
398 |
break; |
|
399 |
} |
|
400 |
case QNetworkAccessManager::UnknownOperation: { |
|
401 |
m_reply = 0; |
|
402 |
ResourceHandleClient* client = m_resourceHandle->client(); |
|
403 |
if (client) { |
|
404 |
ResourceError error(url.host(), 400 /*bad request*/, |
|
405 |
url.toString(), |
|
406 |
QCoreApplication::translate("QWebPage", "Bad HTTP request")); |
|
407 |
client->didFail(m_resourceHandle, error); |
|
408 |
} |
|
409 |
return; |
|
410 |
} |
|
411 |
} |
|
412 |
||
413 |
m_reply->setParent(this); |
|
414 |
||
415 |
connect(m_reply, SIGNAL(finished()), |
|
416 |
this, SLOT(finish()), Qt::QueuedConnection); |
|
417 |
||
418 |
// For http(s) we know that the headers are complete upon metaDataChanged() emission, so we |
|
419 |
// can send the response as early as possible |
|
420 |
if (scheme == QLatin1String("http") || scheme == QLatin1String("https")) |
|
421 |
connect(m_reply, SIGNAL(metaDataChanged()), |
|
422 |
this, SLOT(sendResponseIfNeeded()), Qt::QueuedConnection); |
|
423 |
||
424 |
connect(m_reply, SIGNAL(readyRead()), |
|
425 |
this, SLOT(forwardData()), Qt::QueuedConnection); |
|
426 |
connect(this, SIGNAL(processQueuedItems()), |
|
427 |
this, SLOT(sendQueuedItems()), Qt::QueuedConnection); |
|
428 |
} |
|
429 |
||
430 |
void QNetworkReplyHandler::resetState() |
|
431 |
{ |
|
432 |
m_redirected = false; |
|
433 |
m_responseSent = false; |
|
434 |
m_responseDataSent = false; |
|
435 |
m_shouldStart = true; |
|
436 |
m_shouldFinish = false; |
|
437 |
m_shouldSendResponse = false; |
|
438 |
m_shouldForwardData = false; |
|
439 |
} |
|
440 |
||
441 |
void QNetworkReplyHandler::sendQueuedItems() |
|
442 |
{ |
|
443 |
if (m_loadMode != LoadResuming) |
|
444 |
return; |
|
445 |
m_loadMode = LoadNormal; |
|
446 |
||
447 |
if (m_shouldStart) |
|
448 |
start(); |
|
449 |
||
450 |
if (m_shouldSendResponse) |
|
451 |
sendResponseIfNeeded(); |
|
452 |
||
453 |
if (m_shouldForwardData) |
|
454 |
forwardData(); |
|
455 |
||
456 |
if (m_shouldFinish) |
|
457 |
finish(); |
|
458 |
} |
|
459 |
||
460 |
} |
|
461 |
||
462 |
#include "moc_QNetworkReplyHandler.cpp" |
|
463 |
||
464 |
#endif |