| 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:significant reason:default |
| 4 | |
| 5 | #ifndef QHTTP2PROTOCOLHANDLER_P_H |
| 6 | #define QHTTP2PROTOCOLHANDLER_P_H |
| 7 | |
| 8 | // |
| 9 | // W A R N I N G |
| 10 | // ------------- |
| 11 | // |
| 12 | // This file is not part of the Qt API. It exists for the convenience |
| 13 | // of the Network Access API. This header file may change from |
| 14 | // version to version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | |
| 19 | #include "access/qhttp2connection_p.h" |
| 20 | #include <private/qhttpnetworkconnectionchannel_p.h> |
| 21 | #include <private/qabstractprotocolhandler_p.h> |
| 22 | #include <private/qhttpnetworkrequest_p.h> |
| 23 | |
| 24 | #include <access/qhttp2configuration.h> |
| 25 | |
| 26 | #include <private/qhttp2connection_p.h> |
| 27 | #include <private/http2protocol_p.h> |
| 28 | #include <private/http2streams_p.h> |
| 29 | #include <private/http2frames_p.h> |
| 30 | #include <private/hpacktable_p.h> |
| 31 | #include <private/hpack_p.h> |
| 32 | |
| 33 | #include <QtCore/qnamespace.h> |
| 34 | #include <QtCore/qbytearray.h> |
| 35 | #include <QtCore/qobject.h> |
| 36 | #include <QtCore/qflags.h> |
| 37 | #include <QtCore/qhash.h> |
| 38 | |
| 39 | #include <vector> |
| 40 | #include <limits> |
| 41 | #include <memory> |
| 42 | #include <deque> |
| 43 | #include <set> |
| 44 | |
| 45 | QT_REQUIRE_CONFIG(http); |
| 46 | |
| 47 | QT_BEGIN_NAMESPACE |
| 48 | |
| 49 | class QHttp2ProtocolHandler : public QObject, public QAbstractProtocolHandler |
| 50 | { |
| 51 | Q_OBJECT |
| 52 | |
| 53 | public: |
| 54 | QHttp2ProtocolHandler(QHttpNetworkConnectionChannel *channel); |
| 55 | |
| 56 | QHttp2ProtocolHandler(const QHttp2ProtocolHandler &rhs) = delete; |
| 57 | QHttp2ProtocolHandler(QHttp2ProtocolHandler &&rhs) = delete; |
| 58 | |
| 59 | QHttp2ProtocolHandler &operator=(const QHttp2ProtocolHandler &rhs) = delete; |
| 60 | QHttp2ProtocolHandler &operator=(QHttp2ProtocolHandler &&rhs) = delete; |
| 61 | |
| 62 | Q_INVOKABLE void handleConnectionClosure(); |
| 63 | |
| 64 | private slots: |
| 65 | void _q_uploadDataDestroyed(QObject *uploadData); |
| 66 | |
| 67 | private: |
| 68 | using Stream = Http2::Stream; |
| 69 | |
| 70 | void _q_readyRead() override; |
| 71 | Q_INVOKABLE void _q_receiveReply() override; |
| 72 | Q_INVOKABLE bool sendRequest() override; |
| 73 | bool tryRemoveReply(QHttpNetworkReply *reply) override; |
| 74 | |
| 75 | bool sendSETTINGS_ACK(); |
| 76 | bool sendHEADERS(QHttp2Stream *stream, QHttpNetworkRequest &request); |
| 77 | bool sendDATA(QHttp2Stream *stream, QHttpNetworkReply *reply); |
| 78 | |
| 79 | bool acceptSetting(Http2::Settings identifier, quint32 newValue); |
| 80 | |
| 81 | void handleAuthorization(QHttp2Stream *stream); |
| 82 | void handleHeadersReceived(const HPack::HttpHeader &, bool endStream); |
| 83 | void handleDataReceived(const QByteArray &data, bool endStream); |
| 84 | void finishStream(QHttp2Stream *stream, |
| 85 | Qt::ConnectionType connectionType = Qt::DirectConnection); |
| 86 | // Error code send by a peer (GOAWAY/RST_STREAM): |
| 87 | void handleGOAWAY(Http2::Http2Error errorCode, quint32 lastStreamID); |
| 88 | void finishStreamWithError(QHttp2Stream *stream, Http2::Http2Error errorCode); |
| 89 | // Locally encountered error: |
| 90 | void finishStreamWithError(QHttp2Stream *stream, QNetworkReply::NetworkError error, |
| 91 | const QString &message); |
| 92 | |
| 93 | // Stream's lifecycle management: |
| 94 | QHttp2Stream *createNewStream(const HttpMessagePair &message, bool uploadDone = false); |
| 95 | void connectStream(const HttpMessagePair &message, QHttp2Stream *stream); |
| 96 | quint32 popStreamToResume(); |
| 97 | |
| 98 | QHttp2Connection *h2Connection; |
| 99 | |
| 100 | // In the current implementation we send |
| 101 | // SETTINGS only once, immediately after |
| 102 | // the client's preface 24-byte message. |
| 103 | bool waitingForSettingsACK = false; |
| 104 | |
| 105 | inline static const quint32 maxAcceptableTableSize = 16 * HPack::FieldLookupTable::DefaultSize; |
| 106 | |
| 107 | QHash<QObject *, QPointer<QHttp2Stream>> streamIDs; |
| 108 | using HttpMessagePair = std::pair<QHttpNetworkRequest, QHttpNetworkReply *>; |
| 109 | QHash<QHttp2Stream *, HttpMessagePair> requestReplyPairs; |
| 110 | |
| 111 | void initReplyFromPushPromise(const HttpMessagePair &message, const QUrl &cacheKey); |
| 112 | // Errors: |
| 113 | void connectionError(Http2::Http2Error errorCode, const QString &message); |
| 114 | void closeSession(); |
| 115 | }; |
| 116 | |
| 117 | QT_END_NAMESPACE |
| 118 | |
| 119 | #endif |
| 120 | |