summaryrefslogtreecommitdiff
path: root/src/websockets/qwebsocketdataprocessor_p.h
blob: 5c07be614161ace9c1336fbcc4ca1cedd431807a (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
// Copyright (C) 2016 Kurt Pattyn <pattyn.kurt@gmail.com>.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QWEBSOCKETDATAPROCESSOR_P_H
#define QWEBSOCKETDATAPROCESSOR_P_H

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

#include <QtCore/QObject>
#include <QtCore/QByteArray>
#include <QtCore/QString>
#include <QtCore/QStringDecoder>
#include <QtCore/QTimer>
#include "qwebsocketframe_p.h"
#include "qwebsocketprotocol.h"
#include "qwebsocketprotocol_p.h"

QT_BEGIN_NAMESPACE

class QIODevice;
class QWebSocketFrame;

const quint64 MAX_MESSAGE_SIZE_IN_BYTES = std::numeric_limits<int>::max() - 1;

class Q_AUTOTEST_EXPORT QWebSocketDataProcessor : public QObject
{
    Q_OBJECT
    Q_DISABLE_COPY(QWebSocketDataProcessor)

public:
    explicit QWebSocketDataProcessor(QObject *parent = nullptr);
    ~QWebSocketDataProcessor() override;

    void setMaxAllowedFrameSize(quint64 maxAllowedFrameSize);
    quint64 maxAllowedFrameSize() const;
    void setMaxAllowedMessageSize(quint64 maxAllowedMessageSize);
    quint64 maxAllowedMessageSize() const;
    static quint64 maxMessageSize();
    static quint64 maxFrameSize();

Q_SIGNALS:
    void pingReceived(const QByteArray &data);
    void pongReceived(const QByteArray &data);
    void closeReceived(QWebSocketProtocol::CloseCode closeCode, const QString &closeReason);
    void textFrameReceived(const QString &frame, bool lastFrame);
    void binaryFrameReceived(const QByteArray &frame, bool lastFrame);
    void textMessageReceived(const QString &message);
    void binaryMessageReceived(const QByteArray &message);
    void errorEncountered(QWebSocketProtocol::CloseCode code, const QString &description);

public Q_SLOTS:
    bool process(QIODevice *pIoDevice);
    void clear();

private:
    enum
    {
        PS_READ_HEADER,
        PS_READ_PAYLOAD_LENGTH,
        PS_READ_BIG_PAYLOAD_LENGTH,
        PS_READ_MASK,
        PS_READ_PAYLOAD,
        PS_DISPATCH_RESULT
    } m_processingState;

    bool m_isFinalFrame;
    bool m_isFragmented;
    QWebSocketProtocol::OpCode m_opCode;
    bool m_isControlFrame;
    bool m_hasMask;
    quint32 m_mask;
    QByteArray m_binaryMessage;
    QString m_textMessage;
    quint64 m_payloadLength;
    QStringDecoder m_decoder;
    QWebSocketFrame frame;
    QTimer *m_waitTimer;
    quint64 m_maxAllowedMessageSize = MAX_MESSAGE_SIZE_IN_BYTES;

    bool processControlFrame(const QWebSocketFrame &frame);
    void timeout();
};

QT_END_NAMESPACE

#endif // QWEBSOCKETDATAPROCESSOR_P_H