-
Notifications
You must be signed in to change notification settings - Fork 185
Add features to allow choosing between reqwest or curl for downloading #525
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| use std::fmt; | ||
| use std::io; | ||
|
|
||
| use crate::errors::Result; | ||
| use crate::status::StatusBackend; | ||
| use curl::easy::Easy; | ||
|
|
||
| pub use curl::Error; | ||
|
|
||
| const MAX_HTTP_REDIRECTS_ALLOWED: u32 = 10; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct Client { | ||
| handle: Easy, | ||
| } | ||
|
|
||
| pub struct Response { | ||
| data: io::Cursor<Vec<u8>>, | ||
| status: StatusCode, | ||
| } | ||
|
|
||
| #[derive(Clone, Copy, PartialEq, Debug)] | ||
| pub struct StatusCode(u32); | ||
|
|
||
| impl Default for Client { | ||
| fn default() -> Client { | ||
| Client { | ||
| handle: Easy::new(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Client { | ||
| pub fn new() -> Client { | ||
| Client::default() | ||
| } | ||
|
|
||
| fn get(&mut self, url: &str, range: Option<(u64, u64)>) -> Result<Response> { | ||
| let handle = &mut self.handle; | ||
| handle.url(url)?; | ||
| handle.follow_location(true)?; | ||
| handle.max_redirections(MAX_HTTP_REDIRECTS_ALLOWED)?; | ||
| if let Some((start, end)) = range { | ||
| handle.range(&format!("{}-{}", start, end))?; | ||
| } | ||
| let mut buf = Vec::new(); | ||
| { | ||
| let mut transfer = handle.transfer(); | ||
| transfer.write_function(|data| { | ||
| buf.extend_from_slice(data); | ||
| Ok(data.len()) | ||
| })?; | ||
| transfer.perform()?; | ||
| } | ||
| let data = io::Cursor::new(buf); | ||
| let status = StatusCode(handle.response_code()?); | ||
| Ok(Response { data, status }) | ||
| } | ||
| } | ||
|
|
||
| impl Response { | ||
| pub fn status(&self) -> StatusCode { | ||
| self.status | ||
| } | ||
| } | ||
|
|
||
| impl io::Read for Response { | ||
| fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||
| self.data.read(buf) | ||
| } | ||
| } | ||
|
|
||
| impl StatusCode { | ||
| pub const PARTIAL_CONTENT: StatusCode = StatusCode(206); | ||
|
|
||
| pub fn is_success(self) -> bool { | ||
| 200 <= self.0 && self.0 <= 299 | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for StatusCode { | ||
| fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(fmt, "StatusCode({})", self.0) | ||
| } | ||
| } | ||
|
|
||
| pub fn get(url: &str) -> Result<Response> { | ||
| Client::new().get(url, None) | ||
| } | ||
|
|
||
| pub fn get_range_inclusive( | ||
| client: &mut Client, | ||
| url: &str, | ||
| start: u64, | ||
| end: u64, | ||
| ) -> Result<Response> { | ||
| client.get(url, Some((start, end))) | ||
| } | ||
|
|
||
| pub fn resolve_url(url: &str, _status: &mut dyn StatusBackend) -> Result<String> { | ||
| Ok(url.into()) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could not implement something similar to the reqwest implementation, but I not sure that the implementation is really necessary.