summaryrefslogtreecommitdiffstats
path: root/src/httpserver/qhttpserverparser_p.h
blob: b187b7561dff0d622a40cf27d6897e4be03b742f (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef QHTTPSERVERPARSER_P_H
#define QHTTPSERVERPARSER_P_H

#include <QtHttpServer/qhttpserverrequest.h>
#include <QtNetwork/private/qhttpheaderparser_p.h>
#include <QtCore/private/qbytedata_p.h>

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

QT_BEGIN_NAMESPACE

class QHttp2Stream;

class QHttpServerParser
{
    friend class QHttpServerRequest;

public:
    QHttpServerParser(const QHostAddress &remoteAddress, quint16 remotePort,
                      const QHostAddress &localAddress, quint16 localPort);

    quint16 port = 0;

    enum class State {
        NothingDone,
        ReadingRequestLine,
        ReadingHeader,
        ExpectContinue,
        ReadingData,
        AllDone,
    } state = State::NothingDone;

    QHttpHeaderParser headerParser;

    bool parseRequestLine(QByteArrayView line);
    qsizetype readRequestLine(QIODevice *socket);
    qsizetype readHeader(QIODevice *socket);
    qsizetype sendContinue(QIODevice *socket);
    qsizetype readBodyFast(QIODevice *socket);
    qsizetype readRequestBodyRaw(QIODevice *socket, qsizetype size);
    qsizetype readRequestBodyChunked(QIODevice *socket);
    qsizetype getChunkSize(QIODevice *socket, qsizetype *chunkSize);

    bool parse(QIODevice *socket);
#if QT_CONFIG(http)
    bool parse(QHttp2Stream *socket);
#endif
    void clear();

    qint64 contentLength() const;
    QByteArray headerField(const QByteArray &name) const
    { return headerParser.combinedHeaderValue(name); }

    QHostAddress remoteAddress;
    quint16 remotePort;
    QHostAddress localAddress;
    quint16 localPort;
    QUrl url;
    QHttpServerRequest::Method method;
    QHttpHeaders headers;
    QByteArray body;

    bool handling{false};
    qsizetype bodyLength;
    qsizetype contentRead;
    bool chunkedTransferEncoding;
    bool lastChunkRead;
    qsizetype currentChunkRead;
    qsizetype currentChunkSize;
    bool upgrade;

    QByteArray fragment;
    QByteDataBuffer bodyBuffer;
};

QT_END_NAMESPACE

#endif // QHTTPSERVERPARSER_P_H