File Coverage

blib/lib/HTTP/Cookies/Wget.pm
Criterion Covered Total %
statement 3 40 7.5
branch 0 22 0.0
condition 0 13 0.0
subroutine 1 4 25.0
pod 2 2 100.0
total 6 81 7.4


line stmt bran cond sub pod time code
1             package HTTP::Cookies::Wget;
2             $HTTP::Cookies::Wget::VERSION = '0.2602';
3 25     25   195706 use strict;
  25         90  
  25         23262  
4              
5             our @ISA=qw(HTTP::Cookies);
6             require HTTP::Cookies;
7              
8             sub load
9             {
10 0     0 1   my($self, $file) = @_;
11 0   0       $file ||= $self->{'file'} || return;
      0        
12 0           local(*FILE, $_);
13 0           local $/ = "\n"; # make sure we got standard record separator
14 0           my @cookies;
15 0 0         open(FILE, $file) || return;
16              
17             # don't check for a header; not all export programs save one
18              
19 0           my $now = time() - $HTTP::Cookies::EPOCH_OFFSET;
20 0           while (<FILE>) {
21 0 0         next if /^\s*\#/;
22 0 0         next if /^\s*$/;
23 0           tr/\n\r//d;
24 0           my($domain,$bool1,$path,$secure, $expires,$key,$val) = split(/\t/, $_);
25 0           $secure = ($secure eq "TRUE");
26              
27             # Give expired cookies a fake maxage, because we want all cookies
28             # including session cookies
29 0           my $maxage = $expires-$now;
30 0 0         if ($maxage < 0)
31             {
32 0           $maxage = 9999999;
33             }
34             # Tweak the domain for livejournal cookies,
35             # because it puts '.' in front of all of them
36             # whether they need it or not, and this apparently confuses LWP
37 0 0         if ($domain =~ /^\.([-\w]+\.livejournal\.com)$/)
38             {
39 0           $domain = $1;
40             }
41              
42 0           $self->set_cookie(undef,$key,$val,$path,$domain,undef,
43             0,$secure,$maxage, 0);
44             }
45 0           close(FILE);
46 0           1;
47             }
48              
49             sub save
50             {
51 0     0 1   my($self, $file) = @_;
52 0   0       $file ||= $self->{'file'} || return;
      0        
53 0           local(*FILE, $_);
54 0 0         open(FILE, ">$file") || return;
55              
56             # Use old, now broken link to the old cookie spec just in case something
57             # else (not us!) requires the comment block exactly this way.
58 0           print FILE <<EOT;
59             # Wget HTTP Cookie File
60             # http://www.netscape.com/newsref/std/cookie_spec.html
61             # This is a generated file! Do not edit.
62              
63             EOT
64              
65 0           my $now = time - $HTTP::Cookies::EPOCH_OFFSET;
66             $self->scan(sub {
67 0     0     my($version,$key,$val,$path,$domain,$port,
68             $path_spec,$secure,$expires,$discard,$rest) = @_;
69 0 0 0       return if $discard && !$self->{ignore_discard};
70 0 0         $expires = $expires ? $expires - $HTTP::Cookies::EPOCH_OFFSET : 0;
71 0 0         return if $now > $expires;
72 0 0         $secure = $secure ? "TRUE" : "FALSE";
73 0 0         my $bool = $domain =~ /^\./ ? "TRUE" : "FALSE";
74 0           print FILE join("\t", $domain, $bool, $path, $secure, $expires, $key, $val), "\n";
75 0           });
76 0           close(FILE);
77 0           1;
78             }
79              
80             1;
81             __END__
82              
83             =head1 NAME
84              
85             HTTP::Cookies::Wget - access to Wget cookies files
86              
87             =head1 VERSION
88              
89             version 0.2602
90              
91             =head1 SYNOPSIS
92              
93             use LWP;
94             use HTTP::Cookies::Wget;
95             $cookie_jar = HTTP::Cookies::Wget->new(
96             file => "c:/program files/netscape/users/ZombieCharity/cookies.txt",
97             );
98             my $browser = LWP::UserAgent->new;
99             $browser->cookie_jar( $cookie_jar );
100              
101             =head1 DESCRIPTION
102              
103             This is a subclass of C<HTTP::Cookies> that reads (and optionally
104             writes) Wget/Mozilla cookie files.
105              
106             See the documentation for L<HTTP::Cookies>.
107              
108             =head1 CAVEATS
109              
110             Please note that the Wget/Mozilla cookie file format can't store
111             all the information available in the Set-Cookie2 headers, so you will
112             probably lose some information if you save in this format.
113              
114             At time of writing, this module seems to work fine with Mozilla
115             Phoenix/Firebird.
116              
117             =head1 SEE ALSO
118              
119             L<HTTP::Cookies::Netscape>
120              
121             =head1 COPYRIGHT
122              
123             Copyright 2002-2003 Gisle Aas
124              
125             This library is free software; you can redistribute it and/or
126             modify it under the same terms as Perl itself.
127              
128             =cut