@@ -421,31 +421,29 @@ public function doError($string)
421
421
* Parse headers - CURL callback functin
422
422
*
423
423
* @param resource $ch curl handle, not needed
424
- * @param string $data
424
+ * @param string $data
425
+ *
425
426
* @return int
426
427
*/
427
- protected function parseHeaders ($ ch , $ data )
428
+ protected function parseHeaders ($ ch , $ data ): int
428
429
{
429
- if ($ this ->_headerCount == 0 ) {
430
+ if ($ this ->_headerCount === 0 ) {
431
+ $ line = explode (' ' , trim ($ data ), 3 );
430
432
431
- $ line = explode (" " , trim ($ data ), 3 );
432
- if (count ($ line ) != 3 ) {
433
- return $ this ->doError ("Invalid response line returned from server: " .$ data );
434
- }
435
- $ this ->_responseStatus = intval ($ line [1 ]);
433
+ $ this ->validateHttpVersion ($ line );
434
+ $ this ->_responseStatus = (int )$ line [1 ];
436
435
} else {
437
436
//var_dump($data);
438
437
$ name = $ value = '' ;
439
- $ out = explode (": " , trim ($ data ), 2 );
440
- if (count ($ out ) == 2 ) {
441
- $ name = $ out [0 ];
442
- $ value = $ out [1 ];
438
+ $ out = explode (': ' , trim ($ data ), 2 );
439
+ if (count ($ out ) === 2 ) {
440
+ list ($ name , $ value ) = $ out ;
443
441
}
444
442
445
- if ( strlen ( $ name) ) {
446
- if ( " Set-Cookie " == $ name ) {
447
- if (!isset ($ this ->_responseHeaders [$ name ])) {
448
- $ this ->_responseHeaders [$ name ] = array () ;
443
+ if ( $ name !== '' ) {
444
+ if ( $ name === ' Set-Cookie ' ) {
445
+ if (!isset ($ this ->_responseHeaders [$ name ])) {
446
+ $ this ->_responseHeaders [$ name ] = [] ;
449
447
}
450
448
$ this ->_responseHeaders [$ name ][] = $ value ;
451
449
} else {
@@ -456,10 +454,34 @@ protected function parseHeaders($ch, $data)
456
454
}
457
455
$ this ->_headerCount ++;
458
456
459
-
460
457
return strlen ($ data );
461
458
}
462
459
460
+ /**
461
+ * @param array $line
462
+ *
463
+ * @throws Exception
464
+ */
465
+ protected function validateHttpVersion (array $ line )
466
+ {
467
+ if ($ line [0 ] === 'HTTP/1.0 ' || $ line [0 ] === 'HTTP/1.1 ' ) {
468
+ if (count ($ line ) !== 3 ) {
469
+ $ this ->doError ('Invalid response line returned from server: ' . implode (' ' , $ line ));
470
+ }
471
+
472
+ return ;
473
+ }
474
+
475
+ if ($ line [0 ] === 'HTTP/2 ' ) {
476
+ if (!in_array (count ($ line ), [2 , 3 ])) {
477
+ $ this ->doError ('Invalid response line returned from server: ' . implode (' ' , $ line ));
478
+ }
479
+
480
+ return ;
481
+ }
482
+ $ this ->doError ('Invalid response line returned from server: ' . $ data );
483
+ }
484
+
463
485
/**
464
486
* Set curl option directly
465
487
*
0 commit comments