Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add "Make a partial download with HTTP range headers" example #291

Closed
budziq opened this issue Sep 24, 2017 · 3 comments
Closed

add "Make a partial download with HTTP range headers" example #291

budziq opened this issue Sep 24, 2017 · 3 comments

Comments

@budziq
Copy link
Collaborator

budziq commented Sep 24, 2017

Use reqwest - in conjunction with HTTP range headers to make a partial download of a large file with progress.

cc #84

@sofiageo
Copy link

sofiageo commented Oct 1, 2017

Seems like @jbolila beat me to it :) I didn't make any request comment because i didn't know how far I could go, but it was my first try with rust.

I will still post the code I had so far for any comments

#[macro_use]
extern crate error_chain;
extern crate reqwest;

use std::io::Read;
use reqwest::header::{Headers, Range};

error_chain! {
    foreign_links {
        Io(std::io::Error);
        HttpRequest(reqwest::Error);
        ParseRequest(reqwest::UrlError);
    }
}

fn run() -> Result<()> {
    let url = reqwest::Url::parse("https://rust-lang-nursery.github.io/rust-cookbook/")?;
    let client = reqwest::Client::new()?;

    print!("Making a HEAD request: ");
    let mut req = reqwest::Request::new(reqwest::Method::Head, url.clone());
    let mut res = client.execute(req)?;
    println!("done!");
    let headers= res.headers();
    let supported = headers.get_raw("Accept-Ranges");
    if supported.is_some() {
            let mut headers = Headers::new();
            headers.set(Range::bytes(1, 100));

            println!("Making a GET request: ");
            let mut res = client.get(url.clone())?.headers(headers).send()?;
            let mut body = String::new();
            res.read_to_string(&mut body)?;

            println!("Status: {}", res.status());
            println!("Headers:\n{}", res.headers());
            println!("Body:\n{}", body);
    }

    Ok(())
}

quick_main!(run);

@budziq budziq added the claimed label Oct 3, 2017
@budziq
Copy link
Collaborator Author

budziq commented Oct 3, 2017

@sofiageo no reason to shy away from claiming an issue, we are not pressed for any deadline here! Just pick another issue and start working on it.

In regard to how this issue should be done, please checkout @jbolila 's PR once it's ready (I'll probably do a review there tomorrow). Your code is quite nice but with few suggestions:

  • use convenience method https://docs.rs/reqwest/0.8.0/reqwest/struct.Client.html#method.head instead of reqwest::Request::new(reqwest::Method::Head
  • use typed api headers.get instead of headers.get_raw
  • no need to create temporary mut headers variable it can be constructed within the ?.headers(..).send call
  • we might prefer to read bytes instead of into string (strings must be utf8 and typical binary data would not)
  • we would like to read the supported range from the header to request an appropriate range

@budziq budziq added this to the impl period milestone Oct 4, 2017
budziq pushed a commit that referenced this issue Oct 9, 2017
)

Add "Make a partial download with HTTP range headers" example
@budziq
Copy link
Collaborator Author

budziq commented Oct 9, 2017

Merged!

@budziq budziq closed this as completed Oct 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants