QHttpHeaders Class
The QHttpHeaders class holds the header fields of an HTTP message. More…
| Header | #include <QHttpHeaders> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Network) |
| Module | Nt Network C++ Classes |
| Since | 6.7 |
List of all members, including inherited members
Public Types
| enum | WellKnownHeader |
Public Functions
| QHttpHeaders() | |
| QHttpHeaders(const QHttpHeaders &other) | |
| QHttpHeaders(QHttpHeaders &&other) | |
| QHttpHeaders & | operator=(const QHttpHeaders &other) |
| QHttpHeaders & | operator=(QHttpHeaders &&other) |
| ~QHttpHeaders() | |
| void | swap(QHttpHeaders &other) |
| bool | append(QByteArrayView name, QByteArrayView value) |
| bool | append(WellKnownHeader name, QByteArrayView value) |
| bool | insert(qsizetype i, QByteArrayView name, QByteArrayView value) |
| bool | insert(qsizetype i, WellKnownHeader name, QByteArrayView value) |
| bool | replace(qsizetype i, QByteArrayView name, QByteArrayView newValue) |
| bool | replace(qsizetype i, WellKnownHeader name, QByteArrayView newValue) |
| bool | replaceOrAppend(QByteArrayView name, QByteArrayView newValue) |
| bool | replaceOrAppend(WellKnownHeader name, QByteArrayView newValue) |
| bool | contains(QByteArrayView name) const |
| bool | contains(WellKnownHeader name) const |
| void | clear() |
| void | removeAll(QByteArrayView name) |
| void | removeAll(WellKnownHeader name) |
| void | removeAt(qsizetype i) |
| QByteArrayView | value(QByteArrayView name, QByteArrayView defaultValue = {}) const |
| QByteArrayView | value(WellKnownHeader name, QByteArrayView defaultValue = {}) const |
| QList<QByteArray> | values(QByteArrayView name) const |
| QList<QByteArray> | values(WellKnownHeader name) const |
| QByteArrayView | valueAt(qsizetype i) const |
| QLatin1StringView | nameAt(qsizetype i) const |
| QByteArray | combinedValue(QByteArrayView name) const |
| QByteArray | combinedValue(WellKnownHeader name) const |
| qsizetype | size() const |
| void | reserve(qsizetype size) |
| bool | isEmpty() const |
| QList<std::pair<QByteArray, QByteArray>> | toListOfPairs() const |
| QMultiMap<QByteArray, QByteArray> | toMultiMap() const |
| QMultiHash<QByteArray, QByteArray> | toMultiHash() const |
Static Public Members
| QByteArrayView | wellKnownHeaderName(WellKnownHeader name) |
| QHttpHeaders | fromListOfPairs(const QList<std::pair<QByteArray, QByteArray>> &headers) |
| QHttpHeaders | fromMultiMap(const QMultiMap<QByteArray, QByteArray> &headers) |
| QHttpHeaders | fromMultiHash(const QMultiHash<QByteArray, QByteArray> &headers) |
Detailed Description
A QHttpHeaders is an ordered list of name and value pairs. A name may occur more than once, and the order of the entries is kept, which matters for fields such as Set-Cookie. Names are compared without regard to case, and are stored and returned in lower case.
Names and values are checked when they are added: a name must be an HTTP token, and a value must not contain a line break or a NUL byte. Whitespace around a value is removed. The functions that add a field return false and leave the headers unchanged when a check fails, so untrusted input cannot inject further header lines.
QHttpHeaders headers;
headers.append(QHttpHeaders::WellKnownHeader::ContentType, "text/plain");
headers.append("x-request-id", "42");
QByteArrayView type = headers.value(QHttpHeaders::WellKnownHeader::ContentType);
See also QHttpServerRequest::headers() and QHttpServerResponse::setHeaders().
Differences from Qt
Names and values are passed as QByteArrayView (Qt takes QAnyStringView), so a QString must be converted with toUtf8() or toLatin1() first. WellKnownHeader has the common headers only, not the whole IANA registry. There are no intValue(), dateTimeValue() or QDebug streaming functions.
Member Type Documentation
enum QHttpHeaders::WellKnownHeader
Names of common header fields. wellKnownHeaderName() returns the name of each value in lower case.
| Constant | Description |
|---|---|
QHttpHeaders::AIM | a-im |
QHttpHeaders::Accept | accept |
QHttpHeaders::AcceptCharset | accept-charset |
QHttpHeaders::AcceptEncoding | accept-encoding |
QHttpHeaders::AcceptLanguage | accept-language |
QHttpHeaders::AcceptRanges | accept-ranges |
QHttpHeaders::AccessControlAllowCredentials | access-control-allow-credentials |
QHttpHeaders::AccessControlAllowHeaders | access-control-allow-headers |
QHttpHeaders::AccessControlAllowMethods | access-control-allow-methods |
QHttpHeaders::AccessControlAllowOrigin | access-control-allow-origin |
QHttpHeaders::AccessControlExposeHeaders | access-control-expose-headers |
QHttpHeaders::AccessControlMaxAge | access-control-max-age |
QHttpHeaders::AccessControlRequestHeaders | access-control-request-headers |
QHttpHeaders::AccessControlRequestMethod | access-control-request-method |
QHttpHeaders::Age | age |
QHttpHeaders::Allow | allow |
QHttpHeaders::AltSvc | alt-svc |
QHttpHeaders::Authorization | authorization |
QHttpHeaders::CacheControl | cache-control |
QHttpHeaders::Connection | connection |
QHttpHeaders::ContentDisposition | content-disposition |
QHttpHeaders::ContentEncoding | content-encoding |
QHttpHeaders::ContentLanguage | content-language |
QHttpHeaders::ContentLength | content-length |
QHttpHeaders::ContentLocation | content-location |
QHttpHeaders::ContentRange | content-range |
QHttpHeaders::ContentSecurityPolicy | content-security-policy |
QHttpHeaders::ContentType | content-type |
QHttpHeaders::Cookie | cookie |
QHttpHeaders::Date | date |
QHttpHeaders::ETag | etag |
QHttpHeaders::Expect | expect |
QHttpHeaders::Expires | expires |
QHttpHeaders::Forwarded | forwarded |
QHttpHeaders::From | from |
QHttpHeaders::Host | host |
QHttpHeaders::IfMatch | if-match |
QHttpHeaders::IfModifiedSince | if-modified-since |
QHttpHeaders::IfNoneMatch | if-none-match |
QHttpHeaders::IfRange | if-range |
QHttpHeaders::IfUnmodifiedSince | if-unmodified-since |
QHttpHeaders::KeepAlive | keep-alive |
QHttpHeaders::LastModified | last-modified |
QHttpHeaders::Link | link |
QHttpHeaders::Location | location |
QHttpHeaders::MaxForwards | max-forwards |
QHttpHeaders::Origin | origin |
QHttpHeaders::Pragma | pragma |
QHttpHeaders::ProxyAuthenticate | proxy-authenticate |
QHttpHeaders::ProxyAuthorization | proxy-authorization |
QHttpHeaders::Range | range |
QHttpHeaders::Referer | referer |
QHttpHeaders::RetryAfter | retry-after |
QHttpHeaders::SecWebSocketAccept | sec-websocket-accept |
QHttpHeaders::SecWebSocketExtensions | sec-websocket-extensions |
QHttpHeaders::SecWebSocketKey | sec-websocket-key |
QHttpHeaders::SecWebSocketProtocol | sec-websocket-protocol |
QHttpHeaders::SecWebSocketVersion | sec-websocket-version |
QHttpHeaders::Server | server |
QHttpHeaders::SetCookie | set-cookie |
QHttpHeaders::StrictTransportSecurity | strict-transport-security |
QHttpHeaders::TE | te |
QHttpHeaders::Trailer | trailer |
QHttpHeaders::TransferEncoding | transfer-encoding |
QHttpHeaders::Upgrade | upgrade |
QHttpHeaders::UserAgent | user-agent |
QHttpHeaders::Vary | vary |
QHttpHeaders::Via | via |
QHttpHeaders::WWWAuthenticate | www-authenticate |
QHttpHeaders::XContentTypeOptions | x-content-type-options |
QHttpHeaders::XForwardedFor | x-forwarded-for |
QHttpHeaders::XFrameOptions | x-frame-options |
Member Function Documentation
QHttpHeaders::QHttpHeaders()
Constructs an empty set of headers.
QHttpHeaders::QHttpHeaders(const QHttpHeaders &other)
Constructs a copy of other.
QHttpHeaders::QHttpHeaders(QHttpHeaders &&other)
Move-constructs headers from other, which is left empty.
QHttpHeaders::~QHttpHeaders()
Destroys the headers.
bool QHttpHeaders::append(QByteArrayView name, QByteArrayView value)
Appends a field with the given name and value. Returns true on success, or false if the name or the value is not valid.
See also insert() and replaceOrAppend().
bool QHttpHeaders::append(WellKnownHeader name, QByteArrayView value)
This is an overloaded function.
void QHttpHeaders::clear()
Removes all fields.
See also isEmpty().
QByteArray QHttpHeaders::combinedValue(QByteArrayView name) const
Returns the values of all fields named name joined with commas, the way HTTP combines repeated fields, or a null QByteArray if there is no such field.
See also values().
QByteArray QHttpHeaders::combinedValue(WellKnownHeader name) const
This is an overloaded function.
bool QHttpHeaders::contains(QByteArrayView name) const
Returns true if there is a field named name; otherwise returns false.
bool QHttpHeaders::contains(WellKnownHeader name) const
This is an overloaded function.
QHttpHeaders QHttpHeaders::fromListOfPairs(const QList<std::pair<QByteArray, QByteArray>> &headers)
Returns headers made from the name and value pairs in headers. Pairs whose name or value is not valid are skipped.
See also toListOfPairs().
QHttpHeaders QHttpHeaders::fromMultiHash(const QMultiHash<QByteArray, QByteArray> &headers)
Returns headers made from the entries of headers. Entries whose name or value is not valid are skipped.
See also toMultiHash().
QHttpHeaders QHttpHeaders::fromMultiMap(const QMultiMap<QByteArray, QByteArray> &headers)
Returns headers made from the entries of headers. Entries whose name or value is not valid are skipped.
See also toMultiMap().
bool QHttpHeaders::insert(qsizetype i, QByteArrayView name, QByteArrayView value)
Inserts a field with the given name and value at index i. Returns true on success, or false if i is out of range or the name or the value is not valid.
See also append().
bool QHttpHeaders::insert(qsizetype i, WellKnownHeader name, QByteArrayView value)
This is an overloaded function.
bool QHttpHeaders::isEmpty() const
Returns true if there are no header entries; otherwise returns false.
See also size().
QLatin1StringView QHttpHeaders::nameAt(qsizetype i) const
Returns the name of the field at index i, in lower case. i must be a valid index.
See also valueAt().
QHttpHeaders & QHttpHeaders::operator=(const QHttpHeaders &other)
Assigns other to these headers and returns a reference to them.
QHttpHeaders & QHttpHeaders::operator=(QHttpHeaders &&other)
This is an overloaded function.
Move-assigns other to these headers and returns a reference to them.
void QHttpHeaders::removeAll(QByteArrayView name)
Removes every field named name.
See also removeAt().
void QHttpHeaders::removeAll(WellKnownHeader name)
This is an overloaded function.
void QHttpHeaders::removeAt(qsizetype i)
Removes the field at index i. i must be a valid index.
See also removeAll().
bool QHttpHeaders::replace(qsizetype i, QByteArrayView name, QByteArrayView newValue)
Replaces the field at index i with one named name whose value is newValue. Returns true on success, or false if i is out of range or the name or the value is not valid.
See also replaceOrAppend().
bool QHttpHeaders::replace(qsizetype i, WellKnownHeader name, QByteArrayView newValue)
This is an overloaded function.
bool QHttpHeaders::replaceOrAppend(QByteArrayView name, QByteArrayView newValue)
Sets the field name to newValue. The first field with that name is replaced and any others are removed; if there is none, the field is appended. Returns true on success, or false if the name or the value is not valid.
See also append() and replace().
bool QHttpHeaders::replaceOrAppend(WellKnownHeader name, QByteArrayView newValue)
This is an overloaded function.
void QHttpHeaders::reserve(qsizetype size)
Reserves space for size header entries.
qsizetype QHttpHeaders::size() const
Returns the number of header entries. A name that occurs several times is counted once per entry.
See also isEmpty().
void QHttpHeaders::swap(QHttpHeaders &other)
Swaps these headers with other. This operation is very fast and never fails.
QList<std::pair<QByteArray, QByteArray>> QHttpHeaders::toListOfPairs() const
Returns the fields as a list of name and value pairs, in order.
See also fromListOfPairs().
QMultiHash<QByteArray, QByteArray> QHttpHeaders::toMultiHash() const
Returns the fields as a hash from name to value.
See also fromMultiHash().
QMultiMap<QByteArray, QByteArray> QHttpHeaders::toMultiMap() const
Returns the fields as a map from name to value.
See also fromMultiMap().
QByteArrayView QHttpHeaders::value(QByteArrayView name, QByteArrayView defaultValue = {}) const
Returns the value of the first field named name, or defaultValue if there is none. The view is invalidated when the headers change.
See also values() and combinedValue().
QByteArrayView QHttpHeaders::value(WellKnownHeader name, QByteArrayView defaultValue = {}) const
This is an overloaded function.
QByteArrayView QHttpHeaders::valueAt(qsizetype i) const
Returns the value of the field at index i. i must be a valid index.
See also nameAt().
QList<QByteArray> QHttpHeaders::values(QByteArrayView name) const
Returns the values of all fields named name, in order.
See also value() and combinedValue().
QList<QByteArray> QHttpHeaders::values(WellKnownHeader name) const
This is an overloaded function.
QByteArrayView QHttpHeaders::wellKnownHeaderName(WellKnownHeader name)
Returns the lower-case name of the header name.