mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
cookies and cookies storage implementation
This commit is contained in:
parent
e14c569ed0
commit
3239aeacdc
13 changed files with 6441 additions and 48 deletions
46
components/net/cookie_storage.rs
Normal file
46
components/net/cookie_storage.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
use url::Url;
|
||||
use cookie::Cookie;
|
||||
|
||||
pub struct CookieStorage {
|
||||
cookies: Vec<Cookie>
|
||||
}
|
||||
|
||||
impl CookieStorage {
|
||||
pub fn new() -> CookieStorage {
|
||||
CookieStorage {
|
||||
cookies: Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, cookie: Cookie) {
|
||||
match self.cookies.iter().position(|c| c.domain == cookie.domain && c.path == cookie.path && c.name == cookie.name) {
|
||||
Some(ind) => { self.cookies.remove(ind); }
|
||||
None => {}
|
||||
};
|
||||
|
||||
self.cookies.push(cookie);
|
||||
}
|
||||
|
||||
pub fn cookies_for_url(&mut self, url: Url) -> Option<String> {
|
||||
let filterer = |&:c: &&mut Cookie| -> bool {
|
||||
error!(" === SENT COOKIE : {} {} {} {}", c.name, c.value, c.domain, c.path);
|
||||
error!(" === SENT COOKIE RESULT {}", c.appropriate_for_url(url.clone()));
|
||||
c.appropriate_for_url(url.clone())
|
||||
};
|
||||
let mut url_cookies = self.cookies.iter_mut().filter(filterer);
|
||||
let reducer = |&:acc: String, c: &mut Cookie| -> String {
|
||||
c.touch();
|
||||
(match acc.len() {
|
||||
0 => acc,
|
||||
_ => acc + ";"
|
||||
}) + c.name.as_slice() + "=" + c.value.as_slice()
|
||||
};
|
||||
let result = url_cookies.fold("".to_string(), reducer);
|
||||
|
||||
error!(" === COOKIES SENT: {}", result);
|
||||
match result.len() {
|
||||
0 => None,
|
||||
_ => Some(result)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue