httprb / http

HTTP (The Gem! a.k.a. http.rb) - a fast Ruby HTTP client with a chainable API, streaming support, and timeouts
MIT License
3.01k stars 321 forks source link

[Question] How to fetch a specific byte range ? #629

Open jamesst20 opened 3 years ago

jamesst20 commented 3 years ago

Hi,

I'm currently migrating a project to rails, however, I can't seem to figure out how to fetch a specific byte range from a big file.

Let's say I have a very big file located to https://domain.com/myfile and that I'm only interesting in reading 20 bytes from the offset 0x50. How would I do that ?

This is how it would be done in PHP:

private function fetchData($url, $rangeStart, $rangeEnd)
{
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RANGE, $rangeStart . '-' . $rangeEnd);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        return curl_exec($ch);
}