restructuring src dir
This commit is contained in:
parent
6d41830eba
commit
b56bf54318
12 changed files with 146 additions and 232 deletions
|
|
@ -1,7 +1,7 @@
|
|||
// Highly motivated by https://github.com/felipenoris/hyper-reverse-proxy
|
||||
use super::{utils_headers::*, utils_request::*, utils_response::ResLog, utils_synth_response::*};
|
||||
use super::{utils_headers::*, utils_request::*, utils_synth_response::*};
|
||||
use crate::{
|
||||
backend::{ServerNameLC, UpstreamGroup},
|
||||
backend::{ServerNameExp, UpstreamGroup},
|
||||
error::*,
|
||||
globals::Globals,
|
||||
log::*,
|
||||
|
|
@ -39,9 +39,8 @@ where
|
|||
client_addr: SocketAddr, // アクセス制御用
|
||||
listen_addr: SocketAddr,
|
||||
tls_enabled: bool,
|
||||
tls_server_name: Option<ServerNameLC>,
|
||||
tls_server_name: Option<ServerNameExp>,
|
||||
) -> Result<Response<Body>> {
|
||||
req.log_debug(&client_addr, Some("(from Client)"));
|
||||
////////
|
||||
let mut log_data = MessageLog::from(&req);
|
||||
log_data.client_addr(&client_addr);
|
||||
|
|
@ -102,7 +101,6 @@ where
|
|||
return self.return_with_error_log(StatusCode::SERVICE_UNAVAILABLE, &mut log_data);
|
||||
};
|
||||
// debug!("Request to be forwarded: {:?}", req_forwarded);
|
||||
req.log_debug(&client_addr, Some("(to Backend)"));
|
||||
log_data.xff(&req.headers().get("x-forwarded-for"));
|
||||
log_data.upstream(req.uri());
|
||||
//////
|
||||
|
|
@ -123,14 +121,9 @@ where
|
|||
}
|
||||
};
|
||||
|
||||
res_backend.log_debug(&backend.server_name, &client_addr, Some("(from Backend)"));
|
||||
// let response_log = res_backend.status().to_string();
|
||||
|
||||
if res_backend.status() != StatusCode::SWITCHING_PROTOCOLS {
|
||||
// Generate response to client
|
||||
if self.generate_response_forwarded(&mut res_backend).is_ok() {
|
||||
// info!("{} => {}", request_log, response_log);
|
||||
res_backend.log_debug(&backend.server_name, &client_addr, Some("(to Client)"));
|
||||
log_data.status_code(&res_backend.status()).output();
|
||||
return Ok(res_backend);
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
mod handler_main;
|
||||
mod utils_headers;
|
||||
mod utils_request;
|
||||
mod utils_response;
|
||||
mod utils_synth_response;
|
||||
|
||||
pub use handler_main::HttpMessageHandler;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,9 @@
|
|||
use crate::{backend::UpstreamGroup, backend_opt::UpstreamOption, error::*, log::*, utils::*};
|
||||
use crate::{
|
||||
backend::{UpstreamGroup, UpstreamOption},
|
||||
error::*,
|
||||
log::*,
|
||||
utils::*,
|
||||
};
|
||||
use bytes::BufMut;
|
||||
use hyper::{
|
||||
header::{self, HeaderMap, HeaderName, HeaderValue},
|
||||
|
|
|
|||
|
|
@ -1,58 +1,5 @@
|
|||
use crate::{error::*, log::*, utils::*};
|
||||
use crate::error::*;
|
||||
use hyper::{header, Request};
|
||||
use std::fmt::Display;
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
// Functions of utils for request messages
|
||||
pub trait ReqLog {
|
||||
fn log<T: Display + ToCanonical>(self, src: &T, extra: Option<&str>);
|
||||
fn log_debug<T: Display + ToCanonical>(self, src: &T, extra: Option<&str>);
|
||||
fn build_message<T: Display + ToCanonical>(self, src: &T, extra: Option<&str>) -> String;
|
||||
}
|
||||
impl<B> ReqLog for &Request<B> {
|
||||
fn log<T: Display + ToCanonical>(self, src: &T, extra: Option<&str>) {
|
||||
info!("{}", &self.build_message(src, extra));
|
||||
}
|
||||
fn log_debug<T: Display + ToCanonical>(self, src: &T, extra: Option<&str>) {
|
||||
debug!("{}", &self.build_message(src, extra));
|
||||
}
|
||||
fn build_message<T: Display + ToCanonical>(self, src: &T, extra: Option<&str>) -> String {
|
||||
let canonical_src = src.to_canonical();
|
||||
|
||||
let host = self
|
||||
.headers()
|
||||
.get(header::HOST)
|
||||
.map_or_else(|| "", |v| v.to_str().unwrap_or(""));
|
||||
let uri_scheme = self
|
||||
.uri()
|
||||
.scheme_str()
|
||||
.map_or_else(|| "".to_string(), |v| format!("{}://", v));
|
||||
let uri_host = self.uri().host().unwrap_or("");
|
||||
let uri_pq = self.uri().path_and_query().map_or_else(|| "", |v| v.as_str());
|
||||
let ua = self
|
||||
.headers()
|
||||
.get(header::USER_AGENT)
|
||||
.map_or_else(|| "", |v| v.to_str().unwrap_or(""));
|
||||
let xff = self
|
||||
.headers()
|
||||
.get("x-forwarded-for")
|
||||
.map_or_else(|| "", |v| v.to_str().unwrap_or(""));
|
||||
|
||||
format!(
|
||||
"{} <- {} -- {} {} {:?} -- ({}{}) \"{}\" \"{}\" {}",
|
||||
host,
|
||||
canonical_src,
|
||||
self.method(),
|
||||
uri_pq,
|
||||
self.version(),
|
||||
uri_scheme,
|
||||
uri_host,
|
||||
ua,
|
||||
xff,
|
||||
extra.unwrap_or("")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ParseHost {
|
||||
fn parse_host(&self) -> Result<&[u8]>;
|
||||
|
|
|
|||
|
|
@ -1,41 +0,0 @@
|
|||
use crate::{log::*, utils::*};
|
||||
use hyper::Response;
|
||||
use std::fmt::Display;
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
// Functions of utils for request messages
|
||||
pub trait ResLog {
|
||||
fn log<T1: Display, T2: Display + ToCanonical>(self, server_name: &T1, client_addr: &T2, extra: Option<&str>);
|
||||
fn log_debug<T1: Display, T2: Display + ToCanonical>(self, server_name: &T1, client_addr: &T2, extra: Option<&str>);
|
||||
fn build_message<T1: Display, T2: Display + ToCanonical>(
|
||||
self,
|
||||
server_name: &T1,
|
||||
client_addr: &T2,
|
||||
extra: Option<&str>,
|
||||
) -> String;
|
||||
}
|
||||
impl<B> ResLog for &Response<B> {
|
||||
fn log<T1: Display, T2: Display + ToCanonical>(self, server_name: &T1, client_addr: &T2, extra: Option<&str>) {
|
||||
info!("{}", &self.build_message(server_name, client_addr, extra));
|
||||
}
|
||||
fn log_debug<T1: Display, T2: Display + ToCanonical>(self, server_name: &T1, client_addr: &T2, extra: Option<&str>) {
|
||||
debug!("{}", &self.build_message(server_name, client_addr, extra));
|
||||
}
|
||||
fn build_message<T1: Display, T2: Display + ToCanonical>(
|
||||
self,
|
||||
server_name: &T1,
|
||||
client_addr: &T2,
|
||||
extra: Option<&str>,
|
||||
) -> String {
|
||||
let canonical_client_addr = client_addr.to_canonical();
|
||||
format!(
|
||||
"{} <- {} -- {} {:?} {}",
|
||||
canonical_client_addr,
|
||||
server_name,
|
||||
self.status(),
|
||||
self.version(),
|
||||
// self.headers(),
|
||||
extra.map_or_else(|| "", |v| v)
|
||||
)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue