Protocols::Http::HeaderBase Class Reference

#include <HeaderBase.h> [code]

Inherits Protocols::Generics::DataBase.

Inherited by Protocols::Http::BodyEnd, Protocols::Http::RequestHeader, and Protocols::Http::ResponseHeader.

Inheritance diagram for Protocols::Http::HeaderBase:

Inheritance graph
[legend]
Collaboration diagram for Protocols::Http::HeaderBase:

Collaboration graph
[legend]
List of all members.

Detailed Description

Base class implementing HTTP message headers.

This class is designed to store, parse and write HTTP header fields which can be found in message headers or chunk headers. Their syntax and symantics are described in RFC 2616.

You can parse a bunch of headers from a QString into a Header object using parse().

To get the header fields as raw bytes use the function

HeaderBase implements DataBase but has the special property that it is not an abstract class and thus can be instantiated. In fact Header uses a default constructed HeaderBase object as a Null object.

See also:
HttpRequestHeader HttpResponseHeader

Definition at line 48 of file HeaderBase.h.

Public Member Functions

auto_ptr< DataBasecopy () const
 Virtual copy constructor.
QByteArray toRawBytes () const
 Returns the raw byte representation of the HTTP header.
 HeaderBase ()
 Default constructor.
virtual ~HeaderBase ()
 Destructor.
bool operator== (const HeaderBase &) const
 Equality operator.
bool operator!= (const HeaderBase &) const
 Inequality operator.
bool parse (const QByteArray &rawBytes)
 Parses the HTTP header fields encoded in the rawBytes.
QList< QByteArray > fields () const
 Returns the names of all contained header fields (all in lower case).
bool hasField (const QByteArray &key) const
 Checks whether or not a field with name fieldName exists in this header.
QByteArray fieldValue (const QByteArray &fieldName) const
 Returns the field value for the header field with name fieldName.
QList< QByteArray > fieldValues (const QByteArray &fieldName) const
 Returns the list of field values for the field with name fieldName.
void setFieldValue (const QByteArray &fieldName, const QByteArray &fieldValue)
 Sets the value of the field with name fieldName to value.
void addFieldValue (const QByteArray &fieldName, const QByteArray &fieldValue)
 Adds a new field value fieldValue to the field with name fieldName.
void addFieldValues (const QByteArray &fieldName, const QList< QByteArray > &fieldValues)
 Adds the list of field values to the field with name fieldName.
void removeField (const QByteArray &fieldName)
 Removes the field with name fieldName from the HTTP header.

Protected Member Functions

 HeaderBase (Data *d)
 Protected constructor initializing the shared private data.
virtual bool parseStartLine (const QByteArray &startLine)
 Called to parse the start line of a HTTP header.
virtual QByteArray rawStartLine () const
 Raw representation of the header's start line.

Protected Attributes

SharedDataPointer d_ptr
 Pointer to the implicitly shared private data.

Private Member Functions

 Q_DECLARE_SHARED_DATA (Data)
bool doParse (const QByteArray &rawBytes)
 Helper that does the actual parsing of rawBytes.
bool parseAllLines (const QList< QByteArray > &lines)
 Helper to parse the header lines in lines.
bool parseHeaderLine (const QByteArray &line)
 Tries to parse the raw header in line.
QByteArray rawHeaderLine (const QByteArray &name) const
 Helper returning the raw bytes header line representation for fieldName.

Static Private Member Functions

static QList< QByteArray > splitStringIntoLines (const QByteArray &str)
 Helper to split str into a list of header lines.

Classes

class  Data


Constructor & Destructor Documentation

HeaderBase::HeaderBase  ) 
 

Default constructor.

Definition at line 28 of file HeaderBase.cpp.

HeaderBase::~HeaderBase  )  [virtual]
 

Destructor.

Definition at line 40 of file HeaderBase.cpp.

HeaderBase::HeaderBase Data d  )  [protected]
 

Protected constructor initializing the shared private data.

Definition at line 34 of file HeaderBase.cpp.


Member Function Documentation

void HeaderBase::addFieldValue const QByteArray &  fieldName,
const QByteArray &  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 200 of file HeaderBase.cpp.

void HeaderBase::addFieldValues const QByteArray &  fieldName,
const QList< QByteArray > &  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 221 of file HeaderBase.cpp.

auto_ptr< DataBase > HeaderBase::copy  )  const [virtual]
 

Virtual copy constructor.

Implements Protocols::Generics::DataBase.

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

Definition at line 57 of file HeaderBase.cpp.

bool HeaderBase::doParse const QByteArray &  rawBytes  )  [private]
 

Helper that does the actual parsing of rawBytes.

Parameters:
rawBytes to parse.
Precondition:
this is a default constructed object. No private data will be reset.

Definition at line 300 of file HeaderBase.cpp.

QList< QByteArray > HeaderBase::fields  )  const
 

Returns the names of all contained header fields (all in lower case).

See also:
hasField()

Definition at line 139 of file HeaderBase.cpp.

QByteArray HeaderBase::fieldValue const QByteArray &  fieldName  )  const
 

Returns the field value for the header field with name fieldName.

If no header field fieldName exists, 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 120 of file HeaderBase.cpp.

QList< QByteArray > HeaderBase::fieldValues const QByteArray &  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 166 of file HeaderBase.cpp.

bool HeaderBase::hasField const QByteArray &  fieldName  )  const
 

Checks whether or not a field with name fieldName exists in this header.

Returns:
true if the HTTP header contains a field with name fieldName.

false otherwise.

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

Definition at line 153 of file HeaderBase.cpp.

bool HeaderBase::operator!= const HeaderBase  )  const
 

Inequality operator.

Definition at line 51 of file HeaderBase.cpp.

bool HeaderBase::operator== const HeaderBase  )  const
 

Equality operator.

Definition at line 45 of file HeaderBase.cpp.

bool HeaderBase::parse const QByteArray &  rawBytes  ) 
 

Parses the HTTP header fields encoded in the rawBytes.

Parameters:
rawBytes is the raw bytes representation of HTTP header fields.
Returns:
true if parsing succeeds. In this case all old header fields will be dropped and this object will contain all parsed header fields instead.

false if the parsing fails. In this case this object remains unmodified.

See also:
toRawBytes()

Definition at line 73 of file HeaderBase.cpp.

bool HeaderBase::parseAllLines const QList< QByteArray > &  lines  )  [private]
 

Helper to parse the header lines in lines.

Allows for the first line to be parsed differently by calling the hook parseStartLine(). If it returns false, the line is parsed as a normal header line.

Definition at line 330 of file HeaderBase.cpp.

bool HeaderBase::parseHeaderLine const QByteArray &  line  )  [private]
 

Tries to parse the raw header in line.

Returns:
true if the header could indeed be parsed.

false otherwise.

Definition at line 360 of file HeaderBase.cpp.

bool HeaderBase::parseStartLine const QByteArray &  line  )  [protected, virtual]
 

Called to parse the start line of a HTTP header.

HeaderBase-derived classes can reimplement this function to parse their own specific start lines.

Returns:
true if the start line could be parsed.

false otherwise.

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

Definition at line 350 of file HeaderBase.cpp.

Protocols::Http::HeaderBase::Q_DECLARE_SHARED_DATA Data   )  [private]
 

QByteArray HeaderBase::rawHeaderLine const QByteArray &  name  )  const [private]
 

Helper returning the raw bytes header line representation for fieldName.

Definition at line 273 of file HeaderBase.cpp.

QByteArray HeaderBase::rawStartLine  )  const [protected, virtual]
 

Raw representation of the header's start line.

HeaderBase derived classes can reimplement this function.

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

Definition at line 267 of file HeaderBase.cpp.

void HeaderBase::removeField const QByteArray &  fieldName  ) 
 

Removes the field with name fieldName from the HTTP header.

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

Definition at line 232 of file HeaderBase.cpp.

void HeaderBase::setFieldValue const QByteArray &  fieldName,
const QByteArray &  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 187 of file HeaderBase.cpp.

QList< QByteArray > HeaderBase::splitStringIntoLines const QByteArray &  str  )  [static, private]
 

Helper to split str into a list of header lines.

Uses <CR><LF> or <LF> as line separators. Ignores empty lines.

Definition at line 315 of file HeaderBase.cpp.

QByteArray HeaderBase::toRawBytes  )  const [virtual]
 

Returns the raw byte representation of the HTTP header.

Each header field is finished by a "\r\n". Long header fields (more than 80 chars) are broken on multiple lines.

The BNF from RFC-2616:

	generic-message = start-line
						*(message-header CRLF)
						CRLF
						[ message-body ]
	start-line      = Request-Line | Status-Line
	

See also:
parse()

Implements Protocols::Generics::DataBase.

Definition at line 101 of file HeaderBase.cpp.


Member Data Documentation

SharedDataPointer Protocols::Http::HeaderBase::d_ptr [protected]
 

Pointer to the implicitly shared private data.

Definition at line 95 of file HeaderBase.h.


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