resume download using http 206 partial content and range request

HTTP No Comments »

I was thinking of writing a resume download program using win32 to know how it works since i know how socket is working. While i was reading about different headers of HTTP i came to know about Range request with which you can request a part of a file by specifying the start and end position.

	$out ="GET /a.txt HTTP/1.1\r\n";
	$out.="Host: localhost\r\n";	
	$out.="Range: bytes=3-5\r\n";

Firstly, we should determine the total size of a file using the HEAD request.

	$out ="HEAD /a.txt HTTP/1.1\r\n";
	$out.="Host: localhost\r\n\r\n";

You can use your own parser to fetch the size of the file and have it in the memory.

Here is the code to fetch a part of a file using php

	$f = fsockopen("localhost",80);
	if(!$f)die("cannnot connect");	
 
	$out ="GET /this/one.txt HTTP/1.1\r\n";
	$out.="Host: localhost\r\n";	
	$out.="Range: bytes=3-5\r\n";
	$out.="Keev-alive: timeout=15, max=100\r\n";		
	$out.="connection: keep-alive\r\n\r\n";
 
	fwrite($f,$out);
 
	$str ='';
	while(!feof($f))
		$str.=fgets($f,128);
 
	echo $str;

now you can see the part of the text in the file. change the byte ranges in the Range header and try more to see what happens. You can use a parser to fetch the content. You can find a http parser in php.net site.

I have given the very least of information. I will explore on this more and this post will get updated as i update myself.

If there is any objection then yes… proceed… that could help me and all who read this post… anyway… gozar…

206 partial content and range requests

Entries RSS