mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
DRY out conditional logic in CORS header matching
This commit is contained in:
parent
81e034885b
commit
e3aabd0bc5
1 changed files with 10 additions and 12 deletions
|
@ -108,17 +108,20 @@ pub trait CORSCache {
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct BasicCORSCache(Vec<CORSCacheEntry>);
|
pub struct BasicCORSCache(Vec<CORSCacheEntry>);
|
||||||
|
|
||||||
|
fn match_headers(cors_cache: &CORSCacheEntry, cors_req: &CacheRequestDetails) -> bool {
|
||||||
|
cors_cache.origin.scheme == cors_req.origin.scheme &&
|
||||||
|
cors_cache.origin.host() == cors_req.origin.host() &&
|
||||||
|
cors_cache.origin.port() == cors_req.origin.port() &&
|
||||||
|
cors_cache.url == cors_req.destination &&
|
||||||
|
cors_cache.credentials == cors_req.credentials
|
||||||
|
}
|
||||||
|
|
||||||
impl BasicCORSCache {
|
impl BasicCORSCache {
|
||||||
fn find_entry_by_header<'a>(&'a mut self, request: &CacheRequestDetails,
|
fn find_entry_by_header<'a>(&'a mut self, request: &CacheRequestDetails,
|
||||||
header_name: &str) -> Option<&'a mut CORSCacheEntry> {
|
header_name: &str) -> Option<&'a mut CORSCacheEntry> {
|
||||||
self.cleanup();
|
self.cleanup();
|
||||||
let BasicCORSCache(ref mut buf) = *self;
|
let BasicCORSCache(ref mut buf) = *self;
|
||||||
buf.iter_mut().find(|e| e.origin.scheme == request.origin.scheme &&
|
buf.iter_mut().find(|e| match_headers(e, request) && e.header_or_method.match_header(header_name))
|
||||||
e.origin.host() == request.origin.host() &&
|
|
||||||
e.origin.port() == request.origin.port() &&
|
|
||||||
e.url == request.destination &&
|
|
||||||
e.credentials == request.credentials &&
|
|
||||||
e.header_or_method.match_header(header_name))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_entry_by_method<'a>(&'a mut self, request: &CacheRequestDetails,
|
fn find_entry_by_method<'a>(&'a mut self, request: &CacheRequestDetails,
|
||||||
|
@ -126,12 +129,7 @@ impl BasicCORSCache {
|
||||||
// we can take the method from CORSRequest itself
|
// we can take the method from CORSRequest itself
|
||||||
self.cleanup();
|
self.cleanup();
|
||||||
let BasicCORSCache(ref mut buf) = *self;
|
let BasicCORSCache(ref mut buf) = *self;
|
||||||
buf.iter_mut().find(|e| e.origin.scheme == request.origin.scheme &&
|
buf.iter_mut().find(|e| match_headers(e, request) && e.header_or_method.match_method(&method))
|
||||||
e.origin.host() == request.origin.host() &&
|
|
||||||
e.origin.port() == request.origin.port() &&
|
|
||||||
e.url == request.destination &&
|
|
||||||
e.credentials == request.credentials &&
|
|
||||||
e.header_or_method.match_method(&method))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue