-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathsource_text.rs
More file actions
80 lines (67 loc) · 2.17 KB
/
source_text.rs
File metadata and controls
80 lines (67 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! Source code text extension.
//!
//! Overrides the Content-Type of known source code and metadata files to
//! `text/plain`, so they render directly in the browser instead of being
//! downloaded.
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;
/// File extensions that should be served as plain text.
#[rustfmt::skip]
static TEXT_EXTENSIONS: &[&str] = &[
"c", "cc", "cpp", "csv", "fst", "h",
"java", "md", "mk", "proto", "py",
"rb", "rs", "rst", "sh", "toml", "yml",
];
/// Named files that should be served as plain text.
#[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",
];
/// Middleware that overrides Content-Type to text/plain for source code files.
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;
}
// Check if it's a directory listing (already HTML from dir_list middleware).
// Don't override directories that happened to match.
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
}