/src/qtbase/src/network/socket/qhttpsocketengine.cpp
Line | Count | Source |
1 | | // Copyright (C) 2016 The Qt Company Ltd. |
2 | | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | | // Qt-Security score:critical reason:network-protocol |
4 | | |
5 | | #include "qhttpsocketengine_p.h" |
6 | | #include "qtcpsocket.h" |
7 | | #include "qhostaddress.h" |
8 | | #include "qurl.h" |
9 | | #include "private/qhttpnetworkreply_p.h" |
10 | | #include "private/qiodevice_p.h" |
11 | | #include "qdeadlinetimer.h" |
12 | | #include "qnetworkinterface.h" |
13 | | |
14 | | #if !defined(QT_NO_NETWORKPROXY) |
15 | | #include <qdebug.h> |
16 | | |
17 | | QT_BEGIN_NAMESPACE |
18 | | |
19 | | using namespace Qt::StringLiterals; |
20 | | |
21 | | #define DEBUG |
22 | | |
23 | | QHttpSocketEngine::QHttpSocketEngine(QObject *parent) |
24 | 0 | : QAbstractSocketEngine(*new QHttpSocketEnginePrivate, parent) |
25 | 0 | { |
26 | 0 | } |
27 | | |
28 | | QHttpSocketEngine::~QHttpSocketEngine() |
29 | 0 | { |
30 | 0 | } |
31 | | |
32 | | bool QHttpSocketEngine::initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol) |
33 | 0 | { |
34 | 0 | Q_D(QHttpSocketEngine); |
35 | 0 | if (type != QAbstractSocket::TcpSocket) |
36 | 0 | return false; |
37 | | |
38 | 0 | setProtocol(protocol); |
39 | 0 | setSocketType(type); |
40 | 0 | d->socket = new QTcpSocket(this); |
41 | 0 | d->reply = new QHttpNetworkReply(QUrl(), this); |
42 | | |
43 | | // Explicitly disable proxying on the proxy socket itself to avoid |
44 | | // unwanted recursion. |
45 | 0 | d->socket->setProxy(QNetworkProxy::NoProxy); |
46 | | |
47 | | // Intercept all the signals. |
48 | 0 | connect(d->socket, SIGNAL(connected()), |
49 | 0 | this, SLOT(slotSocketConnected()), |
50 | 0 | Qt::DirectConnection); |
51 | 0 | connect(d->socket, SIGNAL(disconnected()), |
52 | 0 | this, SLOT(slotSocketDisconnected()), |
53 | 0 | Qt::DirectConnection); |
54 | 0 | connect(d->socket, SIGNAL(readyRead()), |
55 | 0 | this, SLOT(slotSocketReadNotification()), |
56 | 0 | Qt::DirectConnection); |
57 | 0 | connect(d->socket, SIGNAL(bytesWritten(qint64)), |
58 | 0 | this, SLOT(slotSocketBytesWritten()), |
59 | 0 | Qt::DirectConnection); |
60 | 0 | connect(d->socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), |
61 | 0 | this, SLOT(slotSocketError(QAbstractSocket::SocketError)), |
62 | 0 | Qt::DirectConnection); |
63 | 0 | connect(d->socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), |
64 | 0 | this, SLOT(slotSocketStateChanged(QAbstractSocket::SocketState)), |
65 | 0 | Qt::DirectConnection); |
66 | |
|
67 | 0 | return true; |
68 | 0 | } |
69 | | |
70 | | bool QHttpSocketEngine::initialize(qintptr, QAbstractSocket::SocketState) |
71 | 0 | { |
72 | 0 | return false; |
73 | 0 | } |
74 | | |
75 | | void QHttpSocketEngine::setProxy(const QNetworkProxy &proxy) |
76 | 0 | { |
77 | 0 | Q_D(QHttpSocketEngine); |
78 | 0 | d->proxy = proxy; |
79 | 0 | QString user = proxy.user(); |
80 | 0 | if (!user.isEmpty()) |
81 | 0 | d->authenticator.setUser(user); |
82 | 0 | QString password = proxy.password(); |
83 | 0 | if (!password.isEmpty()) |
84 | 0 | d->authenticator.setPassword(password); |
85 | 0 | } |
86 | | |
87 | | qintptr QHttpSocketEngine::socketDescriptor() const |
88 | 0 | { |
89 | 0 | Q_D(const QHttpSocketEngine); |
90 | 0 | return d->socket ? d->socket->socketDescriptor() : -1; |
91 | 0 | } |
92 | | |
93 | | bool QHttpSocketEngine::isValid() const |
94 | 0 | { |
95 | 0 | Q_D(const QHttpSocketEngine); |
96 | 0 | return d->socket; |
97 | 0 | } |
98 | | |
99 | | bool QHttpSocketEngine::connectInternal() |
100 | 0 | { |
101 | 0 | Q_D(QHttpSocketEngine); |
102 | |
|
103 | 0 | d->credentialsSent = false; |
104 | | |
105 | | // If the handshake is done, enter ConnectedState state and return true. |
106 | 0 | if (d->state == Connected) { |
107 | 0 | qWarning("QHttpSocketEngine::connectToHost: called when already connected"); |
108 | 0 | setState(QAbstractSocket::ConnectedState); |
109 | 0 | return true; |
110 | 0 | } |
111 | | |
112 | 0 | if (d->state == ConnectSent && d->socketState != QAbstractSocket::ConnectedState) |
113 | 0 | setState(QAbstractSocket::UnconnectedState); |
114 | | |
115 | | // Handshake isn't done. If unconnected, start connecting. |
116 | 0 | if (d->state == None && d->socket->state() == QAbstractSocket::UnconnectedState) { |
117 | 0 | setState(QAbstractSocket::ConnectingState); |
118 | | //limit buffer in internal socket, data is buffered in the external socket under application control |
119 | 0 | d->socket->setReadBufferSize(65536); |
120 | 0 | d->socket->connectToHost(d->proxy.hostName(), d->proxy.port()); |
121 | 0 | } |
122 | | |
123 | | // If connected (might happen right away, at least for localhost services |
124 | | // on some BSD systems), there might already be bytes available. |
125 | 0 | if (bytesAvailable()) |
126 | 0 | slotSocketReadNotification(); |
127 | |
|
128 | 0 | return d->socketState == QAbstractSocket::ConnectedState; |
129 | 0 | } |
130 | | |
131 | | bool QHttpSocketEngine::connectToHost(const QHostAddress &address, quint16 port) |
132 | 0 | { |
133 | 0 | Q_D(QHttpSocketEngine); |
134 | |
|
135 | 0 | setPeerAddress(address); |
136 | 0 | setPeerPort(port); |
137 | 0 | d->peerName.clear(); |
138 | |
|
139 | 0 | return connectInternal(); |
140 | 0 | } |
141 | | |
142 | | bool QHttpSocketEngine::connectToHostByName(const QString &hostname, quint16 port) |
143 | 0 | { |
144 | 0 | Q_D(QHttpSocketEngine); |
145 | |
|
146 | 0 | setPeerAddress(QHostAddress()); |
147 | 0 | setPeerPort(port); |
148 | 0 | d->peerName = hostname; |
149 | |
|
150 | 0 | return connectInternal(); |
151 | 0 | } |
152 | | |
153 | | bool QHttpSocketEngine::bind(const QHostAddress &, quint16) |
154 | 0 | { |
155 | 0 | qWarning("Operation is not supported"); |
156 | 0 | setError(QAbstractSocket::UnsupportedSocketOperationError, "Unsupported socket operation"_L1); |
157 | 0 | return false; |
158 | 0 | } |
159 | | |
160 | | bool QHttpSocketEngine::listen(int backlog) |
161 | 0 | { |
162 | 0 | Q_UNUSED(backlog); |
163 | 0 | qWarning("Operation is not supported"); |
164 | 0 | setError(QAbstractSocket::UnsupportedSocketOperationError, "Unsupported socket operation"_L1); |
165 | 0 | return false; |
166 | 0 | } |
167 | | |
168 | | qintptr QHttpSocketEngine::accept() |
169 | 0 | { |
170 | 0 | qWarning("Operation is not supported"); |
171 | 0 | setError(QAbstractSocket::UnsupportedSocketOperationError, "Unsupported socket operation"_L1); |
172 | 0 | return -1; |
173 | 0 | } |
174 | | |
175 | | void QHttpSocketEngine::close() |
176 | 0 | { |
177 | 0 | Q_D(QHttpSocketEngine); |
178 | 0 | if (d->socket) { |
179 | 0 | d->socket->close(); |
180 | 0 | delete d->socket; |
181 | 0 | d->socket = nullptr; |
182 | 0 | } |
183 | 0 | } |
184 | | |
185 | | qint64 QHttpSocketEngine::bytesAvailable() const |
186 | 0 | { |
187 | 0 | Q_D(const QHttpSocketEngine); |
188 | 0 | return d->socket ? d->socket->bytesAvailable() : 0; |
189 | 0 | } |
190 | | |
191 | | qint64 QHttpSocketEngine::read(char *data, qint64 maxlen) |
192 | 0 | { |
193 | 0 | Q_D(QHttpSocketEngine); |
194 | 0 | qint64 bytesRead = d->socket->read(data, maxlen); |
195 | |
|
196 | 0 | if (d->socket->state() == QAbstractSocket::UnconnectedState |
197 | 0 | && d->socket->bytesAvailable() == 0) { |
198 | 0 | emitReadNotification(); |
199 | 0 | } |
200 | |
|
201 | 0 | if (bytesRead == -1) { |
202 | | // If nothing has been read so far, and the direct socket read |
203 | | // failed, return the socket's error. Otherwise, fall through and |
204 | | // return as much as we read so far. |
205 | 0 | close(); |
206 | 0 | setError(QAbstractSocket::RemoteHostClosedError, "Remote host closed"_L1); |
207 | 0 | setState(QAbstractSocket::UnconnectedState); |
208 | 0 | return -1; |
209 | 0 | } |
210 | 0 | return bytesRead; |
211 | 0 | } |
212 | | |
213 | | qint64 QHttpSocketEngine::write(const char *data, qint64 len) |
214 | 0 | { |
215 | 0 | Q_D(QHttpSocketEngine); |
216 | 0 | return d->socket->write(data, len); |
217 | 0 | } |
218 | | |
219 | | #ifndef QT_NO_UDPSOCKET |
220 | | #ifndef QT_NO_NETWORKINTERFACE |
221 | | bool QHttpSocketEngine::joinMulticastGroup(const QHostAddress &, |
222 | | const QNetworkInterface &) |
223 | 0 | { |
224 | 0 | qWarning("Operation is not supported"); |
225 | 0 | setError(QAbstractSocket::UnsupportedSocketOperationError, "Unsupported socket operation"_L1); |
226 | 0 | return false; |
227 | 0 | } |
228 | | |
229 | | bool QHttpSocketEngine::leaveMulticastGroup(const QHostAddress &, |
230 | | const QNetworkInterface &) |
231 | 0 | { |
232 | 0 | qWarning("Operation is not supported"); |
233 | 0 | setError(QAbstractSocket::UnsupportedSocketOperationError, "Unsupported socket operation"_L1); |
234 | 0 | return false; |
235 | 0 | } |
236 | | |
237 | | QNetworkInterface QHttpSocketEngine::multicastInterface() const |
238 | 0 | { |
239 | 0 | return QNetworkInterface(); |
240 | 0 | } |
241 | | |
242 | | bool QHttpSocketEngine::setMulticastInterface(const QNetworkInterface &) |
243 | 0 | { |
244 | 0 | qWarning("Operation is not supported"); |
245 | 0 | setError(QAbstractSocket::UnsupportedSocketOperationError, "Unsupported socket operation"_L1); |
246 | 0 | return false; |
247 | 0 | } |
248 | | #endif // QT_NO_NETWORKINTERFACE |
249 | | |
250 | | bool QHttpSocketEngine::hasPendingDatagrams() const |
251 | 0 | { |
252 | 0 | qWarning("Operation is not supported"); |
253 | 0 | return false; |
254 | 0 | } |
255 | | |
256 | | qint64 QHttpSocketEngine::pendingDatagramSize() const |
257 | 0 | { |
258 | 0 | qWarning("Operation is not supported"); |
259 | 0 | return -1; |
260 | 0 | } |
261 | | #endif // QT_NO_UDPSOCKET |
262 | | |
263 | | qint64 QHttpSocketEngine::readDatagram(char *, qint64, QIpPacketHeader *, PacketHeaderOptions) |
264 | 0 | { |
265 | 0 | qWarning("Operation is not supported"); |
266 | 0 | setError(QAbstractSocket::UnsupportedSocketOperationError, "Unsupported socket operation"_L1); |
267 | 0 | return -1; |
268 | 0 | } |
269 | | |
270 | | qint64 QHttpSocketEngine::writeDatagram(const char *, qint64, const QIpPacketHeader &) |
271 | 0 | { |
272 | 0 | qWarning("Operation is not supported"); |
273 | 0 | setError(QAbstractSocket::UnsupportedSocketOperationError, "Unsupported socket operation"_L1); |
274 | 0 | return -1; |
275 | 0 | } |
276 | | |
277 | | qint64 QHttpSocketEngine::bytesToWrite() const |
278 | 0 | { |
279 | 0 | Q_D(const QHttpSocketEngine); |
280 | 0 | if (d->socket) { |
281 | 0 | return d->socket->bytesToWrite(); |
282 | 0 | } else { |
283 | 0 | return 0; |
284 | 0 | } |
285 | 0 | } |
286 | | |
287 | | int QHttpSocketEngine::option(SocketOption option) const |
288 | 0 | { |
289 | 0 | Q_D(const QHttpSocketEngine); |
290 | 0 | if (d->socket) { |
291 | | // convert the enum and call the real socket |
292 | 0 | if (option == QAbstractSocketEngine::LowDelayOption) |
293 | 0 | return d->socket->socketOption(QAbstractSocket::LowDelayOption).toInt(); |
294 | 0 | if (option == QAbstractSocketEngine::KeepAliveOption) |
295 | 0 | return d->socket->socketOption(QAbstractSocket::KeepAliveOption).toInt(); |
296 | 0 | } |
297 | 0 | return -1; |
298 | 0 | } |
299 | | |
300 | | bool QHttpSocketEngine::setOption(SocketOption option, int value) |
301 | 0 | { |
302 | 0 | Q_D(QHttpSocketEngine); |
303 | 0 | if (d->socket) { |
304 | | // convert the enum and call the real socket |
305 | 0 | if (option == QAbstractSocketEngine::LowDelayOption) |
306 | 0 | d->socket->setSocketOption(QAbstractSocket::LowDelayOption, value); |
307 | 0 | if (option == QAbstractSocketEngine::KeepAliveOption) |
308 | 0 | d->socket->setSocketOption(QAbstractSocket::KeepAliveOption, value); |
309 | 0 | return true; |
310 | 0 | } |
311 | 0 | return false; |
312 | 0 | } |
313 | | |
314 | | bool QHttpSocketEngine::waitForRead(QDeadlineTimer deadline, bool *timedOut) |
315 | 0 | { |
316 | 0 | Q_D(const QHttpSocketEngine); |
317 | |
|
318 | 0 | if (!d->socket || d->socket->state() == QAbstractSocket::UnconnectedState) |
319 | 0 | return false; |
320 | | |
321 | | // Wait for more data if nothing is available. |
322 | 0 | if (!d->socket->bytesAvailable()) { |
323 | 0 | if (!d->socket->waitForReadyRead(deadline.remainingTime())) { |
324 | 0 | if (d->socket->state() == QAbstractSocket::UnconnectedState) |
325 | 0 | return true; |
326 | 0 | setError(d->socket->error(), d->socket->errorString()); |
327 | 0 | if (timedOut && d->socket->error() == QAbstractSocket::SocketTimeoutError) |
328 | 0 | *timedOut = true; |
329 | 0 | return false; |
330 | 0 | } |
331 | 0 | } |
332 | | |
333 | 0 | waitForProtocolHandshake(deadline); |
334 | | |
335 | | // Report any error that may occur. |
336 | 0 | if (d->state != Connected) { |
337 | 0 | setError(d->socket->error(), d->socket->errorString()); |
338 | 0 | if (timedOut && d->socket->error() == QAbstractSocket::SocketTimeoutError) |
339 | 0 | *timedOut = true; |
340 | 0 | return false; |
341 | 0 | } |
342 | 0 | return true; |
343 | 0 | } |
344 | | |
345 | | bool QHttpSocketEngine::waitForWrite(QDeadlineTimer deadline, bool *timedOut) |
346 | 0 | { |
347 | 0 | Q_D(const QHttpSocketEngine); |
348 | | |
349 | | // If we're connected, just forward the call. |
350 | 0 | if (d->state == Connected) { |
351 | 0 | if (d->socket->bytesToWrite()) { |
352 | 0 | if (!d->socket->waitForBytesWritten(deadline.remainingTime())) { |
353 | 0 | if (d->socket->error() == QAbstractSocket::SocketTimeoutError && timedOut) |
354 | 0 | *timedOut = true; |
355 | 0 | return false; |
356 | 0 | } |
357 | 0 | } |
358 | 0 | return true; |
359 | 0 | } |
360 | | |
361 | 0 | waitForProtocolHandshake(deadline); |
362 | | |
363 | | // Report any error that may occur. |
364 | 0 | if (d->state != Connected) { |
365 | | // setError(d->socket->error(), d->socket->errorString()); |
366 | 0 | if (timedOut && d->socket->error() == QAbstractSocket::SocketTimeoutError) |
367 | 0 | *timedOut = true; |
368 | 0 | } |
369 | |
|
370 | 0 | return true; |
371 | 0 | } |
372 | | |
373 | | bool QHttpSocketEngine::waitForReadOrWrite(bool *readyToRead, bool *readyToWrite, |
374 | | bool checkRead, bool checkWrite, |
375 | | QDeadlineTimer deadline, bool *timedOut) |
376 | 0 | { |
377 | 0 | Q_UNUSED(checkRead); |
378 | |
|
379 | 0 | if (!checkWrite) { |
380 | | // Not interested in writing? Then we wait for read notifications. |
381 | 0 | bool canRead = waitForRead(deadline, timedOut); |
382 | 0 | if (readyToRead) |
383 | 0 | *readyToRead = canRead; |
384 | 0 | return canRead; |
385 | 0 | } |
386 | | |
387 | | // Interested in writing? Then we wait for write notifications. |
388 | 0 | bool canWrite = waitForWrite(deadline, timedOut); |
389 | 0 | if (readyToWrite) |
390 | 0 | *readyToWrite = canWrite; |
391 | 0 | return canWrite; |
392 | 0 | } |
393 | | |
394 | | void QHttpSocketEngine::waitForProtocolHandshake(QDeadlineTimer deadline) const |
395 | 0 | { |
396 | 0 | Q_D(const QHttpSocketEngine); |
397 | | |
398 | | // If we're not connected yet, wait until we are (and until bytes have |
399 | | // been received, i.e. the socket has connected, we have sent the |
400 | | // greeting, and then received the response), or until an error occurs. |
401 | 0 | while (d->state != Connected && d->socket->waitForReadyRead(deadline.remainingTime())) { |
402 | | // Loop while the protocol handshake is taking place. |
403 | 0 | } |
404 | 0 | } |
405 | | |
406 | | bool QHttpSocketEngine::isReadNotificationEnabled() const |
407 | 0 | { |
408 | 0 | Q_D(const QHttpSocketEngine); |
409 | 0 | return d->readNotificationEnabled; |
410 | 0 | } |
411 | | |
412 | | void QHttpSocketEngine::setReadNotificationEnabled(bool enable) |
413 | 0 | { |
414 | 0 | Q_D(QHttpSocketEngine); |
415 | 0 | if (d->readNotificationEnabled == enable) { |
416 | | // When already enabled, we still need to check for |
417 | | // buffered data. The inner socket may have data in its user-space |
418 | | // buffer that won't trigger a new OS-level read notification. |
419 | | // Without this check, partial reads leave data stranded forever. |
420 | 0 | if (enable && bytesAvailable() > 0) |
421 | 0 | emitReadNotification(); |
422 | 0 | return; |
423 | 0 | } |
424 | | |
425 | 0 | d->readNotificationEnabled = enable; |
426 | 0 | if (enable) { |
427 | | // Enabling read notification can trigger a notification. |
428 | 0 | if (bytesAvailable()) { |
429 | 0 | slotSocketReadNotification(); |
430 | 0 | } else if (d->socket && d->socket->state() == QAbstractSocket::UnconnectedState) { |
431 | 0 | emitReadNotification(); |
432 | 0 | } |
433 | 0 | } |
434 | 0 | } |
435 | | |
436 | | bool QHttpSocketEngine::isWriteNotificationEnabled() const |
437 | 0 | { |
438 | 0 | Q_D(const QHttpSocketEngine); |
439 | 0 | return d->writeNotificationEnabled; |
440 | 0 | } |
441 | | |
442 | | void QHttpSocketEngine::setWriteNotificationEnabled(bool enable) |
443 | 0 | { |
444 | 0 | Q_D(QHttpSocketEngine); |
445 | 0 | d->writeNotificationEnabled = enable; |
446 | 0 | if (enable && d->state == Connected && d->socket->state() == QAbstractSocket::ConnectedState) |
447 | 0 | QMetaObject::invokeMethod(this, "writeNotification", Qt::QueuedConnection); |
448 | 0 | } |
449 | | |
450 | | bool QHttpSocketEngine::isExceptionNotificationEnabled() const |
451 | 0 | { |
452 | 0 | Q_D(const QHttpSocketEngine); |
453 | 0 | return d->exceptNotificationEnabled; |
454 | 0 | } |
455 | | |
456 | | void QHttpSocketEngine::setExceptionNotificationEnabled(bool enable) |
457 | 0 | { |
458 | 0 | Q_D(QHttpSocketEngine); |
459 | 0 | d->exceptNotificationEnabled = enable; |
460 | 0 | } |
461 | | |
462 | | void QHttpSocketEngine::slotSocketConnected() |
463 | 0 | { |
464 | 0 | Q_D(QHttpSocketEngine); |
465 | | |
466 | | // Send the greeting. |
467 | 0 | const char method[] = "CONNECT"; |
468 | 0 | QByteArray peerAddress = d->peerName.isEmpty() ? |
469 | 0 | d->peerAddress.toString().toLatin1() : |
470 | 0 | QUrl::toAce(d->peerName); |
471 | 0 | QByteArray path = peerAddress + ':' + QByteArray::number(d->peerPort); |
472 | 0 | QByteArray data = method; |
473 | 0 | data += ' '; |
474 | 0 | data += path; |
475 | 0 | data += " HTTP/1.1\r\n"; |
476 | 0 | data += "Proxy-Connection: keep-alive\r\n"; |
477 | 0 | data += "Host: " + peerAddress + "\r\n"; |
478 | 0 | const auto headers = d->proxy.headers(); |
479 | 0 | if (!headers.contains(QHttpHeaders::WellKnownHeader::UserAgent)) |
480 | 0 | data += "User-Agent: Mozilla/5.0\r\n"; |
481 | 0 | for (qsizetype i = 0; i < headers.size(); ++i) { |
482 | 0 | const auto name = headers.nameAt(i); |
483 | 0 | data += QByteArrayView(name.data(), name.size()) + ": " |
484 | 0 | + headers.valueAt(i) + "\r\n"; |
485 | 0 | } |
486 | 0 | QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(d->authenticator); |
487 | | //qDebug() << "slotSocketConnected: priv=" << priv << (priv ? (int)priv->method : -1); |
488 | 0 | if (priv && priv->method != QAuthenticatorPrivate::None) { |
489 | 0 | d->credentialsSent = true; |
490 | 0 | data += "Proxy-Authorization: " + priv->calculateResponse(method, path, d->proxy.hostName()); |
491 | 0 | data += "\r\n"; |
492 | 0 | } |
493 | 0 | data += "\r\n"; |
494 | | // qDebug() << ">>>>>>>> sending request" << this; |
495 | | // qDebug() << data; |
496 | | // qDebug(">>>>>>>"); |
497 | 0 | d->socket->write(data); |
498 | 0 | d->state = ConnectSent; |
499 | 0 | } |
500 | | |
501 | | void QHttpSocketEngine::slotSocketDisconnected() |
502 | 0 | { |
503 | 0 | } |
504 | | |
505 | | void QHttpSocketEngine::slotSocketReadNotification() |
506 | 0 | { |
507 | 0 | Q_D(QHttpSocketEngine); |
508 | 0 | if (d->state != Connected && d->socket->bytesAvailable() == 0) |
509 | 0 | return; |
510 | | |
511 | 0 | if (d->state == Connected) { |
512 | | // Forward as a read notification. |
513 | 0 | if (d->readNotificationEnabled) |
514 | 0 | emitReadNotification(); |
515 | 0 | return; |
516 | 0 | } |
517 | | |
518 | 0 | if (d->state == ConnectSent) { |
519 | 0 | d->reply->d_func()->state = QHttpNetworkReplyPrivate::NothingDoneState; |
520 | 0 | d->state = ReadResponseHeader; |
521 | 0 | } |
522 | |
|
523 | 0 | if (d->state == ReadResponseHeader) { |
524 | 0 | bool ok = readHttpHeader(); |
525 | 0 | if (!ok) { |
526 | | // protocol error, this isn't HTTP |
527 | 0 | d->socket->close(); |
528 | 0 | setState(QAbstractSocket::UnconnectedState); |
529 | 0 | setError(QAbstractSocket::ProxyProtocolError, tr("Did not receive HTTP response from proxy")); |
530 | 0 | emitConnectionNotification(); |
531 | 0 | return; |
532 | 0 | } |
533 | 0 | if (d->state == ReadResponseHeader) |
534 | 0 | return; // readHttpHeader() was not done yet, need to wait for more header data |
535 | 0 | } |
536 | | |
537 | 0 | if (d->state == ReadResponseContent) { |
538 | 0 | qint64 skipped = d->socket->skip(d->pendingResponseData); |
539 | 0 | if (skipped == -1) { |
540 | 0 | d->socket->disconnectFromHost(); |
541 | 0 | emitWriteNotification(); |
542 | 0 | return; |
543 | 0 | } |
544 | 0 | d->pendingResponseData -= uint(skipped); |
545 | 0 | if (d->pendingResponseData > 0) |
546 | 0 | return; |
547 | 0 | if (d->reply->statusCode() == 407) |
548 | 0 | d->state = SendAuthentication; |
549 | 0 | } |
550 | | |
551 | 0 | int statusCode = d->reply->statusCode(); |
552 | 0 | QAuthenticatorPrivate *priv = nullptr; |
553 | 0 | if (statusCode == 200) { |
554 | 0 | d->state = Connected; |
555 | 0 | setLocalAddress(d->socket->localAddress()); |
556 | 0 | setLocalPort(d->socket->localPort()); |
557 | 0 | d->inboundStreamCount = d->outboundStreamCount = 1; |
558 | 0 | setState(QAbstractSocket::ConnectedState); |
559 | 0 | d->authenticator.detach(); |
560 | 0 | priv = QAuthenticatorPrivate::getPrivate(d->authenticator); |
561 | 0 | priv->hasFailed = false; |
562 | 0 | } else if (statusCode == 407) { |
563 | 0 | if (d->authenticator.isNull()) |
564 | 0 | d->authenticator.detach(); |
565 | 0 | priv = QAuthenticatorPrivate::getPrivate(d->authenticator); |
566 | |
|
567 | 0 | const auto headers = d->reply->header(); |
568 | 0 | priv->parseHttpResponse(headers, true, d->proxy.hostName()); |
569 | |
|
570 | 0 | if (priv->phase == QAuthenticatorPrivate::Invalid) { |
571 | | // problem parsing the reply |
572 | 0 | d->socket->close(); |
573 | 0 | setState(QAbstractSocket::UnconnectedState); |
574 | 0 | setError(QAbstractSocket::ProxyProtocolError, tr("Error parsing authentication request from proxy")); |
575 | 0 | emitConnectionNotification(); |
576 | 0 | return; |
577 | 0 | } |
578 | | |
579 | 0 | if (priv->phase == QAuthenticatorPrivate::Done |
580 | 0 | || (priv->phase == QAuthenticatorPrivate::Start |
581 | 0 | && (priv->method == QAuthenticatorPrivate::Ntlm |
582 | 0 | || priv->method == QAuthenticatorPrivate::Negotiate))) { |
583 | 0 | if (priv->phase == QAuthenticatorPrivate::Start) |
584 | 0 | priv->phase = QAuthenticatorPrivate::Phase1; |
585 | 0 | bool credentialsWasSent = d->credentialsSent; |
586 | 0 | if (d->credentialsSent) { |
587 | | // Remember that (e.g.) NTLM is two-phase, so only reset when the authentication is |
588 | | // not currently in progress. 407 response again means the provided |
589 | | // username/password were invalid. |
590 | 0 | d->authenticator.detach(); |
591 | 0 | priv = QAuthenticatorPrivate::getPrivate(d->authenticator); |
592 | 0 | priv->hasFailed = true; |
593 | 0 | d->credentialsSent = false; |
594 | 0 | priv->phase = QAuthenticatorPrivate::Done; |
595 | 0 | } |
596 | 0 | if ((priv->method != QAuthenticatorPrivate::Ntlm |
597 | 0 | && priv->method != QAuthenticatorPrivate::Negotiate) |
598 | 0 | || credentialsWasSent) |
599 | 0 | proxyAuthenticationRequired(d->proxy, &d->authenticator); |
600 | 0 | } |
601 | |
|
602 | 0 | bool willClose; |
603 | 0 | QByteArray proxyConnectionHeader = d->reply->headerField("Proxy-Connection"); |
604 | | // Although most proxies use the unofficial Proxy-Connection header, the Connection header |
605 | | // from http spec is also allowed. |
606 | 0 | if (proxyConnectionHeader.isEmpty()) |
607 | 0 | proxyConnectionHeader = d->reply->headerField("Connection"); |
608 | 0 | if (proxyConnectionHeader.compare("close", Qt::CaseInsensitive) == 0) { |
609 | 0 | willClose = true; |
610 | 0 | } else if (proxyConnectionHeader.compare("keep-alive", Qt::CaseInsensitive) == 0) { |
611 | 0 | willClose = false; |
612 | 0 | } else { |
613 | | // no Proxy-Connection header, so use the default |
614 | | // HTTP 1.1's default behaviour is to keep persistent connections |
615 | | // HTTP 1.0 or earlier, so we expect the server to close |
616 | 0 | willClose = (d->reply->majorVersion() * 0x100 + d->reply->minorVersion()) <= 0x0100; |
617 | 0 | } |
618 | |
|
619 | 0 | if (willClose) { |
620 | | // the server will disconnect, so let's avoid receiving an error |
621 | | // especially since the signal below may trigger a new event loop |
622 | 0 | d->socket->disconnectFromHost(); |
623 | 0 | d->socket->readAll(); |
624 | | //We're done with the reply and need to reset it for the next connection |
625 | 0 | delete d->reply; |
626 | 0 | d->reply = new QHttpNetworkReply(QUrl(), this); |
627 | 0 | } |
628 | |
|
629 | 0 | if (priv->phase == QAuthenticatorPrivate::Done) { |
630 | 0 | d->authenticator = QAuthenticator(); |
631 | 0 | setError(QAbstractSocket::ProxyAuthenticationRequiredError, tr("Authentication required")); |
632 | 0 | d->socket->disconnectFromHost(); |
633 | 0 | } else { |
634 | | // close the connection if it isn't already and reconnect using the chosen authentication method |
635 | 0 | d->state = SendAuthentication; |
636 | 0 | if (willClose) { |
637 | 0 | d->socket->connectToHost(d->proxy.hostName(), d->proxy.port()); |
638 | 0 | } else { |
639 | | // send the HTTP CONNECT again |
640 | 0 | slotSocketConnected(); |
641 | 0 | } |
642 | 0 | return; |
643 | 0 | } |
644 | 0 | } else { |
645 | 0 | d->socket->close(); |
646 | 0 | setState(QAbstractSocket::UnconnectedState); |
647 | 0 | if (statusCode == 403 || statusCode == 405) { |
648 | | // 403 Forbidden |
649 | | // 405 Method Not Allowed |
650 | 0 | setError(QAbstractSocket::SocketAccessError, tr("Proxy denied connection")); |
651 | 0 | } else if (statusCode == 404) { |
652 | | // 404 Not Found: host lookup error |
653 | 0 | setError(QAbstractSocket::HostNotFoundError, QAbstractSocket::tr("Host not found")); |
654 | 0 | } else if (statusCode == 503) { |
655 | | // 503 Service Unavailable: Connection Refused |
656 | 0 | setError(QAbstractSocket::ConnectionRefusedError, QAbstractSocket::tr("Connection refused")); |
657 | 0 | } else { |
658 | | // Some other reply |
659 | | //qWarning("UNEXPECTED RESPONSE: [%s]", responseHeader.toString().toLatin1().data()); |
660 | 0 | setError(QAbstractSocket::ProxyProtocolError, tr("Error communicating with HTTP proxy")); |
661 | 0 | } |
662 | 0 | } |
663 | | |
664 | | // The handshake is done; notify that we're connected (or failed to connect) |
665 | 0 | emitConnectionNotification(); |
666 | 0 | } |
667 | | |
668 | | bool QHttpSocketEngine::readHttpHeader() |
669 | 0 | { |
670 | 0 | Q_D(QHttpSocketEngine); |
671 | |
|
672 | 0 | if (d->state != ReadResponseHeader) |
673 | 0 | return false; |
674 | | |
675 | 0 | bool ok = true; |
676 | 0 | if (d->reply->d_func()->state == QHttpNetworkReplyPrivate::NothingDoneState) { |
677 | | // do not keep old content sizes, status etc. around |
678 | 0 | d->reply->d_func()->clearHttpLayerInformation(); |
679 | 0 | d->reply->d_func()->state = QHttpNetworkReplyPrivate::ReadingStatusState; |
680 | 0 | } |
681 | 0 | if (d->reply->d_func()->state == QHttpNetworkReplyPrivate::ReadingStatusState) { |
682 | 0 | ok = d->reply->d_func()->readStatus(d->socket) != -1; |
683 | 0 | if (ok && d->reply->d_func()->state == QHttpNetworkReplyPrivate::ReadingStatusState) |
684 | 0 | return true; //Not done parsing headers yet, wait for more data |
685 | 0 | } |
686 | 0 | if (ok && d->reply->d_func()->state == QHttpNetworkReplyPrivate::ReadingHeaderState) { |
687 | 0 | ok = d->reply->d_func()->readHeader(d->socket) != -1; |
688 | 0 | if (ok && d->reply->d_func()->state == QHttpNetworkReplyPrivate::ReadingHeaderState) |
689 | 0 | return true; //Not done parsing headers yet, wait for more data |
690 | 0 | } |
691 | 0 | if (ok) { |
692 | 0 | bool contentLengthOk; |
693 | 0 | int contentLength = d->reply->headerField("Content-Length").toInt(&contentLengthOk); |
694 | 0 | if (contentLengthOk && contentLength > 0) |
695 | 0 | d->pendingResponseData = contentLength; |
696 | 0 | d->state = ReadResponseContent; // we are done reading the header |
697 | 0 | } |
698 | 0 | return ok; |
699 | 0 | } |
700 | | |
701 | | void QHttpSocketEngine::slotSocketBytesWritten() |
702 | 0 | { |
703 | 0 | Q_D(QHttpSocketEngine); |
704 | 0 | if (d->state == Connected && d->writeNotificationEnabled) |
705 | 0 | emitWriteNotification(); |
706 | 0 | } |
707 | | |
708 | | void QHttpSocketEngine::slotSocketError(QAbstractSocket::SocketError error) |
709 | 0 | { |
710 | 0 | Q_D(QHttpSocketEngine); |
711 | |
|
712 | 0 | if (d->state != Connected) { |
713 | | // we are in proxy handshaking stages |
714 | 0 | if (error == QAbstractSocket::HostNotFoundError) |
715 | 0 | setError(QAbstractSocket::ProxyNotFoundError, tr("Proxy server not found")); |
716 | 0 | else if (error == QAbstractSocket::ConnectionRefusedError) |
717 | 0 | setError(QAbstractSocket::ProxyConnectionRefusedError, tr("Proxy connection refused")); |
718 | 0 | else if (error == QAbstractSocket::SocketTimeoutError) |
719 | 0 | setError(QAbstractSocket::ProxyConnectionTimeoutError, tr("Proxy server connection timed out")); |
720 | 0 | else if (error == QAbstractSocket::RemoteHostClosedError) |
721 | 0 | setError(QAbstractSocket::ProxyConnectionClosedError, tr("Proxy connection closed prematurely")); |
722 | 0 | else |
723 | 0 | setError(error, d->socket->errorString()); |
724 | 0 | emitConnectionNotification(); |
725 | 0 | return; |
726 | 0 | } |
727 | | |
728 | | // We're connected |
729 | 0 | if (error == QAbstractSocket::SocketTimeoutError) |
730 | 0 | return; // ignore this error |
731 | | |
732 | 0 | d->state = None; |
733 | 0 | setError(error, d->socket->errorString()); |
734 | 0 | if (error != QAbstractSocket::RemoteHostClosedError) |
735 | 0 | qDebug() << "QHttpSocketEngine::slotSocketError: got weird error =" << error; |
736 | | //read notification needs to always be emitted, otherwise the higher layer doesn't get the disconnected signal |
737 | 0 | emitReadNotification(); |
738 | 0 | } |
739 | | |
740 | | void QHttpSocketEngine::slotSocketStateChanged(QAbstractSocket::SocketState state) |
741 | 0 | { |
742 | 0 | Q_UNUSED(state); |
743 | 0 | } |
744 | | |
745 | | void QHttpSocketEngine::emitPendingReadNotification() |
746 | 0 | { |
747 | 0 | Q_D(QHttpSocketEngine); |
748 | 0 | d->readNotificationPending = false; |
749 | 0 | if (d->readNotificationEnabled) |
750 | 0 | readNotification(); |
751 | 0 | } |
752 | | |
753 | | void QHttpSocketEngine::emitPendingWriteNotification() |
754 | 0 | { |
755 | 0 | Q_D(QHttpSocketEngine); |
756 | 0 | d->writeNotificationPending = false; |
757 | 0 | if (d->writeNotificationEnabled) |
758 | 0 | writeNotification(); |
759 | 0 | } |
760 | | |
761 | | void QHttpSocketEngine::emitPendingConnectionNotification() |
762 | 0 | { |
763 | 0 | Q_D(QHttpSocketEngine); |
764 | 0 | d->connectionNotificationPending = false; |
765 | 0 | connectionNotification(); |
766 | 0 | } |
767 | | |
768 | | void QHttpSocketEngine::emitReadNotification() |
769 | 0 | { |
770 | 0 | Q_D(QHttpSocketEngine); |
771 | | // if there is a connection notification pending we have to emit the readNotification |
772 | | // in case there is connection error. This is only needed for Windows, but it does not |
773 | | // hurt in other cases. |
774 | 0 | if ((d->readNotificationEnabled && !d->readNotificationPending) || d->connectionNotificationPending) { |
775 | 0 | d->readNotificationPending = true; |
776 | 0 | QMetaObject::invokeMethod(this, "emitPendingReadNotification", Qt::QueuedConnection); |
777 | 0 | } |
778 | 0 | } |
779 | | |
780 | | void QHttpSocketEngine::emitWriteNotification() |
781 | 0 | { |
782 | 0 | Q_D(QHttpSocketEngine); |
783 | 0 | if (d->writeNotificationEnabled && !d->writeNotificationPending) { |
784 | 0 | d->writeNotificationPending = true; |
785 | 0 | QMetaObject::invokeMethod(this, "emitPendingWriteNotification", Qt::QueuedConnection); |
786 | 0 | } |
787 | 0 | } |
788 | | |
789 | | void QHttpSocketEngine::emitConnectionNotification() |
790 | 0 | { |
791 | 0 | Q_D(QHttpSocketEngine); |
792 | 0 | if (!d->connectionNotificationPending) { |
793 | 0 | d->connectionNotificationPending = true; |
794 | 0 | QMetaObject::invokeMethod(this, "emitPendingConnectionNotification", Qt::QueuedConnection); |
795 | 0 | } |
796 | 0 | } |
797 | | |
798 | | QHttpSocketEnginePrivate::QHttpSocketEnginePrivate() |
799 | 0 | : readNotificationEnabled(false) |
800 | 0 | , writeNotificationEnabled(false) |
801 | 0 | , exceptNotificationEnabled(false) |
802 | 0 | , readNotificationPending(false) |
803 | 0 | , writeNotificationPending(false) |
804 | 0 | , connectionNotificationPending(false) |
805 | 0 | , credentialsSent(false) |
806 | 0 | , pendingResponseData(0) |
807 | 0 | { |
808 | 0 | socket = nullptr; |
809 | 0 | reply = nullptr; |
810 | 0 | state = QHttpSocketEngine::None; |
811 | 0 | } |
812 | | |
813 | | QHttpSocketEnginePrivate::~QHttpSocketEnginePrivate() |
814 | 0 | { |
815 | 0 | } |
816 | | |
817 | | QAbstractSocketEngine *QHttpSocketEngineHandler::createSocketEngine(QAbstractSocket::SocketType socketType, |
818 | | const QNetworkProxy &proxy, |
819 | | QObject *parent) |
820 | 0 | { |
821 | 0 | if (socketType != QAbstractSocket::TcpSocket) |
822 | 0 | return nullptr; |
823 | | |
824 | | // proxy type must have been resolved by now |
825 | 0 | if (proxy.type() != QNetworkProxy::HttpProxy) |
826 | 0 | return nullptr; |
827 | | |
828 | | // we only accept active sockets |
829 | 0 | if (!qobject_cast<QAbstractSocket *>(parent)) |
830 | 0 | return nullptr; |
831 | | |
832 | 0 | QHttpSocketEngine *engine = new QHttpSocketEngine(parent); |
833 | 0 | engine->setProxy(proxy); |
834 | 0 | return engine; |
835 | 0 | } |
836 | | |
837 | | QAbstractSocketEngine *QHttpSocketEngineHandler::createSocketEngine(qintptr, QObject *) |
838 | 0 | { |
839 | 0 | return nullptr; |
840 | 0 | } |
841 | | |
842 | | QT_END_NAMESPACE |
843 | | |
844 | | #endif // !QT_NO_NETWORKPROXY |
845 | | |
846 | | #include "moc_qhttpsocketengine_p.cpp" |