Skip to content

Commit

Permalink
quininer#33 limit buffer size for TLS session
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Zhai committed Apr 19, 2019
1 parent 5bf48c1 commit 66068c8
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub mod server;

use common::Stream;
use futures::{Async, Future, Poll};
use rustls::{ClientConfig, ClientSession, ServerConfig, ServerSession};
use rustls::{ClientConfig, ClientSession, ServerConfig, ServerSession, Session};
use std::sync::Arc;
use std::{io, mem};
use tokio_io::{try_nb, AsyncRead, AsyncWrite};
Expand Down Expand Up @@ -64,6 +64,7 @@ impl TlsState {
#[derive(Clone)]
pub struct TlsConnector {
inner: Arc<ClientConfig>,
buffer_limit: usize,
#[cfg(feature = "early-data")]
early_data: bool,
}
Expand All @@ -72,12 +73,14 @@ pub struct TlsConnector {
#[derive(Clone)]
pub struct TlsAcceptor {
inner: Arc<ServerConfig>,
buffer_limit: usize,
}

impl From<Arc<ClientConfig>> for TlsConnector {
fn from(inner: Arc<ClientConfig>) -> TlsConnector {
TlsConnector {
inner,
buffer_limit: 0,
#[cfg(feature = "early-data")]
early_data: false,
}
Expand All @@ -86,7 +89,10 @@ impl From<Arc<ClientConfig>> for TlsConnector {

impl From<Arc<ServerConfig>> for TlsAcceptor {
fn from(inner: Arc<ServerConfig>) -> TlsAcceptor {
TlsAcceptor { inner }
TlsAcceptor {
inner,
buffer_limit: 0,
}
}
}

Expand All @@ -101,6 +107,11 @@ impl TlsConnector {
self
}

pub fn set_buffer_limit(mut self, limit: usize) -> TlsConnector {
self.buffer_limit = limit;
self
}

pub fn connect<IO>(&self, domain: DNSNameRef, stream: IO) -> Connect<IO>
where
IO: AsyncRead + AsyncWrite,
Expand All @@ -115,6 +126,7 @@ impl TlsConnector {
F: FnOnce(&mut ClientSession),
{
let mut session = ClientSession::new(&self.inner, domain);
session.set_buffer_limit(self.buffer_limit);
f(&mut session);

#[cfg(not(feature = "early-data"))]
Expand Down Expand Up @@ -148,6 +160,11 @@ impl TlsConnector {
}

impl TlsAcceptor {
pub fn set_buffer_limit(mut self, limit: usize) -> TlsAcceptor {
self.buffer_limit = limit;
self
}

pub fn accept<IO>(&self, stream: IO) -> Accept<IO>
where
IO: AsyncRead + AsyncWrite,
Expand All @@ -162,6 +179,7 @@ impl TlsAcceptor {
F: FnOnce(&mut ServerSession),
{
let mut session = ServerSession::new(&self.inner);
session.set_buffer_limit(self.buffer_limit);
f(&mut session);

Accept(server::MidHandshake::Handshaking(server::TlsStream {
Expand Down

0 comments on commit 66068c8

Please sign in to comment.