Revert "Replace time with std::time in components/net (#31079)" (#31120)

This reverts commit 580062228b.
This commit is contained in:
Martin Robinson 2024-01-18 16:10:48 +01:00 committed by GitHub
parent c3fd27c225
commit 8e5f28839c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 123 additions and 161 deletions

View file

@ -11,7 +11,7 @@ use std::collections::HashMap;
use std::ops::Bound;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::time::SystemTime;
use headers::{
CacheControl, ContentRange, Expires, HeaderMapExt, LastModified, Pragma, Range, Vary,
@ -30,6 +30,7 @@ use net_traits::{FetchMetadata, Metadata, ResourceFetchTiming};
use servo_arc::Arc;
use servo_config::pref;
use servo_url::ServoUrl;
use time::{Duration, Timespec, Tm};
use tokio::sync::mpsc::{unbounded_channel as unbounded, UnboundedSender as TokioSender};
use crate::fetch::methods::{Data, DoneChannel};
@ -74,7 +75,7 @@ struct MeasurableCachedResource {
raw_status: Option<(u16, Vec<u8>)>,
url_list: Vec<ServoUrl>,
expires: Duration,
last_validated: SystemTime,
last_validated: Tm,
}
impl MallocSizeOf for CachedResource {
@ -180,11 +181,11 @@ fn calculate_response_age(response: &Response) -> Duration {
if let Some(secs) = response.headers.get(header::AGE) {
if let Ok(seconds_string) = secs.to_str() {
if let Ok(secs) = seconds_string.parse::<i64>() {
return Duration::from_secs(secs as u64);
return Duration::seconds(secs);
}
}
}
Duration::ZERO
Duration::seconds(0i64)
}
/// Determine the expiry date from relevant headers,
@ -195,10 +196,14 @@ fn get_response_expiry(response: &Response) -> Duration {
if let Some(directives) = response.headers.typed_get::<CacheControl>() {
if directives.no_cache() {
// Requires validation on first use.
return Duration::ZERO;
return Duration::seconds(0i64);
} else {
if let Some(max_age) = directives.max_age().or(directives.s_max_age()) {
return max_age.checked_sub(age).unwrap_or(Duration::ZERO);
if let Some(secs) = directives.max_age().or(directives.s_max_age()) {
let max_age = Duration::from_std(secs).unwrap();
if max_age < age {
return Duration::seconds(0i64);
}
return max_age - age;
}
}
}
@ -206,15 +211,18 @@ fn get_response_expiry(response: &Response) -> Duration {
Some(t) => {
// store the period of time from now until expiry
let t: SystemTime = t.into();
let desired = t.duration_since(UNIX_EPOCH).unwrap_or_default();
let current = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default();
let t = t.duration_since(SystemTime::UNIX_EPOCH).unwrap();
let desired = Timespec::new(t.as_secs() as i64, 0);
let current = time::now().to_timespec();
return desired.checked_sub(current).unwrap_or(Duration::ZERO);
if desired > current {
return desired - current;
} else {
return Duration::seconds(0i64);
}
},
// Malformed Expires header, shouldn't be used to construct a valid response.
None if response.headers.contains_key(header::EXPIRES) => return Duration::ZERO,
None if response.headers.contains_key(header::EXPIRES) => return Duration::seconds(0i64),
_ => {},
}
// Calculating Heuristic Freshness
@ -223,18 +231,19 @@ fn get_response_expiry(response: &Response) -> Duration {
// <https://tools.ietf.org/html/rfc7234#section-5.5.4>
// Since presently we do not generate a Warning header field with a 113 warn-code,
// 24 hours minus response age is the max for heuristic calculation.
let max_heuristic = Duration::from_secs(24 * 3600) - age;
let max_heuristic = Duration::hours(24) - age;
let heuristic_freshness = if let Some(last_modified) =
// If the response has a Last-Modified header field,
// caches are encouraged to use a heuristic expiration value
// that is no more than some fraction of the interval since that time.
response.headers.typed_get::<LastModified>()
{
let current = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default();
let current = time::now().to_timespec();
let last_modified: SystemTime = last_modified.into();
let last_modified = last_modified.duration_since(UNIX_EPOCH).unwrap();
let last_modified = last_modified
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
let last_modified = Timespec::new(last_modified.as_secs() as i64, 0);
// A typical setting of this fraction might be 10%.
let raw_heuristic_calc = (current - last_modified) / 10;
let result = if raw_heuristic_calc < max_heuristic {
@ -259,7 +268,7 @@ fn get_response_expiry(response: &Response) -> Duration {
}
}
// Requires validation upon first use as default.
Duration::ZERO
Duration::seconds(0i64)
}
/// Request Cache-Control Directives
@ -271,19 +280,24 @@ fn get_expiry_adjustment_from_request_headers(request: &Request, expires: Durati
};
if let Some(max_age) = directive.max_stale() {
return expires + max_age;
return expires + Duration::from_std(max_age).unwrap();
}
if let Some(max_age) = directive.max_age() {
let max_age = Duration::from_std(max_age).unwrap();
if expires > max_age {
return Duration::ZERO;
return Duration::min_value();
}
return expires - max_age;
}
if let Some(min_fresh) = directive.min_fresh() {
return expires.checked_sub(min_fresh).unwrap_or(Duration::ZERO);
let min_fresh = Duration::from_std(min_fresh).unwrap();
if expires < min_fresh {
return Duration::min_value();
}
return expires - min_fresh;
}
if directive.no_cache() || directive.no_store() {
return Duration::ZERO;
return Duration::min_value();
}
expires
@ -327,15 +341,8 @@ fn create_cached_response(
response.aborted = cached_resource.aborted.clone();
let expires = cached_resource.data.expires;
let adjusted_expires = get_expiry_adjustment_from_request_headers(request, expires);
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default();
let last_validated = cached_resource
.data
.last_validated
.duration_since(UNIX_EPOCH)
.unwrap_or_default();
let now = Duration::seconds(time::now().to_timespec().sec);
let last_validated = Duration::seconds(cached_resource.data.last_validated.to_timespec().sec);
let time_since_validated = now - last_validated;
// TODO: take must-revalidate into account <https://tools.ietf.org/html/rfc7234#section-5.2.2.1>
// TODO: if this cache is to be considered shared, take proxy-revalidate into account
@ -806,7 +813,7 @@ impl HttpCache {
let entry_key = CacheKey::from_servo_url(url);
if let Some(cached_resources) = self.entries.get_mut(&entry_key) {
for cached_resource in cached_resources.iter_mut() {
cached_resource.data.expires = Duration::ZERO;
cached_resource.data.expires = Duration::seconds(0i64);
}
}
}
@ -890,7 +897,7 @@ impl HttpCache {
raw_status: response.raw_status.clone(),
url_list: response.url_list.clone(),
expires: expiry,
last_validated: SystemTime::now(),
last_validated: time::now(),
}),
};
let entry = self.entries.entry(entry_key).or_insert_with(|| vec![]);