~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
34 lines · 1.1 KB Raw History
//! The authorization pages' build, embedded when the crate is compiled
//! (`build.rs`), and `/oauth/assets/<name>`, which serves it
//! (`spec/OAUTH-PAGES.md`, "Assets").

use axum::{
    extract::Path,
    http::{Method, StatusCode, header},
    response::{IntoResponse, Response},
};

include!(concat!(env!("OUT_DIR"), "/web.rs"));

/// `GET` or `HEAD /oauth/assets/<name>`: a file of the build, which never
/// changes under its name.
pub(super) async fn asset(method: Method, Path(name): Path<String>) -> Response {
    let found = ASSETS
        .binary_search_by_key(&name.as_str(), |(n, _, _)| n)
        .ok();
    match found {
        Some(i) if method == Method::GET || method == Method::HEAD => {
            let (_, content_type, bytes) = ASSETS[i];
            (
                [
                    (header::CONTENT_TYPE, content_type),
                    (header::CACHE_CONTROL, "public, max-age=31536000, immutable"),
                    (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
                ],
                bytes,
            )
                .into_response()
        }
        _ => StatusCode::NOT_FOUND.into_response(),
    }
}