feat: optimize find_matching_rule func (#840)

Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
Gaius 2024-11-13 14:12:16 +08:00 committed by GitHub
parent 6d27c92ac6
commit 0e220c4605
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 18 additions and 8 deletions

View File

@ -332,9 +332,10 @@ pub async fn http_handler(
// If find the matching rule, proxy the request via the dfdaemon.
let request_uri = request.uri();
if let Some(rule) =
find_matching_rule(config.proxy.rules.clone(), request_uri.to_string().as_str())
{
if let Some(rule) = find_matching_rule(
config.proxy.rules.as_deref(),
request_uri.to_string().as_str(),
) {
info!(
"proxy HTTP request via dfdaemon by rule config for method: {}, uri: {}",
request.method(),
@ -534,9 +535,10 @@ pub async fn upgraded_handler(
// If find the matching rule, proxy the request via the dfdaemon.
let request_uri = request.uri();
if let Some(rule) =
find_matching_rule(config.proxy.rules.clone(), request_uri.to_string().as_str())
{
if let Some(rule) = find_matching_rule(
config.proxy.rules.as_deref(),
request_uri.to_string().as_str(),
) {
info!(
"proxy HTTPS request via dfdaemon by rule config for method: {}, uri: {}",
request.method(),
@ -1068,8 +1070,16 @@ fn make_response_headers(
/// find_matching_rule returns whether the dfdaemon should be used to download the task.
/// If the dfdaemon should be used, return the matched rule.
#[instrument(skip_all)]
fn find_matching_rule(rules: Option<Vec<Rule>>, url: &str) -> Option<Rule> {
rules?.iter().find(|rule| rule.regex.is_match(url)).cloned()
fn find_matching_rule(rules: Option<&[Rule]>, url: &str) -> Option<Rule> {
if let Some(rules) = rules {
for rule in rules {
if rule.regex.is_match(url) {
return Some(rule.clone());
}
}
}
None
}
/// make_error_response makes an error response with the given status and message.