Skip to content

Commit

Permalink
Revert previous commit
Browse files Browse the repository at this point in the history
Per issue #132 discussion: the approach in 2697ae complicates the
API, the solution will be via LdapConnSettings, but I want to
record the contribution since the basic idea is sound and useful.
  • Loading branch information
inejge committed Oct 23, 2024
1 parent 2697ae1 commit d3147ff
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 123 deletions.
2 changes: 1 addition & 1 deletion examples/bind_sync.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Demonstrates synchronously connecting, binding to,
// and disconnection from the server.
// and disconnectiong from the server.

use ldap3::result::Result;
use ldap3::LdapConn;
Expand Down
20 changes: 0 additions & 20 deletions examples/bind_sync_from_exiting_tcp_stream.rs

This file was deleted.

57 changes: 5 additions & 52 deletions src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,38 +396,6 @@ impl LdapConnAsync {
Self::from_url_with_settings(LdapConnSettings::new(), url).await
}

/// Create a connection to an LDAP server from existing UnixStream.
#[cfg(unix)]
pub fn from_unix_stream(stream: UnixStream) -> (Self, Ldap) {
Self::conn_pair(ConnType::Unix(stream))
}

/// Create a connection to an LDAP server from existing UnixStream.
#[cfg(not(unix))]
pub fn from_unix_stream(stream: UnixStream) -> (Self, Ldap) {
unimplemented!("no Unix domain sockets on non-Unix platforms");
}

/// Create a connection to an LDAP server from existing TcpStream,
/// specified by an already parsed `Url`, using
/// `settings` to specify additional parameters.
pub async fn from_tcp_stream(
stream: TcpStream,
settings: LdapConnSettings,
url: &Url,
) -> Result<(Self, Ldap)> {
let mut settings = settings;
let timeout = settings.conn_timeout.take();

let (scheme, hostname, _host_port) = Self::extract_scheme_host_port(url, &settings)?;
let conn_future = Self::new_tcp_stream(stream, scheme, hostname, settings);
if let Some(timeout) = timeout {
time::timeout(timeout, conn_future).await?
} else {
conn_future.await
}
}

#[cfg(unix)]
async fn new_unix(url: &Url, _settings: LdapConnSettings) -> Result<(Self, Ldap)> {
let path = url.host_str().unwrap_or("");
Expand All @@ -447,10 +415,8 @@ impl LdapConnAsync {
unimplemented!("no Unix domain sockets on non-Unix platforms");
}

fn extract_scheme_host_port<'a>(
url: &'a Url,
settings: &LdapConnSettings,
) -> Result<(&'a str, &'a str, String)> {
#[allow(unused_mut)]
async fn new_tcp(url: &Url, mut settings: LdapConnSettings) -> Result<(Self, Ldap)> {
let mut port = 389;
let scheme = match url.scheme() {
s @ "ldap" => {
Expand All @@ -462,6 +428,7 @@ impl LdapConnAsync {
}
#[cfg(any(feature = "tls-native", feature = "tls-rustls"))]
s @ "ldaps" => {
settings = settings.set_starttls(false);
port = 636;
s
}
Expand All @@ -470,26 +437,12 @@ impl LdapConnAsync {
if let Some(url_port) = url.port() {
port = url_port;
}
let (hostname, host_port) = match url.host_str() {
let (_hostname, host_port) = match url.host_str() {
Some(h) if !h.is_empty() => (h, format!("{}:{}", h, port)),
Some(h) if !h.is_empty() => ("localhost", format!("localhost:{}", port)),
_ => panic!("unexpected None from url.host_str()"),
};
Ok((scheme, hostname, host_port))
}

async fn new_tcp(url: &Url, settings: LdapConnSettings) -> Result<(Self, Ldap)> {
let (scheme, hostname, host_port) = Self::extract_scheme_host_port(url, &settings)?;
let stream = TcpStream::connect(host_port.as_str()).await?;
Self::new_tcp_stream(stream, scheme, hostname, settings).await
}

async fn new_tcp_stream(
stream: TcpStream,
scheme: &str,
hostname: &str,
settings: LdapConnSettings,
) -> Result<(Self, Ldap)> {
let (mut conn, mut ldap) = Self::conn_pair(ConnType::Tcp(stream));
match scheme {
"ldap" => (),
Expand All @@ -512,7 +465,7 @@ impl LdapConnAsync {
}
let parts = conn.stream.into_parts();
let tls_stream = if let ConnType::Tcp(stream) = parts.io {
LdapConnAsync::create_tls_stream(settings, hostname, stream).await?
LdapConnAsync::create_tls_stream(settings, _hostname, stream).await?
} else {
panic!("underlying stream not TCP");
};
Expand Down
50 changes: 0 additions & 50 deletions src/sync.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::collections::HashSet;
use std::hash::Hash;
use std::net::TcpStream;
#[cfg(unix)]
use std::os::unix::net::UnixStream;
use std::time::Duration;

use crate::adapters::IntoAdapterVec;
Expand Down Expand Up @@ -72,53 +69,6 @@ impl LdapConn {
Ok(LdapConn { ldap, rt })
}

/// Create a connection to an LDAP server from existing UnixStream.
#[cfg(unix)]
pub fn from_unix_stream(stream: UnixStream) -> Result<Self> {
let stream = tokio::net::UnixStream::from_std(stream)?;
let rt = runtime::Builder::new_current_thread()
.enable_all()
.build()?;

let ldap = rt.block_on(async move {
let (conn, ldap) = LdapConnAsync::from_unix_stream(stream);
super::drive!(conn);
ldap
});
Ok(LdapConn { ldap, rt })
}

/// Create a connection to an LDAP server from existing UnixStream.
#[cfg(not(unix))]
pub fn from_unix_stream(stream: UnixStream) -> Result<Self> {
unimplemented!("no Unix domain sockets on non-Unix platforms");
}

/// Create a connection to an LDAP server from existing TcpStream,
/// specified by an already parsed `Url`, using
/// `settings` to specify additional parameters.
pub fn from_tcp_stream(
stream: TcpStream,
settings: LdapConnSettings,
url: &Url,
) -> Result<Self> {
let rt = runtime::Builder::new_current_thread()
.enable_all()
.build()?;

let ldap = rt.block_on(async move {
stream.set_nonblocking(true)?;
let stream = tokio::net::TcpStream::from_std(stream)?;
let (conn, ldap) = match LdapConnAsync::from_tcp_stream(stream, settings, url).await {
Ok((conn, ldap)) => (conn, ldap),
Err(e) => return Err(e),
};
super::drive!(conn);
Ok(ldap)
})?;
Ok(LdapConn { ldap, rt })
}

/// See [`Ldap::with_search_options()`](struct.Ldap.html#method.with_search_options).
pub fn with_search_options(&mut self, opts: SearchOptions) -> &mut Self {
self.ldap.search_opts = Some(opts);
Expand Down

0 comments on commit d3147ff

Please sign in to comment.