Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qhttpheaderparser_p.h
Go to the documentation of this file.
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
26QT_BEGIN_NAMESPACE
27
28namespace 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.
37static constexpr qsizetype DEFAULT_MAX_HEADER_FIELD_SIZE = 100 * 1024;
38// Taken from http://httpd.apache.org/docs/2.2/mod/core.html#limitrequestfields
39static 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
43static 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
46static constexpr qsizetype MAX_REASON_PHRASE_SIZE = 1024;
47}
48
49class Q_NETWORK_EXPORT QHttpHeaderParser
50{
51public:
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 void setMaxHeaderFieldSize(qsizetype size) {
81 Q_ASSERT(size > 0);
82 maxFieldSize = size;
83 }
84 qsizetype maxHeaderFieldSize() const { return maxFieldSize; }
85
86 void setMaxTotalHeaderSize(qsizetype size) {
87 Q_ASSERT(size > 0);
88 maxTotalSize = size;
89 }
90 qsizetype maxTotalHeaderSize() const { return maxTotalSize; }
91
92 void setMaxHeaderFields(qsizetype size) {
93 Q_ASSERT(size > 0);
94 maxFieldCount = size;
95 }
96 qsizetype maxHeaderFields() const { return maxFieldCount; }
97
98private:
99 QHttpHeaders fields;
100 QString reasonPhrase;
101 int statusCode;
102 int majorVersion;
103 int minorVersion;
104
105 qsizetype maxFieldSize = HeaderConstants::DEFAULT_MAX_HEADER_FIELD_SIZE;
106 qsizetype maxTotalSize = HeaderConstants::DEFAULT_MAX_TOTAL_HEADER_SIZE;
107 qsizetype maxFieldCount = HeaderConstants::DEFAULT_MAX_HEADER_FIELDS;
108};
109
110
111QT_END_NAMESPACE
112
113#endif // QHTTPHEADERPARSER_H
static constexpr qsizetype DEFAULT_MAX_HEADER_FIELDS
static constexpr qsizetype DEFAULT_MAX_HEADER_FIELD_SIZE
static constexpr qsizetype MAX_REASON_PHRASE_SIZE
static constexpr qsizetype DEFAULT_MAX_TOTAL_HEADER_SIZE
static bool fieldNameCheck(QByteArrayView name)