| 1 | package Net::APNs::HTTP2; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = "0.02"; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Crypt::PK::ECC 0.059; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Crypt::JWT; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Cache::Memory::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use AnyEvent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use AnyEvent::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use AnyEvent::Socket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Protocol::HTTP2::Client; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | has [qw/auth_key key_id team_id bundle_id is_development/] => ( | |||||||||||||||||||||||||||
| 20 | is => 'rw', | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | has apns_port => ( | |||||||||||||||||||||||||||
| 24 | is => 'rw', | ||||||||||||||||||||||||||||
| 25 | default => 443, # can use 2197 | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | has on_error => ( | ||||||||||||||||||||||||||||
| 29 | is => 'rw', | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 32 | }, | ||||||||||||||||||||||||||||
| 33 | 1 | ); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub _host { | ||||||||||||||||||||||||||
|
Subroutine
_host: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 36 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | $self->is_development ? 'api.development.push.apple.com' : 'api.push.apple.com'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub _client { | ||||||||||||||||||||||||||
|
Subroutine
_client: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 41 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 42 | 0 | ▶ | $self->{_client} ||= Protocol::HTTP2::Client->new(keepalive => 1); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"_client"} ||= "Protocol::HTTP2::Client"->new("keepalive", 1)
MC/DC0%0/2
$self->{"_client"} ||= "Protocol::HTTP2::Client"->new("keepalive", 1)
$self->{"_client"}
"Protocol::HTTP2::Client"->new("keepalive", 1)
Truth table0%0/3
A ||= B
A= $self->{"_client"}B= "Protocol::HTTP2::Client"->new("keepalive", 1)
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub _handle { | ||||||||||||||||||||||||||
|
Subroutine
_handle: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 46 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | unless ($self->_handle_connected) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | my $handle = AnyEvent::Handle->new( | ||||||||||||||||||||||||||||
| 50 | keepalive => 1, | ||||||||||||||||||||||||||||
| 51 | connect => [ $self->_host, $self->apns_port ], | ||||||||||||||||||||||||||||
| 52 | tls => 'connect', | ||||||||||||||||||||||||||||
| 53 | tls_ctx => { | ||||||||||||||||||||||||||||
| 54 | verify => 1, | ||||||||||||||||||||||||||||
| 55 | verify_peername => 'https', | ||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||
| 57 | autocork => 1, | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | on_error => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 59 | 0 | my ($handle, $fatal, $message) = @_; | |||||||||||||||||||||||||||
| 60 | 0 | $self->on_error->($fatal, $message); | |||||||||||||||||||||||||||
| 61 | 0 | $handle->destroy; | |||||||||||||||||||||||||||
| 62 | 0 | $self->{_condvar}->send; | |||||||||||||||||||||||||||
| 63 | }, | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | on_eof => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | 0 | my $handle = shift; | |||||||||||||||||||||||||||
| 66 | 0 | $self->{_condvar}->send; | |||||||||||||||||||||||||||
| 67 | }, | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | on_read => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 69 | 0 | my $handle = shift; | |||||||||||||||||||||||||||
| 70 | 0 | $self->_client->feed(delete $handle->{rbuf}); | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | while (my $frame = $self->_client->next_frame) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | $handle->push_write($frame); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if ($self->_client->shutdown) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | $handle->push_shutdown; | |||||||||||||||||||||||||||
| 76 | 0 | return; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | unless ($self->_client->{active_streams} > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | $self->{_condvar}->send; | |||||||||||||||||||||||||||
| 81 | 0 | return; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | }, | ||||||||||||||||||||||||||||
| 84 | 0 | ); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | $self->{_handle} = $handle; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | return $self->{_handle}; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub _handle_connected { | ||||||||||||||||||||||||||
|
Subroutine
_handle_connected: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 93 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | my $handle = $self->{_handle}; | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | return if !$handle; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | return if $handle->destroyed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | return 1; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub _provider_authentication_token { | ||||||||||||||||||||||||||
|
Subroutine
_provider_authentication_token: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 102 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $self->{_cache} ||= Cache::Memory::Simple->new; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"_cache"} ||= "Cache::Memory::Simple"->new
MC/DC0%0/2
$self->{"_cache"} ||= "Cache::Memory::Simple"->new
$self->{"_cache"}
"Cache::Memory::Simple"->new
Truth table0%0/3
A ||= B
A= $self->{"_cache"}B= "Cache::Memory::Simple"->new
| |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $self->{_cache}->get_or_set('provider_authentication_token', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 106 | 0 | my $craims = { | |||||||||||||||||||||||||||
| 107 | iss => $self->team_id, | ||||||||||||||||||||||||||||
| 108 | iat => time, | ||||||||||||||||||||||||||||
| 109 | }; | ||||||||||||||||||||||||||||
| 110 | 0 | my $jwt = Crypt::JWT::encode_jwt( | |||||||||||||||||||||||||||
| 111 | payload => $craims, | ||||||||||||||||||||||||||||
| 112 | key => [ $self->auth_key ], | ||||||||||||||||||||||||||||
| 113 | alg => 'ES256', | ||||||||||||||||||||||||||||
| 114 | extra_headers => { kid => $self->key_id }, | ||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||
| 116 | 0 | return $jwt; | |||||||||||||||||||||||||||
| 117 | 0 | }, 60 * 50); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub prepare { | ||||||||||||||||||||||||||
|
Subroutine
prepare: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 0 | my ($self, $device_token, $payload, $cb, $extra_header) = @_; | |||||||||||||||||||||||||||
| 122 | 0 | ▶ | my $apns_expiration = $extra_header->{apns_expiration} || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$extra_header->{'apns_expiration'} || 0
MC/DC0%0/1
$extra_header->{'apns_expiration'} || 0
$extra_header->{'apns_expiration'}
Truth table0%0/2
A
A= $extra_header->{'apns_expiration'}
| |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | my $apns_priority = $extra_header->{apns_priority} || 10; | ||||||||||||||||||||||||||
|
Condition0%0/2
$extra_header->{'apns_priority'} || 10
MC/DC0%0/1
$extra_header->{'apns_priority'} || 10
$extra_header->{'apns_priority'}
Truth table0%0/2
A
A= $extra_header->{'apns_priority'}
| |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | my $apns_topic = $extra_header->{apns_topic} || $self->bundle_id; | ||||||||||||||||||||||||||
|
Condition0%0/3
$extra_header->{'apns_topic'} || $self->bundle_id
MC/DC0%0/2
$extra_header->{'apns_topic'} || $self->bundle_id
$extra_header->{'apns_topic'}
$self->bundle_id
Truth table0%0/3
A || B
A= $extra_header->{'apns_topic'}B= $self->bundle_id
| |||||||||||||||||||||||||||||
| 125 | 0 | my $apns_id = $extra_header->{apns_id}; | |||||||||||||||||||||||||||
| 126 | 0 | my $apns_collapse_id = $extra_header->{apns_collapse_id}; | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | $cb ||= sub {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$cb ||= sub {
}
MC/DC0%0/2
$cb ||= sub {
}
$cb
sub {
}
Truth table0%0/3
A ||= B
A= $cbB= sub {
}
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | my $clinet = $self->_client; | |||||||||||||||||||||||||||
| 130 | 0 | ▶ | $clinet->request( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 131 | ':scheme' => 'https', | ||||||||||||||||||||||||||||
| 132 | ':authority' => join(':', $self->_host, $self->apns_port), | ||||||||||||||||||||||||||||
| 133 | ':path' => sprintf('/3/device/%s', $device_token), | ||||||||||||||||||||||||||||
| 134 | ':method' => 'POST', | ||||||||||||||||||||||||||||
| 135 | headers => [ | ||||||||||||||||||||||||||||
| 136 | 'authorization' => sprintf('bearer %s', $self->_provider_authentication_token), | ||||||||||||||||||||||||||||
| 137 | 'apns-expiration' => $apns_expiration, | ||||||||||||||||||||||||||||
| 138 | 'apns-priority' => $apns_priority, | ||||||||||||||||||||||||||||
| 139 | 'apns-topic' => $apns_topic, | ||||||||||||||||||||||||||||
| 140 | defined $apns_id ? ('apns-id' => $apns_id) : (), | ||||||||||||||||||||||||||||
| 141 | defined $apns_collapse_id ? ('apns-collapse-id' => $apns_collapse_id) : (), | ||||||||||||||||||||||||||||
| 142 | ], | ||||||||||||||||||||||||||||
| 143 | data => JSON::encode_json($payload), | ||||||||||||||||||||||||||||
| 144 | on_done => $cb, | ||||||||||||||||||||||||||||
| 145 | ); | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | return $self; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | sub send { | ||||||||||||||||||||||||||
|
Subroutine
send: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | local $self->{_condvar} = AnyEvent->condvar; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | my $handle = $self->_handle; | |||||||||||||||||||||||||||
| 156 | 0 | my $clinet = $self->_client; | |||||||||||||||||||||||||||
| 157 | 0 | ▶ | while (my $frame = $clinet->next_frame) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | $handle->push_write($frame); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | $self->{_condvar}->recv; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | return 1; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 167 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 168 | 0 | ▶ | if ($self->{_client} && !$self->{_client}->shutdown) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"_client"} and not $self->{"_client"}->shutdown
MC/DC0%0/2
$self->{"_client"} and not $self->{"_client"}->shutdown
$self->{"_client"}
not $self->{"_client"}->shutdown
Truth table0%0/3
A and not(B)
A= $self->{"_client"}B= not $self->{"_client"}->shutdown
| |||||||||||||||||||||||||||||
| 169 | 0 | $self->{_client}->close; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | if ($self->{_handle} && !$self->{_handle}->destroyed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"_handle"} and not $self->{"_handle"}->destroyed
MC/DC0%0/2
$self->{"_handle"} and not $self->{"_handle"}->destroyed
$self->{"_handle"}
not $self->{"_handle"}->destroyed
Truth table0%0/3
A and not(B)
A= $self->{"_handle"}B= not $self->{"_handle"}->destroyed
| |||||||||||||||||||||||||||||
| 172 | 0 | $self->{_handle}->destroy; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 0 | delete $self->{_cache}; | |||||||||||||||||||||||||||
| 175 | 0 | delete $self->{_handle}; | |||||||||||||||||||||||||||
| 176 | 0 | delete $self->{_client}; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | return 1; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 1 | 1; | |||||||||||||||||||||||||||
| 182 | __END__ | ||||||||||||||||||||||||||||