CGI-Application-Plugin-Apache

 view release on metacpan or  search on metacpan

Build.PL  view on Meta::CPAN

}

my $build = $build_pkg->new(
	module_name         => 'CGI::Application::Plugin::Apache',
	license             => 'perl',
	requires            => {
            'CGI::Application'                              => 3.22,
            'Exporter'                                      => 0,
            'Test::More'                                    => 0,
            $MP2 ? 'Apache2::Request' : 'Apache::Request'   => 0,
            $MP2 ? 'Apache2::Cookie'  : 'Apache::Cookie'    => 0,
            $MP2 ? 'Apache2::URI'     : 'Apache::URI'       => 0,
            'HTML::GenerateUtil'=> 0,
	},
    build_requires      => {
        'Apache::Test' => 1,
    },
    create_makefile_pl  => 'passthrough',
    create_readme       => 1,
);
$build->create_build_script();

META.json  view on Meta::CPAN

            "Apache::Test" : "1"
         }
      },
      "configure" : {
         "requires" : {
            "Module::Build" : "0.38"
         }
      },
      "runtime" : {
         "requires" : {
            "Apache::Cookie" : 0,
            "Apache::Request" : 0,
            "Apache::URI" : 0,
            "CGI::Application" : "3.22",
            "Exporter" : 0,
            "HTML::GenerateUtil" : 0,
            "Test::More" : 0
         }
      }
   },
   "provides" : {

META.yml  view on Meta::CPAN

  CGI::Application::Plugin::Apache:
    file: lib/CGI/Application/Plugin/Apache.pm
    version: 1.02
  CGI::Application::Plugin::Apache2::Request:
    file: lib/CGI/Application/Plugin/Apache2/Request.pm
    version: 0
  CGI::Application::Plugin::Apache::Request:
    file: lib/CGI/Application/Plugin/Apache/Request.pm
    version: 0
requires:
  Apache::Cookie: 0
  Apache::Request: 0
  Apache::URI: 0
  CGI::Application: 3.22
  Exporter: 0
  HTML::GenerateUtil: 0
  Test::More: 0
resources:
  license: http://dev.perl.org/licenses/
version: 1.02

README  view on Meta::CPAN

        use base 'CGI::Application';
        use CGI::Application::Plugin::Apache qw(:all);
    
        # then later we join our hero in a run mode...
        sub mode1 {
            my $self = shift;
            my $q = $self->query(); # $q is an Apache::Request obj not a CGI.pm obj

            # do some stuff
        
            # now we can bake a cookie using Apache::Cookie without interference  
            $cookie = Apache::Cookie->new(
                    $q,
                    -name       => 'foo',
                    -value      => 'bar',
                    -expires    => '+2h',
            );
            $cookie->bake;

            # now let's play with the content_type and other headers
            $q->content_type('text/plain');
            $q->header_out('MyHeader' => 'MyValue');

README  view on Meta::CPAN

    modules so along came this plugin. At the current moment it only does
    two things:

    Use Apache::Request as the "$self->query" object thus avoiding the
    creation of the CGI.pm object.
    Override the way CGI::Application creates and prints it's HTTP headers.
    Since it was using CGI.pm's "header()" and "redirect()" method's we
    needed an alternative. So now we use the "Apache->send_http_header()"
    method. This has a few additional benefits other than just not using
    CGI.pm. It means that we can use other Apache::* modules that might also
    create outgoing headers (e.g. Apache::Cookie) without CGI::Application
    clobbering them.

EXPORTED METHODS
    This module uses Exporter to provide methods to your application module.
    Most of the time you will never actually use these methods since they
    are used by CGI::Application itself, but I figured you'd like to know
    what's going on.

    No methods are exported by default. It is up to you to pick and choose,
    but please choose wisely. You can import all of the methods by using:

README  view on Meta::CPAN

    think it was a 'protected' not 'private) but right now it's the only way
    to have any say in how the HTTP headers are created. Please see "HTTP
    Headers" for more details.

HTTP Headers
    We encourage you to learn the mod_perl way of manipulating headers and
    cookies. It's really not that hard we promise. But incase you're easing
    your way into it, we try and provide as much backward compatibility as
    possible.

  Cookies
    HTTP cookies should now be created using Apache::Cookie and it's
    "bake()" method not with "header_add()" or "header_props()".

    You can still do the following to create a cookie

        my $cookie = CGI::Cookie->new(
            -name  => 'foo',
            -value => 'bar',
        );
        $self->header_add(-cookie => $cookie);

    But now we encourage you to do the following

        my $cookie = Apache::Cookie->new(
            $self->query,
            -name  => 'foo',
            -value => 'bar',
        );
        $cookie->bake();

  Redirects
    You can still do the following to perform an HTTP redirect

        $self->header_props( uri => $some_url);

README  view on Meta::CPAN

    always better to use the mod_perl api. If you still want to use
    "header_props()" or "header_add()" remember that it will cause a
    performance hit because it will use helper routines that try and emulate
    CGI.pm.

    If you wish to write code that performs well in both environments, you
    can check the $ENV{MOD_PERL} environment setting and branch accordingly.
    For example, to set a cookie:

      if ($ENV{MOD_PERL}) {
        require Apache::Cookie;
        $cookie = Apache::Cookie->new(
          $q,
          -name       => 'favorite',
          -value      => 'chocolate chip',
          -expires    => '+2h',
        );
        $cookie->bake;
      }
      else {
        $cookie = $self->query->cookie(
          -name    => 'favorite',

README  view on Meta::CPAN


    William McKee <william@knowmad.com>
    Cees Hek <ceeshek@gmail.com>
    Drew Taylor <drew@drewtaylor.com>
    Ron Savage <ron@savage.net.au>

SEE ALSO
    * CGI::Application
    * Apache
    * Apache::Request / Apache2::Request
    * Apache::Cookie / Apache2::Cookie

LICENSE
    This library is free software; you can redistribute it and/or modify it
    under the same terms as Perl itself.

lib/CGI/Application/Plugin/Apache.pm  view on Meta::CPAN

    if ( $p3p ) {
        $p3p = join ' ',@$p3p if ref($p3p) eq 'ARRAY';
        $q->headers_out->{'P3P'} = qq(policyref="/w3c/p3p.xml"); 
        $q->headers_out->{'CP'} = $p3p; 
    }
    # send all the cookies -- there may be several
    if ( $cookie ) {
        my(@cookie) = ref($cookie) && ref($cookie) eq 'ARRAY' ? @{$cookie} : $cookie;
        foreach (@cookie) {
            my $cs = '';
            if( UNIVERSAL::isa($_,'CGI::Cookie') || (MP2() ? UNIVERSAL::isa($_, 'Apache2::Cookie') : UNIVERSAL::isa($_,'Apache::Cookie') ) ) {
                $cs = $_->as_string;
            } else {
                $cs = $_;
            }
            $q->headers_out->add('Set-Cookie'  => $cs);
        }
    }
    # if the user indicates an expiration time, then we need an Expires
    if( $expires ) {
        $q->headers_out('Expires' => _expires($expires,'http'));
    }
    # if there's a location...this is generally done for redirects but there may be other reasons
    if( $uri ) {
        $q->headers_out->{'Location'} = $uri;
    }

lib/CGI/Application/Plugin/Apache.pm  view on Meta::CPAN

    use base 'CGI::Application';
    use CGI::Application::Plugin::Apache qw(:all);
    
    # then later we join our hero in a run mode...
    sub mode1 {
        my $self = shift;
        my $q = $self->query(); # $q is an Apache::Request obj not a CGI.pm obj

        # do some stuff
        
        # now we can bake a cookie using Apache::Cookie without interference  
        $cookie = Apache::Cookie->new(
                $q,
                -name       => 'foo',
                -value      => 'bar',
                -expires    => '+2h',
        );
        $cookie->bake;

        # now let's play with the content_type and other headers
        $q->content_type('text/plain');
        $q->header_out('MyHeader' => 'MyValue');

lib/CGI/Application/Plugin/Apache.pm  view on Meta::CPAN


=over

=item Use Apache::Request as the C<< $self->query >> object thus avoiding the creation
of the CGI.pm object.

=item Override the way L<CGI::Application> creates and prints it's HTTP headers. Since it was using
L<CGI.pm|CGI>'s C<< header() >> and C<< redirect() >> method's we needed an alternative. So now we
use the C<< Apache->send_http_header() >> method. This has a few additional benefits other
than just not using L<CGI.pm|CGI>. It means that we can use other Apache::* modules that might
also create outgoing headers (e.g. L<Apache::Cookie>) without L<CGI::Application> clobbering
them.

=back

=head1 EXPORTED METHODS

This module uses L<Exporter> to provide methods to your application module. Most of the time
you will never actually use these methods since they are used by L<CGI::Application> itself,
but I figured you'd like to know what's going on.

lib/CGI/Application/Plugin/Apache.pm  view on Meta::CPAN

I didn't like the idea of exporting this private method (I'd rather think it was a 'protected'
not 'private) but right now it's the only way to have any say in how the HTTP headers are created.
Please see L<"HTTP Headers"> for more details.

=head1  HTTP Headers

We encourage you to learn the mod_perl way of manipulating headers and cookies. It's really not
that hard we promise. But incase you're easing your way into it, we try and provide as much
backward compatibility as possible.

=head2 Cookies

HTTP cookies should now be created using L<Apache::Cookie> and it's C<< bake() >> method not with 
C<< header_add() >> or C<< header_props() >>.

You can still do the following to create a cookie

    my $cookie = CGI::Cookie->new(
        -name  => 'foo',
        -value => 'bar',
    );
    $self->header_add(-cookie => $cookie);

But now we encourage you to do the following

    my $cookie = Apache::Cookie->new(
        $self->query,
        -name  => 'foo',
        -value => 'bar',
    );
    $cookie->bake();

=head2 Redirects 

You can still do the following to perform an HTTP redirect

lib/CGI/Application/Plugin/Apache.pm  view on Meta::CPAN

If you are trying to use this module but don't want to have to change your previous code that
uses C<< header_props() >> or C<< header_add() >> then we try to help you out by being as CGI
compatible as we can, but it is always better to use the mod_perl api. If you still want to use
C<< header_props() >> or C<< header_add() >> remember that it will cause a performance hit because
it will use helper routines that try and emulate L<CGI.pm|CGI>.

If you wish to write code that performs well in both environments, you can check the $ENV{MOD_PERL}
environment setting and branch accordingly. For example, to set a cookie:

  if ($ENV{MOD_PERL}) {
    require Apache::Cookie;
    $cookie = Apache::Cookie->new(
      $q,
      -name       => 'favorite',
      -value      => 'chocolate chip',
      -expires    => '+2h',
    );
    $cookie->bake;
  }
  else {
    $cookie = $self->query->cookie(
      -name    => 'favorite',

lib/CGI/Application/Plugin/Apache.pm  view on Meta::CPAN

=head1 SEE ALSO
                                                                                                                                           
=over 8
                                                                                                                                           
=item * L<CGI::Application>
                                                                                                                                           
=item * L<Apache>

=item * L<Apache::Request> / L<Apache2::Request>

=item * L<Apache::Cookie> / L<Apache2::Cookie>
                                                                                                                                           
=back
                                                                                                                                           
=head1 LICENSE
                                                                                                                                           
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
                                                                                                                                           
=cut

lib/CGI/Application/Plugin/Apache/Request.pm  view on Meta::CPAN

package CGI::Application::Plugin::Apache::Request;
use strict;
use HTML::GenerateUtil;
use base 'Apache::Request';
use Apache::Request;
use Apache::Cookie;
use Apache::URI;

sub new {
    my($class, @args) = @_;
    return bless $class->SUPER::new(@args), $class;
}

=pod

=head1 NAME

lib/CGI/Application/Plugin/Apache/Request.pm  view on Meta::CPAN

}

=item cookie()

=cut

sub cookie {
    my ($self, @args) = @_;
    if($#args == 0) {
        # if we just have a name of a cookie then retrieve the value of the cookie
        my $cookies = Apache::Cookie->fetch();
        if( $cookies && $cookies->{$args[0]} ) {
            return $cookies->{$args[0]}->value;
        } else {
            return;
        }
    } else {
        # else we have several values so try and create a new cookie
        return Apache::Cookie->new($self, @args);
    }
}

=item Dump()

=cut

sub Dump {
    my $self = shift;
    my($param,$value,@result);

lib/CGI/Application/Plugin/Apache2/Request.pm  view on Meta::CPAN

package CGI::Application::Plugin::Apache2::Request;
use strict;
use Apache2::Request;
our @ISA = qw(Apache2::Request);
use Apache2::Upload;
use Apache2::Cookie;
use Apache2::URI;

sub new {
    my ($class, @args) = @_;
    return bless {
        r             => Apache2::Request->new(@args),
        __cap_params  => {},
        __cap_deleted => 0,
    }, $class;
}

lib/CGI/Application/Plugin/Apache2/Request.pm  view on Meta::CPAN

}

=item cookie()

=cut

sub cookie {
    my ($self, @args) = @_;
    if($#args == 0) {
        # if we just have a name of a cookie then retrieve the value of the cookie
        my $cookies = Apache2::Cookie->fetch();
        if( $cookies && $cookies->{$args[0]} ) {
            return $cookies->{$args[0]}->value;
        } else {
            return;
        }
    } else {
        # else we have several values so try and create a new cookie
        return Apache2::Cookie->new($self, @args);
    }
}

=item Dump()

=cut

sub Dump {
    my $self = shift;
    my($param,$value,@result);

t/02-cgi_compat.t  view on Meta::CPAN

    ok($content =~ /obj is CGI::Application::Plugin::Apache\d?::Request/);
}

# 4..9
# cookie()
{
    $response = GET '/cgi_compat?rm=cookie_set';
    ok($response->is_success);
    $content = $response->content();
    ok($content =~ /in runmode cookie_set/);
    ok($response->header('Set-Cookie') =~ /cgi_cookie=yum/);

    $response = GET '/cgi_compat?rm=cookie_get';
    ok($response->is_success);
    $content = $response->content();
    ok($content =~ /in runmode cookie_get/);
    ok($content =~ /cookie value = 'yum'/);
}

# 10..14
# Dump()

t/03-mod_perl.t  view on Meta::CPAN

    ok($response->header('Me') eq 'Myself and I');
}

# 18..21
{
    $response = GET '/mp?rm=cgi_cookie';
    ok($response->is_success);
    $content = $response->content();
    ok($content =~ /in runmode cgi_cookie/);
    ok($response->header('Content-Type') =~ /text\/html/);
    ok($response->header('Set-Cookie') =~ /cgi_cookie=yum/);
}

# 22..25
{
    $response = GET '/mp?rm=apache_cookie';
    ok($response->is_success);
    $content = $response->content();
    ok($content =~ /in runmode apache_cookie/);
    ok($response->header('Content-Type') =~ /text\/html/);
    ok($response->header('Set-Cookie') =~ /apache_cookie=yummier/);
}

# 26..29
{
    $response = GET '/mp?rm=baking_apache_cookie';
    ok($response->is_success);
    $content = $response->content();
    ok($content =~ /in runmode baking_apache_cookie/);
    ok($response->header('Content-Type') =~ /text\/html/);
    ok($response->header('Set-Cookie') =~ /baked_cookie=yummiest/);
}

# 30..34
{
    $response = GET '/mp?rm=cgi_and_apache_cookies';
    ok($response->is_success);
    $content = $response->content();
    ok($content =~ /in runmode cgi_and_apache_cookies/);
    ok($response->header('Content-Type') =~ /text\/html/);
    ok($response->header('Set-Cookie') =~ /cgi_cookie=yum(:|%3A)both/);
    ok($response->header('Set-Cookie') =~ /apache_cookie=yummier(:|%3(A|a))both/);
}

# 35..39
{
    $response = GET '/mp?rm=cgi_and_baked_cookies';
    ok($response->is_success);
    $content = $response->content();
    ok($content =~ /in runmode cgi_and_baked_cookies/);
    ok($response->header('Content-Type') =~ /text\/html/);
    ok($response->header('Set-Cookie') =~ /cgi_cookie=yum(:|%3(A|a))both/);
    ok($response->header('Set-Cookie') =~ /baked_cookie=yummiest(:|%3(A|a))both/);
}

# 40..43
{
    $response = GET '/mp?rm=redirect_cookie';
    ok($response->is_success);
    $content = $response->content();
    ok($response->header('Content-Type') =~ /text\/html/);
    ok($content =~ /in runmode redirect2/);
    ok($content =~ /cookie value = 'mmmm'/);
}

# 44..47
{
    $response = GET '/mp?rm=cookies';
    ok($response->is_success);
    $content = $response->content();
    ok($content =~ /in runmode cookies/);
    ok($response->header('Set-Cookie') =~ /cookie1=mmmm/);
    ok($response->header('Set-Cookie') =~ /cookie2=tasty/);
}





t/lib/ApachePlugin/CGI_compat.pm  view on Meta::CPAN

package ApachePlugin::CGI_compat;
use base 'CGI::Application';
use strict;
use warnings;
use CGI::Cookie;
use CGI::Application::Plugin::Apache qw(:all);

my $content = "<h1>HELLO THERE</h1>";

sub setup {
    my $self = shift;
    $self->start_mode('header');
    $self->run_modes([qw(
        query_obj
        cookie_set

t/lib/ApachePlugin/MP.pm  view on Meta::CPAN

package ApachePlugin::MP;
use base 'CGI::Application';
use strict;
use warnings;
use CGI::Cookie;
use CGI::Application::Plugin::Apache qw(:all);
my $MP2;
my $APACHE_COOKIE_CLASS;

BEGIN {
    $MP2 = $ENV{MOD_PERL_API_VERSION} == 2;
    if( $MP2 ) {
        require Apache2::Cookie;
        import Apache2::Cookie ();
        $APACHE_COOKIE_CLASS = 'Apache2::Cookie';
    } else {
        require Apache::Cookie;
        import Apache::Cookie ();
        $APACHE_COOKIE_CLASS = 'Apache::Cookie';
    }
};

sub setup {
    my $self = shift;
    $self->start_mode('header');
    $self->run_modes([qw(
        query_obj
        header
        no_header

t/lib/ApachePlugin/MP.pm  view on Meta::CPAN

    $self->header_type('header');
    $self->header_add(
        -me => 'Myself and I', 
    );
    return "Im in runmode add_header";
}

sub cgi_cookie {
    my $self = shift;
    $self->header_type('header');
    my $cookie = CGI::Cookie->new(
        -name    => 'cgi_cookie',
        -value   => 'yum',
    );
    $self->header_add(
        -cookie => $cookie,
    );
    return "Im in runmode cgi_cookie";
}

sub apache_cookie {

t/lib/ApachePlugin/MP.pm  view on Meta::CPAN

        -name    => 'baked_cookie',
        -value   => 'yummiest',
    );
    $cookie->bake($self->query);
    return "Im in runmode baking_apache_cookie";
}

sub cgi_and_apache_cookies {
    my $self = shift;
    $self->header_type('header');
    my $cookie1 = CGI::Cookie->new(
        -name    => 'cgi_cookie',
        -value   => 'yum:both',
    );
    my $cookie2 = $APACHE_COOKIE_CLASS->new(
        $self->query,
        -name    => 'apache_cookie',
        -value   => 'yummier:both',
    );
    $self->header_props(
        -cookie => [$cookie2, $cookie1],
    );
    return "Im in runmode cgi_and_apache_cookies";
}

sub cgi_and_baked_cookies {
    my $self = shift;
    $self->header_type('header');
    my $cookie1 = CGI::Cookie->new(
        -name    => 'cgi_cookie',
        -value   => 'yum:both',
    );
    my $cookie2 = $APACHE_COOKIE_CLASS->new(
        $self->query,
        -name    => 'baked_cookie',
        -value   => 'yummiest:both',
    );
    $self->header_props(
        -cookie => $cookie1,
    );
    $cookie2->bake($self->query);
    return "Im in runmode cgi_and_baked_cookies";
}

sub cookies {
    my $self = shift;
    $self->header_type('header');
    my $cookie1 = CGI::Cookie->new(
        -name    => 'cookie1',
        -value   => 'mmmm',
    );
    my $cookie2 = CGI::Cookie->new(
        -name    => 'cookie2',
        -value   => 'tasty',
    );
    $self->header_props( -cookie => [ $cookie1, $cookie2 ]);
    return "Im in runmode cookies";
}

1;



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