use axum::extract::{Path, State};
use axum::http::{header, HeaderMap, HeaderValue, StatusCode};
use axum::body::Body;
use axum::response::{IntoResponse, Redirect, Response};
use axum::Json;
use super::accounts::PartTypeView;
use super::{identify, Shared};
use crate::cad;
pub async fn entry() -> Redirect {
Redirect::to(cad::ENTRY)
}
pub async fn file(State(db): State<Shared>, Path(path): Path<String>, headers: HeaderMap) -> Response {
let config = &db.security().config;
let connect = &config.cad_connect_src;
let bare = cad::policy(&(Vec::new(), Vec::new()), connect);
let refuse = |status: StatusCode, message: &str| {
let mut response = (status, Json(serde_json::json!({ "error": message }))).into_response();
set(&mut response, header::CONTENT_SECURITY_POLICY, &bare);
set(&mut response, header::CACHE_CONTROL, "no-store");
response
};
let Some(root) = config.cad_app_dir.as_deref().filter(|dir| dir.is_dir()) else {
return refuse(
StatusCode::NOT_FOUND,
"no CAD app is installed on this server — the administrator puts a build's web/ and pkg/ into the --cad-app directory",
);
};
let path = if path.is_empty() || path.ends_with('/') { format!("{path}index.html") } else { path };
let Some(found) = cad::resolve(root, &path) else {
return refuse(StatusCode::NOT_FOUND, "no such file in the CAD app");
};
let meta = match std::fs::metadata(&found) {
Ok(meta) => meta,
Err(_) => return refuse(StatusCode::NOT_FOUND, "no such file in the CAD app"),
};
let etag = cad::etag(&meta);
let media = cad::media_type(&found);
let page = media.starts_with("text/html");
let fresh = headers
.get(header::IF_NONE_MATCH)
.and_then(|v| v.to_str().ok())
.is_some_and(|sent| sent.split(',').any(|tag| tag.trim() == etag || tag.trim() == "*"));
let (body, policy) = if page {
let text = match std::fs::read_to_string(&found) {
Ok(text) => text,
Err(error) => return refuse(StatusCode::INTERNAL_SERVER_ERROR, &format!("cannot read the page: {error}")),
};
let policy = cad::policy(&cad::inline_hashes(&text), connect);
(Body::from(text), policy)
} else if fresh {
(Body::empty(), bare)
} else {
let file = match tokio::fs::File::open(&found).await {
Ok(file) => file,
Err(error) => return refuse(StatusCode::INTERNAL_SERVER_ERROR, &format!("cannot read the file: {error}")),
};
(Body::from_stream(stream(file)), bare)
};
let mut response = if fresh {
StatusCode::NOT_MODIFIED.into_response()
} else {
let mut response = body.into_response();
set(&mut response, header::CONTENT_TYPE, media);
response.headers_mut().insert(header::CONTENT_LENGTH, HeaderValue::from(meta.len()));
response
};
set(&mut response, header::ETAG, &etag);
set(&mut response, header::CACHE_CONTROL, "no-cache");
set(&mut response, header::CONTENT_SECURITY_POLICY, &policy);
response
}
fn set(response: &mut Response, name: header::HeaderName, value: &str) {
if let Ok(value) = HeaderValue::from_str(value) {
response.headers_mut().insert(name, value);
}
}
fn stream(file: tokio::fs::File) -> impl futures_util::Stream<Item = Result<Vec<u8>, std::io::Error>> {
futures_util::stream::unfold(file, |mut file| async move {
use tokio::io::AsyncReadExt;
let mut buffer = vec![0u8; 256 * 1024];
match file.read(&mut buffer).await {
Ok(0) => None,
Ok(n) => {
buffer.truncate(n);
Some((Ok(buffer), file))
}
Err(e) => Some((Err(e), file)),
}
})
}
pub async fn config(State(db): State<Shared>, headers: HeaderMap) -> Response {
let security = &db.security().config;
let scheme = if security.is_secure_request(&headers) { "https" } else { "http" };
let url = headers
.get(header::HOST)
.and_then(|v| v.to_str().ok())
.filter(|host| !host.is_empty() && host.chars().all(|c| c.is_ascii_graphic()))
.map(|host| format!("{scheme}://{host}"));
let app = security
.cad_app_dir
.as_deref()
.is_some_and(|dir| cad::resolve(dir, "web/index.html").is_some())
.then_some(cad::ENTRY);
let root = security.cad_app_dir.clone();
let (native, installers) = tokio::task::spawn_blocking(move || {
let list = |entries: &[(&str, &str)]| {
entries
.iter()
.map(|(name, path)| {
let found = root.as_deref().and_then(|dir| cad::listed(dir, path));
(name.to_string(), serde_json::to_value(found).unwrap_or_default())
})
.collect::<serde_json::Map<_, _>>()
};
(list(cad::NATIVE), list(cad::INSTALLERS))
})
.await
.unwrap_or_default();
let part_types: Option<Vec<PartTypeView>> = identify(&db, &headers)
.ok()
.map(|_| db.read(|state| state.part_types.iter().map(PartTypeView::from).collect()));
let mut response = Json(serde_json::json!({
"url": url,
"server_version": crate::version::SERVER_VERSION,
"min_client_version": security.min_client_version(),
"features": crate::version::FEATURES,
"app": app,
"part_types": part_types,
"native": native,
"installers": installers,
"connect_src": security.cad_connect_src,
}))
.into_response();
set(&mut response, header::CACHE_CONTROL, "no-store");
response
}