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