diff --git a/imageflow_core/tests/visuals.rs b/imageflow_core/tests/visuals.rs index f1cfac5f1..72ce224c5 100644 --- a/imageflow_core/tests/visuals.rs +++ b/imageflow_core/tests/visuals.rs @@ -9,7 +9,6 @@ extern crate serde_json; use std::ffi::CString; use std::path::Path; -use std::io::Read; use imageflow_core::{Context, JsonResponse}; diff --git a/imageflow_server/src/diagnose.rs b/imageflow_server/src/diagnose.rs index 5e7a2986b..6787e6d21 100644 --- a/imageflow_server/src/diagnose.rs +++ b/imageflow_server/src/diagnose.rs @@ -1,4 +1,3 @@ -use ::imageflow_helpers as hlp; use ::imageflow_helpers::preludes::from_std::*; use ::imageflow_core::clients::stateless; use ::imageflow_core::clients::fluent; diff --git a/imageflow_server/src/disk_cache.rs b/imageflow_server/src/disk_cache.rs index 5b31be331..2b5f8fe87 100644 --- a/imageflow_server/src/disk_cache.rs +++ b/imageflow_server/src/disk_cache.rs @@ -10,7 +10,7 @@ use ::std::io; use ::std; use ::std::io::prelude::*; use ::std::fs::{create_dir_all, File}; -use std::sync::atomic::{AtomicU64, AtomicBool, Ordering, ATOMIC_U64_INIT}; +use std::sync::atomic::{AtomicBool, Ordering}; // TODO: // Cleanup staging folders automatically (failed renames) @@ -123,16 +123,16 @@ impl CacheFolder{ } fn ensure_meta_layout_confirmed(&self) -> io::Result<()>{ - if !self.meta_layout_confirmed.load(Ordering::Relaxed){ + if !self.meta_layout_confirmed.load(Ordering::SeqCst){ let path = self.meta_dir.join(Path::new(match self.write_layout{ FolderLayout::Huge => "huge", FolderLayout::Tiny => "tiny", FolderLayout::Normal => "normal" })); - if !self.meta_layout_confirmed.load(Ordering::Relaxed) && !path.exists() { + if !self.meta_layout_confirmed.load(Ordering::SeqCst) && !path.exists() { create_dir_all_helpful(&self.meta_dir)?; File::create(path)?; - self.meta_layout_confirmed.store(true, Ordering::Relaxed); + self.meta_layout_confirmed.store(true, Ordering::SeqCst); } } Ok(()) @@ -142,7 +142,7 @@ impl CacheFolder{ /// We would need 'fast path' that falls back to 'careful path' when any of those caches get out of sync fn prepare_for(&self, entry: &CacheEntry) -> io::Result<()> { self.ensure_root().unwrap(); - self.ensure_meta_layout_confirmed(); + self.ensure_meta_layout_confirmed().unwrap(); let dir = entry.path.as_path().parent().expect("Every cache path should have a parent dir; this did not!"); if !dir.exists(){ create_dir_all_helpful(dir)?; diff --git a/imageflow_server/src/lib.rs b/imageflow_server/src/lib.rs index ab2fb1c89..1ec15464b 100644 --- a/imageflow_server/src/lib.rs +++ b/imageflow_server/src/lib.rs @@ -12,7 +12,6 @@ extern crate mount; use staticfile::Static; -use ::time::Duration; #[macro_use] extern crate serde_derive; @@ -29,8 +28,8 @@ extern crate hyper_native_tls; use hyper_native_tls::NativeTlsServer; -use std::sync::atomic::{AtomicU64, AtomicBool, ATOMIC_U64_INIT}; -use std::sync::atomic; +use std::sync::atomic::{AtomicU64, ATOMIC_U64_INIT}; + extern crate conduit_mime_types as mime_types; @@ -56,7 +55,7 @@ pub mod diagnose; mod requested_path; extern crate url; -use disk_cache::{CacheFolder, CacheEntry, FolderLayout}; +use disk_cache::{CacheFolder, FolderLayout}; use logger::Logger; pub mod preludes { @@ -65,21 +64,19 @@ pub mod preludes { } -use iron::{AfterMiddleware, BeforeMiddleware}; use iron::mime::*; use iron::prelude::*; use iron::status; use router::Router; -use iron::typemap::Key; -use iron::middleware::Handler; -use hyper::header::Headers; + + + use time::precise_time_ns; #[macro_use] extern crate log; extern crate env_logger; -use log::LogLevel; #[derive(Debug)] struct SharedData { @@ -173,12 +170,7 @@ fn error_upstream(from: ServerError) -> ServerError { } } -fn error_cache_read(from: ServerError) -> ServerError { - match from { - ServerError::IoError(e) => ServerError::UpstreamIoError(e), - e => e, - } -} + #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] @@ -269,16 +261,6 @@ struct AcquirePerf { impl AcquirePerf { - pub fn new() -> AcquirePerf { - AcquirePerf { fetch_ns: 0, cache_write_ns: 0, cache_read_ns: 0 } - } - fn debug(&self) -> String { - format!("HTTP fetch took: {} ms, cache read {} ms, cache write {} ms", - (self.fetch_ns as f64) / 1000000.0, - (self.cache_read_ns as f64) / 1000000.0, - (self.cache_write_ns as f64) / 1000000.0) - } - fn total(&self) -> u64{ self.fetch_ns + self.cache_read_ns + self.cache_write_ns } @@ -291,13 +273,6 @@ struct RequestPerf { } impl RequestPerf { - fn debug(&self) -> String { - format!("{}, get_image_info took {} ms, execute took {} ms", - self.acquire.debug(), - (self.get_image_info_ns as f64) / 1000000.0, - (self.execute_ns as f64) / 1000000.0) - } - fn short(&self) -> String{ format!("execute {:.2}ms getinfo {:.2}ms fetch-through: {:.2}ms", (self.execute_ns as f64) / 1000000.0 @@ -381,44 +356,7 @@ fn respond_with_server_error(debug_info: A, e: ServerError, detailed_errors: } } } -// -//fn proto1(req: &mut Request) -> IronResult { -// -// let w; -// let h; -// let url; -// { -// let router = req.extensions.get::().unwrap(); -// -// let generic_url = req.url.clone().into_generic_url(); -// -// let router_w = router.find("w").and_then(|v| Some(v.to_owned())); -// -// w = generic_url.query_pairs().find(|&(ref k,ref v)| k == "w").map(|(k,v)| v.into_owned()).or(router_w).and_then(|x| x.parse::().ok()); -// -// h = router.find("h").and_then(|x| x.parse::().ok()); -// url = "http://images.unsplash.com/".to_string() + router.find("url").unwrap(); -// } -// let shared = req.get::>().unwrap(); -// -// respond_one_to_one(shared.as_ref(), &url, |info: s::ImageInfo| { -// let commands = BoringCommands { -// fit: ConstraintMode::Max, -// w: w.and_then(|w| Some(w as i32)), -// h: h.and_then(|h| Some(h as i32)), -// jpeg_quality: 90, -// precise_scaling_ratio: 2.1f32, -// luma_correct: true, -// sharpen: 0f32, -// format: ImageFormat::Jpeg, -// down_filter: Filter::Robidoux, -// up_filter: Filter::Ginseng, -// }; -// let (framewise, (pre_w, pre_h)) = -// create_framewise(info.image_width, info.image_height, commands).unwrap(); -// framewise -// }) -//} + fn ir4_http_respond(shared: &SharedData, url: &str, framewise_generator: F) -> IronResult where F: Fn(s::ImageInfo) -> std::result::Result @@ -428,7 +366,7 @@ fn ir4_http_respond(shared: &SharedData, url: &str, framewise_generator: F) - fn ir4_framewise(info: s::ImageInfo, url: &Url) -> std::result::Result { - ::imageflow_riapi::ir4::parse_to_framewise(info, url).map_err(|e| ServerError::LayoutSizingError(e)).map(|(framewise, warnings)| framewise) + ::imageflow_riapi::ir4::parse_to_framewise(info, url).map_err(|e| ServerError::LayoutSizingError(e)).map(|(framewise, _)| framewise) } @@ -436,16 +374,16 @@ type EngineHandler = fn(req: &mut Request, engine_data: &T, mount: &MountLoca type EngineSetup = fn(mount: &MountLocation) -> Result<(T, EngineHandler), String>; -fn ir4_local_respond(shared: &SharedData, source: &Path, framewise_generator: F) -> IronResult +fn ir4_local_respond(_: &SharedData, source: &Path, framewise_generator: F) -> IronResult where F: Fn(s::ImageInfo) -> std::result::Result { respond_using(source, || fetch_bytes_from_disk(source), framewise_generator) } -fn ir4_local_handler(req: &mut Request, local_path: &PathBuf, mount: &MountLocation) -> IronResult { +fn ir4_local_handler(req: &mut Request, local_path: &PathBuf, _: &MountLocation) -> IronResult { let requested_path = requested_path::RequestedPath::new(local_path, &req); - let url = req.url.clone().into_generic_url(); + let url: url::Url = req.url.clone().into(); let shared = req.get::>().unwrap(); if requested_path.path.exists() { @@ -454,7 +392,7 @@ fn ir4_local_handler(req: &mut Request, local_path: &PathBuf, mount: &MountLocat }); } - writeln!(&mut std::io::stderr(), "404 {:?} using local path {:?} and base {:?}", &url.path(), requested_path.path.as_path(), local_path); + let _ = writeln!(&mut std::io::stderr(), "404 {:?} using local path {:?} and base {:?}", &url.path(), requested_path.path.as_path(), local_path); //writeln!(&mut std::io::stdout(), "404 {:?} using local path {:?}", &url.path(), original ); let bytes = format!("File not found").into_bytes(); @@ -463,7 +401,7 @@ fn ir4_local_handler(req: &mut Request, local_path: &PathBuf, mount: &MountLocat bytes))) } -fn static_handler(req: &mut Request, h: &Static, mount: &MountLocation) -> IronResult { +fn static_handler(_: &mut Request, _: &Static, _: &MountLocation) -> IronResult { let bytes = format!("Do not use").into_bytes(); Ok(Response::with((Mime::from_str("text/plain").unwrap(), @@ -488,8 +426,9 @@ fn static_setup(mount: &MountLocation) -> Result<(Static, EngineHandler) //TODO: validate path let path = Path::new(&mount.engine_args[0]).canonicalize().map_err(|e| format!("{:?}", e))?; let h = if mount.engine_args.len() > 1 { - let mins = mount.engine_args[1].parse::().expect("second argument to static must be the number of minutes to browser cache for"); - Static::new(path) // .cache(Duration::minutes(mins)) (we must compile staticfile with the 'cache' feature enabled) + panic!("Static file cache headers not yet supported"); //(we must compile staticfile with the 'cache' feature enabled) +// let mins = mount.engine_args[1].parse::().expect("second argument to static must be the number of minutes to browser cache for"); +// Static::new(path).cache(Duration::minutes(mins)) } else { Static::new(path) }; @@ -498,8 +437,8 @@ fn static_setup(mount: &MountLocation) -> Result<(Static, EngineHandler) } -fn permacache_proxy_handler(req: &mut Request, base_url: &String, mount: &MountLocation) -> IronResult { - let url = req.url.clone().into_generic_url(); +fn permacache_proxy_handler(req: &mut Request, base_url: &String, _: &MountLocation) -> IronResult { + let url: url::Url = req.url.clone().into(); let shared = req.get::>().unwrap(); //TODO: Ensure the combined url is canonical (or, at least, lacks ..) let remote_url = format!("{}{}{}", base_url, &url.path()[1..], req.url.query().unwrap_or("")); @@ -518,17 +457,17 @@ fn permacache_proxy_handler(req: &mut Request, base_url: &String, mount: &MountL lazy_static! { static ref MIME_TYPES: mime_types::Types = mime_types::Types::new().unwrap(); } -fn permacache_proxy_handler_guess_types(req: &mut Request, base_url: &String, mount: &MountLocation) -> IronResult { +fn permacache_proxy_handler_guess_types(req: &mut Request, base_url: &String, _: &MountLocation) -> IronResult { - let url = req.url.clone().into_generic_url(); + let url: url::Url = req.url.clone().into(); let shared = req.get::>().unwrap(); //TODO: Ensure the combined url is canonical (or, at least, lacks ..) let remote_url = format!("{}{}{}", base_url, &url.path()[1..], req.url.query().unwrap_or("")); match fetch_bytes_using_cache_by_url(&shared.source_cache, &remote_url) { - Ok((bytes, perf)) => { + Ok((bytes, _)) => { let part_path = Path::new(&url.path()[1..]); let mime_str = MIME_TYPES.mime_for_path(&part_path); @@ -545,8 +484,8 @@ fn permacache_proxy_handler_guess_types(req: &mut Request, base_url: &String, mo } -fn ir4_http_handler(req: &mut Request, base_url: &String, mount: &MountLocation) -> IronResult { - let url = req.url.clone().into_generic_url(); +fn ir4_http_handler(req: &mut Request, base_url: &String, _: &MountLocation) -> IronResult { + let url: url::Url = req.url.clone().into(); let shared = req.get::>().unwrap(); //TODO: Ensure the combined url is canonical (or, at least, lacks ..) let remote_url = format!("{}{}", base_url, &url.path()[1..]); @@ -624,7 +563,7 @@ pub fn serve(c: StartServerConfig) { } if c.integration_test { - router.get("/test/shutdown", move |r: &mut Request| -> IronResult { + router.get("/test/shutdown", move |_: &mut Request| -> IronResult { println!("Stopping server due to GET /test/shutdown"); std::process::exit(0); diff --git a/imageflow_server/src/requested_path.rs b/imageflow_server/src/requested_path.rs index a44323750..56a0b106f 100644 --- a/imageflow_server/src/requested_path.rs +++ b/imageflow_server/src/requested_path.rs @@ -24,7 +24,6 @@ SOFTWARE. */ use iron::Request; use std::iter::FromIterator; use std::path::{Component, PathBuf, Path}; -use std::fs::{self, Metadata}; use std::convert::AsRef; use url::percent_encoding::percent_decode; @@ -61,36 +60,4 @@ impl RequestedPath { RequestedPath { path: result } } - pub fn should_redirect(&self, metadata: &Metadata, request: &Request) -> bool { - // As per servo/rust-url/serialize_path, URLs ending in a slash have an - // empty string stored as the last component of their path. Rust-url - // even ensures that url.path() is non-empty by appending a forward slash - // to URLs like http://example.com - // Some middleware may mutate the URL's path to violate this property, - // so the empty list case is handled as a redirect. - let has_trailing_slash = match request.url.path().last() { - Some(&"") => true, - _ => false, - }; - - metadata.is_dir() && !has_trailing_slash - } - - pub fn get_file(self, metadata: &Metadata) -> Option { - if metadata.is_file() { - return Some(self.path); - } - - let index_path = self.path.join("index.html"); - - match fs::metadata(&index_path) { - Ok(m) => - if m.is_file() { - Some(index_path) - } else { - None - }, - Err(_) => None, - } - } }