summaryrefslogtreecommitdiffstats
path: root/src/network/access/qhttp2protocolhandler_p.h
blob: aca8a0b6f66b28aefe945010b40102403c449928 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QHTTP2PROTOCOLHANDLER_P_H
#define QHTTP2PROTOCOLHANDLER_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of the Network Access API.  This header file may change from
// version to version without notice, or even be removed.
//
// We mean it.
//

#include "access/qhttp2connection_p.h"
#include <private/qhttpnetworkconnectionchannel_p.h>
#include <private/qabstractprotocolhandler_p.h>
#include <private/qhttpnetworkrequest_p.h>

#include <access/qhttp2configuration.h>

#include <private/qhttp2connection_p.h>
#include <private/http2protocol_p.h>
#include <private/http2streams_p.h>
#include <private/http2frames_p.h>
#include <private/hpacktable_p.h>
#include <private/hpack_p.h>

#include <QtCore/qnamespace.h>
#include <QtCore/qbytearray.h>
#include <QtCore/qobject.h>
#include <QtCore/qflags.h>
#include <QtCore/qhash.h>

#include <vector>
#include <limits>
#include <memory>
#include <deque>
#include <set>

QT_REQUIRE_CONFIG(http);

QT_BEGIN_NAMESPACE

class QHttp2ProtocolHandler : public QObject, public QAbstractProtocolHandler
{
    Q_OBJECT

public:
    QHttp2ProtocolHandler(QHttpNetworkConnectionChannel *channel);

    QHttp2ProtocolHandler(const QHttp2ProtocolHandler &rhs) = delete;
    QHttp2ProtocolHandler(QHttp2ProtocolHandler &&rhs) = delete;

    QHttp2ProtocolHandler &operator=(const QHttp2ProtocolHandler &rhs) = delete;
    QHttp2ProtocolHandler &operator=(QHttp2ProtocolHandler &&rhs) = delete;

    Q_INVOKABLE void handleConnectionClosure();

private slots:
    void _q_uploadDataDestroyed(QObject *uploadData);

private:
    using Stream = Http2::Stream;

    void _q_readyRead() override;
    Q_INVOKABLE void _q_receiveReply() override;
    Q_INVOKABLE bool sendRequest() override;
    bool tryRemoveReply(QHttpNetworkReply *reply) override;

    bool sendSETTINGS_ACK();
    bool sendHEADERS(QHttp2Stream *stream, QHttpNetworkRequest &request);
    bool sendDATA(QHttp2Stream *stream, QHttpNetworkReply *reply);

    bool acceptSetting(Http2::Settings identifier, quint32 newValue);

    void handleAuthorization(QHttp2Stream *stream);
    void handleHeadersReceived(const HPack::HttpHeader &headers, bool endStream);
    void handleDataReceived(const QByteArray &data, bool endStream);
    void finishStream(QHttp2Stream *stream,
                      Qt::ConnectionType connectionType = Qt::DirectConnection);
    // Error code send by a peer (GOAWAY/RST_STREAM):
    void handleGOAWAY(Http2::Http2Error errorCode, quint32 lastStreamID);
    void finishStreamWithError(QHttp2Stream *stream, Http2::Http2Error errorCode);
    // Locally encountered error:
    void finishStreamWithError(QHttp2Stream *stream, QNetworkReply::NetworkError error,
                               const QString &message);

    // Stream's lifecycle management:
    QHttp2Stream *createNewStream(const HttpMessagePair &message, bool uploadDone = false);
    void connectStream(const HttpMessagePair &message, QHttp2Stream *stream);
    quint32 popStreamToResume();

    QHttp2Connection *h2Connection;

    // In the current implementation we send
    // SETTINGS only once, immediately after
    // the client's preface 24-byte message.
    bool waitingForSettingsACK = false;

    inline static const quint32 maxAcceptableTableSize = 16 * HPack::FieldLookupTable::DefaultSize;

    QHash<QObject *, QPointer<QHttp2Stream>> streamIDs;
    using HttpMessagePair = std::pair<QHttpNetworkRequest, QHttpNetworkReply *>;
    QHash<QHttp2Stream *, HttpMessagePair> requestReplyPairs;

    void initReplyFromPushPromise(const HttpMessagePair &message, const QUrl &cacheKey);
    // Errors:
    void connectionError(Http2::Http2Error errorCode, const QString &message);
    void closeSession();
};

QT_END_NAMESPACE

#endif