Skip to content

Commit cdf16c0

Browse files
GuuBucmb69
authored andcommitted
fix the problem for connect_attr, set db condition, and add a new attribute _server_host
1 parent 81f5215 commit cdf16c0

File tree

6 files changed

+152
-2
lines changed

6 files changed

+152
-2
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ PHP NEWS
77
. Fixed bug #78412 (Generator incorrectly reports non-releasable $this as GC
88
child). (Nikita)
99

10+
- MySQLnd:
11+
. Fixed connect_attr issues and added the _server_host connection attribute.
12+
(Qianqian Bu)
13+
1014
29 Aug 2019, PHP 7.2.22
1115

1216
- Core:
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
--TEST--
2+
mysqli check the session_connect_attrs table for connection attributes
3+
--SKIPIF--
4+
<?php
5+
require_once('skipif.inc');
6+
require_once('skipifconnectfailure.inc');
7+
8+
if (!$IS_MYSQLND)
9+
die("skip: test applies only to mysqlnd");
10+
11+
if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
12+
die("skip Cannot connect to the server");
13+
14+
/* skip test if the server version does not have session_connect_attrs table yet*/
15+
if (!$res = mysqli_query($link, "select count(*) as count from information_schema.tables where table_schema='performance_schema' and table_name='session_connect_attrs';"))
16+
die("skip select from information_schema.tables for session_connect_attrs query failed");
17+
18+
$tmp = mysqli_fetch_assoc($res);
19+
mysqli_free_result($res);
20+
if($tmp['count'] == "0") {
21+
mysqli_close($link);
22+
die("skip mysql does not support session_connect_attrs table yet");
23+
}
24+
25+
/* skip test if performance_schema is OFF*/
26+
if (!$res = mysqli_query($link, "show variables like 'performance_schema';"))
27+
die("skip show variables like 'performance_schema' failed");
28+
29+
$tmp = mysqli_fetch_assoc($res);
30+
mysqli_free_result($res);
31+
if($tmp['Value'] == "OFF") {
32+
mysqli_close($link);
33+
die("skip performance_schema is OFF");
34+
}
35+
36+
mysqli_close($link);
37+
?>
38+
--FILE--
39+
<?php
40+
require_once("connect.inc");
41+
42+
$tmp = NULL;
43+
$link = NULL;
44+
$res = NULL;
45+
if (!$link = mysqli_connect($host, $user, $passwd, $db, $port, $socket))
46+
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);
47+
48+
//in case $host is empty, do not test for _server_host field
49+
if (isset($host) && trim($host) != '') {
50+
if (!$res = mysqli_query($link, "select * from performance_schema.session_connect_attrs where ATTR_NAME='_server_host' and processlist_id = connection_id()")) {
51+
printf("[002] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
52+
} else {
53+
$tmp = mysqli_fetch_assoc($res);
54+
if (!$tmp || !isset($tmp['ATTR_NAME'])) {
55+
echo "[003] _server_host missing\n";
56+
} elseif ($tmp['ATTR_VALUE'] !== $host) {
57+
printf("[004] _server_host value mismatch\n") ;
58+
}
59+
mysqli_free_result($res);
60+
}
61+
}
62+
63+
if (!$res = mysqli_query($link, "select * from performance_schema.session_connect_attrs where ATTR_NAME='_client_name' and processlist_id = connection_id()")) {
64+
printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
65+
} else {
66+
$tmp = mysqli_fetch_assoc($res);
67+
if (!$tmp || !isset($tmp['ATTR_NAME'])) {
68+
echo "[006] _client_name missing\n";
69+
} elseif ($tmp['ATTR_VALUE'] !== "mysqlnd") {
70+
printf("[007] _client_name value mismatch\n") ;
71+
}
72+
mysqli_free_result($res);
73+
}
74+
75+
printf("done!");
76+
?>
77+
--EXPECTF--
78+
done!

ext/mysqlnd/mysqlnd_auth.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ mysqlnd_auth_change_user(MYSQLND_CONN_DATA * const conn,
425425
auth_packet->auth_data_len = auth_plugin_data_len;
426426
auth_packet->auth_plugin_name = auth_protocol;
427427

428+
if (conn->server_capabilities & CLIENT_CONNECT_ATTRS) {
429+
auth_packet->connect_attr = conn->options->connect_attr;
430+
}
428431

429432
if (conn->m->get_server_version(conn) >= 50123) {
430433
auth_packet->charset_no = conn->charset->nr;

ext/mysqlnd/mysqlnd_connection.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,10 @@ MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags)(MYSQLND_CONN_DATA *
519519
}
520520
#endif
521521

522+
if (conn->options->connect_attr && zend_hash_num_elements(conn->options->connect_attr)) {
523+
mysql_flags |= CLIENT_CONNECT_ATTRS;
524+
}
525+
522526
DBG_RETURN(mysql_flags);
523527
}
524528
/* }}} */
@@ -665,7 +669,7 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
665669
password.s = "";
666670
password.l = 0;
667671
}
668-
if (!database.s) {
672+
if (!database.s || !database.s[0]) {
669673
DBG_INF_FMT("no db given, using empty string");
670674
database.s = "";
671675
database.l = 0;
@@ -833,6 +837,9 @@ MYSQLND_METHOD(mysqlnd_conn, connect)(MYSQLND * conn_handle,
833837

834838
if (PASS == conn->m->local_tx_start(conn, this_func)) {
835839
mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_client_name", "mysqlnd");
840+
if (hostname.l > 0) {
841+
mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_server_host", hostname.s);
842+
}
836843
ret = conn->m->connect(conn, hostname, username, password, database, port, socket_or_pipe, mysql_flags);
837844

838845
conn->m->local_tx_end(conn, this_func, FAIL);

ext/mysqlnd/mysqlnd_wireprotocol.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ size_t php_mysqlnd_auth_write(void * _packet)
552552
p+= packet->auth_data_len;
553553
}
554554

555-
if (packet->db) {
555+
if (packet->db_len > 0) {
556556
/* CLIENT_CONNECT_WITH_DB should have been set */
557557
size_t real_db_len = MIN(MYSQLND_MAX_ALLOWED_DB_LEN, packet->db_len);
558558
memcpy(p, packet->db, real_db_len);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
--TEST--
2+
PDO_MYSQL: check the session_connect_attrs table for connection attributes
3+
--SKIPIF--
4+
<?php
5+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
6+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7+
MySQLPDOTest::skip();
8+
if (!MySQLPDOTest::isPDOMySQLnd()) die('skip only for mysqlnd');
9+
10+
$pdo = MySQLPDOTest::factory();
11+
12+
$stmt = $pdo->query("select count(*) from information_schema.tables where table_schema='performance_schema' and table_name='session_connect_attrs'");
13+
if (!$stmt || !$stmt->fetchColumn()) {
14+
die("skip mysql does not support session_connect_attrs table yet");
15+
}
16+
17+
$stmt = $pdo->query("show variables like 'performance_schema'");
18+
if (!$stmt || $stmt->fetchColumn(1) !== 'ON') {
19+
die("skip performance_schema is OFF");
20+
}
21+
22+
?>
23+
--FILE--
24+
<?php
25+
26+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
27+
$pdo = MySQLPDOTest::factory();
28+
29+
if (preg_match('/host=([^;]+)/', PDO_MYSQL_TEST_DSN, $m)) {
30+
$host = $m[1];
31+
}
32+
33+
//in case $host is empty, do not test for _server_host field
34+
if (isset($host) && $host !== '') {
35+
$stmt = $pdo->query("select * from performance_schema.session_connect_attrs where ATTR_NAME='_server_host' and processlist_id = connection_id()");
36+
37+
$row = $stmt->fetch(PDO::FETCH_ASSOC);
38+
39+
if (!$row || !isset($row['attr_name'])) {
40+
echo "_server_host missing\n";
41+
} elseif ($row['attr_value'] !== $host) {
42+
printf("_server_host mismatch (expected '%s', got '%s')\n", $host, $row['attr_value']);
43+
}
44+
}
45+
46+
$stmt = $pdo->query("select * from performance_schema.session_connect_attrs where ATTR_NAME='_client_name' and processlist_id = connection_id()");
47+
48+
$row = $stmt->fetch(PDO::FETCH_ASSOC);
49+
if (!$row || !isset($row['attr_name'])) {
50+
echo "_client_name missing\n";
51+
} elseif ($row['attr_value'] !== 'mysqlnd') {
52+
printf("_client_name mismatch (expected 'mysqlnd', got '%s')\n", $row['attr_value']);
53+
}
54+
55+
printf("done!");
56+
?>
57+
--EXPECT--
58+
done!

0 commit comments

Comments
 (0)