Skip to content

Commit 7f352b6

Browse files
authored
Added support for HTTP2 to Mage_HTTP_Client_Curl (#1137)
1 parent 35acb46 commit 7f352b6

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

lib/Mage/HTTP/Client/Curl.php

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -421,31 +421,29 @@ public function doError($string)
421421
* Parse headers - CURL callback functin
422422
*
423423
* @param resource $ch curl handle, not needed
424-
* @param string $data
424+
* @param string $data
425+
*
425426
* @return int
426427
*/
427-
protected function parseHeaders($ch, $data)
428+
protected function parseHeaders($ch, $data): int
428429
{
429-
if($this->_headerCount == 0) {
430+
if ($this->_headerCount === 0) {
431+
$line = explode(' ', trim($data), 3);
430432

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];
436435
} else {
437436
//var_dump($data);
438437
$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;
443441
}
444442

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] = [];
449447
}
450448
$this->_responseHeaders[$name][] = $value;
451449
} else {
@@ -456,10 +454,34 @@ protected function parseHeaders($ch, $data)
456454
}
457455
$this->_headerCount++;
458456

459-
460457
return strlen($data);
461458
}
462459

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+
463485
/**
464486
* Set curl option directly
465487
*

0 commit comments

Comments
 (0)