-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement download handler using redirection
With this patch we change the way with which we implement the /api/v1/crates/../../download handler. Specifically, instead of "manually" implementing the .crate file lookup, open the file, and send it back, we use a redirection to a new endpoint that serves the contents of the root directory where all .crate files are stored. This way we we make better use of warp's functionality that we already use elsewhere.
- Loading branch information
Showing
3 changed files
with
27 additions
and
44 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// Copyright (C) 2020 Daniel Mueller <[email protected]> | ||
// Copyright (C) 2020-2021 Daniel Mueller <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
use std::collections::BTreeMap; | ||
|
@@ -171,6 +171,11 @@ impl From<(MetaData, &[u8])> for Entry { | |
} | ||
} | ||
|
||
/// Craft the file name for a crate named `name` in version `version`. | ||
pub fn crate_file_name(name: &str, version: &str) -> String { | ||
format!("{}-{}.crate", name, version) | ||
} | ||
|
||
/// Extract and parse a `u32` value from a `Bytes` object. | ||
fn parse_u32(bytes: &mut Bytes) -> Result<u32> { | ||
ensure!(bytes.len() >= size_of::<u32>(), "not enough data for u32"); | ||
|
@@ -275,7 +280,7 @@ pub fn publish_crate(mut body: Bytes, index: &mut Index) -> Result<()> { | |
to_writer(&mut file, &entry).context("failed to write crate index meta data")?; | ||
writeln!(file).context("failed to append new line to crate index meta data file")?; | ||
|
||
let crate_file_name = format!("{}-{}.crate", crate_name, crate_vers); | ||
let crate_file_name = crate_file_name(&crate_name, &crate_vers); | ||
let crate_path = index.root().join(&crate_file_name); | ||
let mut file = OpenOptions::new() | ||
.write(true) | ||
|