Skip to content

Commit

Permalink
Fixes to session
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoppanen committed Jan 21, 2016
1 parent 7ac4e83 commit 7cb0c48
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 23 deletions.
22 changes: 22 additions & 0 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,28 @@ if test "$PHP_MEMCACHED" != "no"; then
AC_MSG_RESULT([no])
fi

ORIG_CFLAGS="$CFLAGS"
ORIG_LIBS="$LIBS"

CFLAGS="$CFLAGS $PHP_LIBMEMCACHED_INCLUDES"
LIBS="$LIBS $PHP_LIBMEMCACHED_LIBS"

AC_CACHE_CHECK([whether memcached_exist is defined], ac_cv_have_memcached_exist, [
AC_TRY_LINK(
[ #include <libmemcached/memcached.h> ],
[ memcached_exist (NULL, NULL, 0); ],
[ ac_cv_have_memcached_exist="yes" ],
[ ac_cv_have_memcached_exist="no" ]
)
])

CFLAGS="$ORIG_CFLAGS"
LIBS="$ORIG_LIBS"

if test "$ac_cv_have_memcached_exist" = "yes"; then
AC_DEFINE(HAVE_MEMCACHED_EXIST, [1], [Whether memcached_exist is defined])
fi

PHP_MEMCACHED_FILES="php_memcached.c php_libmemcached_compat.c g_fmt.c"

if test "$PHP_SYSTEM_FASTLZ" != "no"; then
Expand Down
19 changes: 19 additions & 0 deletions php_memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,25 @@ static void php_memc_destroy(struct memc_obj *m_obj, zend_bool persistent);
****************************************/


memcached_return php_memcached_exist (memcached_st *memc, zend_string *key)
{
#ifdef HAVE_MEMCACHED_EXIST
return memcached_exist (memc, key->val, key->len);
#else
memcached_return rc = MEMCACHED_SUCCESS;
uint32_t flags = 0;
size_t value_length = 0;
char *value = NULL;

value = memcached_get (memc, key->val, key->len, &value_length, &flags, &rc);
if (value) {
free (value);
}
return rc;
#endif
}


char *php_memc_printable_func (zend_fcall_info *fci, zend_fcall_info_cache *fci_cache)
{
char *buffer = NULL;
Expand Down
2 changes: 2 additions & 0 deletions php_memcached_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ int php_memc_sess_list_entry(void);

char *php_memc_printable_func (zend_fcall_info *fci, zend_fcall_info_cache *fci_cache TSRMLS_DC);

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

#endif /* PHP_MEMCACHED_PRIVATE_H */

/*
Expand Down
42 changes: 19 additions & 23 deletions php_memcached_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ PS_OPEN_FUNC(memcached)
PS_CLOSE_FUNC(memcached)
{
memcached_sess *memc_sess = PS_GET_MOD_DATA();

if (!memc_sess) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Session is not allocated, check session.save_path value");
return FAILURE;
Expand Down Expand Up @@ -336,8 +336,9 @@ PS_READ_FUNC(memcached)
*val = zend_string_init(payload, payload_len, 1);
free(payload);
return SUCCESS;
} else if (status = MEMCACHED_NOTFOUND) {
} else if (status == MEMCACHED_NOTFOUND) {
*val = ZSTR_EMPTY_ALLOC();
return SUCCESS;
} else {
return FAILURE;
}
Expand All @@ -351,7 +352,7 @@ PS_WRITE_FUNC(memcached)
memcached_return status;
memcached_sess *memc_sess = PS_GET_MOD_DATA();
size_t key_length;

if (!memc_sess) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Session is not allocated, check session.save_path value");
return FAILURE;
Expand Down Expand Up @@ -409,39 +410,34 @@ PS_GC_FUNC(memcached)
PS_CREATE_SID_FUNC(memcached)
{
zend_string *sid;
int maxfail = 3;
int retries = 3;
memcached_sess *memc_sess = PS_GET_MOD_DATA();
time_t expiration = PS(gc_maxlifetime);

do {
if (!memc_sess) {
return NULL;
}

while (retries-- > 0) {
sid = php_session_create_id((void**)&memc_sess);
if (!sid) {
if (--maxfail < 0) {
return NULL;
} else {
continue;

if (sid) {
if (memcached_add(memc_sess->memc_sess, sid->val, sid->len, "0", 0, expiration, 0) == MEMCACHED_SUCCESS) {
return sid;
}
}
/* Check collision */
/* FIXME: mod_data(memc_sess) should not be NULL (User handler could be NULL) */
if (memc_sess && memcached_exist(memc_sess->memc_sess, sid->val, sid->len) == MEMCACHED_SUCCESS) {
if (sid) {
else {
zend_string_release(sid);
sid = NULL;
}
if (--maxfail < 0) {
return NULL;
}
}
} while(!sid);

return sid;
}
return NULL;
}

PS_VALIDATE_SID_FUNC(memcached)
{
memcached_sess *memc_sess = PS_GET_MOD_DATA();

if (memcached_exist(memc_sess->memc_sess, key->val, key->len) == MEMCACHED_SUCCESS) {
if (php_memcached_exist(memc_sess->memc_sess, key) == MEMCACHED_SUCCESS) {
return SUCCESS;
} else {
return FAILURE;
Expand Down

0 comments on commit 7cb0c48

Please sign in to comment.