From 1ad059d7b9992b910b16549571d6d481153d0ab5 Mon Sep 17 00:00:00 2001 From: Nicolas Favre-Felix Date: Mon, 3 Jan 2011 14:02:24 +0100 Subject: [PATCH] Started adding support for a custom content-type in a second key. --- Makefile | 2 +- README.markdown | 2 +- cmd.c | 6 ++++++ formats/custom-type.c | 44 +++++++++++++++++++++++++++++++++++++++++++ formats/custom-type.h | 12 ++++++++++++ 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 formats/custom-type.c create mode 100644 formats/custom-type.h diff --git a/Makefile b/Makefile index 01019588..617acbf5 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ OUT=webdis HIREDIS_OBJ=hiredis/hiredis.o hiredis/sds.o hiredis/net.o hiredis/async.o hiredis/dict.o JANSSON_OBJ=jansson/src/dump.o jansson/src/error.o jansson/src/hashtable.o jansson/src/load.o jansson/src/strbuffer.o jansson/src/utf.o jansson/src/value.o jansson/src/variadic.o -FORMAT_OBJS=formats/json.o formats/raw.o formats/common.o +FORMAT_OBJS=formats/json.o formats/raw.o formats/common.o formats/custom-type.o OBJS=webdis.o conf.o $(FORMAT_OBJS) cmd.o server.o $(HIREDIS_OBJ) $(JANSSON_OBJ) libb64/cencode.o acl.o CFLAGS=-O3 -Wall -Wextra -I. -Ijansson/src LDFLAGS=-levent diff --git a/README.markdown b/README.markdown index 5aa492e1..be27fe0e 100644 --- a/README.markdown +++ b/README.markdown @@ -25,9 +25,9 @@ curl -d "GET/hello" http://127.0.0.1:7379/ * Possible Redis authentication in the config file. * Pub/Sub using `Transfer-Encoding: chunked`, works with JSONP as well. Webdis can be used as a Comet server. * Drop privileges on startup. +* MIME type in a second key with `?format=custom&typeKey=` # Ideas, TODO... -* Add meta-data info per key (MIME type in a second key, for instance). * Support PUT, DELETE, HEAD, OPTIONS? How? For which commands? * MULTI/EXEC/DISCARD/WATCH are disabled at the moment; find a way to use them. * Add logs. diff --git a/cmd.c b/cmd.c index 51c11e5f..be96c95f 100644 --- a/cmd.c +++ b/cmd.c @@ -5,6 +5,7 @@ #include "formats/json.h" #include "formats/raw.h" +#include "formats/custom-type.h" #include #include @@ -134,6 +135,9 @@ cmd_run(struct server *s, struct evhttp_request *rq, cur_param++; } + /* MGET if */ + // if(cmd->arg_len[0] == 3 && strncasecmp(cmd->argv[0], "GET", 3) == 0 && ) { + redisAsyncCommandArgv(s->ac, fun, cmd, param_count, cmd->argv, cmd->argv_len); return 0; } @@ -152,6 +156,8 @@ get_formatting_function(struct evkeyvalq *params) { return raw_reply; } else if(strcmp(kv->value, "json") == 0) { return json_reply; + } else if(strcmp(kv->value, "custom") == 0) { + return custom_type_reply; } break; } diff --git a/formats/custom-type.c b/formats/custom-type.c new file mode 100644 index 00000000..7b79420b --- /dev/null +++ b/formats/custom-type.c @@ -0,0 +1,44 @@ +#include "custom-type.h" +#include "cmd.h" +#include "common.h" + +#include +#include +#include + +void +custom_type_reply(redisAsyncContext *c, void *r, void *privdata) { + + redisReply *reply = r; + struct cmd *cmd = privdata; + char *ct; + (void)c; + + evhttp_clear_headers(&cmd->uri_params); + + if (reply == NULL) { + evhttp_send_reply(cmd->rq, 404, "Not Found", NULL); + return; + } + + /* we expect array(string, string) */ + if(reply->type != REDIS_REPLY_ARRAY || reply->elements != 2) { + evhttp_send_reply(cmd->rq, 400, "Bad request", NULL); + return; + } + + if(reply->element[0]->type != REDIS_REPLY_STRING) { + evhttp_send_reply(cmd->rq, 400, "Bad request", NULL); + return; + } + + if(reply->element[1]->type != REDIS_REPLY_STRING) { + ct = reply->element[1]->str; + } else { + ct = "binary/octet-stream"; + } + + /* send reply */ + format_send_reply(cmd, reply->element[0]->str, reply->element[0]->len, ct); +} + diff --git a/formats/custom-type.h b/formats/custom-type.h new file mode 100644 index 00000000..cb5b7bee --- /dev/null +++ b/formats/custom-type.h @@ -0,0 +1,12 @@ +#ifndef CUSTOM_TYPE_H +#define CUSTOM_TYPE_H + +#include +#include + +struct cmd; + +void +custom_type_reply(redisAsyncContext *c, void *r, void *privdata); + +#endif