forked from nicolasff/webdis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.c
345 lines (283 loc) · 8 KB
/
cmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "cmd.h"
#include "conf.h"
#include "acl.h"
#include "client.h"
#include "pool.h"
#include "worker.h"
#include "http.h"
#include "server.h"
#include "formats/json.h"
#include "formats/bson.h"
#include "formats/raw.h"
#ifdef MSGPACK
#include "formats/msgpack.h"
#endif
#include "formats/custom-type.h"
#include <stdlib.h>
#include <string.h>
#include <hiredis/hiredis.h>
#include <hiredis/async.h>
#include <ctype.h>
struct cmd *
cmd_new(int count) {
struct cmd *c = calloc(1, sizeof(struct cmd));
c->count = count;
c->argv = calloc(count, sizeof(char*));
c->argv_len = calloc(count, sizeof(size_t));
return c;
}
void
cmd_free(struct cmd *c) {
int i;
if(!c) return;
for(i = 0; i < c->count; ++i) {
free((char*)c->argv[i]);
}
free(c->argv);
free(c->argv_len);
free(c->jsonp);
free(c->separator);
free(c->if_none_match);
if(c->mime_free) free(c->mime);
free(c);
}
/* taken from libevent */
static char *
decode_uri(const char *uri, size_t length, size_t *out_len, int always_decode_plus) {
char c;
size_t i, j;
int in_query = always_decode_plus;
char *ret = malloc(length);
for (i = j = 0; i < length; i++) {
c = uri[i];
if (c == '?') {
in_query = 1;
} else if (c == '+' && in_query) {
c = ' ';
} else if (c == '%' && isxdigit((unsigned char)uri[i+1]) &&
isxdigit((unsigned char)uri[i+2])) {
char tmp[] = { uri[i+1], uri[i+2], '\0' };
c = (char)strtol(tmp, NULL, 16);
i += 2;
}
ret[j++] = c;
}
*out_len = (size_t)j;
return ret;
}
/* setup headers */
void
cmd_setup(struct cmd *cmd, struct http_client *client) {
int i;
cmd->keep_alive = client->keep_alive;
cmd->w = client->w; /* keep track of the worker */
for(i = 0; i < client->header_count; ++i) {
if(strcasecmp(client->headers[i].key, "If-None-Match") == 0) {
cmd->if_none_match = calloc(1+client->headers[i].val_sz, 1);
memcpy(cmd->if_none_match, client->headers[i].val,
client->headers[i].val_sz);
} else if(strcasecmp(client->headers[i].key, "Connection") == 0 &&
strcasecmp(client->headers[i].val, "Keep-Alive") == 0) {
cmd->keep_alive = 1;
}
}
if(client->type) { /* transfer pointer ownership */
cmd->mime = client->type;
cmd->mime_free = 1;
client->type = NULL;
}
if(client->jsonp) { /* transfer pointer ownership */
cmd->jsonp = client->jsonp;
client->jsonp = NULL;
}
if(client->separator) { /* transfer pointer ownership */
cmd->separator = client->separator;
client->separator = NULL;
}
if(client->filename) { /* transfer pointer ownership */
cmd->filename = client->filename;
client->filename = NULL;
}
cmd->fd = client->fd;
cmd->http_version = client->http_version;
}
cmd_response_t
cmd_run(struct worker *w, struct http_client *client,
const char *uri, size_t uri_len,
const char *body, size_t body_len) {
char *qmark = memchr(uri, '?', uri_len);
char *slash;
const char *p;
int cmd_len;
int param_count = 0, cur_param = 1;
struct cmd *cmd;
formatting_fun f_format;
/* count arguments */
if(qmark) {
uri_len = qmark - uri;
}
for(p = uri; p && p < uri + uri_len; param_count++) {
p = memchr(p+1, '/', uri_len - (p+1-uri));
}
if(body && body_len) { /* PUT request */
param_count++;
}
if(param_count == 0) {
return CMD_PARAM_ERROR;
}
cmd = cmd_new(param_count);
cmd->fd = client->fd;
/* get output formatting function */
uri_len = cmd_select_format(client, cmd, uri, uri_len, &f_format);
/* add HTTP info */
cmd_setup(cmd, client);
/* check if we only have one command or more. */
slash = memchr(uri, '/', uri_len);
if(slash) {
cmd_len = slash - uri;
} else {
cmd_len = uri_len;
}
/* there is always a first parameter, it's the command name */
cmd->argv[0] = malloc(cmd_len);
memcpy(cmd->argv[0], uri, cmd_len);
cmd->argv_len[0] = cmd_len;
/* check that the client is able to run this command */
if(!acl_allow_command(cmd, w->s->cfg, client)) {
cmd_free(cmd);
return CMD_ACL_FAIL;
}
if(cmd_is_subscribe(cmd)) {
/* create a new connection to Redis */
cmd->ac = (redisAsyncContext*)pool_connect(w->pool, 0);
/* register with the client, used upon disconnection */
client->pub_sub = cmd;
cmd->pub_sub_client = client;
} else {
/* get a connection from the pool */
cmd->ac = (redisAsyncContext*)pool_get_context(w->pool);
}
/* no args (e.g. INFO command) */
if(!slash) {
if(!cmd->ac) {
cmd_free(cmd);
return CMD_REDIS_UNAVAIL;
}
redisAsyncCommandArgv(cmd->ac, f_format, cmd, 1,
(const char **)cmd->argv, cmd->argv_len);
return CMD_SENT;
}
p = slash + 1;
while(p < uri + uri_len) {
const char *arg = p;
int arg_len;
char *next = memchr(arg, '/', uri_len - (arg-uri));
if(!next || next > uri + uri_len) { /* last argument */
p = uri + uri_len;
arg_len = p - arg;
} else { /* found a slash */
arg_len = next - arg;
p = next + 1;
}
/* record argument */
cmd->argv[cur_param] = decode_uri(arg, arg_len, &cmd->argv_len[cur_param], 1);
cur_param++;
}
if(body && body_len) { /* PUT request */
cmd->argv[cur_param] = malloc(body_len);
memcpy(cmd->argv[cur_param], body, body_len);
cmd->argv_len[cur_param] = body_len;
}
/* send it off! */
if(cmd->ac) {
cmd_send(cmd, f_format);
return CMD_SENT;
}
/* failed to find a suitable connection to Redis. */
cmd_free(cmd);
client->pub_sub = NULL;
return CMD_REDIS_UNAVAIL;
}
void
cmd_send(struct cmd *cmd, formatting_fun f_format) {
redisAsyncCommandArgv(cmd->ac, f_format, cmd, cmd->count,
(const char **)cmd->argv, cmd->argv_len);
}
/**
* Select Content-Type and processing function.
*/
int
cmd_select_format(struct http_client *client, struct cmd *cmd,
const char *uri, size_t uri_len, formatting_fun *f_format) {
const char *ext;
int ext_len = -1;
unsigned int i;
int found = 0; /* did we match it to a predefined format? */
/* those are the available reply formats */
struct reply_format {
const char *s;
size_t sz;
formatting_fun f;
const char *ct;
};
struct reply_format funs[] = {
{.s = "json", .sz = 4, .f = json_reply, .ct = "application/json"},
{.s = "raw", .sz = 3, .f = raw_reply, .ct = "binary/octet-stream"},
{.s = "bson", .sz = 4, .f = bson_reply, .ct = "application/bson"},
#ifdef MSGPACK
{.s = "msg", .sz = 3, .f = msgpack_reply, .ct = "application/x-msgpack"},
#endif
{.s = "bin", .sz = 3, .f = custom_type_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"},
{.s = "js", .sz = 2, .f = json_reply, .ct = "application/javascript"},
{.s = "css", .sz = 3, .f = custom_type_reply, .ct = "text/css"},
};
/* default */
*f_format = json_reply;
/* find extension */
for(ext = uri + uri_len - 1; ext != uri && *ext != '/'; --ext) {
if(*ext == '.') {
ext++;
ext_len = uri + uri_len - ext;
break;
}
}
if(!ext_len) return uri_len; /* nothing found */
/* find function for the given extension */
for(i = 0; i < sizeof(funs)/sizeof(funs[0]); ++i) {
if(ext_len == (int)funs[i].sz && strncmp(ext, funs[i].s, ext_len) == 0) {
if(cmd->mime_free) free(cmd->mime);
cmd->mime = (char*)funs[i].ct;
cmd->mime_free = 0;
*f_format = funs[i].f;
found = 1;
}
}
/* the user can force it with ?type=some/thing */
if(client->type) {
*f_format = custom_type_reply;
cmd->mime = strdup(client->type);
cmd->mime_free = 1;
}
if(found) {
return uri_len - ext_len - 1;
} else {
/* no matching format, use default output with the full argument, extension included. */
return uri_len;
}
}
int
cmd_is_subscribe(struct cmd *cmd) {
if(cmd->count >= 1 &&
(strncasecmp(cmd->argv[0], "SUBSCRIBE", cmd->argv_len[0]) == 0 ||
strncasecmp(cmd->argv[0], "PSUBSCRIBE", cmd->argv_len[0]) == 0)) {
return 1;
}
return 0;
}