1// Copyright (C) 2021 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
4#ifndef QHTTPMESSAGESTREAMPARSER_P_H
5#define QHTTPMESSAGESTREAMPARSER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtJsonRpc/qtjsonrpcglobal.h>
19#include <QtCore/qbytearray.h>
20#include <QtCore/qstring.h>
21
22#include <functional>
23
24QT_BEGIN_NAMESPACE
25
26class Q_JSONRPC_EXPORT QHttpMessageStreamParser
27{
28public:
29 enum class State {
30 PreHeader,
31 InHeaderField,
32 HeaderValueSpace,
33 InHeaderValue,
34 AfterCr,
35 AfterCrLf,
36 AfterCrLfCr,
37 InBody
38 };
39
40 /*!
41 * \internal
42 * \brief Allows to run the FSM with or without keeping any buffers.
43 */
44 enum Mode { BUFFERED, UNBUFFERED };
45
46 QHttpMessageStreamParser(
47 std::function<void(const QByteArray &, const QByteArray &)> headerHandler,
48 std::function<void(const QByteArray &body)> bodyHandler,
49 std::function<void(QtMsgType error, QString msg)> errorHandler, Mode mode = BUFFERED);
50 void receiveData(QByteArray data);
51 bool receiveEof();
52
53 State state() const { return m_state; }
54
55private:
56 void callHasHeader();
57 void callHasBody();
58 void errorMessage(QtMsgType error, QString msg);
59
60 std::function<void(const QByteArray &, const QByteArray &)> m_headerHandler;
61 std::function<void(const QByteArray &body)> m_bodyHandler;
62 std::function<void(QtMsgType error, QString msg)> m_errorHandler;
63
64 State m_state = State::PreHeader;
65 QByteArray m_currentHeaderField;
66 QByteArray m_currentHeaderValue;
67 QByteArray m_currentPacket;
68 int m_contentSize = -1;
69 int m_currentPacketSize = 0;
70 Mode m_mode;
71};
72
73QT_END_NAMESPACE
74#endif // QHTTPMESSAGESTREAMPARSER_P_H
75

source code of qtlanguageserver/src/jsonrpc/qhttpmessagestreamparser_p.h