fix inverted split for DATA frame padding (#330)

This commit is contained in:
Sean McArthur
2018-11-05 10:20:09 -08:00
committed by Carl Lerche
parent 1a8015da4a
commit e656c42353
5 changed files with 91 additions and 7 deletions

View File

@@ -63,6 +63,18 @@ impl<T> Data<T> {
}
}
/// Returns whther the `PADDED` flag is set on this frame.
#[cfg(feature = "unstable")]
pub fn is_padded(&self) -> bool {
self.flags.is_padded()
}
/// Sets the value for the `PADDED` flag on this frame.
#[cfg(feature = "unstable")]
pub fn set_padded(&mut self) {
self.flags.set_padded();
}
/// Returns a reference to this frame's payload.
///
/// This does **not** include any padding that might have been originally
@@ -192,6 +204,11 @@ impl DataFlags {
fn is_padded(&self) -> bool {
self.0 & PADDED == PADDED
}
#[cfg(feature = "unstable")]
fn set_padded(&mut self) {
self.0 |= PADDED
}
}
impl Default for DataFlags {

View File

@@ -15,7 +15,8 @@ use bytes::Bytes;
/// If the padded payload is invalid (e.g. the length of the padding is equal
/// to the total length), returns `None`.
pub fn strip_padding(payload: &mut Bytes) -> Result<u8, Error> {
if payload.len() == 0 {
let payload_len = payload.len();
if payload_len == 0 {
// If this is the case, the frame is invalid as no padding length can be
// extracted, even though the frame should be padded.
return Err(Error::TooMuchPadding);
@@ -23,14 +24,14 @@ pub fn strip_padding(payload: &mut Bytes) -> Result<u8, Error> {
let pad_len = payload[0] as usize;
if pad_len >= payload.len() {
if pad_len >= payload_len {
// This is invalid: the padding length MUST be less than the
// total frame size.
return Err(Error::TooMuchPadding);
}
let _ = payload.split_to(1);
let _ = payload.split_off(pad_len);
let _ = payload.split_off(payload_len - pad_len - 1);
Ok(pad_len as u8)
}