match header(line.as_bytes()) {
nom::IResult::Done(_, h) => Some((h, line.clone())),
result => {
- println!("Failed to parse header: {:?} - {:?}", line, result);
+ warn!("Failed to parse header: {:?} - {:?}", line, result);
None
}
}
fn parse_message(&mut self, buf: &mut BytesMut) -> io::Result<Option<Message>> {
loop {
- println!("Gathering headers from {:?}", std::str::from_utf8(&buf));
+ trace!("Gathering headers from {:?}", std::str::from_utf8(&buf));
let mut done = false;
let mut total = 0;
let mut search: Box<Iterator<Item = &u8>> = Box::new(buf.iter());
while !done {
- println!("Gathering CRLF");
+ trace!("Gathering CRLF");
if let Some(cr) = search.position(|&b| b == CR) {
- println!("Found CR at {}", cr);
+ trace!("Found CR at {}", cr);
total += cr;
let next = search.next();
- println!("Got {:?} after CR", next);
+ trace!("Got {:?} after CR", next);
if let Some(&LF) = next {
done = true;
// We've got a line terminating in \r\n.
let line = buf.split_to(total + 2);
- println!("Using line: {:?}", std::str::from_utf8(&line));
+ trace!("Using line: {:?}", std::str::from_utf8(&line));
match (&mut self.top_line, chartype(line.first()), &mut self.latest_header) {
// Ignore empty top lines
(&mut None, CharType::Line, _) => {
- println!("Ignoring empty top line");
+ trace!("Ignoring empty top line");
}
// Top line beginnning with whitespace - ignore
(&mut None, CharType::Whitespace, _) => {
- println!("Got top line beginning with whitespace - ignore: {:?}",
+ trace!("Got top line beginning with whitespace - ignore: {:?}",
line);
}
// Top line with no headers! Discard.
(top_line @ &mut None, CharType::Other, _) => {
- println!("Got new top line of request: {:?}", line);
+ trace!("Got new top line of request: {:?}", line);
top_line.get_or_insert(UnparsedLine::new(line));
}
// Top line with no headers! Discard.
(top_line @ &mut Some(_), CharType::Line, &mut None) => {
- println!("Got request with no headers - discarding");
+ warn!("Got request with no headers - discarding");
top_line.take();
}
(top_line @ &mut Some(_), CharType::Line, old_header @ &mut Some(_)) => {
self.headers.push(old_header.take().unwrap());
- println!("Got end of headers");
+ trace!("Got end of headers");
if let Ok(new_message) = PartialMessage::new(top_line.take()
.unwrap(),
&mut self.headers) {
- println!("Got partial message: {:?}", new_message);
+ debug!("Got partial message: {:?}", new_message);
self.headers.clear();
if length == 0 {
if let Some(message) = Message::new(new_message) {
- println!("Got message without body: {:?}", message);
+ debug!("Got message without body: {:?}", message);
return Ok(Some(message));
} else {
- println!("Failed to parse message with no body!");
+ debug!("Failed to parse message with no body!");
}
} else if length > buf.len() {
self.message.get_or_insert(new_message);
} else {
let body = buf.split_to(length);
if let Some(message) = Message::new_with_body(new_message, body) {
- println!("Got message with body: {:?}", message);
+ debug!("Got message with body: {:?}", message);
return Ok(Some(message));
} else {
- println!("Failed to parse message with body!");
+ warn!("Failed to parse message with body!");
}
}
} else {
- println!("Failed to parse partial message!");
+ warn!("Failed to parse partial message!");
// Message failed to parse
top_line.take();
}))
}
result => {
- println!("Failed to parse top line: {:?}", result);
+ warn!("Failed to parse top line: {:?}", result);
None
}
}
fn decode(&mut self, buf: &mut BytesMut) -> io::Result<Option<Message>> {
if let Some(_) = self.message {
- println!("Gathering body");
+ trace!("Gathering body");
let length = self.message.iter().map(|m| m.body_length()).next().unwrap();
buf.split_to(length)) {
return Ok(Some(message));
} else {
- println!("Failed to parse message with no body!");
+ warn!("Failed to parse message with no body!");
}
}
let future = stream.for_each(move |message| caller(message));
- let future = future.map_err(|err| println!("Error {:?}", err));
+ let future = future.map_err(|err| error!("Error {:?}", err));
// Spawn the future as a concurrent task
handle.spawn(future);
#[cfg(test)]
mod tests {
+ extern crate env_logger;
+
use std::ops::DerefMut;
use std::sync::Arc;
use std::sync::Mutex;
#[test]
fn test_message_decode() {
+ env_logger::init().unwrap();
+
let message =
decode_sip_message("MESSAGE sip:test.com \
SIP/2.0\r\nAccept:text/plain\r\nAccept-Encoding:\