CGI::Header - Handle CGI.pm-compatible HTTP header properties
use CGI; use CGI::Header; my $query = CGI->new; # CGI.pm-compatible HTTP header properties my $header = CGI::Header->new( query => $query, header => { attachment => 'foo.gif', charset => 'utf-7', cookies => [ $cookie1, $cookie2 ], # CGI::Cookie objects expires => '+3d', nph => 1, p3p => [qw/CAO DSP LAW CURa/], target => 'ResultsWindow', type => 'image/gif' }, ); # update $header $header->set( 'Content-Length' => 3002 ); # overwrite $header->delete('Content-Disposition'); # => 3002 $header->clear; # => $self $header->as_string; # => "Content-Type: text/html\n..."
This document refers to CGI::Header version 0.48.
This module is compatible with CGI.pm 3.51 or higher.
This module is a utility class to manipulate a hash reference received by CGI.pm's header() method.
header()
This module isn't the replacement of the header() method, but complements CGI.pm.
This module can be used in the following situation:
For example, CGI::Application implements header_add() method which can be used to add CGI.pm-compatible HTTP header properties. Instances of CGI.pm-based applications often hold those properties.
header_add()
my $header = { type => 'text/plain' };
Since property names are case-insensitive, application developers have to normalize them manually when they specify header properties. CGI::Header normalizes them automatically.
use CGI::Header; my $h = CGI::Header->new( header => $header ); $h->set( 'Content-Length' => 3002 ); # add Content-Length header $header; # => { # 'type' => 'text/plain', # 'content-length' => '3002', # }
use CGI; print CGI::header( $header ); # Content-length: 3002 # Content-Type: text/plain; charset=ISO-8859-1 #
header() function just stringifies given header properties. This module can be used to generate PSGI-compatible response header array references. See CGI::Header::PSGI.
Returns your current query object. This attribute defaults to the Singleton instance of CGI.pm ($CGI::Q), which is shared by functions exported by the module.
$CGI::Q
Returns the header hash reference associated with this CGI::Header object. This attribute defaults to a reference to an empty hash. The hashref will be passed to CGI.pm's header method to generate CGI response headers. See CGI::Header#as_string.
header
CGI::Header#as_string
Rebuilds the header hash to normalize property names without changing the reference. Returns this object itself. If property names aren't normalized, the methods listed below won't work as you expect.
my $h1 = $header->header; # => { # '-content_type' => 'text/plain', # 'Set-Cookie' => 'ID=123456; path=/', # 'expires' => '+3d', # '-target' => 'ResultsWindow', # '-content-length' => '3002' # } $header->rehash; my $h2 = $header->header; # same reference as $h1 # => { # 'type' => 'text/plain', # 'cookies' => 'ID=123456; path=/', # 'expires' => '+3d', # 'target' => 'ResultsWindow', # 'content-length' => '3002' # }
Normalized property names are:
'Content-Length' -> 'content-length'
'content_length' -> 'content-length'
CGI.pm's header method also accepts aliases of property names. This module converts them as follows:
'content-type' -> 'type' 'cookie' -> 'cookies' 'set-cookie' -> 'cookies' 'window-target' -> 'target'
If a property name is duplicated, throws an exception:
$header->header; # => { # -Type => 'text/plain', # Content_Type => 'text/html', # } $header->rehash; # die "Property '-type' already exists"
Get or set the value of the header field. The header field name ($field) is not case sensitive.
$field
# field names are case-insensitive $header->get('Content-Length'); $header->get('content-length');
The $value argument must be a plain string:
$value
$header->set( 'Content-Length' => 3002 ); my $length = $header->get('Content-Length'); # => 3002
Returns a Boolean value telling whether the specified field exists.
if ( $header->exists('ETag') ) { ... }
Deletes the specified field form CGI response headers. Returns the value of the deleted field.
my $value = $header->delete('Content-Disposition'); # => 'inline'
This will remove all header properties.
It's identical to:
$header->query->header( $header->header );
The following methods were named after property names recognized by CGI.pm's header method. Most of these methods can both be used to read and to set the value of a property.
If you pass an argument to the method, the property value will be set, and also the current object itself will be returned; therefore you can chain methods as follows:
$header->type('text/html')->charset('utf-8');
If no argument is supplied, the property value will returned. If the given property doesn't exist, undef will be returned.
undef
Get or set the attachment property. Can be used to turn the page into an attachment. Represents suggested name for the saved file.
attachment
$header->attachment('genome.jpg');
In this case, the outgoing header will be formatted as:
Content-Disposition: attachment; filename="genome.jpg"
Get or set the charset property. Represents the character set sent to the browser.
charset
Get or set the cookies property.
cookies
Get or set the expires property. The Expires header gives the date and time after which the entity should be considered stale. You can specify an absolute or relative expiration interval. The following forms are all valid for this field:
expires
$header->expires( '+30s' ); # 30 seconds from now $header->expires( '+10m' ); # ten minutes from now $header->expires( '+1h' ); # one hour from now $header->expires( 'now' ); # immediately $header->expires( '+3M' ); # in three months $header->expires( '+10y' ); # in ten years time # at the indicated time & date $header->expires( 'Thu, 25 Apr 1999 00:40:33 GMT' );
Get or set the Location header.
$header->location('http://somewhere.else/in/movie/land');
Get or set the nph property. If set to a true value, will issue the correct headers to work with a NPH (no-parse-header) script.
nph
$header->nph(1);
Get or set the p3p property. The parameter can be an arrayref or a space-delimited string.
p3p
$header->p3p([qw/CAO DSP LAW CURa/]); # or $header->p3p('CAO DSP LAW CURa');
P3P: policyref="/w3c/p3p.xml", CP="CAO DSP LAW CURa"
Sets redirect URL with an optional status code and a human-readable message, which defaults to 302 Found. Returns this object itself.
302 Found
$header->redirect('http://somewhere.else/in/movie/land');
Get or set the Status header.
$header->status('304 Not Modified');
Get or set the Window-Target header.
$header->target('ResultsWindow');
Get or set the type property. Represents the media type of the message content.
type
$header->type('text/html');
The following plugin just adds the Content-Length header to CGI response headers sent by blosxom.cgi:
package content_length; use CGI::Header; sub start { !$blosxom::static_entries; } sub last { my $h = CGI::Header->new( header => $blosxom::header )->rehash; $h->set( 'Content-Length' => length $blosxom::output ); } 1;
Since Blosxom depends on the procedural interface of CGI.pm, you don't have to pass $query to new() in this case.
$query
new()
It's up to you to decide how to manage HTTP cookies.
use parent 'CGI::Header'; # Add cookie() attribute which defaults a reference to an empty hash. # The keys of the hash are the cookies' names, and their corresponding # values are a plain string, e.g., "$header->cookie->{ID} = 123456" sub cookie { $_[0]->{cookie} ||= {}; } # Override as_string() to create and set CGI::Cookie objects right before # stringifying header props. sub as_string { my $self = shift; my @cookies; while ( my ($name, $value) = each %{$self->cookie} ) { push @cookies, $self->query->cookie( $name => $value ); } $self->cookies( \@cookies )->SUPER::as_string; }
Since CGI::Simple is "a relatively lightweight drop in replacement for CGI.pm", this module is compatible with the module. If you're using the procedural interface of the module (CGI::Simple::Standard), you need to override the _build_query method as follows:
_build_query
use parent 'CGI::Header'; use CGI::Simple::Standard; sub _build_query { # NOTE: loader() is designed for debugging CGI::Simple::Standard->loader('_cgi_object'); }
Since the following strings conflict with property names, you can't use them as field names ($field):
"Attachment" "Charset" "Cookie" "Cookies" "NPH" "Target" "Type"
If you don't want to send the Content-Type header, set the type property to an empty string, though it's far from intuitive manipulation:
$header->type(q{}); $header->type(undef); # doesn't work as you expect
If one of the following conditions is met, the Date header will be set automatically, and also the header field will become read-only:
if ( $header->nph or $header->cookie or $header->expires ) { $header->set( 'Date' => 'Thu, 25 Apr 1999 00:40:33 GMT' ); # wrong $header->delete('Date'); # wrong }
You can't assign to the P3P header directly:
# wrong $header->set( 'P3P' => '/path/to/p3p.xml' );
CGI::header() restricts where the policy-reference file is located, and so you can't modify the location (/w3c/p3p.xml). You're allowed to set P3P tags using p3p().
CGI::header()
/w3c/p3p.xml
p3p()
If the following condition is met, the Pragma header will be set automatically, and also the header field will become read-only:
if ( $header->query->cache ) { $header->set( 'Pragma' => 'no-cache' ); # wrong $header->delete('Pragma'); # wrong }
If the following condition is met, the Server header will be set automatically, and also the header field will become read-only:
if ( $header->nph ) { $header->set( 'Server' => 'Apache/1.3.27 (Unix)' ); # wrong $header->delete('Server'); # wrong }
CGI, HTTP::Headers
There are no known bugs in this module. Please report problems to ANAZAWA (anazawa@cpan.org). Patches are welcome.
Ryo Anazawa (anazawa@cpan.org)
This module is free software; you can redistibute it and/or modify it under the same terms as Perl itself. See perlartistic.
To install CGI::Header, copy and paste the appropriate command in to your terminal.
cpanm
cpanm CGI::Header
CPAN shell
perl -MCPAN -e shell install CGI::Header
For more information on module installation, please visit the detailed CPAN module installation guide.