2015-04-07 23:06:19 +02:00
|
|
|
<?php
|
|
|
|
|
2015-04-09 21:49:50 +02:00
|
|
|
class Proxy {
|
|
|
|
public function __construct($serv, $port = 80, $root = '/', $localRoot = '') {
|
|
|
|
// Functions
|
|
|
|
function str_replace_once($search, $replace, $subject) { // TODO Credit
|
|
|
|
$pos = strpos($subject, $search);
|
|
|
|
if ($pos === false) {
|
|
|
|
return $subject;
|
|
|
|
}
|
2015-04-07 23:04:07 +02:00
|
|
|
|
2015-04-09 21:49:50 +02:00
|
|
|
return substr($subject, 0, $pos) . $replace . substr($subject, $pos + strlen($search));
|
2015-04-07 23:06:19 +02:00
|
|
|
}
|
2015-04-09 19:46:07 +02:00
|
|
|
|
2015-04-09 21:49:50 +02:00
|
|
|
if (!function_exists('getallheaders')) { // TODO Credit
|
|
|
|
function getallheaders() {
|
|
|
|
if (!is_array($_SERVER)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$headers = array();
|
|
|
|
foreach ($_SERVER as $name => $value) {
|
2015-04-09 23:47:27 +02:00
|
|
|
if (substr($name, 0, 5) == 'HTTP_') {
|
2015-04-09 21:49:50 +02:00
|
|
|
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
|
2015-04-09 23:47:27 +02:00
|
|
|
} else if (substr($name, 0, 8) == 'CONTENT_') {
|
|
|
|
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $name))))] = $value;
|
2015-04-09 21:49:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $headers;
|
2015-04-07 23:06:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 21:49:50 +02:00
|
|
|
// Target determination
|
|
|
|
$metd = $_SERVER['REQUEST_METHOD'];
|
|
|
|
$reqp = $_SERVER['REQUEST_URI'];
|
2015-04-09 21:59:17 +02:00
|
|
|
if ($localRoot != '') {
|
|
|
|
$reqp = str_replace_once($localRoot, '', $reqp); // TODO Reliable method
|
|
|
|
}
|
2015-04-07 23:06:19 +02:00
|
|
|
|
2015-04-09 21:49:50 +02:00
|
|
|
// Preparing request headers
|
|
|
|
$reqHeds = "$metd $root$reqp HTTP/1.1\r\n";
|
|
|
|
$reqHeds .= "Host: $serv:$port\r\n";
|
2015-04-07 23:06:19 +02:00
|
|
|
|
2015-04-09 21:49:50 +02:00
|
|
|
// Converting client request headers to server request headers
|
|
|
|
$reqsHedsC = getallheaders();
|
|
|
|
foreach ($reqsHedsC as $name => $content) {
|
2015-04-09 23:14:30 +02:00
|
|
|
switch ($name) {
|
|
|
|
case 'Host':
|
|
|
|
case 'Connection':
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-04-09 21:49:50 +02:00
|
|
|
$reqHeds .= "$name: $content\r\n";
|
|
|
|
}
|
|
|
|
}
|
2015-04-09 23:56:45 +02:00
|
|
|
|
|
|
|
$reqHeds .= "Connection: Close\r\n\r\n".file_get_contents('php://input');
|
2015-04-07 23:06:19 +02:00
|
|
|
|
2015-04-09 21:49:50 +02:00
|
|
|
$fp = fsockopen($serv, $port, $errno, $errstr, 30);
|
|
|
|
if (!$fp) { // TODO ErrorCode, ErrorDocument
|
2015-04-09 23:56:45 +02:00
|
|
|
echo "Couldn't connect to server.\n<br/>$errstr ($errno)<br />\n";
|
2015-04-07 23:06:19 +02:00
|
|
|
} else {
|
2015-04-09 21:49:50 +02:00
|
|
|
// Sending request
|
|
|
|
fwrite($fp, $reqHeds);
|
|
|
|
$resBuf = '';
|
|
|
|
while ($get = fgets($fp, 128)) {
|
|
|
|
// Getting response
|
|
|
|
if ($resBuf === True) { // If headers sent
|
|
|
|
echo $get;
|
|
|
|
} else {
|
|
|
|
$resBuf .= $get;
|
|
|
|
if ($sepPos = strrpos($resBuf, "\r\n\r\n")) { // Headers have been retrieved
|
|
|
|
$resHeds = explode("\r\n", substr($resBuf, 0, $sepPos));
|
|
|
|
foreach ($resHeds as $resHed) { // Setting headers
|
|
|
|
header($resHed);
|
|
|
|
if (substr($resHed, 0, 4) == 'HTTP') { // FastCGI fix when using ErrorDocument
|
|
|
|
header('Status: ' . substr($resHed, 9));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo substr($resBuf, $sepPos + 4); // Sending the rest (usually empty)
|
|
|
|
$resBuf = True; // Setting mode to automatically redirect
|
2015-04-07 23:06:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-09 21:49:50 +02:00
|
|
|
fclose($fp);
|
2015-04-07 23:06:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-09 21:49:50 +02:00
|
|
|
|
2015-04-09 19:46:07 +02:00
|
|
|
?>
|