@@ -827,6 +827,22 @@ fn read_fill<R: Read + ?Sized>(r: &mut R, mut slice: &mut [u8])
827
827
pub fn download ( req : & mut Request ) -> CargoResult < Response > {
828
828
let crate_name = & req. params ( ) [ "crate_id" ] ;
829
829
let version = & req. params ( ) [ "version" ] ;
830
+
831
+ try!( increment_download_counts ( req, crate_name, version) ) ;
832
+ let redirect_url = format ! ( "https://{}/crates/{}/{}-{}.crate" ,
833
+ req. app( ) . bucket. host( ) ,
834
+ crate_name, crate_name, version) ;
835
+
836
+ if req. wants_json ( ) {
837
+ #[ derive( RustcEncodable ) ]
838
+ struct R { url : String }
839
+ Ok ( req. json ( & R { url : redirect_url } ) )
840
+ } else {
841
+ Ok ( req. redirect ( redirect_url) )
842
+ }
843
+ }
844
+
845
+ fn increment_download_counts ( req : & Request , crate_name : & str , version : & str ) -> CargoResult < ( ) > {
830
846
let tx = try!( req. tx ( ) ) ;
831
847
let stmt = try!( tx. prepare ( "SELECT versions.id as version_id
832
848
FROM crates
@@ -836,7 +852,7 @@ pub fn download(req: &mut Request) -> CargoResult<Response> {
836
852
canon_crate_name($1)
837
853
AND versions.num = $2
838
854
LIMIT 1" ) ) ;
839
- let rows = try!( stmt. query ( & [ crate_name, version] ) ) ;
855
+ let rows = try!( stmt. query ( & [ & crate_name, & version] ) ) ;
840
856
let row = try!( rows. iter ( ) . next ( ) . chain_error ( || {
841
857
human ( "crate or version not found" )
842
858
} ) ) ;
@@ -862,19 +878,7 @@ pub fn download(req: &mut Request) -> CargoResult<Response> {
862
878
try!( tx. execute ( "INSERT INTO version_downloads
863
879
(version_id) VALUES ($1)" , & [ & version_id] ) ) ;
864
880
}
865
-
866
- // Now that we've done our business, redirect to the actual data.
867
- let redirect_url = format ! ( "https://{}/crates/{}/{}-{}.crate" ,
868
- req. app( ) . bucket. host( ) ,
869
- crate_name, crate_name, version) ;
870
-
871
- if req. wants_json ( ) {
872
- #[ derive( RustcEncodable ) ]
873
- struct R { url : String }
874
- Ok ( req. json ( & R { url : redirect_url } ) )
875
- } else {
876
- Ok ( req. redirect ( redirect_url) )
877
- }
881
+ Ok ( ( ) )
878
882
}
879
883
880
884
/// Handles the `GET /crates/:crate_id/downloads` route.
0 commit comments