diff --git a/NEWS b/NEWS index 1d8f32b966b..013029cf2db 100644 --- a/NEWS +++ b/NEWS @@ -14,6 +14,10 @@ PHP NEWS . Fixed bug #78383 (Casting a DateTime to array no longer returns its properties). (Nikita) +- MySQLnd: + . Fixed connect_attr issues and added the _server_host connection attribute. + (Qianqian Bu) + - OpenSSL: . Fixed bug #78391 (Assertion failure in openssl_random_pseudo_bytes). (Nikita) diff --git a/ext/mysqli/tests/mysqli_connect_attr.phpt b/ext/mysqli/tests/mysqli_connect_attr.phpt new file mode 100644 index 00000000000..96c79b1a6b7 --- /dev/null +++ b/ext/mysqli/tests/mysqli_connect_attr.phpt @@ -0,0 +1,78 @@ +--TEST-- +mysqli check the session_connect_attrs table for connection attributes +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +done! \ No newline at end of file diff --git a/ext/mysqlnd/mysqlnd_auth.c b/ext/mysqlnd/mysqlnd_auth.c index 342005aa36b..b08420f2689 100644 --- a/ext/mysqlnd/mysqlnd_auth.c +++ b/ext/mysqlnd/mysqlnd_auth.c @@ -420,6 +420,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; diff --git a/ext/mysqlnd/mysqlnd_connection.c b/ext/mysqlnd/mysqlnd_connection.c index 359aacc55ad..9154731be5c 100644 --- a/ext/mysqlnd/mysqlnd_connection.c +++ b/ext/mysqlnd/mysqlnd_connection.c @@ -510,6 +510,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); } /* }}} */ @@ -653,7 +657,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; @@ -825,6 +829,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); diff --git a/ext/mysqlnd/mysqlnd_wireprotocol.c b/ext/mysqlnd/mysqlnd_wireprotocol.c index 2b952801ad6..d0f1312f558 100644 --- a/ext/mysqlnd/mysqlnd_wireprotocol.c +++ b/ext/mysqlnd/mysqlnd_wireprotocol.c @@ -545,7 +545,7 @@ size_t php_mysqlnd_auth_write(MYSQLND_CONN_DATA * conn, 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 index 00000000000..2e5285a6e77 --- /dev/null +++ b/ext/pdo_mysql/tests/pdo_mysql_connect_attr.phpt @@ -0,0 +1,58 @@ +--TEST-- +PDO_MYSQL: check the session_connect_attrs table for connection attributes +--SKIPIF-- +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-- +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