Unify the get/set scookie APIs to make them both deal with raw strings.

This commit is contained in:
Josh Matthews 2015-02-01 11:51:27 +01:00
parent 504094aa84
commit 824709f178
2 changed files with 17 additions and 10 deletions

View file

@ -11,7 +11,7 @@ use log;
use std::collections::HashSet; use std::collections::HashSet;
use file_loader; use file_loader;
use hyper::client::Request; use hyper::client::Request;
use hyper::header::common::{ContentLength, ContentType, Host, Location, SetCookie}; use hyper::header::common::{ContentLength, ContentType, Host, Location};
use hyper::HttpError; use hyper::HttpError;
use hyper::method::Method; use hyper::method::Method;
use hyper::net::HttpConnector; use hyper::net::HttpConnector;
@ -189,9 +189,14 @@ reason: \"certificate verify failed\" }]";
} }
} }
if let Some(&SetCookie(ref cookies)) = response.headers.get::<SetCookie>() { if let Some(cookies) = response.headers.get_raw("set-cookie") {
cookies_chan.send(ControlMsg::SetCookies(cookies.clone(), url.clone(), for cookie in cookies.iter() {
CookieSource::HTTP)); if let Ok(cookies) = String::from_utf8(cookie.clone()) {
cookies_chan.send(ControlMsg::SetCookiesForUrl(url.clone(),
cookies,
CookieSource::HTTP));
}
}
} }
if response.status.class() == StatusClass::Redirection { if response.status.class() == StatusClass::Redirection {

View file

@ -10,14 +10,13 @@ use file_loader;
use http_loader; use http_loader;
use sniffer_task; use sniffer_task;
use sniffer_task::SnifferTask; use sniffer_task::SnifferTask;
use cookie_rs::Cookie;
use cookie_storage::{CookieStorage, CookieSource}; use cookie_storage::{CookieStorage, CookieSource};
use cookie; use cookie;
use util::task::spawn_named; use util::task::spawn_named;
use hyper::header::common::UserAgent; use hyper::header::common::UserAgent;
use hyper::header::Headers; use hyper::header::{Headers, Header, SetCookie};
use hyper::http::RawStatus; use hyper::http::RawStatus;
use hyper::method::Method; use hyper::method::Method;
use hyper::mime::{Mime, Attr}; use hyper::mime::{Mime, Attr};
@ -31,7 +30,7 @@ pub enum ControlMsg {
/// Request the data associated with a particular URL /// Request the data associated with a particular URL
Load(LoadData), Load(LoadData),
/// Store a set of cookies for a given originating URL /// Store a set of cookies for a given originating URL
SetCookies(Vec<Cookie>, Url, CookieSource), SetCookiesForUrl(Url, String, CookieSource),
/// Retrieve the stored cookies for a given URL /// Retrieve the stored cookies for a given URL
GetCookiesForUrl(Url, Sender<Option<String>>, CookieSource), GetCookiesForUrl(Url, Sender<Option<String>>, CookieSource),
Exit Exit
@ -229,11 +228,14 @@ impl ResourceManager {
ControlMsg::Load(load_data) => { ControlMsg::Load(load_data) => {
self.load(load_data) self.load(load_data)
} }
ControlMsg::SetCookies(vector, request, source) => { ControlMsg::SetCookiesForUrl(request, cookies, source) => {
for cookie in vector.into_iter() { let header = Header::parse_header([cookies.into_bytes()].as_slice());
if let Some(SetCookie(cookies)) = header {
for cookie in cookies.into_iter() {
if let Some(cookie) = cookie::Cookie::new_wrapped(cookie, &request, source) { if let Some(cookie) = cookie::Cookie::new_wrapped(cookie, &request, source) {
self.cookie_storage.push(cookie, source); self.cookie_storage.push(cookie, source);
} }
}
} }
} }
ControlMsg::GetCookiesForUrl(url, consumer, source) => { ControlMsg::GetCookiesForUrl(url, consumer, source) => {