62 lines
1.6 KiB
Rust
62 lines
1.6 KiB
Rust
|
use iron::headers;
|
||
|
use std::collections::HashMap;
|
||
|
|
||
|
#[derive(Clone, Debug)]
|
||
|
pub struct Cookie {
|
||
|
pub name: String,
|
||
|
pub value: String,
|
||
|
}
|
||
|
|
||
|
impl From<&str> for Cookie {
|
||
|
fn from(s: &str) -> Cookie {
|
||
|
let parts: Vec<&str> = s.split("=").collect();
|
||
|
Cookie {
|
||
|
name: String::from(parts[0]),
|
||
|
value: String::from(parts[1]),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<&String> for Cookie {
|
||
|
fn from(s: &String) -> Cookie {
|
||
|
Cookie::from(s.as_str())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<String> for Cookie {
|
||
|
fn from(s: String) -> Cookie {
|
||
|
Cookie::from(s.as_str())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
pub struct CookieJar(HashMap<String, Cookie>);
|
||
|
|
||
|
impl CookieJar {
|
||
|
pub fn new() -> CookieJar {
|
||
|
CookieJar(HashMap::new())
|
||
|
}
|
||
|
|
||
|
pub fn add_cookie(&mut self, name: String, value: Cookie) {
|
||
|
self.0.insert(name, value);
|
||
|
}
|
||
|
|
||
|
pub fn lookup(&self, name: &str) -> Option<&Cookie> {
|
||
|
self.0.get(name)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Some(Cookie(["auth=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJhYzNhNDZjNi0zZmExLTRkMGEtYWYxMi1lN2QzZmVmZGM4NzgiLCJhdWQiOiJzYXZhbm5pIiwiZXhwIjoxNjIxMzUxNDM2LCJpc3MiOiJzYXZhbm5pIiwiaWF0IjoxNTg5NzI5MDM2LCJzdWIiOiJodHRwczovL3NhdmFubmkubHVtaW5lc2NlbnQtZHJlYW1zLmNvbS9maWxlLXNlcnZpY2UvIiwicGVybXMiOlsiYWRtaW4iXX0.8zjAbZ7Ut0d6EcDeyik39GKhXvH4qkMDdaiQVNKWiuM"]))
|
||
|
impl From<&headers::Cookie> for CookieJar {
|
||
|
fn from(c: &headers::Cookie) -> CookieJar {
|
||
|
let jar = CookieJar::new();
|
||
|
|
||
|
let headers::Cookie(cs) = c;
|
||
|
cs.iter().fold(jar, |mut jar, c_| {
|
||
|
let cookie = Cookie::from(c_);
|
||
|
jar.add_cookie(cookie.name.clone(), cookie);
|
||
|
jar
|
||
|
})
|
||
|
}
|
||
|
}
|