From f7c92b06997d298f63a13d1791fa7a050c8598b9 Mon Sep 17 00:00:00 2001 From: Aaron Stone Date: Sun, 12 Feb 2017 15:56:22 +0000 Subject: [PATCH] Update warning for touch command in binary protocol mode with libmemcached < 1.0.18 (#322) --- package.xml | 1 + php_libmemcached_compat.c | 21 +++++++++++++++ php_libmemcached_compat.h | 3 +++ php_memcached.c | 13 ++------- php_memcached_session.c | 2 +- tests/gh_155.phpt | 7 +++-- tests/session_basic.phpt | 1 + tests/session_basic2.phpt | 1 + tests/session_basic3.phpt | 1 + tests/session_lazy_warning.phpt | 47 +++++++++++++++++++++++++++++++++ tests/session_lock-php71.phpt | 4 +++ tests/session_lock.phpt | 4 +++ tests/touch_binary.phpt | 5 +++- 13 files changed, 95 insertions(+), 15 deletions(-) create mode 100644 tests/session_lazy_warning.phpt diff --git a/package.xml b/package.xml index 7aadf4d..a5c7c07 100644 --- a/package.xml +++ b/package.xml @@ -157,6 +157,7 @@ Fixes + diff --git a/php_libmemcached_compat.c b/php_libmemcached_compat.c index bd35d8f..7f003b1 100644 --- a/php_libmemcached_compat.c +++ b/php_libmemcached_compat.c @@ -35,3 +35,24 @@ memcached_return php_memcached_exist (memcached_st *memc, zend_string *key) return rc; #endif } + +memcached_return php_memcached_touch(memcached_st *memc, const char *key, size_t key_len, time_t expiration) +{ +#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX < 0x01000018 + if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL)) { + 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"); + } +#endif + return memcached_touch(memc, key, key_len, expiration); +} + +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) +{ +#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX < 0x01000018 + if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL)) { + 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"); + } +#endif + return memcached_touch_by_key(memc, server_key, server_key_len, key, key_len, expiration); +} + diff --git a/php_libmemcached_compat.h b/php_libmemcached_compat.h index 9bcbc20..42a409d 100644 --- a/php_libmemcached_compat.h +++ b/php_libmemcached_compat.h @@ -22,6 +22,9 @@ memcached_return php_memcached_exist (memcached_st *memc, zend_string *key); +memcached_return php_memcached_touch(memcached_st *memc, const char *key, size_t key_len, time_t expiration); +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); + #if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x01000017 typedef const memcached_instance_st * php_memcached_instance_st; #else diff --git a/php_memcached.c b/php_memcached.c index d6f4257..1967332 100644 --- a/php_memcached.c +++ b/php_memcached.c @@ -1081,7 +1081,7 @@ zend_bool s_memc_write_zval (php_memc_object_t *intern, php_memc_write_op op, ze break; case MEMC_OP_TOUCH: - status = memcached_touch_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), ZSTR_VAL(key), ZSTR_LEN(key), expiration); + status = php_memcached_touch_by_key(intern->memc, ZSTR_VAL(server_key), ZSTR_LEN(server_key), ZSTR_VAL(key), ZSTR_LEN(key), expiration); break; case MEMC_OP_ADD: @@ -1113,7 +1113,7 @@ retry: break; case MEMC_OP_TOUCH: - status = memcached_touch(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key), expiration); + status = php_memcached_touch(intern->memc, ZSTR_VAL(key), ZSTR_LEN(key), expiration); break; case MEMC_OP_ADD: @@ -1959,15 +1959,6 @@ static void php_memc_store_impl(INTERNAL_FUNCTION_PARAMETERS, int op, zend_bool } } - - if (op == MEMC_OP_TOUCH) { -#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX < 0x01000016 - if (memcached_behavior_get(intern->memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL)) { - php_error_docref(NULL, E_WARNING, "using touch command with binary protocol is not recommended with libmemcached versions below 1.0.16"); - } -#endif - } - if (!s_memc_write_zval (intern, op, server_key, key, value, expiration)) { RETURN_FALSE; } diff --git a/php_memcached_session.c b/php_memcached_session.c index 1695b33..b6d93c5 100644 --- a/php_memcached_session.c +++ b/php_memcached_session.c @@ -542,7 +542,7 @@ PS_UPDATE_TIMESTAMP_FUNC(memcached) memcached_st *memc = PS_GET_MOD_DATA(); time_t expiration = s_session_expiration(maxlifetime); - if (memcached_touch(memc, key->val, key->len, expiration) == MEMCACHED_FAILURE) { + if (php_memcached_touch(memc, key->val, key->len, expiration) == MEMCACHED_FAILURE) { return FAILURE; } return SUCCESS; diff --git a/tests/gh_155.phpt b/tests/gh_155.phpt index a5b61d0..699e8ec 100644 --- a/tests/gh_155.phpt +++ b/tests/gh_155.phpt @@ -4,7 +4,10 @@ Test for bug 155 --FILE-- setOption(Memcached::OPT_BINARY_PROTOCOL,true); +$m->setOption(Memcached::OPT_BINARY_PROTOCOL, true); $m->addServer(MEMC_SERVER_HOST, MEMC_SERVER_PORT); $key = 'bug_155_' . uniqid(); diff --git a/tests/session_basic.phpt b/tests/session_basic.phpt index 3fdb6bd..0e8b346 100644 --- a/tests/session_basic.phpt +++ b/tests/session_basic.phpt @@ -7,6 +7,7 @@ if (!Memcached::HAVE_SESSION) print "skip"; ?> --INI-- session.save_handler = memcached +memcached.sess_binary_protocol = Off --FILE-- --INI-- session.save_handler = memcached +memcached.sess_binary_protocol = Off --FILE-- --INI-- session.save_handler = memcached +memcached.sess_binary_protocol = Off --FILE-- = 0x01000018) die ('skip too old libmemcached'); +?> +--INI-- +session.save_handler = memcached +memcached.sess_binary_protocol = On +--FILE-- +TRUE]); +$_SESSION['foo'] = 1; +session_write_close(); + +$_SESSION = NULL; + +var_dump($_SESSION); +session_start(); +var_dump($_SESSION); +session_write_close(); + +session_start(); +session_destroy(); + +session_start(); +var_dump($_SESSION); +session_write_close(); + + +--EXPECTF-- +NULL +array(1) { + ["foo"]=> + int(1) +} + +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 +array(0) { +} diff --git a/tests/session_lock-php71.phpt b/tests/session_lock-php71.phpt index 207f64c..c0fceb6 100644 --- a/tests/session_lock-php71.phpt +++ b/tests/session_lock-php71.phpt @@ -14,6 +14,10 @@ memcached.sess_lock_wait_max = 1000 memcached.sess_lock_retries = 3 memcached.sess_prefix = "memc.test." +# Turn off binary protocol while the test matrix has older versions of +# libmemcached for which the extension warns of a broken touch command. +memcached.sess_binary_protocol = Off + session.save_handler = memcached --FILE-- diff --git a/tests/session_lock.phpt b/tests/session_lock.phpt index 570e5a2..cedc83b 100644 --- a/tests/session_lock.phpt +++ b/tests/session_lock.phpt @@ -14,6 +14,10 @@ memcached.sess_lock_wait_max = 1000 memcached.sess_lock_retries = 3 memcached.sess_prefix = "memc.test." +# Turn off binary protocol while the test matrix has older versions of +# libmemcached for which the extension warns of a broken touch command. +memcached.sess_binary_protocol = Off + session.save_handler = memcached --FILE-- diff --git a/tests/touch_binary.phpt b/tests/touch_binary.phpt index cc35e8f..382c177 100644 --- a/tests/touch_binary.phpt +++ b/tests/touch_binary.phpt @@ -4,7 +4,10 @@ Touch in binary mode --FILE--