Last active
October 31, 2016 11:20
-
-
Save wturrell/57e1529c704a2a860afa853ab1d9be06 to your computer and use it in GitHub Desktop.
Test for content-length mismatch problems with Drupal repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Test for content-length mismatch problems with Drupal repository | |
// (adapted from mickaelperrin/test.php) | |
// [email protected] 20161031 | |
$test_urls = [ | |
'http://cgit.drupalcode.org/drupal/plain/.csslintrc?h=8.2.1', | |
'http://cgit.drupalcode.org/drupal/plain/.htaccess?h=8.2.1', | |
'http://cgit.drupalcode.org/drupal/plain/.eslintignore?h=8.2.1', | |
'http://cgit.drupalcode.org/drupal/plain/.eslintrc?h=8.2.1', | |
'http://cgit.drupalcode.org/drupal/plain/.gitattributes?h=8.2.1', | |
'http://cgit.drupalcode.org/drupal/plain/index.php?h=8.2.1', | |
'http://cgit.drupalcode.org/drupal/plain/robots.txt?h=8.2.1', | |
'http://cgit.drupalcode.org/drupal/plain/sites/default/default.settings.php?h=8.2.1', | |
'http://cgit.drupalcode.org/drupal/plain/sites/default/default.services.yml?h=8.2.1' | |
]; | |
foreach ($test_urls as $url) { | |
// Test each file in turn | |
echo "Testing: " . $url; | |
test_download( [ 'protocol_version' => 1.1 ], $url); | |
test_download( [ 'protocol_version' => 1.0 ], $url); | |
echo "\n\n"; | |
} | |
function test_download(array $http_headers, string $url) | |
{ | |
$protocol = sprintf('%0.1f', $http_headers['protocol_version']); | |
echo "\nHTTP protocol " . $protocol; | |
$context = array( | |
'http' => $http_headers | |
); | |
$ctx = stream_context_create($context); | |
$result = file_get_contents($url, false, $ctx); | |
analysis($result, $http_response_header); | |
} | |
function analysis(string $result, array $http_response_header) | |
{ | |
$result_content_length = strlen($result); | |
$header_content_length = null; | |
$etag = null; | |
foreach ($http_response_header as $header) { | |
if (preg_match('{^content-length:\s*(.+?)\s*$}i', $header, $match)) { | |
$header_content_length = $match[1]; | |
} | |
if (preg_match('{^etag:\s*(.+?)\s*$}i', $header, $match)) { | |
$etag = $match[1]; | |
} | |
} | |
if ($result_content_length == $header_content_length) { | |
echo "\n[SUCCESS] Content-lengths match (" . $header_content_length . ")"; | |
} else { | |
echo "\n[FAIL] Content-lengths different ", sprintf("\nDownloaded: %d vs reported: %d", $result_content_length, $header_content_length); | |
if ( ! empty($etag)) { | |
echo "\nEtag = ".$etag; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment