5#include "access/http2/http2protocol_p.h"
6#include "access/qhttp2connection_p.h"
10#include "http2/http2frames_p.h"
12#include <private/qnoncontiguousbytedevice_p.h>
13#include <private/qsocketabstraction_p.h>
15#include <QtNetwork/qabstractsocket.h>
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>
24#include <qhttp2configuration.h>
26#ifndef QT_NO_NETWORKPROXY
27# include <QtNetwork/qnetworkproxy.h>
30#include <qcoreapplication.h>
38using namespace Qt::StringLiterals;
43HPack::HttpHeader build_headers(
const QHttpNetworkRequest &request, quint32 maxHeaderListSize,
46 using namespace HPack;
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());
59 HeaderSize size = header_size(header);
63 if (size.second > maxHeaderListSize)
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);
74 if (std::numeric_limits<quint32>::max() - delta.second < size.second)
76 size.second += delta.second;
77 if (size.second > maxHeaderListSize)
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)) {
91 header.emplace_back(
QByteArray{name.data(), name.size()}.toLower(),
98QUrl urlkey_from_request(
const QHttpNetworkRequest &request)
102 url.setScheme(request.url().scheme());
103 url.setAuthority(request.url().authority(QUrl::FullyEncoded | QUrl::RemoveUserInfo));
104 url.setPath(QLatin1StringView(request.uri(
false)));
112using namespace Http2;
117 const auto h2Config = m_connection->http2Parameters();
120 && m_connection->connectionType() != QHttpNetworkConnection::ConnectionTypeHTTP2Direct) {
121 h2Connection = QHttp2Connection::createUpgradedConnection(channel->socket, h2Config);
124 QHttp2Stream *stream = h2Connection->getStream(1);
126 Q_ASSERT(channel->reply);
127 connectStream({ channel->request, channel->reply }, stream);
129 Q_ASSERT(QSocketAbstraction::socketState(channel->socket) == QAbstractSocket::ConnectedState);
130 h2Connection = QHttp2Connection::createDirectConnection(channel->socket, h2Config);
132 connect(h2Connection, &QHttp2Connection::receivedGOAWAY,
this,
133 &QHttp2ProtocolHandler::handleGOAWAY);
134 connect(h2Connection, &QHttp2Connection::errorOccurred,
this,
136 connect(h2Connection, &QHttp2Connection::newIncomingStream,
this,
137 [
this](QHttp2Stream *stream){
141 if (!h2Connection->isGoingAway())
144 connect(h2Connection, &QHttp2Connection::connectionClosed,
this,
150 return h2Connection && h2Connection->isGoingAway();
162 h2Connection->handleConnectionClosure();
167 QPointer<QHttp2Stream> stream = streamIDs.take(uploadData);
168 if (stream && stream->isActive())
169 stream->sendRST_STREAM(
CANCEL);
182 Q_ASSERT(h2Connection);
183 h2Connection->handleReadyRead();
193 auto &requests = m_channel->h2RequestsToSend;
194 for (
auto it = requests.begin(), endIt = requests.end(); it != endIt;) {
195 const auto &pair = *it;
196 if (pair.first.isPreConnect()) {
197 m_connection->preConnectFinished();
198 emit pair.second->finished();
199 it = requests.erase(it);
200 if (requests.empty()) {
212 if (requests.empty())
215 m_channel->state = QHttpNetworkConnectionChannel::WritingState;
219 for (
auto it = requests.begin(), end = requests.end(); it != end;) {
222 QUrl promiseKey = urlkey_from_request(httpPair.first);
223 if (h2Connection->promisedStream(promiseKey) !=
nullptr) {
225 initReplyFromPushPromise(httpPair, promiseKey);
226 it = requests.erase(it);
230 QHttp2Stream *stream = createNewStream(httpPair);
233 if (httpPair.second->isFinished()) {
234 it = requests.erase(it);
240 QHttpNetworkRequest &request = requestReplyPairs[stream].first;
241 if (!sendHEADERS(stream, request)) {
242 finishStreamWithError(stream, QNetworkReply::UnknownNetworkError,
243 "failed to send HEADERS frame(s)"_L1);
246 if (request.uploadByteDevice()) {
247 if (!sendDATA(stream, httpPair.second)) {
248 finishStreamWithError(stream, QNetworkReply::UnknownNetworkError,
249 "failed to send DATA frame(s)"_L1);
253 it = requests.erase(it);
256 m_channel->state = QHttpNetworkConnectionChannel::IdleState;
262
263
264
265
266
267
268
271 QHttp2Stream *stream = streamIDs.take(reply);
273 stream->sendRST_STREAM(stream->isUploadingDATA() ? Http2::CANCEL : Http2::HTTP2_NO_ERROR);
274 clearStreamState(stream);
275 stream->deleteLater();
283 using namespace HPack;
285 bool useProxy =
false;
286#ifndef QT_NO_NETWORKPROXY
287 useProxy = m_connection->d_func()->networkProxy.type() != QNetworkProxy::NoProxy;
289 if (request.withCredentials()) {
290 m_connection->d_func()->createAuthorization(m_socket, request);
291 request.d->needResendWithCredentials =
false;
293 const auto headers = build_headers(request, h2Connection->maxHeaderListSize(), useProxy);
297 bool mustUploadData = request.uploadByteDevice();
298 return stream->sendHEADERS(headers, !mustUploadData);
304 QHttpNetworkReplyPrivate *replyPrivate = reply->d_func();
305 Q_ASSERT(replyPrivate);
306 QHttpNetworkRequest &request = replyPrivate->request;
307 Q_ASSERT(request.uploadByteDevice());
309 bool startedSending = stream->sendDATA(request.uploadByteDevice(),
true);
310 return startedSending && !stream->wasReset();
315 QHttp2Stream *stream = qobject_cast<QHttp2Stream *>(sender());
317 auto &requestPair = requestReplyPairs[stream];
318 auto *httpReply = requestPair.second;
319 auto &httpRequest = requestPair.first;
323 auto *httpReplyPrivate = httpReply->d_func();
331 for (
const auto &pair : headers) {
332 const auto &name = pair.name;
333 const auto value = QByteArrayView(pair.value);
339 if (name ==
":status") {
341 if (
int status = value.toInt(&ok); ok && status >= 0 && status <= 999) {
343 httpReply->setStatusCode(statusCode);
344 m_channel->lastStatus = statusCode;
346 finishStreamWithError(stream, QNetworkReply::ProtocolInvalidOperationError,
347 "invalid :status value"_L1);
350 }
else if (name ==
"content-length") {
352 const qlonglong length = value.toLongLong(&ok);
354 httpReply->setContentLength(length);
356 const auto binder = name ==
"set-cookie" ? QByteArrayView(
"\n") : QByteArrayView(
", ");
357 httpReply->appendHeaderField(name, QByteArray(pair.value).replace(
'\0', binder));
363 if (statusCode == 100 || (102 <= statusCode && statusCode <= 199)) {
364 httpReplyPrivate->clearHttpLayerInformation();
368 if (QHttpNetworkReply::isHttpRedirect(statusCode) && httpRequest.isFollowRedirects()) {
371 if (result.errorCode != QNetworkReply::NoError) {
372 auto errorString = m_connection->d_func()->errorDetail(result.errorCode, m_socket);
373 finishStreamWithError(stream, result.errorCode, errorString);
374 stream->sendRST_STREAM(INTERNAL_ERROR);
378 if (result.redirectUrl.isValid())
379 httpReply->setRedirectUrl(result.redirectUrl);
382 if (httpReplyPrivate->isCompressed() && httpRequest.d->autoDecompress)
383 httpReplyPrivate->removeAutoDecompressHeader();
385 if (QHttpNetworkReply::isHttpRedirect(statusCode)) {
391 if (
auto *byteDevice = httpRequest.uploadByteDevice()) {
393 httpReplyPrivate->totallyUploadedData = 0;
397 QMetaObject::invokeMethod(httpReply, &QHttpNetworkReply::headerChanged, Qt::QueuedConnection);
399 finishStream(stream, Qt::QueuedConnection);
404 QHttp2Stream *stream = qobject_cast<QHttp2Stream *>(sender());
405 auto &httpPair = requestReplyPairs[stream];
406 auto *httpReply = httpPair.second;
409 Q_ASSERT(!stream->isPromisedStream());
411 if (!data.isEmpty() && !httpPair.first.d->needResendWithCredentials) {
412 auto *replyPrivate = httpReply->d_func();
414 replyPrivate->totalProgress += data.size();
416 replyPrivate->responseData.append(data);
418 if (replyPrivate->shouldEmitSignals()) {
419 QMetaObject::invokeMethod(httpReply, &QHttpNetworkReply::readyRead,
420 Qt::QueuedConnection);
421 QMetaObject::invokeMethod(httpReply, &QHttpNetworkReply::dataReadProgress,
422 Qt::QueuedConnection, replyPrivate->totalProgress,
423 replyPrivate->bodyLength);
426 stream->clearDownloadBuffer();
428 finishStream(stream, Qt::QueuedConnection);
436 auto &requestPair = requestReplyPairs[stream];
437 auto *httpReply = requestPair.second;
438 auto *httpReplyPrivate = httpReply->d_func();
439 auto &httpRequest = requestPair.first;
441 Q_ASSERT(httpReply && (httpReply->statusCode() == 401 || httpReply->statusCode() == 407));
443 const auto handleAuth = [&,
this](QByteArrayView authField,
bool isProxy) ->
bool {
445 const QByteArrayView auth = authField.trimmed();
446 if (auth.startsWith(
"Negotiate") || auth.startsWith(
"NTLM")) {
450 emit httpReply->headerChanged();
451 emit httpReply->readyRead();
452 const QNetworkReply::NetworkError error = isProxy
453 ? QNetworkReply::ProxyAuthenticationRequiredError
454 : QNetworkReply::AuthenticationRequiredError;
455 finishStreamWithError(stream, error,
456 m_connection->d_func()->errorDetail(error, m_socket));
461 const bool authenticateHandled = m_connection->d_func()->handleAuthenticateChallenge(
462 m_socket, httpReply, isProxy, resend);
463 if (authenticateHandled) {
465 httpReply->d_func()->eraseData();
468 httpRequest.d->needResendWithCredentials =
true;
469 m_channel->h2RequestsToSend.insert(httpRequest.priority(), requestPair);
470 httpReply->d_func()->clearHeaders();
472 if (
auto *byteDevice = httpRequest.uploadByteDevice()) {
474 httpReplyPrivate->totallyUploadedData = 0;
488 emit httpReply->headerChanged();
489 emit httpReply->readyRead();
490 QNetworkReply::NetworkError error = httpReply->statusCode() == 401
491 ? QNetworkReply::AuthenticationRequiredError
492 : QNetworkReply::ProxyAuthenticationRequiredError;
493 finishStreamWithError(stream, QNetworkReply::AuthenticationRequiredError,
494 m_connection->d_func()->errorDetail(error, m_socket));
504 switch (httpReply->statusCode()) {
506 authOk = handleAuth(httpReply->headerField(
"www-authenticate"),
false);
509 authOk = handleAuth(httpReply->headerField(
"proxy-authenticate"),
true);
515 stream->sendRST_STREAM(CANCEL);
516 clearStreamState(stream);
517 stream->deleteLater();
524 if (stream->state() != QHttp2Stream::State::Closed)
525 stream->sendRST_STREAM(CANCEL);
527 auto &pair = requestReplyPairs[stream];
528 auto *httpReply = pair.second;
530 int statusCode = httpReply->statusCode();
531 if (statusCode == 401 || statusCode == 407) {
535 handleAuthorization(stream);
539 httpReply->disconnect(
this);
541 if (!pair.first.d->needResendWithCredentials) {
542 if (connectionType == Qt::DirectConnection)
543 emit httpReply->finished();
545 QMetaObject::invokeMethod(httpReply, &QHttpNetworkReply::finished, connectionType);
549 clearStreamState(stream);
550 qCDebug(QT_HTTP2) <<
"stream" << stream->streamID() <<
"closed";
556 if (!httpReply || !tryRemoveReply(httpReply)) {
557 requestReplyPairs.remove(stream);
558 stream->deleteLater();
564 qCDebug(QT_HTTP2) <<
"GOAWAY received, error code:" << errorCode <<
"last stream ID:"
574 m_channel->emitFinishedWithError(QNetworkReply::ProtocolUnknownError,
575 "GOAWAY received, cannot start a request");
576 m_channel->h2RequestsToSend.clear();
585 QNetworkReply::NetworkError error = QNetworkReply::NoError;
587 qt_error(errorCode, error, message);
588 finishStreamWithError(stream, error, message);
592 QNetworkReply::NetworkError error,
const QString &message)
594 stream->sendRST_STREAM(CANCEL);
595 const HttpMessagePair &pair = requestReplyPairs.value(stream);
596 if (
auto *httpReply = pair.second) {
597 httpReply->disconnect(
this);
600 emit httpReply->finishedWithError(error, message);
603 clearStreamState(stream);
604 stream->deleteLater();
605 qCWarning(QT_HTTP2) <<
"stream" << stream->streamID() <<
"finished with error:" << message;
609
610
611
612
613
617 QUrl streamKey = urlkey_from_request(message.first);
618 if (
auto promisedStream = h2Connection->promisedStream(streamKey)) {
619 Q_ASSERT(promisedStream->state() != QHttp2Stream::State::Closed);
620 return promisedStream;
623 QH2Expected<QHttp2Stream *, QHttp2Connection::CreateStreamError>
624 streamResult = h2Connection->createStream();
625 if (!streamResult.ok()) {
626 if (streamResult.error()
627 == QHttp2Connection::CreateStreamError::MaxConcurrentStreamsReached) {
632 qCDebug(QT_HTTP2) <<
"failed to create new stream:" << streamResult.error();
633 auto *reply = message.second;
634 const char *cstr =
"Failed to initialize HTTP/2 stream with errorcode: %1";
635 const QString errorString = QCoreApplication::tr(
"QHttp", cstr)
636 .arg(QDebug::toString(streamResult.error()));
637 emit reply->finishedWithError(QNetworkReply::ProtocolFailure, errorString);
640 QHttp2Stream *stream = streamResult.unwrap();
643 if (
auto *src = message.first.uploadByteDevice()) {
644 connect(src, &QObject::destroyed,
this, &QHttp2ProtocolHandler::_q_uploadDataDestroyed);
645 streamIDs.insert(src, stream);
649 auto *reply = message.second;
650 QMetaObject::invokeMethod(reply, &QHttpNetworkReply::requestSent, Qt::QueuedConnection);
652 connectStream(message, stream);
658 auto *reply = message.second;
659 auto *replyPrivate = reply->d_func();
660 replyPrivate->connection = m_connection;
661 replyPrivate->connectionChannel = m_channel;
663 reply->setHttp2WasUsed(
true);
664 QPointer<QHttp2Stream> &oldStream = streamIDs[reply];
666 disconnect(oldStream,
nullptr,
this,
nullptr);
667 clearStreamState(oldStream);
670 requestReplyPairs.emplace(stream, message);
672 QObject::connect(stream, &QHttp2Stream::headersReceived,
this,
673 &QHttp2ProtocolHandler::handleHeadersReceived);
674 QObject::connect(stream, &QHttp2Stream::dataReceived,
this,
675 &QHttp2ProtocolHandler::handleDataReceived);
676 QObject::connect(stream, &QHttp2Stream::errorOccurred,
this,
677 [
this, stream](Http2Error errorCode,
const QString &errorString) {
679 <<
"stream" << stream->streamID() <<
"error:" << errorString;
683 if (
const auto lastId = h2Connection->lastGoAwayStreamID();
684 errorCode == HTTP2_NO_ERROR && lastId
685 && stream->streamID() > *lastId) {
686 finishStreamWithError(
687 stream, QNetworkReply::ContentReSendError,
688 QCoreApplication::translate(
690 "Server stopped accepting new streams before this "
691 "stream was established"));
693 finishStreamWithError(stream, errorCode);
697 QObject::connect(stream, &QHttp2Stream::stateChanged,
this, [
this](QHttp2Stream::State state) {
698 if (state == QHttp2Stream::State::Closed) {
700 if (!m_channel->h2RequestsToSend.empty()) {
701 QMetaObject::invokeMethod(
this, &QHttp2ProtocolHandler::sendRequest,
702 Qt::QueuedConnection);
710 auto it = requestReplyPairs.find(stream);
711 if (it == requestReplyPairs.end())
714 if (
auto *reply = it->second)
715 streamIDs.remove(reply);
716 if (
auto *uploadDevice = it->first.uploadByteDevice())
717 streamIDs.remove(uploadDevice);
719 requestReplyPairs.erase(it);
723 const QUrl &cacheKey)
725 QHttp2Stream *promise = h2Connection->promisedStream(cacheKey);
727 Q_ASSERT(message.second);
728 message.second->setHttp2WasUsed(
true);
730 qCDebug(QT_HTTP2) <<
"found cached/promised response on stream" << promise->streamID();
732 const bool replyFinished = promise->state() == QHttp2Stream::State::Closed;
734 connectStream(message, promise);
739 QByteDataBuffer downloadBuffer = promise->takeDownloadBuffer();
740 if (
const auto &headers = promise->receivedHeaders(); !headers.empty())
741 emit promise->headersReceived(headers, replyFinished && downloadBuffer.isEmpty());
743 if (!downloadBuffer.isEmpty()) {
744 for (qsizetype i = 0; i < downloadBuffer.bufferCount(); ++i) {
745 const bool streamEnded = replyFinished && i == downloadBuffer.bufferCount() - 1;
746 emit promise->dataReceived(downloadBuffer[i], streamEnded);
753 Q_ASSERT(!message.isNull());
755 qCCritical(QT_HTTP2) <<
"connection error:" << message;
757 const auto error = qt_error(errorCode);
758 m_channel->emitFinishedWithError(error, message);
759 m_channel->h2RequestsToSend.clear();
771#include "moc_qhttp2protocolhandler_p.cpp"
bool isGoingAway() const noexcept
QHttp2ProtocolHandler(QHttpNetworkConnectionChannel *channel)
Q_INVOKABLE void handleConnectionClosure()
Q_INVOKABLE void _q_receiveReply() override
Q_INVOKABLE bool sendRequest() override
void _q_readyRead() override
bool tryRemoveReply(QHttpNetworkReply *reply) override
static ParseRedirectResult parseRedirectResponse(QHttpNetworkReply *reply)
Combined button and popup list for selecting options.
std::pair< QHttpNetworkRequest, QHttpNetworkReply * > HttpMessagePair