New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Http cache: introduce cache entries, prevent cache misses when resource will be fetched #24203
Changes from all commits
File filter...
Jump to…
| @@ -5,6 +5,7 @@ | ||
| use crate::data_loader::decode; | ||
| use crate::fetch::cors_cache::CorsCache; | ||
| use crate::filemanager_thread::{fetch_file_in_chunks, FileManager, FILE_CHUNK_SIZE}; | ||
| use crate::http_cache::HttpCacheEntry; | ||
| use crate::http_loader::{determine_request_referrer, http_fetch, HttpState}; | ||
| use crate::http_loader::{set_default_accept, set_default_accept_language}; | ||
| use crate::subresource_integrity::is_response_integrity_valid; | ||
| @@ -40,6 +41,7 @@ lazy_static! { | ||
|
|
||
| pub type Target<'a> = &'a mut (dyn FetchTaskTarget + Send); | ||
|
|
||
| #[derive(Clone)] | ||
| pub enum Data { | ||
| Payload(Vec<u8>), | ||
| Done, | ||
| @@ -224,6 +226,13 @@ pub fn main_fetch( | ||
| // Step 11. | ||
| // Not applicable: see fetch_async. | ||
|
|
||
| // Get the cache entry corresponding to the url, after potential hsts switch. | ||
| let cache_entry = if let Ok(mut http_cache) = context.state.http_cache.write() { | ||
| http_cache.get_entry(&request) | ||
gterzian
Author
Member
|
||
| } else { | ||
| None | ||
| }; | ||
|
|
||
| // Step 12. | ||
| let mut response = response.unwrap_or_else(|| { | ||
| let current_url = request.current_url(); | ||
| @@ -245,15 +254,15 @@ pub fn main_fetch( | ||
| request.response_tainting = ResponseTainting::Basic; | ||
|
|
||
| // Substep 2. | ||
| scheme_fetch(request, cache, target, done_chan, context) | ||
| scheme_fetch(request, cache, target, done_chan, context, &cache_entry) | ||
| } else if request.mode == RequestMode::SameOrigin { | ||
| Response::network_error(NetworkError::Internal("Cross-origin response".into())) | ||
| } else if request.mode == RequestMode::NoCors { | ||
| // Substep 1. | ||
| request.response_tainting = ResponseTainting::Opaque; | ||
|
|
||
| // Substep 2. | ||
| scheme_fetch(request, cache, target, done_chan, context) | ||
| scheme_fetch(request, cache, target, done_chan, context, &cache_entry) | ||
| } else if !matches!(current_url.scheme(), "http" | "https") { | ||
| Response::network_error(NetworkError::Internal("Non-http scheme".into())) | ||
| } else if request.use_cors_preflight || | ||
| @@ -267,7 +276,15 @@ pub fn main_fetch( | ||
| request.response_tainting = ResponseTainting::CorsTainting; | ||
| // Substep 2. | ||
| let response = http_fetch( | ||
| request, cache, true, true, false, target, done_chan, context, | ||
| request, | ||
| cache, | ||
| true, | ||
| true, | ||
| false, | ||
| target, | ||
| done_chan, | ||
| context, | ||
| &cache_entry, | ||
| ); | ||
| // Substep 3. | ||
| if response.is_network_error() { | ||
| @@ -280,7 +297,15 @@ pub fn main_fetch( | ||
| request.response_tainting = ResponseTainting::CorsTainting; | ||
| // Substep 2. | ||
| http_fetch( | ||
| request, cache, true, false, false, target, done_chan, context, | ||
| request, | ||
| cache, | ||
| true, | ||
| false, | ||
| false, | ||
| target, | ||
| done_chan, | ||
| context, | ||
| &cache_entry, | ||
| ) | ||
| } | ||
| }); | ||
| @@ -455,8 +480,14 @@ pub fn main_fetch( | ||
| // Step 24. | ||
| target.process_response_eof(&response); | ||
|
|
||
| if let Ok(mut http_cache) = context.state.http_cache.write() { | ||
| http_cache.update_awaiting_consumers(&request, &response); | ||
| // Note: we need to get an entry again, to take potential re-directs into account. | ||
| let cache_entry = if let Ok(mut http_cache) = context.state.http_cache.write() { | ||
| http_cache.get_entry(&request) | ||
| } else { | ||
| None | ||
| }; | ||
| if let Some(entry) = cache_entry { | ||
| entry.update_awaiting_consumers(&request, &response); | ||
| } | ||
|
|
||
| // Steps 25-27. | ||
| @@ -477,7 +508,7 @@ fn wait_for_response(response: &mut Response, target: Target, done_chan: &mut Do | ||
| }, | ||
| Data::Done => break, | ||
| Data::Cancelled => { | ||
| response.aborted.store(true, Ordering::Relaxed); | ||
| response.aborted.store(true, Ordering::Release); | ||
| break; | ||
| }, | ||
| } | ||
| @@ -570,6 +601,7 @@ fn scheme_fetch( | ||
| target: Target, | ||
| done_chan: &mut DoneChannel, | ||
| context: &FetchContext, | ||
| cache_entry: &Option<HttpCacheEntry>, | ||
| ) -> Response { | ||
| let url = request.current_url(); | ||
|
|
||
| @@ -586,7 +618,15 @@ fn scheme_fetch( | ||
| }, | ||
|
|
||
| "http" | "https" => http_fetch( | ||
| request, cache, false, false, false, target, done_chan, context, | ||
| request, | ||
| cache, | ||
| false, | ||
| false, | ||
| false, | ||
| target, | ||
| done_chan, | ||
| context, | ||
| cache_entry, | ||
| ), | ||
|
|
||
| "data" => match decode(&url) { | ||
Hmm, is this spec-complaint? Shouldn't anything that touches the cache be in
http_network_or_cache_fetch?