Skip to content

Instantly share code, notes, and snippets.

@mickaelperrin
Created August 10, 2016 13:30
Show Gist options
  • Save mickaelperrin/981357d11f6a531967d8fb855dd759dc to your computer and use it in GitHub Desktop.
Save mickaelperrin/981357d11f6a531967d8fb855dd759dc to your computer and use it in GitHub Desktop.
Size of file downloaded on Github differs from headers with PHP and protocol 1.1
<?php
//Smaller file 1M: $test_url = 'https://github.com/mickaelperrin/phantomJS/releases/download/2.1.1/file.txt.bz2';
$test_url = 'https://github.com/medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-linux-x86_64.tar.bz2';
function test_result($result, $http_response_header) {
$result_content_length = strlen($result);
$header_content_length = null;
foreach ($http_response_header as $header) {
if (preg_match('{^content-length:\s*(.+?)\s*$}i', $header, $match)) {
$header_content_length = $match[1];
break;
}
}
if ($result_content_length == $header_content_length ) {
echo "\n[SUCCESS] Content-length are matching";
echo "\nContent-length: $header_content_length";
} else {
echo "\n[FAIL] Content-length are different";
echo "\nDownloaded length: $result_content_length";
echo "\nReported length: $header_content_length";
}
}
echo "\nFirst test with context HTTP protocol 1.1";
$context_1_1 = array (
'http' =>
array (
'protocol_version' => 1.1,
),
);
$ctx = stream_context_create($context_1_1);
$result = file_get_contents($test_url, false, $ctx);
test_result($result, $http_response_header);
echo "\nSecond test with context HTTP protocol 1.0";
$context_1_0 = array (
'http' =>
array (
'protocol_version' => 1.0,
),
);
$ctx = stream_context_create($context_1_0);
$result = file_get_contents($test_url, false, $ctx);
test_result($result, $http_response_header);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment