Commit 886a04c2 authored by Yossi Gottlieb's avatar Yossi Gottlieb
Browse files

Merge commit '418de21d' into refresh-hiredis

parents e6478cfd 418de21d
...@@ -96,6 +96,8 @@ void freeReplyObject(void *reply) { ...@@ -96,6 +96,8 @@ void freeReplyObject(void *reply) {
switch(r->type) { switch(r->type) {
case REDIS_REPLY_INTEGER: case REDIS_REPLY_INTEGER:
case REDIS_REPLY_NIL:
case REDIS_REPLY_BOOL:
break; /* Nothing to free */ break; /* Nothing to free */
case REDIS_REPLY_ARRAY: case REDIS_REPLY_ARRAY:
case REDIS_REPLY_MAP: case REDIS_REPLY_MAP:
...@@ -112,6 +114,7 @@ void freeReplyObject(void *reply) { ...@@ -112,6 +114,7 @@ void freeReplyObject(void *reply) {
case REDIS_REPLY_STRING: case REDIS_REPLY_STRING:
case REDIS_REPLY_DOUBLE: case REDIS_REPLY_DOUBLE:
case REDIS_REPLY_VERB: case REDIS_REPLY_VERB:
case REDIS_REPLY_BIGNUM:
hi_free(r->str); hi_free(r->str);
break; break;
} }
...@@ -129,7 +132,8 @@ static void *createStringObject(const redisReadTask *task, char *str, size_t len ...@@ -129,7 +132,8 @@ static void *createStringObject(const redisReadTask *task, char *str, size_t len
assert(task->type == REDIS_REPLY_ERROR || assert(task->type == REDIS_REPLY_ERROR ||
task->type == REDIS_REPLY_STATUS || task->type == REDIS_REPLY_STATUS ||
task->type == REDIS_REPLY_STRING || task->type == REDIS_REPLY_STRING ||
task->type == REDIS_REPLY_VERB); task->type == REDIS_REPLY_VERB ||
task->type == REDIS_REPLY_BIGNUM);
/* Copy string value */ /* Copy string value */
if (task->type == REDIS_REPLY_VERB) { if (task->type == REDIS_REPLY_VERB) {
...@@ -235,12 +239,14 @@ static void *createDoubleObject(const redisReadTask *task, double value, char *s ...@@ -235,12 +239,14 @@ static void *createDoubleObject(const redisReadTask *task, double value, char *s
* decimal string conversion artifacts. */ * decimal string conversion artifacts. */
memcpy(r->str, str, len); memcpy(r->str, str, len);
r->str[len] = '\0'; r->str[len] = '\0';
r->len = len;
if (task->parent) { if (task->parent) {
parent = task->parent->obj; parent = task->parent->obj;
assert(parent->type == REDIS_REPLY_ARRAY || assert(parent->type == REDIS_REPLY_ARRAY ||
parent->type == REDIS_REPLY_MAP || parent->type == REDIS_REPLY_MAP ||
parent->type == REDIS_REPLY_SET); parent->type == REDIS_REPLY_SET ||
parent->type == REDIS_REPLY_PUSH);
parent->element[task->idx] = r; parent->element[task->idx] = r;
} }
return r; return r;
...@@ -277,7 +283,8 @@ static void *createBoolObject(const redisReadTask *task, int bval) { ...@@ -277,7 +283,8 @@ static void *createBoolObject(const redisReadTask *task, int bval) {
parent = task->parent->obj; parent = task->parent->obj;
assert(parent->type == REDIS_REPLY_ARRAY || assert(parent->type == REDIS_REPLY_ARRAY ||
parent->type == REDIS_REPLY_MAP || parent->type == REDIS_REPLY_MAP ||
parent->type == REDIS_REPLY_SET); parent->type == REDIS_REPLY_SET ||
parent->type == REDIS_REPLY_PUSH);
parent->element[task->idx] = r; parent->element[task->idx] = r;
} }
return r; return r;
...@@ -306,7 +313,7 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) { ...@@ -306,7 +313,7 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
const char *c = format; const char *c = format;
char *cmd = NULL; /* final command */ char *cmd = NULL; /* final command */
int pos; /* position in final command */ int pos; /* position in final command */
hisds curarg, newarg; /* current argument */ sds curarg, newarg; /* current argument */
int touched = 0; /* was the current argument touched? */ int touched = 0; /* was the current argument touched? */
char **curargv = NULL, **newargv = NULL; char **curargv = NULL, **newargv = NULL;
int argc = 0; int argc = 0;
...@@ -319,7 +326,7 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) { ...@@ -319,7 +326,7 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
return -1; return -1;
/* Build the command string accordingly to protocol */ /* Build the command string accordingly to protocol */
curarg = hi_sdsempty(); curarg = sdsempty();
if (curarg == NULL) if (curarg == NULL)
return -1; return -1;
...@@ -331,15 +338,15 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) { ...@@ -331,15 +338,15 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
if (newargv == NULL) goto memory_err; if (newargv == NULL) goto memory_err;
curargv = newargv; curargv = newargv;
curargv[argc++] = curarg; curargv[argc++] = curarg;
totlen += bulklen(hi_sdslen(curarg)); totlen += bulklen(sdslen(curarg));
/* curarg is put in argv so it can be overwritten. */ /* curarg is put in argv so it can be overwritten. */
curarg = hi_sdsempty(); curarg = sdsempty();
if (curarg == NULL) goto memory_err; if (curarg == NULL) goto memory_err;
touched = 0; touched = 0;
} }
} else { } else {
newarg = hi_sdscatlen(curarg,c,1); newarg = sdscatlen(curarg,c,1);
if (newarg == NULL) goto memory_err; if (newarg == NULL) goto memory_err;
curarg = newarg; curarg = newarg;
touched = 1; touched = 1;
...@@ -356,16 +363,16 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) { ...@@ -356,16 +363,16 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
arg = va_arg(ap,char*); arg = va_arg(ap,char*);
size = strlen(arg); size = strlen(arg);
if (size > 0) if (size > 0)
newarg = hi_sdscatlen(curarg,arg,size); newarg = sdscatlen(curarg,arg,size);
break; break;
case 'b': case 'b':
arg = va_arg(ap,char*); arg = va_arg(ap,char*);
size = va_arg(ap,size_t); size = va_arg(ap,size_t);
if (size > 0) if (size > 0)
newarg = hi_sdscatlen(curarg,arg,size); newarg = sdscatlen(curarg,arg,size);
break; break;
case '%': case '%':
newarg = hi_sdscat(curarg,"%"); newarg = sdscat(curarg,"%");
break; break;
default: default:
/* Try to detect printf format */ /* Try to detect printf format */
...@@ -453,7 +460,7 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) { ...@@ -453,7 +460,7 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
if (_l < sizeof(_format)-2) { if (_l < sizeof(_format)-2) {
memcpy(_format,c,_l); memcpy(_format,c,_l);
_format[_l] = '\0'; _format[_l] = '\0';
newarg = hi_sdscatvprintf(curarg,_format,_cpy); newarg = sdscatvprintf(curarg,_format,_cpy);
/* Update current position (note: outer blocks /* Update current position (note: outer blocks
* increment c twice so compensate here) */ * increment c twice so compensate here) */
...@@ -480,9 +487,9 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) { ...@@ -480,9 +487,9 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
if (newargv == NULL) goto memory_err; if (newargv == NULL) goto memory_err;
curargv = newargv; curargv = newargv;
curargv[argc++] = curarg; curargv[argc++] = curarg;
totlen += bulklen(hi_sdslen(curarg)); totlen += bulklen(sdslen(curarg));
} else { } else {
hi_sdsfree(curarg); sdsfree(curarg);
} }
/* Clear curarg because it was put in curargv or was free'd. */ /* Clear curarg because it was put in curargv or was free'd. */
...@@ -497,10 +504,10 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) { ...@@ -497,10 +504,10 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
pos = sprintf(cmd,"*%d\r\n",argc); pos = sprintf(cmd,"*%d\r\n",argc);
for (j = 0; j < argc; j++) { for (j = 0; j < argc; j++) {
pos += sprintf(cmd+pos,"$%zu\r\n",hi_sdslen(curargv[j])); pos += sprintf(cmd+pos,"$%zu\r\n",sdslen(curargv[j]));
memcpy(cmd+pos,curargv[j],hi_sdslen(curargv[j])); memcpy(cmd+pos,curargv[j],sdslen(curargv[j]));
pos += hi_sdslen(curargv[j]); pos += sdslen(curargv[j]);
hi_sdsfree(curargv[j]); sdsfree(curargv[j]);
cmd[pos++] = '\r'; cmd[pos++] = '\r';
cmd[pos++] = '\n'; cmd[pos++] = '\n';
} }
...@@ -522,11 +529,11 @@ memory_err: ...@@ -522,11 +529,11 @@ memory_err:
cleanup: cleanup:
if (curargv) { if (curargv) {
while(argc--) while(argc--)
hi_sdsfree(curargv[argc]); sdsfree(curargv[argc]);
hi_free(curargv); hi_free(curargv);
} }
hi_sdsfree(curarg); sdsfree(curarg);
hi_free(cmd); hi_free(cmd);
return error_type; return error_type;
...@@ -559,19 +566,18 @@ int redisFormatCommand(char **target, const char *format, ...) { ...@@ -559,19 +566,18 @@ int redisFormatCommand(char **target, const char *format, ...) {
return len; return len;
} }
/* Format a command according to the Redis protocol using an hisds string and /* Format a command according to the Redis protocol using an sds string and
* hi_sdscatfmt for the processing of arguments. This function takes the * sdscatfmt for the processing of arguments. This function takes the
* number of arguments, an array with arguments and an array with their * number of arguments, an array with arguments and an array with their
* lengths. If the latter is set to NULL, strlen will be used to compute the * lengths. If the latter is set to NULL, strlen will be used to compute the
* argument lengths. * argument lengths.
*/ */
int redisFormatSdsCommandArgv(hisds *target, int argc, const char **argv, long long redisFormatSdsCommandArgv(sds *target, int argc, const char **argv,
const size_t *argvlen) const size_t *argvlen)
{ {
hisds cmd, aux; sds cmd, aux;
unsigned long long totlen; unsigned long long totlen, len;
int j; int j;
size_t len;
/* Abort on a NULL target */ /* Abort on a NULL target */
if (target == NULL) if (target == NULL)
...@@ -585,36 +591,36 @@ int redisFormatSdsCommandArgv(hisds *target, int argc, const char **argv, ...@@ -585,36 +591,36 @@ int redisFormatSdsCommandArgv(hisds *target, int argc, const char **argv,
} }
/* Use an SDS string for command construction */ /* Use an SDS string for command construction */
cmd = hi_sdsempty(); cmd = sdsempty();
if (cmd == NULL) if (cmd == NULL)
return -1; return -1;
/* We already know how much storage we need */ /* We already know how much storage we need */
aux = hi_sdsMakeRoomFor(cmd, totlen); aux = sdsMakeRoomFor(cmd, totlen);
if (aux == NULL) { if (aux == NULL) {
hi_sdsfree(cmd); sdsfree(cmd);
return -1; return -1;
} }
cmd = aux; cmd = aux;
/* Construct command */ /* Construct command */
cmd = hi_sdscatfmt(cmd, "*%i\r\n", argc); cmd = sdscatfmt(cmd, "*%i\r\n", argc);
for (j=0; j < argc; j++) { for (j=0; j < argc; j++) {
len = argvlen ? argvlen[j] : strlen(argv[j]); len = argvlen ? argvlen[j] : strlen(argv[j]);
cmd = hi_sdscatfmt(cmd, "$%u\r\n", len); cmd = sdscatfmt(cmd, "$%U\r\n", len);
cmd = hi_sdscatlen(cmd, argv[j], len); cmd = sdscatlen(cmd, argv[j], len);
cmd = hi_sdscatlen(cmd, "\r\n", sizeof("\r\n")-1); cmd = sdscatlen(cmd, "\r\n", sizeof("\r\n")-1);
} }
assert(hi_sdslen(cmd)==totlen); assert(sdslen(cmd)==totlen);
*target = cmd; *target = cmd;
return totlen; return totlen;
} }
void redisFreeSdsCommand(hisds cmd) { void redisFreeSdsCommand(sds cmd) {
hi_sdsfree(cmd); sdsfree(cmd);
} }
/* Format a command according to the Redis protocol. This function takes the /* Format a command according to the Redis protocol. This function takes the
...@@ -622,11 +628,11 @@ void redisFreeSdsCommand(hisds cmd) { ...@@ -622,11 +628,11 @@ void redisFreeSdsCommand(hisds cmd) {
* lengths. If the latter is set to NULL, strlen will be used to compute the * lengths. If the latter is set to NULL, strlen will be used to compute the
* argument lengths. * argument lengths.
*/ */
int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen) { long long redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen) {
char *cmd = NULL; /* final command */ char *cmd = NULL; /* final command */
int pos; /* position in final command */ size_t pos; /* position in final command */
size_t len; size_t len, totlen;
int totlen, j; int j;
/* Abort on a NULL target */ /* Abort on a NULL target */
if (target == NULL) if (target == NULL)
...@@ -698,7 +704,7 @@ static redisContext *redisContextInit(void) { ...@@ -698,7 +704,7 @@ static redisContext *redisContextInit(void) {
c->funcs = &redisContextDefaultFuncs; c->funcs = &redisContextDefaultFuncs;
c->obuf = hi_sdsempty(); c->obuf = sdsempty();
c->reader = redisReaderCreate(); c->reader = redisReaderCreate();
c->fd = REDIS_INVALID_FD; c->fd = REDIS_INVALID_FD;
...@@ -715,7 +721,7 @@ void redisFree(redisContext *c) { ...@@ -715,7 +721,7 @@ void redisFree(redisContext *c) {
return; return;
redisNetClose(c); redisNetClose(c);
hi_sdsfree(c->obuf); sdsfree(c->obuf);
redisReaderFree(c->reader); redisReaderFree(c->reader);
hi_free(c->tcp.host); hi_free(c->tcp.host);
hi_free(c->tcp.source_addr); hi_free(c->tcp.source_addr);
...@@ -752,10 +758,10 @@ int redisReconnect(redisContext *c) { ...@@ -752,10 +758,10 @@ int redisReconnect(redisContext *c) {
redisNetClose(c); redisNetClose(c);
hi_sdsfree(c->obuf); sdsfree(c->obuf);
redisReaderFree(c->reader); redisReaderFree(c->reader);
c->obuf = hi_sdsempty(); c->obuf = sdsempty();
c->reader = redisReaderCreate(); c->reader = redisReaderCreate();
if (c->obuf == NULL || c->reader == NULL) { if (c->obuf == NULL || c->reader == NULL) {
...@@ -797,6 +803,9 @@ redisContext *redisConnectWithOptions(const redisOptions *options) { ...@@ -797,6 +803,9 @@ redisContext *redisConnectWithOptions(const redisOptions *options) {
if (options->options & REDIS_OPT_NOAUTOFREE) { if (options->options & REDIS_OPT_NOAUTOFREE) {
c->flags |= REDIS_NO_AUTO_FREE; c->flags |= REDIS_NO_AUTO_FREE;
} }
if (options->options & REDIS_OPT_NOAUTOFREEREPLIES) {
c->flags |= REDIS_NO_AUTO_FREE_REPLIES;
}
/* Set any user supplied RESP3 PUSH handler or use freeReplyObject /* Set any user supplied RESP3 PUSH handler or use freeReplyObject
* as a default unless specifically flagged that we don't want one. */ * as a default unless specifically flagged that we don't want one. */
...@@ -825,7 +834,7 @@ redisContext *redisConnectWithOptions(const redisOptions *options) { ...@@ -825,7 +834,7 @@ redisContext *redisConnectWithOptions(const redisOptions *options) {
c->fd = options->endpoint.fd; c->fd = options->endpoint.fd;
c->flags |= REDIS_CONNECTED; c->flags |= REDIS_CONNECTED;
} else { } else {
// Unknown type - FIXME - FREE redisFree(c);
return NULL; return NULL;
} }
...@@ -939,13 +948,11 @@ int redisBufferRead(redisContext *c) { ...@@ -939,13 +948,11 @@ int redisBufferRead(redisContext *c) {
return REDIS_ERR; return REDIS_ERR;
nread = c->funcs->read(c, buf, sizeof(buf)); nread = c->funcs->read(c, buf, sizeof(buf));
if (nread > 0) { if (nread < 0) {
if (redisReaderFeed(c->reader, buf, nread) != REDIS_OK) { return REDIS_ERR;
__redisSetError(c, c->reader->err, c->reader->errstr); }
return REDIS_ERR; if (nread > 0 && redisReaderFeed(c->reader, buf, nread) != REDIS_OK) {
} else { __redisSetError(c, c->reader->err, c->reader->errstr);
}
} else if (nread < 0) {
return REDIS_ERR; return REDIS_ERR;
} }
return REDIS_OK; return REDIS_OK;
...@@ -966,22 +973,22 @@ int redisBufferWrite(redisContext *c, int *done) { ...@@ -966,22 +973,22 @@ int redisBufferWrite(redisContext *c, int *done) {
if (c->err) if (c->err)
return REDIS_ERR; return REDIS_ERR;
if (hi_sdslen(c->obuf) > 0) { if (sdslen(c->obuf) > 0) {
ssize_t nwritten = c->funcs->write(c); ssize_t nwritten = c->funcs->write(c);
if (nwritten < 0) { if (nwritten < 0) {
return REDIS_ERR; return REDIS_ERR;
} else if (nwritten > 0) { } else if (nwritten > 0) {
if (nwritten == (ssize_t)hi_sdslen(c->obuf)) { if (nwritten == (ssize_t)sdslen(c->obuf)) {
hi_sdsfree(c->obuf); sdsfree(c->obuf);
c->obuf = hi_sdsempty(); c->obuf = sdsempty();
if (c->obuf == NULL) if (c->obuf == NULL)
goto oom; goto oom;
} else { } else {
if (hi_sdsrange(c->obuf,nwritten,-1) < 0) goto oom; if (sdsrange(c->obuf,nwritten,-1) < 0) goto oom;
} }
} }
} }
if (done != NULL) *done = (hi_sdslen(c->obuf) == 0); if (done != NULL) *done = (sdslen(c->obuf) == 0);
return REDIS_OK; return REDIS_OK;
oom: oom:
...@@ -989,17 +996,6 @@ oom: ...@@ -989,17 +996,6 @@ oom:
return REDIS_ERR; return REDIS_ERR;
} }
/* Internal helper function to try and get a reply from the reader,
* or set an error in the context otherwise. */
int redisGetReplyFromReader(redisContext *c, void **reply) {
if (redisReaderGetReply(c->reader,reply) == REDIS_ERR) {
__redisSetError(c,c->reader->err,c->reader->errstr);
return REDIS_ERR;
}
return REDIS_OK;
}
/* Internal helper that returns 1 if the reply was a RESP3 PUSH /* Internal helper that returns 1 if the reply was a RESP3 PUSH
* message and we handled it with a user-provided callback. */ * message and we handled it with a user-provided callback. */
static int redisHandledPushReply(redisContext *c, void *reply) { static int redisHandledPushReply(redisContext *c, void *reply) {
...@@ -1011,12 +1007,34 @@ static int redisHandledPushReply(redisContext *c, void *reply) { ...@@ -1011,12 +1007,34 @@ static int redisHandledPushReply(redisContext *c, void *reply) {
return 0; return 0;
} }
/* Get a reply from our reader or set an error in the context. */
int redisGetReplyFromReader(redisContext *c, void **reply) {
if (redisReaderGetReply(c->reader, reply) == REDIS_ERR) {
__redisSetError(c,c->reader->err,c->reader->errstr);
return REDIS_ERR;
}
return REDIS_OK;
}
/* Internal helper to get the next reply from our reader while handling
* any PUSH messages we encounter along the way. This is separate from
* redisGetReplyFromReader so as to not change its behavior. */
static int redisNextInBandReplyFromReader(redisContext *c, void **reply) {
do {
if (redisGetReplyFromReader(c, reply) == REDIS_ERR)
return REDIS_ERR;
} while (redisHandledPushReply(c, *reply));
return REDIS_OK;
}
int redisGetReply(redisContext *c, void **reply) { int redisGetReply(redisContext *c, void **reply) {
int wdone = 0; int wdone = 0;
void *aux = NULL; void *aux = NULL;
/* Try to read pending replies */ /* Try to read pending replies */
if (redisGetReplyFromReader(c,&aux) == REDIS_ERR) if (redisNextInBandReplyFromReader(c,&aux) == REDIS_ERR)
return REDIS_ERR; return REDIS_ERR;
/* For the blocking context, flush output buffer and read reply */ /* For the blocking context, flush output buffer and read reply */
...@@ -1032,12 +1050,8 @@ int redisGetReply(redisContext *c, void **reply) { ...@@ -1032,12 +1050,8 @@ int redisGetReply(redisContext *c, void **reply) {
if (redisBufferRead(c) == REDIS_ERR) if (redisBufferRead(c) == REDIS_ERR)
return REDIS_ERR; return REDIS_ERR;
/* We loop here in case the user has specified a RESP3 if (redisNextInBandReplyFromReader(c,&aux) == REDIS_ERR)
* PUSH handler (e.g. for client tracking). */ return REDIS_ERR;
do {
if (redisGetReplyFromReader(c,&aux) == REDIS_ERR)
return REDIS_ERR;
} while (redisHandledPushReply(c, aux));
} while (aux == NULL); } while (aux == NULL);
} }
...@@ -1059,9 +1073,9 @@ int redisGetReply(redisContext *c, void **reply) { ...@@ -1059,9 +1073,9 @@ int redisGetReply(redisContext *c, void **reply) {
* the reply (or replies in pub/sub). * the reply (or replies in pub/sub).
*/ */
int __redisAppendCommand(redisContext *c, const char *cmd, size_t len) { int __redisAppendCommand(redisContext *c, const char *cmd, size_t len) {
hisds newbuf; sds newbuf;
newbuf = hi_sdscatlen(c->obuf,cmd,len); newbuf = sdscatlen(c->obuf,cmd,len);
if (newbuf == NULL) { if (newbuf == NULL) {
__redisSetError(c,REDIS_ERR_OOM,"Out of memory"); __redisSetError(c,REDIS_ERR_OOM,"Out of memory");
return REDIS_ERR; return REDIS_ERR;
...@@ -1113,8 +1127,8 @@ int redisAppendCommand(redisContext *c, const char *format, ...) { ...@@ -1113,8 +1127,8 @@ int redisAppendCommand(redisContext *c, const char *format, ...) {
} }
int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen) { int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen) {
hisds cmd; sds cmd;
int len; long long len;
len = redisFormatSdsCommandArgv(&cmd,argc,argv,argvlen); len = redisFormatSdsCommandArgv(&cmd,argc,argv,argvlen);
if (len == -1) { if (len == -1) {
...@@ -1123,11 +1137,11 @@ int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const s ...@@ -1123,11 +1137,11 @@ int redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const s
} }
if (__redisAppendCommand(c,cmd,len) != REDIS_OK) { if (__redisAppendCommand(c,cmd,len) != REDIS_OK) {
hi_sdsfree(cmd); sdsfree(cmd);
return REDIS_ERR; return REDIS_ERR;
} }
hi_sdsfree(cmd); sdsfree(cmd);
return REDIS_OK; return REDIS_OK;
} }
......
...@@ -42,13 +42,13 @@ struct timeval; /* forward declaration */ ...@@ -42,13 +42,13 @@ struct timeval; /* forward declaration */
typedef long long ssize_t; typedef long long ssize_t;
#endif #endif
#include <stdint.h> /* uintXX_t, etc */ #include <stdint.h> /* uintXX_t, etc */
#include "sds.h" /* for hisds */ #include "sds.h" /* for sds */
#include "alloc.h" /* for allocation wrappers */ #include "alloc.h" /* for allocation wrappers */
#define HIREDIS_MAJOR 1 #define HIREDIS_MAJOR 1
#define HIREDIS_MINOR 0 #define HIREDIS_MINOR 0
#define HIREDIS_PATCH 0 #define HIREDIS_PATCH 3
#define HIREDIS_SONAME 1.0.0 #define HIREDIS_SONAME 1.0.3-dev
/* Connection type can be blocking or non-blocking and is set in the /* Connection type can be blocking or non-blocking and is set in the
* least significant bit of the flags field in redisContext. */ * least significant bit of the flags field in redisContext. */
...@@ -80,12 +80,18 @@ typedef long long ssize_t; ...@@ -80,12 +80,18 @@ typedef long long ssize_t;
/* Flag that is set when we should set SO_REUSEADDR before calling bind() */ /* Flag that is set when we should set SO_REUSEADDR before calling bind() */
#define REDIS_REUSEADDR 0x80 #define REDIS_REUSEADDR 0x80
/* Flag that is set when the async connection supports push replies. */
#define REDIS_SUPPORTS_PUSH 0x100
/** /**
* Flag that indicates the user does not want the context to * Flag that indicates the user does not want the context to
* be automatically freed upon error * be automatically freed upon error
*/ */
#define REDIS_NO_AUTO_FREE 0x200 #define REDIS_NO_AUTO_FREE 0x200
/* Flag that indicates the user does not want replies to be automatically freed */
#define REDIS_NO_AUTO_FREE_REPLIES 0x400
#define REDIS_KEEPALIVE_INTERVAL 15 /* seconds */ #define REDIS_KEEPALIVE_INTERVAL 15 /* seconds */
/* number of times we retry to connect in the case of EADDRNOTAVAIL and /* number of times we retry to connect in the case of EADDRNOTAVAIL and
...@@ -112,7 +118,8 @@ typedef struct redisReply { ...@@ -112,7 +118,8 @@ typedef struct redisReply {
double dval; /* The double when type is REDIS_REPLY_DOUBLE */ double dval; /* The double when type is REDIS_REPLY_DOUBLE */
size_t len; /* Length of string */ size_t len; /* Length of string */
char *str; /* Used for REDIS_REPLY_ERROR, REDIS_REPLY_STRING char *str; /* Used for REDIS_REPLY_ERROR, REDIS_REPLY_STRING
REDIS_REPLY_VERB, and REDIS_REPLY_DOUBLE (in additional to dval). */ REDIS_REPLY_VERB, REDIS_REPLY_DOUBLE (in additional to dval),
and REDIS_REPLY_BIGNUM. */
char vtype[4]; /* Used for REDIS_REPLY_VERB, contains the null char vtype[4]; /* Used for REDIS_REPLY_VERB, contains the null
terminated 3 character content type, such as "txt". */ terminated 3 character content type, such as "txt". */
size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */ size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
...@@ -127,10 +134,10 @@ void freeReplyObject(void *reply); ...@@ -127,10 +134,10 @@ void freeReplyObject(void *reply);
/* Functions to format a command according to the protocol. */ /* Functions to format a command according to the protocol. */
int redisvFormatCommand(char **target, const char *format, va_list ap); int redisvFormatCommand(char **target, const char *format, va_list ap);
int redisFormatCommand(char **target, const char *format, ...); int redisFormatCommand(char **target, const char *format, ...);
int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen); long long redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen);
int redisFormatSdsCommandArgv(hisds *target, int argc, const char ** argv, const size_t *argvlen); long long redisFormatSdsCommandArgv(sds *target, int argc, const char ** argv, const size_t *argvlen);
void redisFreeCommand(char *cmd); void redisFreeCommand(char *cmd);
void redisFreeSdsCommand(hisds cmd); void redisFreeSdsCommand(sds cmd);
enum redisConnectionType { enum redisConnectionType {
REDIS_CONN_TCP, REDIS_CONN_TCP,
...@@ -152,6 +159,11 @@ struct redisSsl; ...@@ -152,6 +159,11 @@ struct redisSsl;
/* Don't automatically intercept and free RESP3 PUSH replies. */ /* Don't automatically intercept and free RESP3 PUSH replies. */
#define REDIS_OPT_NO_PUSH_AUTOFREE 0x08 #define REDIS_OPT_NO_PUSH_AUTOFREE 0x08
/**
* Don't automatically free replies
*/
#define REDIS_OPT_NOAUTOFREEREPLIES 0x10
/* In Unix systems a file descriptor is a regular signed int, with -1 /* In Unix systems a file descriptor is a regular signed int, with -1
* representing an invalid descriptor. In Windows it is a SOCKET * representing an invalid descriptor. In Windows it is a SOCKET
* (32- or 64-bit unsigned integer depending on the architecture), where * (32- or 64-bit unsigned integer depending on the architecture), where
...@@ -255,7 +267,7 @@ typedef struct redisContext { ...@@ -255,7 +267,7 @@ typedef struct redisContext {
} unix_sock; } unix_sock;
/* For non-blocking connect */ /* For non-blocking connect */
struct sockadr *saddr; struct sockaddr *saddr;
size_t addrlen; size_t addrlen;
/* Optional data and corresponding destructor users can use to provide /* Optional data and corresponding destructor users can use to provide
......
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)\..\..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(MSBuildThisFileDirectory)\..\..\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
</Project>
\ No newline at end of file
...@@ -56,7 +56,9 @@ typedef enum { ...@@ -56,7 +56,9 @@ typedef enum {
REDIS_SSL_CTX_CERT_KEY_REQUIRED, /* Client cert and key must both be specified or skipped */ REDIS_SSL_CTX_CERT_KEY_REQUIRED, /* Client cert and key must both be specified or skipped */
REDIS_SSL_CTX_CA_CERT_LOAD_FAILED, /* Failed to load CA Certificate or CA Path */ REDIS_SSL_CTX_CA_CERT_LOAD_FAILED, /* Failed to load CA Certificate or CA Path */
REDIS_SSL_CTX_CLIENT_CERT_LOAD_FAILED, /* Failed to load client certificate */ REDIS_SSL_CTX_CLIENT_CERT_LOAD_FAILED, /* Failed to load client certificate */
REDIS_SSL_CTX_PRIVATE_KEY_LOAD_FAILED /* Failed to load private key */ REDIS_SSL_CTX_PRIVATE_KEY_LOAD_FAILED, /* Failed to load private key */
REDIS_SSL_CTX_OS_CERTSTORE_OPEN_FAILED, /* Failed to open system certifcate store */
REDIS_SSL_CTX_OS_CERT_ADD_FAILED /* Failed to add CA certificates obtained from system to the SSL context */
} redisSSLContextError; } redisSSLContextError;
/** /**
......
...@@ -80,7 +80,7 @@ ssize_t redisNetRead(redisContext *c, char *buf, size_t bufcap) { ...@@ -80,7 +80,7 @@ ssize_t redisNetRead(redisContext *c, char *buf, size_t bufcap) {
} }
ssize_t redisNetWrite(redisContext *c) { ssize_t redisNetWrite(redisContext *c) {
ssize_t nwritten = send(c->fd, c->obuf, hi_sdslen(c->obuf), 0); ssize_t nwritten = send(c->fd, c->obuf, sdslen(c->obuf), 0);
if (nwritten < 0) { if (nwritten < 0) {
if ((errno == EWOULDBLOCK && !(c->flags & REDIS_BLOCK)) || (errno == EINTR)) { if ((errno == EWOULDBLOCK && !(c->flags & REDIS_BLOCK)) || (errno == EINTR)) {
/* Try again later */ /* Try again later */
......
...@@ -59,7 +59,7 @@ static void __redisReaderSetError(redisReader *r, int type, const char *str) { ...@@ -59,7 +59,7 @@ static void __redisReaderSetError(redisReader *r, int type, const char *str) {
} }
/* Clear input buffer on errors. */ /* Clear input buffer on errors. */
hi_sdsfree(r->buf); sdsfree(r->buf);
r->buf = NULL; r->buf = NULL;
r->pos = r->len = 0; r->pos = r->len = 0;
...@@ -123,29 +123,28 @@ static char *readBytes(redisReader *r, unsigned int bytes) { ...@@ -123,29 +123,28 @@ static char *readBytes(redisReader *r, unsigned int bytes) {
/* Find pointer to \r\n. */ /* Find pointer to \r\n. */
static char *seekNewline(char *s, size_t len) { static char *seekNewline(char *s, size_t len) {
int pos = 0; char *ret;
int _len = len-1;
/* We cannot match with fewer than 2 bytes */
/* Position should be < len-1 because the character at "pos" should be if (len < 2)
* followed by a \n. Note that strchr cannot be used because it doesn't return NULL;
* allow to search a limited length and the buffer that is being searched
* might not have a trailing NULL character. */ /* Search up to len - 1 characters */
while (pos < _len) { len--;
while(pos < _len && s[pos] != '\r') pos++;
if (pos==_len) { /* Look for the \r */
/* Not found. */ while ((ret = memchr(s, '\r', len)) != NULL) {
return NULL; if (ret[1] == '\n') {
} else { /* Found. */
if (s[pos+1] == '\n') { break;
/* Found. */
return s+pos;
} else {
/* Continue searching. */
pos++;
}
} }
/* Continue searching. */
ret++;
len -= ret - s;
s = ret;
} }
return NULL;
return ret;
} }
/* Convert a string into a long long. Returns REDIS_OK if the string could be /* Convert a string into a long long. Returns REDIS_OK if the string could be
...@@ -274,60 +273,104 @@ static int processLineItem(redisReader *r) { ...@@ -274,60 +273,104 @@ static int processLineItem(redisReader *r) {
if ((p = readLine(r,&len)) != NULL) { if ((p = readLine(r,&len)) != NULL) {
if (cur->type == REDIS_REPLY_INTEGER) { if (cur->type == REDIS_REPLY_INTEGER) {
long long v;
if (string2ll(p, len, &v) == REDIS_ERR) {
__redisReaderSetError(r,REDIS_ERR_PROTOCOL,
"Bad integer value");
return REDIS_ERR;
}
if (r->fn && r->fn->createInteger) { if (r->fn && r->fn->createInteger) {
long long v;
if (string2ll(p, len, &v) == REDIS_ERR) {
__redisReaderSetError(r,REDIS_ERR_PROTOCOL,
"Bad integer value");
return REDIS_ERR;
}
obj = r->fn->createInteger(cur,v); obj = r->fn->createInteger(cur,v);
} else { } else {
obj = (void*)REDIS_REPLY_INTEGER; obj = (void*)REDIS_REPLY_INTEGER;
} }
} else if (cur->type == REDIS_REPLY_DOUBLE) { } else if (cur->type == REDIS_REPLY_DOUBLE) {
if (r->fn && r->fn->createDouble) { char buf[326], *eptr;
char buf[326], *eptr; double d;
double d;
if ((size_t)len >= sizeof(buf)) { if ((size_t)len >= sizeof(buf)) {
__redisReaderSetError(r,REDIS_ERR_PROTOCOL,
"Double value is too large");
return REDIS_ERR;
}
memcpy(buf,p,len);
buf[len] = '\0';
if (len == 3 && strcasecmp(buf,"inf") == 0) {
d = INFINITY; /* Positive infinite. */
} else if (len == 4 && strcasecmp(buf,"-inf") == 0) {
d = -INFINITY; /* Negative infinite. */
} else {
d = strtod((char*)buf,&eptr);
/* RESP3 only allows "inf", "-inf", and finite values, while
* strtod() allows other variations on infinity, NaN,
* etc. We explicity handle our two allowed infinite cases
* above, so strtod() should only result in finite values. */
if (buf[0] == '\0' || eptr != &buf[len] || !isfinite(d)) {
__redisReaderSetError(r,REDIS_ERR_PROTOCOL, __redisReaderSetError(r,REDIS_ERR_PROTOCOL,
"Double value is too large"); "Bad double value");
return REDIS_ERR; return REDIS_ERR;
} }
}
memcpy(buf,p,len); if (r->fn && r->fn->createDouble) {
buf[len] = '\0';
if (strcasecmp(buf,",inf") == 0) {
d = INFINITY; /* Positive infinite. */
} else if (strcasecmp(buf,",-inf") == 0) {
d = -INFINITY; /* Negative infinite. */
} else {
d = strtod((char*)buf,&eptr);
if (buf[0] == '\0' || eptr[0] != '\0' || isnan(d)) {
__redisReaderSetError(r,REDIS_ERR_PROTOCOL,
"Bad double value");
return REDIS_ERR;
}
}
obj = r->fn->createDouble(cur,d,buf,len); obj = r->fn->createDouble(cur,d,buf,len);
} else { } else {
obj = (void*)REDIS_REPLY_DOUBLE; obj = (void*)REDIS_REPLY_DOUBLE;
} }
} else if (cur->type == REDIS_REPLY_NIL) { } else if (cur->type == REDIS_REPLY_NIL) {
if (len != 0) {
__redisReaderSetError(r,REDIS_ERR_PROTOCOL,
"Bad nil value");
return REDIS_ERR;
}
if (r->fn && r->fn->createNil) if (r->fn && r->fn->createNil)
obj = r->fn->createNil(cur); obj = r->fn->createNil(cur);
else else
obj = (void*)REDIS_REPLY_NIL; obj = (void*)REDIS_REPLY_NIL;
} else if (cur->type == REDIS_REPLY_BOOL) { } else if (cur->type == REDIS_REPLY_BOOL) {
int bval = p[0] == 't' || p[0] == 'T'; int bval;
if (len != 1 || !strchr("tTfF", p[0])) {
__redisReaderSetError(r,REDIS_ERR_PROTOCOL,
"Bad bool value");
return REDIS_ERR;
}
bval = p[0] == 't' || p[0] == 'T';
if (r->fn && r->fn->createBool) if (r->fn && r->fn->createBool)
obj = r->fn->createBool(cur,bval); obj = r->fn->createBool(cur,bval);
else else
obj = (void*)REDIS_REPLY_BOOL; obj = (void*)REDIS_REPLY_BOOL;
} else if (cur->type == REDIS_REPLY_BIGNUM) {
/* Ensure all characters are decimal digits (with possible leading
* minus sign). */
for (int i = 0; i < len; i++) {
/* XXX Consider: Allow leading '+'? Error on leading '0's? */
if (i == 0 && p[0] == '-') continue;
if (p[i] < '0' || p[i] > '9') {
__redisReaderSetError(r,REDIS_ERR_PROTOCOL,
"Bad bignum value");
return REDIS_ERR;
}
}
if (r->fn && r->fn->createString)
obj = r->fn->createString(cur,p,len);
else
obj = (void*)REDIS_REPLY_BIGNUM;
} else { } else {
/* Type will be error or status. */ /* Type will be error or status. */
for (int i = 0; i < len; i++) {
if (p[i] == '\r' || p[i] == '\n') {
__redisReaderSetError(r,REDIS_ERR_PROTOCOL,
"Bad simple string value");
return REDIS_ERR;
}
}
if (r->fn && r->fn->createString) if (r->fn && r->fn->createString)
obj = r->fn->createString(cur,p,len); obj = r->fn->createString(cur,p,len);
else else
...@@ -453,7 +496,6 @@ static int processAggregateItem(redisReader *r) { ...@@ -453,7 +496,6 @@ static int processAggregateItem(redisReader *r) {
long long elements; long long elements;
int root = 0, len; int root = 0, len;
/* Set error for nested multi bulks with depth > 7 */
if (r->ridx == r->tasks - 1) { if (r->ridx == r->tasks - 1) {
if (redisReaderGrow(r) == REDIS_ERR) if (redisReaderGrow(r) == REDIS_ERR)
return REDIS_ERR; return REDIS_ERR;
...@@ -569,6 +611,9 @@ static int processItem(redisReader *r) { ...@@ -569,6 +611,9 @@ static int processItem(redisReader *r) {
case '>': case '>':
cur->type = REDIS_REPLY_PUSH; cur->type = REDIS_REPLY_PUSH;
break; break;
case '(':
cur->type = REDIS_REPLY_BIGNUM;
break;
default: default:
__redisReaderSetErrorProtocolByte(r,*p); __redisReaderSetErrorProtocolByte(r,*p);
return REDIS_ERR; return REDIS_ERR;
...@@ -587,6 +632,7 @@ static int processItem(redisReader *r) { ...@@ -587,6 +632,7 @@ static int processItem(redisReader *r) {
case REDIS_REPLY_DOUBLE: case REDIS_REPLY_DOUBLE:
case REDIS_REPLY_NIL: case REDIS_REPLY_NIL:
case REDIS_REPLY_BOOL: case REDIS_REPLY_BOOL:
case REDIS_REPLY_BIGNUM:
return processLineItem(r); return processLineItem(r);
case REDIS_REPLY_STRING: case REDIS_REPLY_STRING:
case REDIS_REPLY_VERB: case REDIS_REPLY_VERB:
...@@ -609,7 +655,7 @@ redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn) { ...@@ -609,7 +655,7 @@ redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn) {
if (r == NULL) if (r == NULL)
return NULL; return NULL;
r->buf = hi_sdsempty(); r->buf = sdsempty();
if (r->buf == NULL) if (r->buf == NULL)
goto oom; goto oom;
...@@ -650,12 +696,12 @@ void redisReaderFree(redisReader *r) { ...@@ -650,12 +696,12 @@ void redisReaderFree(redisReader *r) {
hi_free(r->task); hi_free(r->task);
} }
hi_sdsfree(r->buf); sdsfree(r->buf);
hi_free(r); hi_free(r);
} }
int redisReaderFeed(redisReader *r, const char *buf, size_t len) { int redisReaderFeed(redisReader *r, const char *buf, size_t len) {
hisds newbuf; sds newbuf;
/* Return early when this reader is in an erroneous state. */ /* Return early when this reader is in an erroneous state. */
if (r->err) if (r->err)
...@@ -664,19 +710,19 @@ int redisReaderFeed(redisReader *r, const char *buf, size_t len) { ...@@ -664,19 +710,19 @@ int redisReaderFeed(redisReader *r, const char *buf, size_t len) {
/* Copy the provided buffer. */ /* Copy the provided buffer. */
if (buf != NULL && len >= 1) { if (buf != NULL && len >= 1) {
/* Destroy internal buffer when it is empty and is quite large. */ /* Destroy internal buffer when it is empty and is quite large. */
if (r->len == 0 && r->maxbuf != 0 && hi_sdsavail(r->buf) > r->maxbuf) { if (r->len == 0 && r->maxbuf != 0 && sdsavail(r->buf) > r->maxbuf) {
hi_sdsfree(r->buf); sdsfree(r->buf);
r->buf = hi_sdsempty(); r->buf = sdsempty();
if (r->buf == 0) goto oom; if (r->buf == 0) goto oom;
r->pos = 0; r->pos = 0;
} }
newbuf = hi_sdscatlen(r->buf,buf,len); newbuf = sdscatlen(r->buf,buf,len);
if (newbuf == NULL) goto oom; if (newbuf == NULL) goto oom;
r->buf = newbuf; r->buf = newbuf;
r->len = hi_sdslen(r->buf); r->len = sdslen(r->buf);
} }
return REDIS_OK; return REDIS_OK;
...@@ -721,9 +767,9 @@ int redisReaderGetReply(redisReader *r, void **reply) { ...@@ -721,9 +767,9 @@ int redisReaderGetReply(redisReader *r, void **reply) {
/* Discard part of the buffer when we've consumed at least 1k, to avoid /* Discard part of the buffer when we've consumed at least 1k, to avoid
* doing unnecessary calls to memmove() in sds.c. */ * doing unnecessary calls to memmove() in sds.c. */
if (r->pos >= 1024) { if (r->pos >= 1024) {
if (hi_sdsrange(r->buf,r->pos,-1) < 0) return REDIS_ERR; if (sdsrange(r->buf,r->pos,-1) < 0) return REDIS_ERR;
r->pos = 0; r->pos = 0;
r->len = hi_sdslen(r->buf); r->len = sdslen(r->buf);
} }
/* Emit a reply when there is one. */ /* Emit a reply when there is one. */
......
This diff is collapsed.
...@@ -30,10 +30,10 @@ ...@@ -30,10 +30,10 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef HIREDIS_SDS_H #ifndef __SDS_H
#define HIREDIS_SDS_H #define __SDS_H
#define HI_SDS_MAX_PREALLOC (1024*1024) #define SDS_MAX_PREALLOC (1024*1024)
#ifdef _MSC_VER #ifdef _MSC_VER
#define __attribute__(x) #define __attribute__(x)
typedef long long ssize_t; typedef long long ssize_t;
...@@ -44,235 +44,235 @@ typedef long long ssize_t; ...@@ -44,235 +44,235 @@ typedef long long ssize_t;
#include <stdarg.h> #include <stdarg.h>
#include <stdint.h> #include <stdint.h>
typedef char *hisds; typedef char *sds;
/* Note: sdshdr5 is never used, we just access the flags byte directly. /* Note: sdshdr5 is never used, we just access the flags byte directly.
* However is here to document the layout of type 5 SDS strings. */ * However is here to document the layout of type 5 SDS strings. */
struct __attribute__ ((__packed__)) hisdshdr5 { struct __attribute__ ((__packed__)) sdshdr5 {
unsigned char flags; /* 3 lsb of type, and 5 msb of string length */ unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
char buf[]; char buf[];
}; };
struct __attribute__ ((__packed__)) hisdshdr8 { struct __attribute__ ((__packed__)) sdshdr8 {
uint8_t len; /* used */ uint8_t len; /* used */
uint8_t alloc; /* excluding the header and null terminator */ uint8_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[]; char buf[];
}; };
struct __attribute__ ((__packed__)) hisdshdr16 { struct __attribute__ ((__packed__)) sdshdr16 {
uint16_t len; /* used */ uint16_t len; /* used */
uint16_t alloc; /* excluding the header and null terminator */ uint16_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[]; char buf[];
}; };
struct __attribute__ ((__packed__)) hisdshdr32 { struct __attribute__ ((__packed__)) sdshdr32 {
uint32_t len; /* used */ uint32_t len; /* used */
uint32_t alloc; /* excluding the header and null terminator */ uint32_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[]; char buf[];
}; };
struct __attribute__ ((__packed__)) hisdshdr64 { struct __attribute__ ((__packed__)) sdshdr64 {
uint64_t len; /* used */ uint64_t len; /* used */
uint64_t alloc; /* excluding the header and null terminator */ uint64_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[]; char buf[];
}; };
#define HI_SDS_TYPE_5 0 #define SDS_TYPE_5 0
#define HI_SDS_TYPE_8 1 #define SDS_TYPE_8 1
#define HI_SDS_TYPE_16 2 #define SDS_TYPE_16 2
#define HI_SDS_TYPE_32 3 #define SDS_TYPE_32 3
#define HI_SDS_TYPE_64 4 #define SDS_TYPE_64 4
#define HI_SDS_TYPE_MASK 7 #define SDS_TYPE_MASK 7
#define HI_SDS_TYPE_BITS 3 #define SDS_TYPE_BITS 3
#define HI_SDS_HDR_VAR(T,s) struct hisdshdr##T *sh = (struct hisdshdr##T *)((s)-(sizeof(struct hisdshdr##T))); #define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T)));
#define HI_SDS_HDR(T,s) ((struct hisdshdr##T *)((s)-(sizeof(struct hisdshdr##T)))) #define SDS_HDR(T,s) ((struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T))))
#define HI_SDS_TYPE_5_LEN(f) ((f)>>HI_SDS_TYPE_BITS) #define SDS_TYPE_5_LEN(f) ((f)>>SDS_TYPE_BITS)
static inline size_t hi_sdslen(const hisds s) { static inline size_t sdslen(const sds s) {
unsigned char flags = s[-1]; unsigned char flags = s[-1];
switch(flags & HI_SDS_TYPE_MASK) { switch(flags&SDS_TYPE_MASK) {
case HI_SDS_TYPE_5: case SDS_TYPE_5:
return HI_SDS_TYPE_5_LEN(flags); return SDS_TYPE_5_LEN(flags);
case HI_SDS_TYPE_8: case SDS_TYPE_8:
return HI_SDS_HDR(8,s)->len; return SDS_HDR(8,s)->len;
case HI_SDS_TYPE_16: case SDS_TYPE_16:
return HI_SDS_HDR(16,s)->len; return SDS_HDR(16,s)->len;
case HI_SDS_TYPE_32: case SDS_TYPE_32:
return HI_SDS_HDR(32,s)->len; return SDS_HDR(32,s)->len;
case HI_SDS_TYPE_64: case SDS_TYPE_64:
return HI_SDS_HDR(64,s)->len; return SDS_HDR(64,s)->len;
} }
return 0; return 0;
} }
static inline size_t hi_sdsavail(const hisds s) { static inline size_t sdsavail(const sds s) {
unsigned char flags = s[-1]; unsigned char flags = s[-1];
switch(flags&HI_SDS_TYPE_MASK) { switch(flags&SDS_TYPE_MASK) {
case HI_SDS_TYPE_5: { case SDS_TYPE_5: {
return 0; return 0;
} }
case HI_SDS_TYPE_8: { case SDS_TYPE_8: {
HI_SDS_HDR_VAR(8,s); SDS_HDR_VAR(8,s);
return sh->alloc - sh->len; return sh->alloc - sh->len;
} }
case HI_SDS_TYPE_16: { case SDS_TYPE_16: {
HI_SDS_HDR_VAR(16,s); SDS_HDR_VAR(16,s);
return sh->alloc - sh->len; return sh->alloc - sh->len;
} }
case HI_SDS_TYPE_32: { case SDS_TYPE_32: {
HI_SDS_HDR_VAR(32,s); SDS_HDR_VAR(32,s);
return sh->alloc - sh->len; return sh->alloc - sh->len;
} }
case HI_SDS_TYPE_64: { case SDS_TYPE_64: {
HI_SDS_HDR_VAR(64,s); SDS_HDR_VAR(64,s);
return sh->alloc - sh->len; return sh->alloc - sh->len;
} }
} }
return 0; return 0;
} }
static inline void hi_sdssetlen(hisds s, size_t newlen) { static inline void sdssetlen(sds s, size_t newlen) {
unsigned char flags = s[-1]; unsigned char flags = s[-1];
switch(flags&HI_SDS_TYPE_MASK) { switch(flags&SDS_TYPE_MASK) {
case HI_SDS_TYPE_5: case SDS_TYPE_5:
{ {
unsigned char *fp = ((unsigned char*)s)-1; unsigned char *fp = ((unsigned char*)s)-1;
*fp = (unsigned char)(HI_SDS_TYPE_5 | (newlen << HI_SDS_TYPE_BITS)); *fp = (unsigned char)(SDS_TYPE_5 | (newlen << SDS_TYPE_BITS));
} }
break; break;
case HI_SDS_TYPE_8: case SDS_TYPE_8:
HI_SDS_HDR(8,s)->len = (uint8_t)newlen; SDS_HDR(8,s)->len = (uint8_t)newlen;
break; break;
case HI_SDS_TYPE_16: case SDS_TYPE_16:
HI_SDS_HDR(16,s)->len = (uint16_t)newlen; SDS_HDR(16,s)->len = (uint16_t)newlen;
break; break;
case HI_SDS_TYPE_32: case SDS_TYPE_32:
HI_SDS_HDR(32,s)->len = (uint32_t)newlen; SDS_HDR(32,s)->len = (uint32_t)newlen;
break; break;
case HI_SDS_TYPE_64: case SDS_TYPE_64:
HI_SDS_HDR(64,s)->len = (uint64_t)newlen; SDS_HDR(64,s)->len = (uint64_t)newlen;
break; break;
} }
} }
static inline void hi_sdsinclen(hisds s, size_t inc) { static inline void sdsinclen(sds s, size_t inc) {
unsigned char flags = s[-1]; unsigned char flags = s[-1];
switch(flags&HI_SDS_TYPE_MASK) { switch(flags&SDS_TYPE_MASK) {
case HI_SDS_TYPE_5: case SDS_TYPE_5:
{ {
unsigned char *fp = ((unsigned char*)s)-1; unsigned char *fp = ((unsigned char*)s)-1;
unsigned char newlen = HI_SDS_TYPE_5_LEN(flags)+(unsigned char)inc; unsigned char newlen = SDS_TYPE_5_LEN(flags)+(unsigned char)inc;
*fp = HI_SDS_TYPE_5 | (newlen << HI_SDS_TYPE_BITS); *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS);
} }
break; break;
case HI_SDS_TYPE_8: case SDS_TYPE_8:
HI_SDS_HDR(8,s)->len += (uint8_t)inc; SDS_HDR(8,s)->len += (uint8_t)inc;
break; break;
case HI_SDS_TYPE_16: case SDS_TYPE_16:
HI_SDS_HDR(16,s)->len += (uint16_t)inc; SDS_HDR(16,s)->len += (uint16_t)inc;
break; break;
case HI_SDS_TYPE_32: case SDS_TYPE_32:
HI_SDS_HDR(32,s)->len += (uint32_t)inc; SDS_HDR(32,s)->len += (uint32_t)inc;
break; break;
case HI_SDS_TYPE_64: case SDS_TYPE_64:
HI_SDS_HDR(64,s)->len += (uint64_t)inc; SDS_HDR(64,s)->len += (uint64_t)inc;
break; break;
} }
} }
/* hi_sdsalloc() = hi_sdsavail() + hi_sdslen() */ /* sdsalloc() = sdsavail() + sdslen() */
static inline size_t hi_sdsalloc(const hisds s) { static inline size_t sdsalloc(const sds s) {
unsigned char flags = s[-1]; unsigned char flags = s[-1];
switch(flags & HI_SDS_TYPE_MASK) { switch(flags&SDS_TYPE_MASK) {
case HI_SDS_TYPE_5: case SDS_TYPE_5:
return HI_SDS_TYPE_5_LEN(flags); return SDS_TYPE_5_LEN(flags);
case HI_SDS_TYPE_8: case SDS_TYPE_8:
return HI_SDS_HDR(8,s)->alloc; return SDS_HDR(8,s)->alloc;
case HI_SDS_TYPE_16: case SDS_TYPE_16:
return HI_SDS_HDR(16,s)->alloc; return SDS_HDR(16,s)->alloc;
case HI_SDS_TYPE_32: case SDS_TYPE_32:
return HI_SDS_HDR(32,s)->alloc; return SDS_HDR(32,s)->alloc;
case HI_SDS_TYPE_64: case SDS_TYPE_64:
return HI_SDS_HDR(64,s)->alloc; return SDS_HDR(64,s)->alloc;
} }
return 0; return 0;
} }
static inline void hi_sdssetalloc(hisds s, size_t newlen) { static inline void sdssetalloc(sds s, size_t newlen) {
unsigned char flags = s[-1]; unsigned char flags = s[-1];
switch(flags&HI_SDS_TYPE_MASK) { switch(flags&SDS_TYPE_MASK) {
case HI_SDS_TYPE_5: case SDS_TYPE_5:
/* Nothing to do, this type has no total allocation info. */ /* Nothing to do, this type has no total allocation info. */
break; break;
case HI_SDS_TYPE_8: case SDS_TYPE_8:
HI_SDS_HDR(8,s)->alloc = (uint8_t)newlen; SDS_HDR(8,s)->alloc = (uint8_t)newlen;
break; break;
case HI_SDS_TYPE_16: case SDS_TYPE_16:
HI_SDS_HDR(16,s)->alloc = (uint16_t)newlen; SDS_HDR(16,s)->alloc = (uint16_t)newlen;
break; break;
case HI_SDS_TYPE_32: case SDS_TYPE_32:
HI_SDS_HDR(32,s)->alloc = (uint32_t)newlen; SDS_HDR(32,s)->alloc = (uint32_t)newlen;
break; break;
case HI_SDS_TYPE_64: case SDS_TYPE_64:
HI_SDS_HDR(64,s)->alloc = (uint64_t)newlen; SDS_HDR(64,s)->alloc = (uint64_t)newlen;
break; break;
} }
} }
hisds hi_sdsnewlen(const void *init, size_t initlen); sds sdsnewlen(const void *init, size_t initlen);
hisds hi_sdsnew(const char *init); sds sdsnew(const char *init);
hisds hi_sdsempty(void); sds sdsempty(void);
hisds hi_sdsdup(const hisds s); sds sdsdup(const sds s);
void hi_sdsfree(hisds s); void sdsfree(sds s);
hisds hi_sdsgrowzero(hisds s, size_t len); sds sdsgrowzero(sds s, size_t len);
hisds hi_sdscatlen(hisds s, const void *t, size_t len); sds sdscatlen(sds s, const void *t, size_t len);
hisds hi_sdscat(hisds s, const char *t); sds sdscat(sds s, const char *t);
hisds hi_sdscatsds(hisds s, const hisds t); sds sdscatsds(sds s, const sds t);
hisds hi_sdscpylen(hisds s, const char *t, size_t len); sds sdscpylen(sds s, const char *t, size_t len);
hisds hi_sdscpy(hisds s, const char *t); sds sdscpy(sds s, const char *t);
hisds hi_sdscatvprintf(hisds s, const char *fmt, va_list ap); sds sdscatvprintf(sds s, const char *fmt, va_list ap);
#ifdef __GNUC__ #ifdef __GNUC__
hisds hi_sdscatprintf(hisds s, const char *fmt, ...) sds sdscatprintf(sds s, const char *fmt, ...)
__attribute__((format(printf, 2, 3))); __attribute__((format(printf, 2, 3)));
#else #else
hisds hi_sdscatprintf(hisds s, const char *fmt, ...); sds sdscatprintf(sds s, const char *fmt, ...);
#endif #endif
hisds hi_sdscatfmt(hisds s, char const *fmt, ...); sds sdscatfmt(sds s, char const *fmt, ...);
hisds hi_sdstrim(hisds s, const char *cset); sds sdstrim(sds s, const char *cset);
int hi_sdsrange(hisds s, ssize_t start, ssize_t end); int sdsrange(sds s, ssize_t start, ssize_t end);
void hi_sdsupdatelen(hisds s); void sdsupdatelen(sds s);
void hi_sdsclear(hisds s); void sdsclear(sds s);
int hi_sdscmp(const hisds s1, const hisds s2); int sdscmp(const sds s1, const sds s2);
hisds *hi_sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count); sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count);
void hi_sdsfreesplitres(hisds *tokens, int count); void sdsfreesplitres(sds *tokens, int count);
void hi_sdstolower(hisds s); void sdstolower(sds s);
void hi_sdstoupper(hisds s); void sdstoupper(sds s);
hisds hi_sdsfromlonglong(long long value); sds sdsfromlonglong(long long value);
hisds hi_sdscatrepr(hisds s, const char *p, size_t len); sds sdscatrepr(sds s, const char *p, size_t len);
hisds *hi_sdssplitargs(const char *line, int *argc); sds *sdssplitargs(const char *line, int *argc);
hisds hi_sdsmapchars(hisds s, const char *from, const char *to, size_t setlen); sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen);
hisds hi_sdsjoin(char **argv, int argc, char *sep); sds sdsjoin(char **argv, int argc, char *sep);
hisds hi_sdsjoinsds(hisds *argv, int argc, const char *sep, size_t seplen); sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen);
/* Low level functions exposed to the user API */ /* Low level functions exposed to the user API */
hisds hi_sdsMakeRoomFor(hisds s, size_t addlen); sds sdsMakeRoomFor(sds s, size_t addlen);
void hi_sdsIncrLen(hisds s, int incr); void sdsIncrLen(sds s, int incr);
hisds hi_sdsRemoveFreeSpace(hisds s); sds sdsRemoveFreeSpace(sds s);
size_t hi_sdsAllocSize(hisds s); size_t sdsAllocSize(sds s);
void *hi_sdsAllocPtr(hisds s); void *sdsAllocPtr(sds s);
/* Export the allocator used by SDS to the program using SDS. /* Export the allocator used by SDS to the program using SDS.
* Sometimes the program SDS is linked to, may use a different set of * Sometimes the program SDS is linked to, may use a different set of
* allocators, but may want to allocate or free things that SDS will * allocators, but may want to allocate or free things that SDS will
* respectively free or allocate. */ * respectively free or allocate. */
void *hi_sds_malloc(size_t size); void *sds_malloc(size_t size);
void *hi_sds_realloc(void *ptr, size_t size); void *sds_realloc(void *ptr, size_t size);
void hi_sds_free(void *ptr); void sds_free(void *ptr);
#ifdef REDIS_TEST #ifdef REDIS_TEST
int hi_sdsTest(int argc, char *argv[]); int sdsTest(int argc, char *argv[]);
#endif #endif
#endif /* HIREDIS_SDS_H */ #endif
...@@ -39,6 +39,6 @@ ...@@ -39,6 +39,6 @@
#include "alloc.h" #include "alloc.h"
#define hi_s_malloc hi_malloc #define s_malloc hi_malloc
#define hi_s_realloc hi_realloc #define s_realloc hi_realloc
#define hi_s_free hi_free #define s_free hi_free
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include <string.h> #include <string.h>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#include <wincrypt.h>
#else #else
#include <pthread.h> #include <pthread.h>
#endif #endif
...@@ -182,6 +183,10 @@ const char *redisSSLContextGetError(redisSSLContextError error) ...@@ -182,6 +183,10 @@ const char *redisSSLContextGetError(redisSSLContextError error)
return "Failed to load client certificate"; return "Failed to load client certificate";
case REDIS_SSL_CTX_PRIVATE_KEY_LOAD_FAILED: case REDIS_SSL_CTX_PRIVATE_KEY_LOAD_FAILED:
return "Failed to load private key"; return "Failed to load private key";
case REDIS_SSL_CTX_OS_CERTSTORE_OPEN_FAILED:
return "Failed to open system certifcate store";
case REDIS_SSL_CTX_OS_CERT_ADD_FAILED:
return "Failed to add CA certificates obtained from system to the SSL context";
default: default:
return "Unknown error code"; return "Unknown error code";
} }
...@@ -214,6 +219,11 @@ redisSSLContext *redisCreateSSLContext(const char *cacert_filename, const char * ...@@ -214,6 +219,11 @@ redisSSLContext *redisCreateSSLContext(const char *cacert_filename, const char *
const char *cert_filename, const char *private_key_filename, const char *cert_filename, const char *private_key_filename,
const char *server_name, redisSSLContextError *error) const char *server_name, redisSSLContextError *error)
{ {
#ifdef _WIN32
HCERTSTORE win_store = NULL;
PCCERT_CONTEXT win_ctx = NULL;
#endif
redisSSLContext *ctx = hi_calloc(1, sizeof(redisSSLContext)); redisSSLContext *ctx = hi_calloc(1, sizeof(redisSSLContext));
if (ctx == NULL) if (ctx == NULL)
goto error; goto error;
...@@ -234,6 +244,31 @@ redisSSLContext *redisCreateSSLContext(const char *cacert_filename, const char * ...@@ -234,6 +244,31 @@ redisSSLContext *redisCreateSSLContext(const char *cacert_filename, const char *
} }
if (capath || cacert_filename) { if (capath || cacert_filename) {
#ifdef _WIN32
if (0 == strcmp(cacert_filename, "wincert")) {
win_store = CertOpenSystemStore(NULL, "Root");
if (!win_store) {
if (error) *error = REDIS_SSL_CTX_OS_CERTSTORE_OPEN_FAILED;
goto error;
}
X509_STORE* store = SSL_CTX_get_cert_store(ctx->ssl_ctx);
while (win_ctx = CertEnumCertificatesInStore(win_store, win_ctx)) {
X509* x509 = NULL;
x509 = d2i_X509(NULL, (const unsigned char**)&win_ctx->pbCertEncoded, win_ctx->cbCertEncoded);
if (x509) {
if ((1 != X509_STORE_add_cert(store, x509)) ||
(1 != SSL_CTX_add_client_CA(ctx->ssl_ctx, x509)))
{
if (error) *error = REDIS_SSL_CTX_OS_CERT_ADD_FAILED;
goto error;
}
X509_free(x509);
}
}
CertFreeCertificateContext(win_ctx);
CertCloseStore(win_store, 0);
} else
#endif
if (!SSL_CTX_load_verify_locations(ctx->ssl_ctx, cacert_filename, capath)) { if (!SSL_CTX_load_verify_locations(ctx->ssl_ctx, cacert_filename, capath)) {
if (error) *error = REDIS_SSL_CTX_CA_CERT_LOAD_FAILED; if (error) *error = REDIS_SSL_CTX_CA_CERT_LOAD_FAILED;
goto error; goto error;
...@@ -257,6 +292,10 @@ redisSSLContext *redisCreateSSLContext(const char *cacert_filename, const char * ...@@ -257,6 +292,10 @@ redisSSLContext *redisCreateSSLContext(const char *cacert_filename, const char *
return ctx; return ctx;
error: error:
#ifdef _WIN32
CertFreeCertificateContext(win_ctx);
CertCloseStore(win_store, 0);
#endif
redisFreeSSLContext(ctx); redisFreeSSLContext(ctx);
return NULL; return NULL;
} }
...@@ -353,7 +392,11 @@ int redisInitiateSSLWithContext(redisContext *c, redisSSLContext *redis_ssl_ctx) ...@@ -353,7 +392,11 @@ int redisInitiateSSLWithContext(redisContext *c, redisSSLContext *redis_ssl_ctx)
} }
} }
return redisSSLConnect(c, ssl); if (redisSSLConnect(c, ssl) != REDIS_OK) {
goto error;
}
return REDIS_OK;
error: error:
if (ssl) if (ssl)
...@@ -437,7 +480,7 @@ static ssize_t redisSSLRead(redisContext *c, char *buf, size_t bufcap) { ...@@ -437,7 +480,7 @@ static ssize_t redisSSLRead(redisContext *c, char *buf, size_t bufcap) {
static ssize_t redisSSLWrite(redisContext *c) { static ssize_t redisSSLWrite(redisContext *c) {
redisSSL *rssl = c->privctx; redisSSL *rssl = c->privctx;
size_t len = rssl->lastLen ? rssl->lastLen : hi_sdslen(c->obuf); size_t len = rssl->lastLen ? rssl->lastLen : sdslen(c->obuf);
int rv = SSL_write(rssl->ssl, c->obuf, len); int rv = SSL_write(rssl->ssl, c->obuf, len);
if (rv > 0) { if (rv > 0) {
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment