File Coverage

blib/lib/HTTP/Cookies/Netscape.pm
Criterion Covered Total %
statement 39 41 95.1
branch 18 24 75.0
condition 7 10 70.0
subroutine 4 4 100.0
pod 2 2 100.0
total 70 81 86.4


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