From e2911703855cd499e3323fcb750eac3847d1e18f Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 10 Dec 2018 16:55:20 +0100 Subject: [PATCH] RESP3: verbatim reply API + DEBUG PROTOCOL support. --- src/debug.c | 2 ++ src/networking.c | 29 +++++++++++++++++++++++++++++ src/server.h | 1 + 3 files changed, 32 insertions(+) diff --git a/src/debug.c b/src/debug.c index d1824dfd..6cc734c3 100644 --- a/src/debug.c +++ b/src/debug.c @@ -576,6 +576,8 @@ NULL addReplyBool(c,1); } else if (!strcasecmp(name,"false")) { addReplyBool(c,0); + } else if (!strcasecmp(name,"verbatim")) { + addReplyVerbatim(c,"This is a verbatim\nstring",25,"txt"); } else { addReplyError(c,"Wrong protocol type name. Please use one of the following: string|integer|double|bignum|null|array|set|map|attrib|push|verbatim|true|false|state|err|bloberr"); } diff --git a/src/networking.c b/src/networking.c index 75312a1f..6b13db8e 100644 --- a/src/networking.c +++ b/src/networking.c @@ -696,6 +696,35 @@ void addReplyBulkLongLong(client *c, long long ll) { addReplyBulkCBuffer(c,buf,len); } +/* Reply with a verbatim type having the specified extension. + * + * The 'ext' is the "extension" of the file, actually just a three + * character type that describes the format of the verbatim string. + * For instance "txt" means it should be interpreted as a text only + * file by the receiver, "md " as markdown, and so forth. Only the + * three first characters of the extension are used, and if the + * provided one is shorter than that, the remaining is filled with + * spaces. */ +void addReplyVerbatim(client *c, const char *s, size_t len, const char *ext) { + if (c->resp == 2) { + addReplyBulkCBuffer(c,s,len); + } else { + char buf[32]; + size_t preflen = snprintf(buf,sizeof(buf),"=%zu\r\nxxx:",len+4); + char *p = buf+preflen-4; + for (int i = 0; i < 3; i++) { + if (*ext == '\0') { + p[i] = ' '; + } else { + p[i] = *ext++; + } + } + addReplyString(c,buf,preflen); + addReplyString(c,s,len); + addReplyString(c,"\r\n",2); + } +} + /* Add an array of C strings as status replies with a heading. * This function is typically invoked by from commands that support * subcommands in response to the 'help' subcommand. The help array diff --git a/src/server.h b/src/server.h index 0362c03a..8c1e6a77 100644 --- a/src/server.h +++ b/src/server.h @@ -1440,6 +1440,7 @@ void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask); void addReplyNull(client *c); void addReplyNullArray(client *c); void addReplyBool(client *c, int b); +void addReplyVerbatim(client *c, const char *s, size_t len, const char *ext); void addReplyString(client *c, const char *s, size_t len); void addReplyBulk(client *c, robj *obj); void addReplyBulkCString(client *c, const char *s);