1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QHTTPSERVERPARSER_P_H
6#define QHTTPSERVERPARSER_P_H
7
8#include <QtHttpServer/qhttpserverrequest.h>
9#include <QtNetwork/private/qhttpheaderparser_p.h>
10#include <QtCore/private/qbytedata_p.h>
11
12//
13// W A R N I N G
14// -------------
15//
16// This file is not part of the Qt API. It exists for the convenience
17// of QHttpServer. This header file may change from version to
18// version without notice, or even be removed.
19//
20// We mean it.
21
22QT_BEGIN_NAMESPACE
23
24class QHttp2Stream;
25
26class QHttpServerParser
27{
28 friend class QHttpServerRequest;
29
30public:
31 QHttpServerParser(const QHostAddress &remoteAddress, quint16 remotePort,
32 const QHostAddress &localAddress, quint16 localPort);
33
34 quint16 port = 0;
35
36 enum class State {
37 NothingDone,
38 ReadingRequestLine,
39 ReadingHeader,
40 ExpectContinue,
41 ReadingData,
42 AllDone,
43 } state = State::NothingDone;
44
45 QHttpHeaderParser headerParser;
46
47 bool parseRequestLine(QByteArrayView line);
48 qsizetype readRequestLine(QIODevice *socket);
49 qsizetype readHeader(QIODevice *socket);
50 qsizetype sendContinue(QIODevice *socket);
51 qsizetype readBodyFast(QIODevice *socket);
52 qsizetype readRequestBodyRaw(QIODevice *socket, qsizetype size);
53 qsizetype readRequestBodyChunked(QIODevice *socket);
54 qsizetype getChunkSize(QIODevice *socket, qsizetype *chunkSize);
55
56 bool parse(QIODevice *socket);
57#if QT_CONFIG(http)
58 bool parse(QHttp2Stream *socket);
59#endif
60 void clear();
61
62 qint64 contentLength() const;
63 QByteArray headerField(const QByteArray &name) const
64 { return headerParser.combinedHeaderValue(name); }
65
66 QHostAddress remoteAddress;
67 quint16 remotePort;
68 QHostAddress localAddress;
69 quint16 localPort;
70 QUrl url;
71 QHttpServerRequest::Method method;
72 QHttpHeaders headers;
73 QByteArray body;
74
75 bool handling{false};
76 qsizetype bodyLength;
77 qsizetype contentRead;
78 bool chunkedTransferEncoding;
79 bool lastChunkRead;
80 qsizetype currentChunkRead;
81 qsizetype currentChunkSize;
82 bool upgrade;
83 int majorVersion;
84 int minorVersion;
85
86 QByteArray fragment;
87 QByteDataBuffer bodyBuffer;
88};
89
90QT_END_NAMESPACE
91
92#endif // QHTTPSERVERPARSER_P_H
93

source code of qthttpserver/src/httpserver/qhttpserverparser_p.h