Handle missing cookie file as connection failure

This commit is contained in:
Roman Zeyde 2018-07-28 17:50:59 +03:00
parent 6efab9c8a2
commit 6bb837177c
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
2 changed files with 8 additions and 7 deletions

View File

@ -176,8 +176,8 @@ struct StaticCookie {
} }
impl CookieGetter for StaticCookie { impl CookieGetter for StaticCookie {
fn get(&self) -> String { fn get(&self) -> Result<String> {
self.value.clone() Ok(self.value.clone())
} }
} }
@ -186,8 +186,9 @@ struct CookieFile {
} }
impl CookieGetter for CookieFile { impl CookieGetter for CookieFile {
fn get(&self) -> String { fn get(&self) -> Result<String> {
read_cookie(&self.daemon_dir).unwrap() read_cookie(&self.daemon_dir)
.chain_err(|| ErrorKind::Connection("no cookie found".to_owned()))
} }
} }

View File

@ -128,7 +128,7 @@ impl MempoolEntry {
} }
pub trait CookieGetter: Send + Sync { pub trait CookieGetter: Send + Sync {
fn get(&self) -> String; fn get(&self) -> Result<String>;
} }
struct Connection { struct Connection {
@ -169,10 +169,10 @@ impl Connection {
} }
fn send(&mut self, request: &str) -> Result<()> { fn send(&mut self, request: &str) -> Result<()> {
let cookie_b64 = base64::encode(&self.cookie_getter.get()); let cookie = &self.cookie_getter.get()?;
let msg = format!( let msg = format!(
"POST / HTTP/1.1\nAuthorization: Basic {}\nContent-Length: {}\n\n{}", "POST / HTTP/1.1\nAuthorization: Basic {}\nContent-Length: {}\n\n{}",
cookie_b64, base64::encode(cookie),
request.len(), request.len(),
request, request,
); );