@@ -79,6 +79,9 @@ zend_class_entry* phongo_exception_from_phongo_domain(php_phongo_error_domain_t
7979 return spl_ce_RuntimeException ;
8080 case PHONGO_ERROR_WRITE_FAILED :
8181 return php_phongo_writeexception_ce ;
82+ case PHONGO_ERROR_CONNECTION_FAILED :
83+ /* FIXME: Add ConnectionException */
84+ return php_phongo_writeexception_ce ;
8285 }
8386
8487 mongoc_log (MONGOC_LOG_LEVEL_ERROR , MONGOC_LOG_DOMAIN , "Resolving unknown exception domain!!!" );
@@ -745,10 +748,13 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
745748{
746749 php_phongo_stream_socket * base_stream = NULL ;
747750 php_stream * stream = NULL ;
751+ const bson_t * options ;
752+ bson_iter_t iter ;
748753 char * errmsg = NULL ;
749754 int errcode ;
750755 char * dsn ;
751756 int dsn_len ;
757+ int enable_ssl = 0 ;
752758 (void )user_data ;TSRMLS_FETCH_FROM_CTX (user_data );
753759
754760
@@ -769,14 +775,46 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
769775 return NULL ;
770776 }
771777
772- stream = php_stream_xport_create (dsn , dsn_len , 0 , STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT , (char * )"persistent id" , /*options->connectTimeoutMS*/ 0 , (php_stream_context * )NULL , & errmsg , & errcode );
773- efree (dsn );
774-
775- if (!stream ) {
776- bson_set_error (error , MONGOC_ERROR_STREAM , MONGOC_ERROR_STREAM_CONNECT , "Failed connecting to '%s:%d': %s" , host -> host , host -> port , errmsg );
777- return NULL ;
778+ options = mongoc_uri_get_options (uri );
779+ if (bson_iter_init_find_case (& iter , options , "ssl" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
780+ enable_ssl = bson_iter_int32 (& iter );
778781 }
779782
783+ do {
784+ mongoc_log (MONGOC_LOG_LEVEL_DEBUG , MONGOC_LOG_DOMAIN , "Connecting to '%s'" , dsn );
785+ stream = php_stream_xport_create (dsn , dsn_len , 0 , STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT , (char * )"persistent id" , /*options->connectTimeoutMS*/ 0 , (php_stream_context * )NULL , & errmsg , & errcode );
786+ if (!stream ) {
787+ bson_set_error (error , MONGOC_ERROR_STREAM , MONGOC_ERROR_STREAM_CONNECT , "Failed connecting to '%s:%d': %s" , host -> host , host -> port , errmsg );
788+ }
789+ if (enable_ssl ) {
790+ zend_error_handling error_handling ;
791+ zend_replace_error_handling (EH_THROW , phongo_exception_from_mongoc_domain (MONGOC_ERROR_STREAM , MONGOC_ERROR_STREAM_SOCKET ), & error_handling TSRMLS_CC );
792+
793+ mongoc_log (MONGOC_LOG_LEVEL_DEBUG , MONGOC_LOG_DOMAIN , "Enabling SSL" );
794+ if (php_stream_xport_crypto_setup (stream , STREAM_CRYPTO_METHOD_SSLv23_CLIENT , NULL TSRMLS_CC ) < 0 ) {
795+ zend_restore_error_handling (& error_handling TSRMLS_CC );
796+ bson_set_error (error , MONGOC_ERROR_STREAM , MONGOC_ERROR_STREAM_INVALID_TYPE , "Failed to setup crypto, is the OpenSSL extension loaded?" );
797+ php_stream_free (stream , PHP_STREAM_FREE_CLOSE_PERSISTENT | PHP_STREAM_FREE_RSRC_DTOR );
798+ return NULL ;
799+ }
800+ zend_restore_error_handling (& error_handling TSRMLS_CC );
801+
802+ if (php_stream_xport_crypto_enable (stream , 1 TSRMLS_CC ) < 0 ) {
803+ php_stream_free (stream , PHP_STREAM_FREE_CLOSE_PERSISTENT | PHP_STREAM_FREE_RSRC_DTOR );
804+ if (enable_ssl == 2 ) {
805+ enable_ssl = 0 ;
806+ mongoc_log (MONGOC_LOG_LEVEL_WARNING , MONGOC_LOG_DOMAIN , "Server does not seem to support SSL" );
807+ continue ;
808+ }
809+
810+ bson_set_error (error , MONGOC_ERROR_STREAM , MONGOC_ERROR_STREAM_INVALID_TYPE , "Failed to setup crypto, is the server running with SSL?" );
811+ return NULL ;
812+ }
813+ }
814+ break ;
815+ } while (1 );
816+ efree (dsn );
817+
780818 /* Avoid invalid leak warning in debug mode when freeing the stream */
781819#if ZEND_DEBUG
782820 stream -> __exposed = 1 ;
0 commit comments