clippy: Fix warnings in components/net (#31626)

* clippy: fix warnings in `components/net`

* fix: review comments
This commit is contained in:
eri 2024-03-13 10:40:04 +01:00 committed by GitHub
parent 5ea0531775
commit 63527f56ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 54 additions and 53 deletions

View file

@ -192,9 +192,9 @@ impl CookieStorage {
(match acc.len() { (match acc.len() {
0 => acc, 0 => acc,
_ => acc + "; ", _ => acc + "; ",
}) + &c.cookie.name() + }) + c.cookie.name() +
"=" + "=" +
&c.cookie.value() c.cookie.value()
}; };
let result = url_cookies.iter_mut().fold("".to_owned(), reducer); let result = url_cookies.iter_mut().fold("".to_owned(), reducer);
@ -253,7 +253,7 @@ fn evict_one_cookie(is_secure_cookie: bool, cookies: &mut Vec<Cookie>) -> bool {
true true
} }
fn get_oldest_accessed(is_secure_cookie: bool, cookies: &mut Vec<Cookie>) -> Option<(usize, Tm)> { fn get_oldest_accessed(is_secure_cookie: bool, cookies: &mut [Cookie]) -> Option<(usize, Tm)> {
let mut oldest_accessed: Option<(usize, Tm)> = None; let mut oldest_accessed: Option<(usize, Tm)> = None;
for (i, c) in cookies.iter().enumerate() { for (i, c) in cookies.iter().enumerate() {
if (c.cookie.secure().unwrap_or(false) == is_secure_cookie) && if (c.cookie.secure().unwrap_or(false) == is_secure_cookie) &&

View file

@ -248,7 +248,7 @@ fn poll_with_read(reader: &mut dyn Read, buf: &mut BytesMut) -> Poll<Option<Resu
}; };
match read { match read {
Ok(read) if read == 0 => Poll::Ready(None), Ok(0) => Poll::Ready(None),
Ok(read) => { Ok(read) => {
unsafe { buf.advance_mut(read) }; unsafe { buf.advance_mut(read) };
let chunk = buf.split_to(read).freeze(); let chunk = buf.split_to(read).freeze();

View file

@ -440,10 +440,7 @@ pub async fn main_fetch(
let not_network_error = !response_is_network_error && !internal_response.is_network_error(); let not_network_error = !response_is_network_error && !internal_response.is_network_error();
if not_network_error && if not_network_error &&
(is_null_body_status(&internal_response.status) || (is_null_body_status(&internal_response.status) ||
match request.method { matches!(request.method, Method::HEAD | Method::CONNECT))
Method::HEAD | Method::CONNECT => true,
_ => false,
})
{ {
// when Fetch is used only asynchronously, we will need to make sure // when Fetch is used only asynchronously, we will need to make sure
// that nothing tries to write to the body at this point // that nothing tries to write to the body at this point
@ -487,7 +484,7 @@ pub async fn main_fetch(
if request.synchronous { if request.synchronous {
// process_response is not supposed to be used // process_response is not supposed to be used
// by sync fetch, but we overload it here for simplicity // by sync fetch, but we overload it here for simplicity
target.process_response(&mut response); target.process_response(&response);
if !response_loaded { if !response_loaded {
wait_for_response(&mut response, target, done_chan).await; wait_for_response(&mut response, target, done_chan).await;
} }
@ -872,16 +869,13 @@ async fn scheme_fetch(
} }
fn is_null_body_status(status: &Option<(StatusCode, String)>) -> bool { fn is_null_body_status(status: &Option<(StatusCode, String)>) -> bool {
match *status { matches!(
Some((status, _)) => match status { status,
StatusCode::SWITCHING_PROTOCOLS | Some((StatusCode::SWITCHING_PROTOCOLS, ..)) |
StatusCode::NO_CONTENT | Some((StatusCode::NO_CONTENT, ..)) |
StatusCode::RESET_CONTENT | Some((StatusCode::RESET_CONTENT, ..)) |
StatusCode::NOT_MODIFIED => true, Some((StatusCode::NOT_MODIFIED, ..))
_ => false, )
},
_ => false,
}
} }
/// <https://fetch.spec.whatwg.org/#should-response-to-request-be-blocked-due-to-nosniff?> /// <https://fetch.spec.whatwg.org/#should-response-to-request-be-blocked-due-to-nosniff?>

View file

@ -125,6 +125,7 @@ impl FileManager {
// Read a file for the Fetch implementation. // Read a file for the Fetch implementation.
// It gets the required headers synchronously and reads the actual content // It gets the required headers synchronously and reads the actual content
// in a separate thread. // in a separate thread.
#[allow(clippy::too_many_arguments)]
pub fn fetch_file( pub fn fetch_file(
&self, &self,
done_sender: &mut TokioSender<Data>, done_sender: &mut TokioSender<Data>,
@ -278,6 +279,7 @@ impl FileManager {
}); });
} }
#[allow(clippy::too_many_arguments)]
fn fetch_blob_buf( fn fetch_blob_buf(
&self, &self,
done_sender: &mut TokioSender<Data>, done_sender: &mut TokioSender<Data>,
@ -824,22 +826,20 @@ impl FileManagerStore {
} }
fn promote_memory(&self, id: Uuid, blob_buf: BlobBuf, set_valid: bool, origin: FileOrigin) { fn promote_memory(&self, id: Uuid, blob_buf: BlobBuf, set_valid: bool, origin: FileOrigin) {
match Url::parse(&origin) { // parse to check sanity
// parse to check sanity if Url::parse(&origin).is_err() {
Ok(_) => { return;
self.insert(
id,
FileStoreEntry {
origin,
file_impl: FileImpl::Memory(blob_buf),
refs: AtomicUsize::new(1),
is_valid_url: AtomicBool::new(set_valid),
outstanding_tokens: Default::default(),
},
);
},
Err(_) => {},
} }
self.insert(
id,
FileStoreEntry {
origin,
file_impl: FileImpl::Memory(blob_buf),
refs: AtomicUsize::new(1),
is_valid_url: AtomicBool::new(set_valid),
outstanding_tokens: Default::default(),
},
);
} }
fn set_blob_url_validity( fn set_blob_url_validity(

View file

@ -134,10 +134,10 @@ pub struct HttpCache {
/// Determine if a response is cacheable by default <https://tools.ietf.org/html/rfc7231#section-6.1> /// Determine if a response is cacheable by default <https://tools.ietf.org/html/rfc7231#section-6.1>
fn is_cacheable_by_default(status_code: u16) -> bool { fn is_cacheable_by_default(status_code: u16) -> bool {
match status_code { matches!(
200 | 203 | 204 | 206 | 300 | 301 | 404 | 405 | 410 | 414 | 501 => true, status_code,
_ => false, 200 | 203 | 204 | 206 | 300 | 301 | 404 | 405 | 410 | 414 | 501
} )
} }
/// Determine if a given response is cacheable. /// Determine if a given response is cacheable.

View file

@ -85,6 +85,8 @@ pub enum HttpCacheEntryState {
PendingStore(usize), PendingStore(usize),
} }
type HttpCacheState = Mutex<HashMap<CacheKey, Arc<(Mutex<HttpCacheEntryState>, Condvar)>>>;
pub struct HttpState { pub struct HttpState {
pub hsts_list: RwLock<HstsList>, pub hsts_list: RwLock<HstsList>,
pub cookie_jar: RwLock<CookieStorage>, pub cookie_jar: RwLock<CookieStorage>,
@ -92,7 +94,7 @@ pub struct HttpState {
/// A map of cache key to entry state, /// A map of cache key to entry state,
/// reflecting whether the cache entry is ready to read from, /// reflecting whether the cache entry is ready to read from,
/// or whether a concurrent pending store should be awaited. /// or whether a concurrent pending store should be awaited.
pub http_cache_state: Mutex<HashMap<CacheKey, Arc<(Mutex<HttpCacheEntryState>, Condvar)>>>, pub http_cache_state: HttpCacheState,
pub auth_cache: RwLock<AuthCache>, pub auth_cache: RwLock<AuthCache>,
pub history_states: RwLock<HashMap<HistoryStateId, Vec<u8>>>, pub history_states: RwLock<HashMap<HistoryStateId, Vec<u8>>>,
pub client: Client<Connector, Body>, pub client: Client<Connector, Body>,
@ -348,6 +350,7 @@ fn set_cookies_from_headers(
} }
} }
#[allow(clippy::too_many_arguments)]
fn prepare_devtools_request( fn prepare_devtools_request(
request_id: String, request_id: String,
url: ServoUrl, url: ServoUrl,
@ -478,6 +481,7 @@ impl BodySink {
} }
} }
#[allow(clippy::too_many_arguments)]
async fn obtain_response( async fn obtain_response(
client: &Client<Connector, Body>, client: &Client<Connector, Body>,
url: &ServoUrl, url: &ServoUrl,
@ -703,6 +707,7 @@ async fn obtain_response(
/// [HTTP fetch](https://fetch.spec.whatwg.org#http-fetch) /// [HTTP fetch](https://fetch.spec.whatwg.org#http-fetch)
#[async_recursion] #[async_recursion]
#[allow(clippy::too_many_arguments)]
pub async fn http_fetch( pub async fn http_fetch(
request: &mut Request, request: &mut Request,
cache: &mut CorsCache, cache: &mut CorsCache,
@ -2077,6 +2082,7 @@ async fn cors_preflight_fetch(
// Substep 7 // Substep 7
let unsafe_names = get_cors_unsafe_header_names(&request.headers); let unsafe_names = get_cors_unsafe_header_names(&request.headers);
#[allow(clippy::mutable_key_type)] // We don't mutate the items in the set
let header_names_set: HashSet<&HeaderName> = HashSet::from_iter(header_names.iter()); let header_names_set: HashSet<&HeaderName> = HashSet::from_iter(header_names.iter());
let header_names_contains_star = header_names.iter().any(|hn| hn.as_str() == "*"); let header_names_contains_star = header_names.iter().any(|hn| hn.as_str() == "*");
for unsafe_name in unsafe_names.iter() { for unsafe_name in unsafe_names.iter() {
@ -2180,12 +2186,12 @@ fn is_no_store_cache(headers: &HeaderMap) -> bool {
/// <https://fetch.spec.whatwg.org/#redirect-status> /// <https://fetch.spec.whatwg.org/#redirect-status>
pub fn is_redirect_status(status: &(StatusCode, String)) -> bool { pub fn is_redirect_status(status: &(StatusCode, String)) -> bool {
match status.0 { matches!(
status.0,
StatusCode::MOVED_PERMANENTLY | StatusCode::MOVED_PERMANENTLY |
StatusCode::FOUND | StatusCode::FOUND |
StatusCode::SEE_OTHER | StatusCode::SEE_OTHER |
StatusCode::TEMPORARY_REDIRECT | StatusCode::TEMPORARY_REDIRECT |
StatusCode::PERMANENT_REDIRECT => true, StatusCode::PERMANENT_REDIRECT
_ => false, )
}
} }

View file

@ -63,6 +63,7 @@ fn load_root_cert_store_from_file(file_path: String) -> io::Result<RootCertStore
} }
/// Returns a tuple of (public, private) senders to the new threads. /// Returns a tuple of (public, private) senders to the new threads.
#[allow(clippy::too_many_arguments)]
pub fn new_resource_threads( pub fn new_resource_threads(
user_agent: Cow<'static, str>, user_agent: Cow<'static, str>,
devtools_sender: Option<Sender<DevtoolsControlMsg>>, devtools_sender: Option<Sender<DevtoolsControlMsg>>,
@ -102,6 +103,7 @@ pub fn new_resource_threads(
} }
/// Create a CoreResourceThread /// Create a CoreResourceThread
#[allow(clippy::too_many_arguments)]
pub fn new_core_resource_thread( pub fn new_core_resource_thread(
user_agent: Cow<'static, str>, user_agent: Cow<'static, str>,
devtools_sender: Option<Sender<DevtoolsControlMsg>>, devtools_sender: Option<Sender<DevtoolsControlMsg>>,
@ -225,9 +227,8 @@ impl ResourceChannelManager {
loop { loop {
for receiver in rx_set.select().unwrap().into_iter() { for receiver in rx_set.select().unwrap().into_iter() {
// Handles case where profiler thread shuts down before resource thread. // Handles case where profiler thread shuts down before resource thread.
match receiver { if let ipc::IpcSelectionResult::ChannelClosed(..) = receiver {
ipc::IpcSelectionResult::ChannelClosed(..) => continue, continue;
_ => {},
} }
let (id, data) = receiver.unwrap(); let (id, data) = receiver.unwrap();
// If message is memory report, get the size_of of public and private http caches // If message is memory report, get the size_of of public and private http caches

View file

@ -132,10 +132,10 @@ fn apply_algorithm_to_response<S: ArrayLength<u8>, D: Digest<OutputSize = S>>(
/// <https://w3c.github.io/webappsec-subresource-integrity/#is-response-eligible> /// <https://w3c.github.io/webappsec-subresource-integrity/#is-response-eligible>
fn is_eligible_for_integrity_validation(response: &Response) -> bool { fn is_eligible_for_integrity_validation(response: &Response) -> bool {
match response.response_type { matches!(
ResponseType::Basic | ResponseType::Default | ResponseType::Cors => true, response.response_type,
_ => false, ResponseType::Basic | ResponseType::Default | ResponseType::Cors
} )
} }
/// <https://w3c.github.io/webappsec-subresource-integrity/#does-response-match-metadatalist> /// <https://w3c.github.io/webappsec-subresource-integrity/#does-response-match-metadatalist>