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#include <QtNetwork/qhttpheaders.h>
20
21#include <QByteArray>
22#include <QList>
23#include <QPair>
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 int MAX_HEADER_FIELD_SIZE = 100 * 1024;
38// Taken from http://httpd.apache.org/docs/2.2/mod/core.html#limitrequestfields
39static constexpr int 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 int MAX_TOTAL_HEADER_SIZE = 256 * 1024;
44
45}
46
47class Q_NETWORK_EXPORT QHttpHeaderParser
48{
49public:
50 QHttpHeaderParser();
51
52 void clear();
53 bool parseHeaders(QByteArrayView headers);
54 bool parseStatus(QByteArrayView status);
55
56 const QHttpHeaders& headers() const &;
57 QHttpHeaders headers() &&;
58 void setStatusCode(int code);
59 int getStatusCode() const;
60 int getMajorVersion() const;
61 void setMajorVersion(int version);
62 int getMinorVersion() const;
63 void setMinorVersion(int version);
64 QString getReasonPhrase() const;
65 void setReasonPhrase(const QString &reason);
66
67 QByteArray firstHeaderField(QByteArrayView name,
68 const QByteArray &defaultValue = QByteArray()) const;
69 QByteArray combinedHeaderValue(QByteArrayView name,
70 const QByteArray &defaultValue = QByteArray()) const;
71 QList<QByteArray> headerFieldValues(QByteArrayView name) const;
72 void setHeaderField(const QByteArray &name, const QByteArray &data);
73 void prependHeaderField(const QByteArray &name, const QByteArray &data);
74 void appendHeaderField(const QByteArray &name, const QByteArray &data);
75 void removeHeaderField(QByteArrayView name);
76 void clearHeaders();
77
78 void setMaxHeaderFieldSize(qsizetype size) { maxFieldSize = size; }
79 qsizetype maxHeaderFieldSize() const { return maxFieldSize; }
80
81 void setMaxTotalHeaderSize(qsizetype size) { maxTotalSize = size; }
82 qsizetype maxTotalHeaderSize() const { return maxTotalSize; }
83
84 void setMaxHeaderFields(qsizetype count) { maxFieldCount = count; }
85 qsizetype maxHeaderFields() const { return maxFieldCount; }
86
87private:
88 QHttpHeaders fields;
89 QString reasonPhrase;
90 int statusCode;
91 int majorVersion;
92 int minorVersion;
93
94 qsizetype maxFieldSize = HeaderConstants::MAX_HEADER_FIELD_SIZE;
95 qsizetype maxTotalSize = HeaderConstants::MAX_TOTAL_HEADER_SIZE;
96 qsizetype maxFieldCount = HeaderConstants::MAX_HEADER_FIELDS;
97};
98
99
100QT_END_NAMESPACE
101
102#endif // QHTTPHEADERPARSER_H
103

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

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