http.rs: check response status for fetched images

This patch checks if fetching an image resulted in a non-success status
code. In case of non-success status, the response is discarded and an
error is emitted.

This relies on having 3xx codes already handled by surf's Redirect
middleware, so we should see 4xx and 5xx codes here.

Fixes hipstermojo/paperoni#11
This commit is contained in:
KOVACS Tamas 2021-05-09 13:55:26 +02:00
parent 4581f07330
commit 8ec491ff06

View file

@ -78,6 +78,13 @@ async fn process_img_response<'a>(
img_response: &mut surf::Response, img_response: &mut surf::Response,
url: &'a str, url: &'a str,
) -> Result<ImgItem<'a>, ImgError> { ) -> Result<ImgItem<'a>, ImgError> {
if !img_response.status().is_success() {
let kind = ErrorKind::HTTPError(format!(
"Non-success HTTP status code ({})",
img_response.status()
));
return Err(ImgError::with_kind(kind));
}
let img_content: Vec<u8> = match img_response.body_bytes().await { let img_content: Vec<u8> = match img_response.body_bytes().await {
Ok(bytes) => bytes, Ok(bytes) => bytes,
Err(e) => return Err(e.into()), Err(e) => return Err(e.into()),