Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit 71bc9e39f0e91a0381d5e3dc2ee1f1f05844561a 1 parent 102f9fc
@nicolasff authored
Showing with 25 additions and 10 deletions.
  1. +5 −1 README.markdown
  2. +20 −9 cmd.c
View
6 README.markdown
@@ -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”
View
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;
}
Please sign in to comment.
Something went wrong with that request. Please try again.