author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 |
** All rights reserved. |
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtNetwork module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qhttpsocketengine_p.h" |
|
43 |
#include "qtcpsocket.h" |
|
44 |
#include "qhostaddress.h" |
|
45 |
#include "qdatetime.h" |
|
46 |
#include "qurl.h" |
|
47 |
#include "qhttp.h" |
|
48 |
||
49 |
#if !defined(QT_NO_NETWORKPROXY) && !defined(QT_NO_HTTP) |
|
50 |
#include <qdebug.h> |
|
51 |
||
52 |
QT_BEGIN_NAMESPACE |
|
53 |
||
54 |
#define DEBUG |
|
55 |
||
56 |
QHttpSocketEngine::QHttpSocketEngine(QObject *parent) |
|
57 |
: QAbstractSocketEngine(*new QHttpSocketEnginePrivate, parent) |
|
58 |
{ |
|
59 |
} |
|
60 |
||
61 |
QHttpSocketEngine::~QHttpSocketEngine() |
|
62 |
{ |
|
63 |
} |
|
64 |
||
65 |
bool QHttpSocketEngine::initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol) |
|
66 |
{ |
|
67 |
Q_D(QHttpSocketEngine); |
|
68 |
if (type != QAbstractSocket::TcpSocket) |
|
69 |
return false; |
|
70 |
||
71 |
setProtocol(protocol); |
|
72 |
setSocketType(type); |
|
73 |
d->socket = new QTcpSocket(this); |
|
74 |
||
75 |
// Explicitly disable proxying on the proxy socket itself to avoid |
|
76 |
// unwanted recursion. |
|
77 |
d->socket->setProxy(QNetworkProxy::NoProxy); |
|
78 |
||
79 |
// Intercept all the signals. |
|
80 |
connect(d->socket, SIGNAL(connected()), |
|
81 |
this, SLOT(slotSocketConnected()), |
|
82 |
Qt::DirectConnection); |
|
83 |
connect(d->socket, SIGNAL(disconnected()), |
|
84 |
this, SLOT(slotSocketDisconnected()), |
|
85 |
Qt::DirectConnection); |
|
86 |
connect(d->socket, SIGNAL(readyRead()), |
|
87 |
this, SLOT(slotSocketReadNotification()), |
|
88 |
Qt::DirectConnection); |
|
89 |
connect(d->socket, SIGNAL(bytesWritten(qint64)), |
|
90 |
this, SLOT(slotSocketBytesWritten()), |
|
91 |
Qt::DirectConnection); |
|
92 |
connect(d->socket, SIGNAL(error(QAbstractSocket::SocketError)), |
|
93 |
this, SLOT(slotSocketError(QAbstractSocket::SocketError)), |
|
94 |
Qt::DirectConnection); |
|
95 |
connect(d->socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), |
|
96 |
this, SLOT(slotSocketStateChanged(QAbstractSocket::SocketState)), |
|
97 |
Qt::DirectConnection); |
|
98 |
||
99 |
return true; |
|
100 |
} |
|
101 |
||
102 |
bool QHttpSocketEngine::initialize(int, QAbstractSocket::SocketState) |
|
103 |
{ |
|
104 |
return false; |
|
105 |
} |
|
106 |
||
107 |
void QHttpSocketEngine::setProxy(const QNetworkProxy &proxy) |
|
108 |
{ |
|
109 |
Q_D(QHttpSocketEngine); |
|
110 |
d->proxy = proxy; |
|
111 |
QString user = proxy.user(); |
|
112 |
if (!user.isEmpty()) |
|
113 |
d->authenticator.setUser(user); |
|
114 |
QString password = proxy.password(); |
|
115 |
if (!password.isEmpty()) |
|
116 |
d->authenticator.setPassword(password); |
|
117 |
} |
|
118 |
||
119 |
int QHttpSocketEngine::socketDescriptor() const |
|
120 |
{ |
|
121 |
Q_D(const QHttpSocketEngine); |
|
122 |
return d->socket ? d->socket->socketDescriptor() : 0; |
|
123 |
} |
|
124 |
||
125 |
bool QHttpSocketEngine::isValid() const |
|
126 |
{ |
|
127 |
Q_D(const QHttpSocketEngine); |
|
128 |
return d->socket; |
|
129 |
} |
|
130 |
||
131 |
bool QHttpSocketEngine::connectInternal() |
|
132 |
{ |
|
133 |
Q_D(QHttpSocketEngine); |
|
134 |
||
135 |
// If the handshake is done, enter ConnectedState state and return true. |
|
136 |
if (d->state == Connected) { |
|
137 |
qWarning("QHttpSocketEngine::connectToHost: called when already connected"); |
|
138 |
setState(QAbstractSocket::ConnectedState); |
|
139 |
return true; |
|
140 |
} |
|
141 |
||
142 |
if (d->state == ConnectSent && d->socketState != QAbstractSocket::ConnectedState) |
|
143 |
setState(QAbstractSocket::UnconnectedState); |
|
144 |
||
145 |
// Handshake isn't done. If unconnected, start connecting. |
|
146 |
if (d->state == None && d->socket->state() == QAbstractSocket::UnconnectedState) { |
|
147 |
setState(QAbstractSocket::ConnectingState); |
|
148 |
d->socket->connectToHost(d->proxy.hostName(), d->proxy.port()); |
|
149 |
} |
|
150 |
||
151 |
// If connected (might happen right away, at least for localhost services |
|
152 |
// on some BSD systems), there might already be bytes available. |
|
153 |
if (bytesAvailable()) |
|
154 |
slotSocketReadNotification(); |
|
155 |
||
156 |
return d->socketState == QAbstractSocket::ConnectedState; |
|
157 |
} |
|
158 |
||
159 |
bool QHttpSocketEngine::connectToHost(const QHostAddress &address, quint16 port) |
|
160 |
{ |
|
161 |
Q_D(QHttpSocketEngine); |
|
162 |
||
163 |
setPeerAddress(address); |
|
164 |
setPeerPort(port); |
|
165 |
d->peerName.clear(); |
|
166 |
||
167 |
return connectInternal(); |
|
168 |
} |
|
169 |
||
170 |
bool QHttpSocketEngine::connectToHostByName(const QString &hostname, quint16 port) |
|
171 |
{ |
|
172 |
Q_D(QHttpSocketEngine); |
|
173 |
||
174 |
setPeerAddress(QHostAddress()); |
|
175 |
setPeerPort(port); |
|
176 |
d->peerName = hostname; |
|
177 |
||
178 |
return connectInternal(); |
|
179 |
} |
|
180 |
||
181 |
bool QHttpSocketEngine::bind(const QHostAddress &, quint16) |
|
182 |
{ |
|
183 |
return false; |
|
184 |
} |
|
185 |
||
186 |
bool QHttpSocketEngine::listen() |
|
187 |
{ |
|
188 |
return false; |
|
189 |
} |
|
190 |
||
191 |
int QHttpSocketEngine::accept() |
|
192 |
{ |
|
193 |
return 0; |
|
194 |
} |
|
195 |
||
196 |
void QHttpSocketEngine::close() |
|
197 |
{ |
|
198 |
Q_D(QHttpSocketEngine); |
|
199 |
if (d->socket) { |
|
200 |
d->socket->close(); |
|
201 |
delete d->socket; |
|
202 |
d->socket = 0; |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
qint64 QHttpSocketEngine::bytesAvailable() const |
|
207 |
{ |
|
208 |
Q_D(const QHttpSocketEngine); |
|
209 |
return d->readBuffer.size() + (d->socket ? d->socket->bytesAvailable() : 0); |
|
210 |
} |
|
211 |
||
212 |
qint64 QHttpSocketEngine::read(char *data, qint64 maxlen) |
|
213 |
{ |
|
214 |
Q_D(QHttpSocketEngine); |
|
215 |
qint64 bytesRead = 0; |
|
216 |
||
217 |
if (!d->readBuffer.isEmpty()) { |
|
218 |
// Read as much from the buffer as we can. |
|
219 |
bytesRead = qMin((qint64)d->readBuffer.size(), maxlen); |
|
220 |
memcpy(data, d->readBuffer.constData(), bytesRead); |
|
221 |
data += bytesRead; |
|
222 |
maxlen -= bytesRead; |
|
223 |
d->readBuffer = d->readBuffer.mid(bytesRead); |
|
224 |
} |
|
225 |
||
226 |
qint64 bytesReadFromSocket = d->socket->read(data, maxlen); |
|
227 |
||
228 |
if (d->socket->state() == QAbstractSocket::UnconnectedState |
|
229 |
&& d->socket->bytesAvailable() == 0) { |
|
230 |
emitReadNotification(); |
|
231 |
} |
|
232 |
||
233 |
if (bytesReadFromSocket > 0) { |
|
234 |
// Add to what we read so far. |
|
235 |
bytesRead += bytesReadFromSocket; |
|
236 |
} else if (bytesRead == 0 && bytesReadFromSocket == -1) { |
|
237 |
// If nothing has been read so far, and the direct socket read |
|
238 |
// failed, return the socket's error. Otherwise, fall through and |
|
239 |
// return as much as we read so far. |
|
240 |
close(); |
|
241 |
setError(QAbstractSocket::RemoteHostClosedError, |
|
242 |
QLatin1String("Remote host closed")); |
|
243 |
setState(QAbstractSocket::UnconnectedState); |
|
244 |
return -1; |
|
245 |
} |
|
246 |
return bytesRead; |
|
247 |
} |
|
248 |
||
249 |
qint64 QHttpSocketEngine::write(const char *data, qint64 len) |
|
250 |
{ |
|
251 |
Q_D(QHttpSocketEngine); |
|
252 |
return d->socket->write(data, len); |
|
253 |
} |
|
254 |
||
255 |
#ifndef QT_NO_UDPSOCKET |
|
256 |
qint64 QHttpSocketEngine::readDatagram(char *, qint64, QHostAddress *, |
|
257 |
quint16 *) |
|
258 |
{ |
|
259 |
return 0; |
|
260 |
} |
|
261 |
||
262 |
qint64 QHttpSocketEngine::writeDatagram(const char *, qint64, const QHostAddress &, |
|
263 |
quint16) |
|
264 |
{ |
|
265 |
return 0; |
|
266 |
} |
|
267 |
||
268 |
bool QHttpSocketEngine::hasPendingDatagrams() const |
|
269 |
{ |
|
270 |
return false; |
|
271 |
} |
|
272 |
||
273 |
qint64 QHttpSocketEngine::pendingDatagramSize() const |
|
274 |
{ |
|
275 |
return 0; |
|
276 |
} |
|
277 |
#endif // QT_NO_UDPSOCKET |
|
278 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
279 |
qint64 QHttpSocketEngine::bytesToWrite() const |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
280 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
281 |
Q_D(const QHttpSocketEngine); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
282 |
if (d->socket) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
283 |
return d->socket->bytesToWrite(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
284 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
285 |
return 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
286 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
287 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
288 |
|
0 | 289 |
int QHttpSocketEngine::option(SocketOption option) const |
290 |
{ |
|
291 |
Q_D(const QHttpSocketEngine); |
|
292 |
if (d->socket) { |
|
293 |
// convert the enum and call the real socket |
|
294 |
if (option == QAbstractSocketEngine::LowDelayOption) |
|
295 |
return d->socket->socketOption(QAbstractSocket::LowDelayOption).toInt(); |
|
296 |
if (option == QAbstractSocketEngine::KeepAliveOption) |
|
297 |
return d->socket->socketOption(QAbstractSocket::KeepAliveOption).toInt(); |
|
298 |
} |
|
299 |
return -1; |
|
300 |
} |
|
301 |
||
302 |
bool QHttpSocketEngine::setOption(SocketOption option, int value) |
|
303 |
{ |
|
304 |
Q_D(QHttpSocketEngine); |
|
305 |
if (d->socket) { |
|
306 |
// convert the enum and call the real socket |
|
307 |
if (option == QAbstractSocketEngine::LowDelayOption) |
|
308 |
d->socket->setSocketOption(QAbstractSocket::LowDelayOption, value); |
|
309 |
if (option == QAbstractSocketEngine::KeepAliveOption) |
|
310 |
d->socket->setSocketOption(QAbstractSocket::KeepAliveOption, value); |
|
311 |
return true; |
|
312 |
} |
|
313 |
return false; |
|
314 |
} |
|
315 |
||
316 |
/* |
|
317 |
Returns the difference between msecs and elapsed. If msecs is -1, |
|
318 |
however, -1 is returned. |
|
319 |
*/ |
|
320 |
static int qt_timeout_value(int msecs, int elapsed) |
|
321 |
{ |
|
322 |
if (msecs == -1) |
|
323 |
return -1; |
|
324 |
||
325 |
int timeout = msecs - elapsed; |
|
326 |
return timeout < 0 ? 0 : timeout; |
|
327 |
} |
|
328 |
||
329 |
bool QHttpSocketEngine::waitForRead(int msecs, bool *timedOut) |
|
330 |
{ |
|
331 |
Q_D(const QHttpSocketEngine); |
|
332 |
||
333 |
if (!d->socket || d->socket->state() == QAbstractSocket::UnconnectedState) |
|
334 |
return false; |
|
335 |
||
336 |
QTime stopWatch; |
|
337 |
stopWatch.start(); |
|
338 |
||
339 |
// Wait for more data if nothing is available. |
|
340 |
if (!d->socket->bytesAvailable()) { |
|
341 |
if (!d->socket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed()))) { |
|
342 |
if (d->socket->state() == QAbstractSocket::UnconnectedState) |
|
343 |
return true; |
|
344 |
setError(d->socket->error(), d->socket->errorString()); |
|
345 |
if (timedOut && d->socket->error() == QAbstractSocket::SocketTimeoutError) |
|
346 |
*timedOut = true; |
|
347 |
return false; |
|
348 |
} |
|
349 |
} |
|
350 |
||
351 |
// If we're not connected yet, wait until we are, or until an error |
|
352 |
// occurs. |
|
353 |
while (d->state != Connected && d->socket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed()))) { |
|
354 |
// Loop while the protocol handshake is taking place. |
|
355 |
} |
|
356 |
||
357 |
// Report any error that may occur. |
|
358 |
if (d->state != Connected) { |
|
359 |
setError(d->socket->error(), d->socket->errorString()); |
|
360 |
if (timedOut && d->socket->error() == QAbstractSocket::SocketTimeoutError) |
|
361 |
*timedOut = true; |
|
362 |
return false; |
|
363 |
} |
|
364 |
return true; |
|
365 |
} |
|
366 |
||
367 |
bool QHttpSocketEngine::waitForWrite(int msecs, bool *timedOut) |
|
368 |
{ |
|
369 |
Q_D(const QHttpSocketEngine); |
|
370 |
||
371 |
// If we're connected, just forward the call. |
|
372 |
if (d->state == Connected) { |
|
373 |
if (d->socket->bytesToWrite()) { |
|
374 |
if (!d->socket->waitForBytesWritten(msecs)) { |
|
375 |
if (d->socket->error() == QAbstractSocket::SocketTimeoutError && timedOut) |
|
376 |
*timedOut = true; |
|
377 |
return false; |
|
378 |
} |
|
379 |
} |
|
380 |
return true; |
|
381 |
} |
|
382 |
||
383 |
QTime stopWatch; |
|
384 |
stopWatch.start(); |
|
385 |
||
386 |
// If we're not connected yet, wait until we are, and until bytes have |
|
387 |
// been received (i.e., the socket has connected, we have sent the |
|
388 |
// greeting, and then received the response). |
|
389 |
while (d->state != Connected && d->socket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed()))) { |
|
390 |
// Loop while the protocol handshake is taking place. |
|
391 |
} |
|
392 |
||
393 |
// Report any error that may occur. |
|
394 |
if (d->state != Connected) { |
|
395 |
// setError(d->socket->error(), d->socket->errorString()); |
|
396 |
if (timedOut && d->socket->error() == QAbstractSocket::SocketTimeoutError) |
|
397 |
*timedOut = true; |
|
398 |
} |
|
399 |
||
400 |
return true; |
|
401 |
} |
|
402 |
||
403 |
bool QHttpSocketEngine::waitForReadOrWrite(bool *readyToRead, bool *readyToWrite, |
|
404 |
bool checkRead, bool checkWrite, |
|
405 |
int msecs, bool *timedOut) |
|
406 |
{ |
|
407 |
Q_UNUSED(checkRead); |
|
408 |
||
409 |
if (!checkWrite) { |
|
410 |
// Not interested in writing? Then we wait for read notifications. |
|
411 |
bool canRead = waitForRead(msecs, timedOut); |
|
412 |
if (readyToRead) |
|
413 |
*readyToRead = canRead; |
|
414 |
return canRead; |
|
415 |
} |
|
416 |
||
417 |
// Interested in writing? Then we wait for write notifications. |
|
418 |
bool canWrite = waitForWrite(msecs, timedOut); |
|
419 |
if (readyToWrite) |
|
420 |
*readyToWrite = canWrite; |
|
421 |
return canWrite; |
|
422 |
} |
|
423 |
||
424 |
bool QHttpSocketEngine::isReadNotificationEnabled() const |
|
425 |
{ |
|
426 |
Q_D(const QHttpSocketEngine); |
|
427 |
return d->readNotificationEnabled; |
|
428 |
} |
|
429 |
||
430 |
void QHttpSocketEngine::setReadNotificationEnabled(bool enable) |
|
431 |
{ |
|
432 |
Q_D(QHttpSocketEngine); |
|
433 |
if (d->readNotificationEnabled == enable) |
|
434 |
return; |
|
435 |
||
436 |
d->readNotificationEnabled = enable; |
|
437 |
if (enable) { |
|
438 |
// Enabling read notification can trigger a notification. |
|
439 |
if (bytesAvailable()) |
|
440 |
slotSocketReadNotification(); |
|
441 |
} |
|
442 |
} |
|
443 |
||
444 |
bool QHttpSocketEngine::isWriteNotificationEnabled() const |
|
445 |
{ |
|
446 |
Q_D(const QHttpSocketEngine); |
|
447 |
return d->writeNotificationEnabled; |
|
448 |
} |
|
449 |
||
450 |
void QHttpSocketEngine::setWriteNotificationEnabled(bool enable) |
|
451 |
{ |
|
452 |
Q_D(QHttpSocketEngine); |
|
453 |
d->writeNotificationEnabled = enable; |
|
454 |
if (enable && d->state == Connected && d->socket->state() == QAbstractSocket::ConnectedState) |
|
455 |
QMetaObject::invokeMethod(this, "writeNotification", Qt::QueuedConnection); |
|
456 |
} |
|
457 |
||
458 |
bool QHttpSocketEngine::isExceptionNotificationEnabled() const |
|
459 |
{ |
|
460 |
Q_D(const QHttpSocketEngine); |
|
461 |
return d->exceptNotificationEnabled; |
|
462 |
} |
|
463 |
||
464 |
void QHttpSocketEngine::setExceptionNotificationEnabled(bool enable) |
|
465 |
{ |
|
466 |
Q_D(QHttpSocketEngine); |
|
467 |
d->exceptNotificationEnabled = enable; |
|
468 |
} |
|
469 |
||
470 |
void QHttpSocketEngine::slotSocketConnected() |
|
471 |
{ |
|
472 |
Q_D(QHttpSocketEngine); |
|
473 |
||
474 |
// Send the greeting. |
|
475 |
const char method[] = "CONNECT "; |
|
476 |
QByteArray peerAddress = d->peerName.isEmpty() ? |
|
477 |
d->peerAddress.toString().toLatin1() : |
|
478 |
QUrl::toAce(d->peerName); |
|
479 |
QByteArray path = peerAddress + ':' + QByteArray::number(d->peerPort); |
|
480 |
QByteArray data = method; |
|
481 |
data += path; |
|
482 |
data += " HTTP/1.1\r\n"; |
|
483 |
data += "Proxy-Connection: keep-alive\r\n" |
|
484 |
"User-Agent: Mozilla/5.0\r\n" |
|
485 |
"Host: " + peerAddress + "\r\n"; |
|
486 |
QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(d->authenticator); |
|
487 |
//qDebug() << "slotSocketConnected: priv=" << priv << (priv ? (int)priv->method : -1); |
|
488 |
if (priv && priv->method != QAuthenticatorPrivate::None) { |
|
489 |
data += "Proxy-Authorization: " + priv->calculateResponse(method, path); |
|
490 |
data += "\r\n"; |
|
491 |
} |
|
492 |
data += "\r\n"; |
|
493 |
// qDebug() << ">>>>>>>> sending request" << this; |
|
494 |
// qDebug() << data; |
|
495 |
// qDebug() << ">>>>>>>"; |
|
496 |
d->socket->write(data); |
|
497 |
d->state = ConnectSent; |
|
498 |
} |
|
499 |
||
500 |
void QHttpSocketEngine::slotSocketDisconnected() |
|
501 |
{ |
|
502 |
} |
|
503 |
||
504 |
void QHttpSocketEngine::slotSocketReadNotification() |
|
505 |
{ |
|
506 |
Q_D(QHttpSocketEngine); |
|
507 |
if (d->state != Connected && d->socket->bytesAvailable() == 0) |
|
508 |
return; |
|
509 |
||
510 |
if (d->state == Connected) { |
|
511 |
// Forward as a read notification. |
|
512 |
if (d->readNotificationEnabled) |
|
513 |
emitReadNotification(); |
|
514 |
return; |
|
515 |
} |
|
516 |
||
517 |
readResponseContent: |
|
518 |
if (d->state == ReadResponseContent) { |
|
519 |
char dummybuffer[4096]; |
|
520 |
while (d->pendingResponseData) { |
|
521 |
int read = d->socket->read(dummybuffer, qMin(sizeof(dummybuffer), (size_t)d->pendingResponseData)); |
|
522 |
if (read >= 0) |
|
523 |
dummybuffer[read] = 0; |
|
524 |
||
525 |
if (read == 0) |
|
526 |
return; |
|
527 |
if (read == -1) { |
|
528 |
d->socket->disconnectFromHost(); |
|
529 |
emitWriteNotification(); |
|
530 |
return; |
|
531 |
} |
|
532 |
d->pendingResponseData -= read; |
|
533 |
} |
|
534 |
if (d->pendingResponseData > 0) |
|
535 |
return; |
|
536 |
d->state = SendAuthentication; |
|
537 |
slotSocketConnected(); |
|
538 |
return; |
|
539 |
} |
|
540 |
||
541 |
// Still in handshake mode. Wait until we've got a full response. |
|
542 |
bool done = false; |
|
543 |
do { |
|
544 |
d->readBuffer += d->socket->readLine(); |
|
545 |
} while (!(done = d->readBuffer.endsWith("\r\n\r\n")) && d->socket->canReadLine()); |
|
546 |
||
547 |
if (!done) { |
|
548 |
// Wait for more. |
|
549 |
return; |
|
550 |
} |
|
551 |
||
552 |
if (!d->readBuffer.startsWith("HTTP/1.")) { |
|
553 |
// protocol error, this isn't HTTP |
|
554 |
d->readBuffer.clear(); |
|
555 |
d->socket->close(); |
|
556 |
setState(QAbstractSocket::UnconnectedState); |
|
557 |
setError(QAbstractSocket::ProxyProtocolError, tr("Did not receive HTTP response from proxy")); |
|
558 |
emitConnectionNotification(); |
|
559 |
return; |
|
560 |
} |
|
561 |
||
562 |
QHttpResponseHeader responseHeader(QString::fromLatin1(d->readBuffer)); |
|
563 |
d->readBuffer.clear(); |
|
564 |
||
565 |
int statusCode = responseHeader.statusCode(); |
|
566 |
if (statusCode == 200) { |
|
567 |
d->state = Connected; |
|
568 |
setLocalAddress(d->socket->localAddress()); |
|
569 |
setLocalPort(d->socket->localPort()); |
|
570 |
setState(QAbstractSocket::ConnectedState); |
|
571 |
} else if (statusCode == 407) { |
|
572 |
if (d->authenticator.isNull()) |
|
573 |
d->authenticator.detach(); |
|
574 |
QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(d->authenticator); |
|
575 |
||
576 |
priv->parseHttpResponse(responseHeader, true); |
|
577 |
||
578 |
if (priv->phase == QAuthenticatorPrivate::Invalid) { |
|
579 |
// problem parsing the reply |
|
580 |
d->socket->close(); |
|
581 |
setState(QAbstractSocket::UnconnectedState); |
|
582 |
setError(QAbstractSocket::ProxyProtocolError, tr("Error parsing authentication request from proxy")); |
|
583 |
emitConnectionNotification(); |
|
584 |
return; |
|
585 |
} |
|
586 |
||
587 |
bool willClose; |
|
588 |
QString proxyConnectionHeader = responseHeader.value(QLatin1String("Proxy-Connection")); |
|
589 |
proxyConnectionHeader = proxyConnectionHeader.toLower(); |
|
590 |
if (proxyConnectionHeader == QLatin1String("close")) { |
|
591 |
willClose = true; |
|
592 |
} else if (proxyConnectionHeader == QLatin1String("keep-alive")) { |
|
593 |
willClose = false; |
|
594 |
} else { |
|
595 |
// no Proxy-Connection header, so use the default |
|
596 |
// HTTP 1.1's default behaviour is to keep persistent connections |
|
597 |
// HTTP 1.0 or earlier, so we expect the server to close |
|
598 |
willClose = (responseHeader.majorVersion() * 0x100 + responseHeader.minorVersion()) <= 0x0100; |
|
599 |
} |
|
600 |
||
601 |
if (willClose) { |
|
602 |
// the server will disconnect, so let's avoid receiving an error |
|
603 |
// especially since the signal below may trigger a new event loop |
|
604 |
d->socket->disconnectFromHost(); |
|
605 |
d->socket->readAll(); |
|
606 |
} |
|
607 |
||
608 |
if (priv->phase == QAuthenticatorPrivate::Done) |
|
609 |
emit proxyAuthenticationRequired(d->proxy, &d->authenticator); |
|
610 |
||
611 |
// priv->phase will get reset to QAuthenticatorPrivate::Start if the authenticator got modified in the signal above. |
|
612 |
if (priv->phase == QAuthenticatorPrivate::Done) { |
|
613 |
setError(QAbstractSocket::ProxyAuthenticationRequiredError, tr("Authentication required")); |
|
614 |
d->socket->disconnectFromHost(); |
|
615 |
} else { |
|
616 |
// close the connection if it isn't already and reconnect using the chosen authentication method |
|
617 |
d->state = SendAuthentication; |
|
618 |
if (willClose) { |
|
619 |
d->socket->connectToHost(d->proxy.hostName(), d->proxy.port()); |
|
620 |
} else { |
|
621 |
bool ok; |
|
622 |
int contentLength = responseHeader.value(QLatin1String("Content-Length")).toInt(&ok); |
|
623 |
if (ok && contentLength > 0) { |
|
624 |
d->state = ReadResponseContent; |
|
625 |
d->pendingResponseData = contentLength; |
|
626 |
goto readResponseContent; |
|
627 |
} else { |
|
628 |
d->state = SendAuthentication; |
|
629 |
slotSocketConnected(); |
|
630 |
} |
|
631 |
} |
|
632 |
return; |
|
633 |
} |
|
634 |
} else { |
|
635 |
d->socket->close(); |
|
636 |
setState(QAbstractSocket::UnconnectedState); |
|
637 |
if (statusCode == 403 || statusCode == 405) { |
|
638 |
// 403 Forbidden |
|
639 |
// 405 Method Not Allowed |
|
640 |
setError(QAbstractSocket::SocketAccessError, tr("Proxy denied connection")); |
|
641 |
} else if (statusCode == 404) { |
|
642 |
// 404 Not Found: host lookup error |
|
643 |
setError(QAbstractSocket::HostNotFoundError, QAbstractSocket::tr("Host not found")); |
|
644 |
} else if (statusCode == 503) { |
|
645 |
// 503 Service Unavailable: Connection Refused |
|
646 |
setError(QAbstractSocket::ConnectionRefusedError, QAbstractSocket::tr("Connection refused")); |
|
647 |
} else { |
|
648 |
// Some other reply |
|
649 |
//qWarning("UNEXPECTED RESPONSE: [%s]", responseHeader.toString().toLatin1().data()); |
|
650 |
setError(QAbstractSocket::ProxyProtocolError, tr("Error communicating with HTTP proxy")); |
|
651 |
} |
|
652 |
} |
|
653 |
||
654 |
// The handshake is done; notify that we're connected (or failed to connect) |
|
655 |
emitConnectionNotification(); |
|
656 |
} |
|
657 |
||
658 |
void QHttpSocketEngine::slotSocketBytesWritten() |
|
659 |
{ |
|
660 |
Q_D(QHttpSocketEngine); |
|
661 |
if (d->state == Connected && d->writeNotificationEnabled) |
|
662 |
emitWriteNotification(); |
|
663 |
} |
|
664 |
||
665 |
void QHttpSocketEngine::slotSocketError(QAbstractSocket::SocketError error) |
|
666 |
{ |
|
667 |
Q_D(QHttpSocketEngine); |
|
668 |
d->readBuffer.clear(); |
|
669 |
||
670 |
if (d->state != Connected) { |
|
671 |
// we are in proxy handshaking stages |
|
672 |
if (error == QAbstractSocket::HostNotFoundError) |
|
673 |
setError(QAbstractSocket::ProxyNotFoundError, tr("Proxy server not found")); |
|
674 |
else if (error == QAbstractSocket::ConnectionRefusedError) |
|
675 |
setError(QAbstractSocket::ProxyConnectionRefusedError, tr("Proxy connection refused")); |
|
676 |
else if (error == QAbstractSocket::SocketTimeoutError) |
|
677 |
setError(QAbstractSocket::ProxyConnectionTimeoutError, tr("Proxy server connection timed out")); |
|
678 |
else if (error == QAbstractSocket::RemoteHostClosedError) |
|
679 |
setError(QAbstractSocket::ProxyConnectionClosedError, tr("Proxy connection closed prematurely")); |
|
680 |
else |
|
681 |
setError(error, d->socket->errorString()); |
|
682 |
emitConnectionNotification(); |
|
683 |
return; |
|
684 |
} |
|
685 |
||
686 |
// We're connected |
|
687 |
if (error == QAbstractSocket::SocketTimeoutError) |
|
688 |
return; // ignore this error |
|
689 |
||
690 |
d->state = None; |
|
691 |
setError(error, d->socket->errorString()); |
|
692 |
if (error == QAbstractSocket::RemoteHostClosedError) { |
|
693 |
emitReadNotification(); |
|
694 |
} else { |
|
695 |
qDebug() << "QHttpSocketEngine::slotSocketError: got weird error =" << error; |
|
696 |
} |
|
697 |
} |
|
698 |
||
699 |
void QHttpSocketEngine::slotSocketStateChanged(QAbstractSocket::SocketState state) |
|
700 |
{ |
|
701 |
Q_UNUSED(state); |
|
702 |
} |
|
703 |
||
704 |
void QHttpSocketEngine::emitPendingReadNotification() |
|
705 |
{ |
|
706 |
Q_D(QHttpSocketEngine); |
|
707 |
d->readNotificationPending = false; |
|
708 |
if (d->readNotificationEnabled) |
|
709 |
emit readNotification(); |
|
710 |
} |
|
711 |
||
712 |
void QHttpSocketEngine::emitPendingWriteNotification() |
|
713 |
{ |
|
714 |
Q_D(QHttpSocketEngine); |
|
715 |
d->writeNotificationPending = false; |
|
716 |
if (d->writeNotificationEnabled) |
|
717 |
emit writeNotification(); |
|
718 |
} |
|
719 |
||
720 |
void QHttpSocketEngine::emitPendingConnectionNotification() |
|
721 |
{ |
|
722 |
Q_D(QHttpSocketEngine); |
|
723 |
d->connectionNotificationPending = false; |
|
724 |
emit connectionNotification(); |
|
725 |
} |
|
726 |
||
727 |
void QHttpSocketEngine::emitReadNotification() |
|
728 |
{ |
|
729 |
Q_D(QHttpSocketEngine); |
|
730 |
d->readNotificationActivated = true; |
|
731 |
if (d->readNotificationEnabled && !d->readNotificationPending) { |
|
732 |
d->readNotificationPending = true; |
|
733 |
QMetaObject::invokeMethod(this, "emitPendingReadNotification", Qt::QueuedConnection); |
|
734 |
} |
|
735 |
} |
|
736 |
||
737 |
void QHttpSocketEngine::emitWriteNotification() |
|
738 |
{ |
|
739 |
Q_D(QHttpSocketEngine); |
|
740 |
d->writeNotificationActivated = true; |
|
741 |
if (d->writeNotificationEnabled && !d->writeNotificationPending) { |
|
742 |
d->writeNotificationPending = true; |
|
743 |
QMetaObject::invokeMethod(this, "emitPendingWriteNotification", Qt::QueuedConnection); |
|
744 |
} |
|
745 |
} |
|
746 |
||
747 |
void QHttpSocketEngine::emitConnectionNotification() |
|
748 |
{ |
|
749 |
Q_D(QHttpSocketEngine); |
|
750 |
if (!d->connectionNotificationPending) { |
|
751 |
d->connectionNotificationPending = true; |
|
752 |
QMetaObject::invokeMethod(this, "emitPendingConnectionNotification", Qt::QueuedConnection); |
|
753 |
} |
|
754 |
} |
|
755 |
||
756 |
QHttpSocketEnginePrivate::QHttpSocketEnginePrivate() |
|
757 |
: readNotificationEnabled(false) |
|
758 |
, writeNotificationEnabled(false) |
|
759 |
, exceptNotificationEnabled(false) |
|
760 |
, readNotificationActivated(false) |
|
761 |
, writeNotificationActivated(false) |
|
762 |
, readNotificationPending(false) |
|
763 |
, writeNotificationPending(false) |
|
764 |
, connectionNotificationPending(false) |
|
765 |
, pendingResponseData(0) |
|
766 |
{ |
|
767 |
socket = 0; |
|
768 |
state = QHttpSocketEngine::None; |
|
769 |
} |
|
770 |
||
771 |
QHttpSocketEnginePrivate::~QHttpSocketEnginePrivate() |
|
772 |
{ |
|
773 |
} |
|
774 |
||
775 |
QAbstractSocketEngine *QHttpSocketEngineHandler::createSocketEngine(QAbstractSocket::SocketType socketType, |
|
776 |
const QNetworkProxy &proxy, |
|
777 |
QObject *parent) |
|
778 |
{ |
|
779 |
if (socketType != QAbstractSocket::TcpSocket) |
|
780 |
return 0; |
|
781 |
||
782 |
// proxy type must have been resolved by now |
|
783 |
if (proxy.type() != QNetworkProxy::HttpProxy) |
|
784 |
return 0; |
|
785 |
||
786 |
// we only accept active sockets |
|
787 |
if (!qobject_cast<QAbstractSocket *>(parent)) |
|
788 |
return 0; |
|
789 |
||
790 |
QHttpSocketEngine *engine = new QHttpSocketEngine(parent); |
|
791 |
engine->setProxy(proxy); |
|
792 |
return engine; |
|
793 |
} |
|
794 |
||
795 |
QAbstractSocketEngine *QHttpSocketEngineHandler::createSocketEngine(int, QObject *) |
|
796 |
{ |
|
797 |
return 0; |
|
798 |
} |
|
799 |
||
800 |
QT_END_NAMESPACE |
|
801 |
||
802 |
#endif |