diff options
author | Bastien Orivel <eijebong@bananium.fr> | 2018-08-27 18:36:52 +0200 |
---|---|---|
committer | Bastien Orivel <eijebong@bananium.fr> | 2018-11-01 19:17:36 +0100 |
commit | 024b40b39d3848f1a1f7020bd7ed8c901817f09c (patch) | |
tree | 27508f102b0973cbae3dca22143ea4aedd349f4b /components/net/http_cache.rs | |
parent | 95bfaa0a770479fb3bf6bf0b1f85c9ae343e66ff (diff) | |
download | servo-024b40b39d3848f1a1f7020bd7ed8c901817f09c.tar.gz servo-024b40b39d3848f1a1f7020bd7ed8c901817f09c.zip |
Update hyper to 0.12
Diffstat (limited to 'components/net/http_cache.rs')
-rw-r--r-- | components/net/http_cache.rs | 336 |
1 files changed, 163 insertions, 173 deletions
diff --git a/components/net/http_cache.rs b/components/net/http_cache.rs index 1fcafc92952..dbb777eb103 100644 --- a/components/net/http_cache.rs +++ b/components/net/http_cache.rs @@ -8,12 +8,11 @@ //! and <http://tools.ietf.org/html/rfc7232>. use fetch::methods::{Data, DoneChannel}; -use hyper::header; -use hyper::header::ContentType; -use hyper::header::Headers; -use hyper::method::Method; -use hyper::status::StatusCode; -use hyper_serde::Serde; +use headers_core::HeaderMapExt; +use headers_ext::{CacheControl, ContentRange, Expires, LastModified, Pragma, Range, Vary}; +use http::{header, HeaderMap}; +use http::header::HeaderValue; +use hyper::{Method, StatusCode}; use malloc_size_of::{MallocSizeOf, MallocSizeOfOps, MallocUnconditionalSizeOf, MallocUnconditionalShallowSizeOf}; use malloc_size_of::Measurable; use net_traits::{Metadata, FetchMetadata}; @@ -24,11 +23,12 @@ use servo_channel::{Sender, channel}; use servo_config::prefs::PREFS; use servo_url::ServoUrl; use std::collections::HashMap; -use std::str; +use std::ops::Bound; use std::sync::Mutex; use std::sync::atomic::{AtomicBool, Ordering}; +use std::time::SystemTime; use time; -use time::{Duration, Tm}; +use time::{Duration, Timespec, Tm}; /// The key used to differentiate requests in the cache. @@ -59,7 +59,7 @@ impl CacheKey { /// A complete cached resource. #[derive(Clone)] struct CachedResource { - request_headers: Arc<Mutex<Headers>>, + request_headers: Arc<Mutex<HeaderMap>>, body: Arc<Mutex<ResponseBody>>, aborted: Arc<AtomicBool>, awaiting_body: Arc<Mutex<Vec<Sender<Data>>>>, @@ -71,7 +71,7 @@ struct MeasurableCachedResource { metadata: CachedMetadata, location_url: Option<Result<ServoUrl, String>>, https_state: HttpsState, - status: Option<StatusCode>, + status: Option<(StatusCode, String)>, raw_status: Option<(u16, Vec<u8>)>, url_list: Vec<ServoUrl>, expires: Duration, @@ -80,7 +80,7 @@ struct MeasurableCachedResource { impl MallocSizeOf for CachedResource { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.request_headers.unconditional_size_of(ops) + + // TODO: self.request_headers.unconditional_size_of(ops) + self.body.unconditional_size_of(ops) + self.aborted.unconditional_size_of(ops) + self.awaiting_body.unconditional_size_of(ops) + @@ -92,7 +92,7 @@ impl MallocSizeOf for CachedResource { #[derive(Clone)] struct CachedMetadata { /// Headers - pub headers: Arc<Mutex<Headers>>, + pub headers: Arc<Mutex<HeaderMap>>, /// Fields that implement MallocSizeOf pub data: Measurable<MeasurableCachedMetadata> } @@ -102,7 +102,7 @@ struct MeasurableCachedMetadata { /// Final URL after redirects. pub final_url: ServoUrl, /// MIME type / subtype. - pub content_type: Option<Serde<ContentType>>, + pub content_type: Option<String>, /// Character set. pub charset: Option<String>, /// HTTP Status @@ -112,7 +112,7 @@ struct MeasurableCachedMetadata { impl MallocSizeOf for CachedMetadata { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { self.headers.unconditional_shallow_size_of(ops) + - self.headers.size_of(ops) + + // TODO: self.headers.size_of(ops) + self.data.size_of(ops) } } @@ -141,23 +141,25 @@ fn response_is_cacheable(metadata: &Metadata) -> bool { // 2. check for absence of the Authorization header field. let mut is_cacheable = false; let headers = metadata.headers.as_ref().unwrap(); - if headers.has::<header::Expires>() || - headers.has::<header::LastModified>() || - headers.has::<header::ETag>() { + if headers.contains_key(header::EXPIRES) || + headers.contains_key(header::LAST_MODIFIED) || + headers.contains_key(header::ETAG) { is_cacheable = true; } - if let Some(&header::CacheControl(ref directive)) = headers.get::<header::CacheControl>() { - for directive in directive.iter() { - match *directive { - header::CacheDirective::NoStore => return false, - header::CacheDirective::Public | header::CacheDirective::SMaxAge(_) - | header::CacheDirective::MaxAge(_) | header::CacheDirective::NoCache => is_cacheable = true, - _ => {}, - } + if let Some(ref directive) = headers.typed_get::<CacheControl>() { + if directive.no_store() { + return false + } + if directive.public() || directive.s_max_age().is_some() || + directive.max_age().is_some() || directive.no_cache() + { + is_cacheable = true; } } - if let Some(&header::Pragma::NoCache) = headers.get::<header::Pragma>() { - return false; + if let Some(pragma) = headers.typed_get::<Pragma>() { + if pragma.is_no_cache() { + return false; + } } is_cacheable } @@ -166,10 +168,11 @@ fn response_is_cacheable(metadata: &Metadata) -> bool { /// <https://tools.ietf.org/html/rfc7234#section-4.2.3> fn calculate_response_age(response: &Response) -> Duration { // TODO: follow the spec more closely (Date headers, request/response lag, ...) - if let Some(secs) = response.headers.get_raw("Age") { - let seconds_string = String::from_utf8_lossy(&secs[0]); - if let Ok(secs) = seconds_string.parse::<i64>() { - return Duration::seconds(secs); + 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::seconds(secs); + } } } Duration::seconds(0i64) @@ -180,42 +183,37 @@ fn calculate_response_age(response: &Response) -> Duration { fn get_response_expiry(response: &Response) -> Duration { // Calculating Freshness Lifetime <https://tools.ietf.org/html/rfc7234#section-4.2.1> let age = calculate_response_age(&response); - if let Some(&header::CacheControl(ref directives)) = response.headers.get::<header::CacheControl>() { - let has_no_cache_directive = directives.iter().any(|directive| { - header::CacheDirective::NoCache == *directive - }); - if has_no_cache_directive { + if let Some(directives) = response.headers.typed_get::<CacheControl>() { + if directives.no_cache() { // Requires validation on first use. return Duration::seconds(0i64); } else { - for directive in directives { - match *directive { - header::CacheDirective::SMaxAge(secs) | header::CacheDirective::MaxAge(secs) => { - let max_age = Duration::seconds(secs as i64); - if max_age < age { - return Duration::seconds(0i64); - } - return max_age - age; - }, - _ => (), + 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; } } } - if let Some(&header::Expires(header::HttpDate(t))) = response.headers.get::<header::Expires>() { - // store the period of time from now until expiry - let desired = t.to_timespec(); - let current = time::now().to_timespec(); - if desired > current { - return desired - current; - } else { - return Duration::seconds(0i64); - } - } else { - if let Some(_) = response.headers.get_raw("Expires") { - // Malformed Expires header, shouldn't be used to construct a valid response. - return Duration::seconds(0i64); - } + match response.headers.typed_get::<Expires>() { + Some(t) => { + // store the period of time from now until expiry + let t: SystemTime = t.into(); + 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(); + + 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::seconds(0i64), + _ => {}, } // Calculating Heuristic Freshness // <https://tools.ietf.org/html/rfc7234#section-4.2.2> @@ -224,13 +222,15 @@ fn get_response_expiry(response: &Response) -> Duration { // 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::hours(24) - age; - let heuristic_freshness = if let Some(&header::LastModified(header::HttpDate(t))) = + 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.get::<header::LastModified>() { - let last_modified = t.to_timespec(); + response.headers.typed_get::<LastModified>() { let current = time::now().to_timespec(); + let last_modified: SystemTime = last_modified.into(); + 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 { @@ -249,11 +249,9 @@ fn get_response_expiry(response: &Response) -> Duration { }, _ => { // Other status codes can only use heuristic freshness if the public cache directive is present. - if let Some(&header::CacheControl(ref directives)) = response.headers.get::<header::CacheControl>() { - let has_public_directive = directives.iter().any(|directive| { - header::CacheDirective::Public == *directive - }); - if has_public_directive { + if let Some(ref directives) = response.headers.typed_get::<CacheControl>() + { + if directives.public() { return heuristic_freshness; } } @@ -267,48 +265,39 @@ fn get_response_expiry(response: &Response) -> Duration { /// Request Cache-Control Directives /// <https://tools.ietf.org/html/rfc7234#section-5.2.1> fn get_expiry_adjustment_from_request_headers(request: &Request, expires: Duration) -> Duration { - let directive_data = match request.headers.get_raw("cache-control") { + let directive = match request.headers.typed_get::<CacheControl>() { Some(data) => data, None => return expires, }; - let directives_string = String::from_utf8_lossy(&directive_data[0]); - for directive in directives_string.split(",") { - let mut directive_info = directive.split("="); - match (directive_info.next(), directive_info.next()) { - (Some("max-stale"), Some(sec_str)) => { - if let Ok(secs) = sec_str.parse::<i64>() { - return expires + Duration::seconds(secs); - } - }, - (Some("max-age"), Some(sec_str)) => { - if let Ok(secs) = sec_str.parse::<i64>() { - let max_age = Duration::seconds(secs); - if expires > max_age { - return Duration::min_value(); - } - return expires - max_age; - } - }, - (Some("min-fresh"), Some(sec_str)) => { - if let Ok(secs) = sec_str.parse::<i64>() { - let min_fresh = Duration::seconds(secs); - if expires < min_fresh { - return Duration::min_value(); - } - return expires - min_fresh; - } - }, - (Some("no-cache"), _) | (Some("no-store"), _) => return Duration::min_value(), - _ => {} + + if let Some(max_age) = directive.max_stale() { + 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::min_value(); } + return expires - max_age; } + if let Some(min_fresh) = directive.min_fresh() { + 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::min_value() + } + expires } /// Create a CachedResponse from a request and a CachedResource. fn create_cached_response(request: &Request, cached_resource: &CachedResource, - cached_headers: &Headers, + cached_headers: &HeaderMap, done_chan: &mut DoneChannel) -> CachedResponse { let mut response = Response::new(cached_resource.data.metadata.data.final_url.clone()); @@ -353,7 +342,7 @@ fn create_resource_with_bytes_from_resource(bytes: &[u8], resource: &CachedResou metadata: resource.data.metadata.clone(), location_url: resource.data.location_url.clone(), https_state: resource.data.https_state.clone(), - status: Some(StatusCode::PartialContent), + status: Some((StatusCode::PARTIAL_CONTENT, "Partial Content".into())), raw_status: Some((206, b"Partial Content".to_vec())), url_list: resource.data.url_list.clone(), expires: resource.data.expires.clone(), @@ -365,7 +354,7 @@ fn create_resource_with_bytes_from_resource(bytes: &[u8], resource: &CachedResou /// Support for range requests <https://tools.ietf.org/html/rfc7233>. fn handle_range_request(request: &Request, candidates: Vec<&CachedResource>, - range_spec: &[header::ByteRangeSpec], + range_spec: Vec<(Bound<u64>, Bound<u64>)>, done_chan: &mut DoneChannel) -> Option<CachedResponse> { let mut complete_cached_resources = candidates.iter().filter(|resource| { @@ -389,7 +378,7 @@ fn handle_range_request(request: &Request, // see <https://tools.ietf.org/html/rfc7233#section-4.3>. // TODO: add support for complete and partial resources, // whose body is in the ResponseBody::Receiving state. - (&header::ByteRangeSpec::FromTo(beginning, end), Some(ref complete_resource)) => { + (&(Bound::Included(beginning), Bound::Included(end)), Some(ref complete_resource)) => { if let ResponseBody::Done(ref body) = *complete_resource.body.lock().unwrap() { let b = beginning as usize; let e = end as usize + 1; @@ -402,14 +391,18 @@ fn handle_range_request(request: &Request, } } }, - (&header::ByteRangeSpec::FromTo(beginning, end), None) => { + (&(Bound::Included(beginning), Bound::Included(end)), None) => { for partial_resource in partial_cached_resources { let headers = partial_resource.data.metadata.headers.lock().unwrap(); - let content_range = headers.get::<header::ContentRange>(); + let content_range = headers.typed_get::<ContentRange>(); let (res_beginning, res_end) = match content_range { - Some(&header::ContentRange( - header::ContentRangeSpec::Bytes { - range: Some((res_beginning, res_end)), .. })) => (res_beginning, res_end), + Some(range) => { + if let Some(bytes_range) = range.bytes_range() { + bytes_range + } else { + continue + } + } _ => continue, }; if res_beginning - 1 < beginning && res_end + 1 > end { @@ -430,7 +423,7 @@ fn handle_range_request(request: &Request, } } }, - (&header::ByteRangeSpec::AllFrom(beginning), Some(ref complete_resource)) => { + (&(Bound::Included(beginning), Bound::Unbounded), Some(ref complete_resource)) => { if let ResponseBody::Done(ref body) = *complete_resource.body.lock().unwrap() { let b = beginning as usize; let requested = body.get(b..); @@ -442,16 +435,17 @@ fn handle_range_request(request: &Request, } } }, - (&header::ByteRangeSpec::AllFrom(beginning), None) => { + (&(Bound::Included(beginning), Bound::Unbounded), None) => { for partial_resource in partial_cached_resources { let headers = partial_resource.data.metadata.headers.lock().unwrap(); - let content_range = headers.get::<header::ContentRange>(); - let (res_beginning, res_end, total) = match content_range { - Some(&header::ContentRange( - header::ContentRangeSpec::Bytes { - range: Some((res_beginning, res_end)), - instance_length: Some(total) })) => (res_beginning, res_end, total), - _ => continue, + let content_range = headers.typed_get::<ContentRange>(); + let (res_beginning, res_end, total) = if let Some(range) = content_range { + match (range.bytes_range(), range.bytes_len()) { + (Some(bytes_range), Some(total)) => (bytes_range.0, bytes_range.1, total), + _ => continue, + } + } else { + continue; }; if res_beginning < beginning && res_end == total - 1 { let resource_body = &*partial_resource.body.lock().unwrap(); @@ -470,7 +464,7 @@ fn handle_range_request(request: &Request, } } }, - (&header::ByteRangeSpec::Last(offset), Some(ref complete_resource)) => { + (&(Bound::Unbounded, Bound::Included(offset)), Some(ref complete_resource)) => { if let ResponseBody::Done(ref body) = *complete_resource.body.lock().unwrap() { let from_byte = body.len() - offset as usize; let requested = body.get(from_byte..); @@ -482,16 +476,17 @@ fn handle_range_request(request: &Request, } } }, - (&header::ByteRangeSpec::Last(offset), None) => { + (&(Bound::Unbounded, Bound::Included(offset)), None) => { for partial_resource in partial_cached_resources { let headers = partial_resource.data.metadata.headers.lock().unwrap(); - let content_range = headers.get::<header::ContentRange>(); - let (res_beginning, res_end, total) = match content_range { - Some(&header::ContentRange( - header::ContentRangeSpec::Bytes { - range: Some((res_beginning, res_end)), - instance_length: Some(total) })) => (res_beginning, res_end, total), - _ => continue, + let content_range = headers.typed_get::<ContentRange>(); + let (res_beginning, res_end, total) = if let Some(range) = content_range { + match (range.bytes_range(), range.bytes_len()) { + (Some(bytes_range), Some(total)) => (bytes_range.0, bytes_range.1, total), + _ => continue, + } + } else { + continue; }; if (total - res_beginning) > (offset - 1 ) && (total - res_end) < offset + 1 { let resource_body = &*partial_resource.body.lock().unwrap(); @@ -509,7 +504,9 @@ fn handle_range_request(request: &Request, } } } - } + }, + // All the cases with Bound::Excluded should be unreachable anyway + _ => return None } None } @@ -527,7 +524,7 @@ impl HttpCache { /// <https://tools.ietf.org/html/rfc7234#section-4> pub fn construct_response(&self, request: &Request, done_chan: &mut DoneChannel) -> Option<CachedResponse> { // TODO: generate warning headers as appropriate <https://tools.ietf.org/html/rfc7234#section-5.5> - if request.method != Method::Get { + if request.method != Method::GET { // Only Get requests are cached, avoid a url based match for others. return None; } @@ -538,41 +535,35 @@ impl HttpCache { let mut can_be_constructed = true; let cached_headers = cached_resource.data.metadata.headers.lock().unwrap(); let original_request_headers = cached_resource.request_headers.lock().unwrap(); - if let Some(vary_data) = cached_headers.get_raw("Vary") { - // Calculating Secondary Keys with Vary <https://tools.ietf.org/html/rfc7234#section-4.1> - let vary_data_string = String::from_utf8_lossy(&vary_data[0]); - let vary_values = vary_data_string.split(",").map(|val| val.trim()); - for vary_val in vary_values { + if let Some(vary_value) = cached_headers.typed_get::<Vary>() { + if vary_value.is_any() { + can_be_constructed = false + } else { // For every header name found in the Vary header of the stored response. - if vary_val == "*" { - // A Vary header field-value of "*" always fails to match. - can_be_constructed = false; - break; - } - match request.headers.get_raw(vary_val) { - Some(header_data) => { - // If the header is present in the request. - let request_header_data_string = String::from_utf8_lossy(&header_data[0]); - if let Some(original_header_data) = original_request_headers.get_raw(vary_val) { - // Check that the value of the nominated header field, - // in the original request, matches the value in the current request. - let original_request_header_data_string = - String::from_utf8_lossy(&original_header_data[0]); - if original_request_header_data_string != request_header_data_string { - can_be_constructed = false; - break; + // Calculating Secondary Keys with Vary <https://tools.ietf.org/html/rfc7234#section-4.1> + for vary_val in vary_value.iter_strs() { + match request.headers.get(vary_val) { + Some(header_data) => { + // If the header is present in the request. + if let Some(original_header_data) = original_request_headers.get(vary_val) { + // Check that the value of the nominated header field, + // in the original request, matches the value in the current request. + if original_header_data != header_data { + can_be_constructed = false; + break; + } } - } - }, - None => { - // If a header field is absent from a request, - // it can only match a stored response if those headers, - // were also absent in the original request. - can_be_constructed = original_request_headers.get_raw(vary_val).is_none(); - }, - } - if !can_be_constructed { - break; + }, + None => { + // If a header field is absent from a request, + // it can only match a stored response if those headers, + // were also absent in the original request. + can_be_constructed = original_request_headers.get(vary_val).is_none(); + }, + } + if !can_be_constructed { + break; + } } } } @@ -581,8 +572,8 @@ impl HttpCache { } } // Support for range requests - if let Some(&header::Range::Bytes(ref range_spec)) = request.headers.get::<header::Range>() { - return handle_range_request(request, candidates, &range_spec, done_chan); + if let Some(range_spec) = request.headers.typed_get::<Range>() { + return handle_range_request(request, candidates, range_spec.iter().collect(), done_chan); } else { // Not a Range request. if let Some(ref cached_resource) = candidates.first() { @@ -620,7 +611,7 @@ impl HttpCache { /// Freshening Stored Responses upon Validation. /// <https://tools.ietf.org/html/rfc7234#section-4.3.4> pub fn refresh(&mut self, request: &Request, response: Response, done_chan: &mut DoneChannel) -> Option<Response> { - assert_eq!(response.status, Some(StatusCode::NotModified)); + assert_eq!(response.status.map(|s| s.0), Some(StatusCode::NOT_MODIFIED)); let entry_key = CacheKey::new(request.clone()); if let Some(cached_resources) = self.entries.get_mut(&entry_key) { for cached_resource in cached_resources.iter_mut() { @@ -654,7 +645,7 @@ impl HttpCache { constructed_response.url_list = cached_resource.data.url_list.clone(); cached_resource.data.expires = get_response_expiry(&constructed_response); let mut stored_headers = cached_resource.data.metadata.headers.lock().unwrap(); - stored_headers.extend(response.headers.iter()); + stored_headers.extend(response.headers); constructed_response.headers = stored_headers.clone(); return Some(constructed_response); } @@ -674,17 +665,16 @@ impl HttpCache { /// Invalidation. /// <https://tools.ietf.org/html/rfc7234#section-4.4> pub fn invalidate(&mut self, request: &Request, response: &Response) { - if let Some(&header::Location(ref location)) = response.headers.get::<header::Location>() { + // TODO(eijebong): Once headers support typed_get, update this to use them + if let Some(Ok(location)) = response.headers.get(header::LOCATION).map(HeaderValue::to_str) { if let Ok(url) = request.current_url().join(location) { self.invalidate_for_url(&url); } } - // TODO: update hyper to use typed getter. - if let Some(url_data) = response.headers.get_raw("Content-Location") { - if let Ok(content_location) = str::from_utf8(&url_data[0]) { - if let Ok(url) = request.current_url().join(content_location) { - self.invalidate_for_url(&url); - } + if let Some(Ok(ref content_location)) = response.headers.get(header::CONTENT_LOCATION).map(HeaderValue::to_str) + { + if let Ok(url) = request.current_url().join(&content_location) { + self.invalidate_for_url(&url); } } self.invalidate_for_url(&request.url()); @@ -696,7 +686,7 @@ impl HttpCache { if PREFS.get("network.http-cache.disabled").as_boolean().unwrap_or(false) { return } - if request.method != Method::Get { + if request.method != Method::GET { // Only Get requests are cached. return } @@ -716,7 +706,7 @@ impl HttpCache { headers: Arc::new(Mutex::new(response.headers.clone())), data: Measurable(MeasurableCachedMetadata { final_url: metadata.final_url, - content_type: metadata.content_type, + content_type: metadata.content_type.map(|v| v.0.to_string()), charset: metadata.charset, status: metadata.status }) |