Merge pull request #84 from snshn/ignore-hash-in-cache-url
use clean URLs as hashmap keys
This commit is contained in:
commit
2e623dd9f8
5 changed files with 49 additions and 15 deletions
19
.travis.yml
19
.travis.yml
|
@ -4,23 +4,24 @@ cache: cargo
|
||||||
sudo: false
|
sudo: false
|
||||||
|
|
||||||
os:
|
os:
|
||||||
- linux
|
- linux
|
||||||
- osx
|
- osx
|
||||||
|
|
||||||
rust:
|
rust:
|
||||||
- stable
|
- stable
|
||||||
- beta
|
- beta
|
||||||
- nightly
|
- nightly
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
- rustup component add rustfmt
|
- rustup component add rustfmt
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- cargo build --all --locked --verbose
|
- cargo build --all --locked --verbose
|
||||||
- cargo test --all --locked --verbose
|
- cargo test --all --locked --verbose
|
||||||
- cargo fmt --all -- --check
|
- cargo fmt --all -- --check
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
allow_failures:
|
allow_failures:
|
||||||
|
- rust: beta
|
||||||
- rust: nightly
|
- rust: nightly
|
||||||
fast_finish: true
|
fast_finish: true
|
||||||
|
|
|
@ -94,6 +94,7 @@ environment:
|
||||||
# or test failure in the matching channels/targets from failing the entire build.
|
# or test failure in the matching channels/targets from failing the entire build.
|
||||||
matrix:
|
matrix:
|
||||||
allow_failures:
|
allow_failures:
|
||||||
|
- channel: beta
|
||||||
- channel: nightly
|
- channel: nightly
|
||||||
|
|
||||||
# If you only care about stable channel build failures, uncomment the following line:
|
# If you only care about stable channel build failures, uncomment the following line:
|
||||||
|
|
14
src/http.rs
14
src/http.rs
|
@ -1,7 +1,7 @@
|
||||||
use reqwest::header::CONTENT_TYPE;
|
use reqwest::header::CONTENT_TYPE;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use utils::{data_to_dataurl, is_data_url};
|
use utils::{clean_url, data_to_dataurl, is_data_url};
|
||||||
|
|
||||||
pub fn retrieve_asset(
|
pub fn retrieve_asset(
|
||||||
cache: &mut HashMap<String, String>,
|
cache: &mut HashMap<String, String>,
|
||||||
|
@ -11,15 +11,17 @@ pub fn retrieve_asset(
|
||||||
mime: &str,
|
mime: &str,
|
||||||
opt_silent: bool,
|
opt_silent: bool,
|
||||||
) -> Result<(String, String), reqwest::Error> {
|
) -> Result<(String, String), reqwest::Error> {
|
||||||
|
let cache_key = clean_url(&url);
|
||||||
|
|
||||||
if is_data_url(&url).unwrap() {
|
if is_data_url(&url).unwrap() {
|
||||||
Ok((url.to_string(), url.to_string()))
|
Ok((url.to_string(), url.to_string()))
|
||||||
} else {
|
} else {
|
||||||
if cache.contains_key(&url.to_string()) {
|
if cache.contains_key(&cache_key) {
|
||||||
// url is in cache
|
// url is in cache
|
||||||
if !opt_silent {
|
if !opt_silent {
|
||||||
eprintln!("{} (from cache)", &url);
|
eprintln!("{} (from cache)", &url);
|
||||||
}
|
}
|
||||||
let data = cache.get(&url.to_string()).unwrap();
|
let data = cache.get(&cache_key).unwrap();
|
||||||
Ok((data.to_string(), url.to_string()))
|
Ok((data.to_string(), url.to_string()))
|
||||||
} else {
|
} else {
|
||||||
// url not in cache, we request it
|
// url not in cache, we request it
|
||||||
|
@ -33,6 +35,8 @@ pub fn retrieve_asset(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let new_cache_key = clean_url(response.url().to_string());
|
||||||
|
|
||||||
if as_dataurl {
|
if as_dataurl {
|
||||||
// Convert response into a byte array
|
// Convert response into a byte array
|
||||||
let mut data: Vec<u8> = vec![];
|
let mut data: Vec<u8> = vec![];
|
||||||
|
@ -50,12 +54,12 @@ pub fn retrieve_asset(
|
||||||
};
|
};
|
||||||
let dataurl = data_to_dataurl(&mimetype, &data);
|
let dataurl = data_to_dataurl(&mimetype, &data);
|
||||||
// insert in cache
|
// insert in cache
|
||||||
cache.insert(response.url().to_string(), dataurl.to_string());
|
cache.insert(new_cache_key, dataurl.to_string());
|
||||||
Ok((dataurl, response.url().to_string()))
|
Ok((dataurl, response.url().to_string()))
|
||||||
} else {
|
} else {
|
||||||
let content = response.text().unwrap();
|
let content = response.text().unwrap();
|
||||||
// insert in cache
|
// insert in cache
|
||||||
cache.insert(response.url().to_string(), content.clone());
|
cache.insert(new_cache_key, content.clone());
|
||||||
Ok((content, response.url().to_string()))
|
Ok((content, response.url().to_string()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use crate::utils::{
|
use crate::utils::{
|
||||||
data_to_dataurl, detect_mimetype, is_data_url, is_valid_url, resolve_url, url_has_protocol,
|
clean_url, data_to_dataurl, detect_mimetype, is_data_url, is_valid_url, resolve_url,
|
||||||
|
url_has_protocol,
|
||||||
};
|
};
|
||||||
use url::ParseError;
|
use url::ParseError;
|
||||||
|
|
||||||
|
@ -158,3 +159,19 @@ fn test_is_data_url() {
|
||||||
assert!(!is_data_url("//kernel.org").unwrap_or(false));
|
assert!(!is_data_url("//kernel.org").unwrap_or(false));
|
||||||
assert!(!is_data_url("").unwrap_or(false));
|
assert!(!is_data_url("").unwrap_or(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_clean_url() {
|
||||||
|
assert_eq!(
|
||||||
|
clean_url("https://somewhere.com/font.eot#iefix"),
|
||||||
|
"https://somewhere.com/font.eot"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
clean_url("https://somewhere.com/font.eot#"),
|
||||||
|
"https://somewhere.com/font.eot"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
clean_url("https://somewhere.com/font.eot?#"),
|
||||||
|
"https://somewhere.com/font.eot"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
11
src/utils.rs
11
src/utils.rs
|
@ -196,3 +196,14 @@ pub fn resolve_css_imports(
|
||||||
resolved_css
|
resolved_css
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn clean_url<T: AsRef<str>>(url: T) -> String {
|
||||||
|
let mut result = Url::parse(url.as_ref()).unwrap();
|
||||||
|
// Clear fragment
|
||||||
|
result.set_fragment(None);
|
||||||
|
// Get rid of stray question mark
|
||||||
|
if result.query() == Some("") {
|
||||||
|
result.set_query(None);
|
||||||
|
}
|
||||||
|
result.to_string()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue