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
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#[macro_use]
extern crate log;

pub mod channel;
#[cfg(test)]
mod syncbuf;

use std::io::{Read, Write, Result};
use std::io::{Error, ErrorKind};
use std::net::Shutdown as ShutdownMode;
use std::ops::Deref;

// TODO: get rid of this
const DIGIT_LIMIT: usize = 64;

pub trait ReadNetstring: Shutdown {
    fn read_netstring(&mut self) -> Result<String>;
}

pub trait WriteNetstring: Shutdown {
    fn write_netstring<S: AsRef<str>>(&mut self, value: S) -> Result<()>;
    fn flush(&mut self) -> Result<()>;
}

pub trait Shutdown {
    fn shutdown(&self, how: ShutdownMode) -> Result<()>;
}

impl<R: Read + Shutdown> ReadNetstring for R {
    fn read_netstring(&mut self) -> Result<String> {
        let ln = try!(read_length(self));
        let mut data = vec![0u8;ln];

        let mut offset = 0usize;
        let mut done = false;

        while !done {
            let r = try!(self.read(data[offset..].as_mut()));
            offset = offset + r;
            if r == 0 || offset == ln {
                done = true;
            }
        }

        // TODO: there has to be a a cleaner way to do this...
        // read delimiter ","
        let mut t = vec![0u8].into_boxed_slice();
        try!(self.read(t[..].as_mut()));

        // Verify delimiter
        if t[0] != b',' {
            return Err(Error::new(ErrorKind::InvalidData, "Expected `,` delimiter."));
        }

        // return utf8 string
        match String::from_utf8(data) {
            Ok(s) => Ok(s),
            Err(err) => Err(Error::new(ErrorKind::InvalidData, err)),
        }
    }
}

impl<W: Write + Shutdown> WriteNetstring for W {
    fn write_netstring<S: AsRef<str>>(&mut self, value: S) -> Result<()> {
        let value = value.as_ref();
        let s = format!("{}:{},", value.len(), value);
        try!(self.write_all(s.as_bytes()));
        Ok(())
    }

    fn flush(&mut self) -> Result<()> {
        Write::flush(self)
    }
}

impl Shutdown for ::std::os::unix::net::UnixStream {
    fn shutdown(&self, how: ShutdownMode) -> Result<()> {
        ::std::os::unix::net::UnixStream::shutdown(self, how)
    }
}

impl Shutdown for ::std::net::TcpStream {
    fn shutdown(&self, how: ShutdownMode) -> Result<()> {
        ::std::net::TcpStream::shutdown(self, how)
    }
}

impl<T: Shutdown> Shutdown for Box<T> {
    fn shutdown(&self, how: ShutdownMode) -> Result<()> {
        self.deref().shutdown(how)
    }
}

impl<'a> Shutdown for &'a [u8] {
     fn shutdown(&self, _how: ShutdownMode) -> Result<()> {
         Ok(())
     }
}

impl<T> Shutdown for Vec<T> {
     fn shutdown(&self, _how: ShutdownMode) -> Result<()> {
         Ok(())
     }
}

impl Shutdown for ::std::io::Sink {
    fn shutdown(&self, _how: ShutdownMode) -> Result<()> {
        Ok(())
    }
}

fn read_length<R: Read>(r: &mut R) -> Result<usize> {
    let mut t = [0u8; DIGIT_LIMIT];
    let mut current = 0usize;
    let mut done = false;
    while !done {
        let r = try!(r.read(t[current..current + 1].as_mut()));
        if r == 0 {
            return Err(Error::new(ErrorKind::ConnectionAborted, "Connection closed by target."));
        }
        // Reached ":" signaling the end of the length
        if t[current] == b':' {
            done = true;
        } else {
            current += 1;
        }
    }

    let s = match String::from_utf8(t[..current].to_vec()) {
        Ok(s) => s,
        Err(err) => return Err(Error::new(ErrorKind::InvalidData, err)),
    };

    let ln = match s.parse::<u64>() {
        Ok(x) => x,
        Err(err) => return Err(Error::new(ErrorKind::InvalidData, err)),
    };


    Ok(ln as usize)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn basic_read() {
        let mut raw = "5:hello,".as_bytes();
        let x = raw.read_netstring().unwrap();
        assert_eq!("hello", x);
    }

    #[test]
    fn basic_write() {
        let mut raw = vec![];
        let _ = raw.write_netstring("hello").unwrap();
        assert_eq!(raw, b"5:hello,");
    }

    #[test]
    #[should_panic(expected="Expected `,` delimiter.")]
    fn invalid_delimiter() {
        let mut raw = "5:hello?".as_bytes();
        raw.read_netstring().unwrap();
    }

    #[test]
    #[should_panic(expected="Expected `,` delimiter.")]
    fn longer() {
        let mut raw = "10:hello,".as_bytes();
        raw.read_netstring().unwrap();
    }

    #[test]
    #[should_panic(expected="Expected `,` delimiter.")]
    fn shorter() {
        let mut raw = "2:hello,".as_bytes();
        raw.read_netstring().unwrap();
    }

    #[test]
    fn multiple() {
        let mut raw = "5:hello,5:world,10:xxxxxxxxxx,".as_bytes();
        let x1 = raw.read_netstring().unwrap();
        let x2 = raw.read_netstring().unwrap();
        let x3 = raw.read_netstring().unwrap();
        assert_eq!(x1, "hello");
        assert_eq!(x2, "world");
        assert_eq!(x3, "xxxxxxxxxx");
    }
}