Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: /nar: handle nar-serve URLs #293

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion harmonia/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const BOOTSTRAP_SOURCE: &str = r#"
const CARGO_NAME: &str = env!("CARGO_PKG_NAME");
const CARGO_VERSION: &str = env!("CARGO_PKG_VERSION");
const CARGO_HOME_PAGE: &str = env!("CARGO_PKG_HOMEPAGE");
const NIXBASE32_ALPHABET: &str = "0123456789abcdfghijklmnpqrsvwxyz";

fn cache_control_max_age(max_age: u32) -> http::header::CacheControl {
http::header::CacheControl(vec![http::header::CacheDirective::MaxAge(max_age)])
Expand Down Expand Up @@ -115,7 +116,20 @@ async fn main() -> std::io::Result<()> {
.route("/{hash}.ls", web::head().to(narlist::get))
.route("/{hash}.narinfo", web::get().to(narinfo::get))
.route("/{hash}.narinfo", web::head().to(narinfo::get))
.route("/nar/{hash}.nar", web::get().to(nar::get))
.route(
&format!("/nar/{{narhash:[{0}]{{52}}}}.nar", NIXBASE32_ALPHABET),
web::get().to(nar::get),
)
.route(
// narinfos served by nix-serve have the narhash embedded in the nar URL.
// While we don't do that, if nix-serve is replaced with harmonia, the old nar URLs
// will stay in client caches for a while - so support them anyway.
&format!(
"/nar/{{outhash:[{0}]{{32}}}}-{{narhash:[{0}]{{52}}}}.nar",
NIXBASE32_ALPHABET
),
web::get().to(nar::get),
)
.route("/serve/{hash}{path:.*}", web::get().to(serve::get))
.route("/log/{drv}", web::get().to(buildlog::get))
.route("/version", web::get().to(version::get))
Expand Down
58 changes: 33 additions & 25 deletions harmonia/src/nar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@ use crate::{cache_control_max_age_1y, some_or_404};
use std::ffi::{OsStr, OsString};
use tokio::{sync, task};

/// Represents the query string of a NAR URL.
#[derive(Debug, Deserialize)]
pub struct NarRequest {
hash: String,
hash: Option<String>,
}

/// Represents the parsed parts in a NAR URL.
#[derive(Debug, Deserialize)]
pub struct PathParams {
narhash: String,
outhash: Option<String>,
}

// TODO(conni2461): still missing
Expand Down Expand Up @@ -314,35 +322,35 @@ async fn dump_path(path: &Path, tx: &Sender<Result<Bytes, ThreadSafeError>>) ->
}

pub(crate) async fn get(
_nar_hash: web::Path<String>,
path: web::Path<PathParams>,
req: HttpRequest,
info: web::Query<NarRequest>,
q: web::Query<NarRequest>,
) -> Result<HttpResponse, Box<dyn Error>> {
let (store_path, nar_hash) = some_or_404!((if info.hash.len() == 32 {
Some((info.hash.as_str(), None))
} else if info.hash.len() == 85 {
// narinfos served by nix-serve have the nar file hash embedded in the nar URL.
// While we don't do that, if nix-serve is replaced with harmonia, the old nar URLs
// will stay in the cache for a while - so support them anyway.
info.hash
.split_once('-')
.and_then(|(first, rest)| (first.len() == 32).then_some((first, Some(rest))))
} else {
None
})
.and_then(
|(hash, nar_hash)| libnixstore::query_path_from_hash_part(hash)
.map(|hash| (hash, nar_hash))
));
// Extract the narhash from the query parameter, and bail out if it's missing or invalid.
let narhash = some_or_404!(Some(path.narhash.as_str()));

// lookup the store path.
let store_path = some_or_404!({
// We usually extract the outhash from the query parameter.
// However, when processing nix-serve URLs, it's present in the path
// directly.
if let Some(outhash) = &q.hash {
Some(outhash.as_str())
} else {
path.outhash.as_deref()
}
}
.and_then(libnixstore::query_path_from_hash_part));

// lookup the path info.
let info = libnixstore::query_path_info(&store_path, Radix::default())?;
if let Some(nar_hash) = nar_hash {
if Some(nar_hash) != info.narhash.strip_prefix("sha256:") {
return Ok(HttpResponse::NotFound()
.insert_header(crate::cache_control_no_store())
.body("hash mismatch detected"));
}
// ensure the narhash specified in the request matches.
if format!("sha256:{}", narhash) != info.narhash {
return Ok(HttpResponse::NotFound()
.insert_header(crate::cache_control_no_store())
.body("hash mismatch detected"));
}

let mut rlength = info.size;
let offset;
let mut res = HttpResponse::Ok();
Expand Down