Skip to main content

pipa/http/
status.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub struct HttpStatus(pub u16);
3
4impl HttpStatus {
5    pub fn is_redirect(&self) -> bool {
6        matches!(self.0, 301 | 302 | 303 | 307 | 308)
7    }
8
9    pub fn is_success(&self) -> bool {
10        (200..300).contains(&self.0)
11    }
12
13    pub fn is_client_error(&self) -> bool {
14        (400..500).contains(&self.0)
15    }
16
17    pub fn is_server_error(&self) -> bool {
18        (500..600).contains(&self.0)
19    }
20
21    pub fn reason_phrase(&self) -> &'static str {
22        match self.0 {
23            100 => "Continue",
24            101 => "Switching Protocols",
25            200 => "OK",
26            201 => "Created",
27            204 => "No Content",
28            301 => "Moved Permanently",
29            302 => "Found",
30            303 => "See Other",
31            304 => "Not Modified",
32            307 => "Temporary Redirect",
33            308 => "Permanent Redirect",
34            400 => "Bad Request",
35            401 => "Unauthorized",
36            403 => "Forbidden",
37            404 => "Not Found",
38            405 => "Method Not Allowed",
39            408 => "Request Timeout",
40            413 => "Payload Too Large",
41            429 => "Too Many Requests",
42            500 => "Internal Server Error",
43            502 => "Bad Gateway",
44            503 => "Service Unavailable",
45            504 => "Gateway Timeout",
46            _ => "",
47        }
48    }
49}