From 8ec491ff06610bf017270d576f95e3e356423b9f Mon Sep 17 00:00:00 2001 From: KOVACS Tamas Date: Sun, 9 May 2021 13:55:26 +0200 Subject: [PATCH] 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 --- src/http.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/http.rs b/src/http.rs index b076cf5..efd64b8 100644 --- a/src/http.rs +++ b/src/http.rs @@ -78,6 +78,13 @@ async fn process_img_response<'a>( img_response: &mut surf::Response, url: &'a str, ) -> Result, 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 = match img_response.body_bytes().await { Ok(bytes) => bytes, Err(e) => return Err(e.into()),