6.0.0-rc4Source

QHttpHeaders Class

The QHttpHeaders class holds the header fields of an HTTP message. More…

Header#include <QHttpHeaders>
CMaketarget_link_libraries(app PRIVATE Nt::Network)
ModuleNt Network C++ Classes
Since6.7

List of all members, including inherited members

Public Types

Public Functions

QHttpHeaders()
QHttpHeaders(const QHttpHeaders &other)
QHttpHeaders(QHttpHeaders &&other)
QHttpHeaders &operator=(const QHttpHeaders &other)
QHttpHeaders &operator=(QHttpHeaders &&other)
~QHttpHeaders()
voidswap(QHttpHeaders &other)
boolappend(QByteArrayView name, QByteArrayView value)
boolappend(WellKnownHeader name, QByteArrayView value)
boolinsert(qsizetype i, QByteArrayView name, QByteArrayView value)
boolinsert(qsizetype i, WellKnownHeader name, QByteArrayView value)
boolreplace(qsizetype i, QByteArrayView name, QByteArrayView newValue)
boolreplace(qsizetype i, WellKnownHeader name, QByteArrayView newValue)
boolreplaceOrAppend(QByteArrayView name, QByteArrayView newValue)
boolreplaceOrAppend(WellKnownHeader name, QByteArrayView newValue)
boolcontains(QByteArrayView name) const
boolcontains(WellKnownHeader name) const
voidclear()
voidremoveAll(QByteArrayView name)
voidremoveAll(WellKnownHeader name)
voidremoveAt(qsizetype i)
QByteArrayViewvalue(QByteArrayView name, QByteArrayView defaultValue = {}) const
QByteArrayViewvalue(WellKnownHeader name, QByteArrayView defaultValue = {}) const
QList<QByteArray>values(QByteArrayView name) const
QList<QByteArray>values(WellKnownHeader name) const
QByteArrayViewvalueAt(qsizetype i) const
QLatin1StringViewnameAt(qsizetype i) const
QByteArraycombinedValue(QByteArrayView name) const
QByteArraycombinedValue(WellKnownHeader name) const
qsizetypesize() const
voidreserve(qsizetype size)
boolisEmpty() const
QList<std::pair<QByteArray, QByteArray>>toListOfPairs() const
QMultiMap<QByteArray, QByteArray>toMultiMap() const
QMultiHash<QByteArray, QByteArray>toMultiHash() const

Static Public Members

QByteArrayViewwellKnownHeaderName(WellKnownHeader name)
QHttpHeadersfromListOfPairs(const QList<std::pair<QByteArray, QByteArray>> &headers)
QHttpHeadersfromMultiMap(const QMultiMap<QByteArray, QByteArray> &headers)
QHttpHeadersfromMultiHash(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.

ConstantDescription
QHttpHeaders::AIMa-im
QHttpHeaders::Acceptaccept
QHttpHeaders::AcceptCharsetaccept-charset
QHttpHeaders::AcceptEncodingaccept-encoding
QHttpHeaders::AcceptLanguageaccept-language
QHttpHeaders::AcceptRangesaccept-ranges
QHttpHeaders::AccessControlAllowCredentialsaccess-control-allow-credentials
QHttpHeaders::AccessControlAllowHeadersaccess-control-allow-headers
QHttpHeaders::AccessControlAllowMethodsaccess-control-allow-methods
QHttpHeaders::AccessControlAllowOriginaccess-control-allow-origin
QHttpHeaders::AccessControlExposeHeadersaccess-control-expose-headers
QHttpHeaders::AccessControlMaxAgeaccess-control-max-age
QHttpHeaders::AccessControlRequestHeadersaccess-control-request-headers
QHttpHeaders::AccessControlRequestMethodaccess-control-request-method
QHttpHeaders::Ageage
QHttpHeaders::Allowallow
QHttpHeaders::AltSvcalt-svc
QHttpHeaders::Authorizationauthorization
QHttpHeaders::CacheControlcache-control
QHttpHeaders::Connectionconnection
QHttpHeaders::ContentDispositioncontent-disposition
QHttpHeaders::ContentEncodingcontent-encoding
QHttpHeaders::ContentLanguagecontent-language
QHttpHeaders::ContentLengthcontent-length
QHttpHeaders::ContentLocationcontent-location
QHttpHeaders::ContentRangecontent-range
QHttpHeaders::ContentSecurityPolicycontent-security-policy
QHttpHeaders::ContentTypecontent-type
QHttpHeaders::Cookiecookie
QHttpHeaders::Datedate
QHttpHeaders::ETagetag
QHttpHeaders::Expectexpect
QHttpHeaders::Expiresexpires
QHttpHeaders::Forwardedforwarded
QHttpHeaders::Fromfrom
QHttpHeaders::Hosthost
QHttpHeaders::IfMatchif-match
QHttpHeaders::IfModifiedSinceif-modified-since
QHttpHeaders::IfNoneMatchif-none-match
QHttpHeaders::IfRangeif-range
QHttpHeaders::IfUnmodifiedSinceif-unmodified-since
QHttpHeaders::KeepAlivekeep-alive
QHttpHeaders::LastModifiedlast-modified
QHttpHeaders::Linklink
QHttpHeaders::Locationlocation
QHttpHeaders::MaxForwardsmax-forwards
QHttpHeaders::Originorigin
QHttpHeaders::Pragmapragma
QHttpHeaders::ProxyAuthenticateproxy-authenticate
QHttpHeaders::ProxyAuthorizationproxy-authorization
QHttpHeaders::Rangerange
QHttpHeaders::Refererreferer
QHttpHeaders::RetryAfterretry-after
QHttpHeaders::SecWebSocketAcceptsec-websocket-accept
QHttpHeaders::SecWebSocketExtensionssec-websocket-extensions
QHttpHeaders::SecWebSocketKeysec-websocket-key
QHttpHeaders::SecWebSocketProtocolsec-websocket-protocol
QHttpHeaders::SecWebSocketVersionsec-websocket-version
QHttpHeaders::Serverserver
QHttpHeaders::SetCookieset-cookie
QHttpHeaders::StrictTransportSecuritystrict-transport-security
QHttpHeaders::TEte
QHttpHeaders::Trailertrailer
QHttpHeaders::TransferEncodingtransfer-encoding
QHttpHeaders::Upgradeupgrade
QHttpHeaders::UserAgentuser-agent
QHttpHeaders::Varyvary
QHttpHeaders::Viavia
QHttpHeaders::WWWAuthenticatewww-authenticate
QHttpHeaders::XContentTypeOptionsx-content-type-options
QHttpHeaders::XForwardedForx-forwarded-for
QHttpHeaders::XFrameOptionsx-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.

[static] 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().

[static] 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().

[static] 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.

[static] QByteArrayView QHttpHeaders::wellKnownHeaderName(WellKnownHeader name)

Returns the lower-case name of the header name.