use std::io;
use bytes::BytesMut;
pub struct Request {
first: BytesMut,
head: BytesMut,
body: BytesMut,
}
impl Request {
pub fn body(&self) -> &BytesMut {
&self.body
}
pub fn first(&self) -> &BytesMut {
&self.first
}
pub fn head(&self) -> &BytesMut {
&self.head
}
}
pub fn decode(buf: &mut BytesMut) -> io::Result<Option<Request>> {
if buf.is_empty() {
return Ok(None);
}
let iter = buf.clone();
let mut it = iter.iter();
let mut firstc : u16 = 0_u16;
let mut headc : u16 = 0_u16;
loop {
match it.next() {
Some(&b'\n') => break,
None => break,
_ => {
firstc += 1;
}
};
}
let mut linec : u16 = 0_u16;
loop {
match it.next() {
Some(&b'\n') => {
if linec == 1 {
break;
}
headc += 1;
linec = 0;
},
None => break,
_ => {
linec += 1;
headc += 1;
}
};
}
let first = buf.split_to(firstc as usize);
let head = buf.split_to(headc as usize);
let body = buf.clone();
buf.clear();
let request = Request {
first: first,
head: head,
body: body,
};
Ok(request.into())
}