Skip to content

Commit 3a97a59

Browse files
committed
fix header size
1 parent 4497871 commit 3a97a59

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/Curl.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ public function exec()
119119
$errstr = curl_error($this->handle);//Fix: curl_errno() always return 0 when fail
120120
$url = curl_getinfo($this->handle, CURLINFO_EFFECTIVE_URL);
121121
$code = curl_getinfo($this->handle, CURLINFO_HTTP_CODE);
122-
$this->response = Response::make($url, $code, $responseStr, $errno, $errstr);
122+
$headerSize = curl_getinfo($this->handle, CURLINFO_HEADER_SIZE);
123+
$this->response = Response::make($url, $code, $responseStr, $headerSize, [$errno, $errstr]);
123124
return $this->response;
124125
}
125126

src/Response.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,32 @@ public function __toString()
6565
return $this->body;
6666
}
6767

68-
public static function parse($str)
68+
public static function parse($responseStr, $headerSize)
6969
{
70-
$headers = [];
71-
list($header, $body) = explode("\r\n\r\n", $str, 2);
72-
$data = explode("\n", $header);
73-
array_shift($data);//Remove status
70+
$header = substr($responseStr, 0, $headerSize);
71+
$body = substr($responseStr, $headerSize);
72+
$lines = explode("\n", $header);
73+
array_shift($lines);//Remove status
7474

75-
foreach ($data as $part) {
75+
$headers = [];
76+
foreach ($lines as $part) {
7677
$middle = explode(':', $part);
77-
$headers[trim($middle[0])] = trim($middle[1]);
78+
$key = trim($middle[0]);
79+
if ($key === '') {
80+
continue;
81+
}
82+
$headers[$key] = isset($middle[1]) ? trim($middle[1]) : '';
7883
}
7984
return [$headers, $body];
8085
}
8186

82-
public static function make($url, $code, $responseStr, $errno, $errstr)
87+
public static function make($url, $code, $responseStr, $headerSize, array $error)
8388
{
84-
$error = [];
85-
if ($errno || $errstr) {
89+
if (!empty($error[0]) || !empty($error[1])) {
8690
$headers = [];
8791
$body = '';
88-
$error = [$errno, $errstr];
8992
} else {
90-
list($headers, $body) = static::parse($responseStr);
93+
list($headers, $body) = static::parse($responseStr, $headerSize);
9194
}
9295
return new static($url, $code, $body, $headers, $error);
9396
}

0 commit comments

Comments
 (0)