-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscgisvc.c
423 lines (326 loc) · 8.39 KB
/
scgisvc.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* Copyright (c) 2014-2018 Josef 'Jeff' Sipek <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <jeffpc/error.h>
#include <jeffpc/atomic.h>
#include <jeffpc/io.h>
#include <jeffpc/mem.h>
#include <jeffpc/time.h>
#include <jeffpc/scgisvc.h>
#include <jeffpc/socksvc.h>
#include <jeffpc/scgi.h>
#include <jeffpc/qstring.h>
struct scgiargs {
const struct scgiops *ops;
void *private;
};
static struct mem_cache *scgisvc_cache;
static atomic_t scgi_request_ids;
static void scgi_free(struct scgi *req, bool init_failed);
static void __attribute__((constructor)) init_scgisvc_subsys(void)
{
scgisvc_cache = mem_cache_create("scgisvc-cache",
sizeof(struct scgi), 0);
ASSERT(!IS_ERR(scgisvc_cache));
}
/*
* SCGI protocol parser
*/
static int read_netstring_length(struct scgi *req, size_t *len)
{
int ret;
size_t v;
v = 0;
for (;;) {
char c;
ret = xread(req->fd, &c, sizeof(c));
if (ret)
return ret;
switch (c) {
case ':':
*len = v;
return 0;
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
case '8': case '9':
v = (v * 10) + (c - '0');
break;
default:
return -EINVAL;
}
}
}
static int read_netstring_string(struct scgi *req, size_t len)
{
char *cur, *end;
char *buf;
int ret;
/*
* Receive the string.
*/
buf = malloc(len + 1);
if (!buf)
return -ENOMEM;
ret = xread(req->fd, buf, len + 1);
if (ret)
goto err;
if (buf[len] != ',') {
ret = -EINVAL;
goto err;
}
buf[len] = '\0';
/*
* Now, parse the received string.
*/
cur = buf;
end = buf + len;
while (cur < end) {
char *name, *val;
/* TODO: audit this loop for correctness / safety */
name = cur;
val = cur + strlen(name) + 1;
ret = nvl_set_cstr_dup(req->request.headers, name, val);
if (ret)
break;
cur = val + strlen(val) + 1;
}
err:
free(buf);
return ret;
}
static int read_netstring(struct scgi *req)
{
size_t len;
int ret;
ret = read_netstring_length(req, &len);
if (ret)
return ret;
return read_netstring_string(req, len);
}
/*
* Header conversion
*/
static const struct nvl_convert_info scgi_convert_headers[] = {
{ .name = "SCGI", .tgt_type = VT_INT, },
{ .name = SCGI_CONTENT_LENGTH, .tgt_type = VT_INT, },
{ .name = SCGI_REMOTE_PORT, .tgt_type = VT_INT, },
{ .name = SCGI_SERVER_PORT, .tgt_type = VT_INT, },
{ .name = NULL, },
};
static int parse_headers(struct scgi *req)
{
struct str *s;
uint64_t i;
int ret;
ret = nvl_convert(req->request.headers, scgi_convert_headers, false);
if (ret)
return ret;
ret = nvl_lookup_int(req->request.headers, "SCGI", &i);
if (ret)
return ret;
if (i != 1)
return -EINVAL;
ret = nvl_lookup_int(req->request.headers, SCGI_CONTENT_LENGTH, &i);
if (ret)
return ret;
if (i > SIZE_MAX)
return -EINVAL;
req->request.content_length = i;
s = nvl_lookup_str(req->request.headers, SCGI_REQUEST_METHOD);
if (IS_ERR(s))
return PTR_ERR(s);
if (!strcmp("GET", str_cstr(s)))
req->request.method = SCGI_REQUEST_METHOD_GET;
else if (!strcmp("POST", str_cstr(s)))
req->request.method = SCGI_REQUEST_METHOD_POST;
else if (!strcmp("HEAD", str_cstr(s)))
req->request.method = SCGI_REQUEST_METHOD_HEAD;
else
req->request.method = SCGI_REQUEST_METHOD_UNKNOWN;
str_putref(s);
return 0;
}
static int parse_qstring(struct scgi *req)
{
struct str *qs;
int ret;
qs = nvl_lookup_str(req->request.headers, SCGI_QUERY_STRING);
if (IS_ERR(qs)) {
ret = PTR_ERR(qs);
return (ret == -ENOENT) ? 0 : ret;
}
ret = qstring_parse(req->request.query, str_cstr(qs));
str_putref(qs);
return ret;
}
static int scgi_read_headers(struct scgi *req)
{
int ret;
ret = read_netstring(req);
if (ret)
return ret;
ret = parse_headers(req);
if (ret)
return ret;
return parse_qstring(req);
}
static int scgi_read_body(struct scgi *req)
{
char *buf;
int ret;
if (!req->request.content_length)
return 0;
buf = malloc(req->request.content_length + 1);
if (!buf)
return -ENOMEM;
ret = xread(req->fd, buf, req->request.content_length);
if (ret) {
free(buf);
return ret;
}
buf[req->request.content_length] = '\0';
req->request.body = buf;
return 0;
}
static int scgi_write_headers(struct scgi *req)
{
const struct nvpair *header;
char tmp[256];
int ret;
/* return status */
snprintf(tmp, sizeof(tmp), "Status: %u\n", req->response.status);
ret = xwrite_str(req->fd, tmp);
if (ret)
return ret;
/* write out accumulated headers */
nvl_for_each(header, req->response.headers) {
struct str *str;
str = nvpair_value_str(header);
if (IS_ERR(str))
return PTR_ERR(str);
snprintf(tmp, sizeof(tmp), "%s: %s\n",
nvpair_name(header),
str_cstr(str));
str_putref(str);
ret = xwrite_str(req->fd, tmp);
if (ret)
return ret;
}
/* separate headers from body */
return xwrite_str(req->fd, "\n");
}
static int scgi_write_body(struct scgi *req)
{
return xwrite(req->fd, req->response.body, req->response.bodylen);
}
static struct scgi *scgi_alloc(int fd, struct scgiargs *args)
{
struct scgi *req;
int ret;
req = mem_cache_alloc(scgisvc_cache);
if (!req)
return ERR_PTR(-ENOMEM);
memset(req, 0, sizeof(struct scgi));
req->id = atomic_inc(&scgi_request_ids);
req->fd = fd;
req->ops = args->ops;
req->request.headers = nvl_alloc();
req->request.query = nvl_alloc();
req->request.content_length = 0;
req->request.body = NULL;
req->response.status = SCGI_STATUS_OK;
req->response.headers = nvl_alloc();
req->response.bodylen = 0;
req->response.body = NULL;
if (!req->request.headers || !req->request.query ||
!req->response.headers) {
ret = -ENOMEM;
goto err;
}
if (req->ops->init) {
ret = req->ops->init(req, args->private);
if (ret)
goto err;
}
return req;
err:
scgi_free(req, true);
return ERR_PTR(ret);
}
static void scgi_free(struct scgi *req, bool init_failed)
{
if (!init_failed && req->ops->deinit)
req->ops->deinit(req);
nvl_putref(req->request.headers);
nvl_putref(req->request.query);
nvl_putref(req->response.headers);
/* NOTE: Do *not* close the fd, it'll be closed by socksvc */
mem_cache_free(scgisvc_cache, req);
}
static void scgi_conn(int fd, struct socksvc_stats *sockstats, void *private)
{
struct scgi *req;
int ret;
req = scgi_alloc(fd, private);
if (IS_ERR(req)) {
cmn_err(CE_INFO, "%s failed to alloc: %s", __func__,
xstrerror(PTR_ERR(req)));
return;
}
req->conn_stats = *sockstats;
ret = scgi_read_headers(req);
if (ret)
goto out;
req->scgi_stats.read_header_time = gettime();
ret = scgi_read_body(req);
if (ret)
goto out;
req->scgi_stats.read_body_time = gettime();
req->ops->process(req);
req->scgi_stats.compute_time = gettime();
ret = scgi_write_headers(req);
if (ret)
goto out;
req->scgi_stats.write_header_time = gettime();
/* Write out the body for everything but HEAD requests */
if (req->request.method != SCGI_REQUEST_METHOD_HEAD)
ret = scgi_write_body(req);
req->scgi_stats.write_body_time = gettime();
out:
/*
* We want to print this before freeing the request so that the
* error message includes any potential session information.
*/
if (ret)
cmn_err(CE_INFO, "%s failed: %s", __func__, xstrerror(ret));
scgi_free(req, false);
}
int scgisvc(const char *host, uint16_t port, int nthreads,
const struct scgiops *ops, void *private)
{
struct scgiargs args = {
.ops = ops,
.private = private,
};
if (!ops || !ops->process)
return -EINVAL;
return socksvc(host, port, nthreads, scgi_conn, &args);
}