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 for (qsizetype i = 0; i < requestHeader.size(); ++i) {
68 const auto name = requestHeader.nameAt(i);
69 const auto value = requestHeader.valueAt(i);
70 const HeaderSize delta = entry_size(name, value);
73 if (std::numeric_limits<quint32>::max() - delta.second < size.second)
75 size.second += delta.second;
76 if (size.second > maxHeaderListSize)
79 if (name ==
"connection"_L1 || name ==
"host"_L1 || name ==
"keep-alive"_L1
80 || name ==
"proxy-connection"_L1 || name ==
"transfer-encoding"_L1) {
88 header.emplace_back(
QByteArray{name.data(), name.size()},
95QUrl urlkey_from_request(
const QHttpNetworkRequest &request)
99 url.setScheme(request.url().scheme());
100 url.setAuthority(request.url().authority(QUrl::FullyEncoded | QUrl::RemoveUserInfo));
101 url.setPath(QLatin1StringView(request.uri(
false)));
109using namespace Http2;
111QHttp2ProtocolHandler::QHttp2ProtocolHandler(QHttpNetworkConnectionChannel *channel)
112 : QAbstractProtocolHandler(channel)
114 const auto h2Config = m_connection->http2Parameters();
117 && m_connection->connectionType() != QHttpNetworkConnection::ConnectionTypeHTTP2Direct) {
118 h2Connection = QHttp2Connection::createUpgradedConnection(channel->socket, h2Config);
121 QHttp2Stream *stream = h2Connection->getStream(1);
123 Q_ASSERT(channel->reply);
124 connectStream({ channel->request, channel->reply }, stream);
126 Q_ASSERT(QSocketAbstraction::socketState(channel->socket) == QAbstractSocket::ConnectedState);
127 h2Connection = QHttp2Connection::createDirectConnection(channel->socket, h2Config);
129 connect(h2Connection, &QHttp2Connection::receivedGOAWAY,
this,
130 &QHttp2ProtocolHandler::handleGOAWAY);
131 connect(h2Connection, &QHttp2Connection::errorOccurred,
this,
132 &QHttp2ProtocolHandler::connectionError);
133 connect(h2Connection, &QHttp2Connection::newIncomingStream,
this,
134 [
this](QHttp2Stream *stream){
137 stream->sendRST_STREAM(REFUSE_STREAM);
138 if (!h2Connection->isGoingAway())
139 h2Connection->close(Http2::PROTOCOL_ERROR);
143void QHttp2ProtocolHandler::handleConnectionClosure()
152 h2Connection->handleConnectionClosure();
155void QHttp2ProtocolHandler::_q_uploadDataDestroyed(QObject *uploadData)
157 QPointer<QHttp2Stream> stream = streamIDs.take(uploadData);
158 if (stream && stream->isActive())
159 stream->sendRST_STREAM(
CANCEL);
162void QHttp2ProtocolHandler::_q_readyRead()
167void QHttp2ProtocolHandler::_q_receiveReply()
172 Q_ASSERT(h2Connection);
173 h2Connection->handleReadyRead();
176bool QHttp2ProtocolHandler::sendRequest()
178 if (h2Connection->isGoingAway()) {
181 m_channel->emitFinishedWithError(QNetworkReply::ProtocolUnknownError,
182 "GOAWAY received, cannot start a request");
183 m_channel->h2RequestsToSend.clear();
189 auto &requests = m_channel->h2RequestsToSend;
190 for (
auto it = requests.begin(), endIt = requests.end(); it != endIt;) {
191 const auto &pair = *it;
192 if (pair.first.isPreConnect()) {
193 m_connection->preConnectFinished();
194 emit pair.second->finished();
195 it = requests.erase(it);
196 if (requests.empty()) {
208 if (requests.empty())
211 m_channel->state = QHttpNetworkConnectionChannel::WritingState;
215 for (
auto it = requests.begin(), end = requests.end(); it != end;) {
216 HttpMessagePair &httpPair = *it;
218 QUrl promiseKey = urlkey_from_request(httpPair.first);
219 if (h2Connection->promisedStream(promiseKey) !=
nullptr) {
221 initReplyFromPushPromise(httpPair, promiseKey);
222 it = requests.erase(it);
226 QHttp2Stream *stream = createNewStream(httpPair);
229 if (httpPair.second->isFinished()) {
230 it = requests.erase(it);
236 QHttpNetworkRequest &request = requestReplyPairs[stream].first;
237 if (!sendHEADERS(stream, request)) {
238 finishStreamWithError(stream, QNetworkReply::UnknownNetworkError,
239 "failed to send HEADERS frame(s)"_L1);
242 if (request.uploadByteDevice()) {
243 if (!sendDATA(stream, httpPair.second)) {
244 finishStreamWithError(stream, QNetworkReply::UnknownNetworkError,
245 "failed to send DATA frame(s)"_L1);
249 it = requests.erase(it);
252 m_channel->state = QHttpNetworkConnectionChannel::IdleState;
258
259
260
261
262
263
264
265bool QHttp2ProtocolHandler::tryRemoveReply(QHttpNetworkReply *reply)
267 QHttp2Stream *stream = streamIDs.take(reply);
269 stream->sendRST_STREAM(stream->isUploadingDATA() ? Http2::CANCEL : Http2::HTTP2_NO_ERROR);
270 requestReplyPairs.remove(stream);
271 stream->deleteLater();
277bool QHttp2ProtocolHandler::sendHEADERS(QHttp2Stream *stream, QHttpNetworkRequest &request)
279 using namespace HPack;
281 bool useProxy =
false;
282#ifndef QT_NO_NETWORKPROXY
283 useProxy = m_connection->d_func()->networkProxy.type() != QNetworkProxy::NoProxy;
285 if (request.withCredentials()) {
286 m_connection->d_func()->createAuthorization(m_socket, request);
287 request.d->needResendWithCredentials =
false;
289 const auto headers = build_headers(request, h2Connection->maxHeaderListSize(), useProxy);
293 bool mustUploadData = request.uploadByteDevice();
294 return stream->sendHEADERS(headers, !mustUploadData);
297bool QHttp2ProtocolHandler::sendDATA(QHttp2Stream *stream, QHttpNetworkReply *reply)
300 QHttpNetworkReplyPrivate *replyPrivate = reply->d_func();
301 Q_ASSERT(replyPrivate);
302 QHttpNetworkRequest &request = replyPrivate->request;
303 Q_ASSERT(request.uploadByteDevice());
305 bool startedSending = stream->sendDATA(request.uploadByteDevice(),
true);
306 return startedSending && !stream->wasReset();
309void QHttp2ProtocolHandler::handleHeadersReceived(
const HPack::HttpHeader &headers,
bool endStream)
311 QHttp2Stream *stream = qobject_cast<QHttp2Stream *>(sender());
313 auto &requestPair = requestReplyPairs[stream];
314 auto *httpReply = requestPair.second;
315 auto &httpRequest = requestPair.first;
319 auto *httpReplyPrivate = httpReply->d_func();
327 for (
const auto &pair : headers) {
328 const auto &name = pair.name;
329 const auto value = QByteArrayView(pair.value);
335 if (name ==
":status") {
337 if (
int status = value.toInt(&ok); ok && status >= 0 && status <= 999) {
339 httpReply->setStatusCode(statusCode);
340 m_channel->lastStatus = statusCode;
342 finishStreamWithError(stream, QNetworkReply::ProtocolInvalidOperationError,
343 "invalid :status value"_L1);
346 }
else if (name ==
"content-length") {
348 const qlonglong length = value.toLongLong(&ok);
350 httpReply->setContentLength(length);
352 const auto binder = name ==
"set-cookie" ? QByteArrayView(
"\n") : QByteArrayView(
", ");
353 httpReply->appendHeaderField(name, QByteArray(pair.value).replace(
'\0', binder));
359 if (statusCode == 100 || (102 <= statusCode && statusCode <= 199)) {
360 httpReplyPrivate->clearHttpLayerInformation();
364 if (QHttpNetworkReply::isHttpRedirect(statusCode) && httpRequest.isFollowRedirects()) {
367 if (result.errorCode != QNetworkReply::NoError) {
368 auto errorString = m_connection->d_func()->errorDetail(result.errorCode, m_socket);
369 finishStreamWithError(stream, result.errorCode, errorString);
370 stream->sendRST_STREAM(INTERNAL_ERROR);
374 if (result.redirectUrl.isValid())
375 httpReply->setRedirectUrl(result.redirectUrl);
378 if (httpReplyPrivate->isCompressed() && httpRequest.d->autoDecompress)
379 httpReplyPrivate->removeAutoDecompressHeader();
381 if (QHttpNetworkReply::isHttpRedirect(statusCode)) {
387 if (
auto *byteDevice = httpRequest.uploadByteDevice()) {
389 httpReplyPrivate->totallyUploadedData = 0;
393 QMetaObject::invokeMethod(httpReply, &QHttpNetworkReply::headerChanged, Qt::QueuedConnection);
395 finishStream(stream, Qt::QueuedConnection);
398void QHttp2ProtocolHandler::handleDataReceived(
const QByteArray &data,
bool endStream)
400 QHttp2Stream *stream = qobject_cast<QHttp2Stream *>(sender());
401 auto &httpPair = requestReplyPairs[stream];
402 auto *httpReply = httpPair.second;
405 Q_ASSERT(!stream->isPromisedStream());
407 if (!data.isEmpty() && !httpPair.first.d->needResendWithCredentials) {
408 auto *replyPrivate = httpReply->d_func();
410 replyPrivate->totalProgress += data.size();
412 replyPrivate->responseData.append(data);
414 if (replyPrivate->shouldEmitSignals()) {
415 QMetaObject::invokeMethod(httpReply, &QHttpNetworkReply::readyRead,
416 Qt::QueuedConnection);
417 QMetaObject::invokeMethod(httpReply, &QHttpNetworkReply::dataReadProgress,
418 Qt::QueuedConnection, replyPrivate->totalProgress,
419 replyPrivate->bodyLength);
422 stream->clearDownloadBuffer();
424 finishStream(stream, Qt::QueuedConnection);
430void QHttp2ProtocolHandler::handleAuthorization(QHttp2Stream *stream)
432 auto &requestPair = requestReplyPairs[stream];
433 auto *httpReply = requestPair.second;
434 auto *httpReplyPrivate = httpReply->d_func();
435 auto &httpRequest = requestPair.first;
437 Q_ASSERT(httpReply && (httpReply->statusCode() == 401 || httpReply->statusCode() == 407));
439 const auto handleAuth = [&,
this](QByteArrayView authField,
bool isProxy) ->
bool {
441 const QByteArrayView auth = authField.trimmed();
442 if (auth.startsWith(
"Negotiate") || auth.startsWith(
"NTLM")) {
453 const bool authenticateHandled = m_connection->d_func()->handleAuthenticateChallenge(
454 m_socket, httpReply, isProxy, resend);
455 if (authenticateHandled) {
457 httpReply->d_func()->eraseData();
460 httpRequest.d->needResendWithCredentials =
true;
461 m_channel->h2RequestsToSend.insert(httpRequest.priority(), requestPair);
462 httpReply->d_func()->clearHeaders();
464 if (
auto *byteDevice = httpRequest.uploadByteDevice()) {
466 httpReplyPrivate->totallyUploadedData = 0;
480 emit httpReply->headerChanged();
481 emit httpReply->readyRead();
482 QNetworkReply::NetworkError error = httpReply->statusCode() == 401
483 ? QNetworkReply::AuthenticationRequiredError
484 : QNetworkReply::ProxyAuthenticationRequiredError;
485 finishStreamWithError(stream, QNetworkReply::AuthenticationRequiredError,
486 m_connection->d_func()->errorDetail(error, m_socket));
496 switch (httpReply->statusCode()) {
498 authOk = handleAuth(httpReply->headerField(
"www-authenticate"),
false);
501 authOk = handleAuth(httpReply->headerField(
"proxy-authenticate"),
true);
507 stream->sendRST_STREAM(CANCEL);
512void QHttp2ProtocolHandler::finishStream(QHttp2Stream *stream, Qt::ConnectionType connectionType)
514 if (stream->state() != QHttp2Stream::State::Closed)
515 stream->sendRST_STREAM(CANCEL);
517 auto &pair = requestReplyPairs[stream];
518 auto *httpReply = pair.second;
520 int statusCode = httpReply->statusCode();
521 if (statusCode == 401 || statusCode == 407) {
525 handleAuthorization(stream);
529 httpReply->disconnect(
this);
531 if (!pair.first.d->needResendWithCredentials) {
532 if (connectionType == Qt::DirectConnection)
533 emit httpReply->finished();
535 QMetaObject::invokeMethod(httpReply, &QHttpNetworkReply::finished, connectionType);
539 qCDebug(QT_HTTP2) <<
"stream" << stream->streamID() <<
"closed";
540 stream->deleteLater();
543void QHttp2ProtocolHandler::handleGOAWAY(
Http2Error errorCode, quint32 lastStreamID)
545 qCDebug(QT_HTTP2) <<
"GOAWAY received, error code:" << errorCode <<
"last stream ID:"
550 m_channel->emitFinishedWithError(QNetworkReply::ProtocolUnknownError,
551 "GOAWAY received, cannot start a request");
553 m_channel->h2RequestsToSend.clear();
555 QNetworkReply::NetworkError error = QNetworkReply::NoError;
557 qt_error(errorCode, error, message);
563 error = QNetworkReply::ContentReSendError;
564 message =
"Server stopped accepting new streams before this stream was established"_L1;
568void QHttp2ProtocolHandler::finishStreamWithError(QHttp2Stream *stream, Http2Error errorCode)
570 QNetworkReply::NetworkError error = QNetworkReply::NoError;
572 qt_error(errorCode, error, message);
573 finishStreamWithError(stream, error, message);
576void QHttp2ProtocolHandler::finishStreamWithError(QHttp2Stream *stream,
577 QNetworkReply::NetworkError error,
const QString &message)
579 stream->sendRST_STREAM(CANCEL);
580 const HttpMessagePair &pair = requestReplyPairs.value(stream);
581 if (
auto *httpReply = pair.second) {
582 httpReply->disconnect(
this);
585 emit httpReply->finishedWithError(error, message);
588 qCWarning(QT_HTTP2) <<
"stream" << stream->streamID() <<
"finished with error:" << message;
592
593
594
595
596
597QHttp2Stream *QHttp2ProtocolHandler::createNewStream(
const HttpMessagePair &message,
600 QUrl streamKey = urlkey_from_request(message.first);
601 if (
auto promisedStream = h2Connection->promisedStream(streamKey)) {
602 Q_ASSERT(promisedStream->state() != QHttp2Stream::State::Closed);
603 return promisedStream;
606 QH2Expected<QHttp2Stream *, QHttp2Connection::CreateStreamError>
607 streamResult = h2Connection->createStream();
608 if (!streamResult.ok()) {
609 if (streamResult.error()
610 == QHttp2Connection::CreateStreamError::MaxConcurrentStreamsReached) {
615 qCDebug(QT_HTTP2) <<
"failed to create new stream:" << streamResult.error();
616 auto *reply = message.second;
617 const char *cstr =
"Failed to initialize HTTP/2 stream with errorcode: %1";
618 const QString errorString = QCoreApplication::tr(
"QHttp", cstr)
619 .arg(QDebug::toString(streamResult.error()));
620 emit reply->finishedWithError(QNetworkReply::ProtocolFailure, errorString);
623 QHttp2Stream *stream = streamResult.unwrap();
626 if (
auto *src = message.first.uploadByteDevice()) {
627 connect(src, &QObject::destroyed,
this, &QHttp2ProtocolHandler::_q_uploadDataDestroyed);
628 streamIDs.insert(src, stream);
632 auto *reply = message.second;
633 QMetaObject::invokeMethod(reply, &QHttpNetworkReply::requestSent, Qt::QueuedConnection);
635 connectStream(message, stream);
639void QHttp2ProtocolHandler::connectStream(
const HttpMessagePair &message, QHttp2Stream *stream)
641 auto *reply = message.second;
642 auto *replyPrivate = reply->d_func();
643 replyPrivate->connection = m_connection;
644 replyPrivate->connectionChannel = m_channel;
646 reply->setHttp2WasUsed(
true);
647 QPointer<QHttp2Stream> &oldStream = streamIDs[reply];
649 disconnect(oldStream,
nullptr,
this,
nullptr);
651 requestReplyPairs.emplace(stream, message);
653 QObject::connect(stream, &QHttp2Stream::headersReceived,
this,
654 &QHttp2ProtocolHandler::handleHeadersReceived);
655 QObject::connect(stream, &QHttp2Stream::dataReceived,
this,
656 &QHttp2ProtocolHandler::handleDataReceived);
657 QObject::connect(stream, &QHttp2Stream::errorOccurred,
this,
658 [
this, stream](Http2Error errorCode,
const QString &errorString) {
660 <<
"stream" << stream->streamID() <<
"error:" << errorString;
661 finishStreamWithError(stream, errorCode);
664 QObject::connect(stream, &QHttp2Stream::stateChanged,
this, [
this](QHttp2Stream::State state) {
665 if (state == QHttp2Stream::State::Closed) {
667 if (!m_channel->h2RequestsToSend.empty()) {
668 QMetaObject::invokeMethod(
this, &QHttp2ProtocolHandler::sendRequest,
669 Qt::QueuedConnection);
675void QHttp2ProtocolHandler::initReplyFromPushPromise(
const HttpMessagePair &message,
676 const QUrl &cacheKey)
678 QHttp2Stream *promise = h2Connection->promisedStream(cacheKey);
680 Q_ASSERT(message.second);
681 message.second->setHttp2WasUsed(
true);
683 qCDebug(QT_HTTP2) <<
"found cached/promised response on stream" << promise->streamID();
685 const bool replyFinished = promise->state() == QHttp2Stream::State::Closed;
687 connectStream(message, promise);
692 QByteDataBuffer downloadBuffer = promise->takeDownloadBuffer();
693 if (
const auto &headers = promise->receivedHeaders(); !headers.empty())
694 emit promise->headersReceived(headers, replyFinished && downloadBuffer.isEmpty());
696 if (!downloadBuffer.isEmpty()) {
697 for (qsizetype i = 0; i < downloadBuffer.bufferCount(); ++i) {
698 const bool streamEnded = replyFinished && i == downloadBuffer.bufferCount() - 1;
699 emit promise->dataReceived(downloadBuffer[i], streamEnded);
704void QHttp2ProtocolHandler::connectionError(
Http2::
Http2Error errorCode,
const QString &message)
706 Q_ASSERT(!message.isNull());
708 qCCritical(QT_HTTP2) <<
"connection error:" << message;
710 const auto error = qt_error(errorCode);
711 m_channel->emitFinishedWithError(error, qPrintable(message));
716void QHttp2ProtocolHandler::closeSession()
723#include "moc_qhttp2protocolhandler_p.cpp"
static ParseRedirectResult parseRedirectResponse(QHttpNetworkReply *reply)
Combined button and popup list for selecting options.