diff --git a/README.markdown b/README.markdown index 73a1434..bd2b044 100644 --- a/README.markdown +++ b/README.markdown @@ -147,3 +147,41 @@ $ curl http://127.0.0.1:7379/MAKE-ME-COFFEE?format=raw -ERR unknown command 'MAKE-ME-COFFEE' + +# Custom content-type +Webdis can serve `GET` requests with a custom content-type. There are two ways of doing this; the content-type can be in a key that is fetched with the content, or given as a query string parameter. + +**Content-Type in parameter:** +
+curl -v "http://127.0.0.1:7379/GET/hello.html?type=text/html"
+[...]
+< HTTP/1.1 200 OK
+< Content-Type: text/html
+< Date: Mon, 03 Jan 2011 20:43:36 GMT
+< Content-Length: 137
+<
+<!DOCTYPE html>
+<html>
+...
+</html>
+
+ +**Content-Type in a separate key:** +
+curl "http://127.0.0.1:7379/SET/hello.type/text%2fhtml"
+{"SET":[true,"OK"]}
+
+curl "http://127.0.0.1:7379/GET/hello.type"
+{"GET":"text/html"}
+
+curl -v "http://127.0.0.1:7379/GET/hello.html?typeKey=hello.type"
+< HTTP/1.1 200 OK
+< Content-Type: text/html
+< Date: Mon, 03 Jan 2011 20:56:43 GMT
+< Content-Length: 137
+<
+<!DOCTYPE html>
+<html>
+...
+</html>
+
diff --git a/cmd.c b/cmd.c index e450ce3..742cadd 100644 --- a/cmd.c +++ b/cmd.c @@ -20,8 +20,8 @@ cmd_new(struct evhttp_request *rq, int count) { c->rq = rq; c->count = count; - c->argv = calloc(1+count, sizeof(char*)); - c->argv_len = calloc(1+count, sizeof(size_t)); + c->argv = calloc(count, sizeof(char*)); + c->argv_len = calloc(count, sizeof(size_t)); return c; } @@ -176,7 +176,7 @@ cmd_run(struct server *s, struct evhttp_request *rq, /* push command to Redis. */ redisAsyncCommandArgv(s->ac, f_format, cmd, cmd->count, cmd->argv, cmd->argv_len); - for(i = 1; i < cur_param; ++i) { + for(i = 1; i < cmd->count; ++i) { free((char*)cmd->argv[i]); } diff --git a/formats/custom-type.c b/formats/custom-type.c index abc6bc3..39baff9 100644 --- a/formats/custom-type.c +++ b/formats/custom-type.c @@ -58,16 +58,19 @@ custom_type_reply(redisAsyncContext *c, void *r, void *privdata) { void custom_type_process_cmd(struct cmd *cmd) { /* MGET if mode is “custom” */ - if(cmd->argv_len[0] == 3 && strncasecmp(cmd->argv[0], "GET", 3) == 0 && cmd->mimeKey) { + if(cmd->count == 2 && cmd->argv_len[0] == 3 && + strncasecmp(cmd->argv[0], "GET", 3) == 0 && cmd->mimeKey) { cmd->count++; /* space for content-type key */ + cmd->argv = realloc(cmd->argv, cmd->count * sizeof(char*)); + cmd->argv_len = realloc(cmd->argv_len, cmd->count * sizeof(size_t)); /* replace command with MGET */ cmd->argv[0] = "MGET"; cmd->argv_len[0] = 4; /* add mime key after the key. */ - cmd->argv[2] = cmd->mimeKey; + cmd->argv[2] = strdup(cmd->mimeKey); cmd->argv_len[2] = strlen(cmd->mimeKey); } }