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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
/*
* mod_proxy_scgi.c
* Proxy backend module for the SCGI protocol
* (http://python.ca/scgi/protocol.txt)
*
* Andr� Malo (nd/perlig.de), August 2007
*/
#define APR_WANT_MEMFUNC
#define APR_WANT_STRFUNC
#include "apr_strings.h"
#include "ap_hooks.h"
#include "apr_optional_hooks.h"
#include "apr_buckets.h"
#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
#include "http_protocol.h"
#include "http_request.h"
#include "util_script.h"
#include "mod_proxy.h"
#include "scgi.h"
#define SCHEME "scgi"
#define PROXY_FUNCTION "SCGI"
#define SCGI_MAGIC "SCGI"
#define SCGI_PROTOCOL_VERSION "1"
/* just protect from typos */
#define CONTENT_LENGTH "CONTENT_LENGTH"
#define GATEWAY_INTERFACE "GATEWAY_INTERFACE"
module AP_MODULE_DECLARE_DATA proxy_scgi_module;
typedef enum {
scgi_internal_redirect,
scgi_sendfile
} scgi_request_type;
typedef struct {
const char *location; /* target URL */
scgi_request_type type; /* type of request */
} scgi_request_config;
const char *scgi_sendfile_off = "off";
const char *scgi_sendfile_on = "X-Sendfile";
const char *scgi_internal_redirect_off = "off";
const char *scgi_internal_redirect_on = "Location";
typedef struct {
const char *sendfile;
const char *internal_redirect;
} scgi_config;
/*
* We create our own bucket type, which is actually derived (c&p) from the
* socket bucket.
* Maybe some time this should be made more abstract (like passing an
* interception function to read or something) and go into the ap_ or
* even apr_ namespace.
*/
typedef struct {
apr_socket_t *sock;
apr_off_t *counter;
} socket_ex_data;
static apr_bucket *bucket_socket_ex_create(socket_ex_data *data,
apr_bucket_alloc_t *list);
static apr_status_t bucket_socket_ex_read(apr_bucket *a, const char **str,
apr_size_t *len,
apr_read_type_e block)
{
socket_ex_data *data = a->data;
apr_socket_t *p = data->sock;
char *buf;
apr_status_t rv;
apr_interval_time_t timeout;
if (block == APR_NONBLOCK_READ) {
apr_socket_timeout_get(p, &timeout);
apr_socket_timeout_set(p, 0);
}
*str = NULL;
*len = APR_BUCKET_BUFF_SIZE;
buf = apr_bucket_alloc(*len, a->list);
rv = apr_socket_recv(p, buf, len);
if (block == APR_NONBLOCK_READ) {
apr_socket_timeout_set(p, timeout);
}
if (rv != APR_SUCCESS && rv != APR_EOF) {
apr_bucket_free(buf);
return rv;
}
if (*len > 0) {
apr_bucket_heap *h;
/* count for stats */
*data->counter += *len;
/* Change the current bucket to refer to what we read */
a = apr_bucket_heap_make(a, buf, *len, apr_bucket_free);
h = a->data;
h->alloc_len = APR_BUCKET_BUFF_SIZE; /* note the real buffer size */
*str = buf;
APR_BUCKET_INSERT_AFTER(a, bucket_socket_ex_create(data, a->list));
}
else {
apr_bucket_free(buf);
a = apr_bucket_immortal_make(a, "", 0);
*str = a->data;
}
return APR_SUCCESS;
}
static const apr_bucket_type_t bucket_type_socket_ex = {
"SOCKET_EX", 5, APR_BUCKET_DATA,
apr_bucket_destroy_noop,
bucket_socket_ex_read,
apr_bucket_setaside_notimpl,
apr_bucket_split_notimpl,
apr_bucket_copy_notimpl
};
static apr_bucket *bucket_socket_ex_make(apr_bucket *b, socket_ex_data *data)
{
b->type = &bucket_type_socket_ex;
b->length = (apr_size_t)(-1);
b->start = -1;
b->data = data;
return b;
}
static apr_bucket *bucket_socket_ex_create(socket_ex_data *data,
apr_bucket_alloc_t *list)
{
apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
APR_BUCKET_INIT(b);
b->free = apr_bucket_free;
b->list = list;
return bucket_socket_ex_make(b, data);
}
/*
* Canonicalize scgi-like URLs.
*/
static int scgi_canon(request_rec *r, char *url)
{
char *host, sport[sizeof(":65535")];
const char *err, *path;
apr_port_t port, def_port;
if (ap_cstr_casecmpn(url, SCHEME "://", sizeof(SCHEME) + 2)) {
return DECLINED;
}
url += sizeof(SCHEME); /* Keep slashes */
port = def_port = SCGI_DEF_PORT;
err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
if (err) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00857)
"error parsing URL %s: %s", url, err);
return HTTP_BAD_REQUEST;
}
if (port != def_port) {
apr_snprintf(sport, sizeof(sport), ":%u", port);
}
else {
sport[0] = '\0';
}
if (ap_strchr(host, ':')) { /* if literal IPv6 address */
host = apr_pstrcat(r->pool, "[", host, "]", NULL);
}
path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
r->proxyreq);
if (!path) {
return HTTP_BAD_REQUEST;
}
r->filename = apr_pstrcat(r->pool, "proxy:" SCHEME "://", host, sport, "/",
path, NULL);
if (apr_table_get(r->subprocess_env, "proxy-scgi-pathinfo")) {
r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
}
return OK;
}
/*
* Send a block of data, ensure, everything is sent
*/
static int sendall(proxy_conn_rec *conn, const char *buf, apr_size_t length,
request_rec *r)
{
apr_status_t rv;
apr_size_t written;
while (length > 0) {
written = length;
if ((rv = apr_socket_send(conn->sock, buf, &written)) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00858)
"sending data to %s:%u failed",
conn->hostname, conn->port);
return HTTP_SERVICE_UNAVAILABLE;
}
/* count for stats */
conn->worker->s->transferred += written;
buf += written;
length -= written;
}
return OK;
}
/*
* Send SCGI header block
*/
static int send_headers(request_rec *r, proxy_conn_rec *conn)
{
char *buf, *cp, *bodylen;
const char *ns_len;
const apr_array_header_t *env_table;
const apr_table_entry_t *env;
int j;
apr_size_t len, bodylen_size;
apr_size_t headerlen = sizeof(CONTENT_LENGTH)
+ sizeof(SCGI_MAGIC)
+ sizeof(SCGI_PROTOCOL_VERSION);
ap_add_common_vars(r);
ap_add_cgi_vars(r);
/*
* The header blob basically takes the environment and concatenates
* keys and values using 0 bytes. There are special treatments here:
* - GATEWAY_INTERFACE and SCGI_MAGIC are dropped
* - CONTENT_LENGTH is always set and must be sent as the very first
* variable
*
* Additionally it's wrapped into a so-called netstring (see SCGI spec)
*/
env_table = apr_table_elts(r->subprocess_env);
env = (apr_table_entry_t *)env_table->elts;
for (j = 0; j < env_table->nelts; ++j) {
if ( (!strcmp(env[j].key, GATEWAY_INTERFACE))
|| (!strcmp(env[j].key, CONTENT_LENGTH))
|| (!strcmp(env[j].key, SCGI_MAGIC))) {
continue;
}
headerlen += strlen(env[j].key) + strlen(env[j].val) + 2;
}
bodylen = apr_psprintf(r->pool, "%" APR_OFF_T_FMT, r->remaining);
bodylen_size = strlen(bodylen) + 1;
headerlen += bodylen_size;
ns_len = apr_psprintf(r->pool, "%" APR_SIZE_T_FMT ":", headerlen);
len = strlen(ns_len);
headerlen += len + 1; /* 1 == , */
cp = buf = apr_palloc(r->pool, headerlen);
memcpy(cp, ns_len, len);
cp += len;
memcpy(cp, CONTENT_LENGTH, sizeof(CONTENT_LENGTH));
cp += sizeof(CONTENT_LENGTH);
memcpy(cp, bodylen, bodylen_size);
cp += bodylen_size;
memcpy(cp, SCGI_MAGIC, sizeof(SCGI_MAGIC));
cp += sizeof(SCGI_MAGIC);
memcpy(cp, SCGI_PROTOCOL_VERSION, sizeof(SCGI_PROTOCOL_VERSION));
cp += sizeof(SCGI_PROTOCOL_VERSION);
for (j = 0; j < env_table->nelts; ++j) {
if ( (!strcmp(env[j].key, GATEWAY_INTERFACE))
|| (!strcmp(env[j].key, CONTENT_LENGTH))
|| (!strcmp(env[j].key, SCGI_MAGIC))) {
continue;
}
len = strlen(env[j].key) + 1;
memcpy(cp, env[j].key, len);
cp += len;
len = strlen(env[j].val) + 1;
memcpy(cp, env[j].val, len);
cp += len;
}
*cp++ = ',';
return sendall(conn, buf, headerlen, r);
}
/*
* Send request body (if any)
*/
static int send_request_body(request_rec *r, proxy_conn_rec *conn)
{
if (ap_should_client_block(r)) {
char *buf = apr_palloc(r->pool, AP_IOBUFSIZE);
int status;
long readlen;
readlen = ap_get_client_block(r, buf, AP_IOBUFSIZE);
while (readlen > 0) {
status = sendall(conn, buf, (apr_size_t)readlen, r);
if (status != OK) {
return HTTP_SERVICE_UNAVAILABLE;
}
readlen = ap_get_client_block(r, buf, AP_IOBUFSIZE);
}
if (readlen == -1) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00859)
"receiving request body failed");
return HTTP_INTERNAL_SERVER_ERROR;
}
}
return OK;
}
/*
* Fetch response from backend and pass back to the front
*/
static int pass_response(request_rec *r, proxy_conn_rec *conn)
{
apr_bucket_brigade *bb;
apr_bucket *b;
const char *location;
scgi_config *conf;
socket_ex_data *sock_data;
int status;
sock_data = apr_palloc(r->pool, sizeof(*sock_data));
sock_data->sock = conn->sock;
sock_data->counter = &conn->worker->s->read;
bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
b = bucket_socket_ex_create(sock_data, r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, b);
b = apr_bucket_eos_create(r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, b);
status = ap_scan_script_header_err_brigade_ex(r, bb, NULL,
APLOG_MODULE_INDEX);
if (status != OK) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00860)
"error reading response headers from %s:%u",
conn->hostname, conn->port);
r->status_line = NULL;
apr_brigade_destroy(bb);
return status;
}
conf = ap_get_module_config(r->per_dir_config, &proxy_scgi_module);
if (conf->sendfile && conf->sendfile != scgi_sendfile_off) {
short err = 1;
location = apr_table_get(r->err_headers_out, conf->sendfile);
if (!location) {
err = 0;
location = apr_table_get(r->headers_out, conf->sendfile);
}
if (location) {
scgi_request_config *req_conf = apr_palloc(r->pool,
sizeof(*req_conf));
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00861)
"Found %s: %s - preparing subrequest.",
conf->sendfile, location);
if (err) {
apr_table_unset(r->err_headers_out, conf->sendfile);
}
else {
apr_table_unset(r->headers_out, conf->sendfile);
}
req_conf->location = location;
req_conf->type = scgi_sendfile;
ap_set_module_config(r->request_config, &proxy_scgi_module,
req_conf);
apr_brigade_destroy(bb);
return OK;
}
}
if (r->status == HTTP_OK
&& (!conf->internal_redirect /* default === On */
|| conf->internal_redirect != scgi_internal_redirect_off)) {
short err = 1;
const char *location_header = conf->internal_redirect ?
conf->internal_redirect : scgi_internal_redirect_on;
location = apr_table_get(r->err_headers_out, location_header);
if (!location) {
err = 0;
location = apr_table_get(r->headers_out, location_header);
}
if (location && *location == '/') {
scgi_request_config *req_conf = apr_palloc(r->pool,
sizeof(*req_conf));
if (ap_cstr_casecmp(location_header, "Location")) {
if (err) {
apr_table_unset(r->err_headers_out, location_header);
}
else {
apr_table_unset(r->headers_out, location_header);
}
}
req_conf->location = location;
req_conf->type = scgi_internal_redirect;
ap_set_module_config(r->request_config, &proxy_scgi_module,
req_conf);
apr_brigade_destroy(bb);
return OK;
}
}
if (ap_pass_brigade(r->output_filters, bb)) {
return AP_FILTER_ERROR;
}
return OK;
}
/*
* Internal redirect / subrequest handler, working on request_status hook
*/
static int scgi_request_status(int *status, request_rec *r)
{
scgi_request_config *req_conf;
if ( (*status == OK)
&& (req_conf = ap_get_module_config(r->request_config,
&proxy_scgi_module))) {
switch (req_conf->type) {
case scgi_internal_redirect:
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00862)
"Internal redirect to %s", req_conf->location);
r->status_line = NULL;
if (r->method_number != M_GET) {
/* keep HEAD, which is passed around as M_GET, too */
r->method = "GET";
r->method_number = M_GET;
}
apr_table_unset(r->headers_in, "Content-Length");
ap_internal_redirect_handler(req_conf->location, r);
return OK;
/* break; */
case scgi_sendfile:
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00863)
"File subrequest to %s", req_conf->location);
do {
request_rec *rr;
rr = ap_sub_req_lookup_file(req_conf->location, r,
r->output_filters);
if (rr->status == HTTP_OK && rr->finfo.filetype != APR_NOFILE) {
/*
* We don't touch Content-Length here. It might be
* borked (there's plenty of room for a race condition).
* Either the backend sets it or it's gonna be chunked.
*/
ap_run_sub_req(rr);
}
else {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00864)
"Subrequest to file '%s' not possible. "
"(rr->status=%d, rr->finfo.filetype=%d)",
req_conf->location, rr->status,
rr->finfo.filetype);
*status = HTTP_INTERNAL_SERVER_ERROR;
return *status;
}
} while (0);
return OK;
/* break; */
}
}
return DECLINED;
}
/*
* This handles scgi:(dest) URLs
*/
static int scgi_handler(request_rec *r, proxy_worker *worker,
proxy_server_conf *conf, char *url,
const char *proxyname, apr_port_t proxyport)
{
int status;
proxy_conn_rec *backend = NULL;
apr_pool_t *p = r->pool;
apr_uri_t *uri;
char dummy;
if (ap_cstr_casecmpn(url, SCHEME "://", sizeof(SCHEME) + 2)) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00865)
"declining URL %s", url);
return DECLINED;
}
/* Create space for state information */
status = ap_proxy_acquire_connection(PROXY_FUNCTION, &backend, worker,
r->server);
if (status != OK) {
goto cleanup;
}
backend->is_ssl = 0;
/* Step One: Determine Who To Connect To */
uri = apr_palloc(p, sizeof(*uri));
status = ap_proxy_determine_connection(p, r, conf, worker, backend,
uri, &url, proxyname, proxyport,
&dummy, 1);
if (status != OK) {
goto cleanup;
}
/* Step Two: Make the Connection */
if (ap_proxy_connect_backend(PROXY_FUNCTION, backend, worker, r->server)) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00866)
"failed to make connection to backend: %s:%u",
backend->hostname, backend->port);
status = HTTP_SERVICE_UNAVAILABLE;
goto cleanup;
}
/* Step Three: Process the Request */
if ( ((status = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK)
|| ((status = send_headers(r, backend)) != OK)
|| ((status = send_request_body(r, backend)) != OK)
|| ((status = pass_response(r, backend)) != OK)) {
goto cleanup;
}
cleanup:
if (backend) {
backend->close = 1; /* always close the socket */
ap_proxy_release_connection(PROXY_FUNCTION, backend, r->server);
}
return status;
}
static void *create_scgi_config(apr_pool_t *p, char *dummy)
{
scgi_config *conf=apr_palloc(p, sizeof(*conf));
conf->sendfile = NULL; /* === default (off) */
conf->internal_redirect = NULL; /* === default (on) */
return conf;
}
static void *merge_scgi_config(apr_pool_t *p, void *base_, void *add_)
{
scgi_config *base=base_, *add=add_, *conf=apr_palloc(p, sizeof(*conf));
conf->sendfile = add->sendfile ? add->sendfile: base->sendfile;
conf->internal_redirect = add->internal_redirect
? add->internal_redirect
: base->internal_redirect;
return conf;
}
static const char *scgi_set_send_file(cmd_parms *cmd, void *mconfig,
const char *arg)
{
scgi_config *conf=mconfig;
if (!strcasecmp(arg, "Off")) {
conf->sendfile = scgi_sendfile_off;
}
else if (!strcasecmp(arg, "On")) {
conf->sendfile = scgi_sendfile_on;
}
else {
conf->sendfile = arg;
}
return NULL;
}
static const char *scgi_set_internal_redirect(cmd_parms *cmd, void *mconfig,
const char *arg)
{
scgi_config *conf = mconfig;
if (!strcasecmp(arg, "Off")) {
conf->internal_redirect = scgi_internal_redirect_off;
}
else if (!strcasecmp(arg, "On")) {
conf->internal_redirect = scgi_internal_redirect_on;
}
else {
conf->internal_redirect = arg;
}
return NULL;
}
static const command_rec scgi_cmds[] =
{
AP_INIT_TAKE1("ProxySCGISendfile", scgi_set_send_file, NULL,
RSRC_CONF|ACCESS_CONF,
"The name of the X-Sendfile pseudo response header or "
"On or Off"),
AP_INIT_TAKE1("ProxySCGIInternalRedirect", scgi_set_internal_redirect, NULL,
RSRC_CONF|ACCESS_CONF,
"The name of the pseudo response header or On or Off"),
{NULL}
};
static void register_hooks(apr_pool_t *p)
{
proxy_hook_scheme_handler(scgi_handler, NULL, NULL, APR_HOOK_FIRST);
proxy_hook_canon_handler(scgi_canon, NULL, NULL, APR_HOOK_FIRST);
APR_OPTIONAL_HOOK(proxy, request_status, scgi_request_status, NULL, NULL,
APR_HOOK_MIDDLE);
}
AP_DECLARE_MODULE(proxy_scgi) = {
STANDARD20_MODULE_STUFF,
create_scgi_config, /* create per-directory config structure */
merge_scgi_config, /* merge per-directory config structures */
NULL, /* create per-server config structure */
NULL, /* merge per-server config structures */
scgi_cmds, /* command table */
register_hooks /* register hooks */
};
|