/src/qtbase/src/network/access/qhttpheaderparser_p.h
Line | Count | Source |
1 | | // Copyright (C) 2022 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 | | // Qt-Security score:significant reason:default |
4 | | |
5 | | #ifndef QHTTPHEADERPARSER_H |
6 | | #define QHTTPHEADERPARSER_H |
7 | | |
8 | | // |
9 | | // W A R N I N G |
10 | | // ------------- |
11 | | // |
12 | | // This file is not part of the Qt API. It exists for the convenience |
13 | | // of the Network Access API. This header file may change from |
14 | | // version to version without notice, or even be removed. |
15 | | // |
16 | | // We mean it. |
17 | | // |
18 | | |
19 | | #include <QtNetwork/private/qtnetworkglobal_p.h> |
20 | | #include <QtNetwork/qhttpheaders.h> |
21 | | |
22 | | #include <QByteArray> |
23 | | #include <QList> |
24 | | #include <QString> |
25 | | |
26 | | QT_BEGIN_NAMESPACE |
27 | | |
28 | | namespace HeaderConstants { |
29 | | |
30 | | // We previously used 8K, which is common on server side, but it turned out to |
31 | | // not be enough for various uses. Historically Firefox used 10K as the limit of |
32 | | // a single field, but some Location headers and Authorization challenges can |
33 | | // get even longer. Other browsers, such as Chrome, instead have a limit on the |
34 | | // total size of all the headers (as well as extra limits on some of the |
35 | | // individual fields). We'll use 100K as our default limit, which would be a ridiculously large |
36 | | // header, with the possibility to override it where we need to. |
37 | | static constexpr qsizetype DEFAULT_MAX_HEADER_FIELD_SIZE = 100 * 1024; |
38 | | // Taken from http://httpd.apache.org/docs/2.2/mod/core.html#limitrequestfields |
39 | | static constexpr qsizetype DEFAULT_MAX_HEADER_FIELDS = 100; |
40 | | // Chromium has a limit on the total size of the header set to 256KB, |
41 | | // which is a reasonable default for QNetworkAccessManager. |
42 | | // https://stackoverflow.com/a/3436155 |
43 | | static constexpr qsizetype DEFAULT_MAX_TOTAL_HEADER_SIZE = 256 * 1024; |
44 | | // There is no strict upper limit on the phrase, but in practice it is |
45 | | // rarely longer than 28. 1024 is quite generous, allows custom messages |
46 | | static constexpr qsizetype MAX_REASON_PHRASE_SIZE = 1024; |
47 | | } |
48 | | |
49 | | class Q_NETWORK_EXPORT QHttpHeaderParser |
50 | | { |
51 | | public: |
52 | | QHttpHeaderParser(); |
53 | | |
54 | | void clear(); |
55 | | bool parseHeaders(QByteArrayView headers); |
56 | | bool parseStatus(QByteArrayView status); |
57 | | |
58 | | const QHttpHeaders& headers() const &; |
59 | | QHttpHeaders headers() &&; |
60 | | void setStatusCode(int code); |
61 | | int getStatusCode() const; |
62 | | int getMajorVersion() const; |
63 | | void setMajorVersion(int version); |
64 | | int getMinorVersion() const; |
65 | | void setMinorVersion(int version); |
66 | | QString getReasonPhrase() const; |
67 | | void setReasonPhrase(const QString &reason); |
68 | | |
69 | | QByteArray firstHeaderField(QByteArrayView name, |
70 | | const QByteArray &defaultValue = QByteArray()) const; |
71 | | QByteArray combinedHeaderValue(QByteArrayView name, |
72 | | const QByteArray &defaultValue = QByteArray()) const; |
73 | | QList<QByteArray> headerFieldValues(QByteArrayView name) const; |
74 | | void setHeaderField(const QByteArray &name, const QByteArray &data); |
75 | | void prependHeaderField(const QByteArray &name, const QByteArray &data); |
76 | | void appendHeaderField(const QByteArray &name, const QByteArray &data); |
77 | | void removeHeaderField(QByteArrayView name); |
78 | | void clearHeaders(); |
79 | | |
80 | 0 | void setMaxHeaderFieldSize(qsizetype size) { |
81 | 0 | Q_ASSERT(size > 0); |
82 | 0 | maxFieldSize = size; |
83 | 0 | } |
84 | 0 | qsizetype maxHeaderFieldSize() const { return maxFieldSize; } |
85 | | |
86 | 0 | void setMaxTotalHeaderSize(qsizetype size) { |
87 | 0 | Q_ASSERT(size > 0); |
88 | 0 | maxTotalSize = size; |
89 | 0 | } |
90 | 0 | qsizetype maxTotalHeaderSize() const { return maxTotalSize; } |
91 | | |
92 | 0 | qsizetype maxRawHeaderSize() const { |
93 | 0 | constexpr qsizetype TrailingLineEndings = 4; |
94 | 0 | qsizetype result; |
95 | 0 | if (qAddOverflow(maxTotalSize, TrailingLineEndings, &result)) |
96 | 0 | return (std::numeric_limits<qsizetype>::max)(); |
97 | 0 | return result; |
98 | 0 | } |
99 | | |
100 | 0 | void setMaxHeaderFields(qsizetype size) { |
101 | 0 | Q_ASSERT(size > 0); |
102 | 0 | maxFieldCount = size; |
103 | 0 | } |
104 | 0 | qsizetype maxHeaderFields() const { return maxFieldCount; } |
105 | | |
106 | | private: |
107 | | QHttpHeaders fields; |
108 | | QString reasonPhrase; |
109 | | int statusCode; |
110 | | int majorVersion; |
111 | | int minorVersion; |
112 | | |
113 | | qsizetype maxFieldSize = HeaderConstants::DEFAULT_MAX_HEADER_FIELD_SIZE; |
114 | | qsizetype maxTotalSize = HeaderConstants::DEFAULT_MAX_TOTAL_HEADER_SIZE; |
115 | | qsizetype maxFieldCount = HeaderConstants::DEFAULT_MAX_HEADER_FIELDS; |
116 | | }; |
117 | | |
118 | | |
119 | | QT_END_NAMESPACE |
120 | | |
121 | | #endif // QHTTPHEADERPARSER_H |