Debug the content of a reader


#1

Hello altogether,

I’m quite new to Rust and have now I serious problem, for which I couldn’t find any solution by searching the web.

I’m working on a small web server with iron and here the content of the request body is implementing the Read trait. This content is parsed but there are sometimes problems with the parsing and I like to printout the raw content of the request for debugging.

When I read the body with read_to_string() I can see the content of the request (good!) but of course the content has been consumed and is lost for the application (bad…).

Is there a possibility to get the content of the reader without consuming it? Rewind the reader? Both the receiving and the parsing are done by external crates, so it is no solution to but some debug code there…

Thank you very much in advance!

Best regards,
Christoph


#2

I used something like this to log everything that comes from a certain Read object:

struct ReadTrace<T> {
    r: T
}

impl<T: std::io::Read> std::io::Read for ReadTrace<T> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        match self.r.read(buf) {
            Ok(sz) => {
                println!("read: {:?}", &buf[..sz]);
                Ok(sz)
            }
            Err(err) => {
                println!("read: error: {:?}", err);
                Err(err)
            }
        }
    }
}

If you have an entire pipeline of Read objects, you should arrange for the stage where you suspect the bug is to read from the ReadTrace object.