CGI-Header

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

    arrayref
  - Remove push_cookie(). It's up to you to decide how to manage HTTP cookies.
    For example, Plack::Response#cookies behaves like HASH. On the other
    hand, Mojo::Message::Response#cookies behaves like ARRAY.

0.46  Apr 11th, 2013

  - Remove CGI::Header::PSGI from this distribution. Sorry for confusing
    changes.
  - Simplify cookie() and p3p()
  - push_cookie() creates CGI::Cookie object automatically

0.45  Apr 10th, 2013

  - Add CGI::Header::PSGI to this distribution and rewrite the module
    entirely.

  [INCOMPATIBLE CHANGES]

    - Rewrite test suite entirely
    - Remove flatten() and as_hashref() from CGI::Header

Changes  view on Meta::CPAN


    use parent 'CGI';
    use CGI::Header::PSGI qw(psgi_header psgi_redirect);

  CGI::Header::PSGI helps you create your own CGI::PSGI-compatible class.

  [INCOMPATIBLE CHANGES]

    - rehash() and new() rename '-uri' and '-url' to '-location'

    - flatten() and each() stringify CGI::Cookie objects by default.

0.18  Feb 9th, 2013

  - You can pass a media type to new() in the following situation:

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

  - new() throws an exception in the following situation:

Changes  view on Meta::CPAN

    - Rename _normalize() to _lc()
    - Add a benchmark against HTTP::Response->parse

  [DOCUMENTATION]
    - Add HTTP::Headers to "SEE ALSO"

0.11  Dec 16th, 2012
  - Add FIRSTKEY() and NEXTKEY(). These methods were implemented to
    test this module itself.
  - flatten() receives optional $is_recursive argument
    which determines whether to flatten the Set-Cookie headers recursively.
    $is_recursive defaults to true.
  - flatten() and each() don't stringify CGI::Cookie objects.
  - field_names() returns a list of field names in a random order

0.10  Dec 14th, 2012
  - p3p_tags() returns the number of P3P tags
    instead of the first element in scalar context
  - rehash() returns the current object itself:
      my @headers = CGI::Header->new(@args)->rehash->flatten;
  - each() doesn't depend on field_names().
    field_names() depends on each().
    As a result, if Set-Cookie header is multi-valued,
    field_names() will return a list which contains duplicate values.
    I don't know how to solve this problem at this time.

0.09  Nov 13th, 2012
  - CGI.pm has charset() attribute which defaults to 'ISO-8859-1'.
    CGI::header() function depends on this attribute.
    I noticed this module depends on the default value, 'ISO-8859-1',
    and so decided to remove this dependency because this module
    shouldn't depend on the internal state of CGI.pm.
    Namely,

Changes  view on Meta::CPAN

0.05  Nov 10th, 2012
  - add rehash() method which rebuilds header hash references

0.04  Nov 7th, 2012
  - reorganized tests using subtest() function exported by Test::More
  - doesn't overload '""' (stringify) with as_string()
    because this module isn't the replacement of CGI::header() function.
    I think CGI::header() should be used to stringify
    header hash references in most cases.
  - each() doesn't stringify values (cf. HTTP::Headers->scan),
    and so the callback function will receive raw CGI::Cookie objects.
  - On the other hand, flatten() forces stringification.
    flatten() may be called to generate PSGI-compatible header array
    references.

0.03  Oct 7th, 2012
  - add a benchmark against HTTP::Parser::XS
  - update POD
  - add t/server.t
  - tests require CGI.pm 3.51 because the distribution contains t/headers.t

README  view on Meta::CPAN

      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

README  view on Meta::CPAN


    $self = $header->charset( $character_set )
    $character_set = $header->charset
        Get or set the "charset" property. Represents the character set sent
        to the browser.

    $self = $header->cookies( $cookie )
    $self = $header->cookies([ $cookie1, $cookie2, ... ])
    $cookies = $header->cookies
        Get or set the "cookies" property. The parameter can be a
        CGI::Cookie object or an arrayref which consists of CGI::Cookie
        objects.

    $self = $header->expires( $format )
    $format = $header->expires
        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:

          $header->expires( '+30s' ); # 30 seconds from now

README  view on Meta::CPAN


    Since Blosxom <http://blosxom.sourceforge.net/> depends on the
    procedural interface of CGI.pm, you don't have to pass $query to "new()"
    in this case.

  HANDLING HTTP COOKIES
    It's up to you to decide how to manage HTTP cookies. The following
    method behaves like Mojo::Message::Response's "cookies" method:

      use parent 'CGI::Header';
      use CGI::Cookie;

      sub cookies {
          my $self    = shift;
          my $cookies = $self->header->{cookies} ||= [];

          return $cookies unless @_;

          if ( ref $_[0] eq 'HASH' ) {
              push @$cookies, map { CGI::Cookie->new($_) } @_;
          }
          else {
              push @$cookies, CGI::Cookie->new( @_ );
          }

          $self;
      }

    You can use the "cookies" method as follows:

      # get an arrayref which consists of CGI::Cookie objects
      my $cookies = $header->cookies;

      # push a CGI::Cookie object onto the "cookies" property
      $header->cookies( ID => 123456 );
      $header->cookies({ name => 'ID', value => 123456 });

  WORKING WITH CGI::Simple
    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:

      use parent 'CGI::Header';

README  view on Meta::CPAN

          # NOTE: loader() is designed for debugging
          CGI::Simple::Standard->loader('_cgi_object');
      }

LIMITATIONS
    Since the following strings conflict with property names, you can't use
    them as field names ($field):

      "Attachment"
      "Charset"
      "Cookie"
      "Cookies"
      "NPH"
      "Target"
      "Type"

    Content-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{});

examples/lib/CGI/Simple/Header/Adapter.pm  view on Meta::CPAN

        unless ( $query->cache or $self->exists('Pragma') ) {
            $self->set( 'Pragma' => 'no-cache' );
        }
    }

    $self->SUPER::as_arrayref;
}

sub _bake_cookie {
    my ( $self, $cookie ) = @_;
    ref $cookie eq 'CGI::Simple::Cookie' ? $cookie->as_string : $cookie;
}

sub _date {
    my ( $self, $expires ) = @_;
    CGI::Simple::Util::expires( $expires, 'http' );
}

1;

examples/lib/MyApp/Header.pm  view on Meta::CPAN

package MyApp::Header;
use strict;
use warnings;
use parent 'CGI::Header';
use CGI::Cookie;

sub cookies {
    my $self    = shift;
    my $cookies = $self->header->{cookies} ||= [];

    return $cookies unless @_;

    if ( ref $_[0] eq 'HASH' ) {
        push @$cookies, map { CGI::Cookie->new($_) } @_;
    }
    else {
        push @$cookies, CGI::Cookie->new( @_ );
    }

    $self;
}

1;

examples/t/cgi_redirect.t  view on Meta::CPAN

use Test::Output;

BEGIN {
    use_ok 'CGI::Redirect';
}

my $redirect = CGI::Redirect->new;

my %data = (
    '-Content_Type' => 'type',
    '-Cookie'       => 'cookies',
    '-URI'          => 'location',
    '-URL'          => 'location',
);

while ( my ($input, $expected) = each %data ) {
    is $redirect->_normalize($input), $expected;
}

is $redirect->location('http://somewhere.else/in/movie/land'), $redirect;

examples/t/myapp_header.t  view on Meta::CPAN

use Test::More tests => 3;
use Test::Output;

BEGIN {
    use_ok 'MyApp::Header';
}

my $header = MyApp::Header->new;

is $header->cookies( ID => 123456 ), $header;
stdout_like { $header->finalize } qr{Set-Cookie: ID=123456};

lib/CGI/Header.pm  view on Meta::CPAN

  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

lib/CGI/Header.pm  view on Meta::CPAN

Get or set the C<charset> property. Represents the character set sent to
the browser.

=item $self = $header->cookies( $cookie )

=item $self = $header->cookies([ $cookie1, $cookie2, ... ])

=item $cookies = $header->cookies

Get or set the C<cookies> property.
The parameter can be a L<CGI::Cookie> object or an arrayref which consists of
L<CGI::Cookie> objects.

=item $self = $header->expires( $format )

=item $format = $header->expires

Get or set the C<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:

lib/CGI/Header.pm  view on Meta::CPAN

interface of CGI.pm, you don't have to pass C<$query> to C<new()>
in this case.

=head2 HANDLING HTTP COOKIES

It's up to you to decide how to manage HTTP cookies.
The following method behaves like L<Mojo::Message::Response>'s C<cookies>
method:

  use parent 'CGI::Header';
  use CGI::Cookie;

  sub cookies {
      my $self    = shift;
      my $cookies = $self->header->{cookies} ||= [];

      return $cookies unless @_;

      if ( ref $_[0] eq 'HASH' ) {
          push @$cookies, map { CGI::Cookie->new($_) } @_;
      }
      else {
          push @$cookies, CGI::Cookie->new( @_ );
      }

      $self;
  }

You can use the C<cookies> method as follows:

  # get an arrayref which consists of CGI::Cookie objects
  my $cookies = $header->cookies;

  # push a CGI::Cookie object onto the "cookies" property
  $header->cookies( ID => 123456 );
  $header->cookies({ name => 'ID', value => 123456 });

=head2 WORKING WITH CGI::Simple

Since L<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
(L<CGI::Simple::Standard>), you need to override the C<_build_query> method
as follows:

lib/CGI/Header.pm  view on Meta::CPAN

      CGI::Simple::Standard->loader('_cgi_object');
  }

=head1 LIMITATIONS

Since the following strings conflict with property names,
you can't use them as field names (C<$field>):

  "Attachment"
  "Charset"
  "Cookie"
  "Cookies"
  "NPH"
  "Target"
  "Type"

=over 4

=item Content-Type

If you don't want to send the Content-Type header,
set the C<type> property to an empty string, though it's far from intuitive

lib/CGI/Header/Adapter.pm  view on Meta::CPAN

    push @headers, 'Window-Target', $target if $target;

    if ( $p3p ) {
        my $tags = ref $p3p eq 'ARRAY' ? join ' ', @{$p3p} : $p3p;
        push @headers, 'P3P', qq{policyref="/w3c/p3p.xml", CP="$tags"};
    }

    my @cookies = ref $cookies eq 'ARRAY' ? @{$cookies} : $cookies;
       @cookies = map { $self->_bake_cookie($_) || () } @cookies;

    push @headers, map { ('Set-Cookie', $_) } @cookies;
    push @headers, 'Expires', $self->_date($expires) if $expires;
    push @headers, 'Date', $self->_date if $expires or @cookies or $nph;
    push @headers, 'Pragma', 'no-cache' if $query->cache;

    if ( $attachment ) {
        my $value = qq{attachment; filename="$attachment"};
        push @headers, 'Content-Disposition', $value;
    }

    push @headers, map { ucfirst $_, $header{$_} } keys %header;

lib/CGI/Header/Adapter.pm  view on Meta::CPAN

        $charset = $query->charset if !defined $charset;
        $value .= "; charset=$charset" if $charset && $value !~ /\bcharset\b/;
        push @headers, 'Content-Type', $value;
    }

    \@headers;
}

sub _bake_cookie {
    my ( $self, $cookie ) = @_;
    ref $cookie eq 'CGI::Cookie' ? $cookie->as_string : $cookie;
}

sub _date {
    my ( $self, $expires ) = @_;
    CGI::Util::expires( $expires, 'http' );
}

1;

__END__

t/10_basic.t  view on Meta::CPAN

use CGI::Header;
use Test::More tests => 11; 
use Test::Exception;
use Test::Output;

subtest 'normalization' => sub {
    my $header = CGI::Header->new;

    my %data = (
        '-Content_Type'  => 'type',
        '-Cookie'        => 'cookies',
        #'-Set_Cookie'    => 'cookies',
        #'-Window_Target' => 'target',
    );

    while ( my ($input, $expected) = each %data ) {
        is $header->_normalize($input), $expected;
    }
};

subtest 'CGI::Header#new' => sub {
    my $header = CGI::Header->new;

t/20_adapter.t  view on Meta::CPAN

use strict;
use warnings;
use Test::MockTime qw/set_fixed_time/;
use CGI;
use CGI::Cookie;
use CGI::Header::Adapter;
use Test::More tests => 3;
use Test::Exception;

set_fixed_time( 1341637509 );

# NOTE: $q->redirect( -p3p => \@tags ) doesn't work

my @args = (
    '-NPH'          => 1,
    '-Status'       => '304 Not Modified',
    '-Content_Type' => 'text/plain',
    '-Charset'      => 'utf-8',
    '-Attachment'   => 'genome.jpg',
    '-P3P'          => 'CAO DSP LAW CURa',
    '-Target'       => 'ResultsWindow',
    '-Expires'      => '+3d',
    '-Foo_Bar'      => 'baz',
    '-Cookie'       => [ CGI::Cookie->new(ID => 123456) ],
);

my $header = CGI::Header::Adapter->new( header => { @args } );

is_deeply $header->as_arrayref, [
    'Server',              'cmdline',
    'Status',              '304 Not Modified',
    'Window-Target',       'ResultsWindow',
    'P3P',                 'policyref="/w3c/p3p.xml", CP="CAO DSP LAW CURa"',
    'Set-Cookie',          'ID=123456; path=/',
    'Expires',             'Tue, 10 Jul 2012 05:05:09 GMT',
    'Date',                'Sat, 07 Jul 2012 05:05:09 GMT',
    'Content-Disposition', 'attachment; filename="genome.jpg"',
    'Foo-bar',             'baz',
    'Content-Type',        'text/plain; charset=utf-8',
];

is $header->as_string, $header->query->header( @args );

throws_ok { $header->finalize } qr{^call to abstract method};



( run in 0.573 second using v1.01-cache-2.11-cpan-e9199f4ba4c )