From 811c41e27e368a772a1760e8486adeaf3c5aa84b Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Tue, 12 Sep 2017 15:32:12 +0200 Subject: [PATCH] Send a Vary: Accept, Accept-Encoding header --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 2 +- src/webservice.rs | 26 +++++++++++++++++++------- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8522ced..48e3236 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,8 +11,8 @@ dependencies = [ "iron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "router 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicase 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 2dee869..e15fdd5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,9 +17,9 @@ hyper-native-tls = "~0.2" iron = "~0.5" log = "~0.3" router = "~0.5" -serde = "~1.0" serde_json = "~1.0" clippy = {version = "~0", optional = true} +unicase = "~1.4" [features] default = [] diff --git a/src/main.rs b/src/main.rs index 27f93ac..24f0a6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,8 +12,8 @@ extern crate iron; extern crate log; #[macro_use] extern crate router; -extern crate serde; extern crate serde_json; +extern crate unicase; mod asns; mod webservice; diff --git a/src/webservice.rs b/src/webservice.rs index e9e839f..297c6a7 100644 --- a/src/webservice.rs +++ b/src/webservice.rs @@ -1,7 +1,7 @@ use asns::*; use horrorshow::prelude::*; use iron::{typemap, BeforeMiddleware}; -use iron::headers::{Accept, CacheControl, CacheDirective}; +use iron::headers::{Accept, CacheControl, CacheDirective, Vary}; use iron::mime::*; use iron::modifiers::Header; use iron::prelude::*; @@ -11,6 +11,7 @@ use serde_json; use std::net::IpAddr; use std::str::FromStr; use std::sync::{Arc, RwLock}; +use unicase::UniCase; const TTL: u32 = 86400; @@ -82,6 +83,7 @@ impl WebService { fn output_json( map: serde_json::Map, cache_header: Header, + vary_header: Header, ) -> IronResult { let json = serde_json::to_string(&map).unwrap(); let mime_json = Mime( @@ -89,12 +91,15 @@ impl WebService { SubLevel::Json, vec![(Attr::Charset, Value::Utf8)], ); - Ok(Response::with((status::Ok, mime_json, cache_header, json))) + Ok(Response::with( + (status::Ok, mime_json, cache_header, vary_header, json), + )) } fn output_html( map: serde_json::Map, cache_header: Header, + vary_header: Header, ) -> IronResult { let mime_html = Mime( TopLevel::Text, @@ -153,17 +158,20 @@ impl WebService { .into_string() .unwrap(); let html = format!("\n{}", html); - Ok(Response::with((status::Ok, mime_html, cache_header, html))) + Ok(Response::with( + (status::Ok, mime_html, cache_header, vary_header, html), + )) } fn output( output_type: OutputType, map: serde_json::Map, cache_header: Header, + vary_header: Header, ) -> IronResult { match output_type { - OutputType::Json => Self::output_json(map, cache_header), - _ => Self::output_html(map, cache_header), + OutputType::Json => Self::output_json(map, cache_header, vary_header), + _ => Self::output_html(map, cache_header, vary_header), } } @@ -176,6 +184,10 @@ impl WebService { let cache_header = Header(CacheControl( vec![CacheDirective::Public, CacheDirective::MaxAge(TTL)], )); + let vary_header = Header(Vary::Items(vec![ + UniCase::from_str("accept-encoding").unwrap(), + UniCase::from_str("accept").unwrap(), + ])); let ip_str = match req.extensions.get::().unwrap().find("ip") { None => { let response = Response::with(( @@ -211,7 +223,7 @@ impl WebService { "announced".to_string(), serde_json::value::Value::Bool(false), ); - return Self::output(Self::accept_type(&req), map, cache_header); + return Self::output(Self::accept_type(&req), map, cache_header, vary_header); } Some(found) => found, }; @@ -239,7 +251,7 @@ impl WebService { "as_description".to_string(), serde_json::value::Value::String(found.description.clone()), ); - Self::output(Self::accept_type(&req), map, cache_header) + Self::output(Self::accept_type(&req), map, cache_header, vary_header) } pub fn start(asns_arc: Arc>>, listen_addr: &str) {