Http::Header Class Reference

#include <Header.h> [code]

Inherited by Gnutella::Handshaking::RequestHeader, Gnutella::Handshaking::ResponseHeader, Http::RequestHeader, and Http::ResponseHeader.

Inheritance diagram for Http::Header:

Inheritance graph
[legend]
Collaboration diagram for Http::Header:

Collaboration graph
[legend]
List of all members.

Detailed Description

This is the base for HTTP-style message headers.

Todo:
Adapt the original Qt documentation to the current source:
In most cases you should use the more specialized derivatives of this class, QHttpResponseHeader and QHttpRequestHeader, rather than directly using QHttpHeader.

QHttpHeader provides the HTTP header fields. A HTTP header field consists of a name followed by a colon, a single space, and the field value. (See RFC 1945.) Field names are case-insensitive. A typical header field looks like this:

    content-type: text/html

In the API the header field name is called the "key" and the content is called the "value". You can get and set a header field's value by using its key with value() and setValue(), e.g.

    header.setValue("content-type", "text/html");
    QString contentType = header.value("content-type");

Some fields are so common that getters and setters are provided for them as a convenient alternative to using value() and setValue(), e.g. contentLength() and contentType(), setContentLength() and setContentType().

Each header key has a single value associated with it. If you set the value for a key which already exists the previous value will be discarded.

Note:
This class is derived from QHttpHeader, which is part of the Qt Framework version 4.0.0. It was modified to suite your needs better and to match our coding conventions.
See also:
HttpRequestHeader HttpResponseHeader

Definition at line 71 of file Header.h.

Public Member Functions

 Header ()
 Header (const Header &header)
 Header (const QString &str)
virtual ~Header ()
Headeroperator= (const Header &h)
bool operator== (const Header &other) const
bool operator!= (const Header &other) const
QString fieldValue (const QString &fieldName) const
QStringList fieldValues (const QString &fieldName) const
void setFieldValue (const QString &fieldName, const QString &fieldValue)
void addFieldValue (const QString &fieldName, const QString &fieldValue)
void addFieldValues (const QString &fieldName, const QStringList &fieldValues)
void removeField (const QString &fieldName)
QStringList fields () const
bool hasField (const QString &key) const
bool parse (const QString &str)
virtual QString toString () const
bool isValid () const

Protected Member Functions

virtual bool parseLine (const QString &line, int number)
void setValid (bool)

Private Member Functions

bool parseAllLines (const QStringList &lines)

Static Private Member Functions

static QStringList splitStringIntoLines (const QString &str)

Private Attributes

HeaderPrivated


Constructor & Destructor Documentation

Header::Header  ) 
 

Constructs an empty HTTP header.

Definition at line 57 of file Header.cpp.

Header::Header const Header header  ) 
 

Constructs a copy of header.

Definition at line 66 of file Header.cpp.

Header::Header const QString &  str  ) 
 

Constructs a HTTP header for str.

This constructor parses the string str for header fields and adds this information. The str should consist of one or more "\r\n" delimited lines; each of these lines should have the format key, colon, space, value.

Definition at line 82 of file Header.cpp.

Header::~Header  )  [virtual]
 

Destructor.

Definition at line 91 of file Header.cpp.


Member Function Documentation

void Header::addFieldValue const QString &  fieldName,
const QString &  fieldValue
 

Adds a new field value fieldValue to the field with name fieldName. If the field does not exist, it will be created.

See also:
addFieldValues(), fieldValue(), hasField(), removeField()

Definition at line 280 of file Header.cpp.

void Header::addFieldValues const QString &  fieldName,
const QStringList &  fieldValues
 

Adds the list of field values to the field with name fieldName. If the field does not exist, it will be created.

See also:
addFieldValue(), fieldValue(), hasField(), removeField()

Definition at line 298 of file Header.cpp.

QStringList Header::fields  )  const
 

Returns a list with the names of the header fields names (in lower case) contained in the HTTP header.

See also:
hasField()

Definition at line 235 of file Header.cpp.

QString Header::fieldValue const QString &  fieldName  )  const
 

Returns the field value for the header field with name fieldName. If no header field has this fieldName, an empty string is returned. If multiple field values are available, they are returned as comma-separated list in a single string.

See also:
setFieldValue(), removeField(), hasField(), fields(), addFieldValue(), addFieldValues()

Definition at line 220 of file Header.cpp.

QStringList Header::fieldValues const QString &  fieldName  )  const
 

Returns the list of field values for the field with name fieldName. If no field with name fieldName exists, and empty list is returned.

See also:
addFieldValue(), addFieldValues(), fieldValue(), hasField(), removeField()

Definition at line 312 of file Header.cpp.

bool Header::hasField const QString &  fieldName  )  const
 

Returns true if the HTTP header contains a field with name fieldName; otherwise returns false.

See also:
fieldValue(), setFieldValue(), fields(), addFieldValue(), addFieldValues()

Definition at line 247 of file Header.cpp.

bool Header::isValid  )  const
 

Returns true if the HTTP header is valid; otherwise returns false.

A Header is invalid if it was created by parsing a malformed string.

Definition at line 130 of file Header.cpp.

bool Header::operator!= const Header other  )  const
 

Inequality comparison. Returns true, if the selected other header object is not equal to this object.

Definition at line 120 of file Header.cpp.

Header & Header::operator= const Header header  ) 
 

Assigns h and returns a reference to this HTTP header.

Definition at line 99 of file Header.cpp.

bool Header::operator== const Header other  )  const
 

Equality comparison. Returns true, if the selected other header object is equal to this object.

Definition at line 111 of file Header.cpp.

bool Header::parse const QString &  str  ) 
 

Parses the HTTP header string str for header fields and adds the names/values it finds. All previous fields are discarded. If the string is not parsed successfully the Header becomes invalid.

Returns true if str was successfully parsed; otherwise returns false.

See also:
toString()

Definition at line 188 of file Header.cpp.

bool Header::parseAllLines const QStringList &  lines  )  [private]
 

Definition at line 166 of file Header.cpp.

bool Header::parseLine const QString &  line,
int  number
[protected, virtual]
 

For internal use only.

Parses the single HTTP header line line which has the format field-name: field-values and adds fields to the headers. The linenumber is number. Returns true if the line was successfully parsed and the field was added; otherwise returns false.

See also:
parse()

Reimplemented in Gnutella::Handshaking::RequestHeader, Gnutella::Handshaking::ResponseHeader, Http::RequestHeader, and Http::ResponseHeader.

Definition at line 343 of file Header.cpp.

void Header::removeField const QString &  fieldName  ) 
 

Removes the field with name fieldName from the HTTP header.

See also:
addFieldValue(), addFieldValues(), fieldValue(), hasField()

Definition at line 326 of file Header.cpp.

void Header::setFieldValue const QString &  fieldName,
const QString &  fieldValue
 

Sets the value of the field with name fieldName to value.

If no field with fieldName exists, a new field with the given fieldName and fieldValue is created. If a field with the fieldName already exists, its values are discarded and replaced with the given fieldValue. If the fieldValue is a comma-separated string, it is split in sub values and they are all associated with the header field.

See also:
addFieldValue(), addFieldValues(), fieldValue(), hasField(), removeField()

Definition at line 264 of file Header.cpp.

void Header::setValid bool  v  )  [protected]
 

For internal use only.

Definition at line 206 of file Header.cpp.

QStringList Header::splitStringIntoLines const QString &  str  )  [static, private]
 

Definition at line 154 of file Header.cpp.

QString Header::toString  )  const [virtual]
 

Returns a string representation of the HTTP header.

The string is suitable for use by the constructor that takes a QString. It consists of lines with the format: key, colon, space, value, "\r\n".

Reimplemented in Gnutella::Handshaking::RequestHeader, Gnutella::Handshaking::ResponseHeader, Http::RequestHeader, and Http::ResponseHeader.

Definition at line 361 of file Header.cpp.


Member Data Documentation

HeaderPrivate* Http::Header::d [private]
 

Reimplemented in Http::RequestHeader, and Http::ResponseHeader.

Definition at line 106 of file Header.h.


The documentation for this class was generated from the following files: