diff --git a/Cargo.lock b/Cargo.lock index a4d6ea8..43b9531 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1550,7 +1550,7 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pingap" -version = "0.1.2" +version = "0.1.3" dependencies = [ "async-trait", "base64 0.22.0", diff --git a/Cargo.toml b/Cargo.toml index 9f3b447..f73a9a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [package] name = "pingap" -version = "0.1.2" +version = "0.1.3" authors = ["Tree Xie "] edition = "2021" -categories = ["http-server"] +categories = ["network-programming", "web-programming::http-server"] keywords = ["proxy", "http", "gateway"] description = "A reverse proxy like nginx" license = "Apache-2.0" diff --git a/TODO.md b/TODO.md index b1ccd6b..0cae39a 100644 --- a/TODO.md +++ b/TODO.md @@ -16,3 +16,4 @@ - [ ] status:499 for client abort - [x] support get pingap start time - [ ] custom error for pingora error +- [ ] authentication for admin page diff --git a/docs/introduction_zh.md b/docs/introduction_zh.md index ae4b624..79c4869 100644 --- a/docs/introduction_zh.md +++ b/docs/introduction_zh.md @@ -40,24 +40,25 @@ Location支持配置对应的host与path规则,path支持以下的规则,权 在server中会根据所添加的所有location列表,计算对应的权重重新排序,location的计算权限逻辑如下: ```rust -pub fn get_weight(conf: &LocationConf) -> u8 { +pub fn get_weight(&self) -> u32 { // path starts with - // = 8 - // prefix(default) 4 - // ~ 2 - // host exist 1 - let mut weighted: u8 = 0; - if let Some(path) = &conf.path { + // = 65536 + // prefix(default) 32768 + // ~ 16384 + // host exist 8192 + let mut weighted: u32 = 0; + if let Some(path) = &self.path { if path.starts_with('=') { - weighted += 8; + weighted += 65536; } else if path.starts_with('~') { - weighted += 2; + weighted += 16384; } else { - weighted += 4; + weighted += 32768; } + weighted += path.len() as u32; }; - if conf.host.is_some() { - weighted += 1; + if self.host.is_some() { + weighted += 8192; } weighted } diff --git a/src/proxy/location.rs b/src/proxy/location.rs index 5e0257d..4e50dca 100644 --- a/src/proxy/location.rs +++ b/src/proxy/location.rs @@ -1,6 +1,7 @@ use super::Upstream; use crate::cache::{convert_headers, HttpHeader}; use crate::config::LocationConf; +use pingora::http::{RequestHeader, ResponseHeader}; use regex::Regex; use snafu::{ResultExt, Snafu}; use std::sync::Arc; @@ -134,12 +135,22 @@ impl Location { None } #[inline] - pub fn get_proxy_headers(&self) -> Option> { - self.proxy_headers.clone() + pub fn insert_proxy_headers(&self, header: &mut RequestHeader) { + if let Some(arr) = &self.proxy_headers { + for (k, v) in arr { + // v validate for HeaderValue, so always no error + let _ = header.insert_header(k, v); + } + } } #[inline] - pub fn get_header(&self) -> Option> { - self.headers.clone() + pub fn insert_headers(&self, header: &mut ResponseHeader) { + if let Some(arr) = &self.headers { + for (k, v) in arr { + // v validate for HeaderValue, so always no error + let _ = header.insert_header(k, v); + } + } } } diff --git a/src/proxy/logger.rs b/src/proxy/logger.rs index d1bced0..3c761ec 100644 --- a/src/proxy/logger.rs +++ b/src/proxy/logger.rs @@ -384,7 +384,7 @@ impl Parser { #[cfg(test)] mod tests { use super::Parser; - use crate::proxy::state::State; + use crate::state::State; use pingora::http::RequestHeader; use pretty_assertions::assert_eq; use std::collections::HashMap; diff --git a/src/proxy/server.rs b/src/proxy/server.rs index e191513..d786666 100644 --- a/src/proxy/server.rs +++ b/src/proxy/server.rs @@ -382,16 +382,9 @@ impl ProxyHttp for Server { new_path = format!("{new_path}?{query}"); } // TODO parse error - if let Ok(uri) = new_path.parse::() { - header.set_uri(uri); - } - } - if let Some(arr) = lo.get_proxy_headers() { - for (k, v) in arr { - // v validate for HeaderValue, so always no error - let _ = header.insert_header(k, v); - } + let _ = new_path.parse::().map(|uri| header.set_uri(uri)); } + lo.insert_proxy_headers(header); let peer = lo .upstream .new_http_peer(ctx, header) @@ -425,23 +418,18 @@ impl ProxyHttp for Server { } if let Some(index) = ctx.location_index { if let Some(lo) = self.locations.get(index) { - if let Some(arr) = lo.get_header() { - for (k, v) in arr { - // v validate for HeaderValue, so always no error - let _ = upstream_response.insert_header(k, v); - } - } + lo.insert_headers(upstream_response) } } if let Some(p) = &self.log_parser { let mut m = HashMap::new(); for key in p.response_headers.iter() { - if let Some(value) = upstream_response.headers.get(key) { + upstream_response.headers.get(key).map(|value| { m.insert( key.to_string(), value.to_str().unwrap_or_default().to_string(), - ); - } + ) + }); } ctx.response_headers = Some(m); }