Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 259f103

Browse files
Initial commit for redis:// scheme support
This commit adds support for using the redis:// scheme protocol to connect to a redis server, described here: http://www.iana.org/assignments/uri-schemes/prov/redis There are multiple ways to specify password and database number (both in the uri prefix) as well as in the query arguments, and the spec is ambiguous here so phpredis will just use whichever one we parse last (which for us is the query arguments). In addition, this commit changes $redis->connect() such that it can now be called with no arguments, in which case phpredis will attempt to connect to a localhost instance of Redis at the default port of 6379. I made this change because that's how the redis:// scheme protocol is designed to work, and it should be consistent.
1 parent 05e38d5 commit 259f103

File tree

4 files changed

+236
-44
lines changed

4 files changed

+236
-44
lines changed

common.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ typedef enum _PUBSUB_TYPE {
247247
#define IS_LEX_ARG(s,l) \
248248
(l>0 && (*s=='(' || *s=='[' || (l==1 && (*s=='+' || *s=='-'))))
249249

250+
/* Determine if a string starts with the reids scheme prefix */
251+
#define IS_SCHEME_PREFIX(s,l) \
252+
(l >= sizeof("redis://") && !strncasecmp(s, "redis://", sizeof("redis://")-1))
253+
254+
/* Simple helper macro for calling zend_hash_find with a static string */
255+
#define ZEND_HASH_FIND_STATIC(ht_arr, key, retval) \
256+
zend_hash_find(ht_arr, key, sizeof(key), (void**)&retval)
257+
250258
typedef enum {ATOMIC, MULTI, PIPELINE} redis_mode;
251259

252260
typedef struct fold_item {

library.c

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,35 @@ extern zend_class_entry *redis_ce;
4040
extern zend_class_entry *redis_exception_ce;
4141
extern zend_class_entry *spl_ce_RuntimeException;
4242

43-
/* Helper to reselect the proper DB number when we reconnect */
44-
static int reselect_db(RedisSock *redis_sock TSRMLS_DC) {
43+
/* Helper to select the proper DB number when we connect or reconnect */
44+
PHP_REDIS_API int redis_send_select(RedisSock *redis_sock TSRMLS_DC) {
4545
char *cmd, *response;
4646
int cmd_len, response_len;
4747

4848
cmd_len = redis_cmd_format_static(&cmd, "SELECT", "d", redis_sock->dbNumber);
4949

5050
if (redis_sock_write(redis_sock, cmd, cmd_len TSRMLS_CC) < 0) {
5151
efree(cmd);
52-
return -1;
52+
return FAILURE;
5353
}
5454

5555
efree(cmd);
5656

5757
if ((response = redis_sock_read(redis_sock, &response_len TSRMLS_CC)) == NULL) {
58-
return -1;
58+
return FAILURE;
5959
}
6060

6161
if (strncmp(response, "+OK", 3)) {
6262
efree(response);
63-
return -1;
63+
return FAILURE;
6464
}
6565

6666
efree(response);
67-
return 0;
67+
return SUCCESS;
6868
}
6969

70-
/* Helper to resend AUTH <password> in the case of a reconnect */
71-
static int resend_auth(RedisSock *redis_sock TSRMLS_DC) {
70+
/* Helper to send or resend AUTH <password> on connect or reconnect */
71+
PHP_REDIS_API int redis_send_auth(RedisSock *redis_sock TSRMLS_DC) {
7272
char *cmd, *response;
7373
int cmd_len, response_len;
7474

@@ -77,23 +77,23 @@ static int resend_auth(RedisSock *redis_sock TSRMLS_DC) {
7777

7878
if (redis_sock_write(redis_sock, cmd, cmd_len TSRMLS_CC) < 0) {
7979
efree(cmd);
80-
return -1;
80+
return FAILURE;
8181
}
8282

8383
efree(cmd);
8484

8585
response = redis_sock_read(redis_sock, &response_len TSRMLS_CC);
8686
if (response == NULL) {
87-
return -1;
87+
return FAILURE;
8888
}
8989

9090
if (strncmp(response, "+OK", 3)) {
9191
efree(response);
92-
return -1;
92+
return FAILURE;
9393
}
9494

9595
efree(response);
96-
return 0;
96+
return SUCCESS;
9797
}
9898

9999
/* Helper function that will throw an exception for a small number of ERR codes
@@ -178,12 +178,12 @@ PHP_REDIS_API int redis_check_eof(RedisSock *redis_sock, int no_throw TSRMLS_DC)
178178
/* We've connected if we have a count */
179179
if (count) {
180180
/* If we're using a password, attempt a reauthorization */
181-
if (redis_sock->auth && resend_auth(redis_sock TSRMLS_CC) != 0) {
181+
if (redis_sock->auth && redis_send_auth(redis_sock TSRMLS_CC) != SUCCESS) {
182182
return -1;
183183
}
184184

185185
/* If we're using a non-zero db, reselect it */
186-
if (redis_sock->dbNumber && reselect_db(redis_sock TSRMLS_CC) != 0) {
186+
if (redis_sock->dbNumber && redis_send_select(redis_sock TSRMLS_CC) != SUCCESS) {
187187
return -1;
188188
}
189189
}
@@ -1546,7 +1546,16 @@ redis_sock_create(char *host, int host_len, unsigned short port, double timeout,
15461546
RedisSock *redis_sock;
15471547

15481548
redis_sock = ecalloc(1, sizeof(RedisSock));
1549-
redis_sock->host = estrndup(host, host_len);
1549+
1550+
/* If an empty host is provided, default to "localhost". This conforms
1551+
* with other Redis libraries and is how the redis:// scheme works. */
1552+
if(host && host_len) {
1553+
redis_sock->host = estrndup(host, host_len);
1554+
} else {
1555+
redis_sock->host = estrndup("localhost", sizeof("localhost")-1);
1556+
}
1557+
1558+
//redis_sock->host = estrndup(host, host_len);
15501559
redis_sock->stream = NULL;
15511560
redis_sock->status = REDIS_SOCK_STATUS_DISCONNECTED;
15521561
redis_sock->watching = 0;
@@ -1563,9 +1572,6 @@ redis_sock_create(char *host, int host_len, unsigned short port, double timeout,
15631572
redis_sock->persistent_id = NULL;
15641573
}
15651574

1566-
memcpy(redis_sock->host, host, host_len);
1567-
redis_sock->host[host_len] = '\0';
1568-
15691575
redis_sock->port = port;
15701576
redis_sock->timeout = timeout;
15711577
redis_sock->read_timeout = timeout;
@@ -1614,8 +1620,7 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
16141620
if(redis_sock->host[0] == '/' && redis_sock->port < 1) {
16151621
host_len = spprintf(&host, 0, "unix://%s", redis_sock->host);
16161622
} else {
1617-
if(redis_sock->port == 0)
1618-
redis_sock->port = 6379;
1623+
if(redis_sock->port == 0) redis_sock->port = 6379;
16191624
host_len = spprintf(&host, 0, "%s:%d", redis_sock->host,
16201625
redis_sock->port);
16211626
}

library.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ redis_unserialize(RedisSock *redis_sock, const char *val, int val_len, zval **re
6969

7070
PHP_REDIS_API void redis_free_socket(RedisSock *redis_sock);
7171
PHP_REDIS_API void redis_send_discard(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock);
72+
PHP_REDIS_API int redis_send_auth(RedisSock *redis_sock TSRMLS_DC);
73+
PHP_REDIS_API int redis_send_select(RedisSock *redis_sock TSRMLS_DC);
7274
PHP_REDIS_API int redis_sock_set_err(RedisSock *redis_sock, const char *msg, int msg_len);
7375

7476

0 commit comments

Comments
 (0)