Skip to content

Commit

Permalink
Clean up return types from send_some_plaintext etc
Browse files Browse the repository at this point in the history
These don't need to return errors; they just do short writes.
So remove io::Result usage here, and unwrapping.

This is only an internal API; and there is not change
in observable behaviour.
  • Loading branch information
ctz committed Nov 7, 2020
1 parent 716e7ec commit 931560d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
8 changes: 4 additions & 4 deletions rustls/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ impl ClientSessionImpl {
pub fn write_early_data(&mut self, data: &[u8]) -> io::Result<usize> {
self.early_data.check_write(data.len())
.and_then(|sz| {
self.common.send_early_plaintext(&data[..sz])
Ok(self.common.send_early_plaintext(&data[..sz]))
})
}

Expand All @@ -603,7 +603,7 @@ impl ClientSessionImpl {
.and_then(|st| st.export_keying_material(output, label, context))
}

fn send_some_plaintext(&mut self, buf: &[u8]) -> io::Result<usize> {
fn send_some_plaintext(&mut self, buf: &[u8]) -> usize {
let mut st = self.state.take();
st.as_mut()
.map(|st| st.perhaps_write_key_update(self));
Expand Down Expand Up @@ -755,13 +755,13 @@ impl io::Write for ClientSession {
/// writing much data before it can be sent will
/// cause excess memory usage.
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.imp.send_some_plaintext(buf)
Ok(self.imp.send_some_plaintext(buf))
}

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
let mut sz = 0;
for buf in bufs {
sz += self.imp.send_some_plaintext(buf)?;
sz += self.imp.send_some_plaintext(buf);
}
Ok(sz)
}
Expand Down
6 changes: 3 additions & 3 deletions rustls/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ impl ServerSessionImpl {
.and_then(|st| st.export_keying_material(output, label, context))
}

fn send_some_plaintext(&mut self, buf: &[u8]) -> io::Result<usize> {
fn send_some_plaintext(&mut self, buf: &[u8]) -> usize {
let mut st = self.state.take();
st.as_mut()
.map(|st| st.perhaps_write_key_update(self));
Expand Down Expand Up @@ -674,13 +674,13 @@ impl io::Write for ServerSession {
/// writing much data before it can be sent will
/// cause excess memory usage.
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.imp.send_some_plaintext(buf)
Ok(self.imp.send_some_plaintext(buf))
}

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
let mut sz = 0;
for buf in bufs {
sz += self.imp.send_some_plaintext(buf)?;
sz += self.imp.send_some_plaintext(buf);
}
Ok(sz)
}
Expand Down
24 changes: 14 additions & 10 deletions rustls/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,41 +630,46 @@ impl SessionCommon {
///
/// If internal buffers are too small, this function will not accept
/// all the data.
pub fn send_some_plaintext(&mut self, data: &[u8]) -> io::Result<usize> {
pub fn send_some_plaintext(&mut self, data: &[u8]) -> usize {
self.send_plain(data, Limit::Yes)
}

pub fn send_early_plaintext(&mut self, data: &[u8]) -> io::Result<usize> {
pub fn send_early_plaintext(&mut self, data: &[u8]) -> usize {
debug_assert!(self.early_traffic);
debug_assert!(self.record_layer.is_encrypting());

if data.is_empty() {
// Don't send empty fragments.
return Ok(0);
return 0;
}

Ok(self.send_appdata_encrypt(data, Limit::Yes))
self.send_appdata_encrypt(data, Limit::Yes)
}

fn send_plain(&mut self, data: &[u8], limit: Limit) -> io::Result<usize> {
/// Encrypt and send some plaintext `data`. `limit` controls
/// whether the per-session buffer limits apply.
///
/// Returns the number of bytes written from `data`: this might
/// be less than `data.len()` if buffer limits were exceeded.
fn send_plain(&mut self, data: &[u8], limit: Limit) -> usize {
if !self.traffic {
// If we haven't completed handshaking, buffer
// plaintext to send once we do.
let len = match limit {
Limit::Yes => self.sendable_plaintext.append_limited_copy(data),
Limit::No => self.sendable_plaintext.append(data.to_vec())
};
return Ok(len);
return len;
}

debug_assert!(self.record_layer.is_encrypting());

if data.is_empty() {
// Don't send empty fragments.
return Ok(0);
return 0;
}

Ok(self.send_appdata_encrypt(data, limit))
self.send_appdata_encrypt(data, limit)
}

pub fn start_traffic(&mut self) {
Expand All @@ -681,8 +686,7 @@ impl SessionCommon {

while !self.sendable_plaintext.is_empty() {
let buf = self.sendable_plaintext.take_one();
self.send_plain(&buf, Limit::No)
.unwrap();
self.send_plain(&buf, Limit::No);
}
}

Expand Down

0 comments on commit 931560d

Please sign in to comment.