8000 fix session_regenerate_id · phpredis/phpredis@45802b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 45802b4

Browse files
committed
fix session_regenerate_id
1 parent 3ccd8df commit 45802b4

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

redis_session.c

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,22 +1037,7 @@ PS_GC_FUNC(redis)
10371037
*/
10381038

10391039
/* Prefix a session key */
1040-
static char *cluster_session_key(redisCluster *c, const char *key, int keylen,
1041-
int *skeylen, short *slot) {
1042-
char *skey;
1043-
1044-
*skeylen = keylen + ZSTR_LEN(c->flags->prefix);
1045-
skey = emalloc(*skeylen);
1046-
memcpy(skey, ZSTR_VAL(c->flags->prefix), ZSTR_LEN(c->flags->prefix));
1047-
memcpy(skey + ZSTR_LEN(c->flags->prefix), key, keylen);
1048-
1049-
*slot = cluster_hash_key(skey, *skeylen);
1050-
1051-
return skey;
1052-
}
1053-
1054-
/* Prefix a session key */
1055-
static zend_string *cluster_session_key_new(redisCluster *c, const char *key, int keylen,
1040+
static zend_string *cluster_session_key(redisCluster *c, const char *key, int keylen,
10561041
short *slot) {
10571042
zend_string *session;
10581043
int skeylen;
@@ -1067,6 +1052,22 @@ static zend_string *cluster_session_key_new(redisCluster *c, const char *key, in
10671052
return session;
10681053
}
10691054

1055+
/* Prefix a session key */
1056+
static void *set_cluster_session_key(redis_cluster_session *sc, const char *key, int keylen) {
1057+
int skeylen;
1058+
redisCluster *c = sc->c;
1059+
1060+
if (sc->lock_status.session_key) {
1061+
zend_string_release(sc->lock_status.session_key);
1062+
}
1063+
skeylen = keylen + ZSTR_LEN(c->flags->prefix);
1064+
sc->lock_status.session_key = zend_string_alloc(skeylen, 0);
1065+
memcpy(ZSTR_VAL(sc->lock_status.session_key), ZSTR_VAL(c->flags->prefix), ZSTR_LEN(c->flags->prefix));
1066+
memcpy(ZSTR_VAL(sc->lock_status.session_key) + ZSTR_LEN(c->flags->prefix), key, keylen);
1067+
1068+
sc->slot = cluster_hash_key(ZSTR_VAL(sc->lock_status.session_key), skeylen);
1069+
}
1070+
10701071
PS_OPEN_FUNC(rediscluster) {
10711072
redis_cluster_session *sc;
10721073
redisCluster *c;
@@ -1187,11 +1188,12 @@ PS_OPEN_FUNC(rediscluster) {
11871188
*/
11881189
PS_CREATE_SID_FUNC(rediscluster)
11891190
{
1190-
redisCluster *c = PS_GET_MOD_DATA();
1191+
redis_cluster_session *sc = PS_GET_MOD_DATA();
1192+
redisCluster *c = sc->c;
11911193
clusterReply *reply;
1192-
char *cmd, *skey;
1193-
zend_string *sid;
1194-
int cmdlen, skeylen;
1194+
char *cmd;
1195+
zend_string *sid, *skey;
1196+
int cmdlen;
11951197
int retries = 3;
11961198
short slot;
11971199

@@ -1207,11 +1209,11 @@ PS_CREATE_SID_FUNC(rediscluster)
12071209
sid = php_session_create_id((void **) &c);
12081210

12091211
/* Create session key if it doesn't already exist */
1210-
skey = cluster_session_key(c, ZSTR_VAL(sid), ZSTR_LEN(sid), &skeylen, &slot);
1211-
cmdlen = redis_spprintf(NULL, NULL, &cmd, "SET", "ssssd", skey,
1212-
skeylen, "", 0, "NX", 2, "EX", 2, session_gc_maxlifetime());
1212+
skey = cluster_session_key(c, ZSTR_VAL(sid), ZSTR_LEN(sid), &slot);
1213+
cmdlen = redis_spprintf(NULL, NULL, &cmd, "SET", "Ssssd", skey,
1214+
"", 0, "NX", 2, "EX", 2, session_gc_maxlifetime());
12131215

1214-
efree(skey);
1216+
zend_string_release(skey);
12151217

12161218
/* Attempt to kick off our command */
12171219
c->readonly = 0;
@@ -1252,10 +1254,12 @@ PS_CREATE_SID_FUNC(rediscluster)
12521254
*/
12531255
PS_VALIDATE_SID_FUNC(rediscluster)
12541256
{
1255-
redisCluster *c = PS_GET_MOD_DATA();
1257+
redis_cluster_session *sc = PS_GET_MOD_DATA();
1258+
redisCluster *c = sc->c;
12561259
clusterReply *reply;
1257-
char *cmd, *skey;
1258-
int cmdlen, skeylen;
1260+
char *cmd;
1261+
zend_string *skey;
1262+
int cmdlen;
12591263
int res = FAILURE;
12601264
short slot;
12611265

@@ -1265,9 +1269,9 @@ PS_VALIDATE_SID_FUNC(rediscluster)
12651269
return FAILURE;
12661270
}
12671271

1268-
skey = cluster_session_key(c, ZSTR_VAL(key), ZSTR_LEN(key), &skeylen, &slot);
1269-
cmdlen = redis_spprintf(NULL, NULL, &cmd, "EXISTS", "s", skey, skeylen);
1270-
efree(skey);
1272+
skey = cluster_session_key(c, ZSTR_VAL(key), ZSTR_LEN(key), &slot);
1273+
cmdlen = redis_spprintf(NULL, NULL, &cmd, "EXISTS", "S", skey);
1274+
zend_string_release(skey);
12711275

12721276
/* We send to master, to ensure consistency */
12731277
c->readonly = 0;
@@ -1301,26 +1305,25 @@ PS_VALIDATE_SID_FUNC(rediscluster)
13011305
/* {{{ PS_UPDATE_TIMESTAMP_FUNC
13021306
*/
13031307
PS_UPDATE_TIMESTAMP_FUNC(rediscluster) {
1304-
redisCluster *c = PS_GET_MOD_DATA();
1308+
redis_cluster_session *sc = PS_GET_MOD_DATA();
1309+
redisCluster *c = sc->c;
13051310
clusterReply *reply;
1306-
char *cmd, *skey;
1307-
int cmdlen, skeylen;
1308-
short slot;
1311+
char *cmd;
1312+
int cmdlen;
13091313

13101314
/* No need to update the session timestamp if we've already done so */
13111315
if (INI_INT("redis.session.early_refresh")) {
13121316
return SUCCESS;
13131317
}
13141318

13151319
/* Set up command and slot info */
1316-
skey = cluster_session_key(c, ZSTR_VAL(key), ZSTR_LEN(key), &skeylen, &slot);
1317-
cmdlen = redis_spprintf(NULL, NULL, &cmd, "EXPIRE", "sd", skey,
1318-
skeylen, session_gc_maxlifetime());
1319-
efree(skey);
1320+
set_cluster_session_key(sc, ZSTR_VAL(key), ZSTR_LEN(key));
1321+
cmdlen = redis_spprintf(NULL, NULL, &cmd, "EXPIRE", "Sd", sc->lock_status.session_key,
1322+
session_gc_maxlifetime());
13201323

13211324
/* Attempt to send EXPIRE command */
13221325
c->readonly = 0;
1323-
if (cluster_send_command(c,slot,cmd,cmdlen) < 0 || c->err) {
1326+
if (cluster_send_command(c,sc->slot,cmd,cmdlen) < 0 || c->err) {
13241327
php_error_docref(NULL, E_NOTICE, "Redis unable to update session expiry");
13251328
efree(cmd);
13261329
return FAILURE;
@@ -1354,9 +1357,7 @@ PS_READ_FUNC(rediscluster) {
13541357
short slot;
13551358

13561359
/* Set up our command and slot information */
1357-
if (sc->lock_status.session_key) zend_string_release(sc->lock_status.session_key);
1358-
sc->lock_status.session_key = cluster_session_key_new(c, ZSTR_VAL(key), ZSTR_LEN(key), &slot);
1359-
sc->slot = slot;
1360+
set_cluster_session_key(sc, ZSTR_VAL(key), ZSTR_LEN(key));
13601361

13611362
/* Update the session ttl if early refresh is enabled */
13621363
if (INI_INT("redis.session.early_refresh")) {
@@ -1375,7 +1376,7 @@ PS_READ_FUNC(rediscluster) {
13751376
}
13761377

13771378
/* Attempt to kick off our command */
1378-
if (cluster_send_command(c,slot,cmd,cmdlen) < 0 || c->err) {
1379+
if (cluster_send_command(c,sc->slot,cmd,cmdlen) < 0 || c->err) {
13791380
efree(cmd);
13801381
return FAILURE;
13811382
}
@@ -1453,13 +1454,12 @@ PS_DESTROY_FUNC(rediscluster) {
14531454
clusterReply *reply;
14541455
char *cmd;
14551456
int cmdlen;
1456-
short slot;
14571457

14581458
/* Set up command and slot info */
14591459
cmdlen = redis_spprintf(NULL, NULL, &cmd, "DEL", "S", sc->lock_status.session_key);
14601460

14611461
/* Attempt to send command */
1462-
if (cluster_send_command(c,slot,cmd,cmdlen) < 0 || c->err) {
1462+
if (cluster_send_command(c,sc->slot,cmd,cmdlen) < 0 || c->err) {
14631463
efree(cmd);
14641464
return FAILURE;
14651465
}

0 commit comments

Comments
 (0)
0