Commits

Michael Granger  committed e3f5708

More work on the starttle patch

  • Participants
  • Parent commits 0d45770

Comments (0)

Files changed (1)

 new file mode 100644
 --- /dev/null
 +++ b/examples/asynchronous_api.rb
-@@ -0,0 +1,53 @@
+@@ -0,0 +1,33 @@
 +#!/usr/bin/env ruby
 +
 +require 'openldap'
 +SERVER = 'ldap://ldap.laika.com/'
 +BASE   = 'dc=laika,dc=com'
 +
++query = nil
++
++# Create a new connection
++c = OpenLDAP::AsyncConnection.new( SERVER )
++
++# Set some options
++c.deref_aliases = true
++c.timeout       = 10
++c.sizelimit     = 200
++
++# Connect to the server and wrap the connection in a TLS channel
++op = c.start_tls
++sleep 0.2 until op.finished?
++
++# Start the search (asynchronously) and return the operation token
++op = c.search( BASE,
++	scope: :subtree,
++	filter: '(objectClass=inetOrgPerson)',
++	attributes: [ '*', '+' ],
++	deref_aliases: :always,
++	sizelimit: 10,
++	timeout: 10,
++	attrs_only: false,
++  )
++
++op.on_success {|entries|  }
+diff --git a/examples/nonblocking.rb b/examples/nonblocking.rb
+new file mode 100644
+--- /dev/null
++++ b/examples/nonblocking.rb
+@@ -0,0 +1,56 @@
++#!/usr/bin/env ruby
++
++require 'openldap'
++
++# A small example to demonstrate that even the synchronous calls are
++# non-blocking.
++
++SERVER = 'ldap://ldap.laika.com/'
++BASE   = 'dc=laika,dc=com'
++
 +t = Thread.new do
 +	Thread.current.abort_on_exception = true
 +
 +	c.start_tls
 +
 +	# Search (synchronously) in a Thread and return the results when done.
-+	c.search( BASE, 
-+		scope: :subtree, 
++	c.search( BASE,
++		scope: :subtree,
 +		filter: '(objectClass=inetOrgPerson)',
 +		attributes: [ '*', '+' ],
 +		deref_aliases: :always,
 +	  )
 +end
 +
-+# Set up another thread that will output stuff while the LDAP stuff runs to demonstrate that it
-+# doesn't block other threads (under 1.9, anyway).
++# Set up another thread that will output stuff while the LDAP stuff runs to
++# demonstrate that it doesn't block other threads (under 1.9, anyway).
 +t2 = Thread.new do
 +	Thread.current.abort_on_exception = true
 +	Thread.current[ :done ] = false
 new file mode 100644
 --- /dev/null
 +++ b/examples/synchronous_api.rb
-@@ -0,0 +1,53 @@
+@@ -0,0 +1,32 @@
 +#!/usr/bin/env ruby
 +
 +require 'openldap'
 +SERVER = 'ldap://ldap.laika.com/'
 +BASE   = 'dc=laika,dc=com'
 +
-+t = Thread.new do
-+	Thread.current.abort_on_exception = true
++# Create a new connection
++c = OpenLDAP::Connection.new( SERVER )
 +
-+	# Create a new connection
-+	c = OpenLDAP::Connection.new( SERVER )
++# Set some options
++c.deref_aliases = true
++c.timeout       = 10
++c.sizelimit     = 200
 +
-+	# Set some options
-+	c.deref_aliases = true
-+	c.timeout       = 10
-+	c.sizelimit     = 200
++# Connect to the server and wrap the connection in a TLS channel
++c.start_tls
 +
-+	# Connect to the server and wrap the connection in a TLS channel
-+	c.start_tls
++# Search (synchronously) and return the results when done.
++results = c.search_s( BASE,
++	scope: :subtree,
++	filter: '(objectClass=inetOrgPerson)',
++	attributes: [ '*', '+' ],
++	deref_aliases: :always,
++	sizelimit: 10,
++	timeout: 10,
++	attrs_only: false,
++  )
 +
-+	# Search (synchronously) in a Thread and return the results when done.
-+	c.search( BASE, 
-+		scope: :subtree, 
-+		filter: '(objectClass=inetOrgPerson)',
-+		attributes: [ '*', '+' ],
-+		deref_aliases: :always,
-+		sizelimit: 10,
-+		timeout: 10,
-+		attrs_only: false,
-+	  )
-+end
 +
-+# Set up another thread that will output stuff while the LDAP stuff runs to demonstrate that it
-+# doesn't block other threads (under 1.9, anyway).
-+t2 = Thread.new do
-+	Thread.current.abort_on_exception = true
-+	Thread.current[ :done ] = false
-+
-+	puts "Thread 2 waiting..."
-+	until Thread.current[ :done ]
-+		print "."
-+		sleep 1
-+	end
-+	puts "thread 2 done."
-+end
-+
-+
-+puts t1.join
-+t2[ :done ] = true
-+t2.join
-+
++pp results
 +
 diff --git a/ext/connection.c b/ext/connection.c
 --- a/ext/connection.c
 +++ b/ext/connection.c
-@@ -208,5 +208,6 @@
+@@ -130,7 +130,7 @@
+ 
+ /*
+  *  call-seq:
+- *     OpenLDAP::Connection.allocate   -> store
++ *     OpenLDAP::Connection.allocate   -> conn
+  *
+  *  Allocate a new OpenLDAP::Connection object.
+  *
+@@ -178,13 +178,40 @@
+ 
+ 	} else {
+ 		rb_raise( ropenldap_eOpenLDAPError,
+-				  "Cannot re-initialize a store once it's been created." );
++				  "Cannot re-initialize a connection once it's been created." );
+ 	}
+ 
+ 	return Qnil;
+ }
+ 
+ 
++/*
++ * call-seq:
++ *    conn.start_tls( server_controls=nil, client_controls=nil )
++ *
++ * Initiate TLS processing on the receiving connection.
++ *
++ * @param [Array<OpenLDAP::Control>] server_controls  One or more server controls to use.
++ * @param [Array<OpenLDAP::Control>] client_controls  One or more client controls to use.
++ * @raise [OpenLDAP::Error] if TLS was not successfully initiated.
++ */
++static VALUE
++ropenldap_conn_start_tls( VALUE self, int argc, VALUE *argv )
++{
++	struct ropenldap_connection *conn = ropenldap_get_conn( self );
++	int result;
++	VALUE serverctrls_ary = Qnil, clientctrls_ary = Qnil;
++	LDAPControl *s_ctrls = NULL, *c_ctrls = NULL;
++
++	switch ( rb_scan_args(argc, argv, "02", &serverctrls_ary, &clientctrls_ary) ) {
++		case 2:
++		
++	}
++
++	result = ldap_start_tls_s( conn->ldap );
++
++}
++
+ 
+ 
+ 
+@@ -208,5 +235,6 @@
  
  	rb_define_method( ropenldap_cOpenLDAPConnection, "initialize", ropenldap_conn_initialize, 1 );
  
-+	
++	rb_define_method( ropenldap_cOpenLDAPConnection, "start_tls", ropenldap_conn_start_tls, -1 );
  }
  
 diff --git a/ext/openldap.c b/ext/openldap.c
 --- a/ext/openldap.c
 +++ b/ext/openldap.c
+@@ -117,7 +117,7 @@
+ 	exception_class =
+ 		rb_funcall( ropenldap_eOpenLDAPError, rb_intern("subclass_for"), 1, INT2FIX(resultcode) );
+ 
+-	rb_raise( exception_class, func );
++	rb_raise( exception_class, "%s", func );
+ }
+ 
+ 
 @@ -271,6 +271,21 @@
  	/* versions */
  	rb_define_const( ropenldap_mOpenLDAP, "LDAP_API_VERSION", INT2FIX(LDAP_API_VERSION) );
  	/* search scopes */
  	rb_define_const( ropenldap_mOpenLDAP, "LDAP_SCOPE_BASE", INT2FIX(LDAP_SCOPE_BASE) );
  	rb_define_const( ropenldap_mOpenLDAP, "LDAP_SCOPE_BASEOBJECT", INT2FIX(LDAP_SCOPE_BASEOBJECT) );
+diff --git a/spec/openldap/connection_spec.rb b/spec/openldap/connection_spec.rb
+--- a/spec/openldap/connection_spec.rb
++++ b/spec/openldap/connection_spec.rb
+@@ -31,5 +31,17 @@
+ 	end
+ 
+ 
++	context "a connection to a local server" do
++
++		before( :each ) do
++			@conn = OpenLDAP::Connection.new( TEST_LDAP_URI )
++		end
++
++		it "can start a TLS session" do
++			@conn.start_tls
++		end
++
++	end
++
+ end
+