Add a task-based CORS Cache

This commit is contained in:
Manish Goregaokar 2014-08-23 14:48:10 +05:30
parent a3b5395d50
commit 5d7438a7db
2 changed files with 208 additions and 48 deletions

View file

@ -4,14 +4,13 @@
use http::method::Method;
use std::ascii::StrAsciiExt;
use std::comm::{Sender, Receiver, channel};
use time;
use time::{now, Timespec};
use url::Url;
#[deriving(Clone)]
pub struct BasicCORSCache(Vec<CORSCacheEntry>);
/// Union type for CORS cache entries
///
/// Each entry might pertain to a header or method
#[deriving(Clone)]
pub enum HeaderOrMethod {
@ -61,65 +60,44 @@ impl CORSCacheEntry {
/// Properties of Request required to cache match.
pub struct CacheRequestDetails {
origin: Url,
destination: Url,
pub origin: Url,
pub destination: Url,
pub credentials: bool
}
trait CORSCache {
/// Trait for a generic CORS Cache
pub trait CORSCache {
/// [Clear the cache](http://fetch.spec.whatwg.org/#concept-cache-clear)
fn clear (&mut self, request: &CacheRequestDetails);
fn clear (&mut self, request: CacheRequestDetails);
/// Remove old entries
fn cleanup(&mut self);
/// [Finds an entry with a matching header](http://fetch.spec.whatwg.org/#concept-cache-match-header)
fn find_entry_by_header<'a>(&'a mut self, request: &CacheRequestDetails, header_name: &str) -> Option<&'a mut CORSCacheEntry>;
/// Returns true if an entry with a [matching header](http://fetch.spec.whatwg.org/#concept-cache-match-header) is found
fn match_header(&mut self, request: CacheRequestDetails, header_name: &str) -> bool;
/// Returns true if an entry with a matching header is found
fn match_header(&mut self, request: &CacheRequestDetails, header_name: &str) -> bool {
self.find_entry_by_header(request, header_name).is_some()
}
/// Updates max age if an entry for a [matching header](http://fetch.spec.whatwg.org/#concept-cache-match-header) is found.
///
/// If not, it will insert an equivalent entry
fn match_header_and_update(&mut self, request: CacheRequestDetails, header_name: &str, new_max_age: uint) -> bool;
/// Updates max age if an entry for the same header is found.
fn match_header_and_update(&mut self, request: &CacheRequestDetails, header_name: &str, new_max_age: uint) -> bool {
self.find_entry_by_header(request, header_name).map(|e| e.max_age = new_max_age).is_some()
}
/// [Finds an entry with a matching method](http://fetch.spec.whatwg.org/#concept-cache-match-method)
fn find_entry_by_method<'a>(&'a mut self, request: &CacheRequestDetails, method: &Method) -> Option<&'a mut CORSCacheEntry>;
/// Returns true if an entry with a matching method is found
fn match_method(&mut self, request: &CacheRequestDetails, method: &Method) -> bool {
self.find_entry_by_method(request, method).is_some()
}
/// Updates max age if an entry for the same method is found.
fn match_method_and_update(&mut self, request: &CacheRequestDetails, method: &Method, new_max_age: uint) -> bool {
self.find_entry_by_method(request, method).map(|e| e.max_age = new_max_age).is_some()
}
/// Returns true if an entry with a [matching method](http://fetch.spec.whatwg.org/#concept-cache-match-method) is found
fn match_method(&mut self, request: CacheRequestDetails, method: &Method) -> bool;
/// Updates max age if an entry for [a matching method](http://fetch.spec.whatwg.org/#concept-cache-match-method) is found.
///
/// If not, it will insert an equivalent entry
fn match_method_and_update(&mut self, request: CacheRequestDetails, method: &Method, new_max_age: uint) -> bool;
/// Insert an entry
fn insert(&mut self, entry: CORSCacheEntry);
}
impl CORSCache for BasicCORSCache {
/// http://fetch.spec.whatwg.org/#concept-cache-clear
#[allow(dead_code)]
fn clear (&mut self, request: &CacheRequestDetails) {
let BasicCORSCache(buf) = self.clone();
let new_buf: Vec<CORSCacheEntry> = buf.move_iter().filter(|e| e.origin == request.origin && request.destination == e.url).collect();
*self = BasicCORSCache(new_buf);
}
/// A simple, vector-based CORS Cache
#[deriving(Clone)]
pub struct BasicCORSCache(Vec<CORSCacheEntry>);
// Remove old entries
fn cleanup(&mut self) {
let BasicCORSCache(buf) = self.clone();
let now = time::now().to_timespec();
let new_buf: Vec<CORSCacheEntry> = buf.move_iter().filter(|e| now.sec > e.created.sec + e.max_age as i64).collect();
*self = BasicCORSCache(new_buf);
}
/// http://fetch.spec.whatwg.org/#concept-cache-match-header
impl BasicCORSCache {
fn find_entry_by_header<'a>(&'a mut self, request: &CacheRequestDetails, header_name: &str) -> Option<&'a mut CORSCacheEntry> {
self.cleanup();
let BasicCORSCache(ref mut buf) = *self;
@ -128,6 +106,7 @@ impl CORSCache for BasicCORSCache {
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));
entry
}
@ -141,9 +120,58 @@ impl CORSCache for BasicCORSCache {
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));
entry
}
}
impl CORSCache for BasicCORSCache {
/// http://fetch.spec.whatwg.org/#concept-cache-clear
#[allow(dead_code)]
fn clear (&mut self, request: CacheRequestDetails) {
let BasicCORSCache(buf) = self.clone();
let new_buf: Vec<CORSCacheEntry> = buf.move_iter().filter(|e| e.origin == request.origin && request.destination == e.url).collect();
*self = BasicCORSCache(new_buf);
}
// Remove old entries
fn cleanup(&mut self) {
let BasicCORSCache(buf) = self.clone();
let now = time::now().to_timespec();
let new_buf: Vec<CORSCacheEntry> = buf.move_iter().filter(|e| now.sec > e.created.sec + e.max_age as i64).collect();
*self = BasicCORSCache(new_buf);
}
fn match_header(&mut self, request: CacheRequestDetails, header_name: &str) -> bool {
self.find_entry_by_header(&request, header_name).is_some()
}
fn match_header_and_update(&mut self, request: CacheRequestDetails, header_name: &str, new_max_age: uint) -> 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, request.destination, new_max_age,
request.credentials, HeaderData(header_name.to_string())));
false
}
}
}
fn match_method(&mut self, request: CacheRequestDetails, method: &Method) -> bool {
self.find_entry_by_method(&request, method).is_some()
}
fn match_method_and_update(&mut self, request: CacheRequestDetails, method: &Method, new_max_age: uint) -> bool {
match self.find_entry_by_method(&request, method).map(|e| e.max_age = new_max_age) {
Some(_) => true,
None => {
self.insert(CORSCacheEntry::new(request.origin, request.destination, new_max_age,
request.credentials, MethodData(method.clone())));
false
}
}
}
fn insert(&mut self, entry: CORSCacheEntry) {
self.cleanup();
@ -151,3 +179,132 @@ impl CORSCache for BasicCORSCache {
buf.push(entry);
}
}
/// Various messages that can be sent to a CORSCacheTask
pub enum CORSCacheTaskMsg {
Clear(CacheRequestDetails, Sender<()>),
Cleanup(Sender<()>),
MatchHeader(CacheRequestDetails, String, Sender<bool>),
MatchHeaderUpdate(CacheRequestDetails, String, uint, Sender<bool>),
MatchMethod(CacheRequestDetails, Method, Sender<bool>),
MatchMethodUpdate(CacheRequestDetails, Method, uint, Sender<bool>),
Insert(CORSCacheEntry, Sender<()>)
}
/// A Sender to a CORSCacheTask
///
/// This can be used as a CORS Cache.
/// The methods on this type block until they can run, and it behaves similar to a mutex
pub type CORSCacheSender = Sender<CORSCacheTaskMsg>;
impl CORSCache for CORSCacheSender {
fn clear (&mut self, request: CacheRequestDetails) {
let (tx, rx) = channel();
self.send(Clear(request, tx));
rx.recv_opt().ok();
}
fn cleanup(&mut self) {
let (tx, rx) = channel();
self.send(Cleanup(tx));
rx.recv_opt().ok();
}
fn match_header(&mut self, request: CacheRequestDetails, header_name: &str) -> bool {
let (tx, rx) = channel();
self.send(MatchHeader(request, header_name.to_string(), tx));
rx.recv_opt().ok().unwrap_or(false)
}
fn match_header_and_update(&mut self, request: CacheRequestDetails, header_name: &str, new_max_age: uint) -> bool {
let (tx, rx) = channel();
self.send(MatchHeaderUpdate(request, header_name.to_string(), new_max_age, tx));
rx.recv_opt().ok().unwrap_or(false)
}
fn match_method(&mut self, request: CacheRequestDetails, method: &Method) -> bool {
let (tx, rx) = channel();
self.send(MatchMethod(request, method.clone(), tx));
rx.recv_opt().ok().unwrap_or(false)
}
fn match_method_and_update(&mut self, request: CacheRequestDetails, method: &Method, new_max_age: uint) -> bool {
let (tx, rx) = channel();
self.send(MatchMethodUpdate(request, method.clone(), new_max_age, tx));
rx.recv_opt().ok().unwrap_or(false)
}
fn insert(&mut self, entry: CORSCacheEntry) {
let (tx, rx) = channel();
self.send(Insert(entry, tx));
rx.recv_opt().ok() ;
}
}
/// A simple task-based CORS Cache that can be sent messages
///
/// #Example
/// ```
/// let task = CORSCacheTask::new();
/// let builder = TaskBuilder::new().named("XHRTask");
/// let mut sender = task.get_sender();
/// builder.spawn(proc() { task.run() });
/// sender.insert(CORSCacheEntry::new(/* parameters here */));
/// ```
pub struct CORSCacheTask {
receiver: Receiver<CORSCacheTaskMsg>,
pub cache: BasicCORSCache,
sender: CORSCacheSender
}
impl CORSCacheTask {
pub fn new() -> CORSCacheTask {
let (tx, rx) = channel();
CORSCacheTask {
receiver: rx,
cache: BasicCORSCache(vec![]),
sender: tx
}
}
/// Provides a sender to the cache task
pub fn get_sender(&self) -> CORSCacheSender {
self.sender.clone()
}
/// Runs the cache task
/// This blocks the current task, so it is advised
/// to spawn a new task for this
pub fn run(&mut self) {
loop {
// The recv() here should never fail, we always
// carry a copy of the Sender with us.
match self.receiver.recv() {
Clear(request, tx) => {
self.cache.clear(request);
tx.send(());
},
Cleanup(tx) => {
self.cache.cleanup();
tx.send(());
},
MatchHeader(request, header, tx) => {
tx.send(self.cache.match_header(request, header.as_slice()));
},
MatchHeaderUpdate(request, header, new_max_age, tx) => {
tx.send(self.cache.match_header_and_update(request, header.as_slice(), new_max_age));
},
MatchMethod(request, method, tx) => {
tx.send(self.cache.match_method(request, &method));
},
MatchMethodUpdate(request, method, new_max_age, tx) => {
tx.send(self.cache.match_method_and_update(request, &method, new_max_age));
},
Insert(entry, tx) => {
self.cache.insert(entry);
tx.send(());
}
}
}
}
}

View file

@ -5,6 +5,7 @@
use url::Url;
use http::method::{Get, Method};
use http::headers::request::HeaderCollection;
use fetch::cors_cache::CORSCache;
use fetch::response::Response;
/// A [request context](http://fetch.spec.whatwg.org/#concept-request-context)
@ -77,7 +78,8 @@ pub struct Request {
pub use_url_credentials: bool,
pub manual_redirect: bool,
pub redirect_count: uint,
pub response_tainting: ResponseTainting
pub response_tainting: ResponseTainting,
pub cache: Option<Box<CORSCache>>
}
impl Request {
@ -103,7 +105,8 @@ impl Request {
use_url_credentials: false,
manual_redirect: false,
redirect_count: 0,
response_tainting: Basic
response_tainting: Basic,
cache: None
}
}