Skip to content

Commit

Permalink
Merge pull request #2 from CoverGenius/feature/add-curl-appconnect_time
Browse files Browse the repository at this point in the history
Fix curl appconnect time parsing
  • Loading branch information
specialtactics authored Oct 6, 2022
2 parents 6fd0055 + d148a41 commit e8b104e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/VCR/Util/CurlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ class CurlHelper
\CURLINFO_CONTENT_LENGTH_DOWNLOAD => 'download_content_length',
\CURLINFO_CONTENT_LENGTH_UPLOAD => 'upload_content_length',
\CURLINFO_CONTENT_TYPE => 'content_type',
\CURLINFO_APPCONNECT_TIME => 'appconnect_time',
];

/**
* How many items we expect in the curlinfo array - should correspond to the above list count
*/
const CURLINFO_ITEMS_COUNT = 22;

/**
* Outputs a response depending on the set cURL option.
*
Expand Down Expand Up @@ -107,6 +113,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:
Expand Down
15 changes: 13 additions & 2 deletions src/VCR/Util/HttpUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion tests/Unit/LibraryHooks/CurlHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use VCR\LibraryHooks\CurlHook;
use VCR\Request;
use VCR\Response;
use VCR\Util\CurlHelper;
use VCR\Util\StreamProcessor;

final class CurlHookTest extends TestCase
Expand Down Expand Up @@ -226,8 +227,10 @@ public function testShouldReturnCurlInfoAll(): void
$info = curl_getinfo($curlHandle);
curl_close($curlHandle);

$expectedCount = CurlHelper::CURLINFO_ITEMS_COUNT;

$this->assertIsArray($info, 'curl_getinfo() should return an array.');
$this->assertCount(21, $info, 'curl_getinfo() should return 21 values.');
$this->assertCount($expectedCount, $info, 'curl_getinfo() should return '. $expectedCount .' values.');
$this->curlHook->disable();
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/Util/HttpUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit e8b104e

Please sign in to comment.