blib/lib/Net/APNS/Simple.pm

filter: Statementstmt 65.3% 64 / 98 Branchbran 20.0% 6 / 30 Conditioncond 15.0% 3 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 83.3% 15 / 18 Podpod 66.6% 2 / 3 totaltotal 49.7% 90 / 181 CC 25 SCAR 40.9
CC
25
Cov
49%
_socket
182.0
notify
20.0
prepare
6.2
CRAP 60.0
↑ Summary Build »
1 package Net::APNS::Simple;
2 3 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Protocol::HTTP2::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use IO::Socket::SSL qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = "0.05";
13  
14 3 has [qw/auth_key key_id team_id bundle_id development/] => (
15 is => 'rw',
16 );
17  
18 3 has [qw/cert_file key_file passwd_cb/] => (
19 is => 'rw',
20 );
21  
22 has [qw/proxy/] => (
23 is => 'rw',
24 default => $ENV{https_proxy},
25 3 );
26  
27 3 has [qw/apns_id apns_expiration apns_collapse_id/] => (
28 is => 'rw',
29 );
30  
31 3 has apns_priority => (
32 is => 'rw',
33 default => 10,
34 );
35  
36 2 ▶sub algorithm {'ES256'}
Subroutine algorithm: called CC=1 SCAR=0.0
Pod undocumented
37  
38 sub _host {
39 3 ▶ my ($self) = @_;
Subroutine _host: called CC=2 SCAR=6.9
40 3 ▶ return 'api.' . ($self->development ? 'sandbox.' : '') . 'push.apple.com'
Branch: 1 branch
BranchTrueFalseTotal
$self->development ? : 1 2 3
41 }
42  
43 2 ▶sub _port {443}
Subroutine _port: called CC=1 SCAR=0.0
44  
45 sub _socket {
46 0 ▶ my ($self) = @_;
Subroutine _socket: not called CC=13 SCAR=52.0
47 0 ▶ if (!$self->{_socket} || !$self->{_socket}->opened){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_socket"} and $self->{"_socket"}->opened) 0 0 0
Condition0%0/3
$self->{"_socket"} and $self->{"_socket"}->opened
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_socket"} and $self->{"_socket"}->opened
$self->{"_socket"} $self->{"_socket"}->opened
Truth table0%0/3
A and B
A= $self->{"_socket"}B= $self->{"_socket"}->opened
ABresult
0X0
100
111
48 0 my %ssl_opts = (
49 # openssl 1.0.1 support only NPN
50 SSL_npn_protocols => ['h2'],
51 # openssl 1.0.2 also have ALPN
52 SSL_alpn_protocols => ['h2'],
53 SSL_version => 'TLSv1_2',
54 );
55 0 for (qw/cert_file key_file passwd_cb/) {
56 0 ▶ $ssl_opts{"SSL_$_"} = $self->{$_} if defined $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$_} 0 0 0
57 }
58  
59 0 my ($host,$port) = ($self->_host, $self->_port);
60  
61 0 my $socket;
62 0 ▶ if ( my $proxy = $self->proxy ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proxy = $self->proxy) { } 0 0 0
63 0 ▶ $proxy =~ s|^http://|| or die "Invalid proxy $proxy - only http proxy is supported!\n";
Branch: 1 branch
BranchTrueFalseTotal
$proxy =~ s[^http://][] or die "Invalid proxy $proxy - only http proxy is supported!\n" 0 0 0
64 0 require Net::HTTP;
65 0 ▶ $socket = Net::HTTP->new(PeerAddr => $proxy) || die $@;
Condition0%0/2
'Net::HTTP'->new('PeerAddr', $proxy) || die($@)
l!l
00
MC/DC0%0/1
'Net::HTTP'->new('PeerAddr', $proxy) || die($@)
'Net::HTTP'->new('PeerAddr', $proxy)
Truth table0%0/2
A
A= 'Net::HTTP'->new('PeerAddr', $proxy)
Aresult
11
00
66 0 $socket->write_request(
67 CONNECT => "$host:$port",
68 Host => "$host:$port",
69 Connection => "Keep-Alive",
70 'Proxy-Connection' => "Keep-Alive",
71 );
72 0 my ($code, $mess, %h) = $socket->read_response_headers;
73 0 ▶ $code eq '200' or die "Proxy error: $code $mess";
Branch: 1 branch
BranchTrueFalseTotal
$code eq 200 or die "Proxy error: $code $mess" 0 0 0
74  
75 0 ▶ IO::Socket::SSL->start_SSL(
Branch: 1 branch
BranchTrueFalseTotal
"IO::Socket::SSL"->start_SSL($socket, "SSL_hostname", $host, %ssl_opts) or die $! || $IO::Socket::SSL::SSL_ERROR 0 0 0
Condition0%0/3
$! || $IO::Socket::SSL::SSL_ERROR
l!l&&r!l&&!r
000
MC/DC0%0/2
$! || $IO::Socket::SSL::SSL_ERROR
$! $IO::Socket::SSL::SSL_ERROR
Truth table0%0/3
A || B
A= $!B= $IO::Socket::SSL::SSL_ERROR
ABresult
1X1
011
000
76 $socket,
77 # explicitly set hostname we should use for SNI
78 SSL_hostname => $host,
79 %ssl_opts,
80 ) or die $! || $IO::Socket::SSL::SSL_ERROR;
81 }
82 else {
83 # TLS transport socket
84 0 ▶ $socket = IO::Socket::SSL->new(
Branch: 1 branch
BranchTrueFalseTotal
$socket = "IO::Socket::SSL"->new("PeerHost", $host, "PeerPort", $port, %ssl_opts) or die $! || $IO::Socket::SSL::SSL_ERROR 0 0 0
Condition0%0/3
$! || $IO::Socket::SSL::SSL_ERROR
l!l&&r!l&&!r
000
MC/DC0%0/2
$! || $IO::Socket::SSL::SSL_ERROR
$! $IO::Socket::SSL::SSL_ERROR
Truth table0%0/3
A || B
A= $!B= $IO::Socket::SSL::SSL_ERROR
ABresult
1X1
011
000
85 PeerHost => $host,
86 PeerPort => $port,
87 %ssl_opts,
88 ) or die $! || $IO::Socket::SSL::SSL_ERROR;
89 }
90 0 $self->{_socket} = $socket;
91  
92 # non blocking
93 0 $self->{_socket}->blocking(0);
94 }
95 0 return $self->{_socket};
96 }
97  
98 sub _client {
99 1 ▶ my ($self) = @_;
Subroutine _client: called CC=2 SCAR=7.6
100 1 ▶ $self->{_client} ||= Protocol::HTTP2::Client->new(keepalive => 1);
Condition33%1/3
$self->{"_client"} ||= "Protocol::HTTP2::Client"->new("keepalive", 1)
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"_client"} ||= "Protocol::HTTP2::Client"->new("keepalive", 1)
$self->{"_client"} "Protocol::HTTP2::Client"->new("keepalive", 1)
Truth table33%1/3
A ||= B
A= $self->{"_client"}B= "Protocol::HTTP2::Client"->new("keepalive", 1)
ABresult
1X1
011
000
101 1 return $self->{_client};
102 }
103  
104 sub prepare {
105 1 ▶ my ($self, $device_token, $payload, $cb) = @_;
Subroutine prepare: called CC=6 SCAR=18.2
Pod documented
106 1 my @headers = (
107 'apns-topic' => $self->bundle_id,
108 );
109  
110 1 for (qw/apns_id apns_priority apns_expiration apns_collapse_id/) {
111 4 my $v = $self->$_;
112 4 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 2 2 4
113 2 my $k = $_;
114 2 $k =~ s/_/-/g;
115 2 push @headers, $k => $v;
116 }
117  
118 1 ▶ if ($self->team_id and $self->auth_key and $self->key_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->team_id and $self->auth_key and $self->key_id) 1 0 1
Condition33%2/6
$self->team_id and $self->auth_key and $self->key_id
!ll&&!rl&&r
001
$self->team_id and $self->auth_key
!ll&&!rl&&r
001
MC/DC0%0/3
$self->team_id and $self->auth_key and $self->key_id
$self->team_id $self->auth_key $self->key_id
Truth table25%1/4
A and B and C
A= $self->team_idB= $self->auth_keyC= $self->key_id
ABCresult
0XX0
10X0
1100
1111
119 1 require Crypt::PK::ECC;
120 # require for treat pkcs#8 private key
121 1 Crypt::PK::ECC->VERSION(0.059);
122 1 require Crypt::JWT;
123 1 my $claims = {
124 iss => $self->team_id,
125 iat => time,
126 };
127 1 my $jwt = Crypt::JWT::encode_jwt(
128 payload => $claims,
129 key => [$self->auth_key],
130 alg => $self->algorithm,
131 extra_headers => {
132 kid => $self->key_id,
133 },
134 );
135 1 push @headers, authorization => sprintf('bearer %s', $jwt);
136 }
137 1 my $path = sprintf '/3/device/%s', $device_token;
138 1 push @{$self->{_request}}, {
139 ':scheme' => 'https',
140 ':authority' => join(":", $self->_host, $self->_port),
141 ':path' => $path,
142 ':method' => 'POST',
143 headers => \@headers,
144 data => JSON::encode_json($payload),
145 on_done => $cb,
146 };
147 1 return $self;
148 }
149  
150 sub _make_client_request_single {
151 1 ▶ my ($self) = @_;
Subroutine _make_client_request_single: called CC=2 SCAR=9.2
152 1 ▶ if (my $req = shift @{$self->{_request}}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $req = shift @{$$self{'_request'};}) { } 1 0 1
153 1 my $done_cb = delete $req->{on_done};
154 $self->_client->request(
155 %$req,
156 on_done => sub {
157 0 ▶ ref $done_cb eq 'CODE'
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref $done_cb eq "CODE" and &$done_cb(@_) 0 0 0
158 and $done_cb->(@_);
159 0 $self->_make_client_request_single();
160 },
161 1 );
162 }
163 else {
164 0 $self->_client->close;
165 }
166 }
167  
168 sub notify {
169 0 ▶ my ($self) = @_;
Subroutine notify: not called CC=4 SCAR=30.0
Pod documented
170 # request one by one as APNS server returns SETTINGS_MAX_CONCURRENT_STREAMS = 1
171 0 $self->_make_client_request_single();
172 0 my $io = IO::Select->new($self->_socket);
173 # send/recv frames until request is done
174 0 ▶ while ( !$self->_client->shutdown ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_client->shutdown) 0 0 0
175 0 $io->can_write;
176 0 ▶ while ( my $frame = $self->_client->next_frame ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $frame = $self->_client->next_frame) 0 0 0
177 0 syswrite $self->_socket, $frame;
178 }
179 0 $io->can_read;
180 0 ▶ while ( sysread $self->_socket, my $data, 4096 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread $self->_socket, my $data, 4096) 0 0 0
181 0 $self->_client->feed($data);
182 }
183 }
184 0 undef $self->{_client};
185 0 $self->_socket->close(SSL_ctx_free => 1);
186 }
187  
188 3 1;
189 __END__
↑ Summary Build »