From 9476f820a9d2ec71c5e06cea257d164da2528b8f Mon Sep 17 00:00:00 2001 From: Qianqian Bu Date: Tue, 11 Feb 2020 17:42:45 +0800 Subject: [PATCH] adjust design, add preferrred and enforce redirect when on, and new test cases --- Notes.txt | 22 ++- README.md | 61 +++++++- mysqlnd_azure.c | 89 +++++++++--- mysqlnd_azure.h | 2 + package.xml | 43 ++++-- php_mysqlnd_azure.c | 53 ++++--- php_mysqlnd_azure.h | 14 +- tests/mysqli_azure_option_test.phpt | 133 ++++++++++++++++++ tests/mysqli_azure_redirection_disabled.phpt | 38 ----- tests/mysqli_azure_redirection_enabled.phpt | 38 ----- tests/mysqli_azure_redirection_off.phpt | 72 ++++++++++ tests/mysqli_azure_redirection_on.phpt | 68 +++++++++ tests/mysqli_azure_redirection_preferred.phpt | 72 ++++++++++ tests/server_basic_mysqli.phpt | 8 +- tests/server_basic_mysqli_testcase.php | 12 +- tests/server_basic_pdo.phpt | 8 +- tests/server_basic_pdo_testcase.php | 12 +- tests/skipif_pdo.inc | 2 +- tests/skipifconnectfailure.inc | 1 + 19 files changed, 597 insertions(+), 151 deletions(-) create mode 100644 tests/mysqli_azure_option_test.phpt delete mode 100644 tests/mysqli_azure_redirection_disabled.phpt delete mode 100644 tests/mysqli_azure_redirection_enabled.phpt create mode 100644 tests/mysqli_azure_redirection_off.phpt create mode 100644 tests/mysqli_azure_redirection_on.phpt create mode 100644 tests/mysqli_azure_redirection_preferred.phpt diff --git a/Notes.txt b/Notes.txt index a8e0f7f..4520daf 100644 --- a/Notes.txt +++ b/Notes.txt @@ -9,7 +9,23 @@ connection, and use the new one afterward. +---------------------------+ OPTION DESCRIPTION ------------------ ---------------------------------------------------------- -mysqlnd_azure.enabled This option is to control enable or disable mysqlnd_rd. -If this is set to 0, it will not use redirection. -(Default: 0) +mysqlnd_azure.enableRedirect This option is to control enable or disable redirection feature of mysqlnd_azure. +If this is set to off, it will not use redirection. +Available option values: +(Default: off) +---------------|------------------------------------------------------------------------------------------------------------------------------------------ +off(0) | - It will not use redirection. +---------------|------------------------------------------------------------------------------------------------------------------------------------------ +on(1) | - If ssl is off, no connection will be made, return error: + | "mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL." + | - If on server side redirection is not available, abort the first connection and return error: + | "Connection aborted because redirection is not enabled on the MySQL server or the network package doesn't meet meet redirection protocol." + | - If server supports redirection, but the redirected connection failed for any reason, also abort the first proxy connection. + | Return the error of the redirected connection. +---------------|------------------------------------------------------------------------------------------------------------------------------------------ +preferred(2) | - it will use redirection if possible. + | - If connection does not use SSL, or server does not support redirection, or redirected connection fails + | to connect for any non-fatal reason while the proxy connection is still a valid one, it will fallback + | to the first proxy connection. +---------------|------------------------------------------------------------------------------------------------------------------------------------------ diff --git a/README.md b/README.md index b00615d..6caac8b 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,52 @@ The source code here is a PHP extension implemented using mysqlnd plugin API (ht **Important notice: There is a limitation that for Azure MySQL, redirection is only possible when the connection is configured with SSL, and it will only support TLS 1.2 with FIPS approved cipher for redirection.** +## Option Usage +In 1.0.x versions, the option is with name **mysqlnd_azure.enabled**. When redirection is turned on, but connection does not use SSL, +or server does not support redirection, or redirected connection fails to connect for any non-fatal reason while the proxy connection +is still a valid one, it will fallback to the first proxy connection. The detailed usage of the option enableRedirect is as follows: +(Version 1.0.x. Config name: **mysqlnd_azure.enabled**. Valid value: on/off. Default value: off) + + + + + + + + + +
off(0) - It will not use redirection.
on(1) - It will use redirection if possible (Connection is with SSL and Server supports/need redirection).
+ - If connection does not use SSL, or server does not support redirection, or redirected connection fails to connect for any non-fatal reason while the proxy connection is still a valid one, it will fallback to the first proxy connection. +
+ +Since 1.1.0RC1, the logic changes as follows: +- The option mysqlnd_azure.enabled is renamed to **mysqlnd_azure.enableRedirect**, and there is a new option value "preferred" provided. +- The detailed usage of the option enableRedirect is as follows: + +(Version 1.1.0RC1. Config name: **mysqlnd_azure.enableRedirect**. Valid value: on/off/preferred. Default value: off) + + + + + + + + + + + + + +
off(0) - It will not use redirection.
on(1) - If SSL is off, no connection will be made, return error: + "mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL."
+ - If on server side redirection is not supported, abort the first connection and return error: "Connection aborted because redirection is not enabled on the MySQL server or the network package doesn't meet meet redirection protocol."
+ - If server supports redirection, but the redirected connection failed for any reason, also abort the first proxy connection. Return the error of the redirected connection. +
+preferred(2) + - It will use redirection if possible.
+ - If connection does not use SSL, or server does not support redirection, or redirected connection fails to connect for any non-fatal reason while the proxy connection is still a valid one, it will fallback to the first proxy connection. +
+ ## Name and Extension Version Extension name: **mysqlnd_azure** @@ -15,6 +61,12 @@ Valid version: - 1.0.3RC Change: fix the crash problem when working with PDO interface with flag PDO::ATTR_PERSISTENT=>false. This version is marked as beta, so please use *pecl install mysqlnd_azure-1.0.3RC* to install when using pecl. - 1.0.3 Change: Remove the use of is_using_redirect flag. More strict validation and new test cases with php built-in web server. +- 1.1.0RC1 Change: + 1. Rename option mysqlnd_azure.enabled to mysqlnd_azure.enableRedirect, and add a new option value "preferred". + 2. When enableRedirect is "preferred", it will use redirection if possible. If connection does not use SSL, or server does not support redirection, or redirected connection fails to connect for any non-fatal reason while the proxy connection is still a valid one, it will fallback to the first proxy connection. + 3. When enableRedirect is "on", SSL is off, no connection will be made, return error "mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL." + 4. When enableRedirect is "on", but on server side redirection is not supported, abort the first connection and return error "Connection aborted because redirection is not enabled on the MySQL server or the network package doesn't meet meet redirection protocol." + 5. When enableRedirect is "on" and server supports redirection, but the redirected connection failed for any reason, also abort the first proxy connection. Return the error of the redirected connection. Following is a brief guide of how to install using pecl or build and test the extension from source. @@ -93,7 +145,8 @@ Then you can run **make install** to put the .so to your php so library. However - put mysqlnd_azure.so under extension_dir. - under directory for additional .ini files, you will find the ini files for the common used modules, e.g. 10-mysqlnd.ini for mysqlnd, 20-mysqli.ini for mysqli. Create a new ini file for mysqlnd_azure here. **Make sure the alphabet order of the name is after that of mysqnld**, since the modules are loaded according to the name order of the ini files. E.g. if mysqlnd ini is with name 10-mysqlnd.ini,then name the ini as 20-mysqlnd-azure.ini. In the ini file, add the following two lines: - extension=mysqlnd_azure - - mysqlnd_azure.enabled = on ; you can also set this to off to disable redirection + - mysqlnd_azure.enableRedirect = on/off/preferred + - **Notice:** since 1.1.0RC1, if this value is set to on, the connection must be configured with SSL, and it requires server support redirection. Otherwise, the connection will fail. Please check the Option Usage section for detailed information. ## Step to build on Windows @@ -139,15 +192,17 @@ After this, the code directory should look like C:\php-sdk\phpdev\vc15\x64\php-s - extension=mysqlnd_azure - Under the Module Settings section add: - [mysqlnd_azure] - - mysqlnd_azure.enabled = on + - mysqlnd_azure.enableRedirect = on/off/preferred + - **Notice:** since 1.1.0RC1, if this value is set to on, the connection must be configured with SSL, and it requires server support redirection. Otherwise, the connection will fail. Please check the Option Usage section for detailed information. ## Test * Currently redirection is only possible when the connection is via ssl, and it need that the redirection feature switch is enabled on server side. Following is a snippet to test connection with redirection: ```php - echo "mysqlnd_azure.enabled: ", ini_get("mysqlnd_azure.enabled") == true?"On":"Off", "\n"; + echo "mysqlnd_azure.enableRedirect: ", ini_get("mysqlnd_azure.enableRedirect"), "\n"; $db = mysqli_init(); + //The connection must be configured with SSL for redirection test $link = mysqli_real_connect ($db, 'your-hostname-with-redirection-enabled', 'user@host', 'password', "db", 3306, NULL, MYSQLI_CLIENT_SSL); if (!$link) { die ('Connect error (' . mysqli_connect_errno() . '): ' . mysqli_connect_error() . "\n"); diff --git a/mysqlnd_azure.c b/mysqlnd_azure.c index 1c6aecd..6c39690 100644 --- a/mysqlnd_azure.c +++ b/mysqlnd_azure.c @@ -340,9 +340,16 @@ MYSQLND_METHOD(mysqlnd_azure_data, connect)(MYSQLND_CONN_DATA ** pconn, unsigned int ui_redirect_port = 0; zend_bool serverSupportRedirect = get_redirect_info(conn, redirect_host, redirect_user, &ui_redirect_port); if (!serverSupportRedirect) { - //do nothing else for redirection, just use the previous connection - DBG_ENTER("[redirect]: Server doesnot supoort redirection, do not need redirection. "); - goto after_conn; + DBG_ENTER("[redirect]: Server doesnot supoort redirection."); + if(MYSQLND_AZURE_G(enableRedirect) == REDIRECT_ON) { + //REDIRECT_ON, if redirection is not supported, abort the original connection and return error + conn->m->send_close(conn); + SET_CLIENT_ERROR(conn->error_info, MYSQLND_AZURE_ENFORCE_REDIRECT_ERROR_NO, UNKNOWN_SQLSTATE, "Connection aborted because redirection is not enabled on the MySQL server or the network package doesn't meet meet redirection protocol."); + goto err; + } else { + //REDIRECT_PREFERRED, do nothing else for redirection, just use the previous connection + goto after_conn; + } } //Get here means serverSupportRedirect @@ -359,9 +366,18 @@ MYSQLND_METHOD(mysqlnd_azure_data, connect)(MYSQLND_CONN_DATA ** pconn, enum_func_status ret = FAIL; MYSQLND* redirect_conneHandle = mysqlnd_init(MYSQLND_CLIENT_KNOWS_RSET_COPY_DATA, conn->persistent); //init MYSQLND but only need only MYSQLND_CONN_DATA here if(!redirect_conneHandle) { - //redirect_conneHandle init failed, do nothing more, just use the original connection - goto after_conn; + DBG_ENTER("[redirect]: init redirect_conneHandle failed"); + if(MYSQLND_AZURE_G(enableRedirect) == REDIRECT_ON) { + //REDIRECT_ON, abort the original connection and return error + conn->m->send_close(conn); + SET_CLIENT_ERROR(conn->error_info, MYSQLND_AZURE_ENFORCE_REDIRECT_ERROR_NO, UNKNOWN_SQLSTATE, "Connection aborted because init redirection failed."); + goto err; + } else { + //REDIRECT_PREFERRED, do nothing else for redirection, just use the previous connection + goto after_conn; + } } + MYSQLND_CONN_DATA* redirect_conn = redirect_conneHandle->data; redirect_conneHandle->data = NULL; mnd_pefree(redirect_conneHandle, redirect_conneHandle->persistent); @@ -371,13 +387,22 @@ MYSQLND_METHOD(mysqlnd_azure_data, connect)(MYSQLND_CONN_DATA ** pconn, //init redirect_conn options failed, just use the proxy connection if (ret == FAIL) { - //release resource - redirect_conn->m->dtor(redirect_conn); - goto after_conn; + DBG_ENTER("[redirect]: init redirection option failed. "); + redirect_conn->m->dtor(redirect_conn); //release created resource + + if(MYSQLND_AZURE_G(enableRedirect) == REDIRECT_ON) { + //REDIRECT_ON, abort the original connection + conn->m->send_close(conn); + SET_CLIENT_ERROR(conn->error_info, MYSQLND_AZURE_ENFORCE_REDIRECT_ERROR_NO, UNKNOWN_SQLSTATE, "Connection aborted because init redirection failed."); + goto err; + } else { + //REDIRECT_PREFERRED, do nothing else for redirection, just use the previous connection + goto after_conn; + } } //init redirect_conn succeeded, use this conn to start a new connection and handshake - + const MYSQLND_CSTRING redirect_hostname = { redirect_host, strlen(redirect_host) }; const MYSQLND_CSTRING redirect_username = { redirect_user, strlen(redirect_user) }; MYSQLND_STRING redirect_transport = redirect_conn->m->get_scheme(redirect_conn, redirect_hostname, &socket_or_pipe, ui_redirect_port, &unix_socket, &named_pipe); @@ -387,6 +412,7 @@ MYSQLND_METHOD(mysqlnd_azure_data, connect)(MYSQLND_CONN_DATA ** pconn, enum_func_status redirectState = redirect_conn->m->connect_handshake(redirect_conn, &redirect_scheme, &redirect_username, &password, &database, mysql_flags); if (redirectState == PASS) { //handshake with redirect_conn succeeded, replace original connection info with redirect_conn and add the redirect info into cache table + DBG_ENTER("[redirect]: mysql redirect handshake succeeded."); //add the redirect info into cache table mysqlnd_azure_add_redirect_cache(username.s, hostname.s, port, redirect_username.s, redirect_hostname.s, ui_redirect_port); @@ -410,19 +436,32 @@ MYSQLND_METHOD(mysqlnd_azure_data, connect)(MYSQLND_CONN_DATA ** pconn, port = ui_redirect_port; transport = redirect_transport; - DBG_ENTER("[redirect]: mysql redirect handshake succeeded."); + } else { //redirect failed. if REDIRECT_ON, also abort the original conn, if REDIRECT_PREFERRED, use original connection + DBG_ENTER("[redirect]: mysql redirect handshake fails"); - } - else { //redirect failed. use original connection information + if (MYSQLND_AZURE_G(enableRedirect) == REDIRECT_PREFERRED) { + //free resource and use original connection + redirect_conn->m->dtor(redirect_conn); + if (redirect_transport.s) { + mnd_sprintf_free(redirect_transport.s); + redirect_transport.s = NULL; + } + goto after_conn; - //free resource, and use original connection information - redirect_conn->m->dtor(redirect_conn); - if (redirect_transport.s) { - mnd_sprintf_free(redirect_transport.s); - redirect_transport.s = NULL; + } else { //REDIRECT_ON, free original connect, and use redirect_conn to handle error + conn->m->send_close(conn); + conn->m->dtor(conn); + pfc = NULL; + if (transport.s) { + mnd_sprintf_free(transport.s); + transport.s = NULL; + } + + conn = redirect_conn; + *pconn = redirect_conn; + pfc = redirect_conn->protocol_frame_codec; + goto err; } - DBG_ENTER("[redirect]: mysql redirect handshake fails, use original connection information!"); - } } @@ -582,7 +621,7 @@ MYSQLND_METHOD(mysqlnd_azure, connect)(MYSQLND * conn_handle, mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_server_host", hostname.s); } - if (!MYSQLND_AZURE_G(enabled)) { + if (MYSQLND_AZURE_G(enableRedirect) == REDIRECT_OFF) { DBG_ENTER("mysqlnd_azure::connect redirect disabled"); ret = org_conn_d_m.connect(*pconn, hostname, username, password, database, port, socket_or_pipe, mysql_flags); } @@ -592,7 +631,15 @@ MYSQLND_METHOD(mysqlnd_azure, connect)(MYSQLND * conn_handle, //Redirection is only possible with SSL at present. Continue with no redirection if SSL is not set unsigned int temp_flags = (*pconn)->m->get_updated_connect_flags(*pconn, mysql_flags); if (!(temp_flags & CLIENT_SSL)) { - ret = org_conn_d_m.connect(*pconn, hostname, username, password, database, port, socket_or_pipe, mysql_flags); + if((MYSQLND_AZURE_G(enableRedirect) == REDIRECT_ON)) { + SET_CLIENT_ERROR((*pconn)->error_info, MYSQLND_AZURE_ENFORCE_REDIRECT_ERROR_NO, UNKNOWN_SQLSTATE, "mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL."); + (*pconn)->m->local_tx_end(*pconn, this_func, FAIL); + (*pconn)->m->free_contents(*pconn); + return FAIL; + } + else { + ret = org_conn_d_m.connect(*pconn, hostname, username, password, database, port, socket_or_pipe, mysql_flags); + } } else { //SSL is enabled diff --git a/mysqlnd_azure.h b/mysqlnd_azure.h index 511ba65..74dc082 100644 --- a/mysqlnd_azure.h +++ b/mysqlnd_azure.h @@ -36,6 +36,8 @@ typedef struct st_mysqlnd_azure_redirect_info { #define MAX_REDIRECT_HOST_LEN 128 #define MAX_REDIRECT_USER_LEN 128 +#define MYSQLND_AZURE_ENFORCE_REDIRECT_ERROR_NO CR_NOT_IMPLEMENTED + void mysqlnd_azure_minit_register_hooks(); enum_func_status mysqlnd_azure_add_redirect_cache(const char* user, const char* host, int port, const char* redirect_user, const char* redirect_host, int redirect_port); diff --git a/package.xml b/package.xml index 0cf4943..ec5b674 100644 --- a/package.xml +++ b/package.xml @@ -16,19 +16,24 @@ Qianqian.Bu@microsoft.com yes - 2020-02-10 + 2020-02-15 - 1.0.3 - 1.0.3 + 1.1.0RC1 + 1.1.0RC1 - stable - stable + beta + beta PHP License -- Remove the use of is_using_redirect flag. More strict validation and test cases with php built-in web server. +- 1. Rename option mysqlnd_azure.enabled to mysqlnd_azure.enableRedirect. +- 2. When enableRedirect is "on", ssl is off, no connection will be made, return error "mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL." +- 3. When enableRedirect is "on", but on server side redirection is not available, abort the first connection and return error "Connection aborted because redirection is not enabled on the MySQL server or the network package doesn't meet meet redirection protocol." +- 4. When enableRedirect is "on" and server supports redirection, but the redirected connection failed for any reason, also abort the first proxy connection. Return the error of the redirected connection. +- 5. A new option for mysqlnd_azure.enableRedirect is introduced with name "preferred". When enableRedirect is "preferred", it will use redirection if possible. + If connection does not use SSL, or server does not support redirection, or redirected connection fails to connect for any non-fatal reason while the proxy connection is still a valid one, it will fallback to the first proxy connection. @@ -39,8 +44,10 @@ - - + + + + @@ -72,6 +79,26 @@ mysqlnd_azure + + + 1.1.0RC1 + 1.1.0RC1 + + + beta + beta + + 2020-02-15 + PHP License + +- 1. Rename option mysqlnd_azure.enabled to mysqlnd_azure.enableRedirect, and add a new option value "preferred". +- 2. When enableRedirect is "on", ssl is off, no connection will be made, return error "mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL." +- 3. When enableRedirect is "on", but on server side redirection is not available, abort the first connection and return error "Connection aborted because redirection is not enabled on the MySQL server or the network package doesn't meet meet redirection protocol." +- 4. When enableRedirect is "on" and server supports redirection, but the redirected connection failed for any reason, also abort the first proxy connection. Return the error of the redirected connection. +- 5. A new option for mysqlnd_azure.enableRedirect is introduced with name "preferred". When enableRedirect is "preferred", it will use redirection if possible. + If connection does not use SSL, or server does not support redirection, or redirected connection fails to connect for any non-fatal reason while the proxy connection is still a valid one, it will fallback to the first proxy connection. + + 1.0.3 diff --git a/php_mysqlnd_azure.c b/php_mysqlnd_azure.c index 5c45c4e..7df8a1c 100644 --- a/php_mysqlnd_azure.c +++ b/php_mysqlnd_azure.c @@ -28,13 +28,45 @@ ZEND_DECLARE_MODULE_GLOBALS(mysqlnd_azure) +/* {{{ OnUpdateEnableRedirect */ +static ZEND_INI_MH(OnUpdateEnableRedirect) +{ + if (STRING_EQUALS(new_value,"preferred") + || STRING_EQUALS(new_value, "2")) { + + MYSQLND_AZURE_G(enableRedirect) = REDIRECT_PREFERRED; + + } else if (STRING_EQUALS(new_value, "on") + || STRING_EQUALS(new_value, "yes") + || STRING_EQUALS(new_value, "true") + || STRING_EQUALS(new_value, "1")) { + + MYSQLND_AZURE_G(enableRedirect) = REDIRECT_ON; + + } else { + + MYSQLND_AZURE_G(enableRedirect) = REDIRECT_OFF; + + } + + return SUCCESS; + +} +/* }}} */ + +/* {{{ PHP_INI */ +PHP_INI_BEGIN() +STD_PHP_INI_ENTRY("mysqlnd_azure.enableRedirect", "preferred", PHP_INI_ALL, OnUpdateEnableRedirect, enableRedirect, zend_mysqlnd_azure_globals, mysqlnd_azure_globals) +PHP_INI_END() +/* }}} */ + /* {{{ PHP_GINIT_FUNCTION */ static PHP_GINIT_FUNCTION(mysqlnd_azure) { #if defined(COMPILE_DL_MYSQLND_AZURE) && defined(ZTS) ZEND_TSRMLS_CACHE_UPDATE(); #endif - mysqlnd_azure_globals->enabled = 0; + mysqlnd_azure_globals->enableRedirect = REDIRECT_PREFERRED; mysqlnd_azure_globals->redirectCache = NULL; } /* }}} */ @@ -50,17 +82,6 @@ static PHP_GSHUTDOWN_FUNCTION(mysqlnd_azure) } /* }}} */ -/* {{{ PHP_INI */ -/* - It is handy to allow users to disable any mysqlnd plugin globally - not only for debugging :-) - Because we register our plugin in MINIT changes to mysqlnd_ed.enabled shall be bound to - INI_SYSTEM (and PHP restarts). -*/ -PHP_INI_BEGIN() -STD_PHP_INI_ENTRY("mysqlnd_azure.enabled", "0", PHP_INI_ALL, OnUpdateBool, enabled, zend_mysqlnd_azure_globals, mysqlnd_azure_globals) -PHP_INI_END() -/* }}} */ - /* {{{ PHP_MINIT_FUNCTION */ static PHP_MINIT_FUNCTION(mysqlnd_azure) @@ -87,8 +108,8 @@ static PHP_MSHUTDOWN_FUNCTION(mysqlnd_azure) PHP_MINFO_FUNCTION(mysqlnd_azure) { php_info_print_table_start(); - php_info_print_table_header(2, "mysqlnd_azure", "enabled"); - php_info_print_table_row(2, "enabled", MYSQLND_AZURE_G(enabled)? "Yes":"No"); + php_info_print_table_header(2, "mysqlnd_azure", "enableRedirect"); + php_info_print_table_row(2, "enableRedirect", MYSQLND_AZURE_G(enableRedirect) == REDIRECT_OFF ? "off" : (MYSQLND_AZURE_G(enableRedirect) == REDIRECT_ON ? "on" : "preferred")); php_info_print_table_end(); } /* }}} */ @@ -103,14 +124,14 @@ zend_module_entry mysqlnd_azure_module_entry = { STANDARD_MODULE_HEADER_EX, NULL, mysqlnd_azure_deps, - EXT_MYSQLND_AZURE_NAME, + PHP_MYSQLND_AZURE_NAME, NULL, PHP_MINIT(mysqlnd_azure), PHP_MSHUTDOWN(mysqlnd_azure), NULL, NULL, PHP_MINFO(mysqlnd_azure), - EXT_MYSQLND_AZURE_VERSION, + PHP_MYSQLND_AZURE_VERSION, PHP_MODULE_GLOBALS(mysqlnd_azure), PHP_GINIT(mysqlnd_azure), PHP_GSHUTDOWN(mysqlnd_azure), diff --git a/php_mysqlnd_azure.h b/php_mysqlnd_azure.h index c8dcbd5..497dd90 100644 --- a/php_mysqlnd_azure.h +++ b/php_mysqlnd_azure.h @@ -28,14 +28,22 @@ extern zend_module_entry mysqlnd_azure_module_entry; #define phpext_mysqlnd_azure_ptr &mysqlnd_azure_module_entry -#define EXT_MYSQLND_AZURE_NAME "mysqlnd_azure" -#define EXT_MYSQLND_AZURE_VERSION "1.0.3" +#define PHP_MYSQLND_AZURE_NAME "mysqlnd_azure" +#define PHP_MYSQLND_AZURE_VERSION "1.1.0RC1" + +#define STRING_EQUALS(z_str,str) (ZSTR_LEN((z_str)) == strlen((str)) && strcasecmp((str), ZSTR_VAL((z_str))) == 0) ZEND_BEGIN_MODULE_GLOBALS(mysqlnd_azure) - zend_bool enabled; + zend_bool enableRedirect; HashTable* redirectCache; ZEND_END_MODULE_GLOBALS(mysqlnd_azure) +typedef enum _mysqlnd_azure_redirect_mode { + REDIRECT_OFF = 0, /* completely disabled */ + REDIRECT_ON = 1, /* enabled without fallback, block if redirection fail */ + REDIRECT_PREFERRED = 2 /* enabled with fallback */ +} mysqlnd_azure_redirect_mode; + PHPAPI ZEND_EXTERN_MODULE_GLOBALS(mysqlnd_azure) #define MYSQLND_AZURE_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(mysqlnd_azure, v) diff --git a/tests/mysqli_azure_option_test.phpt b/tests/mysqli_azure_option_test.phpt new file mode 100644 index 0000000..d31f9d6 --- /dev/null +++ b/tests/mysqli_azure_option_test.phpt @@ -0,0 +1,133 @@ +--TEST-- +Azure redirection option test for mysqlnd_azure.enableRedirect +--SKIPIF-- + +--FILE-- +host_info, 0, strlen($host)) == $host) + echo "equal\n"; + else + echo "not equal\n"; + $link->close(); + } + + $link = mysqli_init(); + $ret = @mysqli_real_connect($link, $host, $user, $passwd, $db, $port, NULL, MYSQLI_CLIENT_SSL); + $last_message = ""; + if($ret) + $last_message = $link->info; + + //Server supports redirection + if(strlen($last_message) > 27 && strcmp(substr($last_message, 0, strlen("Location:")), "Location:")==0) { + $config = ini_get("mysqlnd_azure.enableRedirect"); + if(substr($link->host_info, 0, strlen($host)) == $host && (strcasecmp($config, "preferred")==0 || strcasecmp($config, "2")==0)) + echo "FAIL\n"; + else + echo "PASS\n"; + } + else if($ret && is_object($link)) { + if(substr($link->host_info, 0, strlen($host)) == $host) + echo "PASS\n"; + else + echo "FAIL\n"; + } + else if(!$ret) { //Server does not support redirection and redirect on + $lastError = error_get_last()["message"]; + if (strpos($lastError, "Connection aborted because redirection is not enabled on the MySQL server or the network package doesn't meet meet redirection protocol.") !== false) + echo "PASS\n"; + else + echo "FAIL\n"; + } + if($ret) $link->close(); +} + +ini_set_test("on"); +ini_set_test("On"); +ini_set_test("yes"); +ini_set_test("Yes"); +ini_set_test("true"); +ini_set_test("True"); +ini_set_test("1"); +ini_set_test(1); + +ini_set_test("preferred"); +ini_set_test("Preferred"); +ini_set_test("2"); +ini_set_test(2); + +ini_set_test("off"); +ini_set_test("Off"); +ini_set_test("0"); +ini_set_test(0); +ini_set_test("otherValue"); + +echo "Done\n"; +?> +--EXPECTF-- +on +mysqli_real_connect(): (HY000/2054): mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL. +PASS +On +mysqli_real_connect(): (HY000/2054): mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL. +PASS +yes +mysqli_real_connect(): (HY000/2054): mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL. +PASS +Yes +mysqli_real_connect(): (HY000/2054): mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL. +PASS +true +mysqli_real_connect(): (HY000/2054): mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL. +PASS +True +mysqli_real_connect(): (HY000/2054): mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL. +PASS +1 +mysqli_real_connect(): (HY000/2054): mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL. +PASS +1 +mysqli_real_connect(): (HY000/2054): mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL. +PASS +preferred +equal +PASS +Preferred +equal +PASS +2 +equal +PASS +2 +equal +PASS +off +equal +PASS +Off +equal +PASS +0 +equal +PASS +0 +equal +PASS +otherValue +equal +PASS +Done \ No newline at end of file diff --git a/tests/mysqli_azure_redirection_disabled.phpt b/tests/mysqli_azure_redirection_disabled.phpt deleted file mode 100644 index c5c2393..0000000 --- a/tests/mysqli_azure_redirection_disabled.phpt +++ /dev/null @@ -1,38 +0,0 @@ ---TEST-- -Azure redirection test for servers with mysqlnd_azure.enabled=0 ---INI-- -mysqlnd_azure.enabled=0 ---SKIPIF-- - ---FILE-- -host_info."\n"; -echo $link->info."\n"; -if(substr($link->host_info, 0, strlen($host)) == $host) - echo "1\n"; -else - echo "0\n"; -mysqli_close($link); -echo "Done\n"; -?> ---EXPECTF-- -%s -Location: mysql://%s.%s:%d/user=%s@%s -1 -Done diff --git a/tests/mysqli_azure_redirection_enabled.phpt b/tests/mysqli_azure_redirection_enabled.phpt deleted file mode 100644 index 400c629..0000000 --- a/tests/mysqli_azure_redirection_enabled.phpt +++ /dev/null @@ -1,38 +0,0 @@ ---TEST-- -Azure redirection test for servers when mysqlnd_azure.enabled ---INI-- -mysqlnd_azure.enabled=1 ---SKIPIF-- - ---FILE-- -host_info."\n"; -echo $link->info."\n"; -if(substr($link->host_info, 0, strlen($host)) == $host) - echo "1\n"; -else - echo "0\n"; -mysqli_close($link); -echo "Done\n"; -?> ---EXPECTF-- -%s.%s -Location: mysql://%s.%s:%d/user=%s@%s -0 -Done diff --git a/tests/mysqli_azure_redirection_off.phpt b/tests/mysqli_azure_redirection_off.phpt new file mode 100644 index 0000000..602408f --- /dev/null +++ b/tests/mysqli_azure_redirection_off.phpt @@ -0,0 +1,72 @@ +--TEST-- +Azure redirection test for servers when mysqlnd_azure.enableRedirect="off" +--INI-- +mysqlnd_azure.enableRedirect="off" +--SKIPIF-- + +--FILE-- +host_info."\n"; +$last_message = $link->info; + +//Server supports redirection +if(strlen($last_message) > 27 && strcmp(substr($last_message, 0, strlen("Location:")), "Location:")==0) { + if(substr($link->host_info, 0, strlen($host)) == $host) + echo "[003] pass\n"; + else + echo "[003] fail\n"; +} +else { //Server does not support redirection, use the proxy connection + if(substr($link->host_info, 0, strlen($host)) == $host) + echo "[003] pass\n"; + else + echo "[003] fail\n"; +} + +mysqli_close($link); + +//Step 3: check connection result when not use SSL +$link = mysqli_init(); +$ret = @mysqli_real_connect($link, $host, $user, $passwd, $db, $port, NULL, NULL); +if (!$ret || !is_object($link)) +{ + printf("[004] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n - [%d] %s without ssl\n", + $host, $user, $db, $port, $socket, mysqli_connect_errno(), mysqli_connect_error()); + die(); +} + +if(substr($link->host_info, 0, strlen($host)) == $host) + echo "[004] pass\n"; +else + echo "[004] fail\n"; + +echo "Done\n"; +?> +--EXPECTF-- +off +%s +[003] pass +[004] pass +Done \ No newline at end of file diff --git a/tests/mysqli_azure_redirection_on.phpt b/tests/mysqli_azure_redirection_on.phpt new file mode 100644 index 0000000..5678f0b --- /dev/null +++ b/tests/mysqli_azure_redirection_on.phpt @@ -0,0 +1,68 @@ +--TEST-- +Azure redirection test for servers when mysqlnd_azure.enableRedirect="on" +--INI-- +mysqlnd_azure.enableRedirect="on" +--SKIPIF-- + +--FILE-- +info; + +//Server supports redirection +if(strlen($last_message) > 27 && strcmp(substr($last_message, 0, strlen("Location:")), "Location:")==0) { + if(substr($link->host_info, 0, strlen($host)) == $host) + echo "[003] fail\n"; + else + echo "[003] pass\n"; +} +else { //Server does not support redirection + $lastError = error_get_last()["message"]; + if (strpos($lastError, "Connection aborted because redirection is not enabled on the MySQL server or the network package doesn't meet meet redirection protocol.") !== false) + echo "[003] pass\n"; + else + echo "[003] fail\n"; +} + +mysqli_close($link); + +//Step 3: check connection result when not use SSL +$link = mysqli_init(); +$ret = @mysqli_real_connect($link, $host, $user, $passwd, $db, $port, NULL, NULL); +if ($ret) +{ + printf("[004] When enableRedirect=on, connect without SSL expects failure, got pass\n"); + die(); +} + +$lastError = error_get_last()["message"]; +if (strpos($lastError, "mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL.") !== false) + echo "[004] pass\n"; + else + echo "[004] fail\n"; + +echo "Done\n"; +?> +--EXPECTF-- +on +[003] pass +[004] pass +Done \ No newline at end of file diff --git a/tests/mysqli_azure_redirection_preferred.phpt b/tests/mysqli_azure_redirection_preferred.phpt new file mode 100644 index 0000000..0a358d8 --- /dev/null +++ b/tests/mysqli_azure_redirection_preferred.phpt @@ -0,0 +1,72 @@ +--TEST-- +Azure redirection test for servers when mysqlnd_azure.enableRedirect="preferred" +--INI-- +mysqlnd_azure.enableRedirect="preferred" +--SKIPIF-- + +--FILE-- +host_info."\n"; +$last_message = $link->info; + +//Server supports redirection +if(strlen($last_message) > 27 && strcmp(substr($last_message, 0, strlen("Location:")), "Location:")==0) { + if(substr($link->host_info, 0, strlen($host)) == $host) + echo "[003] fail\n"; + else + echo "[003] pass\n"; +} +else { //Server does not support redirection, use the proxy connection + if(substr($link->host_info, 0, strlen($host)) == $host) + echo "[003] pass\n"; + else + echo "[003] fail\n"; +} + +mysqli_close($link); + +//Step 3: check connection result when not use SSL +$link = mysqli_init(); +$ret = @mysqli_real_connect($link, $host, $user, $passwd, $db, $port, NULL, NULL); +if (!$ret || !is_object($link)) +{ + printf("[004] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n - [%d] %s without ssl\n", + $host, $user, $db, $port, $socket, mysqli_connect_errno(), mysqli_connect_error()); + die(); +} + +if(substr($link->host_info, 0, strlen($host)) == $host) + echo "[004] pass\n"; +else + echo "[004] fail\n"; + +echo "Done\n"; +?> +--EXPECTF-- +preferred +%s +[003] pass +[004] pass +Done \ No newline at end of file diff --git a/tests/server_basic_mysqli.phpt b/tests/server_basic_mysqli.phpt index 1e3c26e..37a7757 100644 --- a/tests/server_basic_mysqli.phpt +++ b/tests/server_basic_mysqli.phpt @@ -33,22 +33,22 @@ fclose($fp); --EXPECTF-- *** Testing mysqli in web server: basic functionality *** step1: redirect enabled, non-persistent connection -mysqlnd_azure.enabled: On +mysqlnd_azure.enableRedirect: preferred %s Location: mysql://%s:%d/user=%s 0 step2: redirect enabled, persistent connection -mysqlnd_azure.enabled: On +mysqlnd_azure.enableRedirect: preferred %s Location: mysql://%s:%d/user=%s 0 step3: redirect disabled, non-persistent connection -mysqlnd_azure.enabled: Off +mysqlnd_azure.enableRedirect: off %s Location: mysql://%s:%d/user=%s 1 step4: redirect disabled, persistent connection -mysqlnd_azure.enabled: Off +mysqlnd_azure.enableRedirect: off %s 0 diff --git a/tests/server_basic_mysqli_testcase.php b/tests/server_basic_mysqli_testcase.php index 490a704..4918b5b 100644 --- a/tests/server_basic_mysqli_testcase.php +++ b/tests/server_basic_mysqli_testcase.php @@ -1,11 +1,11 @@