Read cookie as Vec<u8> (instead of String)

This commit is contained in:
Roman Zeyde 2018-07-28 18:12:13 +03:00
parent 6bb837177c
commit 9ee92c823f
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
2 changed files with 9 additions and 16 deletions

View File

@ -161,7 +161,7 @@ impl Config {
pub fn cookie_getter(&self) -> Arc<CookieGetter> {
if let Some(ref value) = self.cookie {
Arc::new(StaticCookie {
value: value.clone(),
value: value.as_bytes().to_vec(),
})
} else {
Arc::new(CookieFile {
@ -172,11 +172,11 @@ impl Config {
}
struct StaticCookie {
value: String,
value: Vec<u8>,
}
impl CookieGetter for StaticCookie {
fn get(&self) -> Result<String> {
fn get(&self) -> Result<Vec<u8>> {
Ok(self.value.clone())
}
}
@ -186,17 +186,10 @@ struct CookieFile {
}
impl CookieGetter for CookieFile {
fn get(&self) -> Result<String> {
read_cookie(&self.daemon_dir)
.chain_err(|| ErrorKind::Connection("no cookie found".to_owned()))
fn get(&self) -> Result<Vec<u8>> {
let path = self.daemon_dir.join(".cookie");
let contents = fs::read(&path)
.chain_err(|| ErrorKind::Connection(format!("failed to read cookie from {:?}", path)))?;
Ok(contents)
}
}
fn read_cookie(daemon_dir: &Path) -> Result<String> {
let mut path = daemon_dir.to_path_buf();
path.push(".cookie");
let contents = String::from_utf8(
fs::read(&path).chain_err(|| format!("failed to read cookie from {:?}", path))?
).chain_err(|| "invalid cookie string")?;
Ok(contents.trim().to_owned())
}

View File

@ -128,7 +128,7 @@ impl MempoolEntry {
}
pub trait CookieGetter: Send + Sync {
fn get(&self) -> Result<String>;
fn get(&self) -> Result<Vec<u8>>;
}
struct Connection {