Here is the initial documentation from an idea I'm playing with and wanted to get some feedback on. I've attached the code to this message as well...

Thanks
--
Michael Peters
Developer
Plus Three, LP


NAME CGI::Application::Plugin::Apache - Allow CGI::Application to use Apache::* modules without interference

SYNOPSIS
        use base 'CGI::Application';
        use CGI::Application::Plugin::Apache;

# 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


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


        1;
 DESCRIPTION
    This plugin helps to try and fix some of the annoyances of using
    CGI::Application in a pure mod_perl environment. CGI::Application
    assumes that you use CGI.pm, but I wanted to avoid it's bloat and have
    access to the performance of the Apache::* 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.


cgiapp_get_query()
This overrides CGI:App's method for retrieving the query object. This is
the standard way of using something other than CGI.pm so it's no
surprise that we use it here. It simply creates and returns a new
Apache::Request object from "Apache->request".


_send_headers()
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. This method will
only recognize the following values set by the "$self->header_type()"
method:


    * header
        This means we just call "Apache->send_http_header('text/html')".
        This will also use any other headers added by baking cookies, etc

    * none
        The means we don't send any headers and it's up to the user to send
        them however they want.

    * redirect
        This is an option that CGI::Application uses to perform a
        redirection header from the values set in the
        "$self->header_props()" method. This means that right now we don't
        do anything with it since it's so easy to say
        "$self->query->internal_redirect($some_url)" and in my opinion much
        cleaner. For more information see "Headers and Cookies".

CAVEATS
Upon using this module you completely leave behind the world of
CGI.pm. Don't look back or you might turn into a pillar of salt. You
will have to look at and read the docs of the Apache::* modules. But
don't worry, they are really easy to use and were designed to mimic
the interface of CGI.pm. This does however mean that somethings
don't quite work the way they used to in the CGI::Application world.
In particular:


  HTTP Headers
        * Cookies
            HTTP headers (and hence HTTP cookies) are now not created with
            "header_add()" or "header_props()". Instead use Apache::Cookie
            for cookies and anything you want for additional header
            manipulation.

        * Redirects
            This also means that the "$self->header_type('redirect')"
            doesn't currently do anything. Instead you should use
            "$self->query->internal_redirect($some_url)" for external
            redirects and use something like

                $self->query->header_out(Location => $some_url);
                $self->query->status(REDIRECT);

AUTHOR
            Michael Peters <[EMAIL PROTECTED]>

SEE ALSO
            * CGI::Application
            * Apache
            * Apache::Registry
            * Apache::Cookie

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

package CGI::Application::Plugin::Apache;
use strict;
use base 'Exporter';
use Apache;
use Apache::Request;
use Apache::Reload;
use Carp;

$CGI::Application::Plugin::Apache::VERSION = 0.01;

use vars '@EXPORT';
@EXPORT = qw(cgiapp_get_query _send_headers);

sub cgiapp_get_query {
    my $self = shift;
    my $apr = Apache::Request->new( Apache->request() );
    return $apr;
}

sub _send_headers {
    my $self = shift;
    my $q = $self->query();
                                                                                       
                                                    
    my $header_type = $self->header_type();
                                                                                       
                                                    
    if ($header_type eq 'redirect') {
        # don't do anything yet... 
        # not sure we can in the future either...
        # just here as a place holder
    } elsif ($header_type eq 'header' ) {
        $self->query->send_http_header('text/html')
    } elsif( $header_type eq 'none' ) {
        # don't do anything here either...
        # geesh, we sure do a lot of nothing around here :)
    } else {
        # croak() if we have an unknown header type
        croak ("Invalid header_type '$header_type'");
    }
                                                                                       
                                                  
    # Don't return anything
    return "";
}

1;

__END__

=pod

=head1 NAME

CGI::Application::Plugin::Apache - Allow CGI::Application to use Apache::* modules 
without interference

=head1 SYNOPSIS

    use base 'CGI::Application';
    use CGI::Application::Plugin::Apache;
    
    # 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
        
        # now we can bake a cookie using Apache::Cookie without interference  
        $cookie = Apache::Cookie->new(
                $q,
                -name       => 'foo',
                -value      => 'bar',
                -expires    => '+2h',
        );
        $cookie->bake;
    }

    1;

=head1 DESCRIPTION

This plugin helps to try and fix some of the annoyances of using L<CGI::Application> in
a pure mod_perl environment. L<CGI::Application> assumes that you use L<CGI.pm|CGI>, 
but I wanted
to avoid it's bloat and have access to the performance of the Apache::* modules so 
along
came this plugin. At the current moment it only does two things:

=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.

=head2 cgiapp_get_query()

This overrides CGI:App's method for retrieving the query object. This is the standard 
way
of using something other than CGI.pm so it's no surprise that we use it here. It simply
creates and returns a new L<Apache::Request> object from C<< Apache->request >>.

=head2 _send_headers()

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.
This method will only recognize the following values set by the C<< 
$self->header_type() >>
method:

=over 

=item * header

This means we just call C<< Apache->send_http_header('text/html') >>. This will also 
use any
other headers added by baking cookies, etc

=item * none

The means we don't send any headers and it's up to the user to send them however they 
want.

=item * redirect

This is an option that L<CGI::Application> uses to perform a redirection header from 
the values
set in the C<< $self->header_props() >> method. This means that right now we don't do 
anything
with it since it's so easy to say C<< $self->query->internal_redirect($some_url) >> 
and in
my opinion much cleaner. For more information see L<"Headers and Cookies">.

=head1 CAVEATS

Upon using this module you completely leave behind the world of L<CGI.pm|CGI>. Don't 
look back or
you might turn into a pillar of salt. You will have to look at and read the docs of 
the Apache::* 
modules. But don't worry, they are really easy to use and were designed to mimic the 
interface
of L<CGI.pm|CGI>. This does however mean that somethings don't quite work the way they 
used to
in the L<CGI::Application> world. In particular:


=head2  HTTP Headers

=over

=item * Cookies

HTTP headers (and hence HTTP cookies) are now not created with C<< header_add() >> or 
C<< header_props() >>.
Instead use L<Apache::Cookie> for cookies and anything you want for additional header 
manipulation. 

=item * Redirects 

This also means that the C<< $self->header_type('redirect') >> doesn't currently do 
anything. Instead you 
should use C<< $self->query->internal_redirect($some_url) >> for external redirects 
and use something like

    $self->query->header_out(Location => $some_url);
    $self->query->status(REDIRECT);

=head1 AUTHOR
                                                                                       
                                                    
Michael Peters <[EMAIL PROTECTED]>
                                                                                       
                                                    
=head1 SEE ALSO
                                                                                       
                                                    
=over 8
                                                                                       
                                                    
=item * L<CGI::Application>
                                                                                       
                                                    
=item * L<Apache>

=item * L<Apache::Registry>

=item * L<Apache::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


---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/[EMAIL PROTECTED]/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to