-
Notifications
You must be signed in to change notification settings - Fork 4
/
urlcmd.c
336 lines (305 loc) · 9.61 KB
/
urlcmd.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
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "urlcmd.h"
#include "kstats.h"
#include "kweb.h"
#include "thumbnails.h"
/* Extern in some global variables for the url commands */
extern struct tpool tpool;
extern struct kstats kstats;
extern void sig_int(int signo);
extern char *page_data[];
/* Struct representing the url commands */
struct url_cmd {
char name[256];
void (*func)(struct intercept_buf *ib,
struct query_params *params,
struct http_request *r);
bool wrapped;
char *mime_type;
};
/* Arrays of available commands */
static struct url_cmd get_cmds[] = {
{"start_measurements", start_measurements, true, "text/html"},
{"stop_measurements", stop_measurements, true, "text/html"},
{"add_vcores", add_vcores, true, "text/html"},
{"yield_pcores", yield_pcores, true, "text/html"},
{"terminate", terminate, true, "text/html"}
};
static struct url_cmd put_cmds[] = {
{"generate_thumbnails_serial", generate_thumbnails_serial, false, "application/x-compressed"},
{"generate_thumbnails_pthreads", generate_thumbnails_pthreads, false, "application/x-compressed"},
#ifdef WITH_LITHE
{"generate_thumbnails_lithe", generate_thumbnails_lithe, false, "application/x-compressed"},
#else
{"generate_thumbnails_lithe", generate_thumbnails_lithe, true, "text/html"},
#endif
};
/* Find the beginning of the query string, or the end of the url */
static char *find_query_string(char *request_line)
{
char *c = request_line;
while(*c != '?' && *c != '\0' && !(*c == '\r' && *(c+1) == '\n'))
c++;
if(*c == '?')
return c+1;
return NULL;
}
/* Parse a query string into an array of (key, value) pairs */
static struct query_params *parse_query_string(char* request_line)
{
/* Find the beginning of the query string. */
char *query = find_query_string(request_line);
if (query == NULL)
return NULL;
/* Find the space at the end of the query string. */
char *c = strstr(query, " ");
if ((int)(c - query) > BUFSIZE)
return NULL;
struct query_params *qps = calloc(1, sizeof(struct query_params));
strncpy(qps->src, query, (int)(c - query));
char *saveptr;
char *token = strtok_r(qps->src, "&", &saveptr);
int i = 0;
while (token != NULL) {
char *saveptr2;
qps->p[i].key = strtok_r(token, "=", &saveptr2);
qps->p[i].value = strtok_r(NULL, "=", &saveptr2);
token = strtok_r(NULL, "&", &saveptr);
i++;
}
return qps;
}
static void wrap_results(struct intercept_buf *ib, struct url_cmd *cmd)
{
void *buf = ib->buf;
size_t size = buf ? strlen(buf) : 6; /* (null) */
ib->buf = malloc(strlen(page_data[URLCMD_PAGE]) +
strlen(cmd->name) + size + 1);
sprintf(ib->buf, page_data[URLCMD_PAGE], cmd->name, buf);
ib->size = strlen(ib->buf);
free(buf);
}
/* The top level function that knows how to intercept a url and run commands */
bool intercept_request(struct intercept_buf *ib,
struct http_request *r)
{
char *url = NULL;
struct url_cmd *cmd_list;
size_t num_cmds;
if(!strncmp(r->buf, "GET ", 4) && strncmp(r->buf, "get ", 4)) {
url = &r->buf[4];
if (url[0] == '/')
url++;
cmd_list = &get_cmds[0];
num_cmds = sizeof(get_cmds)/sizeof(struct url_cmd);
} else if (!strncmp(r->buf, "PUT ", 4) || !strncmp(r->buf, "put ", 4)) {
url = &r->buf[4];
if (url[0] == '/')
url++;
cmd_list = &put_cmds[0];
num_cmds = sizeof(put_cmds)/sizeof(struct url_cmd);
}
if (!url)
return false;
/* Loop through known commands and look for a match */
for (int i = 0; i < num_cmds; i++) {
struct url_cmd *cmd = &cmd_list[i];
/* If we found a match! */
if (strncmp(url, cmd->name, strlen(cmd->name)) == 0) {
/* Parse the query string into a set of key/value pairs. */
struct query_params *params = parse_query_string(url);
/* Now run the command, passing it its parameters */
cmd->func(ib, params, r);
/* Wrap the results if desired. */
if (cmd->wrapped)
wrap_results(ib, cmd);
/* Set the mime-type. */
ib->mime_type = cmd->mime_type;
/* Free the params. */
free(params);
return true;
}
}
return false;
}
void start_measurements(struct intercept_buf *ib,
struct query_params *params,
struct http_request *r)
{
struct {
unsigned int period_ms;
int file_size;
int tpool_size;
} my_params = {1000, 0, tpool.size};
if (params) {
for (int i=0; i < MAX_PARAMS; i++) {
if (params->p[i].key == NULL)
break;
else if (!strcmp(params->p[i].key, "period_ms"))
my_params.period_ms = atoi(params->p[i].value);
else if (!strcmp(params->p[i].key, "file_size"))
my_params.file_size = atoi(params->p[i].value);
else if (!strcmp(params->p[i].key, "tpool_size"))
my_params.tpool_size = atoi(params->p[i].value);
}
}
tpool_resize(&tpool, my_params.tpool_size);
ib->buf = malloc(256);
char *bp = ib->buf;
bp += sprintf(bp, "Starting Measurements<br/>");
bp += sprintf(bp, "Interval Length: %u<br/>", my_params.period_ms);
bp += sprintf(bp, "Thread Pool Size: %d<br/>", my_params.tpool_size);
bp += sprintf(bp, "File Size: %d<br/>", my_params.file_size);
ib->size = bp - (char*)ib->buf;
kstats_start(&kstats, my_params.period_ms);
}
void stop_measurements(struct intercept_buf *ib,
struct query_params *params,
struct http_request *r)
{
ib->buf = malloc(256);
char *bp = ib->buf;
bp += sprintf(ib->buf, "Stopped Measurements<br/>");
ib->size = bp - (char*)ib->buf;
kstats_stop(&kstats);
}
void add_vcores(struct intercept_buf *ib,
struct query_params *params,
struct http_request *r)
{
struct {
int num_vcores;
} my_params = {-1};
if (params) {
for (int i=0; i < MAX_PARAMS; i++) {
if (params->p[i].key == NULL)
break;
else if (!strcmp(params->p[i].key, "num_vcores"))
my_params.num_vcores = atoi(params->p[i].value);
}
}
ib->buf = malloc(256);
char *bp = ib->buf;
#if !defined(__ros__) && !defined(WITH_PARLIB)
bp += sprintf(bp, "Error: only supported on Akaros or Linux With Parlib");
#else
if (my_params.num_vcores < -1) {
bp += sprintf(bp, "Error: you must specify a query srtring parameter "
"of \"num_vcores=\" that is > 0");
} else {
vcore_request(my_params.num_vcores);
bp += sprintf(bp, "Success: you now have requests granted or pending "
"for %d vcores.",
#ifdef __ros__
__procdata.res_req[RES_CORES].amt_wanted);
#else
(int)num_vcores());
#endif
}
#endif
ib->size = bp - (char*)ib->buf;
}
void yield_pcores(struct intercept_buf *ib,
struct query_params *params,
struct http_request *r)
{
int ret;
struct {
int pcoreid;
} my_params = {-1};
if (params) {
for (int i=0; i < MAX_PARAMS; i++) {
if (params->p[i].key == NULL)
break;
else if (!strcmp(params->p[i].key, "pcoreid"))
my_params.pcoreid = atoi(params->p[i].value);
}
}
ib->buf = malloc(256);
char *bp = ib->buf;
#if !defined(WITH_CUSTOM_SCHED)
bp += sprintf(bp, "Error: only supported on Akaros and Linux with Custom Schedulers");
#else
if (my_params.pcoreid < -1) {
bp += sprintf(bp, "Error: you must specify a query srtring parameter "
"of \"pcoreid=\" that is > 0");
} else {
ret = yield_pcore(my_params.pcoreid);
udelay(10000); /* give some time for the yield to kick in, might fail */
if (!ret)
bp += sprintf(bp, "Success: you now have %d vcores wanted",
#ifdef __ros__
__procdata.res_req[RES_CORES].amt_wanted);
#else
(int)num_vcores());
#endif
else
bp += sprintf(bp, "Something failed: you have %d vcores wanted",
#ifdef __ros__
__procdata.res_req[RES_CORES].amt_wanted);
#else
(int)num_vcores());
#endif
}
#endif
ib->size = bp - (char*)ib->buf;
}
void terminate(struct intercept_buf *ib,
struct query_params *params,
struct http_request *r)
{
sig_int(SIGINT);
}
static void generate_thumbnails(struct intercept_buf *ib,
struct query_params *params,
struct http_request *r,
int method)
{
#if !defined(__akaros__) && !defined(WITH_CUSTOM_SCHED)
struct thumbnails_file_data indata, outdata;
char *default_filename = "";
indata.filename = NULL;
if (params) {
for (int i=0; i < MAX_PARAMS; i++) {
if (params->p[i].key == NULL)
break;
else if (!strcmp(params->p[i].key, "file"))
indata.filename = params->p[i].value;
}
}
if (indata.filename == NULL)
indata.filename = default_filename;
indata.stream = &r->buf[r->header_length];
indata.size = r->length - r->header_length;
indata.capacity = indata.size;
archive_thumbnails(&indata, &outdata, method);
free(outdata.filename);
ib->buf = outdata.stream;
ib->size = outdata.size;
#endif
}
void generate_thumbnails_serial(struct intercept_buf *ib,
struct query_params *params,
struct http_request *r)
{
generate_thumbnails(ib, params, r, THUMBNAILS_SERIAL);
}
void generate_thumbnails_pthreads(struct intercept_buf *ib,
struct query_params *params,
struct http_request *r)
{
generate_thumbnails(ib, params, r, THUMBNAILS_PTHREADS);
}
void generate_thumbnails_lithe(struct intercept_buf *ib,
struct query_params *params,
struct http_request *r)
{
generate_thumbnails(ib, params, r, THUMBNAILS_LITHE_FORK_JOIN);
}