Skip to content
Newer
Older
100644 123 lines (91 sloc) 2.18 KB
2507fce @nicolasff Added JSON output.
authored Dec 21, 2010
1 #include "cmd.h"
e2f2b36 @nicolasff Added RAW output.
authored Dec 26, 2010
2 #include "server.h"
3
4 #include "formats/json.h"
5 #include "formats/raw.h"
2507fce @nicolasff Added JSON output.
authored Dec 21, 2010
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <hiredis/hiredis.h>
10
11 struct cmd *
12 cmd_new(struct evhttp_request *rq, int count) {
13
14 struct cmd *c = calloc(1, sizeof(struct cmd));
15
16 c->rq = rq;
17 c->count = count;
18
19 c->argv = calloc(count, sizeof(char*));
20 c->argv_len = calloc(count, sizeof(size_t));
21
22 return c;
23 }
24
25
26 void
27 cmd_free(struct cmd *c) {
28
29 free(c->argv);
30 free(c->argv_len);
31
32 free(c);
33 }
34
35 void
e2f2b36 @nicolasff Added RAW output.
authored Dec 26, 2010
36 cmd_run(struct server *s, struct evhttp_request *rq,
9d2beac @nicolasff Added JSONP.
authored Dec 22, 2010
37 const char *uri, size_t uri_len) {
2507fce @nicolasff Added JSON output.
authored Dec 21, 2010
38
9d2beac @nicolasff Added JSONP.
authored Dec 22, 2010
39 char *qmark = strchr(uri, '?');
2507fce @nicolasff Added JSON output.
authored Dec 21, 2010
40 char *slash = strchr(uri, '/');
e2f2b36 @nicolasff Added RAW output.
authored Dec 26, 2010
41 const char *p;
2507fce @nicolasff Added JSON output.
authored Dec 21, 2010
42 int cmd_len;
43 int param_count = 0, cur_param = 1;
44
45 struct cmd *cmd;
e2f2b36 @nicolasff Added RAW output.
authored Dec 26, 2010
46 formatting_fun fun;
2507fce @nicolasff Added JSON output.
authored Dec 21, 2010
47
48 /* count arguments */
9d2beac @nicolasff Added JSONP.
authored Dec 22, 2010
49 if(qmark) {
50 uri_len = qmark - uri;
51 }
2507fce @nicolasff Added JSON output.
authored Dec 21, 2010
52 for(p = uri; p && p < uri + uri_len; param_count++) {
53 p = strchr(p+1, '/');
54 }
55
56 cmd = cmd_new(rq, param_count);
57
58 if(slash) {
59 cmd_len = slash - uri;
60 } else {
61 cmd_len = uri_len;
62 }
63
9d2beac @nicolasff Added JSONP.
authored Dec 22, 2010
64 /* parse URI parameters */
65 evhttp_parse_query(uri, &cmd->uri_params);
66
e2f2b36 @nicolasff Added RAW output.
authored Dec 26, 2010
67 /* get output formatting function */
68 fun = get_formatting_funtion(&cmd->uri_params);
69
2507fce @nicolasff Added JSON output.
authored Dec 21, 2010
70 /* there is always a first parameter, it's the command name */
71 cmd->argv[0] = uri;
72 cmd->argv_len[0] = cmd_len;
73
74 if(!slash) {
e2f2b36 @nicolasff Added RAW output.
authored Dec 26, 2010
75 redisAsyncCommandArgv(s->ac, fun, cmd, 1, cmd->argv, cmd->argv_len);
2507fce @nicolasff Added JSON output.
authored Dec 21, 2010
76 return;
77 }
78 p = slash + 1;
79 while(p < uri + uri_len) {
80
81 const char *arg = p;
82 int arg_len;
83 char *next = strchr(arg, '/');
84 if(next) { /* found a slash */
85 arg_len = next - arg;
86 p = next + 1;
87 } else { /* last argument */
88 arg_len = uri + uri_len - arg;
89 p = uri + uri_len;
90 }
91
92 /* record argument */
93 cmd->argv[cur_param] = arg;
94 cmd->argv_len[cur_param] = arg_len;
95
96 cur_param++;
97 }
98
e2f2b36 @nicolasff Added RAW output.
authored Dec 26, 2010
99 redisAsyncCommandArgv(s->ac, fun, cmd, param_count, cmd->argv, cmd->argv_len);
2507fce @nicolasff Added JSON output.
authored Dec 21, 2010
100 }
101
e2f2b36 @nicolasff Added RAW output.
authored Dec 26, 2010
102
103
104 formatting_fun
105 get_formatting_funtion(struct evkeyvalq *params) {
106
107 struct evkeyval *kv;
108
109 /* check for JSONP */
110 TAILQ_FOREACH(kv, params, next) {
111 if(strcmp(kv->key, "format") == 0) {
112 if(strcmp(kv->value, "raw") == 0) {
113 return raw_reply;
114 } else if(strcmp(kv->value, "json") == 0) {
115 return json_reply;
116 }
117 break;
118 }
119 }
120
121 return json_reply;
122 }
Something went wrong with that request. Please try again.