@@ -149,7 +149,7 @@ public function send(int $id, Response $response, Request $request)
149
149
return ;
150
150
}
151
151
$ this ->sendBody ($ id , $ response , $ request );
152
- if (! empty ($ pushStream )) {
152
+ if (!empty ($ pushStream )) {
153
153
foreach ($ pushStream as $ push ) {// 发送推送帧
154
154
$ this ->send (...$ push );
155
155
}
@@ -168,7 +168,7 @@ public function send(int $id, Response $response, Request $request)
168
168
169
169
public function sendBody (int $ id , Response $ response , Request $ request ): void
170
170
{
171
- if (! in_array ($ request ->path (), $ this ->streamUrl )) {
171
+ if (!in_array ($ request ->path (), $ this ->streamUrl )) {
172
172
if (is_callable ($ this ->onWriteBody )) {
173
173
// 服务端流模式在此处不停的发送帧
174
174
$ response ->http2Driver = $ this ;
@@ -177,10 +177,10 @@ public function sendBody(int $id, Response $response, Request $request): void
177
177
}
178
178
}
179
179
$ body = $ response ->getBody ();
180
- if (strlen ((string ) $ body )) {
180
+ if (strlen ((string )$ body )) {
181
181
$ this ->writeData ($ body , $ id );
182
182
}
183
- if (! isset ($ this ->streams [$ id ])) {
183
+ if (!isset ($ this ->streams [$ id ])) {
184
184
return ;
185
185
}
186
186
$ trailers = $ response ->getTrailers ();
@@ -249,7 +249,7 @@ public function sendHeader(int $id, Response $response, Request $request): void
249
249
*/
250
250
public function writeData (string $ data , int $ id ): void
251
251
{
252
- if (! isset ($ this ->streams [$ id ])) {
252
+ if (!isset ($ this ->streams [$ id ])) {
253
253
var_dump ('The stream was closed ' );
254
254
return ;
255
255
}
@@ -264,7 +264,7 @@ public function writeFrame(string $data, int $type, int $flags, int $stream = 0)
264
264
265
265
public function handlePing (string $ data ): void
266
266
{
267
- if (! $ this ->pinged ) { // 没有ping过就更新
267
+ if (!$ this ->pinged ) { // 没有ping过就更新
268
268
$ this ->http2Connection ->updateExpirationTime (
269
269
max ($ this ->http2Connection ->getExpirationTime (), time () + 5 )
270
270
);
@@ -282,7 +282,7 @@ public function handleStreamWindowIncrement(int $streamId, int $windowSize): voi
282
282
if ($ streamId > $ this ->remoteStreamId ) {
283
283
throw new Http2ConnectionException ('Stream ID does not exist ' , Http2Parser::PROTOCOL_ERROR );
284
284
}
285
- if (! isset ($ this ->streams [$ streamId ])) {
285
+ if (!isset ($ this ->streams [$ streamId ])) {
286
286
return ;
287
287
}
288
288
$ stream = $ this ->streams [$ streamId ];
@@ -329,7 +329,7 @@ public function handleConnectionWindowIncrement(int $windowSize): void
329
329
public function handleHeaders (int $ streamId , array $ pseudo , array $ headers , bool $ ended ): void
330
330
{
331
331
foreach ($ pseudo as $ name => $ value ) {
332
- if (! isset (Http2Parser::KNOWN_REQUEST_PSEUDO_HEADERS [$ name ])) {
332
+ if (!isset (Http2Parser::KNOWN_REQUEST_PSEUDO_HEADERS [$ name ])) {
333
333
throw new Http2StreamException ('Invalid pseudo header ' , $ streamId , Http2Parser::PROTOCOL_ERROR );
334
334
}
335
335
}
@@ -339,7 +339,7 @@ public function handleHeaders(int $streamId, array $pseudo, array $headers, bool
339
339
throw new Http2StreamException ('Stream remote closed ' , $ streamId , Http2Parser::STREAM_CLOSED );
340
340
}
341
341
} else {
342
- if (! ($ streamId & 1 ) || $ this ->remainingStreams -- <= 0 || $ streamId <= $ this ->remoteStreamId ) {
342
+ if (!($ streamId & 1 ) || $ this ->remainingStreams -- <= 0 || $ streamId <= $ this ->remoteStreamId ) {
343
343
throw new Http2ConnectionException ("Invalid stream ID {$ streamId }" , Http2Parser::PROTOCOL_ERROR );
344
344
}
345
345
$ stream = $ this ->streams [$ streamId ] = new Http2Stream (Options::getBodySizeLimit (), $ this ->initialWindowSize );
@@ -350,7 +350,7 @@ public function handleHeaders(int $streamId, array $pseudo, array $headers, bool
350
350
throw new Http2StreamException ('Stream already reserved ' , $ streamId , Http2Parser::PROTOCOL_ERROR );
351
351
}
352
352
$ stream ->state |= Http2Stream::RESERVED ;
353
- if (! isset ($ pseudo [':method ' ], $ pseudo [':path ' ], $ pseudo [':scheme ' ], $ pseudo [':authority ' ])
353
+ if (!isset ($ pseudo [':method ' ], $ pseudo [':path ' ], $ pseudo [':scheme ' ], $ pseudo [':authority ' ])
354
354
|| isset ($ headers ['connection ' ])
355
355
|| $ pseudo [':path ' ] === ''
356
356
|| (isset ($ headers ['te ' ]) && implode ('' , $ headers ['te ' ]) !== 'trailers ' )
@@ -362,17 +362,17 @@ public function handleHeaders(int $streamId, array $pseudo, array $headers, bool
362
362
$ scheme = $ pseudo [':scheme ' ];
363
363
$ host = $ pseudo [':authority ' ];
364
364
$ query = null ;
365
- if (! preg_match ('#^([A-Z \\d \\. \\-]+| \\[[ \\d:]+ \\])(?::([1-9] \\d*))?$#i ' , (string ) $ host , $ matches )) {
365
+ if (!preg_match ('#^([A-Z \\d \\. \\-]+| \\[[ \\d:]+ \\])(?::([1-9] \\d*))?$#i ' , (string )$ host , $ matches )) {
366
366
throw new Http2StreamException ('Invalid authority (host) name ' , $ streamId , Http2Parser::PROTOCOL_ERROR );
367
367
}
368
368
$ host = $ matches [1 ];
369
- $ port = isset ($ matches [2 ]) ? (int ) $ matches [2 ] : $ this ->http2Connect ->getPort ();
370
- if ($ position = strpos ((string ) $ target , '# ' )) {
371
- $ target = substr ((string ) $ target , 0 , $ position );
369
+ $ port = isset ($ matches [2 ]) ? (int )$ matches [2 ] : $ this ->http2Connection ->getPort ();
370
+ if ($ position = strpos ((string )$ target , '# ' )) {
371
+ $ target = substr ((string )$ target , 0 , $ position );
372
372
}
373
- if ($ position = strpos ((string ) $ target , '? ' )) {
374
- $ query = substr ((string ) $ target , $ position + 1 );
375
- $ target = substr ((string ) $ target , 0 , $ position );
373
+ if ($ position = strpos ((string )$ target , '? ' )) {
374
+ $ query = substr ((string )$ target , $ position + 1 );
375
+ $ target = substr ((string )$ target , 0 , $ position );
376
376
}
377
377
$ headers = array_merge ($ headers , ['scheme ' => $ scheme , 'host ' => $ host , 'port ' => $ port , 'path ' => $ target , 'query ' => $ query , 'method ' => $ method ]);
378
378
$ this ->pinged = 0 ;
@@ -392,17 +392,17 @@ public function handleHeaders(int $streamId, array $pseudo, array $headers, bool
392
392
throw new Http2StreamException ('Received multiple content-length headers ' , $ streamId , Http2Parser::PROTOCOL_ERROR );
393
393
}
394
394
$ contentLength = $ headers ['content-length ' ][0 ];
395
- if (! preg_match ('/^0|[1-9][0-9]*$/ ' , (string ) $ contentLength )) {
395
+ if (!preg_match ('/^0|[1-9][0-9]*$/ ' , (string )$ contentLength )) {
396
396
throw new Http2StreamException ('Invalid content-length header value ' , $ streamId , Http2Parser::PROTOCOL_ERROR );
397
397
}
398
- $ stream ->expectedLength = (int ) $ contentLength ;
398
+ $ stream ->expectedLength = (int )$ contentLength ;
399
399
}
400
400
$ request = new Request ($ streamId , $ this ->http2Connection , $ headers );
401
401
$ this ->streamIdRequestMap [$ streamId ] = $ request ;
402
402
// 如果是客户端流模式,这里应该给前端返回头响应
403
403
if (in_array ($ request ->path (), $ this ->streamUrl )) {
404
404
if (is_callable ($ this ->onRequest )) {
405
- $ response = ($ this ->onRequest )($ request );
405
+ $ response = ($ this ->onRequest )($ request, $ this -> http2Connection );
406
406
} else {
407
407
$ response = new Response (200 , ['content-type ' => 'text/html ' ], '' );
408
408
}
@@ -425,7 +425,7 @@ public function handleData(int $streamId, string $data): void
425
425
if ($ stream ->state & Http2Stream::REMOTE_CLOSED ) {
426
426
throw new Http2StreamException ('Stream remote closed ' , $ streamId , Http2Parser::PROTOCOL_ERROR );
427
427
}
428
- if (! $ length ) {
428
+ if (!$ length ) {
429
429
return ;
430
430
}
431
431
$ this ->serverWindow -= $ length ;
@@ -457,7 +457,7 @@ public function handleData(int $streamId, string $data): void
457
457
$ request ->appendData ($ data );
458
458
}
459
459
if ($ stream ->serverWindow <= self ::MINIMUM_WINDOW ) {
460
- if (! isset ($ this ->streams [$ streamId ])) {
460
+ if (!isset ($ this ->streams [$ streamId ])) {
461
461
return ;
462
462
}
463
463
$ stream = $ this ->streams [$ streamId ];
@@ -478,7 +478,7 @@ public function handleData(int $streamId, string $data): void
478
478
*/
479
479
public function handleStreamEnd (int $ streamId ): void
480
480
{
481
- if (! isset ($ this ->streams [$ streamId ])) {
481
+ if (!isset ($ this ->streams [$ streamId ])) {
482
482
return ;
483
483
}
484
484
$ stream = $ this ->streams [$ streamId ];
@@ -488,16 +488,16 @@ public function handleStreamEnd(int $streamId): void
488
488
}
489
489
/** @var Request $request */
490
490
$ request = $ this ->streamIdRequestMap [$ streamId ];
491
- if (! in_array ($ request ->getMethod (), Options::getAllowedMethods ())) {
491
+ if (!in_array ($ request ->getMethod (), Options::getAllowedMethods ())) {
492
492
$ response = new Response (405 ); // METHOD_NOT_ALLOWED
493
493
} else {
494
- $ response = $ this ->streamIdResponseMap [$ streamId ];
495
- if (! $ response instanceof Response) {
494
+ $ response = $ this ->streamIdResponseMap [$ streamId ] ?? null ;
495
+ if (!$ response instanceof Response) {
496
496
if (is_callable ($ this ->onRequest )) {
497
- $ response = ($ this ->onRequest )($ request );
497
+ $ response = ($ this ->onRequest )($ request, $ this -> http2Connection );
498
498
}
499
499
}
500
- if (! $ response instanceof Response) {
500
+ if (!$ response instanceof Response) {
501
501
$ response = new Response (404 , ['content-type ' => 'text/html ' ], '' );
502
502
}
503
503
}
@@ -529,7 +529,7 @@ public function handlePushPromise(int $streamId, int $pushId, array $pseudo, arr
529
529
*/
530
530
public function handlePriority (int $ streamId , int $ parentId , int $ weight ): void
531
531
{
532
- if (! isset ($ this ->streams [$ streamId ])) {// 为将来的流设置优先级
532
+ if (!isset ($ this ->streams [$ streamId ])) {// 为将来的流设置优先级
533
533
// if ($streamId === 0 || !($streamId & 1) || $this->remainingStreams-- <= 0) {
534
534
if ($ streamId === 0 || $ this ->remainingStreams -- <= 0 ) { // chrome居然发送了偶数帧过来
535
535
throw new Http2ConnectionException ("Invalid stream ID {$ streamId }" , Http2Parser::PROTOCOL_ERROR );
@@ -625,6 +625,7 @@ public function handleConnectionException(Http2ConnectionException $exception):
625
625
}
626
626
627
627
// http1.1升级h2c
628
+
628
629
/**
629
630
* @throws Http2StreamException
630
631
*/
@@ -651,7 +652,7 @@ private function sendPushPromise(Request $request, int $streamId, array $push):
651
652
$ id = $ this ->localStreamId += 2 ;
652
653
// $this->remoteStreamId = \max($id, $this->remoteStreamId);
653
654
$ headers = array_merge (['scheme ' => 'https ' , 'host ' => '' , 'port ' => 443 , 'path ' => '/ ' , 'query ' => '' , 'method ' => 'GET ' ], $ push ['uri ' ], $ mergedHeaders );
654
- $ request = new Request ($ this -> http2Connection , $ this ->http2Connection , $ headers );
655
+ $ request = new Request ($ streamId , $ this ->http2Connection , $ headers );
655
656
$ this ->streams [$ id ] = new Http2Stream (0 , $ this ->initialWindowSize , Http2Stream::RESERVED | Http2Stream::REMOTE_CLOSED );
656
657
$ this ->streamIdRequestMap [$ id ] = $ request ;
657
658
foreach ($ mergedHeaders as $ k => $ v ) {
@@ -680,9 +681,9 @@ private function sendPushPromise(Request $request, int $streamId, array $push):
680
681
}
681
682
$ response = null ;
682
683
if (is_callable ($ this ->onRequest )) {
683
- $ response = ($ this ->onRequest )($ request );
684
+ $ response = ($ this ->onRequest )($ request, $ this -> http2Connection );
684
685
}
685
- if (! $ response instanceof Response) {
686
+ if (!$ response instanceof Response) {
686
687
$ response = new Response (404 , ['content-type ' => 'text/html ' ], '' );
687
688
}
688
689
$ this ->streams [$ id ]->state |= Http2Stream::LOCAL_CLOSED ;
@@ -703,7 +704,7 @@ private function sendBufferedData(): void
703
704
if ($ this ->clientWindow <= 0 ) {
704
705
return ;
705
706
}
706
- if (! strlen ($ stream ->buffer ) || $ stream ->clientWindow <= 0 ) {
707
+ if (!strlen ($ stream ->buffer ) || $ stream ->clientWindow <= 0 ) {
707
708
continue ;
708
709
}
709
710
$ this ->writeBufferedData ($ id );
@@ -752,9 +753,9 @@ private function encodeHeaders(array $headers): string
752
753
{
753
754
$ input = [];
754
755
foreach ($ headers as $ field => $ values ) {
755
- $ values = (array ) $ values ;
756
+ $ values = (array )$ values ;
756
757
foreach ($ values as $ value ) {
757
- $ input [] = [(string ) $ field , (string ) $ value ];
758
+ $ input [] = [(string )$ field , (string )$ value ];
758
759
}
759
760
}
760
761
return $ this ->hPack ->encode ($ input );
@@ -765,7 +766,7 @@ private function shutdown(?int $lastId = null, ?Throwable $reason = null): void
765
766
$ code = $ reason ? $ reason ->getCode () : Http2Parser::GRACEFUL_SHUTDOWN ;
766
767
$ lastId ??= $ id ?? 0 ;
767
768
$ this ->writeFrame (pack ('NN ' , $ lastId , $ code ), Http2Parser::GOAWAY , Http2Parser::NO_FLAG );
768
- if (! empty ($ this ->streams )) {
769
+ if (!empty ($ this ->streams )) {
769
770
if (empty ($ reason )) {
770
771
$ exception = new ClientException ('' , 0 , $ reason );
771
772
} else {
@@ -783,7 +784,7 @@ private function shutdown(?int $lastId = null, ?Throwable $reason = null): void
783
784
*/
784
785
private function releaseStream (int $ id , ClientException $ exception = null ): void
785
786
{
786
- if (! isset ($ this ->streams [$ id ])) {
787
+ if (!isset ($ this ->streams [$ id ])) {
787
788
return ;
788
789
}
789
790
unset($ this ->streams [$ id ]);
0 commit comments