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
use bytes::{BytesMut, BufMut};
use tokio_io::codec::{Encoder, Decoder};

use httplib::Response;
use request::{self, Request};
use std::io;

pub struct Http;

impl Decoder for Http {
    type Item = Request;
    type Error = io::Error;

    fn decode(&mut self, buf: &mut BytesMut) -> io::Result<Option<Request>> {
        request::decode(buf)
    }
}

impl Encoder for Http {
    type Item = Response<String>;
    type Error = io::Error;

    fn encode(&mut self, msg: Response<String>, buf: &mut BytesMut) -> io::Result<()> {
        encode(msg, buf);
        Ok(())
    }
}


pub fn encode(msg: Response<String>, buf: &mut BytesMut) {
    let length = msg.body().len();
    let now = ::date::now();

    templatify_buffer! { buf, "\
        HTTP/1.1 "; format!("{}", msg.status()) ;"\r\n\
        Server: thruster\r\n\
        Content-Length: "; format!("{}", length) ;"\r\n\
        Date: "; format!("{}", now) ;"\r\n\
    " };

    for (ref k, ref v) in msg.headers() {
        let key: &str = k.as_ref();
        let val: &[u8] = v.as_bytes();
        templatify_buffer! { buf, ""; key ;": "; val ;"\r\n" };
    }

    templatify_buffer! { buf, "\r\n"; msg.body() ;"" };
}