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