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
use std::net::SocketAddr;

use bytes::Bytes;
use http::{Request, Response, StatusCode, HeaderValue};
use hyper::{Body, Server, service::{make_service_fn, service_fn}, server::conn::AddrStream};
use urlpattern::UrlPatternMatchInput;

use crate::web_server::{ChiteyError, Factories};

use super::util::throw_chitey_internal_server_error;


#[derive(Clone)]
pub struct HttpServerOpt {
  pub listen: SocketAddr,
  pub redirect: Option<String>,
}

pub async fn launch_http_server<F> (http_server_opt: HttpServerOpt, func: F, factories: Factories) -> Result<(), Box<dyn std::error::Error>>
where
    F: Fn()
{
  let HttpServerOpt{listen, redirect} = http_server_opt;

  // 80ポートにhttpアクセスが来た時にリダイレクトしたりするため
  if let Some(redirect) = redirect {
    // println!("redirect to {}", redirect);
    let http_make_service = make_service_fn(move |_conn: &AddrStream| {
        let location = redirect.clone().to_owned();
        let service = service_fn(move |req| {
            redirect_to_https(location.clone(), req)
        });
        async move { Ok::<_, http::Error>(service) }
    });
    let http_server = Server::bind(&listen).serve(http_make_service);
    println!("Listening on http://{}", listen);
    func();
    let _ = http_server.await?;
  } else {
      let http_make_service = make_service_fn(move |_conn: &AddrStream| {
          let factories = factories.clone();
          let service = service_fn(move |req| {
            not_redirect_to_https_wrap(req, factories.clone(), listen.to_string())
        });
        async move { Ok::<_, http::Error>(service) }
    });
    let http_server = Server::bind(&listen).serve(http_make_service);
    println!("Listening on http://{}", listen);
    func();
    let _ = http_server.await?;

  }
  Ok(())
}

#[inline]
async fn redirect_to_https(
  location: String,
  _req: Request<Body>,
) -> Result<Response<Body>, http::Error> {
  let mut builder = Response::builder();
    builder = builder
        .status(StatusCode::PERMANENT_REDIRECT)
        .header("Location", location);
  // info!("location {}", location);
  builder.body(Body::empty())
}


#[inline]
async fn not_redirect_to_https_wrap(
    req: Request<Body>,
    factories: Factories,
    listen: String,
) -> Result<Response<Body>, ChiteyError> {
    match not_redirect_to_https(req, factories.clone(), listen.to_string()).await {
        Ok(v) => Ok(v),
        Err(e) => {
            tracing::error!("https: {}", e);
            Err(e)
        },
    }
}

#[inline]
async fn not_redirect_to_https(
  req: Request<Body>,
  factories: Factories,
  listen: String,
) -> Result<Response<Body>, ChiteyError> {
    if req.uri().path().contains("..") {
        let builder = Response::builder()
        .header("Alt-Svc", "h3=\":443\"; ma=2592000")
        .status(StatusCode::NOT_FOUND);
        return match builder.body(Body::empty()) {
            Ok(v) => Ok(v),
            Err(e) => Err(ChiteyError::InternalServerError(e.to_string())),
        }
    }

    let input = UrlPatternMatchInput::Url(throw_chitey_internal_server_error((format!("http://{}{}",listen , &req.uri().to_string())).parse())?);
    {
        let method = req.method().clone();
        let req_contain_key = req.headers().contains_key("Another-Header");
        for (res, factory) in factories.factories {
            // GET && POST
            if res.guard == method {
                if let Ok(Some(_)) = res.rdef.exec(input.clone()) {
                    let factory_loc = factory.lock().await;
                    if factory_loc.analyze_types(input.clone()) {
                        return match factory_loc.handler_func(input.clone(), (req, false)).await {
                            Ok(mut resp) => {
                                if req_contain_key {
                                    resp.headers_mut().append("Another-Header", HeaderValue::from_static("Ack"));
                                }
                                resp.headers_mut().append("Alt-Svc", HeaderValue::from_static("h3=\":443\"; ma=2592000"));
                                Ok(resp)
                            },
                            Err(e) => Err(ChiteyError::InternalServerError(e.to_string())),
                        }
                    }
                };
            }
        }
    }

    let builder = Response::builder()
        .header("Alt-Svc", "h3=\":443\"; ma=2592000")
        .status(StatusCode::NOT_FOUND);

    match builder.body(Body::from(Bytes::copy_from_slice(b"page not found"))) {
        // match builder.body(Body::empty()) {
        Ok(v) => Ok(v),
        Err(e) => Err(ChiteyError::InternalServerError(e.to_string())),
    }
}