hyper_fast/server/
http_route.rs

1use std::net::SocketAddr;
2use std::time::Instant;
3
4use chrono::Local;
5use http::{header, Method, Request, Uri};
6use hyper::Body;
7
8use crate::server::commons::{BR_CONTENT_ENCODING, DEFLATE_CONTENT_ENCODING, GZIP_CONTENT_ENCODING};
9
10pub struct HttpRoute<'a> {
11    pub req: &'a Request<Body>,
12    pub req_time: chrono::DateTime<Local>,
13    pub req_instant: Instant,
14    pub method: &'a Method,
15    pub uri: &'a Uri,
16    pub path: &'a str,
17    pub query: Option<&'a str>,
18    pub content_encoding: Option<Vec<u8>>,
19    pub accept_encoding: Option<&'a [u8]>,
20    pub metric_path: Option<&'static str>,
21    pub remote_addr: SocketAddr,
22}
23
24const CONTENT_ENCODINGS: [&'static [u8]; 3] = [BR_CONTENT_ENCODING, GZIP_CONTENT_ENCODING, DEFLATE_CONTENT_ENCODING];
25
26impl<'a> HttpRoute<'a> {
27    pub fn new(req: &'a Request<Body>, req_time: chrono::DateTime<Local>, req_instant: Instant, remote_addr: SocketAddr) -> HttpRoute<'a> {
28        HttpRoute {
29            req,
30            req_time,
31            req_instant,
32            method: req.method(),
33            uri: req.uri(),
34            path: req.uri().path(),
35            query: req.uri().query(),
36            content_encoding: req.headers().get(header::CONTENT_ENCODING).map(|value| value.as_bytes().to_ascii_lowercase()),
37            accept_encoding: req.headers().get(header::ACCEPT_ENCODING).and_then(|value| {
38                let accept_encodings = value.as_bytes().to_ascii_lowercase();
39
40                for encoding in CONTENT_ENCODINGS.iter() {
41                    if let Some(_index) = twoway::find_bytes(&accept_encodings, encoding) {
42                        return Some(*encoding);
43                    }
44                }
45
46                None
47            }),
48            metric_path: None,
49            remote_addr,
50        }
51    }
52}