| 1 | package | ||||||||||||||||||||||||||||
| 2 | Pinto::Remote::SelfContained::Request; | ||||||||||||||||||||||||||||
| 3 | # ABSTRACT: request class for HTTP::Tiny | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use List::Util qw(pairgrep); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Path::Tiny qw(path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Pinto::Remote::SelfContained::Types qw(BodyPart Uri Username); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Types::Standard qw(ArrayRef Enum HashRef Maybe Str); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 5 | our $VERSION = '1.999'; # TRIAL VERSION | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | has username => (is => 'ro', isa => Username, required => 1); | |||||||||||||||||||||||||||
| 20 | 5 | has password => (is => 'ro', isa => Maybe[Str], required => 1); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 5 | has method => (is => 'ro', isa => Enum[qw(GET POST PUT DELETE)], required => 1); | |||||||||||||||||||||||||||
| 23 | 5 | has uri => (is => 'ro', isa => Uri, coerce => 1, required => 1); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 5 | has accept => (is => 'ro', isa => Maybe[Str], default => 'application/vnd.pinto.v1+text'); | |||||||||||||||||||||||||||
| 26 | 5 | has content_type => (is => 'ro', isa => Maybe[Str], default => 'multipart/form-data'); | |||||||||||||||||||||||||||
| 27 | 5 | ▶ | has headers => (is => 'ro', isa => HashRef[Str], default => sub { +{} }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | has body_parts => (is => 'ro', isa => ArrayRef[BodyPart], default => sub { [] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 5 | ▶ | sub as_request_items { | ||||||||||||||||||||||||||
|
Subroutine
as_request_items: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 5 | my ($self, $data_callback) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 5 | return $self->method, $self->uri, $self->headers_and_body_as_options($data_callback); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 5 | ▶ | sub headers_and_body_as_options { | ||||||||||||||||||||||||||
|
Subroutine
headers_and_body_as_options: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 5 | my ($self, $data_callback) = @_; | |||||||||||||||||||||||||||
| 39 | 5 | my ($boundary, $body) = $self->boundary_and_body; | |||||||||||||||||||||||||||
| 40 | return { | ||||||||||||||||||||||||||||
| 41 | 15 | ▶ | pairgrep { defined $b } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 5 | headers => $self->headers_hash($boundary), | |||||||||||||||||||||||||||
| 43 | content => $body, | ||||||||||||||||||||||||||||
| 44 | data_callback => $data_callback, | ||||||||||||||||||||||||||||
| 45 | }; | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 5 | ▶ | sub headers_hash { | ||||||||||||||||||||||||||
|
Subroutine
headers_hash: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | 5 | my ($self, $boundary) = @_; | |||||||||||||||||||||||||||
| 50 | 5 | my $content_type = $self->content_type; | |||||||||||||||||||||||||||
| 51 | 5 | ▶ | $content_type .= "; boundary=$boundary" if defined $boundary; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 5 | my $authorization_header = $self->authorization_header; | |||||||||||||||||||||||||||
| 53 | return { | ||||||||||||||||||||||||||||
| 54 | 11 | ▶ | pairgrep { defined $b } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 5 | ▶ | %{ $self->headers }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 'Accept' => $self->accept, | ||||||||||||||||||||||||||||
| 57 | 'Content-type' => $content_type, | ||||||||||||||||||||||||||||
| 58 | $authorization_header ? ( Authorization => $authorization_header ) : (), | ||||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 5 | ▶ | sub authorization_header { | ||||||||||||||||||||||||||
|
Subroutine
authorization_header: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 64 | 5 | my $password = $self->password; | |||||||||||||||||||||||||||
| 65 | 5 | ▶ | return unless length $password; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 1 | my $authorization = join ':', $self->username, $password; | |||||||||||||||||||||||||||
| 67 | 1 | my $enc = encode_base64($authorization, ''); | |||||||||||||||||||||||||||
| 68 | 1 | return "Basic $enc"; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 5 | ▶ | sub boundary_and_body { | ||||||||||||||||||||||||||
|
Subroutine
boundary_and_body: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 5 | ▶ | my @parts = map $self->body_part_as_content($_), @{ $self->body_parts } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | or return; | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 5 | my $boundary = 'aAAa'; | |||||||||||||||||||||||||||
| 78 | 5 | ▶ | $boundary++ while grep /\n--\Q$boundary\E(?:--)?\r?\n/, @parts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 5 | my $body = join '', ( | |||||||||||||||||||||||||||
| 81 | (map +("--$boundary\r\n", $_), @parts), | ||||||||||||||||||||||||||||
| 82 | "--$boundary--\r\n", | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 5 | return $boundary, $body; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 16 | ▶ | sub body_part_as_content { | ||||||||||||||||||||||||||
|
Subroutine
body_part_as_content: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 89 | 16 | my ($self, $body_part) = @_; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 16 | (my $name_encoded = $body_part->{name}) =~ s/(["\\])/\\$1/g; | |||||||||||||||||||||||||||
| 92 | 16 | my @ret = qq[Content-disposition: form-data; name="$name_encoded"]; | |||||||||||||||||||||||||||
| 93 | 16 | ▶ | if (defined(my $filename = $body_part->{filename})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 1 | (my $filename_encoded = path($filename)->basename) =~ s/(["\\])/\\$1/g; | |||||||||||||||||||||||||||
| 95 | 1 | $ret[0] .= qq[; filename="$filename_encoded"]; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 16 | push @ret, "Content-encoding: $_" for grep defined, $body_part->{encoding}; | |||||||||||||||||||||||||||
| 99 | 16 | push @ret, "Content-type: $_" for grep defined, $body_part->{type}; | |||||||||||||||||||||||||||
| 100 | 16 | push @ret, ''; | |||||||||||||||||||||||||||
| 101 | 16 | ▶ | push @ret, $body_part->{data} // path($body_part->{filename})->slurp_raw; | ||||||||||||||||||||||||||
|
Condition66%2/3
$body_part->{"data"} // path($body_part->{"filename"})->slurp_raw
MC/DC0%0/2
$body_part->{"data"} // path($body_part->{"filename"})->slurp_raw
$body_part->{"data"}
path($body_part->{"filename"})->slurp_raw
Truth table66%2/3
A // B
A= $body_part->{"data"}B= path($body_part->{"filename"})->slurp_raw
| |||||||||||||||||||||||||||||
| 102 | 16 | $_ .= "\r\n" for @ret; | |||||||||||||||||||||||||||
| 103 | 16 | return join '', @ret; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | 0 | my ($self, %opt) = @_; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | my $enc = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 110 | 0 | for (my $s = shift) { | |||||||||||||||||||||||||||
| 111 | 0 | s/([\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\xFF])/sprintf '\\%02x', ord $1/ge; | |||||||||||||||||||||||||||
| 112 | 0 | s/\r/\\r/g; | |||||||||||||||||||||||||||
| 113 | 0 | s/\t/\\t/g; | |||||||||||||||||||||||||||
| 114 | 0 | return $_; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 0 | }; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my ($boundary, $body) = $self->boundary_and_body; | |||||||||||||||||||||||||||
| 119 | 0 | my $headers = $self->headers_hash($boundary); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | my $ret = ''; | |||||||||||||||||||||||||||
| 122 | 0 | $ret .= sprintf "%s %s HTTP/1.0\\r\n", $self->method, $self->uri->path_query; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | for my $h (sort keys %$headers) { | |||||||||||||||||||||||||||
| 125 | 0 | my $v = $enc->($headers->{$h}); | |||||||||||||||||||||||||||
| 126 | 0 | $ret .= sprintf "%s: %s\\r\n", $h, $v; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | $ret .= "\\r\n"; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | if (defined $body) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | $opt{maxlength} //= 512; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opt{"maxlength"} //= 512
MC/DC0%0/1
$opt{"maxlength"} //= 512
$opt{"maxlength"}
Truth table0%0/2
A
A= $opt{"maxlength"}
| |||||||||||||||||||||||||||||
| 133 | substr($body, $opt{maxlength}, length $ret, '') | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if $opt{maxlength} && length($body) > $opt{maxlength}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opt{"maxlength"} and length $body > $opt{"maxlength"}
MC/DC0%0/2
$opt{"maxlength"} and length $body > $opt{"maxlength"}
$opt{"maxlength"}
length $body > $opt{"maxlength"}
Truth table0%0/3
A and B
A= $opt{"maxlength"}B= length $body > $opt{"maxlength"}
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | $ret .= $enc->($body); | |||||||||||||||||||||||||||
| 137 | 0 | $ret .= "\n"; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | print $ret if !defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | return $ret; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 5 | 1; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||