mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
Create HttpStatus to safely deal with HTTP responses status. (#33581)
Signed-off-by: webbeef <me@webbeef.org>
This commit is contained in:
parent
013473f1d5
commit
58f34ad7a3
30 changed files with 344 additions and 403 deletions
|
@ -21,6 +21,7 @@ use ipc_channel::ipc;
|
|||
use ipc_channel::router::ROUTER;
|
||||
use js::jsval::UndefinedValue;
|
||||
use js::rust::{transform_str_to_source_text, CompileOptionsWrapper, HandleObject, Stencil};
|
||||
use net_traits::http_status::HttpStatus;
|
||||
use net_traits::request::{
|
||||
CorsSettings, CredentialsMode, Destination, ParserMetadata, RequestBuilder,
|
||||
};
|
||||
|
@ -360,24 +361,25 @@ impl FetchResponseListener for ClassicContext {
|
|||
FetchMetadata::Filtered { unsafe_, .. } => unsafe_,
|
||||
});
|
||||
|
||||
let status_code = self
|
||||
let status = self
|
||||
.metadata
|
||||
.as_ref()
|
||||
.and_then(|m| match m.status {
|
||||
Some((c, _)) => Some(c),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or(0);
|
||||
.map(|m| m.status.clone())
|
||||
.unwrap_or_else(HttpStatus::new_error);
|
||||
|
||||
self.status = match status_code {
|
||||
0 => Err(NetworkError::Internal(
|
||||
"No http status code received".to_owned(),
|
||||
)),
|
||||
200..=299 => Ok(()), // HTTP ok status codes
|
||||
_ => Err(NetworkError::Internal(format!(
|
||||
"HTTP error code {}",
|
||||
status_code
|
||||
))),
|
||||
self.status = {
|
||||
if status.is_error() {
|
||||
Err(NetworkError::Internal(
|
||||
"No http status code received".to_owned(),
|
||||
))
|
||||
} else if status.is_success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(NetworkError::Internal(format!(
|
||||
"HTTP error code {}",
|
||||
status.code()
|
||||
)))
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue