Hi,
I was trying to use Cookies.pm from libwww v5.53 to read some cookies off a web-page.
It didn't work, and I realised that Cookies.pm doesn't support the META-style of
setting cookies in HTML, which is accepted by all major browsers (Netscape, Mozilla,
Opera, and even Internet Exploiter):
<META http-equiv="Set-Cookie" content="hash=KMvJWfpPpqhgOEdU">
So, I hacked (limited) support for this style of cookies in Cookies.pm. The patch is
attached. It works for me but it's probably not the cleanest code ever (even though it
is quite simple). This could probably be useful for more people, so I wonder if this
'feature' could be added to Cookies.pm?
Bye for now,
Ward Vandewege.
--
http://pong.be - Your Belgian virtual hosting partner.
--- /tmp/Cookies.pm Wed Sep 5 20:43:01 2001
+++ Cookies.pm Wed Sep 5 22:06:52 2001
@@ -9,7 +9,7 @@
use LWP::Debug ();
use vars qw($VERSION);
-$VERSION = sprintf("%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/);
+$VERSION = sprintf("%d.%02d", q$Revision: 1.17 $ =~ /(\d+)\.(\d+)/);
my $EPOCH_OFFSET = 0; # difference from Unix epoch
if ($^O eq "MacOS") {
@@ -226,6 +226,29 @@
$request;
}
+=item $cookie_jar->get_cookies_from_html($response->{'content_'});
+
+The get_cookies_from_html is a helper method for the extract_cookies()
+method below. It should never be accessed directly.
+
+This method can extract cookies that are set in html META-style tags.
+Ward Vandewege ([EMAIL PROTECTED]), 2001-09-05
+
+=cut
+
+sub get_cookies_from_html {
+ my $content = shift;
+ my ($head, $junk) = ("","");
+ ($head,$junk) = split(/<\/head>/,$content,2); #Rude way to separate header
+of body
+ my @set = ();
+ return @set if ($head eq "");
+ my $cnt = 0;
+ while ($head =~ m/<META http-equiv=\"?Set-Cookie\"? content=\"?(.*?)\"?>/g) {
+ $set[$cnt++] = $1;
+ LWP::Debug::debug("Cookie found: $1");
+ }
+ return @set;
+}
=item $cookie_jar->extract_cookies($response);
@@ -244,6 +267,9 @@
my $netscape_cookies;
unless (@set) {
@set = $response->_header("Set-Cookie");
+ unless (@set) { #Try to get cookies from META tags in html
+ @set = get_cookies_from_html($response->{'_content'});
+ }
return $response unless @set;
$netscape_cookies++;
}