some refactor
This commit is contained in:
parent
b1ef8c1079
commit
ebb2fb3c87
3 changed files with 44 additions and 47 deletions
|
|
@ -19,19 +19,14 @@ where
|
||||||
mut req: Request<Body>,
|
mut req: Request<Body>,
|
||||||
client_addr: SocketAddr, // アクセス制御用
|
client_addr: SocketAddr, // アクセス制御用
|
||||||
) -> Result<Response<Body>> {
|
) -> Result<Response<Body>> {
|
||||||
let request_log = log_request_msg(&req, &client_addr);
|
req.log(&client_addr, Some("(Incoming)"));
|
||||||
|
|
||||||
// Here we start to handle with server_name
|
// Here we start to handle with server_name
|
||||||
// Find backend application for given server_name
|
// Find backend application for given server_name, and drop if incoming request is invalid as request.
|
||||||
let (server_name, _port) = if let Ok(v) = parse_host_port(&req) {
|
let (server_name, _port) = parse_host_port(&req)?;
|
||||||
v
|
|
||||||
} else {
|
|
||||||
info!("{} => {}", request_log, StatusCode::BAD_REQUEST);
|
|
||||||
return http_error(StatusCode::BAD_REQUEST);
|
|
||||||
};
|
|
||||||
|
|
||||||
if !self.backends.apps.contains_key(&server_name) && self.backends.default_app.is_none() {
|
if !self.backends.apps.contains_key(&server_name) && self.backends.default_app.is_none() {
|
||||||
info!("{} => {}", request_log, StatusCode::SERVICE_UNAVAILABLE);
|
// info!("{} => {}", request_log, StatusCode::SERVICE_UNAVAILABLE);
|
||||||
return http_error(StatusCode::SERVICE_UNAVAILABLE);
|
return http_error(StatusCode::SERVICE_UNAVAILABLE);
|
||||||
}
|
}
|
||||||
let backend = if let Some(be) = self.backends.apps.get(&server_name) {
|
let backend = if let Some(be) = self.backends.apps.get(&server_name) {
|
||||||
|
|
@ -45,7 +40,7 @@ where
|
||||||
// Redirect to https if !tls_enabled and redirect_to_https is true
|
// Redirect to https if !tls_enabled and redirect_to_https is true
|
||||||
if !self.tls_enabled && backend.https_redirection.unwrap_or(false) {
|
if !self.tls_enabled && backend.https_redirection.unwrap_or(false) {
|
||||||
debug!("Redirect to secure connection: {}", server_name);
|
debug!("Redirect to secure connection: {}", server_name);
|
||||||
info!("{} => {}", request_log, StatusCode::PERMANENT_REDIRECT);
|
// info!("{} => {}", request_log, StatusCode::PERMANENT_REDIRECT);
|
||||||
return secure_redirection(&server_name, self.globals.https_port, &req);
|
return secure_redirection(&server_name, self.globals.https_port, &req);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -108,7 +103,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
debug!("Response from backend: {:?}", res_backend.status());
|
debug!("Response from backend: {:?}", res_backend.status());
|
||||||
let response_log = res_backend.status().to_string();
|
// let response_log = res_backend.status().to_string();
|
||||||
|
|
||||||
if res_backend.status() == StatusCode::SWITCHING_PROTOCOLS {
|
if res_backend.status() == StatusCode::SWITCHING_PROTOCOLS {
|
||||||
// Handle StatusCode::SWITCHING_PROTOCOLS in response
|
// Handle StatusCode::SWITCHING_PROTOCOLS in response
|
||||||
|
|
@ -138,11 +133,11 @@ where
|
||||||
.map_err(|e| anyhow!("Coping between upgraded connections failed: {}", e))?; // TODO: any response code?
|
.map_err(|e| anyhow!("Coping between upgraded connections failed: {}", e))?; // TODO: any response code?
|
||||||
Ok(()) as Result<()>
|
Ok(()) as Result<()>
|
||||||
});
|
});
|
||||||
info!("{} => {}", request_log, response_log);
|
// info!("{} => {}", request_log, response_log);
|
||||||
Ok(res_backend)
|
Ok(res_backend)
|
||||||
} else {
|
} else {
|
||||||
error!("Request does not have an upgrade extension");
|
error!("Request does not have an upgrade extension");
|
||||||
info!("{} => {}", request_log, StatusCode::BAD_REQUEST);
|
// info!("{} => {}", request_log, StatusCode::BAD_REQUEST);
|
||||||
http_error(StatusCode::BAD_REQUEST)
|
http_error(StatusCode::BAD_REQUEST)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -150,16 +145,16 @@ where
|
||||||
"Backend tried to switch to protocol {:?} when {:?} was requested",
|
"Backend tried to switch to protocol {:?} when {:?} was requested",
|
||||||
upgrade_in_response, upgrade_in_request
|
upgrade_in_response, upgrade_in_request
|
||||||
);
|
);
|
||||||
info!("{} => {}", request_log, StatusCode::SERVICE_UNAVAILABLE);
|
// info!("{} => {}", request_log, StatusCode::SERVICE_UNAVAILABLE);
|
||||||
http_error(StatusCode::SERVICE_UNAVAILABLE)
|
http_error(StatusCode::SERVICE_UNAVAILABLE)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Generate response to client
|
// Generate response to client
|
||||||
if self.generate_response_forwarded(&mut res_backend).is_ok() {
|
if self.generate_response_forwarded(&mut res_backend).is_ok() {
|
||||||
info!("{} => {}", request_log, response_log);
|
// info!("{} => {}", request_log, response_log);
|
||||||
Ok(res_backend)
|
Ok(res_backend)
|
||||||
} else {
|
} else {
|
||||||
info!("{} => {}", request_log, StatusCode::BAD_GATEWAY);
|
// info!("{} => {}", request_log, StatusCode::BAD_GATEWAY);
|
||||||
http_error(StatusCode::BAD_GATEWAY)
|
http_error(StatusCode::BAD_GATEWAY)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use super::{Upstream, UpstreamOption};
|
use super::{Upstream, UpstreamOption};
|
||||||
use crate::{error::*, log::*, utils::*};
|
use crate::{error::*, log::*, utils::*};
|
||||||
use hyper::{
|
use hyper::{
|
||||||
header::{self, HeaderMap, HeaderValue},
|
header::{self, HeaderMap, HeaderName, HeaderValue},
|
||||||
Uri,
|
Uri,
|
||||||
};
|
};
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
|
@ -29,12 +29,8 @@ pub(super) fn apply_upstream_options_to_header(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn append_header_entry(
|
pub(super) fn append_header_entry(headers: &mut HeaderMap, key: &str, value: &str) -> Result<()> {
|
||||||
headers: &mut HeaderMap,
|
match headers.entry(HeaderName::from_bytes(key.as_bytes())?) {
|
||||||
key: &'static str,
|
|
||||||
value: &str,
|
|
||||||
) -> Result<()> {
|
|
||||||
match headers.entry(key) {
|
|
||||||
header::Entry::Vacant(entry) => {
|
header::Entry::Vacant(entry) => {
|
||||||
entry.insert(value.parse::<HeaderValue>()?);
|
entry.insert(value.parse::<HeaderValue>()?);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,33 +1,39 @@
|
||||||
use crate::{error::*, utils::*};
|
use crate::{error::*, log::*, utils::*};
|
||||||
use hyper::{header, Request, Uri};
|
use hyper::{header, Request, Uri};
|
||||||
use std::net::SocketAddr;
|
use std::fmt::Display;
|
||||||
|
|
||||||
////////////////////////////////////////////////////
|
////////////////////////////////////////////////////
|
||||||
// Functions of utils for request messages
|
// Functions of utils for request messages
|
||||||
|
pub trait MsgLog {
|
||||||
|
fn log<T: Display + ToCanonical>(self, src: &T, extra: Option<&str>);
|
||||||
|
}
|
||||||
|
impl<B> MsgLog for &Request<B> {
|
||||||
|
fn log<T: Display + ToCanonical>(self, src: &T, extra: Option<&str>) {
|
||||||
|
let canonical_src = src.to_canonical();
|
||||||
|
|
||||||
pub(super) fn log_request_msg<B>(req: &Request<B>, client_addr: &SocketAddr) -> String {
|
let server_name = self.headers().get(header::HOST).map_or_else(
|
||||||
let server_name = req.headers().get(header::HOST).map_or_else(
|
|| {
|
||||||
|| {
|
self
|
||||||
req
|
.uri()
|
||||||
|
.authority()
|
||||||
|
.map_or_else(|| "<none>", |au| au.as_str())
|
||||||
|
},
|
||||||
|
|h| h.to_str().unwrap_or("<none>"),
|
||||||
|
);
|
||||||
|
info!(
|
||||||
|
"{} <- {} -- {} {:?} {:?} {:?} {}",
|
||||||
|
server_name,
|
||||||
|
canonical_src,
|
||||||
|
self.method(),
|
||||||
|
self.version(),
|
||||||
|
self
|
||||||
.uri()
|
.uri()
|
||||||
.authority()
|
.path_and_query()
|
||||||
.map_or_else(|| "<none>", |au| au.as_str())
|
.map_or_else(|| "", |v| v.as_str()),
|
||||||
},
|
self.headers(),
|
||||||
|h| h.to_str().unwrap_or("<none>"),
|
extra.map_or_else(|| "", |v| v)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
return format!(
|
|
||||||
"{} <- {} -- {} {:?} {:?} ({:?})",
|
|
||||||
server_name,
|
|
||||||
client_addr.to_canonical(),
|
|
||||||
req.method(),
|
|
||||||
req.version(),
|
|
||||||
req
|
|
||||||
.uri()
|
|
||||||
.path_and_query()
|
|
||||||
.map_or_else(|| "", |v| v.as_str()),
|
|
||||||
req.headers()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn parse_host_port<B: core::fmt::Debug>(
|
pub(super) fn parse_host_port<B: core::fmt::Debug>(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue