pub struct Router {
pub tree: TrieNode<Vec<(PathRule, MethodRule, HeaderRules, Route)>>,
/* private fields */
}Expand description
Clone lets a listener added next to a sibling on the same address take
over its routes (HttpProxy::add_listener, lib/src/http.rs).
Fields§
§tree: TrieNode<Vec<(PathRule, MethodRule, HeaderRules, Route)>>Implementations§
Source§impl Router
impl Router
pub fn new() -> Router
Sourcepub fn lookup(
&self,
hostname: &str,
path: &str,
method: &Method,
) -> Result<RouteResult, RouterError>
pub fn lookup( &self, hostname: &str, path: &str, method: &Method, ) -> Result<RouteResult, RouterError>
Resolve a request to a RouteResult.
Looks up (hostname, path, method) against the pre, tree, and post
rule lists. The matched Route is converted into a RouteResult:
legacy variants (Route::ClusterId, Route::Deny) synthesize a
minimal RouteResult so existing call sites keep working, while
Route::Frontend runs the full Frontend → RouteResult pipeline,
substituting host/path captures into RewriteParts templates.
A request looked up here carries no header, so a rule with header
predicates never matches it; see Self::lookup_with_headers.
Sourcepub fn lookup_with_headers(
&self,
hostname: &str,
path: &str,
method: &Method,
headers: &dyn RequestHeaders,
) -> Result<RouteResult, RouterError>
pub fn lookup_with_headers( &self, hostname: &str, path: &str, method: &Method, headers: &dyn RequestHeaders, ) -> Result<RouteResult, RouterError>
Self::lookup, with the request’s headers for the frontends that
carry header predicates (sozu-proxy/sozu#1253).
Within each of the pre list, one trie leaf and the post list, a rule
whose predicates all hold outranks every rule without predicates;
among rules of one class the historical precedence applies
unchanged — declaration order for pre/post, the path/method order of
the private select_tree_rule for the tree. A rule whose predicates do not all
hold is skipped, exactly like a rule whose method does not match. A
route table without predicates therefore resolves every request as
it did before, and headers is never read.
Sourcepub fn add_http_front(
&mut self,
front: &HttpFrontend,
) -> Result<(), RouterError>
pub fn add_http_front( &mut self, front: &HttpFrontend, ) -> Result<(), RouterError>
Add an HTTP/HTTPS frontend whose hsts field (if any) came from
the per-frontend configuration directly. Equivalent to
Self::add_http_front_with_hsts_origin called with
HstsOrigin::Explicit. The default for callers that don’t
know about listener-default inheritance — e.g. plain HTTP
listeners (HttpListenerConfig has no HSTS field) and tests.
Sourcepub fn add_http_front_with_hsts_origin(
&mut self,
front: &HttpFrontend,
hsts_origin: HstsOrigin,
) -> Result<(), RouterError>
pub fn add_http_front_with_hsts_origin( &mut self, front: &HttpFrontend, hsts_origin: HstsOrigin, ) -> Result<(), RouterError>
Add an HTTP/HTTPS frontend, recording whether the resolved
front.hsts was inherited from the listener default. The
inheritance bit is preserved on the resulting Frontend so a
later UpdateHttpsListenerConfig.hsts patch can reflow the new
default onto inheriting entries without disturbing explicit
per-frontend overrides.
pub fn remove_http_front( &mut self, front: &HttpFrontend, ) -> Result<(), RouterError>
Sourcepub fn add_tree_rule(
&mut self,
hostname: &[u8],
path: &PathRule,
method: &MethodRule,
cluster: &Route,
) -> bool
pub fn add_tree_rule( &mut self, hostname: &[u8], path: &PathRule, method: &MethodRule, cluster: &Route, ) -> bool
Self::add_tree_rule_with_headers for a rule without header
predicates.
Sourcepub fn add_tree_rule_with_headers(
&mut self,
hostname: &[u8],
path: &PathRule,
method: &MethodRule,
headers: &HeaderRules,
cluster: &Route,
) -> bool
pub fn add_tree_rule_with_headers( &mut self, hostname: &[u8], path: &PathRule, method: &MethodRule, headers: &HeaderRules, cluster: &Route, ) -> bool
Add a tree rule. Its identity in the leaf is (path, method, headers): rules that differ only by their header predicates coexist.
Sourcepub fn remove_tree_rule(
&mut self,
hostname: &[u8],
path: &PathRule,
method: &MethodRule,
) -> bool
pub fn remove_tree_rule( &mut self, hostname: &[u8], path: &PathRule, method: &MethodRule, ) -> bool
Self::remove_tree_rule_with_headers for a rule without header
predicates.
Sourcepub fn remove_tree_rule_with_headers(
&mut self,
hostname: &[u8],
path: &PathRule,
method: &MethodRule,
headers: &HeaderRules,
) -> bool
pub fn remove_tree_rule_with_headers( &mut self, hostname: &[u8], path: &PathRule, method: &MethodRule, headers: &HeaderRules, ) -> bool
Remove the tree rule whose identity is (path, method, headers); a
rule that differs only by its header predicates stays.
Sourcepub fn refresh_inheriting_hsts(
&mut self,
new_hsts: Option<&HstsConfig>,
) -> usize
pub fn refresh_inheriting_hsts( &mut self, new_hsts: Option<&HstsConfig>, ) -> usize
Walk every route and re-materialise the response-side HSTS edit
on frontends that inherited from the listener default. Operator
per-frontend HSTS overrides (inherits_listener_hsts == false)
are left untouched.
Called from lib/src/https.rs::HttpsListener::update_config when
an UpdateHttpsListenerConfig.hsts patch is applied, and from
HttpsProxy::add_listener (lib/src/https.rs) when a listener copies the routes of a
sibling on the same address, so each listener resolves inheritance
against its own default.
Two refresh paths:
-
Route::Frontend(rc)withinherits_listener_hsts == true: rebuildheaders_responseby dropping any existingStrict-Transport-Securityentry and appending a freshly rendered one whennew_hstsresolves to an enabled value (enabled = Some(true)). The existing operatorAppend/Setresponse headers stay in place. An inheriting frontend added while the listener had no default holds noStrict-Transport-Securityentry yet and gains one here. -
Route::ClusterId(id)andRoute::Deny(lightweight “no policy” shapes): whennew_hstsresolves to enabled, promote in place to a minimalRoute::Frontend(rc)carrying just the HSTS edit onheaders_response(andinherits_listener_hsts == trueso subsequent patches keep refreshing the entry). Routing semantics are preserved — the promoted Frontend forwards / denies identically to the original variant — and the promoted entry now participates in path 1 on every later patch. Whennew_hstsresolves to “no HSTS” (None / disabled), lightweight routes are left untouched (no allocation is created just to hold an empty HSTS edit).
Path 2 fixes the case where a frontend was added without any
per-frontend policy field (the routing fast path stores it as
Route::ClusterId / Route::Deny, NOT Route::Frontend). Before
this two-path walk, listener-default HSTS patches silently
skipped every such “no-policy” frontend — which on a typical
Clever Cloud cleverapps.io shared listener was 99 % of the
frontends.
Returns the number of frontends touched. For path 1, refreshed
frontends where the new policy resolves to “no HSTS” are still
counted (the existing HSTS edit is stripped). For path 2, only
frontends actually promoted (i.e. new_hsts enabled) are
counted, since “no HSTS” is a no-op on the lightweight shape.
Sourcepub fn add_pre_rule(
&mut self,
domain: &DomainRule,
path: &PathRule,
method: &MethodRule,
cluster_id: &Route,
) -> bool
pub fn add_pre_rule( &mut self, domain: &DomainRule, path: &PathRule, method: &MethodRule, cluster_id: &Route, ) -> bool
Self::add_pre_rule_with_headers for a rule without header
predicates.
Sourcepub fn add_pre_rule_with_headers(
&mut self,
domain: &DomainRule,
path: &PathRule,
method: &MethodRule,
headers: &HeaderRules,
cluster_id: &Route,
) -> bool
pub fn add_pre_rule_with_headers( &mut self, domain: &DomainRule, path: &PathRule, method: &MethodRule, headers: &HeaderRules, cluster_id: &Route, ) -> bool
Add a pre-rule. Its identity is (domain, path, method, headers): rules that differ only by their header predicates coexist.
Sourcepub fn add_post_rule(
&mut self,
domain: &DomainRule,
path: &PathRule,
method: &MethodRule,
cluster_id: &Route,
) -> bool
pub fn add_post_rule( &mut self, domain: &DomainRule, path: &PathRule, method: &MethodRule, cluster_id: &Route, ) -> bool
Self::add_post_rule_with_headers for a rule without header
predicates.
Sourcepub fn add_post_rule_with_headers(
&mut self,
domain: &DomainRule,
path: &PathRule,
method: &MethodRule,
headers: &HeaderRules,
cluster_id: &Route,
) -> bool
pub fn add_post_rule_with_headers( &mut self, domain: &DomainRule, path: &PathRule, method: &MethodRule, headers: &HeaderRules, cluster_id: &Route, ) -> bool
Add a post-rule. Its identity is (domain, path, method, headers): rules that differ only by their header predicates coexist.
Sourcepub fn remove_pre_rule(
&mut self,
domain: &DomainRule,
path: &PathRule,
method: &MethodRule,
) -> bool
pub fn remove_pre_rule( &mut self, domain: &DomainRule, path: &PathRule, method: &MethodRule, ) -> bool
Self::remove_pre_rule_with_headers for a rule without header
predicates.
Sourcepub fn remove_pre_rule_with_headers(
&mut self,
domain: &DomainRule,
path: &PathRule,
method: &MethodRule,
headers: &HeaderRules,
) -> bool
pub fn remove_pre_rule_with_headers( &mut self, domain: &DomainRule, path: &PathRule, method: &MethodRule, headers: &HeaderRules, ) -> bool
Remove the pre-rule whose identity is (domain, path, method, headers); a rule that differs only by its header predicates stays.
Sourcepub fn remove_post_rule(
&mut self,
domain: &DomainRule,
path: &PathRule,
method: &MethodRule,
) -> bool
pub fn remove_post_rule( &mut self, domain: &DomainRule, path: &PathRule, method: &MethodRule, ) -> bool
Self::remove_post_rule_with_headers for a rule without header
predicates.
Sourcepub fn remove_post_rule_with_headers(
&mut self,
domain: &DomainRule,
path: &PathRule,
method: &MethodRule,
headers: &HeaderRules,
) -> bool
pub fn remove_post_rule_with_headers( &mut self, domain: &DomainRule, path: &PathRule, method: &MethodRule, headers: &HeaderRules, ) -> bool
Remove the post-rule whose identity is (domain, path, method, headers); a rule that differs only by its header predicates stays.
Sourcepub fn has_hostname(&self, hostname: &str) -> bool
pub fn has_hostname(&self, hostname: &str) -> bool
Returns true if any route (pre, tree, or post) references the given hostname.
This is used after removing a frontend to decide whether the hostname’s tags should be cleaned up. Tags must only be removed when no routes remain.