mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Move UTF8 bytes handling into generic constructor.
Separate from Servo specific logic.
This commit is contained in:
parent
52b3226d54
commit
d3b8b6472b
2 changed files with 11 additions and 11 deletions
|
@ -63,9 +63,11 @@ impl HSTSList {
|
|||
}
|
||||
}
|
||||
|
||||
/// Create an `HSTSList` from the contents of a JSON preload file.
|
||||
pub fn from_preload(preload_content: &str) -> Option<HSTSList> {
|
||||
decode(preload_content).ok()
|
||||
/// Create an `HSTSList` from the bytes of a JSON preload file.
|
||||
pub fn from_preload(preload_content: &[u8]) -> Option<HSTSList> {
|
||||
from_utf8(&preload_content)
|
||||
.ok()
|
||||
.and_then(|c| decode(c).ok())
|
||||
}
|
||||
|
||||
pub fn is_host_secure(&self, host: &str) -> bool {
|
||||
|
@ -113,11 +115,9 @@ impl HSTSList {
|
|||
}
|
||||
|
||||
pub fn preload_hsts_domains() -> Option<HSTSList> {
|
||||
read_resource_file("hsts_preload.json").ok().and_then(|bytes| {
|
||||
from_utf8(&bytes).ok().and_then(|hsts_preload_content| {
|
||||
HSTSList::from_preload(hsts_preload_content)
|
||||
})
|
||||
})
|
||||
read_resource_file("hsts_preload.json")
|
||||
.ok()
|
||||
.and_then(|bytes| HSTSList::from_preload(&bytes))
|
||||
}
|
||||
|
||||
pub fn secure_url(url: &Url) -> Url {
|
||||
|
|
|
@ -150,19 +150,19 @@ fn test_push_entry_to_hsts_list_should_add_an_entry() {
|
|||
|
||||
#[test]
|
||||
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")
|
||||
}
|
||||
|
||||
#[test]
|
||||
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")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_hsts_preload_should_decode_host_and_includes_subdomains() {
|
||||
let mock_preload_content = "{\
|
||||
let mock_preload_content = b"{\
|
||||
\"entries\": [\
|
||||
{\"host\": \"mozilla.org\",\
|
||||
\"include_subdomains\": false}\
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue