in reply to Re: Cookies from CGI to LWP and back
in thread Cookies from CGI to LWP and back

Hi,
Thanxs for the response, but I do not understand...
HTTP::Proxy is an LWP based daemon doing LWP requests

I want to transfer a CGI request to an LWP request, cookies and values should be maintained if possible. For the variables this is no problem, but the cookies need to be retrieved out of the CGI object and stored in an HTTP::Cookie object. I do not find an easy way to get them out of the CGI and store them in this HTTP::Cookie object.
The reverse process should also be possible.

my $ua = LWP::UserAgent->new;
my $cookie_jar = new HTTP::Cookies;
my %cookies = fetch CGI::Cookie;
$cookie_jar->extract_cookies(%cookies);
$ua->cookie_jar($cookie_jar);
my $response = $ua->get(http://someserver/somepage.cgi);

The above does not work.

Any idea?????

greetings
Tom
  • Comment on Re: Re: Cookies from CGI to LWP and back

Replies are listed 'Best First'.
Re: Re: Re: Cookies from CGI to LWP and back
by Corion (Patriarch) on Apr 14, 2004 at 15:12 UTC

    You don't tell me in what way it does not work. My mindreading capabilities are limited in range, and I'm currently busy influencing important worldwide decisions, so please excuse me that I don't snoop through your brain to find out the exact "not working" you mean.

    The HTTP::Cookies documentation tells me that you should use its extract_cookies method on the text of the response and not on some hash you retrieved elsewhere.

    So you will either have to craft a HTTP::Response out of your incoming CGI request, or manually transfer each cookie out of the hash you already have:

    foreach $cookie (keys %cookies) { # $cookie_jar->set_cookie($version, $key, $val, $path, $domain, $port, # $path_spec, $secure, $maxage, $discard, \%rest) $cookie_jar->set_cookie(1, $cookie->name,$cookie->value,$cookie->path,$cookie->domain, 80,'',0,$cookie->expires,0,{}); };

    I haven't tested that code, but it comes more or less directly from the documentation of CGI::Cookie and HTTP::Cookie. Also, again, look at how HTTP::Proxy does its thing. It does something quite similar to what you do, if you look closer at it.

      Sorry for my brief error handling on the list, I should provide more input...
      Indead the HTTP::Proxy is what I need but this needs to be implemented in a CGI style. I need to run this scrip on an apache server running CGI. I can not use the HTTP::Proxy for that reason, nor can I use mod_proxy from the apache server. The problem is that the content needs to be modified in the CGI script. So What I try to do (without any luck at thsi moment ;-) is to transform the incomming CGI into a LWP request and change the content of the LWP and send it back to the browser.You can think of this application as a small sceen scrapper, "stealling" pages at an other server and changing this on the fly. All this written in a nice CGI environment.

      I have tried your suggestion in a small script :
      my $ua = LWP::UserAgent->new;
      $ua->agent($q->http('HTTP_USER_AGENT'));

      my $cookie_jar = new HTTP::Cookies();
      my %cookies = fetch CGI::Cookie;
      my $cookie= "";

      foreach $cookie (keys %cookies) {
      $cookie_jar->set_cookie(1,$cookie->name,$cookie->value,$cookie->path,$cookie->domain,80,'',0,$cookie->expires,0,{});
      };

      This gives the error ....
      Premature end of script headers: /usr/local/apache/cgi-bin/cook.cgi
      Can't locate object method "name" via package "SID" (perhaps you forgot to load "SID"?)at /usr/local/apache/cgi-bin/cook.cgi line 47.
      I know that SID is the name of one of my cookies, so I must be doing somthing good...
      But, I do not understand why it is not working

      Any suggestions are welcome,
      Thanxs in advance,
      Tom

        Ooops - that's an error in my script. I didn't read the documentation closely enough. The loop should be:

        my $name; foreach $name (keys %cookies){ my $cookie = $cookies{$name}; $cookie_jar->set_cookie(1,$cookie->name,$cookie->value,$cookie->path +,$cookie->domain,80,'',0,$cookie->expires,0,{}); };

        I am still not saying that you should use HTTP::Proxy, but you should still look at how it does its thing and steal the parts that you can use. Even if the whole is not what you need, parts of it may still be of use for you or aid you as a learning tool.

        PS: You might want to read Writeup Formatting Tips to see how you can wrap your code in <CODE> tags so it shows up nicely.