tokio only
* remove io::Read/io::Write support * stable vecio
This commit is contained in:
parent
db21c4c947
commit
5f6d0233ed
15
Cargo.toml
15
Cargo.toml
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "tokio-rustls"
|
||||
version = "0.9.0"
|
||||
version = "0.10.0-alpha"
|
||||
authors = ["quininer kel <quininer@live.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
repository = "https://github.com/quininer/tokio-rustls"
|
||||
@ -15,18 +15,13 @@ travis-ci = { repository = "quininer/tokio-rustls" }
|
||||
appveyor = { repository = "quininer/tokio-rustls" }
|
||||
|
||||
[dependencies]
|
||||
futures = { version = "0.1", optional = true }
|
||||
tokio-io = { version = "0.1.6", optional = true }
|
||||
bytes = { version = "0.4", optional = true }
|
||||
iovec = { version = "0.1", optional = true }
|
||||
futures = "0.1"
|
||||
tokio-io = "0.1.6"
|
||||
bytes = "0.4"
|
||||
iovec = "0.1"
|
||||
rustls = "0.15"
|
||||
webpki = "0.19"
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = "0.1.6"
|
||||
lazy_static = "1"
|
||||
|
||||
[features]
|
||||
default = ["tokio-support"]
|
||||
nightly = ["bytes", "iovec"]
|
||||
tokio-support = ["futures", "tokio-io"]
|
||||
|
@ -1,16 +1,9 @@
|
||||
#[cfg(feature = "nightly")]
|
||||
#[cfg(feature = "tokio-support")]
|
||||
mod vecbuf;
|
||||
|
||||
use std::io::{ self, Read, Write };
|
||||
#[cfg(feature = "nightly")]
|
||||
use std::io::Initializer;
|
||||
use rustls::Session;
|
||||
#[cfg(feature = "nightly")]
|
||||
use rustls::WriteV;
|
||||
#[cfg(feature = "nightly")]
|
||||
#[cfg(feature = "tokio-support")]
|
||||
use tokio_io::AsyncWrite;
|
||||
use tokio_io::{ AsyncRead, AsyncWrite };
|
||||
|
||||
|
||||
pub struct Stream<'a, S: 'a, IO: 'a> {
|
||||
@ -18,11 +11,11 @@ pub struct Stream<'a, S: 'a, IO: 'a> {
|
||||
pub io: &'a mut IO
|
||||
}
|
||||
|
||||
pub trait WriteTls<'a, S: Session, IO: Read + Write>: Read + Write {
|
||||
pub trait WriteTls<'a, S: Session, IO: AsyncRead + AsyncWrite>: Read + Write {
|
||||
fn write_tls(&mut self) -> io::Result<usize>;
|
||||
}
|
||||
|
||||
impl<'a, S: Session, IO: Read + Write> Stream<'a, S, IO> {
|
||||
impl<'a, S: Session, IO: AsyncRead + AsyncWrite> Stream<'a, S, IO> {
|
||||
pub fn new(session: &'a mut S, io: &'a mut IO) -> Self {
|
||||
Stream { session, io }
|
||||
}
|
||||
@ -73,23 +66,7 @@ impl<'a, S: Session, IO: Read + Write> Stream<'a, S, IO> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "nightly"))]
|
||||
impl<'a, S: Session, IO: Read + Write> WriteTls<'a, S, IO> for Stream<'a, S, IO> {
|
||||
fn write_tls(&mut self) -> io::Result<usize> {
|
||||
self.session.write_tls(self.io)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
impl<'a, S: Session, IO: Read + Write> WriteTls<'a, S, IO> for Stream<'a, S, IO> {
|
||||
default fn write_tls(&mut self) -> io::Result<usize> {
|
||||
self.session.write_tls(self.io)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
#[cfg(feature = "tokio-support")]
|
||||
impl<'a, S: Session, IO: Read + AsyncWrite> WriteTls<'a, S, IO> for Stream<'a, S, IO> {
|
||||
impl<'a, S: Session, IO: AsyncRead + AsyncWrite> WriteTls<'a, S, IO> for Stream<'a, S, IO> {
|
||||
fn write_tls(&mut self) -> io::Result<usize> {
|
||||
use futures::Async;
|
||||
use self::vecbuf::VecBuf;
|
||||
@ -112,12 +89,7 @@ impl<'a, S: Session, IO: Read + AsyncWrite> WriteTls<'a, S, IO> for Stream<'a, S
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: Session, IO: Read + Write> Read for Stream<'a, S, IO> {
|
||||
#[cfg(feature = "nightly")]
|
||||
unsafe fn initializer(&self) -> Initializer {
|
||||
Initializer::nop()
|
||||
}
|
||||
|
||||
impl<'a, S: Session, IO: AsyncRead + AsyncWrite> Read for Stream<'a, S, IO> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
while self.session.wants_read() {
|
||||
if let (0, 0) = self.complete_io()? {
|
||||
@ -128,7 +100,7 @@ impl<'a, S: Session, IO: Read + Write> Read for Stream<'a, S, IO> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: Session, IO: Read + Write> io::Write for Stream<'a, S, IO> {
|
||||
impl<'a, S: Session, IO: AsyncRead + AsyncWrite> Write for Stream<'a, S, IO> {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
let len = self.session.write(buf)?;
|
||||
while self.session.wants_write() {
|
||||
|
@ -7,6 +7,8 @@ use rustls::{
|
||||
ServerSession, ClientSession,
|
||||
Session, NoClientAuth
|
||||
};
|
||||
use futures::{ Async, Poll };
|
||||
use tokio_io::{ AsyncRead, AsyncWrite };
|
||||
use super::Stream;
|
||||
|
||||
|
||||
@ -31,6 +33,13 @@ impl<'a> Write for Good<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> AsyncRead for Good<'a> {}
|
||||
impl<'a> AsyncWrite for Good<'a> {
|
||||
fn shutdown(&mut self) -> Poll<(), io::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
}
|
||||
|
||||
struct Bad(bool);
|
||||
|
||||
impl Read for Bad {
|
||||
@ -53,6 +62,13 @@ impl Write for Bad {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncRead for Bad {}
|
||||
impl AsyncWrite for Bad {
|
||||
fn shutdown(&mut self) -> Poll<(), io::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn stream_good() -> io::Result<()> {
|
||||
|
30
src/lib.rs
30
src/lib.rs
@ -1,34 +1,25 @@
|
||||
//! Asynchronous TLS/SSL streams for Tokio using [Rustls](https://github.com/ctz/rustls).
|
||||
|
||||
#![cfg_attr(feature = "nightly", feature(specialization, read_initializer))]
|
||||
|
||||
pub extern crate rustls;
|
||||
pub extern crate webpki;
|
||||
|
||||
#[cfg(feature = "tokio-support")]
|
||||
extern crate futures;
|
||||
#[cfg(feature = "tokio-support")]
|
||||
extern crate tokio_io;
|
||||
#[cfg(feature = "nightly")]
|
||||
#[cfg(feature = "tokio-support")]
|
||||
extern crate bytes;
|
||||
#[cfg(feature = "nightly")]
|
||||
#[cfg(feature = "tokio-support")]
|
||||
extern crate iovec;
|
||||
|
||||
|
||||
mod common;
|
||||
#[cfg(feature = "tokio-support")] mod tokio_impl;
|
||||
mod tokio_impl;
|
||||
|
||||
use std::io;
|
||||
use std::sync::Arc;
|
||||
#[cfg(feature = "nightly")]
|
||||
use std::io::Initializer;
|
||||
use webpki::DNSNameRef;
|
||||
use rustls::{
|
||||
Session, ClientSession, ServerSession,
|
||||
ClientConfig, ServerConfig,
|
||||
};
|
||||
use tokio_io::{ AsyncRead, AsyncWrite };
|
||||
use common::Stream;
|
||||
|
||||
|
||||
@ -56,7 +47,7 @@ impl From<Arc<ServerConfig>> for TlsAcceptor {
|
||||
|
||||
impl TlsConnector {
|
||||
pub fn connect<IO>(&self, domain: DNSNameRef, stream: IO) -> Connect<IO>
|
||||
where IO: io::Read + io::Write
|
||||
where IO: AsyncRead + AsyncWrite
|
||||
{
|
||||
Self::connect_with_session(stream, ClientSession::new(&self.inner, domain))
|
||||
}
|
||||
@ -64,7 +55,7 @@ impl TlsConnector {
|
||||
#[inline]
|
||||
pub fn connect_with_session<IO>(stream: IO, session: ClientSession)
|
||||
-> Connect<IO>
|
||||
where IO: io::Read + io::Write
|
||||
where IO: AsyncRead + AsyncWrite
|
||||
{
|
||||
Connect(MidHandshake {
|
||||
inner: Some(TlsStream { session, io: stream, is_shutdown: false, eof: false })
|
||||
@ -74,14 +65,14 @@ impl TlsConnector {
|
||||
|
||||
impl TlsAcceptor {
|
||||
pub fn accept<IO>(&self, stream: IO) -> Accept<IO>
|
||||
where IO: io::Read + io::Write,
|
||||
where IO: AsyncRead + AsyncWrite,
|
||||
{
|
||||
Self::accept_with_session(stream, ServerSession::new(&self.inner))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn accept_with_session<IO>(stream: IO, session: ServerSession) -> Accept<IO>
|
||||
where IO: io::Read + io::Write
|
||||
where IO: AsyncRead + AsyncWrite
|
||||
{
|
||||
Accept(MidHandshake {
|
||||
inner: Some(TlsStream { session, io: stream, is_shutdown: false, eof: false })
|
||||
@ -145,13 +136,8 @@ impl<IO, S: Session> From<(IO, S)> for TlsStream<IO, S> {
|
||||
}
|
||||
|
||||
impl<IO, S> io::Read for TlsStream<IO, S>
|
||||
where IO: io::Read + io::Write, S: Session
|
||||
where IO: AsyncRead + AsyncWrite, S: Session
|
||||
{
|
||||
#[cfg(feature = "nightly")]
|
||||
unsafe fn initializer(&self) -> Initializer {
|
||||
Initializer::nop()
|
||||
}
|
||||
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
if self.eof {
|
||||
return Ok(0);
|
||||
@ -172,7 +158,7 @@ impl<IO, S> io::Read for TlsStream<IO, S>
|
||||
}
|
||||
|
||||
impl<IO, S> io::Write for TlsStream<IO, S>
|
||||
where IO: io::Read + io::Write, S: Session
|
||||
where IO: AsyncRead + AsyncWrite, S: Session
|
||||
{
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
Stream::new(&mut self.session, &mut self.io).write(buf)
|
||||
|
@ -35,7 +35,7 @@ impl<IO: AsyncRead + AsyncWrite> Future for Accept<IO> {
|
||||
|
||||
impl<IO, S> Future for MidHandshake<IO, S>
|
||||
where
|
||||
IO: io::Read + io::Write,
|
||||
IO: AsyncRead + AsyncWrite,
|
||||
S: Session
|
||||
{
|
||||
type Item = TlsStream<IO, S>;
|
||||
|
Loading…
Reference in New Issue
Block a user