You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Nenad <ne...@teleweb.at> on 2001/06/16 00:58:14 UTC

Sending Cookies from Access-Handler

Hallo,

After reading the "Writing Apache Modules .."-book,
i took the ticketserver code as the foundation for a new Authtentication Module.
I want Cookies which have expired to silently "refresh".

so as a test i have code similar to this

package Apache::PermanentTicketRenewer
my Counter;
sub handler {
    my $r = shift;

    $Counter += 1;
    my $cookie = CGI::Cookie->new(-name => 'Ticket',
				  -path => '/',
				  -domain => '.my.com',
				  -expires => '+1M',
				  -value => { 'ID' => $Counter }
				 );
		
    $r->warn('setting cookie ',$cookie);    # this shows up in the error_log
    $r->headers_out->add("Set-Cookie" => $cookie);
i also tried
    $r->err_header_out("Set-Cookie" => $cookie);
and
    $r->cgi_header_out("Set-Cookie" => $cookie);
    return OK;
}

and in httpd.conf
<Location /my_protected >
 PerlSendHeader On
 PerlAccessHandler 	Apache::TicketAccess
 #PerlAuthzHandler 	Apache::TicketAccess
...
</Location>


And it doesn't work !
The Browser does not set the new cookie , only the first ever sent cookie is set.
Whats wrong with this ?
As you can see i also tried to use the AutzHandler, but somehow this does not get called at all.

Puzzled,

Nenad

Re: Sending Cookies from Access-Handler

Posted by Rodney Broom <rb...@Desert.NET>.
From: Nenad <ne...@teleweb.at>

>  -expires => '+1M',

What's the expiration date shown in your browswer?


> $r->warn('setting cookie ',$cookie);    # this shows up in the error_log

What does this show in the error log? I'm guessing "setting cookie
Apache::Cookie".


---
Rodney Broom
Programmer: Desert.Net




Re: Sending Cookies from Access-Handler - Fixed

Posted by Rodney Broom <rb...@home.com>.
Something else, I haven't used CGI's cookie handling, but I have used
Apache::Cookie. I know that this can be done from an AuthenHandler and from
a TransHandler.

---
Rodney Broom




Re: Sending Cookies from Access-Handler - Fixed

Posted by Nenad Steric <ne...@teleweb.at>.
Ok, 
i think i should really go to sleep now ...

It was using the wrong domain !
forgot to tell apache that it is called "www.my.com"
as i was sending cookies for ".my.com" ...

strangely under win32 this didn't work even with the right domain...

good night,

Nenad


> 
> Ok,
> i tried the push_handlers with PerlFixupHandler,
> as before this gets called also ,
> i get an entry in my error_log (because i use $r->warn to log)
> and again no cookie is being sent ...
> 
> For the record : i am using apache 1.3.20 and modperl 1.25
> 
> Do i have to do a redirect or something similar so that the browser
> sets the cookie ?
> Or do i have problems with the basic configuration of apache ?
> 
> Nenad Steric wrote:
> >
> > I was wrong, there is no cookie being set at all.
> >
> > So, what happens is that the handler gets called
> > i get an entry in the error_log like
> > "setting cookie Ticket=ID&1; ... expires=Sun, 15-Jul-2001 ..."
> > and No Cookie being sent to the browser.
> >
> > Is it possible to send cookies from the Accesshandler ? or am i wasting my time ?
> > Should i use push_handlers and use another phase of the request ?
> >

Re: Sending Cookies from Access-Handler - Update 2

Posted by Nenad Steric <ne...@teleweb.at>.
Ok,
i tried the push_handlers with PerlFixupHandler,
as before this gets called also , 
i get an entry in my error_log (because i use $r->warn to log)
and again no cookie is being sent ...

For the record : i am using apache 1.3.20 and modperl 1.25

Do i have to do a redirect or something similar so that the browser
sets the cookie ?
Or do i have problems with the basic configuration of apache ?


Nenad Steric wrote:
> 
> I was wrong, there is no cookie being set at all.
> 
> So, what happens is that the handler gets called
> i get an entry in the error_log like
> "setting cookie Ticket=ID&1; ... expires=Sun, 15-Jul-2001 ..."
> and No Cookie being sent to the browser.
> 
> Is it possible to send cookies from the Accesshandler ? or am i wasting my time ?
> Should i use push_handlers and use another phase of the request ?
>

Re: Sending Cookies from Access-Handler - Update

Posted by Nenad Steric <ne...@teleweb.at>.
I was wrong, there is no cookie being set at all.

So, what happens is that the handler gets called 
i get an entry in the error_log like
"setting cookie Ticket=ID&1; ... expires=Sun, 15-Jul-2001 ..."
and No Cookie being sent to the browser.

Is it possible to send cookies from the Accesshandler ? or am i wasting my time ?
Should i use push_handlers and use another phase of the request ?

which my modperl doesn't like, i built it with "EVERYTHING=1" but it doesn't want to call 
other handlers than the AccessHandler .. another Puzzle it seems.


Nenad wrote:
> package Apache::PermanentTicketRenewer
use etc. etc.
> my $Counter;
> sub handler {
>     my $r = shift; 
>     $Counter += 1;
>     my $cookie = CGI::Cookie->new(-name => 'Ticket',
>                                   -path => '/',
>                                   -domain => '.my.com',
>                                   -expires => '+1M',
>                                   -value => { 'ID' => $Counter }
>                                  );
> 
>     $r->warn('setting cookie ',$cookie);    # this shows up in the error_log
>     $r->headers_out->add("Set-Cookie" => $cookie);
> i also tried
>     $r->err_header_out("Set-Cookie" => $cookie);
> and
>     $r->cgi_header_out("Set-Cookie" => $cookie);
>     return OK;
> }
> 
> and in httpd.conf
> <Location /my_protected >
>  PerlSendHeader On
>  PerlAccessHandler      Apache::TicketAccess
>  #PerlAuthzHandler      Apache::TicketAccess
> ...
> </Location>
> 
> And it doesn't work !
> The Browser does not set the new cookie , only the first ever sent cookie is set.
> Whats wrong with this ?
> As you can see i also tried to use the AutzHandler, but somehow this does not get called at all.
>

Re: CGI::Cookie vs Apache::Cookie -- help?

Posted by will trillich <wi...@serensoft.com>.
On Mon, Jun 18, 2001 at 07:18:56AM +0200, Per Einar wrote:
> 
> ----- Original Message -----
> From: "will trillich" <wi...@serensoft.com>
> To: <mo...@apache.org>
> Sent: Monday, June 18, 2001 7:09 AM
> Subject: Re: CGI::Cookie vs Apache::Cookie -- help?

> > what does $cookie->bake do (add set-cookie header) that
> > $r->header_out('set-cookie'=>$cookie) (add set-cookie header)
> > does not?
> >
> > [ and why ain't that mentioned in the manual? ]
> 
> As someone else stated, bake() actually sends a Set-Cookie header, but with
> $cookie->as_string and not just $cookie (which is an instance of
> Apache::Cookie). When you're just sending $cookie you're sending an object
> reference.
> 
> Per Einar Ellefsen
> per.einar@skynet.be

my grok quotient has grown considerably. thanks!

-- 
I figure: if a man's gonna gamble, may as well do it
without plowing.   -- Bama Dillert, "Some Came Running"

will@serensoft.com
http://sourceforge.net/projects/newbiedoc -- we need your brain!
http://www.dontUthink.com/ -- your brain needs us!

Re: CGI::Cookie vs Apache::Cookie -- help?

Posted by Per Einar <pe...@skynet.be>.
----- Original Message -----
From: "will trillich" <wi...@serensoft.com>
To: <mo...@apache.org>
Sent: Monday, June 18, 2001 7:09 AM
Subject: Re: CGI::Cookie vs Apache::Cookie -- help?


> On Sun, Jun 17, 2001 at 11:14:23PM -0400, Chris Winters wrote:
> > * will trillich (will@serensoft.com) [010617 23:04]:
> > > $r->log_error( qq(...id=$ID, sending cookie) );
>
> --this outputs the string i'm hoping for, into the log file.

That's because you're outputting $ID and not the cookie itself, which you're
creating below.

>
> > > my $cookie =
> > > Apache::Cookie->new( $r,
> > > -name   => $cookie_name,
> > > -value  => $ID         ,
> > > -domain => $r->hostname,
> > > -path   => '/'         ,
> > > );
> > > $r->header_out('Set-Cookie', => $cookie);
>
> > From 'perldoc Apache::Cookie'
> >
> >    bake
> >        Put cookie in the oven to bake.  (Add a Set-Cookie
> >        header to the outgoing headers table.)
> >
> >            $cookie->bake;
>
> what does $cookie->bake do (add set-cookie header) that
> $r->header_out('set-cookie'=>$cookie) (add set-cookie header)
> does not?
>
> [ and why ain't that mentioned in the manual? ]

As someone else stated, bake() actually sends a Set-Cookie header, but with
$cookie->as_string and not just $cookie (which is an instance of
Apache::Cookie). When you're just sending $cookie you're sending an object
reference.

Per Einar Ellefsen
per.einar@skynet.be


Re: CGI::Cookie vs Apache::Cookie -- help?

Posted by will trillich <wi...@serensoft.com>.
On Sun, Jun 17, 2001 at 11:14:23PM -0400, Chris Winters wrote:
> * will trillich (will@serensoft.com) [010617 23:04]:
> > 	$r->log_error( qq(...id=$ID, sending cookie) );

--this outputs the string i'm hoping for, into the log file.

> > 	my $cookie =
> > 		Apache::Cookie->new( $r,
> > 			-name   => $cookie_name,
> > 			-value  => $ID         ,
> > 			-domain => $r->hostname,
> > 			-path   => '/'         ,
> > 		);
> > 	$r->header_out('Set-Cookie', => $cookie);

> From 'perldoc Apache::Cookie'
> 
>    bake
>        Put cookie in the oven to bake.  (Add a Set-Cookie
>        header to the outgoing headers table.)
> 
>            $cookie->bake;

what does $cookie->bake do (add set-cookie header) that
$r->header_out('set-cookie'=>$cookie) (add set-cookie header)
does not?

[ and why ain't that mentioned in the manual? ]

-- 
I figure: if a man's gonna gamble, may as well do it
without plowing.   -- Bama Dillert, "Some Came Running"

will@serensoft.com
http://sourceforge.net/projects/newbiedoc -- we need your brain!
http://www.dontUthink.com/ -- your brain needs us!

Re: CGI::Cookie vs Apache::Cookie -- help?

Posted by Chris Winters <ch...@cwinters.com>.
* will trillich (will@serensoft.com) [010617 23:04]:
> mine is similar:
> 
> 	$r->log_error( qq(...id=$ID, sending cookie) );
> 	my $cookie =
> 		Apache::Cookie->new( $r,
> 			-name   => $cookie_name,
> 			-value  => $ID         ,
> 			-domain => $r->hostname,
> 			-path   => '/'         ,
> 		);
> 	$r->header_out('Set-Cookie', => $cookie);
> 
> which sets a cookie value of "SCALAR(0x863c9f8)" instead of the
> $ID string that shows up in the log, which is
> 
> 	...id=483dd0e2202accce6d4d3e07d976bfdc, sending cookie
> 
> in the original ticket example above, -value is a hashref:
> 
> 	-value => { ID => $ID }
> 
> instead of
> 
> 	-value => $ID
> 
> as the Apache::Cookie manpage suggests (a plain scalar value).
> 
> this worked just fine with CGI::Cookie (without the $r, of
> course) -- can't get it to return the actual scalar value with
> Apache::Cookie...
> 
> ideas? help!

>From 'perldoc Apache::Cookie'

   bake
       Put cookie in the oven to bake.  (Add a Set-Cookie
       header to the outgoing headers table.)

           $cookie->bake;

   as_string
       Format the cookie object as a string:

           #same as $cookie->bake
           $r->err_headers_out->add("Set-Cookie" => $cookie->as_string);


HTH

Chris

-- 
Chris Winters (chris@cwinters.com)
Building enterprise-capable snack solutions since 1988.

Re: CGI::Cookie vs Apache::Cookie -- help?

Posted by Cees Hek <ce...@sitesuite.org>.
On Sun, 17 Jun 2001, will trillich wrote:

> 	$r->log_error( qq(...id=$ID, sending cookie) );
> 	my $cookie =
> 		Apache::Cookie->new( $r,
> 			-name   => $cookie_name,
> 			-value  => $ID         ,
> 			-domain => $r->hostname,
> 			-path   => '/'         ,
> 		);
> 	$r->header_out('Set-Cookie', => $cookie);

You forgot to bake your cookie...  Read the Apache::Cookie docs again.

Get rid of the $r->header_out(...) line and use the following instead.

$cookie->bake();

Cees


CGI::Cookie vs Apache::Cookie -- help?

Posted by will trillich <wi...@serensoft.com>.
On Sat, Jun 16, 2001 at 12:58:14AM +0200, Nenad wrote:
> package Apache::PermanentTicketRenewer
> my Counter;
> sub handler {
>     my $r = shift;
> 
>     $Counter += 1;
>     my $cookie = CGI::Cookie->new(-name => 'Ticket',
> 				  -path => '/',
> 				  -domain => '.my.com',
> 				  -expires => '+1M',
> 				  -value => { 'ID' => $Counter }
> 				 );

mine is similar:

	$r->log_error( qq(...id=$ID, sending cookie) );
	my $cookie =
		Apache::Cookie->new( $r,
			-name   => $cookie_name,
			-value  => $ID         ,
			-domain => $r->hostname,
			-path   => '/'         ,
		);
	$r->header_out('Set-Cookie', => $cookie);

which sets a cookie value of "SCALAR(0x863c9f8)" instead of the
$ID string that shows up in the log, which is

	...id=483dd0e2202accce6d4d3e07d976bfdc, sending cookie

in the original ticket example above, -value is a hashref:

	-value => { ID => $ID }

instead of

	-value => $ID

as the Apache::Cookie manpage suggests (a plain scalar value).

this worked just fine with CGI::Cookie (without the $r, of
course) -- can't get it to return the actual scalar value with
Apache::Cookie...

ideas? help!

-- 
I figure: if a man's gonna gamble, may as well do it
without plowing.   -- Bama Dillert, "Some Came Running"

will@serensoft.com
http://sourceforge.net/projects/newbiedoc -- we need your brain!
http://www.dontUthink.com/ -- your brain needs us!

Re: Sending Cookies from Access-Handler

Posted by Mike & Christine <mc...@mirusweb.com>.
----- Original Message -----
From: Nenad <ne...@teleweb.at>
To: <mo...@apache.org>
Sent: Friday, June 15, 2001 4:58 PM
Subject: Sending Cookies from Access-Handler


> Hallo,
>
> After reading the "Writing Apache Modules .."-book,
> i took the ticketserver code as the foundation for a new Authtentication
Module.
> I want Cookies which have expired to silently "refresh".
>
> so as a test i have code similar to this
>
> package Apache::PermanentTicketRenewer
> my Counter;
> sub handler {
>     my $r = shift;
>
>     $Counter += 1;
>     my $cookie = CGI::Cookie->new(-name => 'Ticket',
>   -path => '/',
>   -domain => '.my.com',
>   -expires => '+1M',
>   -value => { 'ID' => $Counter }
> );
>
>     $r->warn('setting cookie ',$cookie);    # this shows up in the
error_log
>     $r->headers_out->add("Set-Cookie" => $cookie);
> i also tried
>     $r->err_header_out("Set-Cookie" => $cookie);
> and
>     $r->cgi_header_out("Set-Cookie" => $cookie);
>     return OK;
> }
>
> and in httpd.conf
> <Location /my_protected >
>  PerlSendHeader On
>  PerlAccessHandler Apache::TicketAccess
>  #PerlAuthzHandler Apache::TicketAccess
> ...
> </Location>
>
>
> And it doesn't work !
> The Browser does not set the new cookie , only the first ever sent cookie
is set.
> Whats wrong with this ?
> As you can see i also tried to use the AutzHandler, but somehow this does
not get called at all.

The reason for that is that there is no 'require'  directive in your
configuration. You need something like
require valid-user
in there for it to call AuthHandlers.
Someone correct me if i am mistaken please.
>
> Puzzled,
>
> Nenad
>