]> granicus.if.org Git - php/commitdiff
fix the problem for connect_attr, set db condition, and add a new attribute _server_host
authorQianqian Bu <qibu@microsoft.com>
Mon, 12 Aug 2019 02:00:31 +0000 (04:00 +0200)
committerChristoph M. Becker <cmbecker69@gmx.de>
Tue, 20 Aug 2019 11:31:58 +0000 (13:31 +0200)
NEWS
ext/mysqli/tests/mysqli_connect_attr.phpt [new file with mode: 0644]
ext/mysqlnd/mysqlnd_auth.c
ext/mysqlnd/mysqlnd_connection.c
ext/mysqlnd/mysqlnd_wireprotocol.c
ext/pdo_mysql/tests/pdo_mysql_connect_attr.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index bcec73d1a7454ef7f648a5c4afa5f5a0fc8e94bc..e747a4f8eb4772062bf54b9d4e52251bdf731d56 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,10 @@ PHP                                                                        NEWS
   . Fixed bug #78412 (Generator incorrectly reports non-releasable $this as GC
     child). (Nikita)
 
+- MySQLnd:
+  . Fixed connect_attr issues and added the _server_host connection attribute.
+    (Qianqian Bu)
+
 29 Aug 2019, PHP 7.2.22
 
 - Core:
diff --git a/ext/mysqli/tests/mysqli_connect_attr.phpt b/ext/mysqli/tests/mysqli_connect_attr.phpt
new file mode 100644 (file)
index 0000000..96c79b1
--- /dev/null
@@ -0,0 +1,78 @@
+--TEST--
+mysqli check the session_connect_attrs table for connection attributes
+--SKIPIF--
+<?php
+require_once('skipif.inc');
+require_once('skipifconnectfailure.inc');
+
+if (!$IS_MYSQLND)
+       die("skip: test applies only to mysqlnd");
+
+if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
+       die("skip Cannot connect to the server");
+
+/* skip test if the server version does not have session_connect_attrs table yet*/
+if (!$res = mysqli_query($link, "select count(*) as count from information_schema.tables where table_schema='performance_schema' and table_name='session_connect_attrs';"))
+    die("skip select from information_schema.tables for session_connect_attrs  query failed");
+
+$tmp = mysqli_fetch_assoc($res);
+mysqli_free_result($res);
+if($tmp['count'] == "0") {
+    mysqli_close($link);
+    die("skip mysql does not support session_connect_attrs table yet");
+}
+
+/* skip test if performance_schema is OFF*/
+if (!$res = mysqli_query($link, "show variables like 'performance_schema';"))
+    die("skip show variables like 'performance_schema' failed");
+
+$tmp = mysqli_fetch_assoc($res);
+mysqli_free_result($res);
+if($tmp['Value'] == "OFF") {
+    mysqli_close($link);
+    die("skip performance_schema is OFF");
+}
+
+mysqli_close($link);
+?>
+--FILE--
+<?php
+       require_once("connect.inc");
+
+       $tmp    = NULL;
+       $link   = NULL;
+    $res    = NULL;
+       if (!$link = mysqli_connect($host, $user, $passwd, $db, $port, $socket))
+               printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",$host, $user, $db, $port, $socket);
+
+    //in case $host is empty, do not test for _server_host field
+    if (isset($host) && trim($host) != '') {
+        if (!$res = mysqli_query($link, "select * from performance_schema.session_connect_attrs where ATTR_NAME='_server_host' and processlist_id = connection_id()")) {
+            printf("[002] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
+        } else {
+            $tmp = mysqli_fetch_assoc($res);
+            if (!$tmp || !isset($tmp['ATTR_NAME'])) {
+                echo "[003] _server_host missing\n";
+            } elseif ($tmp['ATTR_VALUE'] !== $host) {
+                printf("[004] _server_host value mismatch\n") ;
+            }
+            mysqli_free_result($res);
+        }
+    }
+
+    if (!$res = mysqli_query($link, "select * from performance_schema.session_connect_attrs where ATTR_NAME='_client_name' and processlist_id = connection_id()")) {
+        printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
+    } else {
+        $tmp = mysqli_fetch_assoc($res);
+        if (!$tmp || !isset($tmp['ATTR_NAME'])) {
+            echo "[006] _client_name missing\n";
+        } elseif ($tmp['ATTR_VALUE'] !== "mysqlnd") {
+            printf("[007] _client_name value mismatch\n") ;
+        }
+        mysqli_free_result($res);
+    }
+
+    printf("done!");
+?>
+--EXPECTF--
+done!
\ No newline at end of file
index 3ba447cfdf919a8f16cd19e2c20473064925e1db..2b23257b735970ea107e92d54e7ca418b42bd2b5 100644 (file)
@@ -425,6 +425,9 @@ mysqlnd_auth_change_user(MYSQLND_CONN_DATA * const conn,
                auth_packet->auth_data_len = auth_plugin_data_len;
                auth_packet->auth_plugin_name = auth_protocol;
 
+        if (conn->server_capabilities & CLIENT_CONNECT_ATTRS) {
+                       auth_packet->connect_attr = conn->options->connect_attr;
+        }
 
                if (conn->m->get_server_version(conn) >= 50123) {
                        auth_packet->charset_no = conn->charset->nr;
index 42de003ef97826a218fb98f643debc8d0ceb5169..b5b3f4565890ca72d60a9e75b92fa8b718abf75a 100644 (file)
@@ -519,6 +519,10 @@ MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags)(MYSQLND_CONN_DATA *
        }
 #endif
 
+    if (conn->options->connect_attr && zend_hash_num_elements(conn->options->connect_attr)) {
+               mysql_flags |= CLIENT_CONNECT_ATTRS;
+       }
+
        DBG_RETURN(mysql_flags);
 }
 /* }}} */
@@ -665,7 +669,7 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
                password.s = "";
                password.l = 0;
        }
-       if (!database.s) {
+       if (!database.s || !database.s[0]) {
                DBG_INF_FMT("no db given, using empty string");
                database.s = "";
                database.l = 0;
@@ -833,6 +837,9 @@ MYSQLND_METHOD(mysqlnd_conn, connect)(MYSQLND * conn_handle,
 
        if (PASS == conn->m->local_tx_start(conn, this_func)) {
                mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_client_name", "mysqlnd");
+        if (hostname.l > 0) {
+            mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_server_host", hostname.s);
+        }
                ret = conn->m->connect(conn, hostname, username, password, database, port, socket_or_pipe, mysql_flags);
 
                conn->m->local_tx_end(conn, this_func, FAIL);
index 7616540e4f6264c12c6b44c7d87d53678ef77319..759b131b63c700668d45ca11ce0ea1229433b398 100644 (file)
@@ -552,7 +552,7 @@ size_t php_mysqlnd_auth_write(void * _packet)
                        p+= packet->auth_data_len;
                }
 
-               if (packet->db) {
+               if (packet->db_len > 0) {
                        /* CLIENT_CONNECT_WITH_DB should have been set */
                        size_t real_db_len = MIN(MYSQLND_MAX_ALLOWED_DB_LEN, packet->db_len);
                        memcpy(p, packet->db, real_db_len);
diff --git a/ext/pdo_mysql/tests/pdo_mysql_connect_attr.phpt b/ext/pdo_mysql/tests/pdo_mysql_connect_attr.phpt
new file mode 100644 (file)
index 0000000..2e5285a
--- /dev/null
@@ -0,0 +1,58 @@
+--TEST--
+PDO_MYSQL: check the session_connect_attrs table for connection attributes
+--SKIPIF--
+<?php
+require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
+require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
+MySQLPDOTest::skip();
+if (!MySQLPDOTest::isPDOMySQLnd()) die('skip only for mysqlnd');
+
+$pdo = MySQLPDOTest::factory();
+
+$stmt = $pdo->query("select count(*) from information_schema.tables where table_schema='performance_schema' and table_name='session_connect_attrs'");
+if (!$stmt || !$stmt->fetchColumn()) {
+    die("skip mysql does not support session_connect_attrs table yet");
+}
+
+$stmt = $pdo->query("show variables like 'performance_schema'");
+if (!$stmt || $stmt->fetchColumn(1) !== 'ON') {
+    die("skip performance_schema is OFF");
+}
+
+?>
+--FILE--
+<?php
+
+require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
+$pdo = MySQLPDOTest::factory();
+
+if (preg_match('/host=([^;]+)/', PDO_MYSQL_TEST_DSN, $m)) {
+    $host = $m[1];
+}
+
+//in case $host is empty, do not test for _server_host field
+if (isset($host) && $host !== '') {
+    $stmt = $pdo->query("select * from performance_schema.session_connect_attrs where ATTR_NAME='_server_host' and processlist_id = connection_id()");
+
+    $row = $stmt->fetch(PDO::FETCH_ASSOC);
+
+    if (!$row || !isset($row['attr_name'])) {
+        echo "_server_host missing\n";
+    } elseif ($row['attr_value'] !== $host) {
+        printf("_server_host mismatch (expected '%s', got '%s')\n", $host, $row['attr_value']);
+    }
+}
+
+$stmt = $pdo->query("select * from performance_schema.session_connect_attrs where ATTR_NAME='_client_name' and processlist_id = connection_id()");
+
+$row = $stmt->fetch(PDO::FETCH_ASSOC);
+if (!$row || !isset($row['attr_name'])) {
+    echo "_client_name missing\n";
+} elseif ($row['attr_value'] !== 'mysqlnd') {
+    printf("_client_name mismatch (expected 'mysqlnd', got '%s')\n", $row['attr_value']);
+}
+
+printf("done!");
+?>
+--EXPECT--
+done!
\ No newline at end of file