HTTP/Cookies/MozRepl.pm

filter: Statementstmt 41.4% 17 / 41 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 62.5% 5 / 8 Podpod 100% 3 / 3 totaltotal 30.8% 25 / 81 CC 12 SCAR 35.3
CC
12
Cov
27%
load
56.0
set_cookie
42.0
save
2.0
CRAP 34.3
« WWW/Mechanize/Firefox.pm ↑ Summary Firefox/Application.pm »
1 package HTTP::Cookies::MozRepl;
2 80 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 80 ▶use HTTP::Date qw(time2str);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 80 ▶use MozRepl::RemoteObject 'as_list';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 80 ▶use parent 'HTTP::Cookies';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 80 ▶use Carp qw[croak];
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 80 our $VERSION = '0.80';
9  
10 =head1 NAME
11  
12 HTTP::Cookies::MozRepl - retrieve cookies from a live Firefox instance
13  
14 =head1 SYNOPSIS
15  
16 use HTTP::Cookies::MozRepl;
17 my $cookie_jar = HTTP::Cookies::MozRepl->new();
18 # use just like HTTP::Cookies
19  
20 =head1 DESCRIPTION
21  
22 This package overrides the load() and save() methods of HTTP::Cookies
23 so it can work with a live Firefox instance.
24  
25 Note: To use this module, Firefox must be running and it must
26 have the C<mozrepl> extension installed.
27  
28 See L<HTTP::Cookies>.
29  
30 =head1 Reusing an existing connection
31  
32 If you already have an existing connection to Firefox
33 that you want to reuse, just pass the L<MozRepl::RemoteObject>
34 instance to the cookie jar constructor in the C<repl> parameter:
35  
36 my $cookie_jar = HTTP::Cookies::MozRepl->new(
37 repl => $repl
38 );
39  
40 =cut
41  
42 0 ▶sub load {
Subroutine load: not called CC=7 SCAR=40.3
Pod documented
43 0 my ($self,$repl) = @_;
44 0 ▶ $repl ||= $self->{'file'} || $self->{'repl'} || return;
Condition0%0/8
$repl ||= $self->{"file"} || $self->{"repl"} || (return)
l!l&&r!l&&!r
000
$self->{'file'} || $self->{'repl'} || (return)
l!l
00
$self->{"file"} or $self->{"repl"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$repl ||= $self->{"file"} || $self->{"repl"} || (return)
$repl $self->{"file"} $self->{"repl"}
Truth table0%0/4
A ||= B or C
A= $replB= $self->{"file"}C= $self->{"repl"}
ABCresult
1XX1
01X1
0011
0000
45
46 # Get cookie manager
47 0 my $cookie_manager = $repl->expr(<<'JS');
48 Components.classes["@mozilla.org/cookiemanager;1"]
49 .getService(Components.interfaces.nsICookieManager)
50 JS
51  
52 0 my $nsICookie = $repl->expr(<<'JS');
53 Components.interfaces.nsICookie
54 JS
55  
56 0 my $nsICookieManager = $repl->expr(<<'JS');
57 Components.interfaces.nsICookieManager
58 JS
59 0 $cookie_manager = $cookie_manager->QueryInterface($nsICookieManager);
60
61 0 my $e = $cookie_manager->{enumerator};
62 0 my $fetch_cookies = $e->bridge->declare(<<'JS', 'list');
63 function(e,nsiCookie) {
64 var r=[];
65 while (e.hasMoreElements()) {
66 var cookie = e.getNext().QueryInterface(nsiCookie);
67 r.push([1,cookie.name,cookie.value,cookie.path,cookie.host,null,null,cookie.isSecure,cookie.expires]);
68 };
69 return r
70 };
71 JS
72 # This could be even more efficient by fetching the whole result
73 # as one huge data structure
74 0 for my $c ($fetch_cookies->($e,$nsICookie)) {
75 0 my @v = as_list $c;
76 0 ▶ if( $v[8] > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v[8] > 0) { } 0 0 0
elsif ($v[8] == 0) { } 0 0 0
77 0 $v[8] -= time;
78 } elsif( $v[8] == 0 ) {
79 # session cookie, we never let it expire within HTTP::Cookie
80 0 $v[8] += 3600; # well, "never"
81 };
82 0 $self->SUPER::set_cookie(@v);
83 };
84  
85 # This code is pure Perl, but involves far too many roundtrips :-(
86 #$e->bridge->queued(sub{
87 # while ($e->hasMoreElements) {
88 # my $cookie = $e->getNext()->QueryInterface($nsICookie);
89 #
90 # my @values = map { $cookie->{$_} } (qw(name value path host isSecure expires));
91 # $self->set_cookie( undef, @values[0, 1, 2, 3, ], undef, undef, $values[ 4 ], time-$values[5], 0 );
92 # };
93 #});
94 }
95  
96 0 ▶sub set_cookie {
Subroutine set_cookie: not called CC=6 SCAR=37.4
Pod documented
97 0 my ($self, $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard, $rest ) = @_;
98 0 ▶ $rest ||= {};
Condition0%0/2
$rest ||= {}
l!l
00
MC/DC0%0/1
$rest ||= {}
$rest
Truth table0%0/2
A
A= $rest
Aresult
11
00
99 0 ▶ my $repl = $rest->{repl} || $self->{repl};
Condition0%0/3
$rest->{'repl'} || $self->{'repl'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$rest->{'repl'} || $self->{'repl'}
$rest->{'repl'} $self->{'repl'}
Truth table0%0/3
A || B
A= $rest->{'repl'}B= $self->{'repl'}
ABresult
1X1
011
000
100
101 0 my $uri = URI->new("http://$domain",'http');
102 #$uri->protocol('http'); # ???
103 0 $uri->host($domain);
104 0 ▶ $uri->path($path) if $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path 0 0 0
105 0 ▶ $uri->port($port) if $port;
Branch: 1 branch
BranchTrueFalseTotal
if $port 0 0 0
106
107 0 my $set = $repl->declare(<<'JS');
108 function (host,path,name,value,secure,httponly,session,expiry) {
109 var cookieMgr = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager2);
110  
111 cookieMgr.add(host,path,name,value,secure,httponly,session,expiry);
112 };
113 JS
114 0 ▶ $set->($uri->host, $uri->path, $key, $val, 0, 0, 0, $maxage ? time+$maxage : 0);
Branch: 1 branch
BranchTrueFalseTotal
$maxage ? : 0 0 0
115 };
116  
117 0 ▶sub save {
Subroutine save: not called CC=1 SCAR=6.9
Pod documented
118 0 croak 'save is not yet implemented'
119 }
120  
121 80 1;
122  
123 __END__
« WWW/Mechanize/Firefox.pm ↑ Summary Firefox/Application.pm »