Skip to content

Commit b49df63

Browse files
committed
Redirect non-json for download (instead of sending json)
1 parent 7018891 commit b49df63

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

src/package.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,12 @@ pub fn download(req: &mut Request) -> CargoResult<Response> {
466466
let redirect_url = format!("https://{}/pkg/{}/{}-{}.tar.gz",
467467
req.app().bucket.host(),
468468
pkg_name, pkg_name, version);
469-
#[deriving(Encodable)]
470-
struct R { ok: bool, url: String }
471-
Ok(req.json(&R{ ok: true, url: redirect_url }))
469+
470+
if req.wants_json() {
471+
#[deriving(Encodable)]
472+
struct R { ok: bool, url: String }
473+
Ok(req.json(&R{ ok: true, url: redirect_url }))
474+
} else {
475+
Ok(req.redirect(redirect_url))
476+
}
472477
}

src/tests/package.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ fn download() {
296296
let rel = format!("/{}/{}-1.0.0.tar.gz", package.name, package.name);
297297
let mut req = MockRequest::new(conduit::Get, format!("/download{}", rel)
298298
.as_slice());
299-
ok_resp!(middle.call(&mut req));
299+
let resp = t_resp!(middle.call(&mut req));
300+
assert_eq!(resp.status.val0(), 302);
300301
{
301302
let conn = (&mut req as &mut Request).tx().unwrap();
302303
let pkg = Package::find_by_name(conn, package.name.as_slice()).unwrap();

src/util/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub trait RequestUtils {
2727

2828
fn json<'a, T: Encodable<json::Encoder<'a>, IoError>>(self, t: &T) -> Response;
2929
fn query(self) -> HashMap<String, String>;
30+
fn wants_json(self) -> bool;
3031
}
3132

3233
pub fn json_response<'a, T>(t: &T) -> Response
@@ -62,6 +63,11 @@ impl<'a> RequestUtils for &'a Request + 'a {
6263
body: box MemReader::new(Vec::new()),
6364
}
6465
}
66+
67+
fn wants_json(self) -> bool {
68+
let content = self.headers().find("Accept").unwrap_or(Vec::new());
69+
content.iter().any(|s| s.contains("json"))
70+
}
6571
}
6672

6773
pub struct C(pub fn(&mut Request) -> CargoResult<Response>);

0 commit comments

Comments
 (0)