From 752f1f82a6f6e81be1eaf0d4cf1518b1ddbc711e Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Sun, 29 Sep 2024 21:07:11 +0200 Subject: feat(tvix/nar-bridge): treat HEAD requests explicitly We don't need to access castore for HEAD requests. Change-Id: I9365d9520d5a9e52ed92897d3c4972ec5b6e11fb Reviewed-on: https://cl.tvl.fyi/c/depot/+/12547 Autosubmit: flokli Reviewed-by: raitobezarius Tested-by: BuildkiteCI --- tvix/nar-bridge/src/lib.rs | 3 ++- tvix/nar-bridge/src/nar.rs | 14 +++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'tvix') diff --git a/tvix/nar-bridge/src/lib.rs b/tvix/nar-bridge/src/lib.rs index 79f9b7372245..c4a6c8d5f2dc 100644 --- a/tvix/nar-bridge/src/lib.rs +++ b/tvix/nar-bridge/src/lib.rs @@ -58,7 +58,8 @@ pub fn gen_router(priority: u64) -> Router { .route("/nar/:nar_str", get(four_o_four)) .route("/nar/:nar_str", head(four_o_four)) .route("/nar/:nar_str", put(nar::put)) - .route("/nar/tvix-castore/:root_node_enc", get(nar::get)) + .route("/nar/tvix-castore/:root_node_enc", get(nar::get_head)) + .route("/nar/tvix-castore/:root_node_enc", head(nar::get_head)) .route("/:narinfo_str", get(narinfo::get)) .route("/:narinfo_str", head(narinfo::head)) .route("/:narinfo_str", put(narinfo::put)) diff --git a/tvix/nar-bridge/src/nar.rs b/tvix/nar-bridge/src/nar.rs index 707c01e4bcda..d88719b02be9 100644 --- a/tvix/nar-bridge/src/nar.rs +++ b/tvix/nar-bridge/src/nar.rs @@ -23,7 +23,8 @@ pub(crate) struct GetNARParams { } #[instrument(skip(blob_service, directory_service))] -pub async fn get( +pub async fn get_head( + method: axum::http::Method, ranges: Option>, axum::extract::Path(root_node_enc): axum::extract::Path, axum::extract::Query(GetNARParams { nar_size }): Query, @@ -71,8 +72,15 @@ pub async fn get( ("cache-control", "max-age=31536000, immutable"), ("content-type", nix_http::MIME_TYPE_NAR), ], - // If this is a range request, construct a seekable NAR reader - if let Some(TypedHeader(ranges)) = ranges { + if method == axum::http::Method::HEAD { + // If this is a HEAD request, construct a response returning back the + // user-provided content-length, but don't actually talk to castore. + Response::builder() + .header("content-length", nar_size) + .body(Body::empty()) + .unwrap() + } else if let Some(TypedHeader(ranges)) = ranges { + // If this is a range request, construct a seekable NAR reader. let r = tvix_store::nar::seekable::Reader::new(root_node, blob_service, directory_service) .await -- cgit 1.4.1