mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Rustfmt net crate
This commit is contained in:
parent
ba1ed11ced
commit
2481ad25f8
30 changed files with 4957 additions and 2870 deletions
|
@ -9,22 +9,13 @@ use std::iter::Filter;
|
|||
use std::str::Split;
|
||||
use std::sync::MutexGuard;
|
||||
|
||||
const SUPPORTED_ALGORITHM: &'static [&'static str] = &[
|
||||
"sha256",
|
||||
"sha384",
|
||||
"sha512",
|
||||
];
|
||||
const SUPPORTED_ALGORITHM: &'static [&'static str] = &["sha256", "sha384", "sha512"];
|
||||
pub type StaticCharVec = &'static [char];
|
||||
/// A "space character" according to:
|
||||
///
|
||||
/// <https://html.spec.whatwg.org/multipage/#space-character>
|
||||
pub static HTML_SPACE_CHARACTERS: StaticCharVec = &[
|
||||
'\u{0020}',
|
||||
'\u{0009}',
|
||||
'\u{000a}',
|
||||
'\u{000c}',
|
||||
'\u{000d}',
|
||||
];
|
||||
pub static HTML_SPACE_CHARACTERS: StaticCharVec =
|
||||
&['\u{0020}', '\u{0009}', '\u{000a}', '\u{000c}', '\u{000d}'];
|
||||
#[derive(Clone)]
|
||||
pub struct SriEntry {
|
||||
pub alg: String,
|
||||
|
@ -79,9 +70,18 @@ pub fn parsed_metadata(integrity_metadata: &str) -> Vec<SriEntry> {
|
|||
}
|
||||
|
||||
/// <https://w3c.github.io/webappsec-subresource-integrity/#getprioritizedhashfunction>
|
||||
pub fn get_prioritized_hash_function(hash_func_left: &str, hash_func_right: &str) -> Option<String> {
|
||||
let left_priority = SUPPORTED_ALGORITHM.iter().position(|s| s.to_owned() == hash_func_left).unwrap();
|
||||
let right_priority = SUPPORTED_ALGORITHM.iter().position(|s| s.to_owned() == hash_func_right).unwrap();
|
||||
pub fn get_prioritized_hash_function(
|
||||
hash_func_left: &str,
|
||||
hash_func_right: &str,
|
||||
) -> Option<String> {
|
||||
let left_priority = SUPPORTED_ALGORITHM
|
||||
.iter()
|
||||
.position(|s| s.to_owned() == hash_func_left)
|
||||
.unwrap();
|
||||
let right_priority = SUPPORTED_ALGORITHM
|
||||
.iter()
|
||||
.position(|s| s.to_owned() == hash_func_right)
|
||||
.unwrap();
|
||||
|
||||
if left_priority == right_priority {
|
||||
return None;
|
||||
|
@ -91,7 +91,6 @@ pub fn get_prioritized_hash_function(hash_func_left: &str, hash_func_right: &str
|
|||
} else {
|
||||
Some(hash_func_right.to_owned())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <https://w3c.github.io/webappsec-subresource-integrity/#get-the-strongest-metadata>
|
||||
|
@ -100,8 +99,8 @@ pub fn get_strongest_metadata(integrity_metadata_list: Vec<SriEntry>) -> Vec<Sri
|
|||
let mut current_algorithm = result[0].alg.clone();
|
||||
|
||||
for integrity_metadata in &integrity_metadata_list[1..] {
|
||||
let prioritized_hash = get_prioritized_hash_function(&integrity_metadata.alg,
|
||||
&*current_algorithm);
|
||||
let prioritized_hash =
|
||||
get_prioritized_hash_function(&integrity_metadata.alg, &*current_algorithm);
|
||||
if prioritized_hash.is_none() {
|
||||
result.push(integrity_metadata.clone());
|
||||
} else if let Some(algorithm) = prioritized_hash {
|
||||
|
@ -116,9 +115,10 @@ pub fn get_strongest_metadata(integrity_metadata_list: Vec<SriEntry>) -> Vec<Sri
|
|||
}
|
||||
|
||||
/// <https://w3c.github.io/webappsec-subresource-integrity/#apply-algorithm-to-response>
|
||||
fn apply_algorithm_to_response(body: MutexGuard<ResponseBody>,
|
||||
message_digest: MessageDigest)
|
||||
-> String {
|
||||
fn apply_algorithm_to_response(
|
||||
body: MutexGuard<ResponseBody>,
|
||||
message_digest: MessageDigest,
|
||||
) -> String {
|
||||
if let ResponseBody::Done(ref vec) = *body {
|
||||
let response_digest = hash(message_digest, vec).unwrap(); //Now hash
|
||||
base64::encode(&response_digest)
|
||||
|
@ -171,8 +171,12 @@ pub fn is_response_integrity_valid(integrity_metadata: &str, response: &Response
|
|||
false
|
||||
}
|
||||
|
||||
pub fn split_html_space_chars<'a>(s: &'a str) ->
|
||||
Filter<Split<'a, StaticCharVec>, fn(&&str) -> bool> {
|
||||
fn not_empty(&split: &&str) -> bool { !split.is_empty() }
|
||||
s.split(HTML_SPACE_CHARACTERS).filter(not_empty as fn(&&str) -> bool)
|
||||
pub fn split_html_space_chars<'a>(
|
||||
s: &'a str,
|
||||
) -> Filter<Split<'a, StaticCharVec>, fn(&&str) -> bool> {
|
||||
fn not_empty(&split: &&str) -> bool {
|
||||
!split.is_empty()
|
||||
}
|
||||
s.split(HTML_SPACE_CHARACTERS)
|
||||
.filter(not_empty as fn(&&str) -> bool)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue