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

#[derive(Copy, Clone)]
pub enum Status {
    Input = 10,
    Success = 20,
    Redirect = 30,
    TemporaryFailure = 40,
    PermanentFailure = 50,
    ClientCertificateRequired = 60,
}

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

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

    pub fn format(&self) -> String {
        format!("{} {}\r\n", self.status as u8, self.meta)
    }

    pub fn to_vec(&self) -> Vec<u8> {
        self.format().as_bytes().to_vec()
    }
}

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

pub fn not_understood() -> Header {
    Header::new(Status::PermanentFailure, "request not understood")
}

pub fn not_found() -> Header {
    Header::new(Status::PermanentFailure, "resource not found")
}

pub fn internal_error() -> Header {
    Header::new(Status::PermanentFailure, "internal server error")
}