Skip to content

Commit

Permalink
implement /nar/{store_hash}-{nar_hash}.nar endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
chayleaf committed Jul 31, 2023
1 parent 0d985a1 commit 1bfebc9
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions harmonia/src/nar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,29 @@ pub(crate) async fn get(
req: HttpRequest,
info: web::Query<NarRequest>,
) -> Result<HttpResponse, Box<dyn Error>> {
let store_path = some_or_404!(libnixstore::query_path_from_hash_part(&info.hash));

let size = libnixstore::query_path_info(&store_path, Radix::default())?.size;
let mut rlength = size;
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 {
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))
));

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"));
}
}
let mut rlength = info.size;
let offset;
let mut res = HttpResponse::Ok();

Expand All @@ -339,7 +358,7 @@ pub(crate) async fn get(

res.insert_header((
http::header::CONTENT_RANGE,
format!("bytes {}-{}/{}", offset, offset + rlength - 1, size,),
format!("bytes {}-{}/{}", offset, offset + rlength - 1, info.size),
));
} else {
res.insert_header((http::header::CONTENT_RANGE, format!("bytes */{}", rlength)));
Expand Down

0 comments on commit 1bfebc9

Please sign in to comment.