Use serde_json to persist cookies in the net crate

This commit is contained in:
Anthony Ramine 2017-02-22 12:33:10 +01:00
parent ec5ed8edfd
commit fd9cd33892
7 changed files with 45 additions and 27 deletions

View file

@ -30,6 +30,9 @@ openssl = "0.7.6"
openssl-verify = "0.1"
profile_traits = {path = "../profile_traits"}
rustc-serialize = "0.3"
serde = "0.9"
serde_derive = "0.9"
serde_json = "0.9"
servo_config = {path = "../config"}
servo_url = {path = "../url"}
threadpool = "1.0"

View file

@ -6,6 +6,7 @@
//! http://tools.ietf.org/html/rfc6265
use cookie_rs;
use hyper_serde::{self, Serde};
use net_traits::CookieSource;
use net_traits::pub_domains::is_pub_domain;
use servo_url::ServoUrl;
@ -16,14 +17,20 @@ use time::{Tm, now, at, Duration};
/// A stored cookie that wraps the definition in cookie-rs. This is used to implement
/// various behaviours defined in the spec that rely on an associated request URL,
/// which cookie-rs and hyper's header parsing do not support.
#[derive(Clone, Debug, RustcDecodable, RustcEncodable)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Cookie {
#[serde(deserialize_with = "hyper_serde::deserialize",
serialize_with = "hyper_serde::serialize")]
pub cookie: cookie_rs::Cookie,
pub host_only: bool,
pub persistent: bool,
#[serde(deserialize_with = "hyper_serde::deserialize",
serialize_with = "hyper_serde::serialize")]
pub creation_time: Tm,
#[serde(deserialize_with = "hyper_serde::deserialize",
serialize_with = "hyper_serde::serialize")]
pub last_access: Tm,
pub expiry_time: Option<Tm>,
pub expiry_time: Option<Serde<Tm>>,
}
impl Cookie {
@ -85,7 +92,7 @@ impl Cookie {
persistent: persistent,
creation_time: now(),
last_access: now(),
expiry_time: expiry_time,
expiry_time: expiry_time.map(Serde),
})
}

View file

@ -16,7 +16,7 @@ use time::Tm;
extern crate time;
#[derive(Clone, Debug, RustcDecodable, RustcEncodable)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CookieStorage {
version: u32,
cookies_map: HashMap<String, Vec<Cookie>>,
@ -192,7 +192,7 @@ fn reg_host<'a>(url: &'a str) -> String {
fn is_cookie_expired(cookie: &Cookie) -> bool {
match cookie.expiry_time {
Some(t) => t.to_timespec() <= time::get_time(),
Some(ref t) => t.to_timespec() <= time::get_time(),
None => false,
}
}

View file

@ -4,7 +4,7 @@
use net_traits::IncludeSubdomains;
use net_traits::pub_domains::reg_suffix;
use rustc_serialize::json::decode;
use serde_json;
use servo_config::resource_files::read_resource_file;
use std::collections::HashMap;
use std::net::{Ipv4Addr, Ipv6Addr};
@ -12,7 +12,7 @@ use std::str::from_utf8;
use time;
use url::Url;
#[derive(RustcDecodable, RustcEncodable, Clone)]
#[derive(Clone, Deserialize, Serialize)]
pub struct HstsEntry {
pub host: String,
pub include_subdomains: bool,
@ -53,7 +53,7 @@ impl HstsEntry {
}
}
#[derive(RustcDecodable, RustcEncodable, Clone)]
#[derive(Clone, Deserialize, Serialize)]
pub struct HstsList {
pub entries_map: HashMap<String, Vec<HstsEntry>>,
}
@ -65,14 +65,14 @@ impl HstsList {
/// Create an `HstsList` from the bytes of a JSON preload file.
pub fn from_preload(preload_content: &[u8]) -> Option<HstsList> {
#[derive(RustcDecodable)]
#[derive(Deserialize)]
struct HstsEntries {
entries: Vec<HstsEntry>,
}
let hsts_entries: Option<HstsEntries> = from_utf8(&preload_content)
.ok()
.and_then(|c| decode(c).ok());
.and_then(|c| serde_json::from_str(c).ok());
hsts_entries.map_or(None, |hsts_entries| {
let mut hsts_list: HstsList = HstsList::new();

View file

@ -27,6 +27,10 @@ extern crate openssl;
extern crate openssl_verify;
extern crate profile_traits;
extern crate rustc_serialize;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate servo_config;
extern crate servo_url;
extern crate threadpool;

View file

@ -23,8 +23,8 @@ use net_traits::{ResourceThreads, WebSocketCommunicate, WebSocketConnectData};
use net_traits::request::{Request, RequestInit};
use net_traits::storage_thread::StorageThreadMsg;
use profile_traits::time::ProfilerChan;
use rustc_serialize::{Decodable, Encodable};
use rustc_serialize::json;
use serde::{Deserialize, Serialize};
use serde_json;
use servo_url::ServoUrl;
use std::borrow::{Cow, ToOwned};
use std::collections::HashMap;
@ -211,7 +211,7 @@ impl ResourceChannelManager {
}
pub fn read_json_from_file<T>(data: &mut T, config_dir: &Path, filename: &str)
where T: Decodable
where T: Deserialize
{
let path = config_dir.join(filename);
let display = path.display();
@ -233,17 +233,17 @@ pub fn read_json_from_file<T>(data: &mut T, config_dir: &Path, filename: &str)
Ok(_) => println!("successfully read from {}", display),
}
match json::decode(&string_buffer) {
match serde_json::from_str(&string_buffer) {
Ok(decoded_buffer) => *data = decoded_buffer,
Err(why) => warn!("Could not decode buffer{}", why),
}
}
pub fn write_json_to_file<T>(data: &T, config_dir: &Path, filename: &str)
where T: Encodable
where T: Serialize
{
let json_encoded: String;
match json::encode(&data) {
match serde_json::to_string_pretty(&data) {
Ok(d) => json_encoded = d,
Err(_) => return,
}
@ -266,7 +266,7 @@ pub fn write_json_to_file<T>(data: &T, config_dir: &Path, filename: &str)
}
}
#[derive(RustcDecodable, RustcEncodable, Clone)]
#[derive(Clone, Deserialize, Serialize)]
pub struct AuthCacheEntry {
pub user_name: String,
pub password: String,
@ -281,7 +281,7 @@ impl AuthCache {
}
}
#[derive(RustcDecodable, RustcEncodable, Clone)]
#[derive(Clone, Deserialize, Serialize)]
pub struct AuthCache {
pub version: u32,
pub entries: HashMap<String, AuthCacheEntry>,