diff --git a/src/VCR/Util/CurlHelper.php b/src/VCR/Util/CurlHelper.php index 2b6b6c1f3..cf7ba95b8 100644 --- a/src/VCR/Util/CurlHelper.php +++ b/src/VCR/Util/CurlHelper.php @@ -36,6 +36,7 @@ class CurlHelper \CURLINFO_CONTENT_LENGTH_DOWNLOAD => 'download_content_length', \CURLINFO_CONTENT_LENGTH_UPLOAD => 'upload_content_length', \CURLINFO_CONTENT_TYPE => 'content_type', + \CURLINFO_APPCONNECT_TIME => 'appconect_time' ]; /** @@ -107,6 +108,7 @@ public static function getCurlOptionFromResponse(Response $response, int $option $info = mb_strlen(HttpUtil::formatAsStatusWithHeadersString($response), 'ISO-8859-1'); break; case \CURLPROXY_HTTPS: + case \CURLINFO_APPCONNECT_TIME: $info = ''; break; default: diff --git a/src/VCR/Util/HttpUtil.php b/src/VCR/Util/HttpUtil.php index bc4532be5..7d1a7b880 100644 --- a/src/VCR/Util/HttpUtil.php +++ b/src/VCR/Util/HttpUtil.php @@ -68,9 +68,20 @@ public static function parseStatus(string $status): array */ public static function parseResponse(string $response): array { - $response = str_replace("HTTP/1.1 100 Continue\r\n\r\n", '', $response); + $parts = explode("\r\n\r\n", $response); - [$rawHeader, $rawBody] = explode("\r\n\r\n", $response, 2); + $headerIndex = 0; + foreach ($parts as $index => $part) { + if (str_starts_with($part, 'HTTP/')) { + $headerIndex = $index; + } else { + break; + } + } + + $rawHeader = $parts[$headerIndex]; + $bodyParts = \array_slice($parts, $headerIndex + 1); + $rawBody = implode('\r\n\r\n', $bodyParts); // Parse headers and status. $headers = self::parseRawHeader($rawHeader); diff --git a/tests/Unit/Util/HttpUtilTest.php b/tests/Unit/Util/HttpUtilTest.php index 58f8c182f..c59074aab 100644 --- a/tests/Unit/Util/HttpUtilTest.php +++ b/tests/Unit/Util/HttpUtilTest.php @@ -96,6 +96,23 @@ public function testParseContinuePlusResponseMultipleHeaders(): void $this->assertEquals($expectedHeaders, $headers); } + public function testParseMultipleResponseCodes(): void + { + $raw = "HTTP/1.1 200 OK\r\n\r\nHTTP/1.1 200 OK\r\n\r\nHTTP/1.1 201 Created\r\nContent-Type: text/html\r\nDate: Fri, 19 Jun 2015 16:05:18 GMT\r\nVary: Accept-Encoding\r\nContent-Length: 0\r\n\r\n"; + [$status, $headers, $body] = HttpUtil::parseResponse($raw); + + $expectedHeaders = [ + 'Content-Type: text/html', + 'Date: Fri, 19 Jun 2015 16:05:18 GMT', + 'Vary: Accept-Encoding', + 'Content-Length: 0', + ]; + + $this->assertEquals('HTTP/1.1 201 Created', $status); + $this->assertEquals('', $body); + $this->assertEquals($expectedHeaders, $headers); + } + public function testParseHeadersBasic(): void { $inputArray = [