summaryrefslogtreecommitdiff
path: root/src/server/response.rs
blob: f2df722f1cb457f34046873aa93ef3fe75195d66 (plain)
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
use std::vec::Vec;

#[derive(Copy, Clone)]
pub enum Status {
    Input = 1,
    Success = 2,
    Redirect = 3,
    TemporaryFailure = 4,
    PermanentFailure = 5,
    ClientCertificateRequired = 6,
}

pub struct Header {
    status: Status,
    meta: String,
}

pub struct Response {
    header: Header,
    data: Vec<u8>,
}

impl Header {
    pub fn new(status: Status, meta: &str) -> Header {
        Header{
            status: status,
            meta: meta.to_string(),
        }
    }

    pub fn format(&self) -> String {
        let status: u8 = self.status as u8;
        return format!("{} {}\r\n", status * 10, self.meta)
    }
}

impl Response {
    pub fn new(header: Header, data: Vec<u8>) -> Response {
        Response{
            header: header,
            data: data,
        }
    }

    pub fn new_empty(header: Header) -> Response {
        Response{
            header: header,
            data: Vec::new(),
        }
    }

    pub fn format(&self) -> Vec<u8> {
        let mut resp: Vec<u8> = self.header.format().as_bytes().to_vec();
        resp.extend(&self.data);
        return resp;
    }
}

pub fn invalid_protocol () -> Response {
    Response::new_empty(
        Header::new(
            Status::PermanentFailure,
            "this protocol is not supported"
        ),
    )
}