| 1 | package Cikl::Smrt::Fetchers::Http; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Mouse; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Cikl::Smrt::Fetcher; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use IO::File; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use LWP::Authen::Basic; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use File::Temp qw/tmpnam/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Cikl::Logging; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Cikl; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | extends 'Cikl::Smrt::Fetcher'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | my $logger = get_logger(); | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | my @__tempfiles; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | END { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
END: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | unlink($_) for(@__tempfiles); | |||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | @__tempfiles = (); | |||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | our $AGENT = 'cikl-smrt/'.$Cikl::VERSION.' (cikl.org)'; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use constant SCHEMES => qw/http https/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | has 'timeout' => ( | |||||||||||||||||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 29 | isa => 'Num', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | default => 300, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | required => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | has 'proxy' => ( | |||||||||||||||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | #isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | required => 0 | ||||||||||||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | has 'verify_tls' => ( | |||||||||||||||||||||||||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | isa => 'Num', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | default => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | required => 0 | ||||||||||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | has 'feed_user' => ( | |||||||||||||||||||||||||||||||||||||||||
| 48 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 49 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 50 | required => 0 | ||||||||||||||||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | has 'feed_password' => ( | |||||||||||||||||||||||||||||||||||||||||
| 54 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 55 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 56 | required => 0 | ||||||||||||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | has 'mirror' => ( | |||||||||||||||||||||||||||||||||||||||||
| 60 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 61 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 62 | required => 0 | ||||||||||||||||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 4 | ▶ | sub schemes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
schemes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 4 | return SCHEMES; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=15 SCAR=54.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my $feedurl = $self->feedurl(); | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | unless($feedurl->scheme =~ /^http/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | die("Incorrect scheme: " . $feedurl->scheme()); | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | # If a proxy server is set in the configuration use LWP::UserAgent | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # since LWPx::ParanoidAgent does not allow the use of proxies | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # We'll assume that the proxy is sane and handles timeouts and redirects and such appropriately. | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # LWPx::ParanoidAgent doesn't work well with Net-HTTP/TLS timeouts just yet | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $ua; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if (env_proxy() || $self->proxy() || $feedurl->scheme() eq 'https') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
env_proxy() or $self->proxy or $feedurl->scheme eq "https"
env_proxy() or $self->proxy
MC/DC0%0/3
env_proxy() or $self->proxy or $feedurl->scheme eq "https"
env_proxy()
$self->proxy
$feedurl->scheme eq "https"
Truth table0%0/4
A or B or C
A= env_proxy()B= $self->proxyC= $feedurl->scheme eq "https"
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | # setup the initial agent | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | require LWP::UserAgent; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | $ua = LWP::UserAgent->new(agent => $AGENT); | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | # pull from env_ | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | $ua->env_proxy(); | |||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | # if we override, specify | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | my $proxy = $self->proxy(); | |||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | $ua->proxy(['http','https','ftp'], $proxy) if($proxy); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 94 | # we use this instead of ::UserAgent, it does better | ||||||||||||||||||||||||||||||||||||||||||
| 95 | # overall timeout checking | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | require LWPx::ParanoidAgent; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | $ua = LWPx::ParanoidAgent->new(agent => $AGENT); | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | $ua->timeout($self->timeout()); | |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | # work-around for what appears to be a threading / race condition | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $ua->max_redirect(0) if($feedurl->scheme() eq 'https'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if(defined($self->verify_tls == 0)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | $ua->ssl_opts(SSL_verify_mode => 'SSL_VERIFY_NONE'); | |||||||||||||||||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $ua->ssl_opts(SSL_verify_mode => 'SSL_VERIFY_PEER'); | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if(defined($self->feed_user)){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my $auth = LWP::Authen::Basic->auth_header($self->feed_user, $self->feed_password); | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $ua->default_header("Authentication" => $auth); | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | # work-around for a bug in LWP::UserAgent | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | delete($ua->{'ssl_opts'}->{'verify_hostname'}); | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my $filename; | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my $is_tempfile = 0; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if(my $mirror = $self->mirror){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $feedurl->path() =~ m/\/([a-zA-Z0-9._-]+)$/; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | $filename = $mirror.'/'.$1; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $filename = tmpnam(); | |||||||||||||||||||||||||||||||||||||||||
| 127 | # Try to ensure that it gets unlinked when the process exits. | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | push(@__tempfiles, $filename); | |||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | $is_tempfile = 1; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | $logger->debug("Saving response to $filename"); | |||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | die($filename.' isn\'t writeable by our user') if(-e $filename && !-w $filename); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $filename and not -w $filename
MC/DC0%0/2
-e $filename and not -w $filename
-e $filename
not -w $filename
Truth table0%0/3
A and not(B)
A= -e $filenameB= not -w $filename
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | my $response = $ua->mirror($feedurl->as_string(), $filename); | |||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | if( $response->is_error()){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | die('failed to get feed: '.$feedurl->as_string()."\n".$response->status_line()); | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | $ua = undef; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | my $fh = IO::File->new($filename, 'r') or die($!); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if ($is_tempfile == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | # This looks strange, but since we still have the filehandle open, it | ||||||||||||||||||||||||||||||||||||||||||
| 145 | # won't really disappear until the handle is closed. | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | unlink($filename); | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | return($fh); | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub env_proxy { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
env_proxy: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | require Encode; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | require Encode::Locale; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | my($k,$v); | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | my $found = 0; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | while(($k, $v) = each %ENV) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | if ($ENV{REQUEST_METHOD}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | # Need to be careful when called in the CGI environment, as | ||||||||||||||||||||||||||||||||||||||||||
| 160 | # the HTTP_PROXY variable is under control of that other guy. | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | next if $k =~ /^HTTP_/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | $k = "HTTP_PROXY" if $k eq "CGI_HTTP_PROXY"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | $k = lc($k); | |||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | next unless $k =~ /^(.*)_proxy$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | $k = $1; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | unless($k eq 'no') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | # Ignore random _proxy variables, allow only valid schemes | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | next unless $k =~ /^$URI::scheme_re\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | # Ignore xxx_proxy variables if xxx isn't a supported protocol | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | next unless LWP::Protocol::implementor($k); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | $found = 1; | |||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | return $found; | |||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||