File Coverage

blib/lib/HTTP/Cookies/Netscape.pm
Criterion Covered Total %
statement 38 40 95.0
branch 18 24 75.0
condition 7 10 70.0
subroutine 4 4 100.0
pod 2 2 100.0
total 69 80 86.2


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