Skip to content

Commit 3cc6ae8

Browse files
committed
fix q parsing in MediaTypeProposal
1 parent d1d23e9 commit 3cc6ae8

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/content/media_type_proposal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ impl MediaTypeProposal {
5252
/// we have to parse the full string to the media type first, and then see if
5353
/// a `q` value has been set.
5454
pub(crate) fn from_str(s: &str) -> crate::Result<Self> {
55-
let media_type = Mime::from_str(s)?;
55+
let mut media_type = Mime::from_str(s)?;
5656
let weight = media_type
57-
.param("q")
57+
.remove_param("q")
5858
.map(|param| param.as_str().parse())
5959
.transpose()?;
6060
Ok(Self::new(media_type, weight)?)

src/mime/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,33 @@ impl Mime {
109109
})
110110
.flatten()
111111
}
112+
113+
/// Remove a param from the set. Returns the `ParamValue` if it was contained within the set.
114+
pub fn remove_param(&mut self, name: impl Into<ParamName>) -> Option<ParamValue> {
115+
let name: ParamName = name.into();
116+
let mut unset_params = false;
117+
let ret = self
118+
.params
119+
.as_mut()
120+
.map(|inner| match inner {
121+
ParamKind::Vec(v) => match v.iter().position(|(k, _)| k == &name) {
122+
Some(index) => Some(v.remove(index).1),
123+
None => None,
124+
},
125+
ParamKind::Utf8 => match name {
126+
ParamName(Cow::Borrowed("charset")) => {
127+
unset_params = true;
128+
Some(ParamValue(Cow::Borrowed("utf8")))
129+
}
130+
_ => None,
131+
},
132+
})
133+
.flatten();
134+
if unset_params {
135+
self.params = None;
136+
}
137+
ret
138+
}
112139
}
113140

114141
impl PartialEq<Mime> for Mime {

0 commit comments

Comments
 (0)