fn recv_request()

in src/vtok_rpc/src/transport.rs [130:161]


    fn recv_request(&mut self) -> Result<ApiRequest> {
        // This is a nifty trick to get line-by-line input, while also limitting the amount
        // of data we can read from the wire:
        // Read::take(N) will return a Take struct (itself and implementor of Read), that will
        // be limited at reading at most N bytes. We can then pass this Take to BufReader, and
        // get a limited buffered reader.
        // However both Take and BufReader need to own the underlying Read implementor. To get
        // around ownership issues, and since our BufReader only needs to survive reading
        // exactly one message, we can rely on the fact that if R: Read, then &mut R: Read,
        // and use a Take(&mut self.stream).
        let mut reader = BufReader::new((&mut self.stream).take(Self::MAX_HDR_LEN as u64));
        let mut ln = String::new();
        reader.read_line(&mut ln).map_err(Error::IoError)?;
        let mut iter = ln.as_str().trim().split_whitespace();
        match (iter.next(), iter.next(), iter.next()) {
            (Some("POST"), Some(url), Some("HTTP/1.1")) => {
                if url != self.url {
                    return Err(Error::BadUrl);
                }
            }
            _ => return Err(Error::ParseError),
        }

        Self::read_headers(&mut reader).and_then(|h| {
            reader.get_mut().set_limit(h.content_length as u64);
            let mut buf = vec![0u8; h.content_length];
            reader
                .read_exact(buf.as_mut_slice())
                .map_err(Error::IoError)?;
            serde_json::from_slice(buf.as_slice()).map_err(Error::SerdeError)
        })
    }