Skip to content

Commit

Permalink
feat(response): PLAIN_TEXT_ROUTES wildcard matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuwn committed Jun 15, 2024
1 parent 1b38902 commit b2fec31
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ KEEP_GEMINI_EXACT=gemini://fuwn.me/skills
# KEEP_GEMINI_DOMAIN=fuwn.me
# PROXY_BY_DEFAULT=true
FAVICON_EXTERNAL=https://host.fuwn.me/8te8lw0lxm03.webp
PLAIN_TEXT_ROUTE=/robots.txt
PLAIN_TEXT_ROUTE=/robots.txt,*.xml
5 changes: 4 additions & 1 deletion Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ FAVICON_EXTERNAL=https://example.com/favicon.ico

A comma-separated list of paths to treat as plain text routes

These patterns do not support regular expressions, but do support the use of `*`
as a wildcard.

```dotenv
PLAIN_TEXT_ROUTE=/robots.txt,/license.txt
PLAIN_TEXT_ROUTE=/robots.txt,/license.txt,*.xml
```

## `MATHJAX`
Expand Down
38 changes: 37 additions & 1 deletion src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,10 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".<
));

if let Ok(plain_texts) = var("PLAIN_TEXT_ROUTE") {
if plain_texts.split(',').any(|r| r == req.path()) {
if plain_texts.split(',').any(|r| {
path_matches_pattern(r, req.path())
|| path_matches_pattern(r, req.path().trim_end_matches('/'))
}) {
return Ok(
HttpResponse::Ok().body(response.content().clone().unwrap_or_default()),
);
Expand All @@ -304,3 +307,36 @@ For example: to proxy "gemini://fuwn.me/uptime", visit "/proxy/fuwn.me/uptime".<
.body(html_context),
)
}

fn path_matches_pattern(pattern: &str, path: &str) -> bool {
let parts: Vec<&str> = pattern.split('*').collect();
let mut position = 0;

if !pattern.starts_with('*') {
if let Some(part) = parts.first() {
if !path.starts_with(part) {
return false;
}

position = part.len();
}
}

for part in &parts[1..parts.len() - 1] {
if let Some(found_position) = path[position..].find(part) {
position += found_position + part.len();
} else {
return false;
}
}

if !pattern.ends_with('*') {
if let Some(part) = parts.last() {
if !path[position..].ends_with(part) {
return false;
}
}
}

true
}

0 comments on commit b2fec31

Please sign in to comment.