cutelyst  4.5.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
Cutelyst::Headers Class Reference

Container for HTTP headers. More...

#include <Cutelyst/Headers>

Classes

struct  Authorization
 
struct  HeaderKeyValue
 

Public Member Functions

 Headers () noexcept=default
 
 Headers (const Headers &other) noexcept
 
 Headers (std::initializer_list< std::pair< QByteArray, QByteArray >> list)
 
QByteArray authorization () const noexcept
 
QByteArray authorizationBasic () const
 
Authorization authorizationBasicObject () const
 
QByteArray authorizationBearer () const
 
void clear ()
 
QByteArray connection () const noexcept
 
bool contains (QByteArrayView key) const noexcept
 
QByteArray contentDisposition () const noexcept
 
QByteArray contentEncoding () const noexcept
 
bool contentIsHtml () const
 
bool contentIsJson () const
 
bool contentIsText () const
 
bool contentIsXHtml () const
 
bool contentIsXml () const
 
qint64 contentLength () const
 
QByteArray contentType () const
 
QByteArray contentTypeCharset () const
 
QVector< HeaderKeyValuedata () const
 
QDateTime date () const
 
QByteArray header (QByteArrayView key) const noexcept
 
QByteArray header (QByteArrayView key, const QByteArray &defaultValue) const noexcept
 
QString headerAsString (QByteArrayView key) const
 
QString headerAsString (QByteArrayView key, const QByteArray &defaultValue) const
 
QByteArrayList headers (QByteArrayView key) const
 
QStringList headersAsStrings (QByteArrayView key) const
 
QByteArray host () const noexcept
 
bool ifMatch (const QByteArray &etag) const
 
QByteArray ifModifiedSince () const noexcept
 
bool ifModifiedSince (const QDateTime &lastModified) const
 
QDateTime ifModifiedSinceDateTime () const
 
bool ifNoneMatch (const QByteArray &etag) const
 
QByteArrayList keys () const
 
QByteArray lastModified () const noexcept
 
Headersoperator= (const Headers &other) noexcept
 
bool operator== (const Headers &other) const noexcept
 
QByteArray operator[] (QByteArrayView key) const noexcept
 
QByteArray proxyAuthorization () const noexcept
 
QByteArray proxyAuthorizationBasic () const
 
Authorization proxyAuthorizationBasicObject () const
 
void pushHeader (const QByteArray &key, const QByteArray &value)
 
void pushHeader (const QByteArray &key, const QByteArrayList &values)
 
QByteArray referer () const noexcept
 
void removeHeader (QByteArrayView key)
 
QByteArray server () const noexcept
 
QByteArray setAuthorizationBasic (const QString &username, const QString &password)
 
void setCacheControl (const QByteArray &value)
 
void setContentDisposition (const QByteArray &contentDisposition)
 
void setContentDispositionAttachment (const QByteArray &filename={})
 
void setContentEncoding (const QByteArray &encoding)
 
void setContentLength (qint64 value)
 
void setContentType (const QByteArray &contentType)
 
void setContentTypeCharset (const QByteArray &charset)
 
QByteArray setDateWithDateTime (const QDateTime &date)
 
void setETag (const QByteArray &etag)
 
void setHeader (const QByteArray &field, const QByteArrayList &values)
 
void setHeader (const QByteArray &key, const QByteArray &value)
 
void setLastModified (const QByteArray &value)
 
QString setLastModified (const QDateTime &lastModified)
 
void setProxyAuthenticate (const QByteArray &value)
 
void setReferer (const QByteArray &value)
 
void setServer (const QByteArray &value)
 
void setWwwAuthenticate (const QByteArray &value)
 
QByteArray userAgent () const noexcept
 

Detailed Description

Headers is a container for HTTP headers that also implements helper methods to set and get specific headers.

Definition at line 23 of file headers.h.

Constructor & Destructor Documentation

◆ Headers() [1/3]

Cutelyst::Headers::Headers ( )
defaultnoexcept

Construct an empty Header object.

◆ Headers() [2/3]

Headers::Headers ( const Headers other)
noexcept

Constructs a copy of other.

Definition at line 28 of file headers.cpp.

◆ Headers() [3/3]

Cutelyst::Headers::Headers ( std::initializer_list< std::pair< QByteArray, QByteArray >>  list)
inline

Construct a Header from a std::initializer_list given by list.

Definition at line 49 of file headers.h.

Member Function Documentation

◆ authorization()

QByteArray Headers::authorization ( ) const
noexcept

This method is used to get an authorization header without any decoding.

Definition at line 336 of file headers.cpp.

References header().

Referenced by authorizationBasic(), authorizationBasicObject(), and authorizationBearer().

◆ authorizationBasic()

QByteArray Headers::authorizationBasic ( ) const

This method is used to get an authorization header that use the "Basic Authentication Scheme". It will return "username:password" as a single string value.

Definition at line 353 of file headers.cpp.

References authorization().

◆ authorizationBasicObject()

Headers::Authorization Headers::authorizationBasicObject ( ) const

This method is used to get an authorization header that use the "Basic Authentication Scheme". It will return a pair of username and password respectively.

Definition at line 358 of file headers.cpp.

References authorization().

◆ authorizationBearer()

QByteArray Headers::authorizationBearer ( ) const

This method is used to get an authorization token

Definition at line 341 of file headers.cpp.

References authorization(), and QByteArray::mid().

◆ clear()

void Cutelyst::Headers::clear ( )
inline

Clears all headers.

Definition at line 414 of file headers.h.

References QList::clear().

◆ connection()

QByteArray Headers::connection ( ) const
noexcept

Returns the 'Connection' header field that indicates how it should be handled after a request has been processed, like 'close'.

Definition at line 295 of file headers.cpp.

References header().

◆ contains()

bool Headers::contains ( QByteArrayView  key) const
noexcept

Returns true if the header field specified by key is defined.

Definition at line 476 of file headers.cpp.

Referenced by Cutelyst::ViewJson::render().

◆ contentDisposition()

QByteArray Headers::contentDisposition ( ) const
noexcept

The Content-Disposition header field indicates if the content is expected to be displayed inline in the browser, that is, as a Web page or as part of a Web page, or as an attachment, that is downloaded and saved locally.

See also
setContentDisposition()

Definition at line 33 of file headers.cpp.

References header().

Referenced by setContentDisposition().

◆ contentEncoding()

QByteArray Headers::contentEncoding ( ) const
noexcept

The Content-Encoding header field is used as a modifier to the media type. When present, its value indicates what additional encoding mechanism has been applied to the resource.

Definition at line 57 of file headers.cpp.

References header().

◆ contentIsHtml()

bool Headers::contentIsHtml ( ) const

Returns true if the Content-Type header field indicate that the content is some kind of HTML (including XHTML).

Definition at line 134 of file headers.cpp.

References QByteArray::compare(), and contentType().

◆ contentIsJson()

bool Headers::contentIsJson ( ) const

Returns true if the Content-Type header field indicate that the content is JSON.

Definition at line 154 of file headers.cpp.

References header().

◆ contentIsText()

bool Headers::contentIsText ( ) const

Returns true if the Content-Type header field indicate that the content is textual.

Definition at line 129 of file headers.cpp.

References header(), and QByteArray::startsWith().

◆ contentIsXHtml()

bool Headers::contentIsXHtml ( ) const

Returns true if the Content-Type header field indicate that the content is XHTML.

Definition at line 141 of file headers.cpp.

References QByteArray::compare(), and contentType().

◆ contentIsXml()

bool Headers::contentIsXml ( ) const

Returns true if the Content-Type header field indicate that the content is XML.

Definition at line 148 of file headers.cpp.

References QByteArray::compare(), contentType(), and QByteArray::endsWith().

◆ contentLength()

qint64 Headers::contentLength ( ) const

Returns the size in bytes of the message content

See also
setContentLength()

Definition at line 163 of file headers.cpp.

References header().

◆ contentType()

QByteArray Headers::contentType ( ) const

The Content-Type header field indicates the media type of the message content. E.g.: "text/html" The value returned will be converted to lower case, and potential parameters will be ignored. If there is no such header field, then the empty string is returned.

See also
setContentType()

Definition at line 67 of file headers.cpp.

References header(), QByteArray::indexOf(), QByteArray::isEmpty(), QByteArray::mid(), and QByteArray::toLower().

Referenced by contentIsHtml(), contentIsXHtml(), contentIsXml(), contentTypeCharset(), setContentType(), and setContentTypeCharset().

◆ contentTypeCharset()

QByteArray Headers::contentTypeCharset ( ) const

Returns the upper-cased charset specified in the Content-Type header.

See also
setContentTypeCharset()

Definition at line 81 of file headers.cpp.

References contentType(), header(), QByteArray::indexOf(), QByteArray::isEmpty(), QByteArray::mid(), QByteArray::toUpper(), and QByteArray::trimmed().

◆ data()

QVector<HeaderKeyValue> Cutelyst::Headers::data ( ) const
inline

Returns the internal structure of headers, to be used by Engine subclasses.

Definition at line 419 of file headers.h.

Referenced by Cutelyst::UA::forwardRequest(), Cutelyst::ProtoRequestFastCGI::writeHeaders(), and Cutelyst::ProtoRequestHttp::writeHeaders().

◆ date()

QDateTime Headers::date ( ) const

Returns the Date header as QDateTime

Definition at line 187 of file headers.cpp.

References QLocale::c(), QString::fromLatin1(), header(), QDateTime::setTimeSpec(), QLocale::toDateTime(), and Qt::UTC.

Referenced by setDateWithDateTime().

◆ header() [1/2]

◆ header() [2/2]

QByteArray Headers::header ( QByteArrayView  key,
const QByteArray defaultValue 
) const
noexcept

Returns the value associated with key, if field is not set defaultValue is returned

Definition at line 405 of file headers.cpp.

◆ headerAsString() [1/2]

QString Headers::headerAsString ( QByteArrayView  key) const

Returns the value associated with key from latin1

Note
This allocates memory so avoid when possible

Definition at line 400 of file headers.cpp.

References QString::fromLatin1(), and header().

◆ headerAsString() [2/2]

QString Headers::headerAsString ( QByteArrayView  key,
const QByteArray defaultValue 
) const

Returns the value associated with key from latin1, if field is not set defaultValue is returned

Note
This allocates memory so avoid when possible

Definition at line 413 of file headers.cpp.

References QString::fromLatin1(), and header().

◆ headers()

QByteArrayList Headers::headers ( QByteArrayView  key) const

Returns all values associated with key

Definition at line 418 of file headers.cpp.

References QList::append().

◆ headersAsStrings()

QStringList Headers::headersAsStrings ( QByteArrayView  key) const

Returns all values associated with key

Note
This allocates memory so avoid when possible

Definition at line 427 of file headers.cpp.

References QList::append(), and QString::fromLatin1().

◆ host()

QByteArray Headers::host ( ) const
noexcept

Returns the 'Host' header field used in request messages, containing information about which host the client would like to talk to, this is especially useful for building URIs and for VirtualHosts.

Definition at line 300 of file headers.cpp.

References header().

◆ ifMatch()

bool Headers::ifMatch ( const QByteArray etag) const

Checks for If-Match header usually used on POST to avoid mid-air collisions, making sure the content has not changed while the client changes it. Returns true if the etag value matches the value between double quotes of the client header or if the client did not provide the If-Match header.

In case of false client should usually discard posted data and return status code of 412 - Response::PreconditionFailed.

Definition at line 239 of file headers.cpp.

References header(), and QByteArray::sliced().

◆ ifModifiedSince() [1/2]

QByteArray Headers::ifModifiedSince ( ) const
noexcept

This header field is used to make a request conditional. If the requested resource has (or has not) been modified since the time specified in this field, then the server will return a 304 Not Modified response instead of the document itself.

Definition at line 205 of file headers.cpp.

References header().

◆ ifModifiedSince() [2/2]

bool Headers::ifModifiedSince ( const QDateTime lastModified) const

Checks if Last-Modified's content has changed in comparison to the If-Modified-Since header.

Definition at line 228 of file headers.cpp.

References QLocale::c(), header(), lastModified(), QString::toLatin1(), and QLocale::toString().

◆ ifModifiedSinceDateTime()

QDateTime Headers::ifModifiedSinceDateTime ( ) const

This header fields is used to make a request conditional. If the requested resource has (or has not) been modified since the time specified in this field, then the server will return a 304 Not Modified response instead of the document itself. This method parses the header and convert to QDateTime assuming the date is in GMT timezone.

Definition at line 210 of file headers.cpp.

References QLocale::c(), QString::fromLatin1(), header(), QDateTime::setTimeSpec(), QLocale::toDateTime(), and Qt::UTC.

◆ ifNoneMatch()

bool Headers::ifNoneMatch ( const QByteArray etag) const

Checks for If-None-Match header to see if the client has the most recent version of a cached resource. Returns true if the etag value matches the value between double quotes of the client header.

In case of true client should usually return an empty body along with a status code of 304 - Response::NotModified.

Definition at line 250 of file headers.cpp.

References header(), and QByteArray::sliced().

◆ lastModified()

QByteArray Headers::lastModified ( ) const
noexcept

This header indicates the date and time at which the resource was last modified.

Definition at line 266 of file headers.cpp.

References header().

Referenced by ifModifiedSince(), and setLastModified().

◆ operator=()

Headers& Cutelyst::Headers::operator= ( const Headers other)
inlinenoexcept

Assigns other to this Header and returns a reference to this Header.

Definition at line 436 of file headers.h.

◆ operator==()

bool Headers::operator== ( const Headers other) const
noexcept

Compares if other has the same data as this.

Definition at line 508 of file headers.cpp.

◆ operator[]()

QByteArray Headers::operator[] ( QByteArrayView  key) const
noexcept

Returns the value associated with key.

Definition at line 503 of file headers.cpp.

◆ proxyAuthorization()

QByteArray Headers::proxyAuthorization ( ) const
noexcept

A user agent that wishes to authenticate itself with a server or a proxy, may do so by including these headers.

Definition at line 377 of file headers.cpp.

References header().

Referenced by proxyAuthorizationBasic(), and proxyAuthorizationBasicObject().

◆ proxyAuthorizationBasic()

QByteArray Headers::proxyAuthorizationBasic ( ) const

This method is used to get an authorization header that use the "Basic Authentication Scheme" but using the "Proxy-Authorization" header instead. It will return "username:password" as a single string value.

Definition at line 382 of file headers.cpp.

References proxyAuthorization().

◆ proxyAuthorizationBasicObject()

Headers::Authorization Headers::proxyAuthorizationBasicObject ( ) const

This method is used to get an authorization header that use the "Basic Authentication Scheme" but using the "Proxy-Authorization" header instead. It will return a pair of username and password respectively.

Definition at line 387 of file headers.cpp.

References proxyAuthorization().

◆ pushHeader() [1/2]

void Headers::pushHeader ( const QByteArray key,
const QByteArray value 
)

Appends the header field to values

Definition at line 460 of file headers.cpp.

Referenced by Cutelyst::EngineRequest::finalizeCookies(), Cutelyst::UA::forwardAsync(), and Cutelyst::UA::forwardRequestResponse().

◆ pushHeader() [2/2]

void Headers::pushHeader ( const QByteArray key,
const QByteArrayList values 
)

This method appends a header to internal data normalizing the key.

Definition at line 465 of file headers.cpp.

References QByteArrayList::join().

◆ referer()

QByteArray Headers::referer ( ) const
noexcept

Used to specify the address (URI) of the document from which the requested resource address was obtained.

Definition at line 310 of file headers.cpp.

References header().

◆ removeHeader()

void Headers::removeHeader ( QByteArrayView  key)

This method removes a header identified by field.

Definition at line 470 of file headers.cpp.

References Qt::CaseInsensitive, and QByteArrayView::compare().

Referenced by setContentTypeCharset().

◆ server()

QByteArray Headers::server ( ) const
noexcept

Returns the server header field contains information about the software being used by the originating server program handling the request.

Definition at line 285 of file headers.cpp.

References header().

◆ setAuthorizationBasic()

QByteArray Headers::setAuthorizationBasic ( const QString username,
const QString password 
)

This method is used to set an authorization header that use the "Basic Authentication Scheme". It won't set the values if username contains a colon ':'.

Definition at line 363 of file headers.cpp.

References QString::contains(), setHeader(), QByteArray::toBase64(), and QString::toLatin1().

◆ setCacheControl()

void Headers::setCacheControl ( const QByteArray value)

Sets the value for the Cache-Control header.

Definition at line 38 of file headers.cpp.

References setHeader().

◆ setContentDisposition()

void Headers::setContentDisposition ( const QByteArray contentDisposition)

Sets the value for the Content-Disposition header to contentDisposition.

See also
contentDisposition()

Definition at line 43 of file headers.cpp.

References contentDisposition(), and setHeader().

Referenced by setContentDispositionAttachment().

◆ setContentDispositionAttachment()

void Headers::setContentDispositionAttachment ( const QByteArray filename = {})

Sets the Content-Disposition header as type attachment and sets the filename as optional filename parameter.

See also
contentDisposition()

Definition at line 48 of file headers.cpp.

References QByteArray::isEmpty(), and setContentDisposition().

◆ setContentEncoding()

void Headers::setContentEncoding ( const QByteArray encoding)

Sets the Content-Encoding header to encoding.

See also
contentEncoding()

Definition at line 62 of file headers.cpp.

References setHeader().

◆ setContentLength()

void Headers::setContentLength ( qint64  value)

Sets the size in bytes of the message content.

See also
contentLength()

Definition at line 172 of file headers.cpp.

References QByteArray::number(), and setHeader().

Referenced by Cutelyst::EngineRequest::finalizeHeaders().

◆ setContentType()

void Headers::setContentType ( const QByteArray contentType)

Sets the value for the Content-Type header to contentType.

See also
contentType()

Definition at line 76 of file headers.cpp.

References contentType(), and setHeader().

Referenced by setContentTypeCharset().

◆ setContentTypeCharset()

void Headers::setContentTypeCharset ( const QByteArray charset)

Set the optional charset parameter of the Content-Type header field to charset.

See also
contentTypeCharset()

Definition at line 96 of file headers.cpp.

References QByteArray::append(), contentType(), QByteArray::indexOf(), QByteArray::isEmpty(), QByteArray::lastIndexOf(), QByteArray::length(), QByteArray::remove(), removeHeader(), QByteArray::replace(), and setContentType().

◆ setDateWithDateTime()

QByteArray Headers::setDateWithDateTime ( const QDateTime date)

Sets the Date header that represents the date and time at which the message was originated.

Definition at line 177 of file headers.cpp.

References QLocale::c(), date(), setHeader(), QString::toLatin1(), QLocale::toString(), and QDateTime::toUTC().

◆ setETag()

void Headers::setETag ( const QByteArray etag)

Sets the ETag header in strong form by not prepending a 'W/' (weak etag) This method will place the etag value between double quotes, like: ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"

Definition at line 261 of file headers.cpp.

References setHeader().

◆ setHeader() [1/2]

void Headers::setHeader ( const QByteArray field,
const QByteArrayList values 
)

Sets the header field to the list of values

Definition at line 455 of file headers.cpp.

References QByteArrayList::join(), and setHeader().

◆ setHeader() [2/2]

◆ setLastModified() [1/2]

void Headers::setLastModified ( const QByteArray value)

Defines the date and time at which the resource was last modified.

Definition at line 271 of file headers.cpp.

References setHeader().

Referenced by setLastModified().

◆ setLastModified() [2/2]

QString Headers::setLastModified ( const QDateTime lastModified)

Defines the date and time at which the resource was last modified. This method takes a QDateTime and write it in RFC 822 and GMT timezone.

Definition at line 276 of file headers.cpp.

References QLocale::c(), lastModified(), setLastModified(), and QLocale::toString().

◆ setProxyAuthenticate()

void Headers::setProxyAuthenticate ( const QByteArray value)

This header must be included in a 407 Proxy Authentication Required response.

Definition at line 331 of file headers.cpp.

References setHeader().

◆ setReferer()

void Headers::setReferer ( const QByteArray value)

Sets the referrer (Referer) header. This method removes the fragment from the given URI if it is present, as mandated by RFC2616. Note that the removal does not happen automatically if using the setHeader() method to set the referrer

Definition at line 315 of file headers.cpp.

References QByteArray::indexOf(), QByteArray::mid(), and setHeader().

◆ setServer()

void Headers::setServer ( const QByteArray value)

Defines the server header field contains information about the software being used by the originating server program handling the request.

Definition at line 290 of file headers.cpp.

References setHeader().

◆ setWwwAuthenticate()

void Headers::setWwwAuthenticate ( const QByteArray value)

This header must be included as part of a 401 Unauthorized response. The field value consist of a challenge that indicates the authentication scheme and parameters applicable to the requested URI.

Definition at line 326 of file headers.cpp.

References setHeader().

◆ userAgent()

QByteArray Headers::userAgent ( ) const
noexcept

Returns the header field used in request messages, containing information about the user agent originating the request.

Definition at line 305 of file headers.cpp.

References header().