Skip to content

Commit 3771d66

Browse files
committed
Merge branch 'PHP-7.2' into PHP-7.3
* PHP-7.2: fix the problem for connect_attr, set db condition, and add a new attribute _server_host
2 parents 5e19f1d + cdf16c0 commit 3771d66

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
@@ -11,6 +11,10 @@ PHP NEWS
1111
. Ensure IDNA2003 rules are used with idn_to_ascii() and idn_to_utf8()
1212
when requested. (Sara)
1313

14+
- MySQLnd:
15+
. Fixed connect_attr issues and added the _server_host connection attribute.
16+
(Qianqian Bu)
17+
1418
29 Aug 2019, PHP 7.3.9
1519

1620
- 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
@@ -409,6 +409,9 @@ mysqlnd_auth_change_user(MYSQLND_CONN_DATA * const conn,
409409
auth_packet.auth_data_len = auth_plugin_data_len;
410410
auth_packet.auth_plugin_name = auth_protocol;
411411

412+
if (conn->server_capabilities & CLIENT_CONNECT_ATTRS) {
413+
auth_packet.connect_attr = conn->options->connect_attr;
414+
}
412415

413416
if (conn->m->get_server_version(conn) >= 50123) {
414417
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
@@ -510,6 +510,10 @@ MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags)(MYSQLND_CONN_DATA *
510510
}
511511
#endif
512512

513+
if (conn->options->connect_attr && zend_hash_num_elements(conn->options->connect_attr)) {
514+
mysql_flags |= CLIENT_CONNECT_ATTRS;
515+
}
516+
513517
DBG_RETURN(mysql_flags);
514518
}
515519
/* }}} */
@@ -653,7 +657,7 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
653657
password.s = "";
654658
password.l = 0;
655659
}
656-
if (!database.s) {
660+
if (!database.s || !database.s[0]) {
657661
DBG_INF_FMT("no db given, using empty string");
658662
database.s = "";
659663
database.l = 0;
@@ -821,6 +825,9 @@ MYSQLND_METHOD(mysqlnd_conn, connect)(MYSQLND * conn_handle,
821825

822826
if (PASS == conn->m->local_tx_start(conn, this_func)) {
823827
mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_client_name", "mysqlnd");
828+
if (hostname.l > 0) {
829+
mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_server_host", hostname.s);
830+
}
824831
ret = conn->m->connect(conn, hostname, username, password, database, port, socket_or_pipe, mysql_flags);
825832

826833
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
@@ -546,7 +546,7 @@ size_t php_mysqlnd_auth_write(MYSQLND_CONN_DATA * conn, void * _packet)
546546
p+= packet->auth_data_len;
547547
}
548548

549-
if (packet->db) {
549+
if (packet->db_len > 0) {
550550
/* CLIENT_CONNECT_WITH_DB should have been set */
551551
size_t real_db_len = MIN(MYSQLND_MAX_ALLOWED_DB_LEN, packet->db_len);
552552
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)