[Perl] Apache::Session & Mason

Valentin Ortiz Ferretiz vortiz@vera.net
04 Apr 2002 13:46:48 -0600


Primero quiero dale las gracias a Gabriel Lopez y a Rodrigo Gallardo por ha=
ber respondio mi correo anterior.
=BFKe creen?, tengo otra inquietud...=20

Ahora intento usar Apache::Session con Mason, encontre la siguiente liga
en el sitio de Mason que "explica" como hacerle para manejar con mason
las sesiones...=20
http://www.masonhq.com/user/adpacifico/ApacheSessionMason.html

La verdad es que no pude hacer funcionar los ejemplos  :(=20
bueno, no del todo, por que la galleta si era enviada, pero no
almacenaba nada de info. Ocupe el session_handler.pl que viene en la
distribuci=F3n de Mason.


Mi configuracion de Apache quedo asi:

PerlSetVar MasonCompRoot /var/www/html
PerlSetVar MasonDataDir /var/www/mason
PerlModule HTML::Mason::ApacheHandler

PerlRequire     /var/www/tools/session_handler.pl

<Directory "/var/www/html/mason">
  # HTML::Mason handles any .html files
  <FilesMatch "*.html">
     SetHandler perl-script
     PerlHandler HTML::Mason
#     PerlHandler HTML::Mason::ApacheHandler
  </FilesMatch>
  # Deny .comp files from being accessed directly
  <FilesMatch "*.comp">
    SetHandler perl-script
    PerlInitHandler Apache::Constants::NOT_FOUND
  </FilesMatch>
</Directory>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D

y los ejemplos que intente fueron:
 -=3D-=3D-=3D-=3D-=3D- test-store.html -=3D-=3D-=3D-=3D-=3D-
    <HTML><BODY>
<A HREF=3D"test-retrieve.html">Go here</A>
<%perl>
$session{'first_name'} =3D 'your first name';
    $session{'last_name'}  =3D 'your last name';
    </%perl>
</BODY></HTML>
-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=
=3D-

-=3D-=3D-=3D-=3D-=3D- test-retrieve.html -=3D-=3D-=3D-=3D
    <HTML><BODY>
First name: <% $session{first_name} %><BR>
Last name: <% $session{last_name} %>
</BODY></HTML>
-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=
=3D-

y pues ya que estoy en esto, les mando el session_handler.pl
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D sessi=
on_handler.pl =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D
#!/usr/bin/perl
#
# This is a handler that supports a %session global, using cookies, that
# persists across HTTP requests.
#
# *NOTE* There is a bug in perl5.005 that can rear it's head when you
#        die() within an eval {}.  Unfortunately, Apache::Session
#        expects you to catch failures by using eval, so you may get
#        bitten.  You can either hack Apache::Session to change how it
#        returns its error conditions, or upgrade to Perl 5.6.0 or
#        greater, which has its own bugs.

package HTML::Mason;

# Bring in main Mason package.
use HTML::Mason;

# Bring in ApacheHandler, necessary for mod_perl integration.
# Uncomment the second line (and comment the first) to use
# Apache::Request instead of CGI.pm to parse arguments.
# use HTML::Mason::ApacheHandler;
 use HTML::Mason::ApacheHandler (args_method=3D>'mod_perl');

use strict;

# List of modules that you want to use from components (see Admin
# manual for details)
{  package HTML::Mason::Commands;
   use vars qw(%session);
   # You might want to replace this with Apache::Cookie if you have
   # libapreq installed.
   use CGI::Cookie;
   # Replace this if you want to use a different storage method.  Also
   # see Apache::Session::Flex in Apache::Session 1.50+ for a way to
   # specify this at run time.
   use Apache::Session::File 1.50;
}

# Create Mason objects
#
my $parser =3D new HTML::Mason::Parser;
my $interp =3D new HTML::Mason::Interp (parser=3D>$parser,
                                      comp_root=3D>'/var/www/html',
                                      data_dir=3D>'/var/');
my $ah =3D new HTML::Mason::ApacheHandler (interp=3D>$interp);

# Activate the following if running httpd as root (the normal case).
# Resets ownership of all files created by Mason at startup.
#
#chown (Apache->server->uid, Apache->server->gid,
$interp->files_written);


sub handler
{
    my ($r) =3D @_;

    # If you plan to intermix images in the same directory as
    # components, activate the following to prevent Mason from
    # evaluating image files as components.
    #
    #return -1 if $r->content_type && $r->content_type !~ m|^text/|io;

    my %cookies =3D parse CGI::Cookie($r->header_in('Cookie'));

    # Don't even bother trying to use badly formed session ids.
    my $sid;
    if ( defined $cookies{'AF_SID'} && $cookies{'AF_SID'}->value() =3D~
/^[a-z\d]+$/ ) {
	$sid =3D $cookies{'AF_SID'}->value();
    }

    # If $sid is not defined this will generate a new session.
    eval {
	tie %HTML::Mason::Commands::session, 'Apache::Session::File', $sid;
    };

    if ( $@ ) {
	if ( $@ =3D~ /Object does not exist in the data store/ && defined $sid )
{
	    # Attempt to create a new session if the previous one was
	    # not valid.  This attempt will die (leading to a 500
	    # error) if it fails.  Use eval {} to trap this if you so
	    # desire.
	    tie %HTML::Mason::Commands::session, 'Apache::Session::File',
undef;
	} else {
	    # This means that we got a different error or we were
	    # attempting to create a new session from scratch.
	    die $@;
	}
    }

    # Always send the cookie out as there is no reason not to.
    my $cookie =3D new CGI::Cookie(-name=3D>'AF_SID',
-value=3D>$HTML::Mason::Commands::session{_session_id}, -path =3D> '/',);
    $r->header_out('Set-Cookie' =3D> $cookie);

    my $status =3D $ah->handle_request($r);

    untie %HTML::Mason::Commands::session;

    return $status;
}

1;



--=20
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Valentin Ortiz Ferretiz
   Reco S.A. de C.V.
  http://reco.vera.net
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Key fingerprint =3D E2E7 5691 79C9 8B15 CC6B  6DC7 F2F1 DA03 1350 9640