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
qhttpheaders.h
Go to the documentation of this file.
1// Copyright (C) 2023 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 QHTTPHEADERS_H
6#define QHTTPHEADERS_H
7
8#include <QtNetwork/qtnetworkglobal.h>
9
10#include <QtCore/qdatetime.h>
11#include <QtCore/qmetaobject.h>
12#include <QtCore/qobjectdefs.h>
13#include <QtCore/qshareddata.h>
14#include <QtCore/qcontainerfwd.h>
15#include <QtCore/qspan.h>
16#include <QtCore/qcomparehelpers.h>
17#include <optional>
18
19QT_BEGIN_NAMESPACE
20
21class QDebug;
22
24{
25public:
26 constexpr QHttpHeaderRange() noexcept = default;
27 constexpr QHttpHeaderRange(std::optional<qint64> start, std::optional<qint64> end) noexcept
28 : m_start(start), m_end(end) {}
29
30 constexpr std::optional<qint64> start() const noexcept { return m_start; }
31 constexpr std::optional<qint64> end() const noexcept { return m_end; }
32 constexpr void setStart(std::optional<qint64> start) noexcept { m_start = start; }
33 constexpr void setEnd(std::optional<qint64> end) noexcept { m_end = end; }
34
35 constexpr bool isValid() const noexcept
36 {
37 if (!m_start && !m_end)
38 return false;
39 if (m_start && m_start < 0)
40 return false;
41 if (m_end && m_end < 0)
42 return false;
43 if (m_start && m_end && *m_start > *m_end)
44 return false;
45 return true;
46 }
47
48#ifndef QT_NO_DEBUG_STREAM
49 friend QDebug operator<<(QDebug debug, const QHttpHeaderRange &range);
50#endif
51
52private:
53 friend constexpr bool comparesEqual(const QHttpHeaderRange &lhs,
54 const QHttpHeaderRange &rhs) noexcept
55 {
56 return lhs.m_start == rhs.m_start && lhs.m_end == rhs.m_end;
57 }
59
61 std::optional<qint64> m_end;
62};
63
64inline size_t qHash(QHttpHeaderRange range, size_t seed = 0) noexcept
65{
66 return qHashMulti(seed, range.start().value_or(0), range.end().value_or(0));
67}
68
70QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QHttpHeadersPrivate, Q_NETWORK_EXPORT)
72{
73 Q_GADGET_EXPORT(Q_NETWORK_EXPORT)
74public:
75 enum class WellKnownHeader {
76 // IANA Permanent status:
249 // IANA Deprecated status:
255 };
256 Q_ENUM(WellKnownHeader)
257
260
262 QHttpHeaders(QHttpHeaders &&other) noexcept = default;
265 void swap(QHttpHeaders &other) noexcept { d.swap(other.d); }
266
268 Q_NETWORK_EXPORT bool append(WellKnownHeader name, QAnyStringView value);
269
270 Q_NETWORK_EXPORT bool insert(qsizetype i, QAnyStringView name, QAnyStringView value);
271 Q_NETWORK_EXPORT bool insert(qsizetype i, WellKnownHeader name, QAnyStringView value);
272
273 Q_NETWORK_EXPORT bool replace(qsizetype i, QAnyStringView name, QAnyStringView newValue);
274 Q_NETWORK_EXPORT bool replace(qsizetype i, WellKnownHeader name, QAnyStringView newValue);
275
276 Q_NETWORK_EXPORT bool replaceOrAppend(QAnyStringView name, QAnyStringView newValue);
277 Q_NETWORK_EXPORT bool replaceOrAppend(WellKnownHeader name, QAnyStringView newValue);
278
279 Q_NETWORK_EXPORT bool contains(QAnyStringView name) const;
280 Q_NETWORK_EXPORT bool contains(WellKnownHeader name) const;
281
282 Q_NETWORK_EXPORT void clear();
283 Q_NETWORK_EXPORT void removeAll(QAnyStringView name);
284 Q_NETWORK_EXPORT void removeAll(WellKnownHeader name);
285 Q_NETWORK_EXPORT void removeAt(qsizetype i);
286
287 Q_NETWORK_EXPORT QByteArrayView value(QAnyStringView name, QByteArrayView defaultValue = {}) const noexcept;
289
292
294 Q_NETWORK_EXPORT QLatin1StringView nameAt(qsizetype i) const noexcept;
295
296 Q_NETWORK_EXPORT QByteArray combinedValue(QAnyStringView name) const;
298
301
304
306
309
312
314
315 Q_NETWORK_EXPORT void setDateTimeValue(QAnyStringView name, const QDateTime &dateTime);
316 Q_NETWORK_EXPORT void setDateTimeValue(WellKnownHeader name, const QDateTime &dateTime);
317
318 Q_NETWORK_EXPORT QList<QHttpHeaderRange> rangeValues(bool *ok = nullptr) const;
319 Q_NETWORK_EXPORT void setRangeValues(QSpan<const QHttpHeaderRange> ranges);
320
321 Q_NETWORK_EXPORT qsizetype size() const noexcept;
322 Q_NETWORK_EXPORT void reserve(qsizetype size);
323 bool isEmpty() const noexcept { return size() == 0; }
324
325 Q_NETWORK_EXPORT static QByteArrayView wellKnownHeaderName(WellKnownHeader name) noexcept;
326
327 Q_NETWORK_EXPORT static QHttpHeaders
329 Q_NETWORK_EXPORT static QHttpHeaders
331 Q_NETWORK_EXPORT static QHttpHeaders
333
337
338private:
339#ifndef QT_NO_DEBUG_STREAM
341#endif
342
343#ifndef QT_NO_DATASTREAM
346#endif
347
348 Q_ALWAYS_INLINE void verify([[maybe_unused]] qsizetype pos = 0,
349 [[maybe_unused]] qsizetype n = 1) const
350 {
351 Q_ASSERT(pos >= 0);
352 Q_ASSERT(pos <= size());
353 Q_ASSERT(n >= 0);
354 Q_ASSERT(n <= size() - pos);
355 }
356 QExplicitlySharedDataPointer<QHttpHeadersPrivate> d;
357};
358
359Q_DECLARE_SHARED(QHttpHeaders)
360
361QT_END_NAMESPACE
362
363#endif // QHTTPHEADERS_H
constexpr QHttpHeaderRange() noexcept=default
friend constexpr bool comparesEqual(const QHttpHeaderRange &lhs, const QHttpHeaderRange &rhs) noexcept
constexpr std::optional< qint64 > end() const noexcept
constexpr void setStart(std::optional< qint64 > start) noexcept
constexpr QHttpHeaderRange(std::optional< qint64 > start, std::optional< qint64 > end) noexcept
constexpr void setEnd(std::optional< qint64 > end) noexcept
constexpr std::optional< qint64 > start() const noexcept
constexpr bool isValid() const noexcept
Q_NETWORK_EXPORT void clear()
Clears all header entries.
bool isEmpty() const noexcept
Returns true if the headers have size 0; otherwise returns false.
Q_NETWORK_EXPORT bool insert(qsizetype i, QAnyStringView name, QAnyStringView value)
Inserts a header entry at index i, with name and value.
Q_NETWORK_EXPORT bool contains(QAnyStringView name) const
Returns whether the headers contain header with name.
Q_NETWORK_EXPORT ~QHttpHeaders()
Disposes of the headers object.
Q_NETWORK_EXPORT void removeAll(QAnyStringView name)
Removes the header name.
Q_NETWORK_EXPORT bool replaceOrAppend(QAnyStringView name, QAnyStringView newValue)
Q_NETWORK_EXPORT bool replace(qsizetype i, QAnyStringView name, QAnyStringView newValue)
Replaces the header entry at index i, with name and newValue.
Q_NETWORK_EXPORT void reserve(qsizetype size)
Attempts to allocate memory for at least size header entries.
Q_NETWORK_EXPORT void removeAt(qsizetype i)
Removes the header at index i.
QHttpHeaders(QHttpHeaders &&other) noexcept=default
Move-constructs the object from other, which will be left \l{isEmpty()}{empty}.
Q_NETWORK_EXPORT void setDateTimeValue(QAnyStringView name, const QDateTime &dateTime)
Q_NETWORK_EXPORT void setRangeValues(QSpan< const QHttpHeaderRange > ranges)
Q_NETWORK_EXPORT bool append(WellKnownHeader name, QAnyStringView value)
static QByteArrayView unescapeMaxAge(QByteArrayView value)
Definition qhsts.cpp:292
static bool isCTL(int c)
Definition qhsts.cpp:251
static bool isLWS(int c)
Definition qhsts.cpp:259
static bool isCHAR(int c)
Definition qhsts.cpp:245
static bool isTEXT(char c)
Definition qhsts.cpp:274
static bool isTOKEN(char c)
Definition qhsts.cpp:301
static bool isSeparator(char c)
Definition qhsts.cpp:281
size_t qHash(QHttpHeaderRange range, size_t seed=0) noexcept