Move UTF8 bytes handling into generic constructor.

Separate from Servo specific logic.
This commit is contained in:
Corey Farwell 2016-04-17 12:14:56 -04:00
parent 52b3226d54
commit d3b8b6472b
2 changed files with 11 additions and 11 deletions

View file

@ -63,9 +63,11 @@ impl HSTSList {
} }
} }
/// Create an `HSTSList` from the contents of a JSON preload file. /// Create an `HSTSList` from the bytes of a JSON preload file.
pub fn from_preload(preload_content: &str) -> Option<HSTSList> { pub fn from_preload(preload_content: &[u8]) -> Option<HSTSList> {
decode(preload_content).ok() from_utf8(&preload_content)
.ok()
.and_then(|c| decode(c).ok())
} }
pub fn is_host_secure(&self, host: &str) -> bool { pub fn is_host_secure(&self, host: &str) -> bool {
@ -113,11 +115,9 @@ impl HSTSList {
} }
pub fn preload_hsts_domains() -> Option<HSTSList> { pub fn preload_hsts_domains() -> Option<HSTSList> {
read_resource_file("hsts_preload.json").ok().and_then(|bytes| { read_resource_file("hsts_preload.json")
from_utf8(&bytes).ok().and_then(|hsts_preload_content| { .ok()
HSTSList::from_preload(hsts_preload_content) .and_then(|bytes| HSTSList::from_preload(&bytes))
})
})
} }
pub fn secure_url(url: &Url) -> Url { pub fn secure_url(url: &Url) -> Url {

View file

@ -150,19 +150,19 @@ fn test_push_entry_to_hsts_list_should_add_an_entry() {
#[test] #[test]
fn test_parse_hsts_preload_should_return_none_when_json_invalid() { fn test_parse_hsts_preload_should_return_none_when_json_invalid() {
let mock_preload_content = "derp"; let mock_preload_content = b"derp";
assert!(HSTSList::from_preload(mock_preload_content).is_none(), "invalid preload list should not have parsed") assert!(HSTSList::from_preload(mock_preload_content).is_none(), "invalid preload list should not have parsed")
} }
#[test] #[test]
fn test_parse_hsts_preload_should_return_none_when_json_contains_no_entries_key() { fn test_parse_hsts_preload_should_return_none_when_json_contains_no_entries_key() {
let mock_preload_content = "{\"nothing\": \"to see here\"}"; let mock_preload_content = b"{\"nothing\": \"to see here\"}";
assert!(HSTSList::from_preload(mock_preload_content).is_none(), "invalid preload list should not have parsed") assert!(HSTSList::from_preload(mock_preload_content).is_none(), "invalid preload list should not have parsed")
} }
#[test] #[test]
fn test_parse_hsts_preload_should_decode_host_and_includes_subdomains() { fn test_parse_hsts_preload_should_decode_host_and_includes_subdomains() {
let mock_preload_content = "{\ let mock_preload_content = b"{\
\"entries\": [\ \"entries\": [\
{\"host\": \"mozilla.org\",\ {\"host\": \"mozilla.org\",\
\"include_subdomains\": false}\ \"include_subdomains\": false}\