Skip to content

Commit 94b1cec

Browse files
authored
Merge pull request #8904 from jolicode/project-configuration-domains
Do not consider unknown domain as a redirection loop
2 parents 2882c91 + 692a7b1 commit 94b1cec

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

src/api/explain_request.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ pub struct ExplainRequestInput {
1919
pub example: Example,
2020
pub rules: Vec<Rule>,
2121
pub max_hops: u8,
22+
#[serde(default)]
23+
pub project_domains: Vec<String>,
2224
}
2325

2426
#[derive(Deserialize, Debug, Clone)]
2527
pub struct ExplainRequestProjectInput {
2628
pub example: Example,
2729
pub change_set: RuleChangeSet,
2830
pub max_hops: u8,
31+
#[serde(default)]
32+
pub project_domains: Vec<String>,
2933
}
3034

3135
// Output
@@ -66,6 +70,7 @@ impl ExplainRequestOutput {
6670
&explain_request_router,
6771
&explain_request_input.example,
6872
explain_request_input.max_hops,
73+
explain_request_input.project_domains,
6974
)
7075
}
7176

@@ -78,10 +83,20 @@ impl ExplainRequestOutput {
7883
router.insert(rule.clone());
7984
}
8085

81-
Self::create_result(&router, &explain_request_input.example, explain_request_input.max_hops)
86+
Self::create_result(
87+
&router,
88+
&explain_request_input.example,
89+
explain_request_input.max_hops,
90+
explain_request_input.project_domains,
91+
)
8292
}
8393

84-
fn create_result(router: &Router<Rule>, example: &Example, max_hops: u8) -> Result<ExplainRequestOutput, ExplainRequestOutputError> {
94+
fn create_result(
95+
router: &Router<Rule>,
96+
example: &Example,
97+
max_hops: u8,
98+
project_domains: Vec<String>,
99+
) -> Result<ExplainRequestOutput, ExplainRequestOutputError> {
85100
let request = match Request::from_example(&router.config, example) {
86101
Ok(request) => request,
87102
Err(e) => {
@@ -121,7 +136,7 @@ impl ExplainRequestOutput {
121136

122137
unit_trace.squash_with_target_unit_traces();
123138

124-
let redirection_loop = Some(RedirectionLoop::from_example(router, max_hops, example));
139+
let redirection_loop = Some(RedirectionLoop::from_example(router, max_hops, example, project_domains));
125140

126141
Ok(ExplainRequestOutput {
127142
example: example.to_owned(),

src/api/impact.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub struct ImpactInput {
1616
pub router_config: RouterConfig,
1717
pub max_hops: u8,
1818
pub with_redirection_loop: bool,
19+
#[serde(default)]
20+
pub domains: Vec<String>,
1921
pub rule: Rule,
2022
pub action: String,
2123
pub rules: Vec<Rule>,
@@ -25,6 +27,8 @@ pub struct ImpactInput {
2527
pub struct ImpactProjectInput {
2628
pub max_hops: u8,
2729
pub with_redirection_loop: bool,
30+
#[serde(default)]
31+
pub domains: Vec<String>,
2832
pub rule: Rule,
2933
pub action: String,
3034
pub change_set: RuleChangeSet,
@@ -88,6 +92,7 @@ impl ImpactOutput {
8892
impact_input.max_hops,
8993
impact_input.action.as_str(),
9094
impact_input.rule,
95+
impact_input.domains,
9196
)
9297
}
9398

@@ -114,9 +119,11 @@ impl ImpactOutput {
114119
impact_input.max_hops,
115120
impact_input.action.as_str(),
116121
impact_input.rule,
122+
impact_input.domains,
117123
)
118124
}
119125

126+
#[allow(clippy::too_many_arguments)]
120127
fn compute_impacts(
121128
router: &mut Router<Rule>,
122129
trace_unique_router: &mut Router<Rule>,
@@ -125,6 +132,7 @@ impl ImpactOutput {
125132
max_hops: u8,
126133
action: &str,
127134
rule: Rule,
135+
project_domains: Vec<String>,
128136
) -> ImpactOutput {
129137
if action == "add" || action == "update" {
130138
router.insert(rule.clone());
@@ -182,7 +190,7 @@ impl ImpactOutput {
182190
unit_trace.squash_with_target_unit_traces();
183191

184192
let redirection_loop = if with_redirection_loop {
185-
Some(RedirectionLoop::from_example(router, max_hops, &example))
193+
Some(RedirectionLoop::from_example(router, max_hops, &example, project_domains.clone()))
186194
} else {
187195
None
188196
};

src/api/redirection_loop.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ enum RedirectionError {
2828
}
2929

3030
impl RedirectionLoop {
31-
pub fn from_example(router: &Router<Rule>, max_hops: u8, example: &Example) -> RedirectionLoop {
32-
Self::compute(router, max_hops, example)
31+
pub fn from_example(router: &Router<Rule>, max_hops: u8, example: &Example, project_domains: Vec<String>) -> RedirectionLoop {
32+
Self::compute(router, max_hops, example, project_domains)
3333
}
3434
pub fn has_error(&self) -> bool {
3535
self.error.is_some()
@@ -40,7 +40,7 @@ impl RedirectionLoop {
4040
pub fn has_error_loop(&self) -> bool {
4141
self.error.is_some() && matches!(self.error, Some(RedirectionError::Loop))
4242
}
43-
fn compute(router: &Router<Rule>, max_hops: u8, example: &Example) -> RedirectionLoop {
43+
fn compute(router: &Router<Rule>, max_hops: u8, example: &Example, project_domains: Vec<String>) -> RedirectionLoop {
4444
let mut current_url = example.url.clone();
4545
let mut current_method = example.method.clone().unwrap_or(String::from("GET"));
4646
let mut error = None;
@@ -121,6 +121,16 @@ impl RedirectionLoop {
121121
method: current_method.clone(),
122122
});
123123

124+
// If the url cannot be parsed, let's treat it as a relative Url.
125+
// Otherwise, we check if the corresponding domain is registered in the project.
126+
if let Ok(url) = Url::parse(&current_url) {
127+
if !project_domains.is_empty() && !project_domains.contains(&url.host_str().unwrap().to_string()) {
128+
// The current url target a domain that is not registered in the project.
129+
// So we consider there is no redirection loop here.
130+
break;
131+
}
132+
}
133+
124134
if i >= max_hops {
125135
error = Some(RedirectionError::TooManyHops);
126136
break;

src/api/test_examples.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ pub struct TestExamplesInput {
2222
pub router_config: RouterConfig,
2323
pub rules: Vec<Rule>,
2424
pub max_hops: u8,
25+
#[serde(default)]
26+
pub project_domains: Vec<String>,
2527
}
2628

2729
#[derive(Deserialize, Debug, Clone, Default)]
2830
pub struct TestExamplesProjectInput {
2931
pub change_set: RuleChangeSet,
3032
pub max_hops: u8,
33+
#[serde(default)]
34+
pub project_domains: Vec<String>,
3135
}
3236

3337
#[derive(Serialize, Debug, Clone, Default)]
@@ -70,7 +74,11 @@ impl TestExamplesOutput {
7074
pub fn from_project(test_examples_input: TestExamplesProjectInput, existing_router: Arc<Router<Rule>>) -> TestExamplesOutput {
7175
let test_example_router = test_examples_input.change_set.update_existing_router(existing_router);
7276

73-
Self::create_result(&test_example_router, test_examples_input.max_hops)
77+
Self::create_result(
78+
&test_example_router,
79+
test_examples_input.max_hops,
80+
test_examples_input.project_domains,
81+
)
7482
}
7583

7684
pub fn create_result_without_project(test_examples_input: TestExamplesInput) -> TestExamplesOutput {
@@ -80,10 +88,10 @@ impl TestExamplesOutput {
8088
router.insert(rule.clone());
8189
}
8290

83-
Self::create_result(&router, test_examples_input.max_hops)
91+
Self::create_result(&router, test_examples_input.max_hops, test_examples_input.project_domains)
8492
}
8593

86-
fn create_result(router: &Router<Rule>, max_hops: u8) -> TestExamplesOutput {
94+
fn create_result(router: &Router<Rule>, max_hops: u8, project_domains: Vec<String>) -> TestExamplesOutput {
8795
let mut results = TestExamplesOutput::default();
8896

8997
for (id, route) in router.routes() {
@@ -94,7 +102,15 @@ impl TestExamplesOutput {
94102
}
95103

96104
for example in examples.as_ref().unwrap().iter() {
97-
Self::test_example(router, example, &mut results, id.as_str(), route.clone(), max_hops);
105+
Self::test_example(
106+
router,
107+
example,
108+
&mut results,
109+
id.as_str(),
110+
route.clone(),
111+
max_hops,
112+
project_domains.clone(),
113+
);
98114
}
99115
}
100116

@@ -108,6 +124,7 @@ impl TestExamplesOutput {
108124
id: &str,
109125
route: Arc<Route<Rule>>,
110126
max_hops: u8,
127+
project_domains: Vec<String>,
111128
) {
112129
if example.unit_ids_applied.is_none() {
113130
return;
@@ -172,7 +189,7 @@ impl TestExamplesOutput {
172189
None,
173190
);
174191
} else {
175-
let redirection_loop = RedirectionLoop::from_example(router, max_hops, example);
192+
let redirection_loop = RedirectionLoop::from_example(router, max_hops, example, project_domains);
176193

177194
if redirection_loop.has_error_too_many_hops() || redirection_loop.has_error_loop() {
178195
results.add_failed_example(

0 commit comments

Comments
 (0)