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
|
#include "first.h"
#include <sys/types.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "gw_backend.h"
typedef gw_plugin_config plugin_config;
typedef gw_plugin_data plugin_data;
typedef gw_handler_ctx handler_ctx;
#include "base.h"
#include "buffer.h"
#include "http_cgi.h"
#include "log.h"
enum { LI_PROTOCOL_SCGI, LI_PROTOCOL_UWSGI };
static void mod_scgi_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
case 0: /* scgi.server */
if (cpv->vtype == T_CONFIG_LOCAL) {
gw_plugin_config * const gw = cpv->v.v;
pconf->exts = gw->exts;
pconf->exts_auth = gw->exts_auth;
pconf->exts_resp = gw->exts_resp;
}
break;
case 1: /* scgi.balance */
/*if (cpv->vtype == T_CONFIG_LOCAL)*//*always true here for this param*/
pconf->balance = (int)cpv->v.u;
break;
case 2: /* scgi.debug */
pconf->debug = (int)cpv->v.u;
break;
case 3: /* scgi.map-extensions */
pconf->ext_mapping = cpv->v.a;
break;
case 4: /* scgi.protocol */
/*if (cpv->vtype == T_CONFIG_LOCAL)*//*always true here for this param*/
pconf->proto = (int)cpv->v.u;
break;
default:/* should not happen */
return;
}
}
static void mod_scgi_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
do {
mod_scgi_merge_config_cpv(pconf, cpv);
} while ((++cpv)->k_id != -1);
}
static void mod_scgi_patch_config(request_st * const r, plugin_data * const p) {
memcpy(&p->conf, &p->defaults, sizeof(plugin_config));
for (int i = 1, used = p->nconfig; i < used; ++i) {
if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
mod_scgi_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
}
}
SETDEFAULTS_FUNC(mod_scgi_set_defaults) {
static const config_plugin_keys_t cpk[] = {
{ CONST_STR_LEN("scgi.server"),
T_CONFIG_ARRAY_KVARRAY,
T_CONFIG_SCOPE_CONNECTION }
,{ CONST_STR_LEN("scgi.balance"),
T_CONFIG_STRING,
T_CONFIG_SCOPE_CONNECTION }
,{ CONST_STR_LEN("scgi.debug"),
T_CONFIG_INT,
T_CONFIG_SCOPE_CONNECTION }
,{ CONST_STR_LEN("scgi.map-extensions"),
T_CONFIG_ARRAY_KVSTRING,
T_CONFIG_SCOPE_CONNECTION }
,{ CONST_STR_LEN("scgi.protocol"),
T_CONFIG_STRING,
T_CONFIG_SCOPE_CONNECTION }
,{ NULL, 0,
T_CONFIG_UNSET,
T_CONFIG_SCOPE_UNSET }
};
plugin_data * const p = p_d;
if (!config_plugin_values_init(srv, p, cpk, "mod_scgi"))
return HANDLER_ERROR;
/* process and validate config directives
* (init i to 0 if global context; to 1 to skip empty global context) */
for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
for (; -1 != cpv->k_id; ++cpv) {
switch (cpv->k_id) {
case 0:{/* scgi.server */
gw_plugin_config *gw = ck_calloc(1, sizeof(gw_plugin_config));
if (!gw_set_defaults_backend(srv, p, cpv->v.a, gw, 1,
cpk[cpv->k_id].k)) {
gw_plugin_config_free(gw);
return HANDLER_ERROR;
}
cpv->v.v = gw;
cpv->vtype = T_CONFIG_LOCAL;
break;
}
case 1: /* scgi.balance */
cpv->v.u = (unsigned int)gw_get_defaults_balance(srv, cpv->v.b);
break;
case 2: /* scgi.debug */
case 3: /* scgi.map-extensions */
break;
case 4: /* scgi.protocol */
if (buffer_eq_slen(cpv->v.b, CONST_STR_LEN("scgi")))
cpv->v.u = LI_PROTOCOL_SCGI;
else if (buffer_eq_slen(cpv->v.b, CONST_STR_LEN("uwsgi")))
cpv->v.u = LI_PROTOCOL_UWSGI;
else {
log_error(srv->errh, __FILE__, __LINE__,
"unexpected type for key: %s"
"expected \"scgi\" or \"uwsgi\"", cpk[cpv->k_id].k);
return HANDLER_ERROR;
}
break;
default:/* should not happen */
break;
}
}
}
/* default is 0 */
/*p->defaults.balance = (unsigned int)gw_get_defaults_balance(srv, NULL);*/
/*p->defaults.proto = LI_PROTOCOL_SCGI;*//*(default)*/
/* initialize p->defaults from global config context */
if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
if (-1 != cpv->k_id)
mod_scgi_merge_config(&p->defaults, cpv);
}
return HANDLER_GO_ON;
}
static int scgi_env_add_scgi(void *venv, const char *key, size_t key_len, const char *val, size_t val_len) {
buffer *env = venv;
size_t len;
if (!key || (!val && val_len)) return -1;
len = key_len + val_len + 2;
char *dst = buffer_extend(env, len);
memcpy(dst, key, key_len);
dst[key_len] = '\0';
dst += key_len + 1;
memcpy(dst, val, val_len);
dst[val_len] = '\0';
return 0;
}
static int scgi_env_add_uwsgi(void *venv, const char *key, size_t key_len, const char *val, size_t val_len) {
if (!key || (!val && val_len)) return -1;
if (key_len > USHRT_MAX || val_len > USHRT_MAX) return -1;
char *dst = buffer_extend(venv, 2 + key_len + 2 + val_len);
dst[0] = key_len & 0xff; /* little-endian */
dst[1] = (key_len >> 8) & 0xff;
memcpy(dst + 2, key, key_len);
dst += 2+key_len;
dst[0] = val_len & 0xff; /* little-endian */
dst[1] = (val_len >> 8) & 0xff;
memcpy(dst + 2, val, val_len);
return 0;
}
static handler_t scgi_create_env(handler_ctx *hctx) {
gw_host *host = hctx->host;
request_st * const r = hctx->r;
http_cgi_opts opts = { 0, 0, host->docroot, NULL };
http_cgi_header_append_cb scgi_env_add = hctx->conf.proto == LI_PROTOCOL_SCGI
? scgi_env_add_scgi
: scgi_env_add_uwsgi;
size_t offset;
size_t rsz = (size_t)(r->read_queue.bytes_out - hctx->wb.bytes_in);
if (rsz >= 65536) rsz = r->rqst_header_len;
buffer * const b = chunkqueue_prepend_buffer_open_sz(&hctx->wb, rsz);
/* save space for 9 digits (plus ':'), though incoming HTTP request
* currently limited to 64k (65535, so 5 chars) */
buffer_copy_string_len(b, CONST_STR_LEN(" "));
if (0 != http_cgi_headers(r, &opts, scgi_env_add, b)) {
r->http_status = 400;
r->handler_module = NULL;
buffer_clear(b);
chunkqueue_remove_finished_chunks(&hctx->wb);
return HANDLER_FINISHED;
}
if (hctx->conf.proto == LI_PROTOCOL_SCGI) {
buffer * const tb = r->tmp_buf;
size_t len;
scgi_env_add(b, CONST_STR_LEN("SCGI"), CONST_STR_LEN("1"));
buffer_clear(tb);
buffer_append_int(tb, buffer_clen(b)-10);
buffer_append_char(tb, ':');
len = buffer_clen(tb);
offset = 10 - len;
memcpy(b->ptr+offset, tb->ptr, len);
buffer_append_char(b, ',');
} else { /* LI_PROTOCOL_UWSGI */
/* http://uwsgi-docs.readthedocs.io/en/latest/Protocol.html */
size_t len = buffer_clen(b)-10;
if (len > USHRT_MAX) {
r->http_status = 431; /* Request Header Fields Too Large */
r->handler_module = NULL;
buffer_clear(b);
chunkqueue_remove_finished_chunks(&hctx->wb);
return HANDLER_FINISHED;
}
offset = 10 - 4;
b->ptr[offset] = 0;
b->ptr[offset+1] = len & 0xff; /* little-endian */
b->ptr[offset+2] = (len >> 8) & 0xff;
b->ptr[offset+3] = 0;
}
hctx->wb_reqlen = buffer_clen(b) - offset;
chunkqueue_prepend_buffer_commit(&hctx->wb);
chunkqueue_mark_written(&hctx->wb, offset);
hctx->wb.bytes_in -= (off_t)offset;
hctx->wb.bytes_out -= (off_t)offset;
if (r->reqbody_length) {
chunkqueue_append_chunkqueue(&hctx->wb, &r->reqbody_queue);
if (r->reqbody_length > 0)
hctx->wb_reqlen += r->reqbody_length; /* total req size */
else /* as-yet-unknown total request size (Transfer-Encoding: chunked)*/
hctx->wb_reqlen = -hctx->wb_reqlen;
}
plugin_stats_inc("scgi.requests");
return HANDLER_GO_ON;
}
static handler_t scgi_check_extension(request_st * const r, void *p_d, int uri_path_handler) {
plugin_data *p = p_d;
handler_t rc;
if (NULL != r->handler_module) return HANDLER_GO_ON;
mod_scgi_patch_config(r, p);
if (NULL == p->conf.exts) return HANDLER_GO_ON;
rc = gw_check_extension(r, p, uri_path_handler, 0);
if (HANDLER_GO_ON != rc) return rc;
if (r->handler_module == p->self) {
handler_ctx *hctx = r->plugin_ctx[p->id];
hctx->opts.backend = BACKEND_SCGI;
hctx->create_env = scgi_create_env;
hctx->response = chunk_buffer_acquire();
}
return HANDLER_GO_ON;
}
/* uri-path handler */
static handler_t scgi_check_extension_1(request_st * const r, void *p_d) {
return scgi_check_extension(r, p_d, 1);
}
/* start request handler */
static handler_t scgi_check_extension_2(request_st * const r, void *p_d) {
return scgi_check_extension(r, p_d, 0);
}
__attribute_cold__
__declspec_dllexport__
int mod_scgi_plugin_init(plugin *p);
int mod_scgi_plugin_init(plugin *p) {
p->version = LIGHTTPD_VERSION_ID;
p->name = "scgi";
p->init = gw_init;
p->cleanup = gw_free;
p->set_defaults = mod_scgi_set_defaults;
p->handle_request_reset = gw_handle_request_reset;
p->handle_uri_clean = scgi_check_extension_1;
p->handle_subrequest_start = scgi_check_extension_2;
p->handle_subrequest = gw_handle_subrequest;
p->handle_trigger = gw_handle_trigger;
p->handle_waitpid = gw_handle_waitpid_cb;
return 0;
}
|