Create HttpStatus to safely deal with HTTP responses status. (#33581)

Signed-off-by: webbeef <me@webbeef.org>
This commit is contained in:
webbeef 2024-09-29 11:23:48 -07:00 committed by GitHub
parent 013473f1d5
commit 58f34ad7a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 344 additions and 403 deletions

View file

@ -0,0 +1,117 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#![deny(unsafe_code)]
use std::ops::RangeBounds;
use serde::{Deserialize, Serialize};
use crate::{MallocSizeOf, StatusCode};
/// A representation of a HTTP Status Code and Message that can be used for
/// DOM Response objects and other cases.
/// These objects are immutable once created.
#[derive(Clone, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
pub struct HttpStatus {
code: u16,
message: Vec<u8>,
}
impl HttpStatus {
/// Creates a new HttpStatus for a valid status code.
pub fn new(code: StatusCode, message: Vec<u8>) -> Self {
Self {
code: code.as_u16(),
message,
}
}
/// Creates a new HttpStatus from a raw status code, but will panic
/// if the code is not in the 100 to 599 valid range.
pub fn new_raw(code: u16, message: Vec<u8>) -> Self {
if !(100..=599).contains(&code) {
panic!(
"HttpStatus code must be in the range 100 to 599, inclusive, but is {}",
code
);
}
Self { code, message }
}
/// Creates an instance that represents a Response.error() instance.
pub fn new_error() -> Self {
Self {
code: 0,
message: vec![],
}
}
/// Returns the StatusCode for non-error cases, panics otherwise.
pub fn code(&self) -> StatusCode {
StatusCode::from_u16(self.code).expect("HttpStatus code is 0, can't return a StatusCode")
}
/// Returns the StatusCode if not an error instance, or None otherwise.
pub fn try_code(&self) -> Option<StatusCode> {
StatusCode::from_u16(self.code).ok()
}
/// Returns the u16 representation of the access code. This is usable both for
/// valid HTTP status codes and in the error case.
pub fn raw_code(&self) -> u16 {
self.code
}
/// Get access to a reference of the message part.
pub fn message(&self) -> &[u8] {
&self.message
}
/// Helper that relays is_success() from the underlying code.
pub fn is_success(&self) -> bool {
StatusCode::from_u16(self.code).map_or(false, |s| s.is_success())
}
/// True when the object was created with `new_error`.
pub fn is_error(&self) -> bool {
self.code == 0
}
/// Returns true if this status is in the given range.
/// Always return false for error statuses.
pub fn in_range<T: RangeBounds<u16>>(&self, range: T) -> bool {
self.code != 0 && range.contains(&self.code)
}
}
impl Default for HttpStatus {
/// The default implementation creates a "200 OK" response.
fn default() -> Self {
Self {
code: 200,
message: b"OK".to_vec(),
}
}
}
impl PartialEq<StatusCode> for HttpStatus {
fn eq(&self, other: &StatusCode) -> bool {
self.code == other.as_u16()
}
}
impl From<StatusCode> for HttpStatus {
fn from(code: StatusCode) -> Self {
Self {
code: code.as_u16(),
message: code
.canonical_reason()
.unwrap_or_default()
.as_bytes()
.to_vec(),
}
}
}

View file

@ -26,12 +26,14 @@ use servo_rand::RngCore;
use servo_url::{ImmutableOrigin, ServoUrl};
use crate::filemanager_thread::FileManagerThreadMsg;
use crate::http_status::HttpStatus;
use crate::request::{Request, RequestBuilder};
use crate::response::{HttpsState, Response, ResponseInit};
use crate::storage_thread::StorageThreadMsg;
pub mod blob_url_store;
pub mod filemanager_thread;
pub mod http_status;
pub mod image_cache;
pub mod pub_domains;
pub mod quality;
@ -658,7 +660,7 @@ pub struct Metadata {
pub headers: Option<Serde<HeaderMap>>,
/// HTTP Status
pub status: Option<(u16, Vec<u8>)>,
pub status: HttpStatus,
/// Is successful HTTPS connection
pub https_state: HttpsState,
@ -683,8 +685,7 @@ impl Metadata {
content_type: None,
charset: None,
headers: None,
// https://fetch.spec.whatwg.org/#concept-response-status-message
status: Some((200, b"".to_vec())),
status: HttpStatus::default(),
https_state: HttpsState::None,
referrer: None,
referrer_policy: None,

View file

@ -8,13 +8,14 @@ use std::sync::atomic::AtomicBool;
use std::sync::Mutex;
use headers::{ContentType, HeaderMapExt};
use http::{HeaderMap, StatusCode};
use http::HeaderMap;
use hyper_serde::Serde;
use malloc_size_of_derive::MallocSizeOf;
use serde::{Deserialize, Serialize};
use servo_arc::Arc;
use servo_url::ServoUrl;
use crate::http_status::HttpStatus;
use crate::{
FetchMetadata, FilteredMetadata, Metadata, NetworkError, ReferrerPolicy, ResourceFetchTiming,
ResourceTimingType,
@ -95,10 +96,7 @@ pub struct Response {
pub termination_reason: Option<TerminationReason>,
url: Option<ServoUrl>,
pub url_list: Vec<ServoUrl>,
/// `None` can be considered a StatusCode of `0`.
#[ignore_malloc_size_of = "Defined in hyper"]
pub status: Option<(StatusCode, String)>,
pub raw_status: Option<(u16, Vec<u8>)>,
pub status: HttpStatus,
#[ignore_malloc_size_of = "Defined in hyper"]
pub headers: HeaderMap,
#[ignore_malloc_size_of = "Mutex heap size undefined"]
@ -131,8 +129,7 @@ impl Response {
termination_reason: None,
url: Some(url),
url_list: vec![],
status: Some((StatusCode::OK, "".to_string())),
raw_status: Some((200, b"".to_vec())),
status: HttpStatus::default(),
headers: HeaderMap::new(),
body: Arc::new(Mutex::new(ResponseBody::Empty)),
cache_state: CacheState::None,
@ -153,9 +150,7 @@ impl Response {
res.location_url = init.location_url;
res.headers = init.headers;
res.referrer = init.referrer;
res.status = StatusCode::from_u16(init.status_code)
.map(|s| (s, s.to_string()))
.ok();
res.status = HttpStatus::new_raw(init.status_code, vec![]);
res
}
@ -165,8 +160,7 @@ impl Response {
termination_reason: None,
url: None,
url_list: vec![],
status: None,
raw_status: None,
status: HttpStatus::new_error(),
headers: HeaderMap::new(),
body: Arc::new(Mutex::new(ResponseBody::Empty)),
cache_state: CacheState::None,
@ -282,14 +276,14 @@ impl Response {
response.url_list = vec![];
response.url = None;
response.headers = HeaderMap::new();
response.status = None;
response.status = HttpStatus::new_error();
response.body = Arc::new(Mutex::new(ResponseBody::Empty));
response.cache_state = CacheState::None;
},
ResponseType::OpaqueRedirect => {
response.headers = HeaderMap::new();
response.status = None;
response.status = HttpStatus::new_error();
response.body = Arc::new(Mutex::new(ResponseBody::Empty));
response.cache_state = CacheState::None;
},
@ -310,7 +304,7 @@ impl Response {
);
metadata.location_url.clone_from(&response.location_url);
metadata.headers = Some(Serde(response.headers.clone()));
metadata.status.clone_from(&response.raw_status);
metadata.status.clone_from(&response.status);
metadata.https_state = response.https_state;
metadata.referrer.clone_from(&response.referrer);
metadata.referrer_policy = response.referrer_policy;