From 3c5e970bad1a567499fd021f4cdc02d7799760d2 Mon Sep 17 00:00:00 2001 From: Lorenz Leutgeb Date: Fri, 1 May 2026 20:53:42 +0200 Subject: [PATCH] httpd/raw: Do not archive for HEAD requests `git archive` is executed for HEAD requests, even though the resulting archive is never sent to the client. Resolve this by taking the HTTP method as an argument an only producing the archive if the request method is not HEAD. --- radicle-httpd/src/raw.rs | 46 +++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/radicle-httpd/src/raw.rs b/radicle-httpd/src/raw.rs index 99fd36f5a..f3ef6508a 100644 --- a/radicle-httpd/src/raw.rs +++ b/radicle-httpd/src/raw.rs @@ -2,8 +2,9 @@ use std::process::Command; use std::str::FromStr; use std::sync::Arc; +use axum::body::Body; use axum::extract::{Query, State}; -use axum::http::{header, HeaderValue, StatusCode}; +use axum::http::{header, HeaderValue, Method, Response, StatusCode}; use axum::response::IntoResponse; use axum::routing::get; use axum::Router; @@ -111,10 +112,11 @@ pub fn router(profile: Arc) -> Router { } async fn commit_handler( + method: Method, Path((rid, sha)): Path<(RepoId, String)>, Query(q): Query, State(profile): State>, -) -> Result<(StatusCode, HeaderMap, Vec), Error> { +) -> Result, Error> { let storage = &profile.storage; let repo = storage.repository(rid)?; @@ -131,7 +133,7 @@ async fn commit_handler( return Err(Error::BadRequest); }; - archive_by_committish(rid, Committish::Oid(oid), q.prefix, format, profile).await + archive_by_committish(method, rid, Committish::Oid(oid), q.prefix, format, profile).await } async fn file_by_commit_handler( @@ -156,12 +158,14 @@ async fn file_by_commit_handler( } async fn archive_by_refname_handler( + method: Method, Path((rid, refname)): Path<(RepoId, String)>, Query(q): Query, State(profile): State>, -) -> Result<(StatusCode, HeaderMap, Vec), Error> { +) -> Result, Error> { let (refname, format) = ArchiveFormat::detect(&refname); archive_by_committish( + method, rid, Committish::Ref(refname), q.prefix, @@ -172,12 +176,13 @@ async fn archive_by_refname_handler( } async fn archive_by_committish( + method: Method, rid: RepoId, committish: Committish<'_>, use_prefix: bool, format: ArchiveFormat, profile: Arc, -) -> Result<(StatusCode, HeaderMap, Vec), Error> { +) -> Result, Error> { let storage = &profile.storage; let repo = storage.repository(rid)?; @@ -221,6 +226,22 @@ async fn archive_by_committish( build }; + let mut response_headers = HeaderMap::new(); + response_headers.insert("Content-Type", HeaderValue::from_str(format.mime_type())?); + response_headers.insert( + "Content-Disposition", + HeaderValue::from_str(&format!( + "attachment; filename=\"{prefix}{}\"", + format.extension() + ))?, + ); + + let response = response_headers.into_response(); + + if method == Method::HEAD { + return Ok(response); + } + let mut command = Command::new("git"); command @@ -244,16 +265,11 @@ async fn archive_by_committish( )); } - let mut response_headers = HeaderMap::new(); - response_headers.insert("Content-Type", HeaderValue::from_str(format.mime_type())?); - response_headers.insert( - "Content-Disposition", - HeaderValue::from_str(&format!( - "attachment; filename=\"{prefix}{}\"", - format.extension() - ))?, - ); - Ok::<_, Error>((StatusCode::OK, response_headers, output.stdout)) + let mut response = response; + let body = response.body_mut(); + *body = Body::from(output.stdout); + + Ok(response) } async fn file_by_canonical_head_handler(