Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: CGI Cookie Problems

by pg (Canon)
on Jan 24, 2003 at 04:38 UTC ( [id://229532]=note: print w/replies, xml ) Need Help??


in reply to CGI Cookie Problems

I am recently working on some cookie related stuff, and made a small tool for myself. maybe it would help you in some way.

This tool captures html packets, and is capable to extra Set-Cookies.

To use it just make your browser point to this script as a proxy: localhost, 8080. This script requires one parameter to specify action, "r" for recording, and "d" for extract cookies. You can add more function, actually I just started with it yesterday, and will add more functions, if you are interested, I can send you a new version next week, or I can post it somewhere on perlmonks.

browser(user agent) <==> script <==> server

use IO::Socket::INET; use strict; my $action = $ARGV[0]; my $old_cookies = {}; my $new_cookies = {}; my $client = new IO::Socket::INET(Proto => "tcp", LocalPort => 8080, Listen => 10, Reuse => 1, Timeout => 2000) || die "failed"; if ($action eq "r") { while (1) { my $c = $client->accept(); my $request = get_request($c); logmsg($request."\n"); my $response = get_response($request); logmsg($response."\n"); print $c $response; close($c); } } if ($action eq "d") { my $request_file; open($request_file, "<", "result"); while (<$request_file>) { get_cookie($_, $old_cookies); } close($request_file); } sub get_response { my ($request, $new_cookies) = @_; my $s = new IO::Socket::INET(Proto => "tcp", PeerPort => 8080, PeerAddr => "yourrealproxy")#set this + to your real proxy || die "failed to connect to ofg\n"; print $s $request; my $response = ""; my $piece; while (1) { sysread($s, $piece, 1000); $response .= $piece; last if ($piece eq ""); }; close($s); my @lines = split(/\r\n\r\n/, $response); @lines = split(/\r\n/, $lines[0]); foreach my $line (@lines) { get_cookie($line, $new_cookies); } return $response; } sub get_request { my $c = shift; my $piece; my $request = ""; do { sysread($c, $piece, 1000); $request .= $piece; } until ($request =~ m/\r\n\r\n$/); return $request; } sub get_request_from_file { my ($request_file, $old_cookies) = @_; my $request = ""; my $found = 0; while (<$request_file>) { if (!$found) { if (m/^GET/) { $found = 1; $request .= $_; } else { get_cookie($_, $old_cookies); } } else { $request .= $_; last if (m/^\r\n$/); } } return $request; } sub logmsg { my $log = shift; my $result_file; open($result_file, ">>", "result"); print $result_file $log; close($result_file); } sub get_cookie { my ($response, $cookie_hash) = @_; $response =~ m/Set-Cookie:\s*(.*?)=(.*?)[;]/i; if ($1 && $2) { print "[$1, $2]\n"; $cookie_hash->{$1} = $2; } }

Replies are listed 'Best First'.
Re: Re: CGI Cookie Problems
by smitz (Chaplain) on Jan 24, 2003 at 09:01 UTC
    If this isn't a candidate for Cool Uses for Perl, I dont what is.

            > if you are interested, I can send you a new version next week, or I can post it somewhere on perlmonks.

    Please do!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://229532]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-16 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found