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 QDataStream;
22class QDebug;
23
25{
26public:
27 constexpr QHttpHeaderRange() noexcept = default;
28 constexpr QHttpHeaderRange(std::optional<qint64> start, std::optional<qint64> end) noexcept
29 : m_start(start), m_end(end) {}
30
31 constexpr std::optional<qint64> start() const noexcept { return m_start; }
32 constexpr std::optional<qint64> end() const noexcept { return m_end; }
33 constexpr void setStart(std::optional<qint64> start) noexcept { m_start = start; }
34 constexpr void setEnd(std::optional<qint64> end) noexcept { m_end = end; }
35
36 constexpr bool isValid() const noexcept
37 {
38 if (!m_start && !m_end)
39 return false;
40 if (m_start && m_start < 0)
41 return false;
42 if (m_end && m_end < 0)
43 return false;
44 if (m_start && m_end && *m_start > *m_end)
45 return false;
46 return true;
47 }
48
49#ifndef QT_NO_DEBUG_STREAM
51#endif
52
53private:
54 friend constexpr bool comparesEqual(const QHttpHeaderRange &lhs,
55 const QHttpHeaderRange &rhs) noexcept
56 {
57 return lhs.m_start == rhs.m_start && lhs.m_end == rhs.m_end;
58 }
60
62 {
63 return qHashMulti(seed, range.start().value_or(0), range.end().value_or(0));
64 }
65
67 std::optional<qint64> m_end;
68};
69
71QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QHttpHeadersPrivate, Q_NETWORK_EXPORT)
73{
74 Q_GADGET_EXPORT(Q_NETWORK_EXPORT)
75public:
76 enum class WellKnownHeader {
77 // IANA Permanent status:
250 // IANA Deprecated status:
256 };
257 Q_ENUM(WellKnownHeader)
258
261
263 QHttpHeaders(QHttpHeaders &&other) noexcept = default;
266 void swap(QHttpHeaders &other) noexcept { d.swap(other.d); }
267
269 Q_NETWORK_EXPORT bool append(WellKnownHeader name, QAnyStringView value);
270
271 Q_NETWORK_EXPORT bool insert(qsizetype i, QAnyStringView name, QAnyStringView value);
272 Q_NETWORK_EXPORT bool insert(qsizetype i, WellKnownHeader name, QAnyStringView value);
273
274 Q_NETWORK_EXPORT bool replace(qsizetype i, QAnyStringView name, QAnyStringView newValue);
275 Q_NETWORK_EXPORT bool replace(qsizetype i, WellKnownHeader name, QAnyStringView newValue);
276
277 Q_NETWORK_EXPORT bool replaceOrAppend(QAnyStringView name, QAnyStringView newValue);
278 Q_NETWORK_EXPORT bool replaceOrAppend(WellKnownHeader name, QAnyStringView newValue);
279
280 Q_NETWORK_EXPORT bool contains(QAnyStringView name) const;
281 Q_NETWORK_EXPORT bool contains(WellKnownHeader name) const;
282
283 Q_NETWORK_EXPORT void clear();
284 Q_NETWORK_EXPORT void removeAll(QAnyStringView name);
285 Q_NETWORK_EXPORT void removeAll(WellKnownHeader name);
286 Q_NETWORK_EXPORT void removeAt(qsizetype i);
287
288 Q_NETWORK_EXPORT QByteArrayView value(QAnyStringView name, QByteArrayView defaultValue = {}) const noexcept;
290
293
295 Q_NETWORK_EXPORT QLatin1StringView nameAt(qsizetype i) const noexcept;
296
297 Q_NETWORK_EXPORT QByteArray combinedValue(QAnyStringView name) const;
299
302
305
307
310
313
315
316 Q_NETWORK_EXPORT void setDateTimeValue(QAnyStringView name, const QDateTime &dateTime);
317 Q_NETWORK_EXPORT void setDateTimeValue(WellKnownHeader name, const QDateTime &dateTime);
318
319 Q_NETWORK_EXPORT QList<QHttpHeaderRange> rangeValues(bool *ok = nullptr) const;
320 Q_NETWORK_EXPORT void setRangeValues(QSpan<const QHttpHeaderRange> ranges);
321
322 Q_NETWORK_EXPORT qsizetype size() const noexcept;
323 Q_NETWORK_EXPORT void reserve(qsizetype size);
324 bool isEmpty() const noexcept { return size() == 0; }
325
326 Q_NETWORK_EXPORT static QByteArrayView wellKnownHeaderName(WellKnownHeader name) noexcept;
327
328 Q_NETWORK_EXPORT static QHttpHeaders
330 Q_NETWORK_EXPORT static QHttpHeaders
332 Q_NETWORK_EXPORT static QHttpHeaders
334
338
339private:
340#ifndef QT_NO_DEBUG_STREAM
342#endif
343
344#ifndef QT_NO_DATASTREAM
347#endif
348
349 Q_ALWAYS_INLINE void verify([[maybe_unused]] qsizetype pos = 0,
350 [[maybe_unused]] qsizetype n = 1) const
351 {
352 Q_ASSERT(pos >= 0);
353 Q_ASSERT(pos <= size());
354 Q_ASSERT(n >= 0);
355 Q_ASSERT(n <= size() - pos);
356 }
357 QExplicitlySharedDataPointer<QHttpHeadersPrivate> d;
358};
359
360Q_DECLARE_SHARED(QHttpHeaders)
361
362QT_END_NAMESPACE
363
364#endif // QHTTPHEADERS_H
QHttpHeaderRange represents a single byte range as used in the HTTP {Range} and {Content-Range}...
constexpr QHttpHeaderRange() noexcept=default
Constructs a default-initialized range.
friend constexpr bool comparesEqual(const QHttpHeaderRange &lhs, const QHttpHeaderRange &rhs) noexcept
constexpr std::optional< qint64 > end() const noexcept
Returns the end byte offset of the range, or {std::nullopt} if no end was set.
constexpr void setStart(std::optional< qint64 > start) noexcept
Sets the start byte offset to start.
constexpr QHttpHeaderRange(std::optional< qint64 > start, std::optional< qint64 > end) noexcept
Constructs a range with start and end byte offsets.
constexpr void setEnd(std::optional< qint64 > end) noexcept
Sets the end byte offset to end.
constexpr std::optional< qint64 > start() const noexcept
Returns the start byte offset of the range, or {std::nullopt} if no start was set.
constexpr bool isValid() const noexcept
Returns true if the range is well-formed, false otherwise.
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