Skip to content

Commit

Permalink
Add very barebones middleware to remove HTML comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fsktom committed Dec 16, 2024
1 parent b32381b commit 7c1a61c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions endsong_web/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions endsong_web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ urlencoding = "2.1"
serde = "1.0"
plotly = { version = "0.11", features = ["with-axum"] }
chrono = "0.4"
regex = "1.11"
48 changes: 48 additions & 0 deletions endsong_web/src/layers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! Contains custom [`tower`] [`tower::Layer`]s used here
//! or functions used later with [`axum::middleware::from_fn`]
use axum::{
body::{self, Body},
extract::Request,
http::header::CONTENT_TYPE,
middleware::Next,
response::Response,
};
use regex::Regex;
use tracing::trace;

/// Removes HTML comments from `text/html` responses
///
/// # Panics
///
/// I hope it doesn't panic.
pub async fn remove_html_comments(request: Request, next: Next) -> Response {
trace!("removing HTML comments");
let response = next.run(request).await;
let headers = response.headers().clone();

let Some(content_type) = response.headers().get(CONTENT_TYPE) else {
return response;
};

// thanks chat gippity
if content_type.to_str().unwrap_or("").starts_with("text/html") {
if let Ok(body_bytes) = body::to_bytes(response.into_body(), usize::MAX).await {
let body_str = String::from_utf8_lossy(&body_bytes);
// Use regex to remove HTML comments
let re = Regex::new(r"<!--.*?-->").unwrap();
let cleaned_body = re.replace_all(&body_str, "").to_string();

// Rebuild the response with the cleaned body
let body = Body::from(cleaned_body);
let mut response = Response::builder();
let h = response.headers_mut().unwrap();
*h = headers;
response.body(body).unwrap()
} else {
Response::new("body".into())
}
} else {
response
}
}
1 change: 1 addition & 0 deletions endsong_web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub mod album;
pub mod artist;
pub mod artists;
pub mod history;
pub mod layers;
pub mod song;
pub mod r#static;

Expand Down
4 changes: 3 additions & 1 deletion endsong_web/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
#![warn(clippy::allow_attributes_without_reason)]
#![warn(clippy::allow_attributes)]

use endsong_web::{album, artist, artists, history, r#static, song};
use endsong_web::{album, artist, artists, history, layers, r#static, song};
use endsong_web::{index, not_found, top_artists, AppState};

use axum::middleware;
use axum::{routing::get, routing::post, Router};
use endsong::prelude::*;
use tower_http::compression::CompressionLayer;
Expand Down Expand Up @@ -77,6 +78,7 @@ async fn main() {
.route("/history/datepicker", post(history::date_picker))
.with_state(state)
.fallback(not_found)
.layer(middleware::from_fn(layers::remove_html_comments))
.layer(compression);

let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
Expand Down

0 comments on commit 7c1a61c

Please sign in to comment.