@@ -218,26 +218,14 @@ impl Storage {
218
218
///
219
219
/// The function doesn't check for the existence of the file.
220
220
pub fn crate_location ( & self , name : & str , version : & str ) -> String {
221
- self . with_cdn_prefix ( & crate_file_path ( name, version) )
222
- . replace ( '+' , "%2B" )
221
+ apply_cdn_prefix ( & self . cdn_prefix , & crate_file_path ( name, version) ) . replace ( '+' , "%2B" )
223
222
}
224
223
225
224
/// Returns the URL of an uploaded crate's version readme.
226
225
///
227
226
/// The function doesn't check for the existence of the file.
228
227
pub fn readme_location ( & self , name : & str , version : & str ) -> String {
229
- self . with_cdn_prefix ( & readme_path ( name, version) )
230
- . replace ( '+' , "%2B" )
231
- }
232
-
233
- fn with_cdn_prefix ( & self , path : & Path ) -> String {
234
- match self . cdn_prefix . as_ref ( ) {
235
- Some ( cdn_prefix) if !cdn_prefix. starts_with ( "https://" ) => {
236
- format ! ( "https://{cdn_prefix}/{path}" )
237
- }
238
- Some ( cdn_prefix) => format ! ( "{cdn_prefix}/{path}" ) ,
239
- None => format ! ( "/{path}" ) ,
240
- }
228
+ apply_cdn_prefix ( & self . cdn_prefix , & readme_path ( name, version) ) . replace ( '+' , "%2B" )
241
229
}
242
230
243
231
#[ instrument( skip( self ) ) ]
@@ -357,6 +345,16 @@ fn readme_path(name: &str, version: &str) -> Path {
357
345
format ! ( "{PREFIX_READMES}/{name}/{name}-{version}.html" ) . into ( )
358
346
}
359
347
348
+ fn apply_cdn_prefix ( cdn_prefix : & Option < String > , path : & Path ) -> String {
349
+ match cdn_prefix {
350
+ Some ( cdn_prefix) if !cdn_prefix. starts_with ( "https://" ) => {
351
+ format ! ( "https://{cdn_prefix}/{path}" )
352
+ }
353
+ Some ( cdn_prefix) => format ! ( "{cdn_prefix}/{path}" ) ,
354
+ None => format ! ( "/{path}" ) ,
355
+ }
356
+ }
357
+
360
358
#[ cfg( test) ]
361
359
mod tests {
362
360
use super :: * ;
@@ -422,6 +420,32 @@ mod tests {
422
420
}
423
421
}
424
422
423
+ #[ test]
424
+ fn cdn_prefix ( ) {
425
+ assert_eq ! ( apply_cdn_prefix( & None , & "foo" . into( ) ) , "/foo" ) ;
426
+ assert_eq ! (
427
+ apply_cdn_prefix( & Some ( "static.crates.io" . to_string( ) ) , & "foo" . into( ) ) ,
428
+ "https://static.crates.io/foo"
429
+ ) ;
430
+ assert_eq ! (
431
+ apply_cdn_prefix(
432
+ & Some ( "https://fastly-static.crates.io" . to_string( ) ) ,
433
+ & "foo" . into( )
434
+ ) ,
435
+ "https://fastly-static.crates.io/foo"
436
+ ) ;
437
+
438
+ assert_eq ! (
439
+ apply_cdn_prefix( & Some ( "static.crates.io" . to_string( ) ) , & "/foo/bar" . into( ) ) ,
440
+ "https://static.crates.io/foo/bar"
441
+ ) ;
442
+
443
+ assert_eq ! (
444
+ apply_cdn_prefix( & Some ( "static.crates.io/" . to_string( ) ) , & "/foo/bar" . into( ) ) ,
445
+ "https://static.crates.io//foo/bar"
446
+ ) ;
447
+ }
448
+
425
449
#[ tokio:: test]
426
450
async fn delete_all_crate_files ( ) {
427
451
let storage = prepare ( ) . await ;
0 commit comments