Skip to content

Commit 66fde13

Browse files
Make Reqwest-HTTP environment reject responses based on status code
Signed-off-by: Tilo Wiklund <tilo.wiklund@sensmetry.com>
1 parent 481cdfd commit 66fde13

1 file changed

Lines changed: 43 additions & 14 deletions

File tree

core/src/env/reqwest_http.rs

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,33 +163,62 @@ impl HTTPEnvironment {
163163
}
164164
}
165165

166+
#[derive(Debug)]
167+
pub struct OptionalIter<I> {
168+
inner: Option<I>
169+
}
170+
171+
impl<I: Iterator> Iterator for OptionalIter<I> {
172+
type Item = I::Item;
173+
174+
fn next(&mut self) -> Option<Self::Item> {
175+
if let Some(inner) = &mut self.inner {
176+
inner.next()
177+
} else {
178+
None
179+
}
180+
}
181+
}
182+
183+
type HTTPLinesIter = std::iter::Map<std::io::Lines<BufReader<reqwest::blocking::Response>>, fn(Result<String, std::io::Error>) -> Result<String, HTTPEnvironmentError>>;
184+
166185
impl ReadEnvironment for HTTPEnvironment {
167186
type ReadError = HTTPEnvironmentError;
168187

169-
type UriIter = std::iter::Map<
170-
std::io::Lines<BufReader<reqwest::blocking::Response>>,
171-
fn(Result<String, std::io::Error>) -> Result<String, Self::ReadError>,
172-
>;
188+
type UriIter = OptionalIter<HTTPLinesIter>;
173189

174190
fn uris(&self) -> Result<Self::UriIter, Self::ReadError> {
175191
let response = self.get_entries_request()?.send()?;
176192

177-
Ok(BufReader::new(response)
178-
.lines()
179-
.map(|line| Ok(line?.trim().to_string())))
193+
let inner: std::option::Option<HTTPLinesIter> = if response.status().is_success() {
194+
Some(
195+
BufReader::new(response)
196+
.lines()
197+
.map(|line| Ok(line?.trim().to_string()))
198+
)
199+
} else {
200+
None
201+
};
202+
203+
Ok(OptionalIter { inner })
180204
}
181205

182-
type VersionIter = std::iter::Map<
183-
std::io::Lines<BufReader<reqwest::blocking::Response>>,
184-
fn(Result<String, std::io::Error>) -> Result<String, Self::ReadError>,
185-
>;
206+
type VersionIter = OptionalIter<HTTPLinesIter>;
186207

187208
fn versions<S: AsRef<str>>(&self, uri: S) -> Result<Self::VersionIter, Self::ReadError> {
188209
let response = self.get_versions_request(uri)?.send()?;
189210

190-
Ok(BufReader::new(response)
191-
.lines()
192-
.map(|line| Ok(line?.trim().to_string())))
211+
let inner: Option<HTTPLinesIter> = if response.status().is_success() {
212+
Some(
213+
BufReader::new(response)
214+
.lines()
215+
.map(|line| Ok(line?.trim().to_string()))
216+
)
217+
} else {
218+
None
219+
};
220+
221+
Ok(OptionalIter { inner })
193222
}
194223

195224
type InterchangeProjectRead = HTTPProject;

0 commit comments

Comments
 (0)