Skip to content

Commit

Permalink
apply all matched rules in the order of rule list (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexilee authored May 21, 2021
1 parent 4da2004 commit 5b545b3
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions src/tproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,18 @@ impl HttpService {
}

async fn handle(self, mut request: Request<Body>) -> Result<Response<Body>> {
if let Some(rule) = self.config.rules.iter().find(|rule| {
matches!(rule.target, Target::Request)
&& select_request(self.target.port(), &request, &rule.selector)
}) {
debug!("request matched");
let request_rules: Vec<_> = self
.config
.rules
.iter()
.filter(|rule| {
matches!(rule.target, Target::Request)
&& select_request(self.target.port(), &request, &rule.selector)
})
.collect();

for rule in request_rules {
debug!("request matched, rule({:?})", rule);
request = apply_request_action(request, &rule.actions).await?;
}

Expand All @@ -139,17 +146,24 @@ impl HttpService {
}
};

if let Some(rule) = self.config.rules.iter().find(|rule| {
matches!(rule.target, Target::Response)
&& select_response(
self.target.port(),
&uri,
&method,
&headers,
&response,
&rule.selector,
)
}) {
let response_rules: Vec<_> = self
.config
.rules
.iter()
.filter(|rule| {
matches!(rule.target, Target::Response)
&& select_response(
self.target.port(),
&uri,
&method,
&headers,
&response,
&rule.selector,
)
})
.collect();

for rule in response_rules {
debug!("response matched");
response = apply_response_action(response, &rule.actions).await?;
}
Expand Down

0 comments on commit 5b545b3

Please sign in to comment.