Know-How für Ihr Projekt

Perl Documentation


AxKit2::HTTPHeaders - HTTP Request and Response header class


This class parses and encapsulates HTTP headers, including the request/response line (e.g. GET / HTTP/1.0).



Construct a new header object from the given STRREF. The TYPE argument chooses variations in behaviour:

1. Assume an HTTP/0.9 request.

2. Create response headers.

3. Create generic request headers without start line, e.g. multipart parts.

CLASS->new_response( [ CODE ] )

Create a new response header with response code CODE (default: 200 "OK").

Assumes response is HTTP/1.0.


Parse the request URI for querystring parameters.

$obj->add_param( KEY, VALUE )

Add a new param (see $obj->param below).

$obj->del_param( KEY )

Remove a query parameter (see $obj->param below).

$obj->param( [ KEY ] )

Returns all params for KEY in LIST context, or the last param for KEY in SCALAR context.

Returns a list of current params if KEY is not supplied.

$obj->http_code_english( [ CODE ] )

Returns the english equivalent for HTTP response code CODE. If code is not supplied uses the current response code (not valid for request headers).

$obj->code( CODE [, TEXT] )

Sets the response code to CODE with optional TEXT.


Gets the response code.

$obj->request_method( [ METHOD ] )

Sets/gets the request method (e.g. "GET", "POST", etc).


Gets/sets the authenticated request username, if any.


Gets the request URI. Returns the full URI including query string. The URI is normalized to have minimum neccessary %-encoding as specified by RFC2396. Furthermore %-escapes are normalized to have uppercase hex chars only.

Also callable as $obj->uri.


Gets the request URI including host name and port. This URI can be used to build links to the currently running code, or for redirects.

Please be aware that it can yield wrong results in some reverse-proxy setups. Moreover, this method works like $obj->uri when no Host-Header is present.

$obj->cookie( [ NAME ] )

Returns a list of values for the given cookie NAME in LIST context, or the last set cookie for the given cookie NAME in scalar context. Only works on request headers.

If no name is given, returns a list of cookie names.

$obj->cookie( NAME, VALUE [, PARAMS] )

Set the cookie called NAME with value VALUE.

If value is an arrayref sets a multi-valued cookie.

Optional params are key-value pairs as per the cookie spec, e.g.:

$header->cookie( foo => "bar", path => "/", secure => 1 );

Expiration via expires is not parsed and does not take formats such as "+1d". It is suggested to use max-age instead (as per RFC 2109) which is a timeout in seconds from the current time.

$obj->filename( [ STRING ] )

Gets/Sets the request filename value.

$obj->mime_type( [ STRING ] )

Gets/Sets the request MIME type.

$obj->path_info( [ STRING ] )

Gets/Sets the request path-info value.

$obj->version_number( [ VER ] )

Gets/Sets the header version number. Uses the form MAJOR * 1000 + MINOR, so HTTP/1.0 will return 1000 and HTTP/0.9 will return 9.


Returns the very first line of the request as seen.

$obj->header( KEY [, VALUE ] )

Get/Set the value for a given header. If no name is given, returns list of present headers.

$obj->header_param( KEY, PARAM )

Get a parameter value for a given header, for example $headers->header_param('Content-Type', 'charset').

Returns undef if PARAM was not found.

$obj->header_value( KEY )

Get value for a given header excluding parameters. This only works for headers that are structured as tokens, not *text.

$obj->header_remove( KEY )

Remove a given header.


Returns true if the request was a HTTP/0.9 request.


Returns a reference to a string representing the headers in full.


Clone the current header object. Returns a new AxKit2::HTTPHeaders object.

$obj->set_version( VER )

Set the HTTP version using the N.N format.


Using all available information, attempt to determine the content length of the message body being sent to us.

$obj->req_keep_alive( RESPONSE_HEADERS )

Given $obj is the request headers, answers the question: "should a response to this person specify keep-alive, given the request and the given response?"

This is used in proxy mode to determine based on the client's request and the backend's response whether or not the response from the proxy (us) should do keep-alive.

For normal responses (should a response be kept alive) see res_keep_alive.


Determine if an options response from a backend looks like it can do keep-alive.

$obj->res_keep_alive( REQUEST_HEADERS )

Given $obj is the response headers, answers the question: "is the backend expected to stay open?" this is a combination of the request we sent to it and the response they sent...

You don't normally need to call this - it is automatically performed by the backend.

$obj->range( SIZE )

Given a size, returns STATUS, RANGE_START, RANGE_END.

Status will be one of:

  1. - Invalid or non-existant range header. Serve normally.
  2. * 206 - Parsable range is good. Serve partial content.
  3. * 416 - Range is unsatisfiable.


Sets up response headers to prevent caching by clients and intermediate proxies. This sets several headers to make sure all clients act accordingly, regardless of which HTTP version they implement.

Do not use this for more fine-grained cache control.