Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qhttp2protocolhandler.cpp
Go to the documentation of this file.
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 "access/http2/http2protocol_p.h"
6#include "access/qhttp2connection_p.h"
9
10#include "http2/http2frames_p.h"
11
12#include <private/qnoncontiguousbytedevice_p.h>
13#include <private/qsocketabstraction_p.h>
14
15#include <QtNetwork/qabstractsocket.h>
16
17#include <QtCore/qloggingcategory.h>
18#include <QtCore/qendian.h>
19#include <QtCore/qdebug.h>
20#include <QtCore/qlist.h>
21#include <QtCore/qnumeric.h>
22#include <QtCore/qurl.h>
23
24#include <qhttp2configuration.h>
25
26#ifndef QT_NO_NETWORKPROXY
27# include <QtNetwork/qnetworkproxy.h>
28#endif
29
30#include <qcoreapplication.h>
31
32#include <algorithm>
33#include <vector>
34#include <optional>
35
37
38using namespace Qt::StringLiterals;
39
40namespace
41{
42
43HPack::HttpHeader build_headers(const QHttpNetworkRequest &request, quint32 maxHeaderListSize,
44 bool useProxy)
45{
46 using namespace HPack;
47
48 HttpHeader header;
49 header.reserve(300);
50
51 // 1. Before anything - mandatory fields, if they do not fit into maxHeaderList -
52 // then stop immediately with error.
53 const auto auth = request.url().authority(QUrl::FullyEncoded | QUrl::RemoveUserInfo).toLatin1();
54 header.emplace_back(":authority", auth);
55 header.emplace_back(":method", request.methodName());
56 header.emplace_back(":path", request.uri(useProxy));
57 header.emplace_back(":scheme", request.url().scheme().toLatin1());
58
59 HeaderSize size = header_size(header);
60 if (!size.first) // Ooops!
61 return HttpHeader();
62
63 if (size.second > maxHeaderListSize)
64 return HttpHeader(); // Bad, we cannot send this request ...
65
66 const QHttpHeaders requestHeader = request.header();
67 using WK = QHttpHeaders::WellKnownHeader;
68 for (qsizetype i = 0; i < requestHeader.size(); ++i) {
69 const auto name = requestHeader.nameAt(i);
70 const auto value = requestHeader.valueAt(i);
71 const HeaderSize delta = entry_size(name, value);
72 if (!delta.first) // Overflow???
73 break;
74 if (std::numeric_limits<quint32>::max() - delta.second < size.second)
75 break;
76 size.second += delta.second;
77 if (size.second > maxHeaderListSize)
78 break;
79
80 if (name == QHttpHeaders::wellKnownHeaderName(WK::Connection)
81 || name == QHttpHeaders::wellKnownHeaderName(WK::Host)
82 || name == QHttpHeaders::wellKnownHeaderName(WK::KeepAlive)
83 || name.compare("proxy-connection"_L1, Qt::CaseInsensitive) == 0
84 || name == QHttpHeaders::wellKnownHeaderName(WK::TransferEncoding)) {
85 continue; // Those headers are not valid (section 3.2.1) - from QSpdyProtocolHandler
86 }
87 // TODO: verify with specs, which fields are valid to send ....
88 //
89 // Note: RFC 7450 8.1.2 (HTTP/2) states that header field names must be lower-cased
90 // prior to their encoding in HTTP/2
91 header.emplace_back(QByteArray{name.data(), name.size()}.toLower(),
92 QByteArray{value.data(), value.size()});
93 }
94
95 return header;
96}
97
98QUrl urlkey_from_request(const QHttpNetworkRequest &request)
99{
100 QUrl url;
101
102 url.setScheme(request.url().scheme());
103 url.setAuthority(request.url().authority(QUrl::FullyEncoded | QUrl::RemoveUserInfo));
104 url.setPath(QLatin1StringView(request.uri(false)));
105
106 return url;
107}
108
109} // Unnamed namespace
110
111// Since we anyway end up having this in every function definition:
112using namespace Http2;
113
114QHttp2ProtocolHandler::QHttp2ProtocolHandler(QHttpNetworkConnectionChannel *channel)
116{
117 const auto h2Config = m_connection->http2Parameters();
118
119 if (!channel->ssl
120 && m_connection->connectionType() != QHttpNetworkConnection::ConnectionTypeHTTP2Direct) {
121 h2Connection = QHttp2Connection::createUpgradedConnection(channel->socket, h2Config);
122 // Since we upgraded there is already one stream (the request was sent as http1)
123 // and we need to handle it:
124 QHttp2Stream *stream = h2Connection->getStream(1);
125 Q_ASSERT(stream);
126 Q_ASSERT(channel->reply);
127 connectStream({ channel->request, channel->reply }, stream);
128 } else {
129 Q_ASSERT(QSocketAbstraction::socketState(channel->socket) == QAbstractSocket::ConnectedState);
130 h2Connection = QHttp2Connection::createDirectConnection(channel->socket, h2Config);
131 }
132 connect(h2Connection, &QHttp2Connection::receivedGOAWAY, this,
133 &QHttp2ProtocolHandler::handleGOAWAY);
134 connect(h2Connection, &QHttp2Connection::errorOccurred, this,
135 &QHttp2ProtocolHandler::connectionError);
136 connect(h2Connection, &QHttp2Connection::newIncomingStream, this,
137 [this](QHttp2Stream *stream){
138 // Having our peer start streams doesn't make sense. We are
139 // doing regular http request-response.
140 stream->sendRST_STREAM(REFUSE_STREAM);
141 if (!h2Connection->isGoingAway())
142 h2Connection->close(Http2::PROTOCOL_ERROR);
143 });
144 connect(h2Connection, &QHttp2Connection::connectionClosed, this,
145 &QHttp2ProtocolHandler::closeSession);
146}
147
149{
150 // The channel has just received RemoteHostClosedError and since it will
151 // not try (for HTTP/2) to re-connect, it's time to finish all replies
152 // with error.
153
154 // Maybe we still have some data to read and can successfully finish
155 // a stream/request?
156 _q_receiveReply();
157 h2Connection->handleConnectionClosure();
158}
159
160void QHttp2ProtocolHandler::_q_uploadDataDestroyed(QObject *uploadData)
161{
162 QPointer<QHttp2Stream> stream = streamIDs.take(uploadData);
163 if (stream && stream->isActive())
164 stream->sendRST_STREAM(CANCEL);
165}
166
168{
169 _q_receiveReply();
170}
171
173{
174 // not using QObject::connect because the QHttpNetworkConnectionChannel
175 // already handles the signals we care about, so we just call the slot
176 // directly.
177 Q_ASSERT(h2Connection);
178 h2Connection->handleReadyRead();
179}
180
182{
183 if (h2Connection->isGoingAway()) {
184 // Stop further calls to this method: we have received GOAWAY
185 // so we cannot create new streams.
186 m_channel->emitFinishedWithError(QNetworkReply::ProtocolUnknownError,
187 "GOAWAY received, cannot start a request");
188 m_channel->h2RequestsToSend.clear();
189 return false;
190 }
191
192 // Process 'fake' (created by QNetworkAccessManager::connectToHostEncrypted())
193 // requests first:
194 auto &requests = m_channel->h2RequestsToSend;
195 for (auto it = requests.begin(), endIt = requests.end(); it != endIt;) {
196 const auto &pair = *it;
197 if (pair.first.isPreConnect()) {
198 m_connection->preConnectFinished();
199 emit pair.second->finished();
200 it = requests.erase(it);
201 if (requests.empty()) {
202 // Normally, after a connection was established and H2
203 // was negotiated, we send a client preface. connectToHostEncrypted
204 // though is not meant to send any data, it's just a 'preconnect'.
205 // Thus we return early:
206 return true;
207 }
208 } else {
209 ++it;
210 }
211 }
212
213 if (requests.empty())
214 return true;
215
216 m_channel->state = QHttpNetworkConnectionChannel::WritingState;
217 // Check what was promised/pushed, maybe we do not have to send a request
218 // and have a response already?
219
220 for (auto it = requests.begin(), end = requests.end(); it != end;) {
221 HttpMessagePair &httpPair = *it;
222
223 QUrl promiseKey = urlkey_from_request(httpPair.first);
224 if (h2Connection->promisedStream(promiseKey) != nullptr) {
225 // There's a PUSH_PROMISE for this request, so we don't send one
226 initReplyFromPushPromise(httpPair, promiseKey);
227 it = requests.erase(it);
228 continue;
229 }
230
231 QHttp2Stream *stream = createNewStream(httpPair);
232 if (!stream) { // There was an issue creating the stream
233 // Check if it was unrecoverable, ie. the reply is errored out and finished:
234 if (httpPair.second->isFinished()) {
235 it = requests.erase(it);
236 }
237 // ... either way we stop looping:
238 break;
239 }
240
241 QHttpNetworkRequest &request = requestReplyPairs[stream].first;
242 if (!sendHEADERS(stream, request)) {
243 finishStreamWithError(stream, QNetworkReply::UnknownNetworkError,
244 "failed to send HEADERS frame(s)"_L1);
245 continue;
246 }
247 if (request.uploadByteDevice()) {
248 if (!sendDATA(stream, httpPair.second)) {
249 finishStreamWithError(stream, QNetworkReply::UnknownNetworkError,
250 "failed to send DATA frame(s)"_L1);
251 continue;
252 }
253 }
254 it = requests.erase(it);
255 }
256
257 m_channel->state = QHttpNetworkConnectionChannel::IdleState;
258
259 return true;
260}
261
262/*!
263 \internal
264 This gets called during destruction of \a reply, so do not call any functions
265 on \a reply. We check if there is a stream associated with the reply and,
266 if there is, we remove the request-reply pair associated with this stream,
267 delete the stream and return \c{true}. Otherwise nothing happens and we
268 return \c{false}.
269*/
270bool QHttp2ProtocolHandler::tryRemoveReply(QHttpNetworkReply *reply)
271{
272 QHttp2Stream *stream = streamIDs.take(reply);
273 if (stream) {
274 stream->sendRST_STREAM(stream->isUploadingDATA() ? Http2::CANCEL : Http2::HTTP2_NO_ERROR);
275 clearStreamState(stream);
276 stream->deleteLater();
277 return true;
278 }
279 return false;
280}
281
282bool QHttp2ProtocolHandler::sendHEADERS(QHttp2Stream *stream, QHttpNetworkRequest &request)
283{
284 using namespace HPack;
285
286 bool useProxy = false;
287#ifndef QT_NO_NETWORKPROXY
288 useProxy = m_connection->d_func()->networkProxy.type() != QNetworkProxy::NoProxy;
289#endif
290 if (request.withCredentials()) {
291 m_connection->d_func()->createAuthorization(m_socket, request);
292 request.d->needResendWithCredentials = false;
293 }
294 const auto headers = build_headers(request, h2Connection->maxHeaderListSize(), useProxy);
295 if (headers.empty()) // nothing fits into maxHeaderListSize
296 return false;
297
298 bool mustUploadData = request.uploadByteDevice();
299 return stream->sendHEADERS(headers, !mustUploadData);
300}
301
302bool QHttp2ProtocolHandler::sendDATA(QHttp2Stream *stream, QHttpNetworkReply *reply)
303{
304 Q_ASSERT(reply);
305 QHttpNetworkReplyPrivate *replyPrivate = reply->d_func();
306 Q_ASSERT(replyPrivate);
307 QHttpNetworkRequest &request = replyPrivate->request;
308 Q_ASSERT(request.uploadByteDevice());
309
310 bool startedSending = stream->sendDATA(request.uploadByteDevice(), true);
311 return startedSending && !stream->wasReset();
312}
313
314void QHttp2ProtocolHandler::handleHeadersReceived(const HPack::HttpHeader &headers, bool endStream)
315{
316 QHttp2Stream *stream = qobject_cast<QHttp2Stream *>(sender());
317 Q_ASSERT(stream);
318 auto &requestPair = requestReplyPairs[stream];
319 auto *httpReply = requestPair.second;
320 auto &httpRequest = requestPair.first;
321 if (!httpReply)
322 return;
323
324 auto *httpReplyPrivate = httpReply->d_func();
325
326 // For HTTP/1 'location' is handled (and redirect URL set) when a protocol
327 // handler emits channel->allDone(). Http/2 protocol handler never emits
328 // allDone, since we have many requests multiplexed in one channel at any
329 // moment and we are probably not done yet. So we extract url and set it
330 // here, if needed.
331 int statusCode = 0;
332 for (const auto &pair : headers) {
333 const auto &name = pair.name;
334 const auto value = QByteArrayView(pair.value);
335
336 // TODO: part of this code copies what SPDY protocol handler does when
337 // processing headers. Binary nature of HTTP/2 and SPDY saves us a lot
338 // of parsing and related errors/bugs, but it would be nice to have
339 // more detailed validation of headers.
340 if (name == ":status") {
341 bool ok = false;
342 if (int status = value.toInt(&ok); ok && status >= 0 && status <= 999) {
343 statusCode = status;
344 httpReply->setStatusCode(statusCode);
345 m_channel->lastStatus = statusCode; // Mostly useless for http/2, needed for auth
346 } else {
347 finishStreamWithError(stream, QNetworkReply::ProtocolInvalidOperationError,
348 "invalid :status value"_L1);
349 return;
350 }
351 } else if (name == "content-length") {
352 bool ok = false;
353 const qlonglong length = value.toLongLong(&ok);
354 if (ok)
355 httpReply->setContentLength(length);
356 } else {
357 const auto binder = name == "set-cookie" ? QByteArrayView("\n") : QByteArrayView(", ");
358 httpReply->appendHeaderField(name, QByteArray(pair.value).replace('\0', binder));
359 }
360 }
361
362 // Discard all informational (1xx) replies with the exception of 101.
363 // Also see RFC 9110 (Chapter 15.2)
364 if (statusCode == 100 || (102 <= statusCode && statusCode <= 199)) {
365 httpReplyPrivate->clearHttpLayerInformation();
366 return;
367 }
368
369 if (QHttpNetworkReply::isHttpRedirect(statusCode) && httpRequest.isFollowRedirects()) {
372 if (result.errorCode != QNetworkReply::NoError) {
373 auto errorString = m_connection->d_func()->errorDetail(result.errorCode, m_socket);
374 finishStreamWithError(stream, result.errorCode, errorString);
375 stream->sendRST_STREAM(INTERNAL_ERROR);
376 return;
377 }
378
379 if (result.redirectUrl.isValid())
380 httpReply->setRedirectUrl(result.redirectUrl);
381 }
382
383 if (httpReplyPrivate->isCompressed() && httpRequest.d->autoDecompress)
384 httpReplyPrivate->removeAutoDecompressHeader();
385
386 if (QHttpNetworkReply::isHttpRedirect(statusCode)) {
387 // Note: This status code can trigger uploadByteDevice->reset() in
388 // QHttpNetworkConnectionChannel::handleStatus. Alas, we have no single
389 // request/reply, we multiplex several requests and thus we never simply
390 // call 'handleStatus'. If we have a byte-device - we try to reset it
391 // here, we don't (and can't) handle any error during reset operation.
392 if (auto *byteDevice = httpRequest.uploadByteDevice()) {
393 byteDevice->reset();
394 httpReplyPrivate->totallyUploadedData = 0;
395 }
396 }
397
398 QMetaObject::invokeMethod(httpReply, &QHttpNetworkReply::headerChanged, Qt::QueuedConnection);
399 if (endStream)
400 finishStream(stream, Qt::QueuedConnection);
401}
402
403void QHttp2ProtocolHandler::handleDataReceived(const QByteArray &data, bool endStream)
404{
405 QHttp2Stream *stream = qobject_cast<QHttp2Stream *>(sender());
406 auto &httpPair = requestReplyPairs[stream];
407 auto *httpReply = httpPair.second;
408 if (!httpReply)
409 return;
410 Q_ASSERT(!stream->isPromisedStream());
411
412 if (!data.isEmpty() && !httpPair.first.d->needResendWithCredentials) {
413 auto *replyPrivate = httpReply->d_func();
414
415 replyPrivate->totalProgress += data.size();
416
417 replyPrivate->responseData.append(data);
418
419 if (replyPrivate->shouldEmitSignals()) {
420 QMetaObject::invokeMethod(httpReply, &QHttpNetworkReply::readyRead,
421 Qt::QueuedConnection);
422 QMetaObject::invokeMethod(httpReply, &QHttpNetworkReply::dataReadProgress,
423 Qt::QueuedConnection, replyPrivate->totalProgress,
424 replyPrivate->bodyLength);
425 }
426 }
427 stream->clearDownloadBuffer();
428 if (endStream)
429 finishStream(stream, Qt::QueuedConnection);
430}
431
432// After calling this function, either the request will be re-sent or
433// the reply will be finishedWithError! Do not emit finished() or similar on the
434// reply after this!
435void QHttp2ProtocolHandler::handleAuthorization(QHttp2Stream *stream)
436{
437 auto &requestPair = requestReplyPairs[stream];
438 auto *httpReply = requestPair.second;
439 auto *httpReplyPrivate = httpReply->d_func();
440 auto &httpRequest = requestPair.first;
441
442 Q_ASSERT(httpReply && (httpReply->statusCode() == 401 || httpReply->statusCode() == 407));
443
444 const auto handleAuth = [&, this](QByteArrayView authField, bool isProxy) -> bool {
445 Q_ASSERT(httpReply);
446 const QByteArrayView auth = authField.trimmed();
447 if (auth.startsWith("Negotiate") || auth.startsWith("NTLM")) {
448 // NTLM/Kerberos/Negotiate authentication is not supported with HTTP/2.
449 // Finish the stream with an error so QNetworkReply::finished is emitted.
450 // Falling back to HTTP/1.1 is a separate, larger effort (QTBUG-143926).
451 emit httpReply->headerChanged();
452 emit httpReply->readyRead();
453 const QNetworkReply::NetworkError error = isProxy
454 ? QNetworkReply::ProxyAuthenticationRequiredError
455 : QNetworkReply::AuthenticationRequiredError;
456 finishStreamWithError(stream, error,
457 m_connection->d_func()->errorDetail(error, m_socket));
458 return false;
459 }
460 // Somewhat mimics parts of QHttpNetworkConnectionChannel::handleStatus
461 bool resend = false;
462 const bool authenticateHandled = m_connection->d_func()->handleAuthenticateChallenge(
463 m_socket, httpReply, isProxy, resend);
464 if (authenticateHandled) {
465 if (resend) {
466 httpReply->d_func()->eraseData();
467 // Add the request back in queue, we'll retry later now that
468 // we've gotten some username/password set on it:
469 httpRequest.d->needResendWithCredentials = true;
470 m_channel->h2RequestsToSend.insert(httpRequest.priority(), requestPair);
471 httpReply->d_func()->clearHeaders();
472 // If we have data we were uploading we need to reset it:
473 if (auto *byteDevice = httpRequest.uploadByteDevice()) {
474 byteDevice->reset();
475 httpReplyPrivate->totallyUploadedData = 0;
476 }
477 // We automatically try to send new requests when the stream is
478 // closed, so we don't need to call sendRequest ourselves.
479 return true;
480 } // else: we're just not resending the request.
481 // @note In the http/1.x case we (at time of writing) call close()
482 // for the connectionChannel (which is a bit weird, we could surely
483 // reuse the open socket outside "connection:close"?), but in http2
484 // we only have one channel, so we won't close anything.
485 } else {
486 // No authentication header or authentication isn't supported, but
487 // we got a 401/407 so we cannot succeed. We need to emit signals
488 // for headers and data, and then finishWithError.
489 emit httpReply->headerChanged();
490 emit httpReply->readyRead();
491 QNetworkReply::NetworkError error = httpReply->statusCode() == 401
492 ? QNetworkReply::AuthenticationRequiredError
493 : QNetworkReply::ProxyAuthenticationRequiredError;
494 finishStreamWithError(stream, QNetworkReply::AuthenticationRequiredError,
495 m_connection->d_func()->errorDetail(error, m_socket));
496 }
497 return false;
498 };
499
500 // These statuses would in HTTP/1.1 be handled by
501 // QHttpNetworkConnectionChannel::handleStatus. But because h2 has
502 // multiple streams/requests in a single channel this structure does not
503 // map properly to that function.
504 bool authOk = true;
505 switch (httpReply->statusCode()) {
506 case 401:
507 authOk = handleAuth(httpReply->headerField("www-authenticate"), false);
508 break;
509 case 407:
510 authOk = handleAuth(httpReply->headerField("proxy-authenticate"), true);
511 break;
512 default:
513 Q_UNREACHABLE();
514 }
515 if (authOk) {
516 stream->sendRST_STREAM(CANCEL);
517 clearStreamState(stream);
518 stream->deleteLater();
519 } // else: errors handled inside handleAuth
520}
521
522// Called when we have received a frame with the END_STREAM flag set
523void QHttp2ProtocolHandler::finishStream(QHttp2Stream *stream, Qt::ConnectionType connectionType)
524{
525 if (stream->state() != QHttp2Stream::State::Closed)
526 stream->sendRST_STREAM(CANCEL);
527
528 auto &pair = requestReplyPairs[stream];
529 auto *httpReply = pair.second;
530 if (httpReply) {
531 int statusCode = httpReply->statusCode();
532 if (statusCode == 401 || statusCode == 407) {
533 // handleAuthorization will either re-send the request or
534 // finishWithError. In either case we don't want to emit finished
535 // here.
536 handleAuthorization(stream);
537 return;
538 }
539
540 httpReply->disconnect(this);
541
542 if (!pair.first.d->needResendWithCredentials) {
543 if (connectionType == Qt::DirectConnection)
544 emit httpReply->finished();
545 else
546 QMetaObject::invokeMethod(httpReply, &QHttpNetworkReply::finished, connectionType);
547 }
548 }
549
550 clearStreamState(stream);
551 qCDebug(QT_HTTP2) << "stream" << stream->streamID() << "closed";
552
553 // Detach the reply and tear down the stream via canonical path, so a
554 // later stray frame cannot fail an already-finished reply. tryRemoveReply()
555 // also clears the reply->stream map, its sendRST_STREAM() does nothing on a
556 // gracefully closed stream.
557 if (!httpReply || !tryRemoveReply(httpReply)) {
558 requestReplyPairs.remove(stream);
559 stream->deleteLater();
560 }
561}
562
563void QHttp2ProtocolHandler::handleGOAWAY(Http2Error errorCode, quint32 lastStreamID)
564{
565 qCDebug(QT_HTTP2) << "GOAWAY received, error code:" << errorCode << "last stream ID:"
566 << lastStreamID;
567
568 // For the requests (and streams) we did not start yet, we have to report an
569 // error.
570 m_channel->emitFinishedWithError(QNetworkReply::ProtocolUnknownError,
571 "GOAWAY received, cannot start a request");
572 // Also, prevent further calls to sendRequest:
573 m_channel->h2RequestsToSend.clear();
574
575 QNetworkReply::NetworkError error = QNetworkReply::NoError;
576 QString message;
577 qt_error(errorCode, error, message);
578
579 // Even if the GOAWAY frame contains NO_ERROR we must send an error
580 // when terminating streams to ensure users can distinguish from a
581 // successful completion.
582 if (errorCode == HTTP2_NO_ERROR) {
583 error = QNetworkReply::ContentReSendError;
584 message = "Server stopped accepting new streams before this stream was established"_L1;
585 }
586}
587
588void QHttp2ProtocolHandler::finishStreamWithError(QHttp2Stream *stream, Http2Error errorCode)
589{
590 QNetworkReply::NetworkError error = QNetworkReply::NoError;
591 QString message;
592 qt_error(errorCode, error, message);
593 finishStreamWithError(stream, error, message);
594}
595
596void QHttp2ProtocolHandler::finishStreamWithError(QHttp2Stream *stream,
597 QNetworkReply::NetworkError error, const QString &message)
598{
599 stream->sendRST_STREAM(CANCEL);
600 const HttpMessagePair &pair = requestReplyPairs.value(stream);
601 if (auto *httpReply = pair.second) {
602 httpReply->disconnect(this);
603
604 // TODO: error message must be translated!!! (tr)
605 emit httpReply->finishedWithError(error, message);
606 }
607
608 clearStreamState(stream);
609 stream->deleteLater();
610 qCWarning(QT_HTTP2) << "stream" << stream->streamID() << "finished with error:" << message;
611}
612
613/*!
614 \internal
615
616 Creates a QHttp2Stream for the request, will return \nullptr if the stream
617 could not be created for some reason, and will finish the reply if required.
618*/
619QHttp2Stream *QHttp2ProtocolHandler::createNewStream(const HttpMessagePair &message,
620 bool uploadDone)
621{
622 QUrl streamKey = urlkey_from_request(message.first);
623 if (auto promisedStream = h2Connection->promisedStream(streamKey)) {
624 Q_ASSERT(promisedStream->state() != QHttp2Stream::State::Closed);
625 return promisedStream;
626 }
627
628 QH2Expected<QHttp2Stream *, QHttp2Connection::CreateStreamError>
629 streamResult = h2Connection->createStream();
630 if (!streamResult.ok()) {
631 if (streamResult.error()
632 == QHttp2Connection::CreateStreamError::MaxConcurrentStreamsReached) {
633 // We have to wait for a stream to be closed before we can create a new one, so
634 // we just return nullptr, the caller should not remove it from the queue.
635 return nullptr;
636 }
637 qCDebug(QT_HTTP2) << "failed to create new stream:" << streamResult.error();
638 auto *reply = message.second;
639 const char *cstr = "Failed to initialize HTTP/2 stream with errorcode: %1";
640 const QString errorString = QCoreApplication::tr("QHttp", cstr)
641 .arg(QDebug::toString(streamResult.error()));
642 emit reply->finishedWithError(QNetworkReply::ProtocolFailure, errorString);
643 return nullptr;
644 }
645 QHttp2Stream *stream = streamResult.unwrap();
646
647 if (!uploadDone) {
648 if (auto *src = message.first.uploadByteDevice()) {
649 connect(src, &QObject::destroyed, this, &QHttp2ProtocolHandler::_q_uploadDataDestroyed);
650 streamIDs.insert(src, stream);
651 }
652 }
653
654 auto *reply = message.second;
655 QMetaObject::invokeMethod(reply, &QHttpNetworkReply::requestSent, Qt::QueuedConnection);
656
657 connectStream(message, stream);
658 return stream;
659}
660
661void QHttp2ProtocolHandler::connectStream(const HttpMessagePair &message, QHttp2Stream *stream)
662{
663 auto *reply = message.second;
664 auto *replyPrivate = reply->d_func();
665 replyPrivate->connection = m_connection;
666 replyPrivate->connectionChannel = m_channel;
667
668 reply->setHttp2WasUsed(true);
669 QPointer<QHttp2Stream> &oldStream = streamIDs[reply];
670 if (oldStream) {
671 disconnect(oldStream, nullptr, this, nullptr);
672 clearStreamState(oldStream);
673 }
674 oldStream = stream;
675 requestReplyPairs.emplace(stream, message);
676
677 QObject::connect(stream, &QHttp2Stream::headersReceived, this,
678 &QHttp2ProtocolHandler::handleHeadersReceived);
679 QObject::connect(stream, &QHttp2Stream::dataReceived, this,
680 &QHttp2ProtocolHandler::handleDataReceived);
681 QObject::connect(stream, &QHttp2Stream::errorOccurred, this,
682 [this, stream](Http2Error errorCode, const QString &errorString) {
683 qCWarning(QT_HTTP2)
684 << "stream" << stream->streamID() << "error:" << errorString;
685 finishStreamWithError(stream, errorCode);
686 });
687
688 QObject::connect(stream, &QHttp2Stream::stateChanged, this, [this](QHttp2Stream::State state) {
689 if (state == QHttp2Stream::State::Closed) {
690 // Try to send more requests if we have any
691 if (!m_channel->h2RequestsToSend.empty()) {
692 QMetaObject::invokeMethod(this, &QHttp2ProtocolHandler::sendRequest,
693 Qt::QueuedConnection);
694 }
695 }
696 });
697}
698
699void QHttp2ProtocolHandler::clearStreamState(QHttp2Stream *stream)
700{
701 auto it = requestReplyPairs.find(stream);
702 if (it == requestReplyPairs.end())
703 return;
704
705 if (auto *reply = it->second)
706 streamIDs.remove(reply);
707 if (auto *uploadDevice = it->first.uploadByteDevice())
708 streamIDs.remove(uploadDevice);
709
710 requestReplyPairs.erase(it);
711}
712
713void QHttp2ProtocolHandler::initReplyFromPushPromise(const HttpMessagePair &message,
714 const QUrl &cacheKey)
715{
716 QHttp2Stream *promise = h2Connection->promisedStream(cacheKey);
717 Q_ASSERT(promise);
718 Q_ASSERT(message.second);
719 message.second->setHttp2WasUsed(true);
720
721 qCDebug(QT_HTTP2) << "found cached/promised response on stream" << promise->streamID();
722
723 const bool replyFinished = promise->state() == QHttp2Stream::State::Closed;
724
725 connectStream(message, promise);
726
727 // Now that we have connect()ed, re-emit signals so that the reply
728 // can be processed as usual:
729
730 QByteDataBuffer downloadBuffer = promise->takeDownloadBuffer();
731 if (const auto &headers = promise->receivedHeaders(); !headers.empty())
732 emit promise->headersReceived(headers, replyFinished && downloadBuffer.isEmpty());
733
734 if (!downloadBuffer.isEmpty()) {
735 for (qsizetype i = 0; i < downloadBuffer.bufferCount(); ++i) {
736 const bool streamEnded = replyFinished && i == downloadBuffer.bufferCount() - 1;
737 emit promise->dataReceived(downloadBuffer[i], streamEnded);
738 }
739 }
740}
741
742void QHttp2ProtocolHandler::connectionError(Http2::Http2Error errorCode, const QString &message)
743{
744 Q_ASSERT(!message.isNull());
745
746 qCCritical(QT_HTTP2) << "connection error:" << message;
747
748 const auto error = qt_error(errorCode);
749 m_channel->emitFinishedWithError(error, qPrintable(message));
750
751 closeSession();
752}
753
754void QHttp2ProtocolHandler::closeSession()
755{
756 m_channel->close();
757}
758
759QT_END_NAMESPACE
760
761#include "moc_qhttp2protocolhandler_p.cpp"
QHttp2ProtocolHandler(QHttpNetworkConnectionChannel *channel)
Q_INVOKABLE void handleConnectionClosure()
Q_INVOKABLE void _q_receiveReply() override
Q_INVOKABLE bool sendRequest() override
bool tryRemoveReply(QHttpNetworkReply *reply) override
static ParseRedirectResult parseRedirectResponse(QHttpNetworkReply *reply)
Combined button and popup list for selecting options.
std::pair< QHttpNetworkRequest, QHttpNetworkReply * > HttpMessagePair