The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

GX::HTTP::Headers - HTTP message headers class

SYNOPSIS

    # Load the class
    use GX::HTTP::Headers;
    
    # Create a new headers object
    $headers = GX::HTTP::Headers->new;
    
    # Set the value of a header field
    $headers->set( 'Content-Type' => 'text/plain' );
    
    # Get the value of a header field
    $content_type = $headers->get( 'Content-Type' );
    
    # Print the headers
    print $headers->as_string;
    
    # Parse a HTTP message header
    $headers->parse(
        "Content-Length: 26971\015\012" .
        "Content-Type: text/html\015\012" .
        "Date: Fri, 02 Oct 2009 12:05:02 GMT"
    );

DESCRIPTION

This module provides the GX::HTTP::Headers class which extends the GX::Class::Object class.

METHODS

Constructor

new

Returns a new GX::HTTP::Headers object.

    $headers = GX::HTTP::Headers->new;
Returns:
Exceptions:

Also see parse().

Basic Public API

add

Adds the given value to the specified header field.

    $headers->add( $field, $value );
Arguments:
  • $field ( string )

  • $value ( string )

Multiple values can be passed as a list:

    $headers->add( $field, @values );
Arguments:
  • $field ( string )

  • @values ( strings )

as_string

Returns the header fields formatted as a HTTP message header.

    $string = $headers->as_string;
Returns:
  • $string ( string )

clear

Removes all header fields.

    $headers->clear;

count

Returns the total number of header fields.

    $count = $headers->count;
Returns:
  • $count ( integer )

field_names

Returns the names of the header fields.

    @fields = $headers->field_names;
Returns:
  • @fields ( strings )

get

Returns the values of the specified header field in the order they were added.

    @values = $headers->get( $field );
Arguments:
  • $field ( string )

Returns:
  • @values ( strings )

In scalar context, the first of those values is returned.

    $value = $headers->get( $field );
Arguments:
  • $field ( string )

Returns:
  • $value ( string | undef )

parse

Parses the given message header and adds the resulting header field / value pairs to the container.

    $headers->parse( $string );
Arguments:
  • $string ( string )

This method can also be used as a constructor.

    $headers = GX::HTTP::Headers->parse( $string );
Arguments:
  • $string ( string )

Returns:
Exceptions:

remove

Removes the specified header field.

    $result = $headers->remove( $field );
Arguments:
  • $field ( string )

Returns:
  • $result ( bool )

set

Same as add(), but replaces any existing values for the specified header field with the ones given.

    $headers->set( $field, $value );
    $headers->set( $field, @values );

sorted_field_names

Returns the names of the header fields, sorted as suggested by RFC 2616.

    @fields = $headers->sorted_field_names;
Returns:
  • @fields ( strings )

Public Field Accessors

content_disposition

Returns / sets the value of the "Content-Disposition" header field.

    $value = $headers->content_disposition;
    $value = $headers->content_disposition( $value );
Arguments:
  • $value ( string | undef ) [ optional ]

Returns:
  • $value ( string | undef )

See RFC 2616, section 15.5.

content_encoding

Returns / sets the value of the "Content-Encoding" header field.

    $value = $headers->content_encoding;
    $value = $headers->content_encoding( $value );
Arguments:
  • $value ( string | undef ) [ optional ]

Returns:
  • $value ( string | undef )

See RFC 2616, section 14.11.

content_language

Returns / sets the value of the "Content-Language" header field.

    $value = $headers->content_language;
    $value = $headers->content_language( $value );
Arguments:
  • $value ( string | undef ) [ optional ]

Returns:
  • $value ( string | undef )

See RFC 2616, section 14.12.

content_length

Returns / sets the value of the "Content-Length" header field.

    $value = $headers->content_length;
    $value = $headers->content_length( $value );
Arguments:
  • $value ( string | undef ) [ optional ]

Returns:
  • $value ( string | undef )

See RFC 2616, section 14.13.

content_type

Returns / sets the value of the "Content-Type" header field.

    $value = $headers->content_type;
    $value = $headers->content_type( $value );
Arguments:
  • $value ( string | undef ) [ optional ]

Returns:
  • $value ( string | undef )

See RFC 2616, section 14.17.

date

Returns / sets the value of the "Date" header field.

    $value = $headers->date;
    $value = $headers->date( $value );
Arguments:
  • $value ( string | undef ) [ optional ]

Returns:
  • $value ( string | undef )

See RFC 2616, section 14.18.

expires

Returns / sets the value of the "Expires" header field.

    $value = $headers->expires;
    $value = $headers->expires( $value );
Arguments:
  • $value ( string | undef ) [ optional ]

Returns:
  • $value ( string | undef )

See RFC 2616, section 14.21.

last_modified

Returns / sets the value of the "Last-Modified" header field.

    $value = $headers->last_modified;
    $value = $headers->last_modified( $value );
Arguments:
  • $value ( string | undef ) [ optional ]

Returns:
  • $value ( string | undef )

See RFC 2616, section 14.29.

SUBCLASSES

The following classes inherit directly from GX::HTTP::Headers:

SEE ALSO

AUTHOR

Jörg A. Uzarek <uzarek@runlevelnull.de>

COPYRIGHT AND LICENSE

Copyright (c) 2009-2011 Jörg A. Uzarek.

This module is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License Version 3 as published by the Free Software Foundation.