Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 128 lines (95 sloc) 2.373 kb
2507fce @nicolasff Added JSON output.
authored
1 #include "cmd.h"
e2f2b36 @nicolasff Added RAW output.
authored
2 #include "server.h"
3
4 #include "formats/json.h"
5 #include "formats/raw.h"
2507fce @nicolasff Added JSON output.
authored
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
36 cmd_run(struct server *s, struct evhttp_request *rq,
9d2beac @nicolasff Added JSONP.
authored
37 const char *uri, size_t uri_len) {
2507fce @nicolasff Added JSON output.
authored
38
9d2beac @nicolasff Added JSONP.
authored
39 char *qmark = strchr(uri, '?');
2507fce @nicolasff Added JSON output.
authored
40 char *slash = strchr(uri, '/');
e2f2b36 @nicolasff Added RAW output.
authored
41 const char *p;
2507fce @nicolasff Added JSON output.
authored
42 int cmd_len;
43 int param_count = 0, cur_param = 1;
44
45 struct cmd *cmd;
e2f2b36 @nicolasff Added RAW output.
authored
46 formatting_fun fun;
2507fce @nicolasff Added JSON output.
authored
47
48 /* count arguments */
9d2beac @nicolasff Added JSONP.
authored
49 if(qmark) {
50 uri_len = qmark - uri;
51 }
2507fce @nicolasff Added JSON output.
authored
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
64 /* parse URI parameters */
65 evhttp_parse_query(uri, &cmd->uri_params);
66
e2f2b36 @nicolasff Added RAW output.
authored
67 /* get output formatting function */
68 fun = get_formatting_funtion(&cmd->uri_params);
69
2507fce @nicolasff Added JSON output.
authored
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
2c980a2 @nicolasff First try.
authored
74 /* check if we have to split the connection */
75 if(strncasecmp(cmd->argv[0], "SUBSCRIBE", cmd->argv_len[0]) == 0) {
76 s = server_copy(s);
77 }
78
2507fce @nicolasff Added JSON output.
authored
79 if(!slash) {
e2f2b36 @nicolasff Added RAW output.
authored
80 redisAsyncCommandArgv(s->ac, fun, cmd, 1, cmd->argv, cmd->argv_len);
2507fce @nicolasff Added JSON output.
authored
81 return;
82 }
83 p = slash + 1;
84 while(p < uri + uri_len) {
85
86 const char *arg = p;
87 int arg_len;
88 char *next = strchr(arg, '/');
89 if(next) { /* found a slash */
90 arg_len = next - arg;
91 p = next + 1;
92 } else { /* last argument */
93 arg_len = uri + uri_len - arg;
94 p = uri + uri_len;
95 }
96
97 /* record argument */
98 cmd->argv[cur_param] = arg;
99 cmd->argv_len[cur_param] = arg_len;
100
101 cur_param++;
102 }
103
e2f2b36 @nicolasff Added RAW output.
authored
104 redisAsyncCommandArgv(s->ac, fun, cmd, param_count, cmd->argv, cmd->argv_len);
2507fce @nicolasff Added JSON output.
authored
105 }
106
e2f2b36 @nicolasff Added RAW output.
authored
107
108
109 formatting_fun
110 get_formatting_funtion(struct evkeyvalq *params) {
111
112 struct evkeyval *kv;
113
114 /* check for JSONP */
115 TAILQ_FOREACH(kv, params, next) {
116 if(strcmp(kv->key, "format") == 0) {
117 if(strcmp(kv->value, "raw") == 0) {
118 return raw_reply;
119 } else if(strcmp(kv->value, "json") == 0) {
120 return json_reply;
121 }
122 break;
123 }
124 }
125
126 return json_reply;
127 }
Something went wrong with that request. Please try again.