Skip to content

Commit c2763d8

Browse files
committed
Return a download redirect even if crate version isn't in db
If we are a mirror that is just handling the index and not tracking downloads, return the redirect URL even if we don't know about this crate or version.
1 parent 0b18952 commit c2763d8

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

src/krate.rs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,10 @@ pub fn download(req: &mut Request) -> CargoResult<Response> {
828828
let crate_name = &req.params()["crate_id"];
829829
let version = &req.params()["version"];
830830

831-
try!(increment_download_counts(req, crate_name, version));
831+
match increment_download_counts(req, crate_name, version) {
832+
_ => {} // do not try, ignore failures
833+
}
834+
832835
let redirect_url = format!("https://{}/crates/{}/{}-{}.crate",
833836
req.app().bucket.host(),
834837
crate_name, crate_name, version);
@@ -853,32 +856,34 @@ fn increment_download_counts(req: &Request, crate_name: &str, version: &str) ->
853856
AND versions.num = $2
854857
LIMIT 1"));
855858
let rows = try!(stmt.query(&[&crate_name, &version]));
856-
let row = try!(rows.iter().next().chain_error(|| {
857-
human("crate or version not found")
858-
}));
859-
let version_id: i32 = row.get("version_id");
860-
let now = ::now();
861-
862-
// Bump download counts.
863-
//
864-
// Note that this is *not* an atomic update, and that's somewhat
865-
// intentional. It doesn't appear that postgres supports an atomic update of
866-
// a counter, so we just do the hopefully "least racy" thing. This is
867-
// largely ok because these download counters are just that, counters. No
868-
// need to have super high-fidelity counter.
869-
//
870-
// Also, we only update the counter for *today*, nothing else. We have lots
871-
// of other counters, but they're all updated later on via the
872-
// update-downloads script.
873-
let amt = try!(tx.execute("UPDATE version_downloads
874-
SET downloads = downloads + 1
875-
WHERE version_id = $1 AND date($2) = date(date)",
876-
&[&version_id, &now]));
877-
if amt == 0 {
878-
try!(tx.execute("INSERT INTO version_downloads
879-
(version_id) VALUES ($1)", &[&version_id]));
859+
match rows.iter().next() {
860+
None => Err(human("crate or version not found")),
861+
Some(row) => {
862+
let version_id: i32 = row.get("version_id");
863+
let now = ::now();
864+
865+
// Bump download counts.
866+
//
867+
// Note that this is *not* an atomic update, and that's somewhat
868+
// intentional. It doesn't appear that postgres supports an atomic update of
869+
// a counter, so we just do the hopefully "least racy" thing. This is
870+
// largely ok because these download counters are just that, counters. No
871+
// need to have super high-fidelity counter.
872+
//
873+
// Also, we only update the counter for *today*, nothing else. We have lots
874+
// of other counters, but they're all updated later on via the
875+
// update-downloads script.
876+
let amt = try!(tx.execute("UPDATE version_downloads
877+
SET downloads = downloads + 1
878+
WHERE version_id = $1 AND date($2) = date(date)",
879+
&[&version_id, &now]));
880+
if amt == 0 {
881+
try!(tx.execute("INSERT INTO version_downloads
882+
(version_id) VALUES ($1)", &[&version_id]));
883+
}
884+
Ok(())
885+
}
880886
}
881-
Ok(())
882887
}
883888

884889
/// Handles the `GET /crates/:crate_id/downloads` route.

src/tests/krate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,8 @@ fn download_bad() {
669669
let mut req = ::req(app, Method::Get, "/api/v1/crates/foo/0.1.0/download");
670670
::mock_user(&mut req, ::user("foo"));
671671
::mock_crate(&mut req, ::krate("foo"));
672-
let mut response = ok_resp!(middle.call(&mut req));
673-
::json::<::Bad>(&mut response);
672+
let resp = t_resp!(middle.call(&mut req));
673+
assert_eq!(resp.status.0, 302);
674674
}
675675

676676
#[test]

0 commit comments

Comments
 (0)