| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
use std::fmt; |
| 8 |
|
| 9 |
use neqo_transport::StreamId; |
| 10 |
use sfv::{BareItem, Item, ListEntry, Parser}; |
| 11 |
|
| 12 |
use crate::{frames::HFrame, Error, Header, Res}; |
| 13 |
|
| 14 |
#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 15 |
pub struct Priority { |
| 16 |
urgency: u8, |
| 17 |
incremental: bool, |
| 18 |
} |
| 19 |
|
| 20 |
impl Default for Priority { |
| 21 |
fn default() -> Self { |
| 22 |
Self { |
| 23 |
urgency: 3, |
| 24 |
incremental: false, |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
impl Priority { |
| 30 |
/// # Panics |
| 31 |
/// |
| 32 |
/// If an invalid urgency (>7 is given) |
| 33 |
#[must_use] |
| 34 |
pub fn new(urgency: u8, incremental: bool) -> Self { |
| 35 |
assert!(urgency < 8); |
| 36 |
Self { |
| 37 |
urgency, |
| 38 |
incremental, |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/// Returns a header if required to send |
| 43 |
#[must_use] |
| 44 |
pub fn header(self) -> Option<Header> { |
| 45 |
match self { |
| 46 |
Self { |
| 47 |
urgency: 3, |
| 48 |
incremental: false, |
| 49 |
} => None, |
| 50 |
other => Some(Header::new("priority", format!("{other}"))), |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/// Constructs a priority from raw bytes (either a field value of frame content). |
| 55 |
/// |
| 56 |
/// # Errors |
| 57 |
/// |
| 58 |
/// When the contained syntax is invalid. |
| 59 |
/// |
| 60 |
/// # Panics |
| 61 |
/// |
| 62 |
/// Never, but the compiler is not smart enough to work that out. |
| 63 |
pub fn from_bytes(bytes: &[u8]) -> Res<Self> { |
| 64 |
let dict = Parser::parse_dictionary(bytes).map_err(|_| Error::HttpFrame)?; |
| 65 |
let urgency = match dict.get("u") { |
| 66 |
Some(ListEntry::Item(Item { |
| 67 |
bare_item: BareItem::Integer(u), |
| 68 |
.. |
| 69 |
})) if (0..=7).contains(u) => u8::try_from(*u).map_err(|_| Error::Internal)?, |
| 70 |
_ => 3, |
| 71 |
}; |
| 72 |
let incremental = match dict.get("i") { |
| 73 |
Some(ListEntry::Item(Item { |
| 74 |
bare_item: BareItem::Boolean(i), |
| 75 |
.. |
| 76 |
})) => *i, |
| 77 |
_ => false, |
| 78 |
}; |
| 79 |
Ok(Self { |
| 80 |
urgency, |
| 81 |
incremental, |
| 82 |
}) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
impl fmt::Display for Priority { |
| 87 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 88 |
match self { |
| 89 |
Self { |
| 90 |
urgency: 3, |
| 91 |
incremental: false, |
| 92 |
} => Ok(()), |
| 93 |
Self { |
| 94 |
urgency: 3, |
| 95 |
incremental: true, |
| 96 |
} => write!(f, "i"), |
| 97 |
Self { |
| 98 |
urgency, |
| 99 |
incremental: false, |
| 100 |
} => write!(f, "u={urgency}"), |
| 101 |
Self { |
| 102 |
urgency, |
| 103 |
incremental: true, |
| 104 |
} => write!(f, "u={urgency},i"), |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
#[derive(Debug)] |
| 110 |
pub struct PriorityHandler { |
| 111 |
push_stream: bool, |
| 112 |
priority: Priority, |
| 113 |
last_send_priority: Priority, |
| 114 |
} |
| 115 |
|
| 116 |
impl PriorityHandler { |
| 117 |
pub const fn new(push_stream: bool, priority: Priority) -> Self { |
| 118 |
Self { |
| 119 |
push_stream, |
| 120 |
priority, |
| 121 |
last_send_priority: priority, |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/*pub fn priority(&self) -> Priority { |
| 126 |
self.priority |
| 127 |
}*/ |
| 128 |
|
| 129 |
/// Returns if an priority update will be issued |
| 130 |
pub fn maybe_update_priority(&mut self, priority: Priority) -> bool { |
| 131 |
if priority == self.priority { |
| 132 |
false |
| 133 |
} else { |
| 134 |
self.priority = priority; |
| 135 |
true |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
pub fn priority_update_sent(&mut self) { |
| 140 |
self.last_send_priority = self.priority; |
| 141 |
} |
| 142 |
|
| 143 |
/// Returns `HFrame` if an priority update is outstanding |
| 144 |
pub fn maybe_encode_frame(&self, stream_id: StreamId) -> Option<HFrame> { |
| 145 |
if self.priority == self.last_send_priority { |
| 146 |
None |
| 147 |
} else if self.push_stream { |
| 148 |
Some(HFrame::PriorityUpdatePush { |
| 149 |
element_id: stream_id.as_u64(), |
| 150 |
priority: self.priority, |
| 151 |
}) |
| 152 |
} else { |
| 153 |
Some(HFrame::PriorityUpdateRequest { |
| 154 |
element_id: stream_id.as_u64(), |
| 155 |
priority: self.priority, |
| 156 |
}) |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
#[cfg(test)] |
| 162 |
mod test { |
| 163 |
use neqo_transport::StreamId; |
| 164 |
|
| 165 |
use crate::{priority::PriorityHandler, HFrame, Priority}; |
| 166 |
|
| 167 |
#[test] |
| 168 |
fn priority_updates_ignore_same() { |
| 169 |
let mut p = PriorityHandler::new(false, Priority::new(5, false)); |
| 170 |
assert!(!p.maybe_update_priority(Priority::new(5, false))); |
| 171 |
// updating with the same priority -> there should not be any priority frame sent |
| 172 |
assert!(p.maybe_encode_frame(StreamId::new(4)).is_none()); |
| 173 |
} |
| 174 |
|
| 175 |
#[test] |
| 176 |
fn priority_updates_send_update() { |
| 177 |
let mut p = PriorityHandler::new(false, Priority::new(5, false)); |
| 178 |
assert!(p.maybe_update_priority(Priority::new(6, false))); |
| 179 |
// updating with the a different priority -> there should be a priority frame sent |
| 180 |
assert!(p.maybe_encode_frame(StreamId::new(4)).is_some()); |
| 181 |
} |
| 182 |
|
| 183 |
#[test] |
| 184 |
fn multiple_priority_updates_ignore_same() { |
| 185 |
let mut p = PriorityHandler::new(false, Priority::new(5, false)); |
| 186 |
assert!(p.maybe_update_priority(Priority::new(6, false))); |
| 187 |
assert!(p.maybe_update_priority(Priority::new(5, false))); |
| 188 |
// initial and last priority same -> there should not be any priority frame sent |
| 189 |
assert!(p.maybe_encode_frame(StreamId::new(4)).is_none()); |
| 190 |
} |
| 191 |
|
| 192 |
#[test] |
| 193 |
fn multiple_priority_updates_send_update() { |
| 194 |
let mut p = PriorityHandler::new(false, Priority::new(5, false)); |
| 195 |
assert!(p.maybe_update_priority(Priority::new(6, false))); |
| 196 |
assert!(p.maybe_update_priority(Priority::new(7, false))); |
| 197 |
// updating two times with a different priority -> the last priority update should be in the |
| 198 |
// next frame |
| 199 |
let expected = HFrame::PriorityUpdateRequest { |
| 200 |
element_id: 4, |
| 201 |
priority: Priority::new(7, false), |
| 202 |
}; |
| 203 |
assert_eq!(p.maybe_encode_frame(StreamId::new(4)), Some(expected)); |
| 204 |
} |
| 205 |
|
| 206 |
#[test] |
| 207 |
fn priority_updates_incremental() { |
| 208 |
let mut p = PriorityHandler::new(false, Priority::new(5, false)); |
| 209 |
assert!(p.maybe_update_priority(Priority::new(5, true))); |
| 210 |
// updating the incremental parameter -> there should be a priority frame sent |
| 211 |
let expected = HFrame::PriorityUpdateRequest { |
| 212 |
element_id: 4, |
| 213 |
priority: Priority::new(5, true), |
| 214 |
}; |
| 215 |
assert_eq!(p.maybe_encode_frame(StreamId::new(4)), Some(expected)); |
| 216 |
} |
| 217 |
} |