WebSource/Fetcher.pm

filter: Statementstmt 19.7% 19 / 96 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 54.5% 6 / 11 Podpod 33.3% 1 / 3 totaltotal 15.4% 26 / 168 CC 25 SCAR 47.3
CC
25
Cov
13%
handle
240.0
_init_
42.0
makeRequest
20.0
CRAP 113.6
« WebSource.pm ↑ Summary WebSource/Filter/tests.pm »
1 package WebSource::Fetcher;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use WebSource::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶eval "use Net::INET6Glue::INET_is_INET6"; # use Net::INET6Glue::INET_is_INET6 when available
Subroutine BEGIN: called CC=1 SCAR=1.2
9  
10 1 our @ISA = ('WebSource::Module');
11  
12 =head1 NAME
13  
14 WebSource::Fetcher : fetching module
15 When run downloads given urls and returns the corresponding http response
16
17 =head1 DESCRIPTION
18  
19 A fetch operator is declared with the following format :
20  
21 <ws:fetch name="opname" forward-to="ops" />
22  
23  
24 =head1 SYNOPSIS
25  
26 $fetcher = WebSource::Fetcher->new(wsnode => $node);
27  
28 # for the rest it works as a WebSource::Module
29  
30 =head1 METHODS
31  
32 =over 2
33  
34 =item B<< $source = WebSource->new(desc => $node); >>
35  
36 Create a new Fetcher;
37  
38 =cut
39  
40 0 ▶sub _init_ {
Subroutine _init_: not called CC=6 SCAR=37.4
41 0 my $self = shift;
42 0 ▶ $self->{method} or $self->{method} = 'GET';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"method"} or $self->{"method"} = "GET" 0 0 0
43 0 $self->log(6,"Default method set to '",$self->{method},"'");
44 0 $self->SUPER::_init_;
45 $self->{useragent} or $self->{useragent} =
46 0 ▶ LWP::UserAgent->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->{"useragent"} or $self->{"useragent"} = "LWP::UserAgent"->new("agent", "WebSource/1.0", "keep_alive", 1, "timeout", 20, "env_proxy", 0) 0 0 0
47 agent => "WebSource/1.0",
48 keep_alive => 1,
49 timeout => 20,
50 env_proxy => 0,
51 );
52 0 ▶ if($self->{cookies}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cookies'}) { } 0 0 0
53 0 $self->log(5,"Got cookie jar : ",$self->{cookies});
54 } else {
55 0 $self->log(5,"Creating new cookie jar");
56 0 $self->{cookies} = HTTP::Cookies->new;
57 }
58 0 ▶ $self->{maxreqinterval} or $self->{maxreqinterval} = 3;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"maxreqinterval"} or $self->{"maxreqinterval"} = 3 0 0 0
59 0 ▶ $self->{maxtries} or $self->{maxtries} = 3;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"maxtries"} or $self->{"maxtries"} = 3 0 0 0
60 }
61  
62 0 ▶sub makeRequest {
Subroutine makeRequest: not called CC=4 SCAR=30.0
Pod undocumented
63 0 my $self = shift;
64 0 my $env = shift;
65 0 ▶ if($env->type eq "object/http-request") {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->type eq "object/http-request") 0 0 0
66 0 return $env->data;
67 }
68 0 my $str = $env->dataString;
69 my $uri = $env->{baseuri} ?
70 0 ▶ URI->new_abs($str,$env->{baseuri}) :
Branch: 1 branch
BranchTrueFalseTotal
$env->{'baseuri'} ? : 0 0 0
71 URI->new($str);
72 0 ▶ if($uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri) 0 0 0
73 0 $self->log(6,"Generating HTTP::Request for $uri with method '",$self->{method},"'");
74 0 return HTTP::Request->new($self->{method},$uri);
75 }
76 0 return undef;
77 }
78  
79 =item B<< $fetcher->handle($env); >>
80  
81 Builds an HTTP::Request from the data in enveloppe, fetches
82 the URI (eventually stores it in a file) and builds
83 the corresponding DOM object
84  
85 =cut
86  
87 0 ▶sub handle {
Subroutine handle: not called CC=15 SCAR=54.8
Pod documented
88 0 my $self = shift;
89 0 my $data = shift;
90 # my $request = $self->makeRequest($data);
91 0 my $request = $data->dataAsHttpRequest;
92 0 ▶ if(!$request) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request) 0 0 0
93 0 $self->log(1,"Couldn't convert to HTTP::Request");
94 0 return ();
95 }
96 0 my $scheme = $request->uri->scheme;
97 0 ▶ if(!($scheme eq "http" || $scheme eq "ftp" || $scheme eq "https" || $scheme eq "file")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scheme eq "http" or $scheme eq "ftp" or $scheme eq "https" or $scheme eq "file") 0 0 0
Condition0%0/9
$scheme eq "http" or $scheme eq "ftp" or $scheme eq "https" or $scheme eq "file"
l!l&&r!l&&!r
000
$scheme eq "http" or $scheme eq "ftp" or $scheme eq "https"
l!l&&r!l&&!r
000
$scheme eq "http" or $scheme eq "ftp"
l!l&&r!l&&!r
000
MC/DC0%0/4
$scheme eq "http" or $scheme eq "ftp" or $scheme eq "https" or $scheme eq "file"
$scheme eq "http" $scheme eq "ftp" $scheme eq "https" $scheme eq "file"
Truth table0%0/5
A or B or C or D
A= $scheme eq "http"B= $scheme eq "ftp"C= $scheme eq "https"D= $scheme eq "file"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
98 0 $self->log(1,"Can't fetch scheme ",$scheme);
99 0 return ();
100 }
101 0 $self->log(5,"Handling request \n",$request->as_string);
102 # $self->{cookies}->add_cookie_header($request);
103 0 $self->log(3, "Posting request\n",
104 "-------------------\n",
105 $request->as_string,
106 "-------------------");
107 0 my $tries = $self->{maxtries};
108 0 ▶ $tries > 0 or $tries = 1;
Branch: 1 branch
BranchTrueFalseTotal
$tries > 0 or $tries = 1 0 0 0
109 0 my $response;
110 0 ▶ while($tries > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tries > 0) 0 0 0
111 0 $self->temporize();
112 0 $self->log(5, "Try ", $self->{maxtries} - $tries + 1, " / ", $self->{maxtries});
113 0 $response = $self->{useragent}->request($request);;
114 0 ▶ $tries = $response->is_success ? 0 : $tries - 1;
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success ? : 0 0 0
115 0 ▶ ($response->code eq "500" && $response->message =~ m/SIGINT/)
Branch: 1 branch
BranchTrueFalseTotal
$response->code eq 500 and $response->message =~ /SIGINT/ and die $response->message 0 0 0
Condition0%0/3
$response->code eq 500 and $response->message =~ /SIGINT/
!ll&&!rl&&r
000
MC/DC0%0/2
$response->code eq 500 and $response->message =~ /SIGINT/
$response->code eq 500 $response->message =~ /SIGINT/
Truth table0%0/3
A and B
A= $response->code eq 500B= $response->message =~ /SIGINT/
ABresult
0X0
100
111
116 and die($response->message);
117 0 $self->log(5, "Response status : ",$response->status_line);
118 }
119 0 ▶ if($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
120 0 $self->log(1, "success");
121 # $self->{cookies}->extract_cookies($response);
122  
123 0 my $base = $response->request->uri;
124 0 my %meta = %$data;
125 $self->log(6,"Meta data is as follows :\n",
126 0 map{ $_ . " => " . $meta{$_} ."\n" } keys(%meta)
127 );
128 0 $self->log(3,$response->headers->as_string());
129 0 ▶ $response->headers->scan(sub { my ($h,$v) = @_; $meta{$h} = $v; });
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 ▶ if($meta{'Content-Type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta{"Content-Type"}) 0 0 0
131 0 $self->log(2,"Parsing Content-Type: ".$meta{'Content-Type'});
132
133 0 ▶ if($meta{'Content-Type'} =~ m/([A-Za-z0-9\/\-]+)(?:;\s+charset=([a-zA-Z0-9\-]+))?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta{"Content-Type"} =~ m[([A-Za-z0-9/\-]+)(?:;\s+charset=([a-zA-Z0-9\-]+))?]) 0 0 0
134 0 $meta{type} = $1;
135 0 $meta{encoding} = $2;
136 }
137 }
138 # $meta{encoding} = $response->content_encoding;
139 # $meta{type} = $response->content_type;
140 0 $meta{baseuri} = $base;
141 0 $meta{data} = $response->content;
142 0 $self->log(2,"Content-Encoding: ".$meta{encoding});
143 0 return WebSource::Envelope->new(%meta);
144 } else {
145 0 ▶ if ($response->{request}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'request'}) { } 0 0 0
146 $self->log(1, "WebSource : couldn't fetch ",
147 $request->uri, " received ",
148 0 $response->{request}->status_line);
149 } else {
150 0 $self->log(1, $response->message);
151 }
152 }
153 0 return ();
154 }
155  
156 0 ▶sub temporize {
Subroutine temporize: not called CC=3 SCAR=24.8
Pod undocumented
157 0 my $self = shift;
158 0 my $sleep = 0;
159  
160 0 ▶ if($self->{lastrequest}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lastrequest"}) 0 0 0
161 0 my $dist = time() - $self->{lastrequest};
162 0 $sleep = $self->{minreqinterval} - $dist;
163 }
164 0 ▶ if ($sleep > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sleep > 0) 0 0 0
165 0 $self->log(3, "WebSource : temporizing : waiting ", $sleep, " seconds");
166 0 sleep($sleep);
167 }
168 0 $self->{lastrequest} = time();
169 }
170  
171 =back
172  
173 =head1 SEE ALSO
174  
175 WebSource::Module
176  
177 =cut
178  
179 1 1;
« WebSource.pm ↑ Summary WebSource/Filter/tests.pm »