use axum::extract::{Request, State};
use axum::http::header;
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use std::path::PathBuf;
use crate::server;
#[rustfmt::skip]
static TEXT_EXTENSIONS: &[&str] = &[
"c", "cc", "cpp", "csv", "fst", "h",
"java", "md", "mk", "proto", "py",
"rb", "rs", "rst", "sh", "toml", "yml",
];
#[rustfmt::skip]
static TEXT_FILES: &[&str] = &[
".gitattributes", ".gitignore", ".mailmap",
"AUTHORS", "CODE_OF_CONDUCT", "CONTRIBUTING",
"COPYING", "COPYRIGHT", "Cargo.lock",
"LICENSE", "LICENSE-APACHE", "LICENSE-MIT",
"Makefile", "rust-toolchain",
];
pub async fn source_text_middleware(
State(root_dir): State<PathBuf>,
req: Request,
next: Next,
) -> Response {
let uri_path = req.uri().path().to_string();
let resp = next.run(req).await;
if !should_convert(&uri_path) {
return resp;
}
if let Ok(path) = server::local_path_for_request(&uri_path, &root_dir) {
if path.is_dir() {
return resp;
}
}
tracing::trace!("source text override: {}", uri_path);
let (mut parts, body) = resp.into_parts();
parts
.headers
.insert(header::CONTENT_TYPE, "text/plain".parse().unwrap());
Response::from_parts(parts, body).into_response()
}
fn should_convert(path: &str) -> bool {
let file_name = match path.rsplit('/').next() {
Some(n) => n,
None => return false,
};
if TEXT_FILES.contains(&file_name) {
return true;
}
if let Some(ext) = file_name.rsplit('.').next() {
if TEXT_EXTENSIONS.contains(&ext) {
return true;
}
}
false
}