Skip to content

Commit

Permalink
include domain in is_page check
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed Mar 23, 2024
1 parent 18ba005 commit 7472680
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
6 changes: 2 additions & 4 deletions src/pages/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use serde::{Serialize, Deserialize};

use crate::util::read_file_utf8;

const re_is_page: &str = r"^[^\.]+$|\.html";

/// An HTML webpage with a uri and body
///
/// A Page may also be converted into an Axum HTML response via
Expand Down Expand Up @@ -94,9 +92,9 @@ impl IntoResponse for Page {
}
}

pub fn is_page(uri: &str) -> bool
pub fn is_page(uri: &str, domain: &str) -> bool
{
match Regex::new(re_is_page)
match Regex::new(format!(r"(^|(http)(s|)://{}(/|))[^\.]+$|\.html",domain).as_str())
{
Ok(re) =>
{
Expand Down
4 changes: 3 additions & 1 deletion src/web/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::sync::Arc;
use std::time::Instant;
use axum::middleware::from_fn;
use chrono::{DateTime, Datelike, TimeZone, Timelike};
use openssl::conf;
use openssl::sha::sha512;
use regex::Regex;
use tokio::sync::{Mutex, MutexGuard};

use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -289,7 +291,7 @@ impl Stats
}
}

if is_page(&hit.path)
if is_page(&hit.path, &config.domain)
{
match pages.contains_key(&hit.path)
{
Expand Down
39 changes: 30 additions & 9 deletions tests/test_regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,36 @@ mod page_regex
#[test]
fn test_is_page()
{
println!("{}", is_page("abc.html"));
assert!(is_page("abc.html"));
assert!(is_page("html.html"));
assert!(is_page("abc"));
assert!(!is_page("abc.svg"));
assert!(!is_page("abc.png"));
assert!(!is_page("abc.js"));
assert!(!is_page("html.js"));
assert!(!is_page("abc."));
println!("{}", is_page("abc.html", ""));
assert!(is_page("abc.html", ""));
assert!(is_page("html.html", ""));
assert!(is_page("abc", ""));
assert!(!is_page("abc.svg", ""));
assert!(!is_page("abc.png", ""));
assert!(!is_page("abc.js", ""));
assert!(!is_page("html.js", ""));
assert!(!is_page("abc.", ""));
assert!(!is_page("a.htm", ""));

assert!(is_page("http://domain", "domain"));
assert!(is_page("http://domain/", "domain"));
assert!(is_page("http://domain/something", "domain"));
assert!(is_page("http://domain/something.html", "domain"));

assert!(is_page("https://domain", "domain"));
assert!(is_page("https://domain/", "domain"));
assert!(is_page("https://domain/something", "domain"));
assert!(is_page("https://domain/something.html", "domain"));

assert!(!is_page("http://domain.", "domain"));
assert!(!is_page("http://domain/a.b", "domain"));
assert!(!is_page("http://domain/something.h", "domain"));
assert!(!is_page("http://domain/something.abc", "domain"));

assert!(!is_page("https://domain.", "domain"));
assert!(!is_page("https://domain/a.b", "domain"));
assert!(!is_page("https://domain/something.h", "domain"));
assert!(!is_page("https://domain/something.abc", "domain"));
}

}

0 comments on commit 7472680

Please sign in to comment.