@@ -40,35 +40,35 @@ extern zend_class_entry *redis_ce;
40
40
extern zend_class_entry * redis_exception_ce ;
41
41
extern zend_class_entry * spl_ce_RuntimeException ;
42
42
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 ) {
45
45
char * cmd , * response ;
46
46
int cmd_len , response_len ;
47
47
48
48
cmd_len = redis_cmd_format_static (& cmd , "SELECT" , "d" , redis_sock -> dbNumber );
49
49
50
50
if (redis_sock_write (redis_sock , cmd , cmd_len TSRMLS_CC ) < 0 ) {
51
51
efree (cmd );
52
- return -1 ;
52
+ return FAILURE ;
53
53
}
54
54
55
55
efree (cmd );
56
56
57
57
if ((response = redis_sock_read (redis_sock , & response_len TSRMLS_CC )) == NULL ) {
58
- return -1 ;
58
+ return FAILURE ;
59
59
}
60
60
61
61
if (strncmp (response , "+OK" , 3 )) {
62
62
efree (response );
63
- return -1 ;
63
+ return FAILURE ;
64
64
}
65
65
66
66
efree (response );
67
- return 0 ;
67
+ return SUCCESS ;
68
68
}
69
69
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 ) {
72
72
char * cmd , * response ;
73
73
int cmd_len , response_len ;
74
74
@@ -77,23 +77,23 @@ static int resend_auth(RedisSock *redis_sock TSRMLS_DC) {
77
77
78
78
if (redis_sock_write (redis_sock , cmd , cmd_len TSRMLS_CC ) < 0 ) {
79
79
efree (cmd );
80
- return -1 ;
80
+ return FAILURE ;
81
81
}
82
82
83
83
efree (cmd );
84
84
85
85
response = redis_sock_read (redis_sock , & response_len TSRMLS_CC );
86
86
if (response == NULL ) {
87
- return -1 ;
87
+ return FAILURE ;
88
88
}
89
89
90
90
if (strncmp (response , "+OK" , 3 )) {
91
91
efree (response );
92
- return -1 ;
92
+ return FAILURE ;
93
93
}
94
94
95
95
efree (response );
96
- return 0 ;
96
+ return SUCCESS ;
97
97
}
98
98
99
99
/* 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)
178
178
/* We've connected if we have a count */
179
179
if (count ) {
180
180
/* 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 ) {
182
182
return -1 ;
183
183
}
184
184
185
185
/* 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 ) {
187
187
return -1 ;
188
188
}
189
189
}
@@ -1546,7 +1546,16 @@ redis_sock_create(char *host, int host_len, unsigned short port, double timeout,
1546
1546
RedisSock * redis_sock ;
1547
1547
1548
1548
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);
1550
1559
redis_sock -> stream = NULL ;
1551
1560
redis_sock -> status = REDIS_SOCK_STATUS_DISCONNECTED ;
1552
1561
redis_sock -> watching = 0 ;
@@ -1563,9 +1572,6 @@ redis_sock_create(char *host, int host_len, unsigned short port, double timeout,
1563
1572
redis_sock -> persistent_id = NULL ;
1564
1573
}
1565
1574
1566
- memcpy (redis_sock -> host , host , host_len );
1567
- redis_sock -> host [host_len ] = '\0' ;
1568
-
1569
1575
redis_sock -> port = port ;
1570
1576
redis_sock -> timeout = timeout ;
1571
1577
redis_sock -> read_timeout = timeout ;
@@ -1614,8 +1620,7 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
1614
1620
if (redis_sock -> host [0 ] == '/' && redis_sock -> port < 1 ) {
1615
1621
host_len = spprintf (& host , 0 , "unix://%s" , redis_sock -> host );
1616
1622
} else {
1617
- if (redis_sock -> port == 0 )
1618
- redis_sock -> port = 6379 ;
1623
+ if (redis_sock -> port == 0 ) redis_sock -> port = 6379 ;
1619
1624
host_len = spprintf (& host , 0 , "%s:%d" , redis_sock -> host ,
1620
1625
redis_sock -> port );
1621
1626
}
0 commit comments