Skip to content

Commit f7c92b0

Browse files
authored
Update warning for touch command in binary protocol mode with libmemcached < 1.0.18 (#322)
1 parent 83d5f30 commit f7c92b0

13 files changed

+95
-15
lines changed

package.xml

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ Fixes
157157
<file role='test' name='gh_155.phpt'/>
158158
<file role='test' name='get_flags.phpt'/>
159159
<file role='test' name='session_lock.phpt'/>
160+
<file role='test' name='session_lazy_warning.phpt'/>
160161
<file role='test' name='session_regenerate.phpt'/>
161162
<file role='test' name='stats.phpt'/>
162163
<file role='test' name='default_behavior.phpt'/>

php_libmemcached_compat.c

+21
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,24 @@ memcached_return php_memcached_exist (memcached_st *memc, zend_string *key)
3535
return rc;
3636
#endif
3737
}
38+
39+
memcached_return php_memcached_touch(memcached_st *memc, const char *key, size_t key_len, time_t expiration)
40+
{
41+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX < 0x01000018
42+
if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL)) {
43+
php_error_docref(NULL, E_WARNING, "using touch command with binary protocol is not recommended with libmemcached versions below 1.0.18, please use ascii protocol or upgrade libmemcached");
44+
}
45+
#endif
46+
return memcached_touch(memc, key, key_len, expiration);
47+
}
48+
49+
memcached_return php_memcached_touch_by_key(memcached_st *memc, const char *server_key, size_t server_key_len, const char *key, size_t key_len, time_t expiration)
50+
{
51+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX < 0x01000018
52+
if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL)) {
53+
php_error_docref(NULL, E_WARNING, "using touch command with binary protocol is not recommended with libmemcached versions below 1.0.18, please use ascii protocol or upgrade libmemcached");
54+
}
55+
#endif
56+
return memcached_touch_by_key(memc, server_key, server_key_len, key, key_len, expiration);
57+
}
58+

php_libmemcached_compat.h

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
memcached_return php_memcached_exist (memcached_st *memc, zend_string *key);
2424

25+
memcached_return php_memcached_touch(memcached_st *memc, const char *key, size_t key_len, time_t expiration);
26+
memcached_return php_memcached_touch_by_key(memcached_st *memc, const char *server_key, size_t server_key_len, const char *key, size_t key_len, time_t expiration);
27+
2528
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x01000017
2629
typedef const memcached_instance_st * php_memcached_instance_st;
2730
#else

php_memcached.c

+2-11
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ zend_bool s_memc_write_zval (php_memc_object_t *intern, php_memc_write_op op, ze
10811081
break;
10821082

10831083
case MEMC_OP_TOUCH:
1084-
status = memcached_touch_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), ZSTR_VAL(key), ZSTR_LEN(key), expiration);
1084+
status = php_memcached_touch_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), ZSTR_VAL(key), ZSTR_LEN(key), expiration);
10851085
break;
10861086

10871087
case MEMC_OP_ADD:
@@ -1113,7 +1113,7 @@ zend_bool s_memc_write_zval (php_memc_object_t *intern, php_memc_write_op op, ze
11131113
break;
11141114

11151115
case MEMC_OP_TOUCH:
1116-
status = memcached_touch(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key), expiration);
1116+
status = php_memcached_touch(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key), expiration);
11171117
break;
11181118

11191119
case MEMC_OP_ADD:
@@ -1959,15 +1959,6 @@ static void php_memc_store_impl(INTERNAL_FUNCTION_PARAMETERS, int op, zend_bool
19591959
}
19601960
}
19611961

1962-
1963-
if (op == MEMC_OP_TOUCH) {
1964-
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX < 0x01000016
1965-
if (memcached_behavior_get(intern->memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL)) {
1966-
php_error_docref(NULL, E_WARNING, "using touch command with binary protocol is not recommended with libmemcached versions below 1.0.16");
1967-
}
1968-
#endif
1969-
}
1970-
19711962
if (!s_memc_write_zval (intern, op, server_key, key, value, expiration)) {
19721963
RETURN_FALSE;
19731964
}

php_memcached_session.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ PS_UPDATE_TIMESTAMP_FUNC(memcached)
542542
memcached_st *memc = PS_GET_MOD_DATA();
543543
time_t expiration = s_session_expiration(maxlifetime);
544544

545-
if (memcached_touch(memc, key->val, key->len, expiration) == MEMCACHED_FAILURE) {
545+
if (php_memcached_touch(memc, key->val, key->len, expiration) == MEMCACHED_FAILURE) {
546546
return FAILURE;
547547
}
548548
return SUCCESS;

tests/gh_155.phpt

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ Test for bug 155
44
<?php
55
$min_version = "1.4.8";
66
include dirname(__FILE__) . "/skipif.inc";
7-
if (Memcached::LIBMEMCACHED_VERSION_HEX < 0x01000016) die ('skip too old libmemcached');
7+
// The touch command in binary mode will work in libmemcached 1.0.16, but runs out the timeout clock
8+
// See https://github.com/php-memcached-dev/php-memcached/issues/310 for further explanation
9+
// The problem is fixed fully in libmemcached 1.0.18, so we'll focus tests on that version
10+
if (Memcached::LIBMEMCACHED_VERSION_HEX < 0x01000018) die ('skip too old libmemcached');
811
?>
912
--FILE--
1013
<?php
1114
include dirname (__FILE__) . '/config.inc';
1215

1316
$m = new Memcached ();
1417

15-
$m->setOption(Memcached::OPT_BINARY_PROTOCOL,true);
18+
$m->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
1619
$m->addServer(MEMC_SERVER_HOST, MEMC_SERVER_PORT);
1720

1821
$key = 'bug_155_' . uniqid();

tests/session_basic.phpt

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ if (!Memcached::HAVE_SESSION) print "skip";
77
?>
88
--INI--
99
session.save_handler = memcached
10+
memcached.sess_binary_protocol = Off
1011
--FILE--
1112
<?php
1213
include dirname (__FILE__) . '/config.inc';

tests/session_basic2.phpt

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ if (!Memcached::HAVE_SESSION) print "skip";
77
?>
88
--INI--
99
session.save_handler = memcached
10+
memcached.sess_binary_protocol = Off
1011
--FILE--
1112
<?php
1213
include dirname (__FILE__) . '/config.inc';

tests/session_basic3.phpt

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ if (!Memcached::HAVE_SESSION) print "skip";
77
?>
88
--INI--
99
session.save_handler = memcached
10+
memcached.sess_binary_protocol = Off
1011
--FILE--
1112
<?php
1213
include dirname (__FILE__) . '/config.inc';

tests/session_lazy_warning.phpt

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
Session lazy binary warning old libmemcached
3+
--SKIPIF--
4+
<?php
5+
include dirname(__FILE__) . "/skipif.inc";
6+
if (!Memcached::HAVE_SESSION) print "skip";
7+
if (Memcached::LIBMEMCACHED_VERSION_HEX >= 0x01000018) die ('skip too old libmemcached');
8+
?>
9+
--INI--
10+
session.save_handler = memcached
11+
memcached.sess_binary_protocol = On
12+
--FILE--
13+
<?php
14+
include dirname (__FILE__) . '/config.inc';
15+
ini_set ('session.save_path', MEMC_SERVER_HOST . ':' . MEMC_SERVER_PORT);
16+
17+
ob_start();
18+
19+
session_start(['lazy_write'=>TRUE]);
20+
$_SESSION['foo'] = 1;
21+
session_write_close();
22+
23+
$_SESSION = NULL;
24+
25+
var_dump($_SESSION);
26+
session_start();
27+
var_dump($_SESSION);
28+
session_write_close();
29+
30+
session_start();
31+
session_destroy();
32+
33+
session_start();
34+
var_dump($_SESSION);
35+
session_write_close();
36+
37+
38+
--EXPECTF--
39+
NULL
40+
array(1) {
41+
["foo"]=>
42+
int(1)
43+
}
44+
45+
Warning: session_write_close(): using touch command with binary protocol is not recommended with libmemcached versions below 1.0.18, please use ascii protocol or upgrade libmemcached in %s on line %d
46+
array(0) {
47+
}

tests/session_lock-php71.phpt

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ memcached.sess_lock_wait_max = 1000
1414
memcached.sess_lock_retries = 3
1515
memcached.sess_prefix = "memc.test."
1616

17+
# Turn off binary protocol while the test matrix has older versions of
18+
# libmemcached for which the extension warns of a broken touch command.
19+
memcached.sess_binary_protocol = Off
20+
1721
session.save_handler = memcached
1822

1923
--FILE--

tests/session_lock.phpt

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ memcached.sess_lock_wait_max = 1000
1414
memcached.sess_lock_retries = 3
1515
memcached.sess_prefix = "memc.test."
1616

17+
# Turn off binary protocol while the test matrix has older versions of
18+
# libmemcached for which the extension warns of a broken touch command.
19+
memcached.sess_binary_protocol = Off
20+
1721
session.save_handler = memcached
1822

1923
--FILE--

tests/touch_binary.phpt

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ Touch in binary mode
44
<?php
55
$min_version = "1.4.8"; //TOUCH is added since 1.4.8
66
include dirname(__FILE__) . "/skipif.inc";
7-
if (Memcached::LIBMEMCACHED_VERSION_HEX < 0x01000016) die ('skip too old libmemcached');
7+
// The touch command in binary mode will work in libmemcached 1.0.16, but runs out the timeout clock
8+
// See https://github.com/php-memcached-dev/php-memcached/issues/310 for further explanation
9+
// The problem is fixed fully in libmemcached 1.0.18, so we'll focus tests on that version
10+
if (Memcached::LIBMEMCACHED_VERSION_HEX < 0x01000018) die ('skip too old libmemcached');
811
?>
912
--FILE--
1013
<?php

0 commit comments

Comments
 (0)