mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Rustfmt net crate
This commit is contained in:
parent
ba1ed11ced
commit
2481ad25f8
30 changed files with 4957 additions and 2870 deletions
|
@ -21,21 +21,21 @@ use time::{self, Timespec};
|
|||
#[derive(Clone, Debug)]
|
||||
pub enum HeaderOrMethod {
|
||||
HeaderData(HeaderName),
|
||||
MethodData(Method)
|
||||
MethodData(Method),
|
||||
}
|
||||
|
||||
impl HeaderOrMethod {
|
||||
fn match_header(&self, header_name: &HeaderName) -> bool {
|
||||
match *self {
|
||||
HeaderOrMethod::HeaderData(ref n) => n == header_name,
|
||||
_ => false
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn match_method(&self, method: &Method) -> bool {
|
||||
match *self {
|
||||
HeaderOrMethod::MethodData(ref m) => m == method,
|
||||
_ => false
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -48,19 +48,24 @@ pub struct CorsCacheEntry {
|
|||
pub max_age: u32,
|
||||
pub credentials: bool,
|
||||
pub header_or_method: HeaderOrMethod,
|
||||
created: Timespec
|
||||
created: Timespec,
|
||||
}
|
||||
|
||||
impl CorsCacheEntry {
|
||||
fn new(origin: Origin, url: ServoUrl, max_age: u32, credentials: bool,
|
||||
header_or_method: HeaderOrMethod) -> CorsCacheEntry {
|
||||
fn new(
|
||||
origin: Origin,
|
||||
url: ServoUrl,
|
||||
max_age: u32,
|
||||
credentials: bool,
|
||||
header_or_method: HeaderOrMethod,
|
||||
) -> CorsCacheEntry {
|
||||
CorsCacheEntry {
|
||||
origin: origin,
|
||||
url: url,
|
||||
max_age: max_age,
|
||||
credentials: credentials,
|
||||
header_or_method: header_or_method,
|
||||
created: time::now().to_timespec()
|
||||
created: time::now().to_timespec(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -80,25 +85,36 @@ impl CorsCache {
|
|||
CorsCache(vec![])
|
||||
}
|
||||
|
||||
fn find_entry_by_header<'a>(&'a mut self, request: &Request,
|
||||
header_name: &HeaderName) -> Option<&'a mut CorsCacheEntry> {
|
||||
fn find_entry_by_header<'a>(
|
||||
&'a mut self,
|
||||
request: &Request,
|
||||
header_name: &HeaderName,
|
||||
) -> Option<&'a mut CorsCacheEntry> {
|
||||
self.cleanup();
|
||||
self.0.iter_mut().find(|e| match_headers(e, request) && e.header_or_method.match_header(header_name))
|
||||
self.0
|
||||
.iter_mut()
|
||||
.find(|e| match_headers(e, request) && e.header_or_method.match_header(header_name))
|
||||
}
|
||||
|
||||
fn find_entry_by_method<'a>(&'a mut self, request: &Request,
|
||||
method: Method) -> Option<&'a mut CorsCacheEntry> {
|
||||
fn find_entry_by_method<'a>(
|
||||
&'a mut self,
|
||||
request: &Request,
|
||||
method: Method,
|
||||
) -> Option<&'a mut CorsCacheEntry> {
|
||||
// we can take the method from CorSRequest itself
|
||||
self.cleanup();
|
||||
self.0.iter_mut().find(|e| match_headers(e, request) && e.header_or_method.match_method(&method))
|
||||
self.0
|
||||
.iter_mut()
|
||||
.find(|e| match_headers(e, request) && e.header_or_method.match_method(&method))
|
||||
}
|
||||
|
||||
/// [Clear the cache](https://fetch.spec.whatwg.org/#concept-cache-clear)
|
||||
pub fn clear(&mut self, request: &Request) {
|
||||
let CorsCache(buf) = self.clone();
|
||||
let new_buf: Vec<CorsCacheEntry> =
|
||||
buf.into_iter().filter(|e| e.origin == request.origin &&
|
||||
request.current_url() == e.url).collect();
|
||||
let new_buf: Vec<CorsCacheEntry> = buf
|
||||
.into_iter()
|
||||
.filter(|e| e.origin == request.origin && request.current_url() == e.url)
|
||||
.collect();
|
||||
*self = CorsCache(new_buf);
|
||||
}
|
||||
|
||||
|
@ -106,9 +122,10 @@ impl CorsCache {
|
|||
pub fn cleanup(&mut self) {
|
||||
let CorsCache(buf) = self.clone();
|
||||
let now = time::now().to_timespec();
|
||||
let new_buf: Vec<CorsCacheEntry> = buf.into_iter()
|
||||
.filter(|e| now.sec < e.created.sec + e.max_age as i64)
|
||||
.collect();
|
||||
let new_buf: Vec<CorsCacheEntry> = buf
|
||||
.into_iter()
|
||||
.filter(|e| now.sec < e.created.sec + e.max_age as i64)
|
||||
.collect();
|
||||
*self = CorsCache(new_buf);
|
||||
}
|
||||
|
||||
|
@ -122,16 +139,27 @@ impl CorsCache {
|
|||
/// [matching header](https://fetch.spec.whatwg.org/#concept-cache-match-header) is found.
|
||||
///
|
||||
/// If not, it will insert an equivalent entry
|
||||
pub fn match_header_and_update(&mut self, request: &Request,
|
||||
header_name: &HeaderName, new_max_age: u32) -> bool {
|
||||
match self.find_entry_by_header(&request, header_name).map(|e| e.max_age = new_max_age) {
|
||||
pub fn match_header_and_update(
|
||||
&mut self,
|
||||
request: &Request,
|
||||
header_name: &HeaderName,
|
||||
new_max_age: u32,
|
||||
) -> bool {
|
||||
match self
|
||||
.find_entry_by_header(&request, header_name)
|
||||
.map(|e| e.max_age = new_max_age)
|
||||
{
|
||||
Some(_) => true,
|
||||
None => {
|
||||
self.insert(CorsCacheEntry::new(request.origin.clone(), request.current_url(), new_max_age,
|
||||
request.credentials_mode == CredentialsMode::Include,
|
||||
HeaderOrMethod::HeaderData(header_name.clone())));
|
||||
self.insert(CorsCacheEntry::new(
|
||||
request.origin.clone(),
|
||||
request.current_url(),
|
||||
new_max_age,
|
||||
request.credentials_mode == CredentialsMode::Include,
|
||||
HeaderOrMethod::HeaderData(header_name.clone()),
|
||||
));
|
||||
false
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -145,15 +173,27 @@ impl CorsCache {
|
|||
/// [a matching method](https://fetch.spec.whatwg.org/#concept-cache-match-method) is found.
|
||||
///
|
||||
/// If not, it will insert an equivalent entry
|
||||
pub fn match_method_and_update(&mut self, request: &Request, method: Method, new_max_age: u32) -> bool {
|
||||
match self.find_entry_by_method(&request, method.clone()).map(|e| e.max_age = new_max_age) {
|
||||
pub fn match_method_and_update(
|
||||
&mut self,
|
||||
request: &Request,
|
||||
method: Method,
|
||||
new_max_age: u32,
|
||||
) -> bool {
|
||||
match self
|
||||
.find_entry_by_method(&request, method.clone())
|
||||
.map(|e| e.max_age = new_max_age)
|
||||
{
|
||||
Some(_) => true,
|
||||
None => {
|
||||
self.insert(CorsCacheEntry::new(request.origin.clone(), request.current_url(), new_max_age,
|
||||
request.credentials_mode == CredentialsMode::Include,
|
||||
HeaderOrMethod::MethodData(method)));
|
||||
self.insert(CorsCacheEntry::new(
|
||||
request.origin.clone(),
|
||||
request.current_url(),
|
||||
new_max_age,
|
||||
request.credentials_mode == CredentialsMode::Include,
|
||||
HeaderOrMethod::MethodData(method),
|
||||
));
|
||||
false
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue