Skip to content

Commit

Permalink
chore: update pingora version
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Apr 6, 2024
1 parent 70c62df commit 50f81a5
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 70 deletions.
81 changes: 48 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pingap"
version = "0.1.13"
version = "0.1.14"
authors = ["Tree Xie <[email protected]>"]
edition = "2021"
categories = ["network-programming", "web-programming::http-server"]
Expand Down Expand Up @@ -39,7 +39,8 @@ once_cell = "1.19.0"
path-absolutize = "3.1.1"
pingora = { version = "0.1.0", default-features = false, features = [
"lb",
], git = "https://github.com/cloudflare/pingora.git", rev = "5fdf287c4d6a9ddc8a9caf3447cd27575c13a24c" }
"openssl",
], git = "https://github.com/cloudflare/pingora.git", tag = "0.1.1" }
regex = "1.10.4"
rust-embed = { version = "8.3.0", features = ["mime-guess", "compression"] }
serde = "1.0.197"
Expand Down
4 changes: 2 additions & 2 deletions src/config/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ impl LocationConf {
let validate = |headers: &Option<Vec<String>>| -> Result<()> {
if let Some(headers) = headers {
for header in headers.iter() {
let arr = utils::split_to_two_trim(header, ":");
let arr = header.split_once(':');
if arr.is_none() {
return Err(Error::Invalid {
message: format!("{header} is invalid header(location:{name})"),
});
}
HeaderValue::from_str(&arr.unwrap()[1]).map_err(|err| Error::Invalid {
HeaderValue::from_str(arr.unwrap().1.trim()).map_err(|err| Error::Invalid {
message: format!("{}(location:{name})", err),
})?;
}
Expand Down
9 changes: 5 additions & 4 deletions src/http_extra/http_header.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::utils::split_to_two_trim;
use http::header;
use http::{HeaderName, HeaderValue};
use once_cell::sync::Lazy;
Expand All @@ -25,9 +24,11 @@ pub type HttpHeader = (HeaderName, HeaderValue);
pub fn convert_headers(header_values: &[String]) -> Result<Vec<HttpHeader>> {
let mut arr = vec![];
for item in header_values {
if let Some([k, v]) = split_to_two_trim(item, ":") {
let name = HeaderName::from_str(&k).context(InvalidHeaderNameSnafu { value: k })?;
let value = HeaderValue::from_str(&v).context(InvalidHeaderValueSnafu { value: v })?;
if let Some((k, v)) = item.split_once(':') {
let name =
HeaderName::from_str(k.trim()).context(InvalidHeaderNameSnafu { value: k })?;
let value =
HeaderValue::from_str(v.trim()).context(InvalidHeaderValueSnafu { value: v })?;
arr.push((name, value));
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ struct Args {
}

fn new_server_conf(args: &Args, conf: &PingapConf) -> server::configuration::ServerConf {
let mut server_conf = server::configuration::ServerConf::default();
server_conf.pid_file = format!("/tmp/{}.pid", utils::get_pkg_name());
server_conf.upgrade_sock = format!("/tmp/{}.sock", utils::get_pkg_name());
let mut server_conf = server::configuration::ServerConf {
pid_file: format!("/tmp/{}.pid", utils::get_pkg_name()),
upgrade_sock: format!("/tmp/{}.sock", utils::get_pkg_name()),
user: conf.user.clone(),
group: conf.group.clone(),
daemon: args.daemon,
error_log: args.log.clone(),
..Default::default()
};
if let Some(pid_file) = &conf.pid_file {
server_conf.pid_file = pid_file.to_string();
}
Expand All @@ -59,10 +65,6 @@ fn new_server_conf(args: &Args, conf: &PingapConf) -> server::configuration::Ser
if let Some(work_stealing) = conf.work_stealing {
server_conf.work_stealing = work_stealing
}
server_conf.user = conf.user.clone();
server_conf.group = conf.group.clone();
server_conf.daemon = args.daemon;
server_conf.error_log = args.log.clone();

server_conf
}
Expand Down
4 changes: 2 additions & 2 deletions src/proxy/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ impl Parser {
let cookie_value =
get_req_header_value(req_header, "Cookie").unwrap_or_default();
for item in cookie_value.split(';') {
if let Some([k, v]) = utils::split_to_two_trim(item, "=") {
if let Some((k, v)) = item.split_once('=') {
if k == cookie_name {
buf.push_str(&v);
buf.push_str(v.trim());
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/proxy/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@ impl Server {
let mut bg_services: Vec<Box<dyn IService>> = vec![];
for item in self.locations.iter() {
let name = format!("BG {}", item.upstream.name);
if let Some(up) = item.upstream.get_round_robind() {
if let Some(up) = item.upstream.as_round_robind() {
bg_services.push(Box::new(GenBackgroundService::new(name.clone(), up)));
}
if let Some(up) = item.upstream.get_consistent() {
if let Some(up) = item.upstream.as_consistent() {
bg_services.push(Box::new(GenBackgroundService::new(name, up)));
}
}
Expand Down Expand Up @@ -395,11 +395,11 @@ impl ProxyHttp for Server {
let _ = new_path.parse::<http::Uri>().map(|uri| header.set_uri(uri));
}
ctx.location_index = Some(location_index);
if let Some(dir) = lo.upstream.get_directory() {
if let Some(dir) = lo.upstream.as_directory() {
let result = dir.handle(session, ctx).await?;
return Ok(result);
}
if let Some(mock) = lo.upstream.get_mock() {
if let Some(mock) = lo.upstream.as_mock() {
let result = mock.handle(session, ctx).await?;
return Ok(result);
}
Expand Down Expand Up @@ -488,10 +488,11 @@ impl ProxyHttp for Server {
}
}
}

fn upstream_response_body_filter(
&self,
_session: &mut Session,
body: &Option<bytes::Bytes>,
body: &mut Option<bytes::Bytes>,
_end_of_stream: bool,
ctx: &mut Self::CTX,
) {
Expand Down
10 changes: 5 additions & 5 deletions src/proxy/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,28 +324,28 @@ impl Upstream {
}

#[inline]
pub fn get_round_robind(&self) -> Option<Arc<LoadBalancer<RoundRobin>>> {
pub fn as_round_robind(&self) -> Option<Arc<LoadBalancer<RoundRobin>>> {
match &self.lb {
SelectionLb::RoundRobin(lb) => Some(lb.clone()),
_ => None,
}
}
#[inline]
pub fn get_consistent(&self) -> Option<Arc<LoadBalancer<Consistent>>> {
pub fn as_consistent(&self) -> Option<Arc<LoadBalancer<Consistent>>> {
match &self.lb {
SelectionLb::Consistent(lb) => Some(lb.clone()),
_ => None,
}
}
#[inline]
pub fn get_directory(&self) -> Option<Arc<Directory>> {
pub fn as_directory(&self) -> Option<Arc<Directory>> {
match &self.lb {
SelectionLb::Directory(lb) => Some(lb.clone()),
_ => None,
}
}
#[inline]
pub fn get_mock(&self) -> Option<Arc<MockResponse>> {
pub fn as_mock(&self) -> Option<Arc<MockResponse>> {
match &self.lb {
SelectionLb::Mock(lb) => Some(lb.clone()),
_ => None,
Expand Down Expand Up @@ -444,6 +444,6 @@ mod tests {
true,
up.new_http_peer(&State::default(), &session).is_some()
);
assert_eq!(true, up.get_round_robind().is_some());
assert_eq!(true, up.as_round_robind().is_some());
}
}
10 changes: 0 additions & 10 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ use pingora::proxy::Session;
use std::{path::Path, str::FromStr};
use substring::Substring;

pub fn split_to_two_trim(value: &str, pat: &str) -> Option<[String; 2]> {
let arr: Vec<&str> = value.split(pat).collect();
if arr.len() < 2 {
return None;
}
let value = arr[1..].join(pat).trim().to_string();

Some([arr[0].trim().to_string(), value])
}

const NAME: &str = env!("CARGO_PKG_NAME");
const VERSION: &str = env!("CARGO_PKG_VERSION");

Expand Down

0 comments on commit 50f81a5

Please sign in to comment.