1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| <?php
// proxy http
// auteur: Marc Gauthier
// 02/09/2009
// http://www.papygeek.com/download/53/
// Transfer-Encoding: chunked
$debug = 1;
$sUrl = '';
$sReponse = '';
// via un proxy
$proxy_port = 80;
$proxy_host = '';
if ($_SERVER["REQUEST_METHOD"] === 'GET' && isset($_REQUEST["url"]) ) {
$sUrl = $_REQUEST["url"];
debug("GET url:$sUrl");
proxy($sUrl);
exit;
}
phpinfo(INFO_VARIABLES);exit;
function proxy($sUrl) {
global $proxy_host,$proxy_port;
$aUrl = @parse_url($sUrl);
if ( ! isset($aUrl['scheme']) ) {
confess("proxy scheme");
}
// construction de la requete
$sReq = "GET $sUrl HTTP/1.0\r\n" .
'Host: ' . $aUrl['host'] . ($aUrl['port'] ? ':'. $aUrl['port'] : '') .
"\r\nConnection: close\r\n\r\n";
if ( empty($proxy_host)) {
$host = $aUrl["host"];
$port = $aUrl["port"] ? $aUrl["port"] : 80;
} else {
$host = $proxy_host;
$port = $proxy_port;
}
debug("host:$host port:$port");
$fp = @fsockopen ($host, $port, $errno, $errstr, 5);
if (!$fp) {
confess("fsockopen failed: $errstr ($errno)");
}
debug("sReq:$sReq");
fwrite($fp, $sReq);
debug("before loop");
$headers_processed = 0;
$reponse = '';
$strip_header = 1;
$set_content_type = 1;
$chunked = 0;
ob_start();
while (!feof($fp)) {
$r = fread($fp, 4096);
#debug("in loop: $r");
if ($strip_header && !$headers_processed) {
#debug("in loop strip headers: $r");
debug("trying to find end of headers");
$response .= $r;
$nlnl = strpos($response, "\r\n\r\n");
$add = 4;
if (!$nlnl) {
$nlnl = strpos($response, "\n\n");
$add = 2;
}
if (!$nlnl) continue;
debug("end of headers found");
// Transfer-Encoding: chunked
if ($set_content_type) {
debug("extracting content-type");
$headers = substr($response, 0, $nlnl);
debug("headers: $headers");
if ( ! valideHeaders($headers) ) {
confess("headers");
}
if (preg_match_all('/^(Content-.*?)(\r?\n|$)/ims', $headers, $matches)) {
for ($i = 0; $i < count($matches[0]); ++$i) {
$ct = $matches[1][$i];
debug("header($ct)");
header($ct);
}
}
}
$response = substr($response, $nlnl+$add);
if ( $chunked ) {
$nlnl = strpos($response, "\r\n<?xml");
if ($nlnl) {
$response = substr($response, $nlnl+2);
}
}
debug("header post:".substr($response,0,20));
print $response;
$headers_processed = 1;
} else {
print $r;
}
}
fclose ($fp);
debug("after loop");
}
//
// on valide les headers
function valideHeaders($header=null) {
$headers = explode("\n",$header);
if (!is_array($headers) or count($headers) < 1) {
return false;
}
switch(trim(strtolower($headers[0]))) {
case 'http/1.0 100 ok':
case 'http/1.0 200 ok':
case 'http/1.1 100 ok':
case 'http/1.1 200 ok':
return true;
break;
}
return false;
}
//
// mode chunk
//
// la chaine "str" peut contenir plusieurs chunks
function unchunk($str=null) {
global $sReponse;
if (!is_string($str)) {
confess("unchunk string");
}
// on ajoute au reliquat precedent
$sReponse .= $str;
$eol = "\r\n";
$add = strlen($eol);
$tmp = $sReponse;
$str = '';
// on recherche les chunks
do {
$tmp = ltrim($tmp);
$pos = strpos($tmp, $eol);
if ($pos === false) {
confess("unchunk eol");
}
$len = hexdec(substr($tmp,0,$pos));
if (!is_numeric($len) or $len < 0) {
confess("unchunk len:$len");
}
// on a tout dans le buffer
if ( strlen($tmp) < ($pos + $add + $len) ) {
// pour la prochaine fois
$sReponse = $tmp;
return;
}
$str = substr($tmp, ($pos + $add), $len);
print $str;
$sReponse = substr($tmp, ($len + $pos + $add));
$check = trim($sReponse);
} while(!empty($check));
unset($tmp);
unset($str);
return;
}
function confess($msg) {
debug($msg);
header("HTTP/1.0 500 $msg");
exit;
}
function debug($msg) {
global $debug;
if ($debug) error_log(__FILE__.": $msg", 0);
}
?> |
Partager