-
Notifications
You must be signed in to change notification settings - Fork 1
/
fcgi.c
618 lines (574 loc) · 11.7 KB
/
fcgi.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/* Quilt: FastCGI server interface
*
* Author: Mo McRoberts <[email protected]>
*
* Copyright (c) 2014-2015 BBC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* This is the FastCGI interface for Quilt; it can be used with any
* FastCGI-compatible server, either in standalone or on-demand modes
* (i.e., it can be launched as a daemon which opens a FastCGI socket,
* or it can be invoked by a web server as needed).
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "p_fcgi.h"
const char *quilt_progname = "quilt-fcgid";
static URI *quilt_fcgi_uri;
static int quilt_fcgi_socket = -1;
/* Utilities */
static int process_args(int argc, char **argv);
static void usage(void);
static int config_defaults(void);
static int fcgi_init_(void);
static int fcgi_runloop_(void);
static int fcgi_sockpath_(URI *uri, char **ptr);
static int fcgi_hostport_(URI *uri, char **ptr);
static int fcgi_preprocess_(QUILTIMPLDATA *data);
static int fcgi_fallback_error_(QUILTIMPLDATA *data, int code);
/* QUILTIMPL methods */
static const char *fcgi_getenv(QUILTREQ *request, const char *name);
static const char *fcgi_getparam(QUILTREQ *request, const char *name);
static const char *const *fcgi_getparam_multi(QUILTREQ *request, const char *name);
static int fcgi_put(QUILTREQ *request, const unsigned char *str, size_t len);
static int fcgi_vprintf(QUILTREQ *request, const char *format, va_list ap);
static int fcgi_header(QUILTREQ *request, const unsigned char *str, size_t len);
static int fcgi_headerf(QUILTREQ *request, const char *format, va_list ap);
static int fcgi_begin(QUILTREQ *request);
static int fcgi_end(QUILTREQ *request);
static QUILTIMPL fcgi_impl = {
NULL, NULL, NULL,
fcgi_getenv,
fcgi_getparam,
fcgi_getparam_multi,
fcgi_put,
fcgi_vprintf,
fcgi_header,
fcgi_headerf,
fcgi_begin,
fcgi_end
};
int
main(int argc, char **argv)
{
struct quilt_configfn_struct configfn;
log_set_ident(argv[0]);
log_set_stderr(1);
log_set_level(LOG_NOTICE);
if(config_init(config_defaults))
{
return 1;
}
if(process_args(argc, argv))
{
return 1;
}
if(config_load(NULL))
{
return 1;
}
log_set_use_config(1);
configfn.config_get = config_get;
configfn.config_geta = config_geta;
configfn.config_get_int = config_get_int;
configfn.config_get_bool = config_get_bool;
configfn.config_get_all = config_get_all;
if(quilt_init(log_vprintf, &configfn))
{
return 1;
}
if(fcgi_init_())
{
return 1;
}
if(fcgi_runloop_())
{
return 1;
}
return 0;
}
static int
process_args(int argc, char **argv)
{
const char *t;
int c;
t = getenv("QUILT_CONFIG");
if(t)
{
config_set("global:configFile", t);
}
if(argc > 0 && argv[0])
{
t = strrchr(argv[0], '/');
if(t)
{
quilt_progname = t + 1;
}
else
{
quilt_progname = argv[0];
}
}
config_set_default("log:ident", quilt_progname);
while((c = getopt(argc, argv, "hdc:")) != -1)
{
switch(c)
{
case 'h':
usage();
exit(0);
case 'd':
config_set("log:level", "debug");
config_set("log:stderr", "1");
break;
case 'c':
config_set("global:configFile", optarg);
break;
default:
usage();
return -1;
}
}
return 0;
}
static void
usage(void)
{
fprintf(stderr, "Usage: %s [OPTIONS]\n"
"\n"
"OPTIONS is one or more of:\n"
" -h Print this notice and exit\n"
" -d Enable debug output to standard error\n"
" -c FILE Specify path to configuration file\n",
quilt_progname);
}
static int
config_defaults(void)
{
config_set_default("global:configFile", SYSCONFDIR "/quilt.conf");
config_set_default("log:level", "notice");
config_set_default("log:facility", "daemon");
config_set_default("log:syslog", "1");
config_set_default("log:stderr", "0");
config_set_default("sparql:query", "http://localhost/sparql/");
config_set_default("fastcgi:socket", "/tmp/quilt.sock");
config_set_default("quilt:base", "http://www.example.com/");
return 0;
}
static int
fcgi_init_(void)
{
char *p;
int ispath;
struct stat statbuf;
if(FCGX_Init())
{
return -1;
}
if(0 == fstat(0, &statbuf) && S_ISSOCK(statbuf.st_mode))
{
log_printf(LOG_DEBUG, "invoked by FastCGI web server; will not open new listening socket\n");
quilt_fcgi_socket = 0;
}
else
{
p = config_geta("fastcgi:socket", NULL);
if(!p)
{
log_printf(LOG_CRIT, "failed to retrieve FastCGI socket URI from configuration\n");
return -1;
}
quilt_fcgi_uri = uri_create_str(p, NULL);
if(!quilt_fcgi_uri)
{
log_printf(LOG_CRIT, "failed to parse <%s>\n", p);
free(p);
return -1;
}
free(p);
p = NULL;
if(fcgi_sockpath_(quilt_fcgi_uri, &p) == -1)
{
return -1;
}
if(p)
{
ispath = 1;
}
else
{
ispath = 0;
if(fcgi_hostport_(quilt_fcgi_uri, &p) == -1)
{
return -1;
}
if(!p)
{
log_printf(LOG_ERR, "failed to obtain either a socket path or host:port from FastCGI URI");
return -1;
}
}
log_printf(LOG_DEBUG, "opening FastCGI socket %s\n", p);
quilt_fcgi_socket = FCGX_OpenSocket(p, 5);
if(quilt_fcgi_socket < 0)
{
log_printf(LOG_ERR, "failed to open FastCGI socket: %s\n", p);
free(p);
return -1;
}
if(ispath)
{
chmod(p, 0777);
}
free(p);
}
return 0;
}
static int
fcgi_runloop_(void)
{
int r, status;
QUILTIMPLDATA *data;
QUILTREQ *req;
log_printf(LOG_DEBUG, "server is ready and waiting for FastCGI requests\n");
data = (QUILTIMPLDATA *) calloc(1, sizeof(QUILTIMPLDATA));
if(!data)
{
log_printf(LOG_CRIT, "failed to allocate memory for FastCGI requests\n");
return -1;
}
while(1)
{
req = NULL;
FCGX_InitRequest(&(data->req), quilt_fcgi_socket, 0);
if(FCGX_Accept_r(&(data->req)) < 0)
{
log_printf(LOG_CRIT, "failed to accept FastCGI request\n");
free(data);
return -1;
}
if(fcgi_preprocess_(data))
{
r = -1;
}
else
{
req = quilt_request_create(&fcgi_impl, data);
if(!req)
{
r = -1;
}
else if((status = quilt_request_status(req)))
{
r = status;
}
else
{
r = quilt_request_process(req);
}
}
if(r < 0)
{
r = 500;
}
if(r)
{
if(req)
{
quilt_error(req, r);
}
else
{
fcgi_fallback_error_(data, r);
}
}
quilt_request_free(req);
FCGX_Finish_r(&(data->req));
kvset_destroy(data->kv);
data->kv = NULL;
if(r >= 500 && r <= 599)
{
exit(EXIT_FAILURE);
}
}
return 0;
}
/* Obtain a filesystem path from a URI; returns -1 if an error
* occurs, or 0 if there wasn't a valid path found
*/
static int
fcgi_sockpath_(URI *uri, char **ptr)
{
char *p, *t;
size_t l;
*ptr = NULL;
l = uri_path(uri, NULL, 0);
if(l == (size_t) -1)
{
return -1;
}
if(l < 2)
{
return 0;
}
p = (char *) malloc(l);
if(!p)
{
return -1;
}
if(uri_path(uri, p, l) != l)
{
free(p);
return -1;
}
if(p[0] != '/')
{
free(p);
return 0;
}
for(t = p; *t; t++)
{
if(*t != '/')
{
break;
}
}
if(!*t)
{
/* The path was obviously empty */
free(p);
return 0;
}
*ptr = p;
return 0;
}
static int
fcgi_hostport_(URI *uri, char **ptr)
{
char *p;
size_t l, hl, pl;
*ptr = NULL;
hl = uri_host(uri, NULL, 0);
pl = uri_port(uri, NULL, 0);
if(hl == (size_t) -1 || pl == (size_t) - 1)
{
return -1;
}
if(pl < 2)
{
return 0;
}
l = hl + pl + 1;
p = (char *) malloc(l);
if(!p)
{
return -1;
}
if(uri_host(uri, p, hl) != hl)
{
free(p);
return -1;
}
p[hl - 1] = ':';
if(uri_port(uri, &(p[hl]), pl) != pl)
{
free(p);
return -1;
}
*ptr = p;
return 0;
}
static int
fcgi_preprocess_(QUILTIMPLDATA *data)
{
const char *qs, *s, *t, *key, *value;
char *qbuf;
char *p;
char cbuf[3];
data->headers_sent = 0;
data->kv = kvset_create();
if(!data->kv)
{
return -1;
}
qs = FCGX_GetParam("QUERY_STRING", data->req.envp);
if(!qs)
{
return 0;
}
qbuf = (char *) calloc(1, strlen(qs) + 1);
p = qbuf;
s = qs;
while(s)
{
key = p;
value = NULL;
t = strchr(s, '&');
while(*s &&(!t || s < t))
{
if(*s == '=')
{
*p = 0;
p++;
s++;
value = p;
continue;
}
if(*s == '%')
{
if(isxdigit(s[1]) && isxdigit(s[2]))
{
cbuf[0] = s[1];
cbuf[1] = s[2];
cbuf[2] = 0;
*p = (char) ((unsigned char) strtol(cbuf, NULL, 16));
p++;
s += 3;
continue;
}
}
*p = *s;
p++;
s++;
}
*p = 0;
p++;
if(value)
{
log_printf(LOG_DEBUG, "fcgi_preprocess_ param: %s value %s\n", key, value);
kvset_add(data->kv, key, value);
}
if(t)
{
t++;
}
s = t;
}
free(qbuf);
return 0;
}
static int
fcgi_fallback_error_(QUILTIMPLDATA *data, int status)
{
FCGX_FPrintF(data->req.out, "Status: %d Error\n"
"Content-type: text/html; charset=utf-8\n"
"Server: Quilt/" PACKAGE_VERSION "\n"
"\n", status);
FCGX_FPrintF(data->req.out, "<!DOCTYPE html>\n"
"<html>\n"
"\t<head>\n"
"\t\t<meta charset=\"utf-8\">\n"
"\t\t<title>Error %d</title>\n"
"\t</head>\n"
"\t<body>\n"
"\t\t<h1>Error %d</h1>\n"
"\t\t<p>An error occurred while processing the request.</p>\n"
"\t</body>\n"
"</html>\n",
status, status);
return 0;
}
/* QUILTIMPL methods */
static const char *
fcgi_getenv(QUILTREQ *request, const char *name)
{
QUILTIMPLDATA *data;
data = quilt_request_impldata(request);
return FCGX_GetParam(name, data->req.envp);
}
static const char *
fcgi_getparam(QUILTREQ *request, const char *name)
{
QUILTIMPLDATA *data;
data = quilt_request_impldata(request);
return kvset_get(data->kv, name);
}
/* fcgi_getparam_multi(QUILTREQ *request, const char *name)
* Returns an array of values for the requested parameter 'name'
* obtained from the request.
*/
static const char *const *
fcgi_getparam_multi(QUILTREQ *request, const char *name)
{
QUILTIMPLDATA *data;
data = quilt_request_impldata(request);
return kvset_getall(data->kv, name);
}
static int
fcgi_put(QUILTREQ *request, const unsigned char *str, size_t len)
{
QUILTIMPLDATA *data;
data = quilt_request_impldata(request);
if(!data->headers_sent)
{
data->headers_sent = 1;
FCGX_PutChar('\n', data->req.out);
}
return FCGX_PutStr((const char *) str, len, data->req.out);
}
static int
fcgi_vprintf(QUILTREQ *request, const char *format, va_list ap)
{
QUILTIMPLDATA *data;
data = quilt_request_impldata(request);
if(!data->headers_sent)
{
data->headers_sent = 1;
FCGX_PutChar('\n', data->req.out);
}
return FCGX_VFPrintF(data->req.out, format, ap);
}
static int
fcgi_header(QUILTREQ *request, const unsigned char *str, size_t len)
{
QUILTIMPLDATA *data;
data = quilt_request_impldata(request);
if(data->headers_sent)
{
quilt_logf(LOG_WARNING, "cannot send headers; payload has already begun\n");
return -1;
}
return FCGX_PutStr((const char *) str, len, data->req.out);
}
static int
fcgi_headerf(QUILTREQ *request, const char *format, va_list ap)
{
QUILTIMPLDATA *data;
data = quilt_request_impldata(request);
if(data->headers_sent)
{
quilt_logf(LOG_WARNING, "cannot send headers; payload has already begun\n");
return -1;
}
return FCGX_VFPrintF(data->req.out, format, ap);
}
static int
fcgi_begin(QUILTREQ *request)
{
(void) request;
return 0;
}
static int
fcgi_end(QUILTREQ *request)
{
QUILTIMPLDATA *data;
data = quilt_request_impldata(request);
if(!data->headers_sent)
{
data->headers_sent = 1;
FCGX_PutChar('\n', data->req.out);
}
return 0;
}