Skip to content

Commit aaaf0f2

Browse files
hongboliuyatsukhnenko
authored andcommitted
Add callbacks validate_sid() & update_timestamp() to session handler
Newer session handler API(PS_MOD_UPDATE_TIMESTAMP) supports 2 more callbacks: * validate_sid() is used by session.use_strict_mode, which provides better security. * update_timestamp() is used by session.lazy_write, which can improve performance in some situations.
1 parent 28ec432 commit aaaf0f2

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

Diff for: redis_session.c

+106
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,16 @@
6161
/* Check if a response is the Redis +OK status response */
6262
#define IS_REDIS_OK(r, len) (r != NULL && len == 3 && !memcmp(r, "+OK", 3))
6363

64+
#if (PHP_MAJOR_VERSION < 7)
6465
ps_module ps_mod_redis = {
6566
PS_MOD_SID(redis)
6667
};
68+
#else
69+
ps_module ps_mod_redis = {
70+
PS_MOD_UPDATE_TIMESTAMP(redis)
71+
};
72+
#endif
73+
6774
ps_module ps_mod_redis_cluster = {
6875
PS_MOD(rediscluster)
6976
};
@@ -636,6 +643,105 @@ PS_CREATE_SID_FUNC(redis)
636643
}
637644
/* }}} */
638645

646+
#if (PHP_MAJOR_VERSION >= 7)
647+
/* {{{ PS_VALIDATE_SID_FUNC
648+
*/
649+
PS_VALIDATE_SID_FUNC(redis)
650+
{
651+
char *cmd, *response;
652+
int cmd_len, response_len;
653+
#if (PHP_MAJOR_VERSION < 7)
654+
const char *skey = key;
655+
size_t skeylen = strlen(key);
656+
#else
657+
const char *skey = ZSTR_VAL(key);
658+
size_t skeylen = ZSTR_LEN(key);
659+
#endif
660+
661+
if (!skeylen) return FAILURE;
662+
663+
redis_pool *pool = PS_GET_MOD_DATA();
664+
redis_pool_member *rpm = redis_pool_get_sock(pool, skey TSRMLS_CC);
665+
RedisSock *redis_sock = rpm ? rpm->redis_sock : NULL;
666+
if (!redis_sock) {
667+
return FAILURE;
668+
}
669+
670+
/* send EXISTS command */
671+
zend_string *session = redis_session_key(rpm, skey, skeylen);
672+
cmd_len = REDIS_SPPRINTF(&cmd, "EXISTS", "S", session);
673+
zend_string_release(session);
674+
if (redis_sock_write(redis_sock, cmd, cmd_len TSRMLS_CC) < 0) {
675+
efree(cmd);
676+
return FAILURE;
677+
}
678+
efree(cmd);
679+
680+
/* read response */
681+
if ((response = redis_sock_read(redis_sock, &response_len TSRMLS_CC)) == NULL) {
682+
return FAILURE;
683+
}
684+
685+
if (response_len == 2 && response[0] == ':' && response[1] == '1') {
686+
efree(response);
687+
return SUCCESS;
688+
} else {
689+
efree(response);
690+
return FAILURE;
691+
}
692+
}
693+
/* }}} */
694+
695+
/* {{{ PS_UPDATE_TIMESTAMP_FUNC
696+
*/
697+
PS_UPDATE_TIMESTAMP_FUNC(redis)
698+
{
699+
char *cmd, *response;
700+
int cmd_len, response_len;
701+
#if (PHP_MAJOR_VERSION < 7)
702+
const char *skey = key, *sval = val;
703+
size_t skeylen = strlen(key), svallen = vallen;
704+
#else
705+
const char *skey = ZSTR_VAL(key), *sval = ZSTR_VAL(val);
706+
size_t skeylen = ZSTR_LEN(key), svallen = ZSTR_LEN(val);
707+
#endif
708+
709+
if (!skeylen) return FAILURE;
710+
711+
redis_pool *pool = PS_GET_MOD_DATA();
712+
redis_pool_member *rpm = redis_pool_get_sock(pool, skey TSRMLS_CC);
713+
RedisSock *redis_sock = rpm ? rpm->redis_sock : NULL;
714+
if (!redis_sock) {
715+
return FAILURE;
716+
}
717+
718+
/* send EXPIRE command */
719+
zend_string *session = redis_session_key(rpm, skey, skeylen);
720+
cmd_len = REDIS_SPPRINTF(&cmd, "EXPIRE", "Sd", session, INI_INT("session.gc_maxlifetime"));
721+
zend_string_release(session);
722+
723+
if (!write_allowed(redis_sock, &pool->lock_status TSRMLS_CC) || redis_sock_write(redis_sock, cmd, cmd_len TSRMLS_CC) < 0) {
724+
efree(cmd);
725+
return FAILURE;
726+
}
727+
efree(cmd);
728+
729+
/* read response */
730+
if ((response = redis_sock_read(redis_sock, &response_len TSRMLS_CC)) == NULL) {
731+
return FAILURE;
732+
}
733+
734+
if (response_len == 2 && response[0] == ':' && response[1] == '1') {
735+
efree(response);
736+
return SUCCESS;
737+
} else {
738+
efree(response);
739+
return FAILURE;
740+
}
741+
}
742+
/* }}} */
743+
#endif
744+
639745
/* {{{ PS_READ_FUNC
640746
*/
641747
PS_READ_FUNC(redis)

Diff for: redis_session.h

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ PS_DESTROY_FUNC(redis);
1111
PS_GC_FUNC(redis);
1212
PS_CREATE_SID_FUNC(redis);
1313

14+
#if (PHP_MAJOR_VERSION >= 7)
15+
PS_VALIDATE_SID_FUNC(redis);
16+
PS_UPDATE_TIMESTAMP_FUNC(redis);
17+
#endif
18+
1419
PS_OPEN_FUNC(rediscluster);
1520
PS_CLOSE_FUNC(rediscluster);
1621
PS_READ_FUNC(rediscluster);

0 commit comments

Comments
 (0)