Cookies/Netscape.pm

filter: Statementstmt 95.5% 43 / 45 Branchbran 76.9% 20 / 26 Conditioncond 70.0% 7 / 10 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 4 / 4 Podpod 100% 2 / 2 totaltotal 83.7% 77 / 92 CC 18 SCAR 19.1
CC
18
Cov
86%
load
8.1
__ANON__
7.1
save
5.1
CRAP 6.7
« Cookies.pm ↑ Summary
1 package HTTP::Cookies::Netscape;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 our $VERSION = '6.11';
6  
7 6 require HTTP::Cookies;
8 6 our @ISA=qw(HTTP::Cookies);
9  
10 sub load
11 {
12 6 ▶ my ($self, $file) = @_;
Subroutine load: called CC=8 SCAR=20.9
Pod documented
13 6 ▶ $file ||= $self->{'file'} || return;
Condition80%4/5
$file ||= $self->{"file"} || (return)
l!l&&r!l&&!r
150
$self->{'file'} || (return)
l!l
32
MC/DC0%0/2
$file ||= $self->{"file"} || (return)
$file $self->{'file'}
Truth table66%2/3
A ||= B
A= $fileB= $self->{'file'}
ABresult
1X1
011
000
14  
15 4 local $/ = "\n"; # make sure we got standard record separator
16 4 ▶ open (my $fh, '<', $file) || return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return 1 3 4
17 3 my $magic = <$fh>;
18 3 chomp $magic;
19 3 ▶ unless ($magic =~ /^#(?: Netscape)? HTTP Cookie File/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($magic =~ /^#(?: Netscape)? HTTP Cookie File/) 0 3 3
20 0 warn "$file does not look like a netscape cookies file";
21 0 return;
22 }
23  
24 3 my $now = time() - $HTTP::Cookies::EPOCH_OFFSET;
25 3 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 24 3 27
26 24 chomp($line);
27 24 $line =~ s/\s*\#HttpOnly_//;
28 24 ▶ next if $line =~ /^\s*\#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*\#/ 12 12 24
29 12 ▶ next if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 6 6 12
30 6 $line =~ tr/\n\r//d;
31 6 my($domain,$bool1,$path,$secure, $expires,$key,$val) = split(/\t/, $line);
32 6 $secure = ($secure eq "TRUE");
33 6 $self->set_cookie(undef, $key, $val, $path, $domain, undef, 0, $secure, $expires-$now, 0);
34 }
35 3 1;
36 }
37  
38 sub save
39 {
40 2 ▶ my $self = shift;
Subroutine save: called CC=5 SCAR=16.4
Pod documented
41 my %args = (
42 file => $self->{'file'},
43 2 ▶ ignore_discard => $self->{'ignore_discard'},
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 2 2
44 @_ == 1 ? ( file => $_[0] ) : @_
45 );
46 2 ▶ Carp::croak('Unexpected argument to save method') if keys %args > 2;
Branch: 1 branch
BranchTrueFalseTotal
if keys %args > 2 0 2 2
47 2 ▶ my $file = $args{'file'} || return;
Condition50%1/2
$args{'file'} || (return)
l!l
20
MC/DC0%0/1
$args{'file'} || (return)
$args{'file'}
Truth table50%1/2
A
A= $args{'file'}
Aresult
11
00
48  
49 2 ▶ open(my $fh, '>', $file) || return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or return 0 2 2
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 print {$fh} <<EOT;
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 my $now = time - $HTTP::Cookies::EPOCH_OFFSET;
61 $self->scan(sub {
62 4 ▶ my ($version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard, $rest) = @_;
Subroutine __ANON__: called CC=7 SCAR=19.7
63 4 ▶ return if $discard && !$args{'ignore_discard'};
Branch: 1 branch
BranchTrueFalseTotal
if $discard and not $args{"ignore_discard"} 1 3 4
Condition66%2/3
$discard and not $args{"ignore_discard"}
!ll&&!rl&&r
301
MC/DC50%1/2
$discard and not $args{"ignore_discard"}
$discard not $args{"ignore_discard"}
Truth table66%2/3
A and not(B)
A= $discardB= not $args{"ignore_discard"}
ABresult
0X0
100
111
64 3 ▶ $expires = $expires ? $expires - $HTTP::Cookies::EPOCH_OFFSET : 0;
Branch: 1 branch
BranchTrueFalseTotal
$expires ? : 2 1 3
65 3 ▶ return if $now > $expires;
Branch: 1 branch
BranchTrueFalseTotal
if $now > $expires 1 2 3
66 2 ▶ $secure = $secure ? "TRUE" : "FALSE";
Branch: 1 branch
BranchTrueFalseTotal
$secure ? : 0 2 2
67 2 ▶ my $bool = $domain =~ /^\./ ? "TRUE" : "FALSE";
Branch: 1 branch
BranchTrueFalseTotal
$domain =~ /^\./ ? : 0 2 2
68 2 print {$fh} join("\t", $domain, $bool, $path, $secure, $expires, $key, $val), "\n";
69 2 });
70 2 1;
71 }
72  
73 6 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.11
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<HTTP::Cookies> that reads (and optionally
100 writes) Netscape/Mozilla cookie files.
101  
102 See the documentation for L<HTTP::Cookies>.
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<HTTP::Cookies::Microsoft>
116  
117 =head1 AUTHOR
118  
119 Gisle Aas <gisle@activestate.com>
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__
« Cookies.pm ↑ Summary