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/qhashfunctions.h>
12#include <QtCore/qmetaobject.h>
13#include <QtCore/qobjectdefs.h>
14#include <QtCore/qshareddata.h>
15#include <QtCore/qcontainerfwd.h>
16#include <QtCore/qspan.h>
17#include <QtCore/qcomparehelpers.h>
18
19#include <initializer_list>
20#include <optional>
21
22QT_BEGIN_NAMESPACE
23
24class QDataStream;
25class QDebug;
26
27struct QT_TECH_PREVIEW_API QHttpHeaderRangeSpec // ### TP: what about non-integral ranges?
28{
29 std::optional<qint64> start;
30 std::optional<qint64> end;
31
32 constexpr bool isValid() const noexcept
33 {
34 if (!start && !end)
35 return false;
36 if (start && start < 0)
37 return false;
38 if (end && end < 0)
39 return false;
40 if (start && end && *start > *end)
41 return false;
42 return true;
43 }
44
45#ifndef QT_NO_DEBUG_STREAM
46 friend Q_NETWORK_EXPORT QDebug operator<<(QDebug debug, const QHttpHeaderRangeSpec &range);
47#endif
48
49private:
50 friend constexpr bool comparesEqual(const QHttpHeaderRangeSpec &lhs,
51 const QHttpHeaderRangeSpec &rhs) noexcept
52 {
53 return lhs.start == rhs.start && lhs.end == rhs.end;
54 }
55 Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QHttpHeaderRangeSpec)
56
57 friend size_t qHash(QHttpHeaderRangeSpec range, size_t seed = 0) noexcept
58 {
59 return qHashMulti(seed, range.start, range.end);
60 }
61};
62
64QT_DECLARE_QESDP_SPECIALIZATION_DTOR(QHttpHeaderRangeSetPrivate)
65class QT_TECH_PREVIEW_API QHttpHeaderRangeSet // ### TP: only does bytes=, but sounds like it could also do seconds=
66{
67 QExplicitlySharedDataPointer<QHttpHeaderRangeSetPrivate> d_ptr;
68 Q_DECLARE_PRIVATE(QHttpHeaderRangeSet)
69public:
70 Q_NETWORK_EXPORT QHttpHeaderRangeSet();
71 Q_NETWORK_EXPORT QHttpHeaderRangeSet(const QHttpHeaderRangeSet &other);
72 QHttpHeaderRangeSet(QHttpHeaderRangeSet &&) = default;
73 Q_NETWORK_EXPORT Q_IMPLICIT QHttpHeaderRangeSet(QSpan<const QHttpHeaderRangeSpec> r);
74 Q_IMPLICIT QHttpHeaderRangeSet(std::initializer_list<QHttpHeaderRangeSpec> r)
75 : QHttpHeaderRangeSet(QSpan<const QHttpHeaderRangeSpec>{r}) {}
76 Q_NETWORK_EXPORT QHttpHeaderRangeSet &operator=(const QHttpHeaderRangeSet &other);
77 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QHttpHeaderRangeSet)
78 Q_NETWORK_EXPORT ~QHttpHeaderRangeSet();
79
80 void swap(QHttpHeaderRangeSet &other) noexcept { d_ptr.swap(other.d_ptr); }
81
82 Q_NETWORK_EXPORT QSpan<const QHttpHeaderRangeSpec> ranges() const noexcept;
83 Q_NETWORK_EXPORT void setRanges(QSpan<const QHttpHeaderRangeSpec> r);
84
85 // ### normalization (a canonical form of sorted, non-overlapping ranges)
86 // and the set functions on top of it to be added in 6.13:
87 // - normalize/normalized/isNormalized
88 // - contains
89 // - unite
90 // - intersect
91 // ...
92
93private:
94#ifndef QT_NO_DEBUG_STREAM
95 friend Q_NETWORK_EXPORT QDebug operator<<(QDebug debug, const QHttpHeaderRangeSet &ranges);
96#endif
97
98 Q_NETWORK_EXPORT friend bool comparesEqual(const QHttpHeaderRangeSet &lhs,
99 const QHttpHeaderRangeSet &rhs) noexcept;
100 Q_DECLARE_EQUALITY_COMPARABLE(QHttpHeaderRangeSet)
101
102 Q_NETWORK_EXPORT friend size_t qHash(const QHttpHeaderRangeSet &key, size_t seed) noexcept;
103 friend size_t qHash(const QHttpHeaderRangeSet &key) noexcept { return qHash(key, size_t{0}); }
104};
105
107
109QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QHttpHeadersPrivate, Q_NETWORK_EXPORT)
111{
112 Q_GADGET_EXPORT(Q_NETWORK_EXPORT)
113public:
114 enum class WellKnownHeader {
115 // IANA Permanent status:
288 // IANA Deprecated status:
294 };
295 Q_ENUM(WellKnownHeader)
296
299
301 QHttpHeaders(QHttpHeaders &&other) noexcept = default;
304 void swap(QHttpHeaders &other) noexcept { d.swap(other.d); }
305
307 Q_NETWORK_EXPORT bool append(WellKnownHeader name, QAnyStringView value);
308
309 Q_NETWORK_EXPORT bool insert(qsizetype i, QAnyStringView name, QAnyStringView value);
310 Q_NETWORK_EXPORT bool insert(qsizetype i, WellKnownHeader name, QAnyStringView value);
311
312 Q_NETWORK_EXPORT bool replace(qsizetype i, QAnyStringView name, QAnyStringView newValue);
313 Q_NETWORK_EXPORT bool replace(qsizetype i, WellKnownHeader name, QAnyStringView newValue);
314
315 Q_NETWORK_EXPORT bool replaceOrAppend(QAnyStringView name, QAnyStringView newValue);
316 Q_NETWORK_EXPORT bool replaceOrAppend(WellKnownHeader name, QAnyStringView newValue);
317
318 Q_NETWORK_EXPORT bool contains(QAnyStringView name) const;
319 Q_NETWORK_EXPORT bool contains(WellKnownHeader name) const;
320
321 Q_NETWORK_EXPORT void clear();
322 Q_NETWORK_EXPORT void removeAll(QAnyStringView name);
323 Q_NETWORK_EXPORT void removeAll(WellKnownHeader name);
324 Q_NETWORK_EXPORT void removeAt(qsizetype i);
325
326 Q_NETWORK_EXPORT QByteArrayView value(QAnyStringView name, QByteArrayView defaultValue = {}) const noexcept;
328
331
333 Q_NETWORK_EXPORT QLatin1StringView nameAt(qsizetype i) const noexcept;
334
335 Q_NETWORK_EXPORT QByteArray combinedValue(QAnyStringView name) const;
337
340
343
345
348
351
353
354 Q_NETWORK_EXPORT void setDateTimeValue(QAnyStringView name, const QDateTime &dateTime);
355 Q_NETWORK_EXPORT void setDateTimeValue(WellKnownHeader name, const QDateTime &dateTime);
356
357 Q_NETWORK_EXPORT std::optional<QHttpHeaderRangeSet> rangeValues() const;
358 Q_NETWORK_EXPORT void setRangeValues(const QHttpHeaderRangeSet &ranges);
359
360 Q_NETWORK_EXPORT qsizetype size() const noexcept;
361 Q_NETWORK_EXPORT void reserve(qsizetype size);
362 bool isEmpty() const noexcept { return size() == 0; }
363
364 Q_NETWORK_EXPORT static QByteArrayView wellKnownHeaderName(WellKnownHeader name) noexcept;
365
366 Q_NETWORK_EXPORT static QHttpHeaders
368 Q_NETWORK_EXPORT static QHttpHeaders
370 Q_NETWORK_EXPORT static QHttpHeaders
372
376
377private:
378#ifndef QT_NO_DEBUG_STREAM
380#endif
381
382#ifndef QT_NO_DATASTREAM
385#endif
386
387 Q_ALWAYS_INLINE void verify([[maybe_unused]] qsizetype pos = 0,
388 [[maybe_unused]] qsizetype n = 1) const
389 {
390 Q_ASSERT(pos >= 0);
391 Q_ASSERT(pos <= size());
392 Q_ASSERT(n >= 0);
393 Q_ASSERT(n <= size() - pos);
394 }
395 QExplicitlySharedDataPointer<QHttpHeadersPrivate> d;
396};
397
398Q_DECLARE_SHARED(QHttpHeaders)
399
400QT_END_NAMESPACE
401
402#endif // QHTTPHEADERS_H
QHttpHeaderRangeSet represents the byte ranges of an HTTP {Range} header.
Q_NETWORK_EXPORT void setRangeValues(const QHttpHeaderRangeSet &ranges)
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 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
QHttpHeaderRangeSpec represents a single byte range as used in the HTTP {Range} and {Content-Range}...