mirror of
https://gitlab.com/pulsechaincom/lighthouse-pulse.git
synced 2024-12-22 19:50:37 +00:00
Use MediaType accept header parser (#4216)
## Issue Addressed #3510 ## Proposed Changes - Replace mime with MediaTypeList - Remove parse_accept fn as MediaTypeList does it built-in - Get the supported media type of the highest q-factor in a single list iteration without sorting ## Additional Info I have addressed the suggested changes in previous [PR](https://github.com/sigp/lighthouse/pull/3520#discussion_r959048633)
This commit is contained in:
parent
2639e67e90
commit
a227ee7478
8
Cargo.lock
generated
8
Cargo.lock
generated
@ -2290,7 +2290,7 @@ dependencies = [
|
|||||||
"futures-util",
|
"futures-util",
|
||||||
"libsecp256k1",
|
"libsecp256k1",
|
||||||
"lighthouse_network",
|
"lighthouse_network",
|
||||||
"mime",
|
"mediatype",
|
||||||
"procinfo",
|
"procinfo",
|
||||||
"proto_array",
|
"proto_array",
|
||||||
"psutil",
|
"psutil",
|
||||||
@ -4962,6 +4962,12 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mediatype"
|
||||||
|
version = "0.19.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fea6e62614ab2fc0faa58bb15102a0382d368f896a9fa4776592589ab55c4de7"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
version = "2.5.0"
|
version = "2.5.0"
|
||||||
|
@ -10,7 +10,7 @@ edition = "2021"
|
|||||||
serde = { version = "1.0.116", features = ["derive"] }
|
serde = { version = "1.0.116", features = ["derive"] }
|
||||||
serde_json = "1.0.58"
|
serde_json = "1.0.58"
|
||||||
types = { path = "../../consensus/types" }
|
types = { path = "../../consensus/types" }
|
||||||
reqwest = { version = "0.11.0", features = ["json","stream"] }
|
reqwest = { version = "0.11.0", features = ["json", "stream"] }
|
||||||
lighthouse_network = { path = "../../beacon_node/lighthouse_network" }
|
lighthouse_network = { path = "../../beacon_node/lighthouse_network" }
|
||||||
proto_array = { path = "../../consensus/proto_array", optional = true }
|
proto_array = { path = "../../consensus/proto_array", optional = true }
|
||||||
ethereum_serde_utils = "0.5.0"
|
ethereum_serde_utils = "0.5.0"
|
||||||
@ -26,7 +26,7 @@ futures-util = "0.3.8"
|
|||||||
futures = "0.3.8"
|
futures = "0.3.8"
|
||||||
store = { path = "../../beacon_node/store", optional = true }
|
store = { path = "../../beacon_node/store", optional = true }
|
||||||
slashing_protection = { path = "../../validator_client/slashing_protection", optional = true }
|
slashing_protection = { path = "../../validator_client/slashing_protection", optional = true }
|
||||||
mime = "0.3.16"
|
mediatype = "0.19.13"
|
||||||
|
|
||||||
[target.'cfg(target_os = "linux")'.dependencies]
|
[target.'cfg(target_os = "linux")'.dependencies]
|
||||||
psutil = { version = "3.2.2", optional = true }
|
psutil = { version = "3.2.2", optional = true }
|
||||||
@ -34,4 +34,10 @@ procinfo = { version = "0.4.2", optional = true }
|
|||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["lighthouse"]
|
default = ["lighthouse"]
|
||||||
lighthouse = ["proto_array", "psutil", "procinfo", "store", "slashing_protection"]
|
lighthouse = [
|
||||||
|
"proto_array",
|
||||||
|
"psutil",
|
||||||
|
"procinfo",
|
||||||
|
"store",
|
||||||
|
"slashing_protection",
|
||||||
|
]
|
||||||
|
@ -3,9 +3,8 @@
|
|||||||
|
|
||||||
use crate::Error as ServerError;
|
use crate::Error as ServerError;
|
||||||
use lighthouse_network::{ConnectionDirection, Enr, Multiaddr, PeerConnectionStatus};
|
use lighthouse_network::{ConnectionDirection, Enr, Multiaddr, PeerConnectionStatus};
|
||||||
use mime::{Mime, APPLICATION, JSON, OCTET_STREAM, STAR};
|
use mediatype::{names, MediaType, MediaTypeList};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::cmp::Reverse;
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::str::{from_utf8, FromStr};
|
use std::str::{from_utf8, FromStr};
|
||||||
@ -1172,35 +1171,58 @@ impl FromStr for Accept {
|
|||||||
type Err = String;
|
type Err = String;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
let mut mimes = parse_accept(s)?;
|
let media_type_list = MediaTypeList::new(s);
|
||||||
|
|
||||||
// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
||||||
// find the highest q-factor supported accept type
|
// find the highest q-factor supported accept type
|
||||||
mimes.sort_by_key(|m| {
|
let mut highest_q = 0_u16;
|
||||||
Reverse(m.get_param("q").map_or(1000_u16, |n| {
|
let mut accept_type = None;
|
||||||
(n.as_ref().parse::<f32>().unwrap_or(0_f32) * 1000_f32) as u16
|
|
||||||
}))
|
|
||||||
});
|
|
||||||
mimes
|
|
||||||
.into_iter()
|
|
||||||
.find_map(|m| match (m.type_(), m.subtype()) {
|
|
||||||
(APPLICATION, OCTET_STREAM) => Some(Accept::Ssz),
|
|
||||||
(APPLICATION, JSON) => Some(Accept::Json),
|
|
||||||
(STAR, STAR) => Some(Accept::Any),
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
.ok_or_else(|| "accept header is not supported".to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_accept(accept: &str) -> Result<Vec<Mime>, String> {
|
const APPLICATION: &str = names::APPLICATION.as_str();
|
||||||
accept
|
const OCTET_STREAM: &str = names::OCTET_STREAM.as_str();
|
||||||
.split(',')
|
const JSON: &str = names::JSON.as_str();
|
||||||
.map(|part| {
|
const STAR: &str = names::_STAR.as_str();
|
||||||
part.parse()
|
const Q: &str = names::Q.as_str();
|
||||||
.map_err(|e| format!("error parsing Accept header: {}", e))
|
|
||||||
})
|
media_type_list.into_iter().for_each(|item| {
|
||||||
.collect()
|
if let Ok(MediaType {
|
||||||
|
ty,
|
||||||
|
subty,
|
||||||
|
suffix: _,
|
||||||
|
params,
|
||||||
|
}) = item
|
||||||
|
{
|
||||||
|
let q_accept = match (ty.as_str(), subty.as_str()) {
|
||||||
|
(APPLICATION, OCTET_STREAM) => Some(Accept::Ssz),
|
||||||
|
(APPLICATION, JSON) => Some(Accept::Json),
|
||||||
|
(STAR, STAR) => Some(Accept::Any),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
.map(|item_accept_type| {
|
||||||
|
let q_val = params
|
||||||
|
.iter()
|
||||||
|
.find_map(|(n, v)| match n.as_str() {
|
||||||
|
Q => {
|
||||||
|
Some((v.as_str().parse::<f32>().unwrap_or(0_f32) * 1000_f32) as u16)
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.or(Some(1000_u16));
|
||||||
|
|
||||||
|
(q_val.unwrap(), item_accept_type)
|
||||||
|
});
|
||||||
|
|
||||||
|
match q_accept {
|
||||||
|
Some((q, accept)) if q > highest_q => {
|
||||||
|
highest_q = q;
|
||||||
|
accept_type = Some(accept);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
accept_type.ok_or_else(|| "accept header is not supported".to_string())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
@ -1268,6 +1290,11 @@ mod tests {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
Accept::from_str("text/plain"),
|
Accept::from_str("text/plain"),
|
||||||
Err("accept header is not supported".to_string())
|
Err("accept header is not supported".to_string())
|
||||||
)
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
Accept::from_str("application/json;message=\"Hello, world!\";q=0.3,*/*;q=0.6").unwrap(),
|
||||||
|
Accept::Any
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user