Skip to content

Commit 101b637

Browse files
authored
Merge branch 'master' into feat_type_contentencoding_bc
2 parents 9ef274a + a9dd75a commit 101b637

File tree

8 files changed

+37
-14
lines changed

8 files changed

+37
-14
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
strategy:
1818
fail-fast: false
1919
matrix:
20-
php: [ '8.1', '8.2', '8.3' ]
20+
php: [ '8.1', '8.2', '8.3', '8.4' ]
2121
ext_base: [ 'none, dom, tokenizer, xml, xmlwriter,' ]
2222
ext_lib: [ 'curl, mbstring, openssl,' ]
2323
ext_optional: [ '', 'bcmath', 'gmp' ]

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ $defaultOptions = [
203203
'urgency' => 'normal', // protocol defaults to "normal". (very-low, low, normal, or high)
204204
'topic' => 'newEvent', // not defined by default. Max. 32 characters from the URL or filename-safe Base64 characters sets
205205
'batchSize' => 200, // defaults to 1000
206+
'contentType' => 'application/json', // defaults to "application/octet-stream"
206207
];
207208

208209
// for every notification
@@ -236,6 +237,11 @@ In order to fix this, WebPush sends notifications in batches. The default size i
236237
to decrease this number. Do this while instantiating WebPush or calling `setDefaultOptions`. Or, if you want to customize this for a specific flush, give
237238
it as a parameter : `$webPush->flush($batchSize)`.
238239

240+
#### contentType
241+
242+
Sets the "Content-Type" header for HTTP requests with a non-empty payload sent to the push service.
243+
Especially newer [Declarative push messages](https://www.w3.org/TR/push-api/#declarative-push-message) require a specific JSON payload, so this should be set to "application/json" in such cases.
244+
239245
### Server errors
240246

241247
You can see what the browser vendor's server sends back in case it encountered an error (push subscription expiration, wrong parameters...).

composer.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
"ext-json": "*",
3434
"ext-mbstring": "*",
3535
"ext-openssl": "*",
36-
"guzzlehttp/guzzle": "^7.4.5",
37-
"web-token/jwt-library": "^3.3.0|^4.0.0",
38-
"spomky-labs/base64url": "^2.0.4"
36+
"guzzlehttp/guzzle": "^7.9.2",
37+
"spomky-labs/base64url": "^2.0.4",
38+
"symfony/polyfill-php82": "^v1.31.0",
39+
"web-token/jwt-library": "^3.3.0|^4.0.0"
3940
},
4041
"suggest": {
4142
"ext-bcmath": "Optional for performance.",
@@ -44,7 +45,8 @@
4445
"require-dev": {
4546
"phpunit/phpunit": "^10.5.44|^11.5.6",
4647
"phpstan/phpstan": "^2.1.2",
47-
"friendsofphp/php-cs-fixer": "^v3.68.5"
48+
"friendsofphp/php-cs-fixer": "^v3.91.3",
49+
"symfony/polyfill-iconv": "^1.33"
4850
},
4951
"autoload": {
5052
"psr-4": {

phpstan.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ parameters:
44
- src
55
reportUnmatchedIgnoredErrors: false
66
ignoreErrors:
7-
- '#Unreachable statement \- code above always terminates\.#'
87
- identifier: missingType.iterableValue

src/Encryption.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,16 @@ public static function padPayload(string $payload, int $maxLengthToPad, ContentE
4747
* @param string $userPublicKey Base 64 encoded (MIME or URL-safe)
4848
* @param string $userAuthToken Base 64 encoded (MIME or URL-safe)
4949
*
50-
* @throws \ErrorException
50+
* @throws \ErrorException Thrown on php 8.1
51+
* @throws \Random\RandomException Thrown on php 8.2 and higher
5152
*/
52-
public static function encrypt(string $payload, string $userPublicKey, string $userAuthToken, ContentEncoding $contentEncoding): array
53-
{
53+
public static function encrypt(
54+
string $payload,
55+
string $userPublicKey,
56+
#[\SensitiveParameter]
57+
string $userAuthToken,
58+
ContentEncoding $contentEncoding,
59+
): array {
5460
return self::deterministicEncrypt(
5561
$payload,
5662
$userPublicKey,
@@ -67,6 +73,7 @@ public static function encrypt(string $payload, string $userPublicKey, string $u
6773
public static function deterministicEncrypt(
6874
string $payload,
6975
string $userPublicKey,
76+
#[\SensitiveParameter]
7077
string $userAuthToken,
7178
ContentEncoding $contentEncoding,
7279
array $localKeyObject,

src/Notification.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function getOptions(array $defaultOptions = []): array
4343
$options['TTL'] = array_key_exists('TTL', $options) ? $options['TTL'] : $defaultOptions['TTL'];
4444
$options['urgency'] = array_key_exists('urgency', $options) ? $options['urgency'] : $defaultOptions['urgency'];
4545
$options['topic'] = array_key_exists('topic', $options) ? $options['topic'] : $defaultOptions['topic'];
46+
$options['contentType'] = array_key_exists('contentType', $options) ? $options['contentType'] : $defaultOptions['contentType'];
4647

4748
return $options;
4849
}

src/VAPID.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,15 @@ public static function validate(array $vapid): array
9797
* @return array Returns an array with the 'Authorization' and 'Crypto-Key' values to be used as headers
9898
* @throws \ErrorException
9999
*/
100-
public static function getVapidHeaders(string $audience, string $subject, string $publicKey, string $privateKey, ContentEncoding $contentEncoding, ?int $expiration = null): array
101-
{
100+
public static function getVapidHeaders(
101+
string $audience,
102+
string $subject,
103+
string $publicKey,
104+
#[\SensitiveParameter]
105+
string $privateKey,
106+
ContentEncoding $contentEncoding,
107+
?int $expiration = null,
108+
): array {
102109
$expirationLimit = time() + 43200; // equal margin of error between 0 and 24h
103110
if (null === $expiration || $expiration > $expirationLimit) {
104111
$expiration = $expirationLimit;

src/WebPush.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ protected function createRejectedReport($reason): MessageSentReport
235235
}
236236

237237
/**
238-
* @throws \ErrorException
239-
* add back @throws \Random\RandomException when we drop PHP 8.1 support
238+
* @throws \ErrorException Thrown on php 8.1
239+
* @throws \Random\RandomException Thrown on php 8.2 and higher
240240
*/
241241
protected function prepare(array $notifications): array
242242
{
@@ -263,7 +263,7 @@ protected function prepare(array $notifications): array
263263
$localPublicKey = $encrypted['localPublicKey'];
264264

265265
$headers = [
266-
'Content-Type' => 'application/octet-stream',
266+
'Content-Type' => $options['contentType'],
267267
'Content-Encoding' => $contentEncoding,
268268
];
269269

@@ -384,6 +384,7 @@ public function setDefaultOptions(array $defaultOptions): WebPush
384384
$this->defaultOptions['topic'] = $defaultOptions['topic'] ?? null;
385385
$this->defaultOptions['batchSize'] = $defaultOptions['batchSize'] ?? 1000;
386386
$this->defaultOptions['requestConcurrency'] = $defaultOptions['requestConcurrency'] ?? 100;
387+
$this->defaultOptions['contentType'] = $defaultOptions['contentType'] ?? 'application/octet-stream';
387388

388389

389390
return $this;

0 commit comments

Comments
 (0)