Added a few content types, added support for ?type.

master
Nicolas Favre-Felix 14 years ago
parent 102f9fcd12
commit 71bc9e39f0

@ -25,7 +25,7 @@ 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.
* Custom Content-Type using a pre-defined file extension.
* Custom Content-Type using a pre-defined file extension, or with `?type=some/thing`.
* URL-encoded parameters for binary data or slashes. For instance, `%2f` is decoded as `/` but not used as a command separator.
# Ideas, TODO...
@ -157,7 +157,11 @@ Several content-types are available:
* `.json` for `application/json` (this is the default Content-Type).
* `.txt` for `text/plain`
* `.html` for `text/html`
* `xhtml` for `application/xhtml+xml`
* `xml` for `text/xml`
* `.png` for `image/png`
* `jpg` or `jpeg` for `image/jpeg`
* Any other with the `?type=anything/youwant` query string.
<pre>
curl -v "http://127.0.0.1:7379/GET/hello.html" # the key is “hello” here, not “hello.html”

29
cmd.c

@ -172,7 +172,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 < cmd->count; ++i) {
for(i = 1; i < cur_param; ++i) {
free((char*)cmd->argv[i]);
}
@ -186,7 +186,8 @@ cmd_run(struct server *s, struct evhttp_request *rq,
int
cmd_select_format(struct cmd *cmd, const char *uri, size_t uri_len, formatting_fun *f_format) {
const char *ext, *accept_ct;
struct evkeyval *kv;
const char *ext;
int ext_len = -1;
unsigned int i;
@ -202,19 +203,17 @@ cmd_select_format(struct cmd *cmd, const char *uri, size_t uri_len, formatting_f
{.s = "raw", .sz = 3, .f = raw_reply, .ct = "binary/octet-stream"},
{.s = "txt", .sz = 3, .f = custom_type_reply, .ct = "text/plain"},
{.s = "html", .sz = 4, .f = custom_type_reply, .ct = "text/html"},
{.s = "xhtml", .sz = 5, .f = custom_type_reply, .ct = "application/xhtml+xml"},
{.s = "xml", .sz = 3, .f = custom_type_reply, .ct = "text/xml"},
{.s = "png", .sz = 3, .f = custom_type_reply, .ct = "image/png"},
{.s = "jpg", .sz = 3, .f = custom_type_reply, .ct = "image/jpeg"},
{.s = "jpeg", .sz = 4, .f = custom_type_reply, .ct = "image/jpeg"},
};
/* default */
*f_format = json_reply;
/* if there is an Accept header, use it */
if((accept_ct = evhttp_find_header(cmd->rq->input_headers, "Accept"))) {
cmd->mime = strdup(accept_ct);
cmd->mime_free = 1;
// printf("Accept: [%s]\n", cmd->mime);
}
/* find extension */
for(ext = uri + uri_len - 1; ext != uri && *ext != '/'; --ext) {
if(*ext == '.') {
@ -236,6 +235,18 @@ cmd_select_format(struct cmd *cmd, const char *uri, size_t uri_len, formatting_f
*f_format = funs[i].f;
}
}
/* the user can force it with ?type=some/thing */
TAILQ_FOREACH(kv, &cmd->uri_params, next) {
if(strcmp(kv->key, "type") == 0) {
*f_format = custom_type_reply;
cmd->mime = strdup(kv->value);
cmd->mime_free = 1;
break;
}
}
return uri_len - ext_len - 1;
}

Loading…
Cancel
Save