Final touch to content-type feature.

master
Nicolas Favre-Felix 14 years ago
parent a3aa1a9225
commit 1abb4149f5

@ -147,3 +147,41 @@ $ curl http://127.0.0.1:7379/MAKE-ME-COFFEE?format=raw
-ERR unknown command 'MAKE-ME-COFFEE'
</pre>
# 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:**
<pre>
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
<
&lt;!DOCTYPE html&gt;
&lt;html&gt;
...
&lt;/html&gt;
</pre>
**Content-Type in a separate key:**
<pre>
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
<
&lt;!DOCTYPE html&gt;
&lt;html&gt;
...
&lt;/html&gt;
</pre>

@ -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]);
}

@ -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);
}
}

Loading…
Cancel
Save