Skip to content

Commit

Permalink
Return HttpError from get_content_length
Browse files Browse the repository at this point in the history
  • Loading branch information
pka committed Sep 3, 2023
1 parent 5f04ce8 commit c3a4ead
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/buffered_range_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,11 @@ pub(crate) mod sync {
}

/// Send a HEAD request and get content-length
pub fn get_content_length(&mut self) -> std::result::Result<Option<u64>, std::io::Error> {
let header_val = self
.head_response_header("content-length")
.map_err(|e| match e {
HttpError::HttpStatus(416) => {
std::io::Error::from(std::io::ErrorKind::UnexpectedEof)
}
e => std::io::Error::new(std::io::ErrorKind::Other, e.to_string()),
})?;
pub fn get_content_length(&mut self) -> Result<Option<u64>> {
let header_val = self.head_response_header("content-length")?;
let length_info = if let Some(val) = header_val {
let length = u64::from_str(&val).map_err(|_| {
std::io::Error::new(
std::io::ErrorKind::Other,
"Invalid content-length received",
)
HttpError::HttpError("Invalid content-length received".to_string())
})?;
Some(length)
} else {
Expand Down Expand Up @@ -247,7 +237,7 @@ pub(crate) mod sync {
impl<T: SyncHttpRangeClient> BufRead for SyncBufferedHttpRangeClient<T> {
fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
if self.buffer.offset >= self.buffer.tail() || self.buffer.offset < self.buffer.head {
let res = self.get_range(self.buffer.offset, self.buffer.min_req_size);
let res = self.get_bytes(self.buffer.min_req_size);
if let Some(HttpError::HttpStatus(416)) = res.as_ref().err() {
// An empty buffer indicates that the stream has reached EOF
return Ok(&[]);
Expand All @@ -273,7 +263,9 @@ pub(crate) mod sync {
SeekFrom::End(p) => {
if self.length_info.is_none() {
// Read content-length with HEAD request
let _ = self.get_content_length()?;
let _ = self.get_content_length().map_err(|e| {
std::io::Error::new(std::io::ErrorKind::Other, e.to_string())
})?;
}
if let Some(Some(length)) = self.length_info {
self.buffer.offset = length.saturating_add_signed(p) as usize;
Expand Down

0 comments on commit c3a4ead

Please sign in to comment.