mirror of https://github.com/ctz/rustls
Move tail of `pop()` into its own function
For me, this was relatively unclear control flow. Replace the `break len` with direct function calls. Observe that both lengths are always the handshake state `expected_len`, so that parallel data flow can be dropped.
This commit is contained in:
parent
c6a1e58954
commit
ba5c08ee36
|
@ -49,14 +49,16 @@ impl MessageDeframer {
|
||||||
// We loop over records we've received but not processed yet.
|
// We loop over records we've received but not processed yet.
|
||||||
// For records that decrypt as `Handshake`, we keep the current state of the joined
|
// For records that decrypt as `Handshake`, we keep the current state of the joined
|
||||||
// handshake message payload in `self.joining_hs`, appending to it as we see records.
|
// handshake message payload in `self.joining_hs`, appending to it as we see records.
|
||||||
let expected_len = loop {
|
loop {
|
||||||
// Fragmented handshake messages are collected (in plaintext) at the start of
|
// Fragmented handshake messages are collected (in plaintext) at the start of
|
||||||
// `buffer`, and the usage for this purpose is tracked by `joining_hs`.
|
// `buffer`, and the usage for this purpose is tracked by `joining_hs`.
|
||||||
let start = match &self.joining_hs {
|
let start = match &self.joining_hs {
|
||||||
Some(meta) => {
|
Some(meta) => {
|
||||||
match meta.expected_len {
|
match meta.expected_len {
|
||||||
// We're joining a handshake payload, and we've seen the full payload.
|
// We're joining a handshake payload, and we've seen the full payload.
|
||||||
Some(len) if len <= meta.payload.len() => break len,
|
Some(len) if len <= meta.payload.len() => {
|
||||||
|
return self.pop_handshake(buffer)
|
||||||
|
}
|
||||||
// Not enough data, and we can't parse any more out of the buffer (QUIC).
|
// Not enough data, and we can't parse any more out of the buffer (QUIC).
|
||||||
_ if meta.quic => return Ok(None),
|
_ if meta.quic => return Ok(None),
|
||||||
// Try parsing some more of the encrypted buffered data.
|
// Try parsing some more of the encrypted buffered data.
|
||||||
|
@ -171,12 +173,21 @@ impl MessageDeframer {
|
||||||
// than the currently buffered payload, we need to wait for more data.
|
// than the currently buffered payload, we need to wait for more data.
|
||||||
match self.append_hs::<_, false>(msg.version, &msg.payload.0, end, buffer)? {
|
match self.append_hs::<_, false>(msg.version, &msg.payload.0, end, buffer)? {
|
||||||
HandshakePayloadState::Blocked => return Ok(None),
|
HandshakePayloadState::Blocked => return Ok(None),
|
||||||
HandshakePayloadState::Complete(len) => break len,
|
HandshakePayloadState::Complete => return self.pop_handshake(buffer),
|
||||||
HandshakePayloadState::Continue => continue,
|
HandshakePayloadState::Continue => continue,
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let meta = self.joining_hs.as_mut().unwrap(); // safe after calling `append_hs()`
|
fn pop_handshake(
|
||||||
|
&mut self,
|
||||||
|
buffer: &mut DeframerSliceBuffer,
|
||||||
|
) -> Result<Option<Deframed>, Error> {
|
||||||
|
// prerequisites:
|
||||||
|
// - `joining_hs` not None (it tells us where the handshake message is)
|
||||||
|
// - `joining_hs.expected_len` not None (ditto, how big it is)
|
||||||
|
let meta = self.joining_hs.as_mut().unwrap();
|
||||||
|
let expected_len = meta.expected_len.unwrap();
|
||||||
|
|
||||||
// We can now wrap the complete handshake payload in a `PlainMessage`, to be returned.
|
// We can now wrap the complete handshake payload in a `PlainMessage`, to be returned.
|
||||||
let message = PlainMessage {
|
let message = PlainMessage {
|
||||||
|
@ -291,7 +302,7 @@ impl MessageDeframer {
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(match meta.expected_len {
|
Ok(match meta.expected_len {
|
||||||
Some(len) if len <= meta.payload.len() => HandshakePayloadState::Complete(len),
|
Some(len) if len <= meta.payload.len() => HandshakePayloadState::Complete,
|
||||||
_ => match buffer.len() > meta.message.end {
|
_ => match buffer.len() > meta.message.end {
|
||||||
true => HandshakePayloadState::Continue,
|
true => HandshakePayloadState::Continue,
|
||||||
false => HandshakePayloadState::Blocked,
|
false => HandshakePayloadState::Blocked,
|
||||||
|
@ -515,7 +526,7 @@ enum HandshakePayloadState {
|
||||||
/// Waiting for more data.
|
/// Waiting for more data.
|
||||||
Blocked,
|
Blocked,
|
||||||
/// We have a complete handshake message.
|
/// We have a complete handshake message.
|
||||||
Complete(usize),
|
Complete,
|
||||||
/// More records available for processing.
|
/// More records available for processing.
|
||||||
Continue,
|
Continue,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue