Ryo Anazawa > CGI-Header-0.05 > CGI::Header

Download:
CGI-Header-0.05.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.05   Source   Latest Release: CGI-Header-0.63

NAME ^

CGI::Header - Adapter for CGI::header() function

SYNOPSIS ^

  use CGI::Header;

  # supported parameters
  my $header = {
      -attachment => 'foo.gif',
      -charset    => 'utf-7',
      -cookie     => [ $cookie1, $cookie2 ], # CGI::Cookie objects
      -expires    => '+3d',
      -nph        => 1,
      -p3p        => [qw/CAO DSP LAW CURa/],
      -target     => 'ResultsWindow',
      -type       => 'image/gif',
  };

  my $h = CGI::Header->new( $header );

  # update $header
  $h->set( 'Content-Length' => 3002 );
  $h->delete( 'Content-Disposition' );
  $h->clear;

  my @headers = $h->flatten;
  # => ( 'Content-length', '3002', 'Content-Type', 'text/plain' )

  print $h->as_string;
  # Content-length: 3002
  # Content-Type: text/plain

  $h->header; # same reference as $header

DESCRIPTION ^

This module is a utility class to manipulate a hash reference which CGI's header() function receives.

METHODS

$header = CGI::Header->new({ -type => 'text/plain', ... })

Given a header hash reference, returns a CGI::Header object which holds a reference to the original given argument:

  my $header = { -type => 'text/plain' };
  my $h = CGI::Header->new( $header );

The object updates the reference when called write methods like set(), delete() or clear():

  $h->set( 'Content-Length' => 3002 );
  $h->delete( 'Content-Disposition' );
  $h->clear;

It also has header() method that would return the same reference:

  $h->header; # same reference as $header
$header->rehash

Rebuilds the header hash reference:

  use Data::Dumper;

  print Dumper( $header->header );
  # => {
  #     '-content_type' => 'text/plain',
  #     'Set_Cookie'    => 'ID=123456; path=/',
  #     'expires'       => '+3d',
  #     '-target'       => 'ResultsWindow',
  # }

  $header->rehash;

  print Dumper( $header->header );
  # => {
  #     '-type'    => 'text/plain',
  #     '-cookie'  => 'ID=123456; path=/',
  #     '-expires' => '+3d',
  #     '-target'  => 'ResultsWindow',
  # }
$value = $header->get( $field )
$header->set( $field => $value )

Get or set the value of the header field. The header field name ($field) is not case sensitive. You can use underscores as a replacement for dashes in header names.

  # field names are case-insensitive
  $header->get( 'Content-Length' );
  $header->get( 'content_length' );

The $value argument may be a plain string or a reference to an array of CGI::Cookie objects for the Set-Cookie header.

  $header->set( 'Content-Length' => 3002 );
  $header->set( 'Set-Cookie' => [$cookie1, $cookie2] );
$bool = $header->exists( $field )

Returns a Boolean value telling whether the specified field exists.

  if ( $header->exists('ETag') ) {
      ...
  }
$value = $header->delete( $field )

Deletes the specified field form CGI response headers. Returns the value of the deleted field.

  my $value = $header->delete( 'Content-Disposition' ); # => 'inline'
@fields = $header->field_names

Returns the list of distinct field names present in the header. The field names have case as returned by CGI::header().

  my @fields = $header->field_names;
  # => ( 'Set-Cookie', 'Content-length', 'Content-Type' )
$header->each( \&callback )

Apply a subroutine to each header field in turn. The callback routine is called with two parameters; the name of the field and a value. If the Set-Cookie header is multi-valued, then the routine is called once for each value. Any return values of the callback routine are ignored.

  my @lines;

  $header->each(sub {
      my ( $field, $value ) = @_;
      push @lines, "$field: $value";
  });

  print join @lines, "\n";
  # Content-length: 3002
  # Content-Type: text/plain
@headers = $header->flatten

Returns pairs of fields and values.

  @headers = $header->flatten;
  # => ( 'Content-length', '3002', 'Content-Type', 'text/plain' )

It's identical to:

  my @headers;
  $self->each(sub {
      my ( $field, $value ) = @_;
      push @headers, $field, "$value"; # force stringification
  });

This method can be used to generate PSGI-compatible header array references:

  # NOTE: untested

  my $status_code = $header->delete( 'Status' ) || '200 OK';
  $status_code =~ s/\D*$//;

  my @headers = $header->flatten;
$header->clear

This will remove all header fields.

$bool = $header->is_empty

Returns true if the header contains no key-value pairs.

  $header->clear;

  if ( $header->is_empty ) { # true
      ...
  }
$clone = $header->clone

Returns a copy of this CGI::Header object. It's identical to:

  my %copy = %{ $header->header };
  my $clone = CGI::Header->new( \%copy );
$header->as_string
$header->as_string( $eol )

Returns the header fields as a formatted MIME header. The optional $eol parameter specifies the line ending sequence to use. The default is \015\012.

The following:

  use CGI;
  print CGI::header( $header->header );

is identical to:

  my $CRLF = $CGI::CRLF;
  print $header->as_string( $CRLF ), $CRLF;

When valid multi-line headers are included, this method will always output them back as a single line, according to the folding rules of RFC 2616: the newlines will be removed, while the white space remains.

Unlike CGI.pm, when invalid newlines are included, this module removes them instead of throwing exceptions.

$filename = $header->attachment
$header->attachment( $filename )

Can be used to turn the page into an attachment. Represents suggested name for the saved file.

  $header->attachment( 'genome.jpg' );

In this case, the outgoing header will be formatted as:

  Content-Disposition: attachment; filename="genome.jpg"
@tags = $header->p3p_tags
$header->p3p_tags( @tags )

Represents P3P tags. The parameter can be an array or a space-delimited string. Returns a list of P3P tags.

  $header->p3p_tags(qw/CAO DSP LAW CURa/);

In this case, the outgoing header will be formatted as:

  P3P: policyref="/w3c/p3p.xml", CP="CAO DSP LAW CURa"
$format = $header->expires
$header->expires( $format )

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:

  $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' );
$header->nph

If set to a true value, will issue the correct headers to work with a NPH (no-parse-header) script.

  $header->nph( 1 );

DIAGNOSTICS ^

Can't set '-content_type' to neither undef nor an empty string
  # wrong
  $header->set( 'Content-Type' => undef );
  $header->set( 'Content-Type' => q{} );

Use delete() instead:

  $header->delete( 'Content-Type' );
Can't assign to 'Expires' directly, use expires() instead
  # wrong
  $header->set( 'Expires' => '+3d' );

Use expires() instead:

  $header->expires( '+3d' );

This module follows the rule of least surprize. The following behavior will surprize us:

  $header->set( 'Expires' => '+3d' );

  my $value = $header->get( 'Expires' );
  # => "Thu, 25 Apr 1999 00:40:33 GMT"
Can't assign to '-p3p' directly, use p3p_tags() instead

CGI::header() restricts where the policy-reference file is located, and so you can't modify the location (/w3c/p3p.xml). The following code doesn't work as you expect:

  # wrong
  $header->set( 'P3P' => '/path/to/p3p.xml' );

You're allowed to set P3P tags using p3p_tags().

SEE ALSO ^

CGI, Plack::Util

BUGS AND LIMITATIONS ^

This module is beta state. API may change without notice.

AUTHOR ^

Ryo Anazawa (anazawa@cpan.org)

LICENSE ^

This module is free software; you can redistibute it and/or modify it under the same terms as Perl itself. See perlartistic.

syntax highlighting: