Skip to content

Commit 4b8336f

Browse files
committed
Use zend_string for storing auth and prefix members
1 parent 4cf3414 commit 4b8336f

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

redis_session.c

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,8 @@ typedef struct redis_pool_member_ {
5353
RedisSock *redis_sock;
5454
int weight;
5555
int database;
56-
57-
char *prefix;
58-
size_t prefix_len;
59-
60-
char *auth;
61-
size_t auth_len;
62-
56+
zend_string *prefix;
57+
zend_string *auth;
6358
struct redis_pool_member_ *next;
6459

6560
} redis_pool_member;
@@ -80,18 +75,16 @@ redis_pool_new(TSRMLS_D) {
8075

8176
PHP_REDIS_API void
8277
redis_pool_add(redis_pool *pool, RedisSock *redis_sock, int weight,
83-
int database, char *prefix, char *auth TSRMLS_DC) {
78+
int database, zend_string *prefix, zend_string *auth TSRMLS_DC) {
8479

8580
redis_pool_member *rpm = ecalloc(1, sizeof(redis_pool_member));
8681
rpm->redis_sock = redis_sock;
8782
rpm->weight = weight;
8883
rpm->database = database;
8984

9085
rpm->prefix = prefix;
91-
rpm->prefix_len = (prefix?strlen(prefix):0);
9286

9387
rpm->auth = auth;
94-
rpm->auth_len = (auth?strlen(auth):0);
9588

9689
rpm->next = pool->head;
9790
pool->head = rpm;
@@ -108,8 +101,8 @@ redis_pool_free(redis_pool *pool TSRMLS_DC) {
108101
next = rpm->next;
109102
redis_sock_disconnect(rpm->redis_sock TSRMLS_CC);
110103
redis_free_socket(rpm->redis_sock);
111-
if(rpm->prefix) efree(rpm->prefix);
112-
if(rpm->auth) efree(rpm->auth);
104+
if(rpm->prefix) zend_string_release(rpm->prefix);
105+
if(rpm->auth) zend_string_release(rpm->auth);
113106
efree(rpm);
114107
rpm = next;
115108
}
@@ -123,11 +116,11 @@ redis_pool_member_auth(redis_pool_member *rpm TSRMLS_DC) {
123116
int response_len, cmd_len;
124117

125118
/* Short circuit if we don't have a password */
126-
if(!rpm->auth || !rpm->auth_len) {
119+
if (!rpm->auth) {
127120
return;
128121
}
129122

130-
cmd_len = REDIS_SPPRINTF(&cmd, "AUTH", "s", rpm->auth, rpm->auth_len);
123+
cmd_len = REDIS_SPPRINTF(&cmd, "AUTH", "S", rpm->auth);
131124
if(redis_sock_write(redis_sock, cmd, cmd_len TSRMLS_CC) >= 0) {
132125
if ((response = redis_sock_read(redis_sock, &response_len TSRMLS_CC))) {
133126
efree(response);
@@ -163,7 +156,7 @@ redis_pool_get_sock(redis_pool *pool, const char *key TSRMLS_DC) {
163156
for(i = 0; i < pool->totalWeight;) {
164157
if(pos >= i && pos < i + rpm->weight) {
165158
int needs_auth = 0;
166-
if(rpm->auth && rpm->auth_len && rpm->redis_sock->status != REDIS_SOCK_STATUS_CONNECTED) {
159+
if (rpm->auth && rpm->redis_sock->status != REDIS_SOCK_STATUS_CONNECTED) {
167160
needs_auth = 1;
168161
}
169162
redis_sock_server_open(rpm->redis_sock TSRMLS_CC);
@@ -208,7 +201,8 @@ PS_OPEN_FUNC(redis)
208201
double timeout = 86400.0, read_timeout = 0.0;
209202
int persistent = 0;
210203
int database = -1;
211-
char *prefix = NULL, *auth = NULL, *persistent_id = NULL;
204+
char *persistent_id = NULL;
205+
zend_string *prefix = NULL, *auth = NULL;
212206
long retry_interval = 0;
213207

214208
/* translate unix: into file: */
@@ -255,10 +249,10 @@ PS_OPEN_FUNC(redis)
255249
persistent_id = estrndup(Z_STRVAL_P(param), Z_STRLEN_P(param));
256250
}
257251
if ((param = zend_hash_str_find(Z_ARRVAL(params), "prefix", sizeof("prefix") - 1)) != NULL) {
258-
prefix = estrndup(Z_STRVAL_P(param), Z_STRLEN_P(param));
252+
prefix = zend_string_init(Z_STRVAL_P(param), Z_STRLEN_P(param), 0);
259253
}
260254
if ((param = zend_hash_str_find(Z_ARRVAL(params), "auth", sizeof("auth") - 1)) != NULL) {
261-
auth = estrndup(Z_STRVAL_P(param), Z_STRLEN_P(param));
255+
auth = zend_string_init(Z_STRVAL_P(param), Z_STRLEN_P(param), 0);
262256
}
263257
if ((param = zend_hash_str_find(Z_ARRVAL(params), "database", sizeof("database") - 1)) != NULL) {
264258
database = zval_get_long(param);
@@ -273,8 +267,8 @@ PS_OPEN_FUNC(redis)
273267
if ((url->path == NULL && url->host == NULL) || weight <= 0 || timeout <= 0) {
274268
php_url_free(url);
275269
if (persistent_id) efree(persistent_id);
276-
if (prefix) efree(prefix);
277-
if (auth) efree(auth);
270+
if (prefix) zend_string_release(prefix);
271+
if (auth) zend_string_release(auth);
278272
redis_pool_free(pool TSRMLS_CC);
279273
PS_SET_MOD_DATA(NULL);
280274
return FAILURE;
@@ -324,8 +318,8 @@ redis_session_key(redis_pool_member *rpm, const char *key, int key_len, int *ses
324318
size_t prefix_len = sizeof(default_prefix)-1;
325319

326320
if(rpm->prefix) {
327-
prefix = rpm->prefix;
328-
prefix_len = rpm->prefix_len;
321+
prefix = ZSTR_VAL(rpm->prefix);
322+
prefix_len = ZSTR_LEN(rpm->prefix);
329323
}
330324

331325
/* build session key */

0 commit comments

Comments
 (0)