Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tonic-xds/examples/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("Channel built. Sending requests (Ctrl-C to stop)...\n");

for i in 1.. {
let mut i = 1_u64;
loop {
let request = HelloRequest {
name: format!("request-{i}"),
};
Expand All @@ -87,7 +88,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}

tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
i = i.checked_add(1).ok_or("request counter exhausted")?;
}

Ok(())
}
5 changes: 2 additions & 3 deletions tonic-xds/src/xds/resource/route_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,7 @@ fn validate_route(
.ok_or_else(|| Error::Validation("route missing action field".into()))?;

let validated_action;
let route_retry;
match action {
let route_retry = match action {
route::Action::Route(mut route_action) => {
// Take the retry policy before `route_action` is consumed; parse it
// only if the route is kept, so dropped routes cost no parse.
Expand All @@ -418,7 +417,7 @@ fn validate_route(
Some(action) => validated_action = action,
None => return Ok(None),
}
route_retry = retry_policy.as_ref().map(parse_retry).transpose()?;
retry_policy.as_ref().map(parse_retry).transpose()?
}
// Per A28: action field must be "route", otherwise NACK.
_ => {
Expand Down
4 changes: 2 additions & 2 deletions tonic-xds/src/xds/resource/string_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ impl StringMatcher {
}
}

fn starts_with_ignore_ascii_case(s: &str, pat: &str) -> bool {
pub(crate) fn starts_with_ignore_ascii_case(s: &str, pat: &str) -> bool {
let (s, pat) = (s.as_bytes(), pat.as_bytes());
s.len() >= pat.len() && s[..pat.len()].eq_ignore_ascii_case(pat)
}

fn ends_with_ignore_ascii_case(s: &str, pat: &str) -> bool {
pub(crate) fn ends_with_ignore_ascii_case(s: &str, pat: &str) -> bool {
let (s, pat) = (s.as_bytes(), pat.as_bytes());
s.len()
.checked_sub(pat.len())
Expand Down
48 changes: 38 additions & 10 deletions tonic-xds/src/xds/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ use crate::xds::resource::route_config::{
HeaderMatchSpecifierConfig, HeaderMatcherConfig, PathSpecifierConfig, RouteConfig,
RouteConfigAction, RouteConfigMatch, RouteConfigResource, VirtualHostConfig, WeightedCluster,
};
use crate::xds::resource::string_matcher::{
ends_with_ignore_ascii_case, starts_with_ignore_ascii_case,
};

/// Default timeout for waiting for the initial route config (matches gRFC A57
/// resource initial timeout).
Expand Down Expand Up @@ -280,29 +283,28 @@ fn match_domain(authority: &str, pattern: &str) -> Option<DomainMatchScore> {
return Some(DomainMatchScore(DomainMatchType::Universal, Reverse(0)));
}

let authority_lower = authority.to_ascii_lowercase();
let pattern_lower = pattern.to_ascii_lowercase();

if authority_lower == pattern_lower {
if authority.eq_ignore_ascii_case(pattern) {
return Some(DomainMatchScore(
DomainMatchType::Exact,
Reverse(pattern.len()),
));
}

if let Some(suffix) = pattern_lower.strip_prefix(WILDCARD)
&& authority_lower.ends_with(suffix)
&& authority_lower.len() > suffix.len()
// The wildcard must absorb at least one authority byte, so the
// authority has to be strictly longer than the pattern tail.
if let Some(suffix) = pattern.strip_prefix(WILDCARD)
&& authority.len() > suffix.len()
&& ends_with_ignore_ascii_case(authority, suffix)
{
return Some(DomainMatchScore(
DomainMatchType::Suffix,
Reverse(suffix.len()),
));
}

if let Some(prefix) = pattern_lower.strip_suffix(WILDCARD)
&& authority_lower.starts_with(prefix)
&& authority_lower.len() > prefix.len()
if let Some(prefix) = pattern.strip_suffix(WILDCARD)
&& authority.len() > prefix.len()
&& starts_with_ignore_ascii_case(authority, prefix)
{
return Some(DomainMatchScore(
DomainMatchType::Prefix,
Expand Down Expand Up @@ -484,6 +486,32 @@ mod tests {
assert!(rc.route("bar.foo", "/", &h).is_err());
}

#[test]
fn domain_suffix_wildcard_case_insensitive() {
let rc = simple_rc(vec![VirtualHostConfig {
name: "vh1".into(),
domains: vec!["*.FOO.com".into()],
routes: vec![simple_route("/", "c1")],
}]);
let h = http::HeaderMap::new();
assert!(rc.route("bar.foo.com", "/", &h).is_ok());
assert!(rc.route("BAR.foo.COM", "/", &h).is_ok());
assert!(rc.route("foo.com", "/", &h).is_err());
}

#[test]
fn domain_prefix_wildcard_case_insensitive() {
let rc = simple_rc(vec![VirtualHostConfig {
name: "vh1".into(),
domains: vec!["FOO.*".into()],
routes: vec![simple_route("/", "c1")],
}]);
let h = http::HeaderMap::new();
assert!(rc.route("foo.bar", "/", &h).is_ok());
assert!(rc.route("FOO.bar", "/", &h).is_ok());
assert!(rc.route("FOO.", "/", &h).is_err());
}

#[test]
fn domain_universal() {
let rc = simple_rc(vec![VirtualHostConfig {
Expand Down
Loading