update path pattern matcher
This commit is contained in:
parent
e52ed43050
commit
087eba1d07
3 changed files with 49 additions and 19 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
use crate::backend_opt::UpstreamOption;
|
use crate::{backend_opt::UpstreamOption, log::*};
|
||||||
use crate::log::*;
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
|
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
|
||||||
use std::{
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{self, BufReader, Cursor, Read},
|
io::{self, BufReader, Cursor, Read},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
|
|
@ -15,6 +15,7 @@ use tokio_rustls::rustls::{Certificate, PrivateKey, ServerConfig};
|
||||||
|
|
||||||
// server name (hostname or ip address) in ascii lower case
|
// server name (hostname or ip address) in ascii lower case
|
||||||
pub type ServerNameLC = Vec<u8>;
|
pub type ServerNameLC = Vec<u8>;
|
||||||
|
pub type PathNameLC = Vec<u8>;
|
||||||
|
|
||||||
pub struct Backends {
|
pub struct Backends {
|
||||||
pub apps: HashMap<ServerNameLC, Backend>, // TODO: hyper::uriで抜いたhostで引っ掛ける。Stringでいいのか?
|
pub apps: HashMap<ServerNameLC, Backend>, // TODO: hyper::uriで抜いたhostで引っ掛ける。Stringでいいのか?
|
||||||
|
|
@ -34,8 +35,42 @@ pub struct Backend {
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ReverseProxy {
|
pub struct ReverseProxy {
|
||||||
pub default_upstream: Option<Upstream>,
|
pub upstream: HashMap<PathNameLC, Upstream>,
|
||||||
pub upstream: HashMap<String, Upstream>,
|
}
|
||||||
|
|
||||||
|
impl ReverseProxy {
|
||||||
|
pub fn get<'a>(&self, path_str: impl Into<Cow<'a, str>>) -> Option<&Upstream> {
|
||||||
|
// trie使ってlongest prefix match させてもいいけどルート記述は少ないと思われるので、
|
||||||
|
// コスト的にこの程度で十分
|
||||||
|
let path_lc = path_str.into().to_ascii_lowercase();
|
||||||
|
let path_bytes = path_lc.as_bytes();
|
||||||
|
|
||||||
|
let matched_upstream = self
|
||||||
|
.upstream
|
||||||
|
.iter()
|
||||||
|
.filter(|(route_bytes, _)| {
|
||||||
|
match path_bytes.starts_with(route_bytes) {
|
||||||
|
true => {
|
||||||
|
route_bytes.len() == 1 // route = '/', i.e., default
|
||||||
|
|| match path_bytes.get(route_bytes.len()) {
|
||||||
|
None => true, // exact case
|
||||||
|
Some(p) => p == &b'/', // sub-path case
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.max_by_key(|(route_bytes, _)| route_bytes.len());
|
||||||
|
if let Some((_path, u)) = matched_upstream {
|
||||||
|
debug!(
|
||||||
|
"Found upstream: {:?}",
|
||||||
|
String::from_utf8(_path.to_vec()).unwrap_or_else(|_| "<none>".to_string())
|
||||||
|
);
|
||||||
|
Some(u)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use super::toml::{ConfigToml, ReverseProxyOption};
|
use super::toml::{ConfigToml, ReverseProxyOption};
|
||||||
use crate::{
|
use crate::{
|
||||||
backend::{Backend, ReverseProxy, Upstream},
|
backend::{Backend, PathNameLC, ReverseProxy, Upstream},
|
||||||
backend_opt::UpstreamOption,
|
backend_opt::UpstreamOption,
|
||||||
constants::*,
|
constants::*,
|
||||||
error::*,
|
error::*,
|
||||||
|
|
@ -169,8 +169,7 @@ pub fn parse_opts(globals: &mut Globals) -> Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_reverse_proxy(rp_settings: &[ReverseProxyOption]) -> Result<ReverseProxy> {
|
fn get_reverse_proxy(rp_settings: &[ReverseProxyOption]) -> Result<ReverseProxy> {
|
||||||
let mut upstream: HashMap<String, Upstream> = HashMap::default();
|
let mut upstream: HashMap<PathNameLC, Upstream> = HashMap::default();
|
||||||
let mut default_upstream: Option<Upstream> = None;
|
|
||||||
rp_settings.iter().for_each(|rpo| {
|
rp_settings.iter().for_each(|rpo| {
|
||||||
let elem = Upstream {
|
let elem = Upstream {
|
||||||
uri: rpo.upstream.iter().map(|x| x.to_uri().unwrap()).collect(),
|
uri: rpo.upstream.iter().map(|x| x.to_uri().unwrap()).collect(),
|
||||||
|
|
@ -189,17 +188,17 @@ fn get_reverse_proxy(rp_settings: &[ReverseProxyOption]) -> Result<ReverseProxy>
|
||||||
};
|
};
|
||||||
|
|
||||||
if rpo.path.is_some() {
|
if rpo.path.is_some() {
|
||||||
upstream.insert(rpo.path.as_ref().unwrap().to_owned(), elem);
|
upstream.insert(
|
||||||
|
rpo.path.as_ref().unwrap().as_bytes().to_ascii_lowercase(),
|
||||||
|
elem,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
default_upstream = Some(elem)
|
upstream.insert("/".as_bytes().to_ascii_lowercase(), elem);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ensure!(
|
ensure!(
|
||||||
rp_settings.iter().filter(|rpo| rpo.path.is_none()).count() < 2,
|
rp_settings.iter().filter(|rpo| rpo.path.is_none()).count() < 2,
|
||||||
"Multiple default reverse proxy setting"
|
"Multiple default reverse proxy setting"
|
||||||
);
|
);
|
||||||
Ok(ReverseProxy {
|
Ok(ReverseProxy { upstream })
|
||||||
default_upstream,
|
|
||||||
upstream,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,14 +54,11 @@ where
|
||||||
return secure_redirection(&backend.server_name, self.globals.https_port, &req);
|
return secure_redirection(&backend.server_name, self.globals.https_port, &req);
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////
|
|
||||||
// Find reverse proxy for given path and choose one of upstream host
|
// Find reverse proxy for given path and choose one of upstream host
|
||||||
// TODO: More flexible path matcher
|
// Longest prefix match
|
||||||
let path = req.uri().path();
|
let path = req.uri().path();
|
||||||
let upstream = if let Some(upstream) = backend.reverse_proxy.upstream.get(path) {
|
let upstream = if let Some(upstream) = backend.reverse_proxy.get(path) {
|
||||||
upstream
|
upstream
|
||||||
} else if let Some(default_upstream) = &backend.reverse_proxy.default_upstream {
|
|
||||||
default_upstream
|
|
||||||
} else {
|
} else {
|
||||||
return http_error(StatusCode::NOT_FOUND);
|
return http_error(StatusCode::NOT_FOUND);
|
||||||
};
|
};
|
||||||
|
|
@ -70,7 +67,6 @@ where
|
||||||
} else {
|
} else {
|
||||||
return http_error(StatusCode::INTERNAL_SERVER_ERROR);
|
return http_error(StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
};
|
};
|
||||||
///////////////////////
|
|
||||||
|
|
||||||
// Upgrade in request header
|
// Upgrade in request header
|
||||||
let upgrade_in_request = extract_upgrade(req.headers());
|
let upgrade_in_request = extract_upgrade(req.headers());
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue