Created
February 26, 2021 10:39
-
-
Save mrkskwsnck/da2dea04df5ec7dbfa81fcb0dfc68614 to your computer and use it in GitHub Desktop.
Extra HTTP header from proxy
This file contains 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 | |
/* | |
* Description: Demonstration of the "extra HTTP header" issue using a | |
* web proxy. The problem becomes significant when the | |
* header is output with the body alltogether. | |
* | |
* Solution: See https://stackoverflow.com/a/17059693/3375584 | |
*/ | |
$url = 'https://www.zdv.uni-mainz.de/feed/'; | |
$use_proxy = true; // Turn proxy on or off | |
$use_fix = false; // Turn fix on or off | |
$ch = curl_init(); | |
curl_setopt_array($ch, [ | |
CURLOPT_URL => $url, | |
CURLOPT_HEADER => true, | |
CURLOPT_RETURNTRANSFER => true | |
]); | |
if ($use_proxy) { | |
curl_setopt_array($ch, [ | |
CURLOPT_PROXY => 'http://webproxy.zdv.uni-mainz.de/', | |
CURLOPT_PROXYPORT => 3128 | |
]); | |
} | |
$response = curl_exec($ch); | |
curl_close($ch); | |
// cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string | |
$extra_headers = [ | |
"HTTP/1.0 200 Connection established\r\n\r\n", | |
"HTTP/1.1 200 Connection established\r\n\r\n" | |
]; | |
if ($use_fix) { | |
foreach($extra_headers as $extra_header) { | |
print 1; | |
if (false !== stripos($response, $extra_header)) { | |
$response = str_ireplace($extra_header, '', $response); | |
break; | |
} | |
} | |
} | |
list($header, $body) = explode("\r\n\r\n", $response, 2); | |
print $header; | |
/* End of Demo */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment