1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::borrow::Cow;
use std::fmt;
use std::io;
use byteorder::{ReadBytesExt, WriteBytesExt};

/// Username and password authentication token embedded in requests as there is no concept of
/// session in the TCP protocol, every request must be authenticated.
#[derive(Clone, PartialEq, Eq)]
pub struct UsernamePassword(Cow<'static, str>, Cow<'static, str>);

impl fmt::Debug for UsernamePassword {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "({:?}, PASSWORD)", self.0)
    }
}

impl UsernamePassword {
    /// Create a new value
    pub fn new<S: Into<Cow<'static, str>>>(username: S, password: S) -> UsernamePassword {
        UsernamePassword(username.into(), password.into())
    }

    #[doc(hidden)]
    pub fn decode<R: ReadBytesExt>(buf: &mut R) -> io::Result<Self> {
        use std::string;

        fn convert_utf8_err(e: string::FromUtf8Error) -> io::Error {
            io::Error::new(io::ErrorKind::InvalidData, e.utf8_error())
        }

        let len = buf.read_u8()?;
        let mut username = vec![0u8, len];
        buf.read_exact(&mut username[..])?;
        let username = String::from_utf8(username).map_err(convert_utf8_err)?;

        let len = buf.read_u8()?;
        let mut password = vec![0u8, len];
        buf.read_exact(&mut password[..])?;
        let password = String::from_utf8(password).map_err(convert_utf8_err)?;

        Ok(UsernamePassword(Cow::Owned(username), Cow::Owned(password)))
    }

    #[doc(hidden)]
    pub fn encode<W: WriteBytesExt>(&self, buf: &mut W) -> io::Result<usize> {
        // TODO: new that disallows too long strings
        buf.write_u8(self.0.len() as u8)?;
        buf.write_all(self.0.as_bytes())?;
        buf.write_u8(self.1.len() as u8)?;
        buf.write_all(self.1.as_bytes())?;

        Ok(1 + self.0.len() + 1 + self.1.len())
    }
}

impl Into<(String, String)> for UsernamePassword {
    fn into(self) -> (String, String) {
        (self.0.into_owned(), self.1.into_owned())
    }
}