Merge branch 'no-http-status-err' into 'main'

dirclient: Remove HttpStatus error variant

Closes #349

See merge request tpo/core/arti!329
This commit is contained in:
Ian Jackson 2022-02-18 12:04:54 +00:00
commit ccb4604237
3 changed files with 18 additions and 9 deletions

View File

@ -18,10 +18,6 @@ pub enum Error {
#[error("truncated HTTP headers")]
TruncatedHeaders,
/// Got an HTTP status other than 200
#[error("unexpected HTTP status {0:?}")]
HttpStatus(Option<u16>),
/// Received a response that was longer than we expected.
#[error("response too long; gave up after {0} bytes")]
ResponseTooLong(usize),
@ -94,7 +90,6 @@ impl HasKind for Error {
match self {
E::DirTimeout => EK::TorNetworkTimeout,
E::TruncatedHeaders => EK::TorProtocolViolation,
E::HttpStatus(_) => EK::RemoteRefused,
E::ResponseTooLong(_) => EK::TorProtocolViolation,
E::Utf8Encoding(_) => EK::TorProtocolViolation,
// TODO: it would be good to get more information out of the IoError

View File

@ -179,7 +179,12 @@ where
// TODO: should there be a separate timeout here?
let header = read_headers(&mut buffered).await?;
if header.status != Some(200) {
return Err(Error::HttpStatus(header.status));
return Ok(DirResponse::new(
header.status.unwrap_or(0),
None,
vec![],
source,
));
}
let mut decoder = get_decoder(buffered, header.encoding.as_deref())?;
@ -726,7 +731,7 @@ mod test {
let response_text = b"HTTP/1.0 418 I'm a teapot\r\n\r\n";
let (response, _request) = run_download_test(req, response_text);
assert!(matches!(response, Err(Error::HttpStatus(Some(418)))));
assert_eq!(response.unwrap().status_code(), 418);
}
#[test]

View File

@ -94,9 +94,18 @@ async fn fetch_multiple<R: Runtime>(
let mut useful_responses = Vec::new();
for r in responses {
// TODO: on some error cases we might want to stop using this source.
match r {
Ok(x) => useful_responses.push(x),
// TODO: in this case we might want to stop using this source.
Ok((request, response)) => {
if response.status_code() == 200 {
useful_responses.push((request, response));
} else {
trace!(
"cache declined request; reported status {:?}",
response.status_code()
);
}
}
Err(e) => warn!("error while downloading: {:?}", e),
}
}