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
4#ifndef QHTTPHEADERPARSER_H
5#define QHTTPHEADERPARSER_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 for the convenience
12// of the Network Access API. This header file may change from
13// version to version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtNetwork/private/qtnetworkglobal_p.h>
19
20#include <QByteArray>
21#include <QList>
22#include <QPair>
23#include <QString>
24
25QT_BEGIN_NAMESPACE
26
27namespace HeaderConstants {
28
29// We previously used 8K, which is common on server side, but it turned out to
30// not be enough for various uses. Historically Firefox used 10K as the limit of
31// a single field, but some Location headers and Authorization challenges can
32// get even longer. Other browsers, such as Chrome, instead have a limit on the
33// total size of all the headers (as well as extra limits on some of the
34// individual fields). We'll use 100K as our default limit, which would be a ridiculously large
35// header, with the possibility to override it where we need to.
36static constexpr int MAX_HEADER_FIELD_SIZE = 100 * 1024;
37// Taken from http://httpd.apache.org/docs/2.2/mod/core.html#limitrequestfields
38static constexpr int MAX_HEADER_FIELDS = 100;
39// Chromium has a limit on the total size of the header set to 256KB,
40// which is a reasonable default for QNetworkAccessManager.
41// https://stackoverflow.com/a/3436155
42static constexpr int MAX_TOTAL_HEADER_SIZE = 256 * 1024;
43
44}
45
46class Q_NETWORK_PRIVATE_EXPORT QHttpHeaderParser
47{
48public:
49 QHttpHeaderParser();
50
51 void clear();
52 bool parseHeaders(QByteArrayView headers);
53 bool parseStatus(QByteArrayView status);
54
55 const QList<QPair<QByteArray, QByteArray> >& headers() const;
56 void setStatusCode(int code);
57 int getStatusCode() const;
58 int getMajorVersion() const;
59 void setMajorVersion(int version);
60 int getMinorVersion() const;
61 void setMinorVersion(int version);
62 QString getReasonPhrase() const;
63 void setReasonPhrase(const QString &reason);
64
65 QByteArray firstHeaderField(const QByteArray &name,
66 const QByteArray &defaultValue = QByteArray()) const;
67 QByteArray combinedHeaderValue(const QByteArray &name,
68 const QByteArray &defaultValue = QByteArray()) const;
69 QList<QByteArray> headerFieldValues(const QByteArray &name) const;
70 void setHeaderField(const QByteArray &name, const QByteArray &data);
71 void prependHeaderField(const QByteArray &name, const QByteArray &data);
72 void appendHeaderField(const QByteArray &name, const QByteArray &data);
73 void removeHeaderField(const QByteArray &name);
74 void clearHeaders();
75
76 void setMaxHeaderFieldSize(qsizetype size) { maxFieldSize = size; }
77 qsizetype maxHeaderFieldSize() const { return maxFieldSize; }
78
79 void setMaxTotalHeaderSize(qsizetype size) { maxTotalSize = size; }
80 qsizetype maxTotalHeaderSize() const { return maxTotalSize; }
81
82 void setMaxHeaderFields(qsizetype count) { maxFieldCount = count; }
83 qsizetype maxHeaderFields() const { return maxFieldCount; }
84
85private:
86 QList<QPair<QByteArray, QByteArray> > fields;
87 QString reasonPhrase;
88 int statusCode;
89 int majorVersion;
90 int minorVersion;
91
92 qsizetype maxFieldSize = HeaderConstants::MAX_HEADER_FIELD_SIZE;
93 qsizetype maxTotalSize = HeaderConstants::MAX_TOTAL_HEADER_SIZE;
94 qsizetype maxFieldCount = HeaderConstants::MAX_HEADER_FIELDS;
95};
96
97
98QT_END_NAMESPACE
99
100#endif // QHTTPHEADERPARSER_H
101

source code of qtbase/src/network/access/qhttpheaderparser_p.h