From 747268098b4310f83afd3a321f26f797f7ebdaab Mon Sep 17 00:00:00 2001 From: Jerboa-app Date: Sat, 23 Mar 2024 08:20:53 +0000 Subject: [PATCH] include domain in is_page check --- src/pages/page.rs | 6 ++---- src/web/stats.rs | 4 +++- tests/test_regex.rs | 39 ++++++++++++++++++++++++++++++--------- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/pages/page.rs b/src/pages/page.rs index 9ec7308..b9fdfa2 100644 --- a/src/pages/page.rs +++ b/src/pages/page.rs @@ -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 @@ -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) => { diff --git a/src/web/stats.rs b/src/web/stats.rs index 68e8ce3..b76a4ac 100644 --- a/src/web/stats.rs +++ b/src/web/stats.rs @@ -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}; @@ -289,7 +291,7 @@ impl Stats } } - if is_page(&hit.path) + if is_page(&hit.path, &config.domain) { match pages.contains_key(&hit.path) { diff --git a/tests/test_regex.rs b/tests/test_regex.rs index f80b221..4d2e584 100644 --- a/tests/test_regex.rs +++ b/tests/test_regex.rs @@ -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")); } } \ No newline at end of file