Okay ... I'm planned to create a script that could bounching data from other http server.
Get data from other srver and send them to client is a simple matter...
but It could be a big problem when you want to make it resumable.
Theorically to make it resumable ... you should also send a "range-request header " to the original server.
for example :
"Range: bytes=273376-"
now ... here is my script to grab a file from another server starting from specified bytes...
<?php
function readHTTPDigestAuthenticatedFile($host,$file,$username,$password)
{
if (!$fp=fsockopen($host,80, $errno, $errstr, 15))
return false;
//first do the non-authenticated header so that the server
//sends back a 401 error containing its nonce and opaque
$out = "GET /$file HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
// $out .= "Range: bytes=400-\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
//read the reply and look for the WWW-Authenticate element
while (!feof($fp))
{
$line=fgets($fp, 512);
if (strpos($line,"WWW-Authenticate:")!==false)
$authline=trim(substr($line,18));
}
fclose($fp);
//split up the WWW-Authenticate string to find digest-realm,nonce and opaque values
//if qop value is presented as a comma-seperated list (e.g auth,auth-int) then it won't be retrieved correctly
//but that doesn't matter because going to use 'auth' anyway
$authlinearr=explode(",",$authline);
$autharr=array();
foreach ($authlinearr as $el)
{
$elarr=explode("=",$el);
//the substr here is used to remove the double quotes from the values
$autharr[trim($elarr[0])]=substr($elarr[1],1,strlen($elarr[1])-2);
}
foreach ($autharr as $k=>$v)
echo("$k ==> $v\r\n");
//these are all the vals required from the server
$nonce=$autharr['nonce'];
$opaque=$autharr['opaque'];
$drealm=$autharr['Digest realm'];
//client nonce can be anything since this authentication session is not going to be persistent
//likewise for the cookie - just call it MyCookie
$cnonce="sausages";
//calculate the hashes of A1 and A2 as described in RFC 2617
$a1="$username:$drealm:$password";$a2="GET:/$file";
$ha1=md5($a1);$ha2=md5($a2);
//calculate the response hash as described in RFC 2617
$concat = $ha1.':'.$nonce.':00000001:'.$cnonce.':auth:'.$ha2;
$response=md5($concat);
//put together the Authorization Request Header
$out = "GET /$file HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Connection: Close\r\n";
$out .= "Cookie: cookie=MyCookie\r\n";
// i want to get the file starting from bytes 400
$out .= "Range: bytes=400-\r\n";
$out .= "Pragma: no-cache\r\n";
$out .= "Cache-Control: no-cache\r\n";
$out .= "Authorization: Digest username=\"$username\", realm=\"$drealm\", qop=\"auth\", algorithm=\"MD5\", uri=\"/$file\", nonce=\"$nonce\", nc=00000001, cnonce=\"$cnonce\", opaque=\"$opaque\", response=\"$response\"\r\n\r\n";
if (!$fp=fsockopen($host,80, $errno, $errstr, 15))
return false;
fwrite($fp, $out);
//read in a string which is the contents of the required file
while (!feof($fp))
{
print (fgets($fp, 512));
// flush the memory for better performance
flush();
}
fclose($fp);
}
readHTTPDigestAuthenticatedFile("172.17.100.132","/text.txt","asdf","asdf");
?>
(a modification script from php.net)
Those method using fsockopen to open the remote file. But the most anoying problem when using this function is ... the header is also printed out unwantedly ...
so anyone please tell me how to get rid those header message....