Skip to content

Commit bb27bac

Browse files
fakeshadowrobjtede
andauthored
Add native tls support for actix_tls::connect module (#295)
Co-authored-by: Rob Ede <[email protected]>
1 parent f9262db commit bb27bac

File tree

9 files changed

+105
-9
lines changed

9 files changed

+105
-9
lines changed

actix-router/src/url.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,11 @@ impl Quoter {
170170
idx += 1;
171171
}
172172

173-
if let Some(data) = cloned {
174-
// Unsafe: we get data from http::Uri, which does utf-8 checks already
173+
cloned.map(|data| {
174+
// SAFETY: we get data from http::Uri, which does UTF-8 checks already
175175
// this code only decodes valid pct encoded values
176-
Some(unsafe { String::from_utf8_unchecked(data) })
177-
} else {
178-
None
179-
}
176+
unsafe { String::from_utf8_unchecked(data) }
177+
})
180178
}
181179
}
182180

actix-server/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changes
22

33
## Unreleased - 2021-xx-xx
4+
* Prevent panic when shutdown_timeout is very large. [f9262db]
5+
6+
[f9262db]: https://github.com/actix/actix-net/commit/f9262db
47

58

69
## 2.0.0-beta.3 - 2021-02-06

actix-server/src/test_server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ impl TestServer {
9292
let port = addr.port();
9393

9494
TestServerRuntime {
95-
system,
9695
addr,
9796
host,
9897
port,
98+
system,
9999
}
100100
}
101101

actix-service/src/map_err.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ where
180180
F: Fn(A::Error) -> E,
181181
{
182182
fn new(fut: A::Future, f: F) -> Self {
183-
MapErrServiceFuture { f, fut }
183+
MapErrServiceFuture { fut, f }
184184
}
185185
}
186186

actix-tls/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
generation failed instead of panic. [#296]
66
* Remove `connect::ssl::openssl::OpensslConnectServiceFactory`. [#297]
77
* Remove `connect::ssl::openssl::OpensslConnectService`. [#297]
8+
* Add `connect::ssl::native_tls` module for native tls support. [#295]
9+
* Rename `accept::{nativetls => native_tls}`. [#295]
810

11+
[#295]: https://github.com/actix/actix-net/pull/295
912
[#296]: https://github.com/actix/actix-net/pull/296
1013
[#297]: https://github.com/actix/actix-net/pull/297
1114

15+
1216
## 3.0.0-beta.4 - 2021-02-24
1317
* Rename `accept::openssl::{SslStream => TlsStream}`.
1418
* Add `connect::Connect::set_local_addr` to attach local `IpAddr`. [#282]

actix-tls/src/accept/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub mod openssl;
1616
pub mod rustls;
1717

1818
#[cfg(feature = "native-tls")]
19-
pub mod nativetls;
19+
pub mod native_tls;
2020

2121
pub(crate) static MAX_CONN: AtomicUsize = AtomicUsize::new(256);
2222

actix-tls/src/connect/ssl/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ pub mod openssl;
55

66
#[cfg(feature = "rustls")]
77
pub mod rustls;
8+
9+
#[cfg(feature = "native-tls")]
10+
pub mod native_tls;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use std::io;
2+
3+
use actix_rt::net::ActixStream;
4+
use actix_service::{Service, ServiceFactory};
5+
use futures_core::future::LocalBoxFuture;
6+
use log::trace;
7+
use tokio_native_tls::{TlsConnector as TokioNativetlsConnector, TlsStream};
8+
9+
pub use tokio_native_tls::native_tls::TlsConnector;
10+
11+
use crate::connect::{Address, Connection};
12+
13+
/// Native-tls connector factory and service
14+
pub struct NativetlsConnector {
15+
connector: TokioNativetlsConnector,
16+
}
17+
18+
impl NativetlsConnector {
19+
pub fn new(connector: TlsConnector) -> Self {
20+
Self {
21+
connector: TokioNativetlsConnector::from(connector),
22+
}
23+
}
24+
}
25+
26+
impl NativetlsConnector {
27+
pub fn service(connector: TlsConnector) -> Self {
28+
Self::new(connector)
29+
}
30+
}
31+
32+
impl Clone for NativetlsConnector {
33+
fn clone(&self) -> Self {
34+
Self {
35+
connector: self.connector.clone(),
36+
}
37+
}
38+
}
39+
40+
impl<T: Address, U> ServiceFactory<Connection<T, U>> for NativetlsConnector
41+
where
42+
U: ActixStream + 'static,
43+
{
44+
type Response = Connection<T, TlsStream<U>>;
45+
type Error = io::Error;
46+
type Config = ();
47+
type Service = Self;
48+
type InitError = ();
49+
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
50+
51+
fn new_service(&self, _: ()) -> Self::Future {
52+
let connector = self.clone();
53+
Box::pin(async { Ok(connector) })
54+
}
55+
}
56+
57+
// NativetlsConnector is both it's ServiceFactory and Service impl type.
58+
// As the factory and service share the same type and state.
59+
impl<T, U> Service<Connection<T, U>> for NativetlsConnector
60+
where
61+
T: Address,
62+
U: ActixStream + 'static,
63+
{
64+
type Response = Connection<T, TlsStream<U>>;
65+
type Error = io::Error;
66+
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
67+
68+
actix_service::always_ready!();
69+
70+
fn call(&self, stream: Connection<T, U>) -> Self::Future {
71+
let (io, stream) = stream.replace_io(());
72+
let connector = self.connector.clone();
73+
Box::pin(async move {
74+
trace!("SSL Handshake start for: {:?}", stream.host());
75+
connector
76+
.connect(stream.host(), io)
77+
.await
78+
.map(|res| {
79+
trace!("SSL Handshake success: {:?}", stream.host());
80+
stream.replace_io(res).1
81+
})
82+
.map_err(|e| {
83+
trace!("SSL Handshake error: {:?}", e);
84+
io::Error::new(io::ErrorKind::Other, format!("{}", e))
85+
})
86+
})
87+
}
88+
}

0 commit comments

Comments
 (0)