Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

mod_perl cookies and CGI::Apache2::Wrapper

by amasidlover (Sexton)
on Dec 25, 2009 at 11:25 UTC ( #814345=perlquestion: print w/replies, xml ) Need Help??
amasidlover has asked for the wisdom of the Perl Monks concerning the following question:

I am completely baffled by cookies in mod_perl 2.0, I'm passing my Session Object a reference to either a CGI object or a CGI::Apache2::Wrapper object - my hope was that I could then treat them more or less the same in my Session object, however I simply cannot get the headers to include a cookie in mod_perl mode.

I originally tried using CGI::Cookie and that obviously didn't work I then tried using CGI::Apache2::Wrapper::Cookie->new to create a cookie object and then $self->cookie->bake() to make it set the header - I then used $self->cgi->send_http_header( $args{type} || '' ); to set the headers. The content type is XML so the headers seem to be getting set correctly - however no cookie...

I then tried using the CGI object's cookie method to create a cookie, which, as far as I can work out, should be syntatically identical for CGI and CGI::Apache2::Wrapper - however this then fails to set a cookie in either mode with either send_http_header or just cgi->header().

There seem to be a large number of ways of doing cookies in mod_perl2 and none of the examples seem to be complete (i.e. including how to then generate headers) or the ones that are complete just don't seem to work for me. I've now spent the last 2 hours trying various different styles out and have only succeeded in breaking cookies in the previously working non-mod_perl version.

Ideally I'd like code that didn't need to do $self->cgi->isa('CGI::Apache2::Wrapper') and then do something different for mod_perl as it seems to defeat the purpose!

My (relevant) code at the moment is:

sub set_client_side_cookie { my $self = shift; debug( "CGI is " . ref( $self->cgi ) ); if ( $self->cgi->isa('CGI::Apache2::Wrapper') ) { debug("Using mod_perl Cookie"); $self->{cookie} = CGI::Apache2::Wrapper::Cookie->new( $self->r, -name => $self->cookie_name, -value => $self->cookie_value, -expires => $self->cookie_expiry, -domain => $self->config->sys_opt('Cookie Domain') || $se +lf->server_name, ); } else { debug("Using CGI cookie"); $self->{cookie} = $self->cgi->cookie( -name => $self->cookie_name, -value => $self->cookie_value, -expires => $self->cookie_expiry, -domain => $self->config->sys_opt('Cookie Domain') || $se +lf->server_name, ); } } sub header { my $self = shift; my %args = @_; if ( $self->cgi->isa('CGI::Apache2::Wrapper') ) { $self->cookie->bake; $self->cgi->send_http_header( $args{type} || '' ); } else { return $self->cgi->header( -type => ( $args{type} || 'text/xml' ), -cookie => $self->cookie, ); } }

Where $self->cgi is a CGI or CGI::Apache2::Wrapper object and $self->r is an Apache::Request object.

My suspicion is that I'm not actually sending the headers and the content type is just co-incidentally being correctly detected...

Thanks in advance! Alex

Replies are listed 'Best First'.
Re: mod_perl cookies and CGI::Apache2::Wrapper
by amasidlover (Sexton) on Dec 27, 2009 at 13:57 UTC
    Turns out that I had a few issues - the first one being that my headers weren't actually being sent from my header method at all in mod_perl mode. However once that was fixed I found the following solution: 1) header takes a hashref of _correctly_ named headers i.e. instead of -type and -cookie it needs Content-Type and Set-Cookie and bake() isn't needed at all i.e.
    $self->cgi->header( { 'Content-Type' => ( $args{type} || 'text/xml' ), 'Set-Cookie' => $self->cookie, } );
    Once that was changed my original calls to cgi->cookie didn't need any changes for mod_perl mode.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://814345]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2016-09-26 19:33 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (489 votes). Check out past polls.