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
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//! # Handles everything related to HTTP responses.

use std::collections::HashMap;
use std::str;

/// # A request message
pub struct Message {
    pub protocol: String,
    pub status: String,
    pub headers: HashMap<String, String>,
    pub body: Vec<u8>,
}

impl Message {
    /// # Create a new HTTP Message
    pub fn new(
        protocol: String,
        status: String,
        headers: HashMap<String, String>,
        body: Vec<u8>,
    ) -> Message {
        Message {
            protocol,
            status,
            headers,
            body,
        }
    }

    /// # Get the HTTP header as a new string
    /// ```rust
    /// use milstian_http::response::Message;
    /// use std::collections::HashMap;
    /// assert_eq!(
    ///     Message::new(
    ///         "HTTP/1.0".to_string(),
    ///         "200 OK".to_string(),
    ///         HashMap::new(),
    ///         b"<html><body>Nothing here</body></html>".to_vec()
    ///     ).header_to_string(),
    ///     "HTTP/1.0 200 OK\r\n\r\n".to_string()
    /// );    
    /// ```
    pub fn header_to_string(&self) -> String {
        let mut response = format!("{} {}\r\n", &self.protocol, &self.status);

        if !&self.headers.is_empty() {
            let mut headers: Vec<(&String, &String)> = self.headers.iter().collect();
            headers.sort_by(|a, b| a.cmp(b));
            for (key, value) in headers {
                response.push_str(&format!("{}: {}\r\n", &key, &value));
            }
        }
        response.push_str("\r\n");

        response
    }

    /// # Convert response message into a string
    /// ```rust
    /// use milstian_http::response::Message;
    /// use std::collections::HashMap;
    /// assert_eq!(
    ///     Message::new(
    ///         "HTTP/1.0".to_string(),
    ///         "200 OK".to_string(),
    ///         HashMap::new(),
    ///         b"<html><body>Nothing here</body></html>".to_vec()
    ///     ).to_string(),
    ///     "HTTP/1.0 200 OK\r\n\r\n<html><body>Nothing here</body></html>".to_string()
    /// );
    /// ```
    pub fn to_string(&mut self) -> String {
        let mut response = format!("{} {}\r\n", &self.protocol, &self.status);

        if !&self.headers.is_empty() {
            let mut headers: Vec<(&String, &String)> = self.headers.iter().collect();
            headers.sort_by(|a, b| a.cmp(b));
            for (key, value) in headers {
                response.push_str(&format!("{}: {}\r\n", &key, &value));
            }
        }
        response.push_str("\r\n");

        if !&self.body.is_empty() {
            if let Ok(body_string) = str::from_utf8(&self.body) {
                response.push_str(body_string);
            }
        }

        response
    }

    /// # Convert message into bytes
    /// ```rust
    /// use milstian_http::response::Message;
    /// use std::collections::HashMap;
    /// assert_eq!(
    ///     Message::new(
    ///         "HTTP/1.0".to_string(),
    ///         "200 OK".to_string(),
    ///         HashMap::new(),
    ///         b"<html><body>Nothing here</body></html>".to_vec()
    ///     ).to_bytes(),
    ///     b"HTTP/1.0 200 OK\r\n\r\n<html><body>Nothing here</body></html>".to_vec()
    /// );
    /// ```
    pub fn to_bytes(&mut self) -> Vec<u8> {
        let mut response = format!("{} {}\r\n", &self.protocol, &self.status).into_bytes();

        if !&self.headers.is_empty() {
            let mut headers: Vec<(&String, &String)> = self.headers.iter().collect();
            headers.sort_by(|a, b| a.cmp(b));
            for (key, value) in headers {
                response.append(&mut format!("{}: {}\r\n", &key, &value).into_bytes());
            }
        }
        response.append(&mut "\r\n".to_string().into_bytes());

        if !&self.body.is_empty() {
            response.append(&mut self.body);
        }

        response
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_to_string() {
        let mut message = Message::new(
            "HTTP/1.0".to_string(),
            "200 OK".to_string(),
            HashMap::new(),
            b"<html><body>Nothing here</body></html>".to_vec(),
        );
        assert_eq!(
            message.to_string(),
            "HTTP/1.0 200 OK\r\n\r\n<html><body>Nothing here</body></html>".to_string()
        );
    }

    #[test]
    fn test_to_bytes() {
        let mut message = Message::new(
            "HTTP/1.0".to_string(),
            "200 OK".to_string(),
            HashMap::new(),
            b"<html><body>Nothing here</body></html>".to_vec(),
        );
        assert_eq!(
            message.to_bytes(),
            b"HTTP/1.0 200 OK\r\n\r\n<html><body>Nothing here</body></html>".to_vec()
        );
    }

}