{"id":18,"date":"2009-05-25T18:12:21","date_gmt":"2009-05-25T12:42:21","guid":{"rendered":"http:\/\/vikku.info\/codesnippets\/?p=18"},"modified":"2009-05-28T02:22:20","modified_gmt":"2009-05-27T20:52:20","slug":"resume-download-and-http-range-header","status":"publish","type":"post","link":"http:\/\/vikku.info\/codesnippets\/http\/resume-download-and-http-range-header\/","title":{"rendered":"resume download using http 206 partial content and range request"},"content":{"rendered":"<p>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.<\/p>\n<pre lang=\"php\">\r\n\t$out =\"GET \/a.txt HTTP\/1.1\\r\\n\";\r\n\t$out.=\"Host: localhost\\r\\n\";\t\r\n\t$out.=\"Range: bytes=3-5\\r\\n\";\r\n<\/pre>\n<p>Firstly, we should determine the total size of a file using the HEAD request.<\/p>\n<pre lang=\"php\">\r\n\t$out =\"HEAD \/a.txt HTTP\/1.1\\r\\n\";\r\n\t$out.=\"Host: localhost\\r\\n\\r\\n\";\r\n<\/pre>\n<p>You can use your own parser to fetch the size of the file and have it in the memory.<\/p>\n<p>Here is the code to fetch a part of a file using php<\/p>\n<pre lang=\"php\">\r\n\t$f = fsockopen(\"localhost\",80);\r\n\tif(!$f)die(\"cannnot connect\");\t\r\n\t\r\n\t$out =\"GET \/this\/one.txt HTTP\/1.1\\r\\n\";\r\n\t$out.=\"Host: localhost\\r\\n\";\t\r\n\t$out.=\"Range: bytes=3-5\\r\\n\";\r\n\t$out.=\"Keev-alive: timeout=15, max=100\\r\\n\";\t\t\r\n\t$out.=\"connection: keep-alive\\r\\n\\r\\n\";\r\n\r\n\tfwrite($f,$out);\r\n\r\n\t$str ='';\r\n\twhile(!feof($f))\r\n\t\t$str.=fgets($f,128);\r\n\t\t\r\n\techo $str;\r\n<\/pre>\n<p>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.<\/p>\n<p>I have given the very least of information. I will explore on this more and this post will get updated as i update myself. <\/p>\n<p>If there is any objection then yes&#8230; proceed&#8230; that could help me and all who read this post&#8230; anyway&#8230; gozar&#8230;<\/p>\n<p><a title='206 partial content and range requests' style='text-decoration: underline' href=\"http:\/\/benramsey.com\/blog\/2008\/05\/206-partial-content-and-range-requests\/\" target=\"_blank\">206 partial content and range requests<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[],"_links":{"self":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/18"}],"collection":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":8,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":74,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/18\/revisions\/74"}],"wp:attachment":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}