blokhead has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a CGI application which acts as a very light-weight proxy to another website. It must relay (after processing) the form and cookie information it receives and return the result of the website. The other website may set new cookies, and those would have to be "translated" back so that the user receives them, but they are set for the domain of my script.

I'm using LWP to do the basic relaying, and it's no problem. I have not yet implemented the relaying of the POST information, but I am not as worried about that -- it shouldn't be so bad. My main concern is that the cookie information needs to travel back in both directions, and all the cookie handling in LWP seems to be done with cookie-jar files. This isn't going to be a good solution for me. I need to detect when a new cookie is set by the other website, and if so, translate and pass it back to the user. I also want to avoid the cooke-jar file because it's running as a CGI script.

Does anyone know of any CGI/cookie relaying tricks? I'm most interested in ways around using a cookie-jar file on the disk, and detecting new cookies.

Thanks,
blokhead

Replies are listed 'Best First'.
Re: Relaying POST queries cookies
by jdporter (Paladin) on Nov 05, 2002 at 20:19 UTC
    Have a look at the guts of the HTTP::Cookies module.

    I suspect you should be able to subclass HTTP::Cookies, and override the save, load, and set_cookie methods.

    save and load could be defined to do nothing, or to access some in-memory structure under your control.

    You'd override set_cookie just so you can intercept calls to it; you'd probably want subsequently to chain to the base class's version of that sub.

Re: Relaying POST queries cookies
by sauoq (Abbot) on Nov 05, 2002 at 21:53 UTC
    I'm most interested in ways around using a cookie-jar file on the disk, and detecting new cookies.

    I'm pretty sure HTTP::Cookies doesn't require the use of a file. Just don't specify one and it won't use one.

    LWP::UserAgent doesn't really care a whole lot about what you use as a cookie jar. The only requirements are that the object has two methods:

    1. extract_cookies() must take an HTTP::Request object.
    2. add_cookie_header() must take an HTTP::Response object.
    So, one way to do this would be to write a simple wrapper class around HTTP::Cookies that performed the translation. I would prefer this approach to subclassing HTTP::Cookies as was suggested by jdporter. It wouldn't require you to dig into the guts, just the docs. ;-)

    -sauoq
    "My two cents aren't worth a dime.";