| 1 | package HTTP::Tiny::FileProtocol; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 9 | $HTTP::Tiny::FileProtocol::VERSION = '0.07'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Add support for file:// protocol to HTTP::Tiny | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use File::Copy; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use File::stat; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use Fcntl qw(S_IRUSR); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 9 | ▶ | use LWP::MediaTypes; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 9 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 9 | my $orig = *HTTP::Tiny::get{CODE}; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 9 | my $orig_mirror = *HTTP::Tiny::mirror{CODE}; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 12 | ▶ | *HTTP::Tiny::get = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 12 | my ($self, $url, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 12 | ▶ | @_ == 2 || (@_ == 3 && ref $args eq 'HASH') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
@_ == 2 or @_ == 3 and ref $args eq "HASH"
@_ == 3 and ref $args eq "HASH"
MC/DC100%3/3
@_ == 2 or @_ == 3 and ref $args eq "HASH"
@_ == 2
@_ == 3
ref $args eq "HASH"
Truth table100%4/4
A or B and C
A= @_ == 2B= @_ == 3C= ref $args eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | or croak(q/Usage: $http->get(URL, [HASHREF])/ . "\n"); | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 9 | ▶ | if ( $url !~ m{\Afile://} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | return $self->$orig( $url, $args || {}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args || {}
MC/DC100%1/1
$args || {}
$args
Truth table100%2/2
A
A= $args
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 7 | my $success; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 7 | my $status = 599; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 7 | my $reason = 'Internal Exception'; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 7 | my $content = ''; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 7 | my $content_type = 'text/plain'; | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 7 | (my $path = $url) =~ s{\Afile://}{}; | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 7 | my $st = stat $path; | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 7 | ▶ | if ( !$st ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | $status = 404; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 2 | $reason = 'File Not Found'; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 2 | return _build_response( $url, $success, $status, $reason, $content, $content_type ); | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | elsif ( not $st->cando( S_IRUSR ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | $status = 403; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | $reason = 'Permission Denied'; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | return _build_response( $url, $success, $status, $reason, $content, $content_type ); | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$filesize, | |||||||||||||||||||||||||||||||||||||||||
| 53 | $atime,$mtime,$ctime,$blksize,$blocks) | ||||||||||||||||||||||||||||||||||||||||||
| 54 | = stat($path); | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 4 | $status = 200; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | $success = 1; | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | { | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 4 | open my $fh, '<', $path; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 4 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 4 | binmode $fh; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 4 | $content = <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 4 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 4 | $content_type = LWP::MediaTypes::guess_media_type( $path ); | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 4 | return _build_response( $url, $success, $status, $reason, $content, $content_type ); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 9 | }; | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 8 | ▶ | *HTTP::Tiny::mirror = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 8 | my ($self, $url, $file, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 8 | ▶ | @_ == 3 || (@_ == 4 && ref $args eq 'HASH') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
@_ == 3 or @_ == 4 and ref $args eq "HASH"
@_ == 4 and ref $args eq "HASH"
MC/DC100%3/3
@_ == 3 or @_ == 4 and ref $args eq "HASH"
@_ == 3
@_ == 4
ref $args eq "HASH"
Truth table100%4/4
A or B and C
A= @_ == 3B= @_ == 4C= ref $args eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | or croak(q/Usage: $http->mirror(URL, FILE, [HASHREF])/ . "\n"); | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 5 | ▶ | if ( $url !~ m{\Afile://} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | ▶ | return $self->$orig_mirror( $url, $file, $args || {}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args || {}
MC/DC100%1/1
$args || {}
$args
Truth table100%2/2
A
A= $args
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | ▶ | my $response = $self->get( $url, $args || {} ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args || {}
MC/DC100%1/1
$args || {}
$args
Truth table100%2/2
A
A= $args
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | ▶ | return $response if !$response->{success}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | (my $path = $url) =~ s{\Afile://}{}; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | copy $path, $file; | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 2 | return $response; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 9 | }; | |||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 10 | ▶ | sub _build_response { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_response: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 10 | my ($url, $success, $status, $reason, $content, $content_type) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 10 | my $bytes; | |||||||||||||||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 9 | ▶ | use bytes; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 10 | $bytes = length $content; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 10 | ▶ | my $response = { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%4/4
$content // ""
$bytes // 0
MC/DC100%1/1
$content // ""
$content
MC/DC100%1/1
$bytes // 0
$bytes
Truth table100%2/2
A
A= $content
Truth table100%2/2
A
A= $bytes
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | url => $url, | ||||||||||||||||||||||||||||||||||||||||||
| 103 | success => $success, | ||||||||||||||||||||||||||||||||||||||||||
| 104 | status => $status, | ||||||||||||||||||||||||||||||||||||||||||
| 105 | ( !$success ? (reason => $reason) : () ), | ||||||||||||||||||||||||||||||||||||||||||
| 106 | content => $content // '', | ||||||||||||||||||||||||||||||||||||||||||
| 107 | headers => { | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 'content-type' => $content_type, | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 'content-length' => $bytes // 0, | ||||||||||||||||||||||||||||||||||||||||||
| 110 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 111 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 10 | return $response; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||||||||||||||||