@@ -16,7 +16,7 @@ use rustc_serialize::json::{Json, ToJson};
16
16
use std:: collections:: BTreeMap ;
17
17
use iron:: headers:: { Expires , HttpDate , CacheControl , CacheDirective } ;
18
18
use time;
19
-
19
+ use iron :: Handler ;
20
20
21
21
22
22
#[ derive( Debug ) ]
@@ -110,6 +110,10 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
110
110
}
111
111
112
112
113
+ /// Serves documentation generated by rustdoc.
114
+ ///
115
+ /// This includes all HTML files for an individual crate, as well as the `search-index.js`, which is
116
+ /// also crate-specific.
113
117
pub fn rustdoc_html_server_handler ( req : & mut Request ) -> IronResult < Response > {
114
118
115
119
let router = extension ! ( req, Router ) ;
@@ -251,3 +255,28 @@ pub fn badge_handler(req: &mut Request) -> IronResult<Response> {
251
255
CacheDirective :: MustRevalidate ] ) ) ;
252
256
Ok ( resp)
253
257
}
258
+
259
+ /// Serves shared web resources used by rustdoc-generated documentation.
260
+ ///
261
+ /// This includes common `css` and `js` files that only change when the compiler is updated, but are
262
+ /// otherwise the same for all crates documented with that compiler. Those have a custom handler to
263
+ /// deduplicate them and save space.
264
+ pub struct SharedResourceHandler ;
265
+
266
+ impl Handler for SharedResourceHandler {
267
+ fn handle ( & self , req : & mut Request ) -> IronResult < Response > {
268
+ let path = req. url . path ( ) ;
269
+ let filename = path. last ( ) . unwrap ( ) ; // unwrap is fine: vector is non-empty
270
+ let suffix = filename. split ( '.' ) . last ( ) . unwrap ( ) ; // unwrap is fine: split always works
271
+ if [ "js" , "css" , "woff" , "svg" ] . contains ( & suffix) {
272
+ let conn = extension ! ( req, Pool ) ;
273
+
274
+ if let Some ( file) = File :: from_path ( conn, filename) {
275
+ return Ok ( file. serve ( ) ) ;
276
+ }
277
+ }
278
+
279
+ // Just always return a 404 here - the main handler will then try the other handlers
280
+ Err ( IronError :: new ( Nope :: ResourceNotFound , status:: NotFound ) )
281
+ }
282
+ }
0 commit comments