comparison src/http/modules/ngx_http_uwsgi_module.c @ 635:e67b227c8dbb default tip

Merge with current.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 25 Apr 2011 04:07:55 +0400
parents b9763778e212
children
comparison
equal deleted inserted replaced
578:f3a9e57d2e17 635:e67b227c8dbb
1
2 /*
3 * Copyright (C) Unbit S.a.s. 2009-2010
4 * Copyright (C) 2008 Manlio Perillo (manlio.perillo@gmail.com)
5 * Copyright (C) Igor Sysoev
6 */
7
8
9 #include <ngx_config.h>
10 #include <ngx_core.h>
11 #include <ngx_http.h>
12
13
14 typedef struct {
15 ngx_http_upstream_conf_t upstream;
16
17 ngx_array_t *flushes;
18 ngx_array_t *params_len;
19 ngx_array_t *params;
20 ngx_array_t *params_source;
21
22 ngx_hash_t headers_hash;
23 ngx_uint_t header_params;
24
25 ngx_array_t *uwsgi_lengths;
26 ngx_array_t *uwsgi_values;
27
28 #if (NGX_HTTP_CACHE)
29 ngx_http_complex_value_t cache_key;
30 #endif
31
32 ngx_str_t uwsgi_string;
33
34 ngx_uint_t modifier1;
35 ngx_uint_t modifier2;
36 } ngx_http_uwsgi_loc_conf_t;
37
38
39 static ngx_int_t ngx_http_uwsgi_eval(ngx_http_request_t *r,
40 ngx_http_uwsgi_loc_conf_t *uwcf);
41 static ngx_int_t ngx_http_uwsgi_create_request(ngx_http_request_t *r);
42 static ngx_int_t ngx_http_uwsgi_reinit_request(ngx_http_request_t *r);
43 static ngx_int_t ngx_http_uwsgi_process_status_line(ngx_http_request_t *r);
44 static ngx_int_t ngx_http_uwsgi_process_header(ngx_http_request_t *r);
45 static ngx_int_t ngx_http_uwsgi_process_header(ngx_http_request_t *r);
46 static void ngx_http_uwsgi_abort_request(ngx_http_request_t *r);
47 static void ngx_http_uwsgi_finalize_request(ngx_http_request_t *r,
48 ngx_int_t rc);
49
50 static void *ngx_http_uwsgi_create_loc_conf(ngx_conf_t *cf);
51 static char *ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent,
52 void *child);
53
54 static char *ngx_http_uwsgi_pass(ngx_conf_t *cf, ngx_command_t *cmd,
55 void *conf);
56 static char *ngx_http_uwsgi_store(ngx_conf_t *cf, ngx_command_t *cmd,
57 void *conf);
58
59 #if (NGX_HTTP_CACHE)
60 static ngx_int_t ngx_http_uwsgi_create_key(ngx_http_request_t *r);
61 static char *ngx_http_uwsgi_cache(ngx_conf_t *cf, ngx_command_t *cmd,
62 void *conf);
63 static char *ngx_http_uwsgi_cache_key(ngx_conf_t *cf, ngx_command_t *cmd,
64 void *conf);
65 #endif
66
67
68 static ngx_conf_num_bounds_t ngx_http_uwsgi_modifier_bounds = {
69 ngx_conf_check_num_bounds, 0, 255
70 };
71
72
73 static ngx_conf_bitmask_t ngx_http_uwsgi_next_upstream_masks[] = {
74 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
75 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
76 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
77 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
78 { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
79 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
80 { ngx_string("updating"), NGX_HTTP_UPSTREAM_FT_UPDATING },
81 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
82 { ngx_null_string, 0 }
83 };
84
85
86 ngx_module_t ngx_http_uwsgi_module;
87
88
89 static ngx_command_t ngx_http_uwsgi_commands[] = {
90
91 { ngx_string("uwsgi_pass"),
92 NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
93 ngx_http_uwsgi_pass,
94 NGX_HTTP_LOC_CONF_OFFSET,
95 0,
96 NULL },
97
98 { ngx_string("uwsgi_modifier1"),
99 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
100 ngx_conf_set_num_slot,
101 NGX_HTTP_LOC_CONF_OFFSET,
102 offsetof(ngx_http_uwsgi_loc_conf_t, modifier1),
103 &ngx_http_uwsgi_modifier_bounds },
104
105 { ngx_string("uwsgi_modifier2"),
106 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
107 ngx_conf_set_num_slot,
108 NGX_HTTP_LOC_CONF_OFFSET,
109 offsetof(ngx_http_uwsgi_loc_conf_t, modifier2),
110 &ngx_http_uwsgi_modifier_bounds },
111
112 { ngx_string("uwsgi_store"),
113 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
114 ngx_http_uwsgi_store,
115 NGX_HTTP_LOC_CONF_OFFSET,
116 0,
117 NULL },
118
119 { ngx_string("uwsgi_store_access"),
120 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
121 ngx_conf_set_access_slot,
122 NGX_HTTP_LOC_CONF_OFFSET,
123 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.store_access),
124 NULL },
125
126 { ngx_string("uwsgi_ignore_client_abort"),
127 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
128 ngx_conf_set_flag_slot,
129 NGX_HTTP_LOC_CONF_OFFSET,
130 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ignore_client_abort),
131 NULL },
132
133 { ngx_string("uwsgi_bind"),
134 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
135 ngx_http_upstream_bind_set_slot,
136 NGX_HTTP_LOC_CONF_OFFSET,
137 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.local),
138 NULL },
139
140 { ngx_string("uwsgi_connect_timeout"),
141 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
142 ngx_conf_set_msec_slot,
143 NGX_HTTP_LOC_CONF_OFFSET,
144 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.connect_timeout),
145 NULL },
146
147 { ngx_string("uwsgi_send_timeout"),
148 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
149 ngx_conf_set_msec_slot,
150 NGX_HTTP_LOC_CONF_OFFSET,
151 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.send_timeout),
152 NULL },
153
154 { ngx_string("uwsgi_buffer_size"),
155 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
156 ngx_conf_set_size_slot,
157 NGX_HTTP_LOC_CONF_OFFSET,
158 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.buffer_size),
159 NULL },
160
161 { ngx_string("uwsgi_pass_request_headers"),
162 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
163 ngx_conf_set_flag_slot,
164 NGX_HTTP_LOC_CONF_OFFSET,
165 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.pass_request_headers),
166 NULL },
167
168 { ngx_string("uwsgi_pass_request_body"),
169 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
170 ngx_conf_set_flag_slot,
171 NGX_HTTP_LOC_CONF_OFFSET,
172 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.pass_request_body),
173 NULL },
174
175 { ngx_string("uwsgi_intercept_errors"),
176 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
177 ngx_conf_set_flag_slot,
178 NGX_HTTP_LOC_CONF_OFFSET,
179 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.intercept_errors),
180 NULL },
181
182 { ngx_string("uwsgi_read_timeout"),
183 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
184 ngx_conf_set_msec_slot,
185 NGX_HTTP_LOC_CONF_OFFSET,
186 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.read_timeout),
187 NULL },
188
189 { ngx_string("uwsgi_buffers"),
190 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
191 ngx_conf_set_bufs_slot,
192 NGX_HTTP_LOC_CONF_OFFSET,
193 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.bufs),
194 NULL },
195
196 { ngx_string("uwsgi_busy_buffers_size"),
197 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
198 ngx_conf_set_size_slot,
199 NGX_HTTP_LOC_CONF_OFFSET,
200 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.busy_buffers_size_conf),
201 NULL },
202
203 #if (NGX_HTTP_CACHE)
204
205 { ngx_string("uwsgi_cache"),
206 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
207 ngx_http_uwsgi_cache,
208 NGX_HTTP_LOC_CONF_OFFSET,
209 0,
210 NULL },
211
212 { ngx_string("uwsgi_cache_key"),
213 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
214 ngx_http_uwsgi_cache_key,
215 NGX_HTTP_LOC_CONF_OFFSET,
216 0,
217 NULL },
218
219 { ngx_string("uwsgi_cache_path"),
220 NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE,
221 ngx_http_file_cache_set_slot,
222 0,
223 0,
224 &ngx_http_uwsgi_module },
225
226 { ngx_string("uwsgi_cache_bypass"),
227 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
228 ngx_http_set_predicate_slot,
229 NGX_HTTP_LOC_CONF_OFFSET,
230 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_bypass),
231 NULL },
232
233 { ngx_string("uwsgi_no_cache"),
234 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
235 ngx_http_set_predicate_slot,
236 NGX_HTTP_LOC_CONF_OFFSET,
237 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.no_cache),
238 NULL },
239
240 { ngx_string("uwsgi_cache_valid"),
241 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
242 ngx_http_file_cache_valid_set_slot,
243 NGX_HTTP_LOC_CONF_OFFSET,
244 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_valid),
245 NULL },
246
247 { ngx_string("uwsgi_cache_min_uses"),
248 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
249 ngx_conf_set_num_slot,
250 NGX_HTTP_LOC_CONF_OFFSET,
251 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_min_uses),
252 NULL },
253
254 { ngx_string("uwsgi_cache_use_stale"),
255 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
256 ngx_conf_set_bitmask_slot,
257 NGX_HTTP_LOC_CONF_OFFSET,
258 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_use_stale),
259 &ngx_http_uwsgi_next_upstream_masks },
260
261 { ngx_string("uwsgi_cache_methods"),
262 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
263 ngx_conf_set_bitmask_slot,
264 NGX_HTTP_LOC_CONF_OFFSET,
265 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_methods),
266 &ngx_http_upstream_cache_method_mask },
267
268 #endif
269
270 { ngx_string("uwsgi_temp_path"),
271 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
272 ngx_conf_set_path_slot,
273 NGX_HTTP_LOC_CONF_OFFSET,
274 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.temp_path),
275 NULL },
276
277 { ngx_string("uwsgi_max_temp_file_size"),
278 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
279 ngx_conf_set_size_slot,
280 NGX_HTTP_LOC_CONF_OFFSET,
281 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.max_temp_file_size_conf),
282 NULL },
283
284 { ngx_string("uwsgi_temp_file_write_size"),
285 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
286 ngx_conf_set_size_slot,
287 NGX_HTTP_LOC_CONF_OFFSET,
288 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.temp_file_write_size_conf),
289 NULL },
290
291 { ngx_string("uwsgi_next_upstream"),
292 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
293 ngx_conf_set_bitmask_slot,
294 NGX_HTTP_LOC_CONF_OFFSET,
295 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.next_upstream),
296 &ngx_http_uwsgi_next_upstream_masks },
297
298 { ngx_string("uwsgi_param"),
299 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
300 ngx_conf_set_keyval_slot,
301 NGX_HTTP_LOC_CONF_OFFSET,
302 offsetof(ngx_http_uwsgi_loc_conf_t, params_source),
303 NULL },
304
305 { ngx_string("uwsgi_string"),
306 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
307 ngx_conf_set_str_slot,
308 NGX_HTTP_LOC_CONF_OFFSET,
309 offsetof(ngx_http_uwsgi_loc_conf_t, uwsgi_string),
310 NULL },
311
312 { ngx_string("uwsgi_pass_header"),
313 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
314 ngx_conf_set_str_array_slot,
315 NGX_HTTP_LOC_CONF_OFFSET,
316 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.pass_headers),
317 NULL },
318
319 { ngx_string("uwsgi_hide_header"),
320 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
321 ngx_conf_set_str_array_slot,
322 NGX_HTTP_LOC_CONF_OFFSET,
323 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.hide_headers),
324 NULL },
325
326 { ngx_string("uwsgi_ignore_headers"),
327 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
328 ngx_conf_set_bitmask_slot,
329 NGX_HTTP_LOC_CONF_OFFSET,
330 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.ignore_headers),
331 &ngx_http_upstream_ignore_headers_masks },
332
333 ngx_null_command
334 };
335
336
337 static ngx_http_module_t ngx_http_uwsgi_module_ctx = {
338 NULL, /* preconfiguration */
339 NULL, /* postconfiguration */
340
341 NULL, /* create main configuration */
342 NULL, /* init main configuration */
343
344 NULL, /* create server configuration */
345 NULL, /* merge server configuration */
346
347 ngx_http_uwsgi_create_loc_conf, /* create location configuration */
348 ngx_http_uwsgi_merge_loc_conf /* merge location configuration */
349 };
350
351
352 ngx_module_t ngx_http_uwsgi_module = {
353 NGX_MODULE_V1,
354 &ngx_http_uwsgi_module_ctx, /* module context */
355 ngx_http_uwsgi_commands, /* module directives */
356 NGX_HTTP_MODULE, /* module type */
357 NULL, /* init master */
358 NULL, /* init module */
359 NULL, /* init process */
360 NULL, /* init thread */
361 NULL, /* exit thread */
362 NULL, /* exit process */
363 NULL, /* exit master */
364 NGX_MODULE_V1_PADDING
365 };
366
367
368 static ngx_str_t ngx_http_uwsgi_hide_headers[] = {
369 ngx_string("X-Accel-Expires"),
370 ngx_string("X-Accel-Redirect"),
371 ngx_string("X-Accel-Limit-Rate"),
372 ngx_string("X-Accel-Buffering"),
373 ngx_string("X-Accel-Charset"),
374 ngx_null_string
375 };
376
377
378 #if (NGX_HTTP_CACHE)
379
380 static ngx_keyval_t ngx_http_uwsgi_cache_headers[] = {
381 { ngx_string("HTTP_IF_MODIFIED_SINCE"), ngx_string("") },
382 { ngx_string("HTTP_IF_UNMODIFIED_SINCE"), ngx_string("") },
383 { ngx_string("HTTP_IF_NONE_MATCH"), ngx_string("") },
384 { ngx_string("HTTP_IF_MATCH"), ngx_string("") },
385 { ngx_string("HTTP_RANGE"), ngx_string("") },
386 { ngx_string("HTTP_IF_RANGE"), ngx_string("") },
387 { ngx_null_string, ngx_null_string }
388 };
389
390 #endif
391
392
393 static ngx_path_init_t ngx_http_uwsgi_temp_path = {
394 ngx_string(NGX_HTTP_UWSGI_TEMP_PATH), { 1, 2, 0 }
395 };
396
397
398 static ngx_int_t
399 ngx_http_uwsgi_handler(ngx_http_request_t *r)
400 {
401 ngx_int_t rc;
402 ngx_http_status_t *status;
403 ngx_http_upstream_t *u;
404 ngx_http_uwsgi_loc_conf_t *uwcf;
405
406 if (r->subrequest_in_memory) {
407 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
408 "ngx_http_uwsgi_module does not support "
409 "subrequests in memory");
410 return NGX_HTTP_INTERNAL_SERVER_ERROR;
411 }
412
413 if (ngx_http_upstream_create(r) != NGX_OK) {
414 return NGX_HTTP_INTERNAL_SERVER_ERROR;
415 }
416
417 status = ngx_pcalloc(r->pool, sizeof(ngx_http_status_t));
418 if (status == NULL) {
419 return NGX_HTTP_INTERNAL_SERVER_ERROR;
420 }
421
422 ngx_http_set_ctx(r, status, ngx_http_uwsgi_module);
423
424 uwcf = ngx_http_get_module_loc_conf(r, ngx_http_uwsgi_module);
425
426 if (uwcf->uwsgi_lengths) {
427 if (ngx_http_uwsgi_eval(r, uwcf) != NGX_OK) {
428 return NGX_HTTP_INTERNAL_SERVER_ERROR;
429 }
430 }
431
432 u = r->upstream;
433
434 ngx_str_set(&u->schema, "uwsgi://");
435 u->output.tag = (ngx_buf_tag_t) &ngx_http_uwsgi_module;
436
437 u->conf = &uwcf->upstream;
438
439 #if (NGX_HTTP_CACHE)
440 u->create_key = ngx_http_uwsgi_create_key;
441 #endif
442 u->create_request = ngx_http_uwsgi_create_request;
443 u->reinit_request = ngx_http_uwsgi_reinit_request;
444 u->process_header = ngx_http_uwsgi_process_status_line;
445 u->abort_request = ngx_http_uwsgi_abort_request;
446 u->finalize_request = ngx_http_uwsgi_finalize_request;
447
448 u->buffering = 1;
449
450 u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
451 if (u->pipe == NULL) {
452 return NGX_HTTP_INTERNAL_SERVER_ERROR;
453 }
454
455 u->pipe->input_filter = ngx_event_pipe_copy_input_filter;
456 u->pipe->input_ctx = r;
457
458 rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
459
460 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
461 return rc;
462 }
463
464 return NGX_DONE;
465 }
466
467
468 static ngx_int_t
469 ngx_http_uwsgi_eval(ngx_http_request_t *r, ngx_http_uwsgi_loc_conf_t * uwcf)
470 {
471 ngx_url_t url;
472 ngx_http_upstream_t *u;
473
474 ngx_memzero(&url, sizeof(ngx_url_t));
475
476 if (ngx_http_script_run(r, &url.url, uwcf->uwsgi_lengths->elts, 0,
477 uwcf->uwsgi_values->elts)
478 == NULL)
479 {
480 return NGX_ERROR;
481 }
482
483 url.no_resolve = 1;
484
485 if (ngx_parse_url(r->pool, &url) != NGX_OK) {
486 if (url.err) {
487 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
488 "%s in upstream \"%V\"", url.err, &url.url);
489 }
490
491 return NGX_ERROR;
492 }
493
494 u = r->upstream;
495
496 u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
497 if (u->resolved == NULL) {
498 return NGX_ERROR;
499 }
500
501 if (url.addrs && url.addrs[0].sockaddr) {
502 u->resolved->sockaddr = url.addrs[0].sockaddr;
503 u->resolved->socklen = url.addrs[0].socklen;
504 u->resolved->naddrs = 1;
505 u->resolved->host = url.addrs[0].name;
506
507 } else {
508 u->resolved->host = url.host;
509 u->resolved->port = url.port;
510 u->resolved->no_port = url.no_port;
511 }
512
513 return NGX_OK;
514 }
515
516
517 #if (NGX_HTTP_CACHE)
518
519 static ngx_int_t
520 ngx_http_uwsgi_create_key(ngx_http_request_t *r)
521 {
522 ngx_str_t *key;
523 ngx_http_uwsgi_loc_conf_t *uwcf;
524
525 key = ngx_array_push(&r->cache->keys);
526 if (key == NULL) {
527 return NGX_ERROR;
528 }
529
530 uwcf = ngx_http_get_module_loc_conf(r, ngx_http_uwsgi_module);
531
532 if (ngx_http_complex_value(r, &uwcf->cache_key, key) != NGX_OK) {
533 return NGX_ERROR;
534 }
535
536 return NGX_OK;
537 }
538
539 #endif
540
541
542 static ngx_int_t
543 ngx_http_uwsgi_create_request(ngx_http_request_t *r)
544 {
545 u_char ch, *lowcase_key;
546 size_t key_len, val_len, len, allocated;
547 ngx_uint_t i, n, hash, header_params;
548 ngx_buf_t *b;
549 ngx_chain_t *cl, *body;
550 ngx_list_part_t *part;
551 ngx_table_elt_t *header, **ignored;
552 ngx_http_script_code_pt code;
553 ngx_http_script_engine_t e, le;
554 ngx_http_uwsgi_loc_conf_t *uwcf;
555 ngx_http_script_len_code_pt lcode;
556
557 len = 0;
558 header_params = 0;
559 ignored = NULL;
560
561 uwcf = ngx_http_get_module_loc_conf(r, ngx_http_uwsgi_module);
562
563 if (uwcf->params_len) {
564 ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
565
566 ngx_http_script_flush_no_cacheable_variables(r, uwcf->flushes);
567 le.flushed = 1;
568
569 le.ip = uwcf->params_len->elts;
570 le.request = r;
571
572 while (*(uintptr_t *) le.ip) {
573
574 lcode = *(ngx_http_script_len_code_pt *) le.ip;
575 key_len = lcode(&le);
576
577 for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode (&le)) {
578 lcode = *(ngx_http_script_len_code_pt *) le.ip;
579 }
580 le.ip += sizeof(uintptr_t);
581
582 len += 2 + key_len + 2 + val_len;
583 }
584 }
585
586 if (uwcf->upstream.pass_request_headers) {
587
588 allocated = 0;
589 lowcase_key = NULL;
590
591 if (uwcf->header_params) {
592 ignored = ngx_palloc(r->pool, uwcf->header_params * sizeof(void *));
593 if (ignored == NULL) {
594 return NGX_ERROR;
595 }
596 }
597
598 part = &r->headers_in.headers.part;
599 header = part->elts;
600
601 for (i = 0; /* void */ ; i++) {
602
603 if (i >= part->nelts) {
604 if (part->next == NULL) {
605 break;
606 }
607
608 part = part->next;
609 header = part->elts;
610 i = 0;
611 }
612
613 if (uwcf->header_params) {
614 if (allocated < header[i].key.len) {
615 allocated = header[i].key.len + 16;
616 lowcase_key = ngx_pnalloc(r->pool, allocated);
617 if (lowcase_key == NULL) {
618 return NGX_ERROR;
619 }
620 }
621
622 hash = 0;
623
624 for (n = 0; n < header[i].key.len; n++) {
625 ch = header[i].key.data[n];
626
627 if (ch >= 'A' && ch <= 'Z') {
628 ch |= 0x20;
629
630 } else if (ch == '-') {
631 ch = '_';
632 }
633
634 hash = ngx_hash(hash, ch);
635 lowcase_key[n] = ch;
636 }
637
638 if (ngx_hash_find(&uwcf->headers_hash, hash, lowcase_key, n)) {
639 ignored[header_params++] = &header[i];
640 continue;
641 }
642 }
643
644 len += 2 + sizeof("HTTP_") - 1 + header[i].key.len
645 + 2 + header[i].value.len;
646 }
647 }
648
649 len += uwcf->uwsgi_string.len;
650
651 #if 0
652 /* allow custom uwsgi packet */
653 if (len > 0 && len < 2) {
654 ngx_log_error (NGX_LOG_ALERT, r->connection->log, 0,
655 "uwsgi request is too little: %uz", len);
656 return NGX_ERROR;
657 }
658 #endif
659
660 b = ngx_create_temp_buf(r->pool, len + 4);
661 if (b == NULL) {
662 return NGX_ERROR;
663 }
664
665 cl = ngx_alloc_chain_link(r->pool);
666 if (cl == NULL) {
667 return NGX_ERROR;
668 }
669
670 cl->buf = b;
671
672 *b->last++ = (u_char) uwcf->modifier1;
673 *b->last++ = (u_char) (len & 0xff);
674 *b->last++ = (u_char) ((len >> 8) & 0xff);
675 *b->last++ = (u_char) uwcf->modifier2;
676
677 if (uwcf->params_len) {
678 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
679
680 e.ip = uwcf->params->elts;
681 e.pos = b->last;
682 e.request = r;
683 e.flushed = 1;
684
685 le.ip = uwcf->params_len->elts;
686
687 while (*(uintptr_t *) le.ip) {
688
689 lcode = *(ngx_http_script_len_code_pt *) le.ip;
690 key_len = (u_char) lcode (&le);
691
692 for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
693 lcode = *(ngx_http_script_len_code_pt *) le.ip;
694 }
695 le.ip += sizeof(uintptr_t);
696
697 *e.pos++ = (u_char) (key_len & 0xff);
698 *e.pos++ = (u_char) ((key_len >> 8) & 0xff);
699
700 code = *(ngx_http_script_code_pt *) e.ip;
701 code((ngx_http_script_engine_t *) & e);
702
703 *e.pos++ = (u_char) (val_len & 0xff);
704 *e.pos++ = (u_char) ((val_len >> 8) & 0xff);
705
706 while (*(uintptr_t *) e.ip) {
707 code = *(ngx_http_script_code_pt *) e.ip;
708 code((ngx_http_script_engine_t *) & e);
709 }
710
711 e.ip += sizeof(uintptr_t);
712
713 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
714 "uwsgi param: \"%*s: %*s\"",
715 key_len, e.pos - (key_len + 2 + val_len),
716 val_len, e.pos - val_len);
717 }
718
719 b->last = e.pos;
720 }
721
722 if (uwcf->upstream.pass_request_headers) {
723
724 part = &r->headers_in.headers.part;
725 header = part->elts;
726
727 for (i = 0; /* void */ ; i++) {
728
729 if (i >= part->nelts) {
730 if (part->next == NULL) {
731 break;
732 }
733
734 part = part->next;
735 header = part->elts;
736 i = 0;
737 }
738
739 for (n = 0; n < header_params; n++) {
740 if (&header[i] == ignored[n]) {
741 goto next;
742 }
743 }
744
745 key_len = sizeof("HTTP_") - 1 + header[i].key.len;
746 *b->last++ = (u_char) (key_len & 0xff);
747 *b->last++ = (u_char) ((key_len >> 8) & 0xff);
748
749 b->last = ngx_cpymem(b->last, "HTTP_", sizeof("HTTP_") - 1);
750 for (n = 0; n < header[i].key.len; n++) {
751 ch = header[i].key.data[n];
752
753 if (ch >= 'a' && ch <= 'z') {
754 ch &= ~0x20;
755
756 } else if (ch == '-') {
757 ch = '_';
758 }
759
760 *b->last++ = ch;
761 }
762
763 val_len = header[i].value.len;
764 *b->last++ = (u_char) (val_len & 0xff);
765 *b->last++ = (u_char) ((val_len >> 8) & 0xff);
766 b->last = ngx_copy(b->last, header[i].value.data, val_len);
767
768 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
769 "uwsgi param: \"%*s: %*s\"",
770 key_len, b->last - (key_len + 2 + val_len),
771 val_len, b->last - val_len);
772 next:
773
774 continue;
775 }
776 }
777
778 b->last = ngx_copy(b->last, uwcf->uwsgi_string.data,
779 uwcf->uwsgi_string.len);
780
781 if (uwcf->upstream.pass_request_body) {
782 body = r->upstream->request_bufs;
783 r->upstream->request_bufs = cl;
784
785 while (body) {
786 b = ngx_alloc_buf(r->pool);
787 if (b == NULL) {
788 return NGX_ERROR;
789 }
790
791 ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));
792
793 cl->next = ngx_alloc_chain_link(r->pool);
794 if (cl->next == NULL) {
795 return NGX_ERROR;
796 }
797
798 cl = cl->next;
799 cl->buf = b;
800
801 body = body->next;
802 }
803
804 } else {
805 r->upstream->request_bufs = cl;
806 }
807
808 cl->next = NULL;
809
810 return NGX_OK;
811 }
812
813
814 static ngx_int_t
815 ngx_http_uwsgi_reinit_request(ngx_http_request_t *r)
816 {
817 ngx_http_status_t *status;
818
819 status = ngx_http_get_module_ctx(r, ngx_http_uwsgi_module);
820
821 if (status == NULL) {
822 return NGX_OK;
823 }
824
825 status->code = 0;
826 status->count = 0;
827 status->start = NULL;
828 status->end = NULL;
829
830 r->upstream->process_header = ngx_http_uwsgi_process_status_line;
831
832 return NGX_OK;
833 }
834
835
836 static ngx_int_t
837 ngx_http_uwsgi_process_status_line(ngx_http_request_t *r)
838 {
839 size_t len;
840 ngx_int_t rc;
841 ngx_http_status_t *status;
842 ngx_http_upstream_t *u;
843
844 status = ngx_http_get_module_ctx(r, ngx_http_uwsgi_module);
845
846 if (status == NULL) {
847 return NGX_ERROR;
848 }
849
850 u = r->upstream;
851
852 rc = ngx_http_parse_status_line(r, &u->buffer, status);
853
854 if (rc == NGX_AGAIN) {
855 return rc;
856 }
857
858 if (rc == NGX_ERROR) {
859 r->http_version = NGX_HTTP_VERSION_9;
860
861 u->process_header = ngx_http_uwsgi_process_header;
862
863 return ngx_http_uwsgi_process_header(r);
864 }
865
866 if (u->state) {
867 u->state->status = status->code;
868 }
869
870 u->headers_in.status_n = status->code;
871
872 len = status->end - status->start;
873 u->headers_in.status_line.len = len;
874
875 u->headers_in.status_line.data = ngx_pnalloc(r->pool, len);
876 if (u->headers_in.status_line.data == NULL) {
877 return NGX_ERROR;
878 }
879
880 ngx_memcpy(u->headers_in.status_line.data, status->start, len);
881
882 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
883 "http uwsgi status %ui \"%V\"",
884 u->headers_in.status_n, &u->headers_in.status_line);
885
886 u->process_header = ngx_http_uwsgi_process_header;
887
888 return ngx_http_uwsgi_process_header(r);
889 }
890
891
892 static ngx_int_t
893 ngx_http_uwsgi_process_header(ngx_http_request_t *r)
894 {
895 ngx_str_t *status_line;
896 ngx_int_t rc, status;
897 ngx_table_elt_t *h;
898 ngx_http_upstream_t *u;
899 ngx_http_upstream_header_t *hh;
900 ngx_http_upstream_main_conf_t *umcf;
901
902 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
903
904 for ( ;; ) {
905
906 rc = ngx_http_parse_header_line(r, &r->upstream->buffer, 1);
907
908 if (rc == NGX_OK) {
909
910 /* a header line has been parsed successfully */
911
912 h = ngx_list_push(&r->upstream->headers_in.headers);
913 if (h == NULL) {
914 return NGX_ERROR;
915 }
916
917 h->hash = r->header_hash;
918
919 h->key.len = r->header_name_end - r->header_name_start;
920 h->value.len = r->header_end - r->header_start;
921
922 h->key.data = ngx_pnalloc(r->pool,
923 h->key.len + 1 + h->value.len + 1
924 + h->key.len);
925 if (h->key.data == NULL) {
926 return NGX_ERROR;
927 }
928
929 h->value.data = h->key.data + h->key.len + 1;
930 h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;
931
932 ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1);
933 ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1);
934
935 if (h->key.len == r->lowcase_index) {
936 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
937
938 } else {
939 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
940 }
941
942 hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
943 h->lowcase_key, h->key.len);
944
945 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
946 return NGX_ERROR;
947 }
948
949 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
950 "http uwsgi header: \"%V: %V\"", &h->key, &h->value);
951
952 continue;
953 }
954
955 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
956
957 /* a whole header has been parsed successfully */
958
959 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
960 "http uwsgi header done");
961
962 if (r->http_version > NGX_HTTP_VERSION_9) {
963 return NGX_OK;
964 }
965
966 u = r->upstream;
967
968 if (u->headers_in.status) {
969 status_line = &u->headers_in.status->value;
970
971 status = ngx_atoi(status_line->data, 3);
972 if (status == NGX_ERROR) {
973 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
974 "upstream sent invalid status \"%V\"",
975 status_line);
976 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
977 }
978
979 r->http_version = NGX_HTTP_VERSION_10;
980 u->headers_in.status_n = status;
981 u->headers_in.status_line = *status_line;
982
983 } else if (u->headers_in.location) {
984 r->http_version = NGX_HTTP_VERSION_10;
985 u->headers_in.status_n = 302;
986 ngx_str_set(&u->headers_in.status_line,
987 "302 Moved Temporarily");
988
989 } else {
990 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
991 "upstream sent neither valid HTTP/1.0 header "
992 "nor \"Status\" header line");
993 u->headers_in.status_n = 200;
994 ngx_str_set(&u->headers_in.status_line, "200 OK");
995 }
996
997 if (u->state) {
998 u->state->status = u->headers_in.status_n;
999 }
1000
1001 return NGX_OK;
1002 }
1003
1004 if (rc == NGX_AGAIN) {
1005 return NGX_AGAIN;
1006 }
1007
1008 /* there was error while a header line parsing */
1009
1010 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1011 "upstream sent invalid header");
1012
1013 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1014 }
1015 }
1016
1017
1018 static void
1019 ngx_http_uwsgi_abort_request(ngx_http_request_t *r)
1020 {
1021 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1022 "abort http uwsgi request");
1023
1024 return;
1025 }
1026
1027
1028 static void
1029 ngx_http_uwsgi_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
1030 {
1031 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1032 "finalize http uwsgi request");
1033
1034 return;
1035 }
1036
1037
1038 static void *
1039 ngx_http_uwsgi_create_loc_conf(ngx_conf_t *cf)
1040 {
1041 ngx_http_uwsgi_loc_conf_t *conf;
1042
1043 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_uwsgi_loc_conf_t));
1044 if (conf == NULL) {
1045 return NULL;
1046 }
1047
1048 conf->modifier1 = NGX_CONF_UNSET_UINT;
1049 conf->modifier2 = NGX_CONF_UNSET_UINT;
1050
1051 conf->upstream.store = NGX_CONF_UNSET;
1052 conf->upstream.store_access = NGX_CONF_UNSET_UINT;
1053 conf->upstream.buffering = NGX_CONF_UNSET;
1054 conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
1055
1056 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
1057 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
1058 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
1059
1060 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
1061 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
1062
1063 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
1064 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
1065 conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;
1066
1067 conf->upstream.pass_request_headers = NGX_CONF_UNSET;
1068 conf->upstream.pass_request_body = NGX_CONF_UNSET;
1069
1070 #if (NGX_HTTP_CACHE)
1071 conf->upstream.cache = NGX_CONF_UNSET_PTR;
1072 conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
1073 conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
1074 conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
1075 conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
1076 #endif
1077
1078 conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
1079 conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;
1080
1081 conf->upstream.intercept_errors = NGX_CONF_UNSET;
1082
1083 /* "uwsgi_cyclic_temp_file" is disabled */
1084 conf->upstream.cyclic_temp_file = 0;
1085
1086 return conf;
1087 }
1088
1089
1090 static char *
1091 ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
1092 {
1093 ngx_http_uwsgi_loc_conf_t *prev = parent;
1094 ngx_http_uwsgi_loc_conf_t *conf = child;
1095
1096 u_char *p;
1097 size_t size;
1098 uintptr_t *code;
1099 ngx_uint_t i;
1100 ngx_array_t headers_names;
1101 ngx_keyval_t *src;
1102 ngx_hash_key_t *hk;
1103 ngx_hash_init_t hash;
1104 ngx_http_core_loc_conf_t *clcf;
1105 ngx_http_script_compile_t sc;
1106 ngx_http_script_copy_code_t *copy;
1107
1108 if (conf->upstream.store != 0) {
1109 ngx_conf_merge_value(conf->upstream.store, prev->upstream.store, 0);
1110
1111 if (conf->upstream.store_lengths == NULL) {
1112 conf->upstream.store_lengths = prev->upstream.store_lengths;
1113 conf->upstream.store_values = prev->upstream.store_values;
1114 }
1115 }
1116
1117 ngx_conf_merge_uint_value(conf->upstream.store_access,
1118 prev->upstream.store_access, 0600);
1119
1120 ngx_conf_merge_value(conf->upstream.buffering,
1121 prev->upstream.buffering, 1);
1122
1123 ngx_conf_merge_value(conf->upstream.ignore_client_abort,
1124 prev->upstream.ignore_client_abort, 0);
1125
1126 ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
1127 prev->upstream.connect_timeout, 60000);
1128
1129 ngx_conf_merge_msec_value(conf->upstream.send_timeout,
1130 prev->upstream.send_timeout, 60000);
1131
1132 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
1133 prev->upstream.read_timeout, 60000);
1134
1135 ngx_conf_merge_size_value(conf->upstream.send_lowat,
1136 prev->upstream.send_lowat, 0);
1137
1138 ngx_conf_merge_size_value(conf->upstream.buffer_size,
1139 prev->upstream.buffer_size,
1140 (size_t) ngx_pagesize);
1141
1142
1143 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
1144 8, ngx_pagesize);
1145
1146 if (conf->upstream.bufs.num < 2) {
1147 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1148 "there must be at least 2 \"uwsgi_buffers\"");
1149 return NGX_CONF_ERROR;
1150 }
1151
1152
1153 size = conf->upstream.buffer_size;
1154 if (size < conf->upstream.bufs.size) {
1155 size = conf->upstream.bufs.size;
1156 }
1157
1158
1159 ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
1160 prev->upstream.busy_buffers_size_conf,
1161 NGX_CONF_UNSET_SIZE);
1162
1163 if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
1164 conf->upstream.busy_buffers_size = 2 * size;
1165 } else {
1166 conf->upstream.busy_buffers_size =
1167 conf->upstream.busy_buffers_size_conf;
1168 }
1169
1170 if (conf->upstream.busy_buffers_size < size) {
1171 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1172 "\"uwsgi_busy_buffers_size\" must be equal or bigger "
1173 "than maximum of the value of \"uwsgi_buffer_size\" and "
1174 "one of the \"uwsgi_buffers\"");
1175
1176 return NGX_CONF_ERROR;
1177 }
1178
1179 if (conf->upstream.busy_buffers_size
1180 > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
1181 {
1182 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1183 "\"uwsgi_busy_buffers_size\" must be less than "
1184 "the size of all \"uwsgi_buffers\" minus one buffer");
1185
1186 return NGX_CONF_ERROR;
1187 }
1188
1189
1190 ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
1191 prev->upstream.temp_file_write_size_conf,
1192 NGX_CONF_UNSET_SIZE);
1193
1194 if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
1195 conf->upstream.temp_file_write_size = 2 * size;
1196 } else {
1197 conf->upstream.temp_file_write_size =
1198 conf->upstream.temp_file_write_size_conf;
1199 }
1200
1201 if (conf->upstream.temp_file_write_size < size) {
1202 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1203 "\"uwsgi_temp_file_write_size\" must be equal or bigger than "
1204 "maximum of the value of \"uwsgi_buffer_size\" and "
1205 "one of the \"uwsgi_buffers\"");
1206
1207 return NGX_CONF_ERROR;
1208 }
1209
1210
1211 ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
1212 prev->upstream.max_temp_file_size_conf,
1213 NGX_CONF_UNSET_SIZE);
1214
1215 if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
1216 conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
1217 } else {
1218 conf->upstream.max_temp_file_size =
1219 conf->upstream.max_temp_file_size_conf;
1220 }
1221
1222 if (conf->upstream.max_temp_file_size != 0
1223 && conf->upstream.max_temp_file_size < size) {
1224 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1225 "\"uwsgi_max_temp_file_size\" must be equal to zero to disable "
1226 "the temporary files usage or must be equal or bigger than "
1227 "maximum of the value of \"uwsgi_buffer_size\" and "
1228 "one of the \"uwsgi_buffers\"");
1229
1230 return NGX_CONF_ERROR;
1231 }
1232
1233
1234 ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers,
1235 prev->upstream.ignore_headers,
1236 NGX_CONF_BITMASK_SET);
1237
1238
1239 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
1240 prev->upstream.next_upstream,
1241 (NGX_CONF_BITMASK_SET
1242 |NGX_HTTP_UPSTREAM_FT_ERROR
1243 |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
1244
1245 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
1246 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
1247 |NGX_HTTP_UPSTREAM_FT_OFF;
1248 }
1249
1250 if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path,
1251 prev->upstream.temp_path,
1252 &ngx_http_uwsgi_temp_path)
1253 != NGX_OK)
1254 {
1255 return NGX_CONF_ERROR;
1256 }
1257
1258 #if (NGX_HTTP_CACHE)
1259
1260 ngx_conf_merge_ptr_value(conf->upstream.cache,
1261 prev->upstream.cache, NULL);
1262
1263 if (conf->upstream.cache && conf->upstream.cache->data == NULL) {
1264 ngx_shm_zone_t *shm_zone;
1265
1266 shm_zone = conf->upstream.cache;
1267
1268 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1269 "\"uwsgi_cache\" zone \"%V\" is unknown",
1270 &shm_zone->shm.name);
1271
1272 return NGX_CONF_ERROR;
1273 }
1274
1275 ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
1276 prev->upstream.cache_min_uses, 1);
1277
1278 ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
1279 prev->upstream.cache_use_stale,
1280 (NGX_CONF_BITMASK_SET
1281 |NGX_HTTP_UPSTREAM_FT_OFF));
1282
1283 if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {
1284 conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET
1285 |NGX_HTTP_UPSTREAM_FT_OFF;
1286 }
1287
1288 if (conf->upstream.cache_methods == 0) {
1289 conf->upstream.cache_methods = prev->upstream.cache_methods;
1290 }
1291
1292 conf->upstream.cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD;
1293
1294 ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,
1295 prev->upstream.cache_bypass, NULL);
1296
1297 ngx_conf_merge_ptr_value(conf->upstream.no_cache,
1298 prev->upstream.no_cache, NULL);
1299
1300 ngx_conf_merge_ptr_value(conf->upstream.cache_valid,
1301 prev->upstream.cache_valid, NULL);
1302
1303 if (conf->cache_key.value.data == NULL) {
1304 conf->cache_key = prev->cache_key;
1305 }
1306
1307 #endif
1308
1309 ngx_conf_merge_value(conf->upstream.pass_request_headers,
1310 prev->upstream.pass_request_headers, 1);
1311 ngx_conf_merge_value(conf->upstream.pass_request_body,
1312 prev->upstream.pass_request_body, 1);
1313
1314 ngx_conf_merge_value(conf->upstream.intercept_errors,
1315 prev->upstream.intercept_errors, 0);
1316
1317 ngx_conf_merge_str_value(conf->uwsgi_string, prev->uwsgi_string, "");
1318
1319 hash.max_size = 512;
1320 hash.bucket_size = ngx_align(64, ngx_cacheline_size);
1321 hash.name = "uwsgi_hide_headers_hash";
1322
1323 if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
1324 &prev->upstream, ngx_http_uwsgi_hide_headers, &hash)
1325 != NGX_OK)
1326 {
1327 return NGX_CONF_ERROR;
1328 }
1329
1330 if (conf->upstream.upstream == NULL) {
1331 conf->upstream.upstream = prev->upstream.upstream;
1332 }
1333
1334 if (conf->uwsgi_lengths == NULL) {
1335 conf->uwsgi_lengths = prev->uwsgi_lengths;
1336 conf->uwsgi_values = prev->uwsgi_values;
1337 }
1338
1339 if (conf->upstream.upstream || conf->uwsgi_lengths) {
1340 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
1341 if (clcf->handler == NULL && clcf->lmt_excpt) {
1342 clcf->handler = ngx_http_uwsgi_handler;
1343 }
1344 }
1345
1346 ngx_conf_merge_uint_value(conf->modifier1, prev->modifier1, 0);
1347 ngx_conf_merge_uint_value(conf->modifier2, prev->modifier2, 0);
1348
1349 if (conf->params_source == NULL) {
1350 conf->flushes = prev->flushes;
1351 conf->params_len = prev->params_len;
1352 conf->params = prev->params;
1353 conf->params_source = prev->params_source;
1354 conf->headers_hash = prev->headers_hash;
1355
1356 #if (NGX_HTTP_CACHE)
1357
1358 if (conf->params_source == NULL) {
1359
1360 if ((conf->upstream.cache == NULL)
1361 == (prev->upstream.cache == NULL))
1362 {
1363 return NGX_CONF_OK;
1364 }
1365
1366 /* 6 is a number of ngx_http_uwsgi_cache_headers entries */
1367 conf->params_source = ngx_array_create(cf->pool, 6,
1368 sizeof(ngx_keyval_t));
1369 if (conf->params_source == NULL) {
1370 return NGX_CONF_ERROR;
1371 }
1372 }
1373 #else
1374
1375 if (conf->params_source == NULL) {
1376 return NGX_CONF_OK;
1377 }
1378
1379 #endif
1380 }
1381
1382 conf->params_len = ngx_array_create(cf->pool, 64, 1);
1383 if (conf->params_len == NULL) {
1384 return NGX_CONF_ERROR;
1385 }
1386
1387 conf->params = ngx_array_create(cf->pool, 512, 1);
1388 if (conf->params == NULL) {
1389 return NGX_CONF_ERROR;
1390 }
1391
1392 if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
1393 != NGX_OK)
1394 {
1395 return NGX_CONF_ERROR;
1396 }
1397
1398 src = conf->params_source->elts;
1399
1400 #if (NGX_HTTP_CACHE)
1401
1402 if (conf->upstream.cache) {
1403 ngx_keyval_t *h, *s;
1404
1405 for (h = ngx_http_uwsgi_cache_headers; h->key.len; h++) {
1406
1407 for (i = 0; i < conf->params_source->nelts; i++) {
1408 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
1409 goto next;
1410 }
1411 }
1412
1413 s = ngx_array_push(conf->params_source);
1414 if (s == NULL) {
1415 return NGX_CONF_ERROR;
1416 }
1417
1418 *s = *h;
1419
1420 src = conf->params_source->elts;
1421
1422 next:
1423
1424 h++;
1425 }
1426 }
1427
1428 #endif
1429
1430 for (i = 0; i < conf->params_source->nelts; i++) {
1431
1432 if (src[i].key.len > sizeof("HTTP_") - 1
1433 && ngx_strncmp(src[i].key.data, "HTTP_", sizeof("HTTP_") - 1) == 0)
1434 {
1435 hk = ngx_array_push(&headers_names);
1436 if (hk == NULL) {
1437 return NGX_CONF_ERROR;
1438 }
1439
1440 hk->key.len = src[i].key.len - 5;
1441 hk->key.data = src[i].key.data + 5;
1442 hk->key_hash = ngx_hash_key_lc(hk->key.data, hk->key.len);
1443 hk->value = (void *) 1;
1444
1445 if (src[i].value.len == 0) {
1446 continue;
1447 }
1448 }
1449
1450 copy = ngx_array_push_n(conf->params_len,
1451 sizeof(ngx_http_script_copy_code_t));
1452 if (copy == NULL) {
1453 return NGX_CONF_ERROR;
1454 }
1455
1456 copy->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code;
1457 copy->len = src[i].key.len;
1458
1459
1460 size = (sizeof(ngx_http_script_copy_code_t)
1461 + src[i].key.len + sizeof(uintptr_t) - 1)
1462 & ~(sizeof(uintptr_t) - 1);
1463
1464 copy = ngx_array_push_n(conf->params, size);
1465 if (copy == NULL) {
1466 return NGX_CONF_ERROR;
1467 }
1468
1469 copy->code = ngx_http_script_copy_code;
1470 copy->len = src[i].key.len;
1471
1472 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
1473 ngx_memcpy(p, src[i].key.data, src[i].key.len);
1474
1475
1476 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
1477
1478 sc.cf = cf;
1479 sc.source = &src[i].value;
1480 sc.flushes = &conf->flushes;
1481 sc.lengths = &conf->params_len;
1482 sc.values = &conf->params;
1483
1484 if (ngx_http_script_compile(&sc) != NGX_OK) {
1485 return NGX_CONF_ERROR;
1486 }
1487
1488 code = ngx_array_push_n(conf->params_len, sizeof(uintptr_t));
1489 if (code == NULL) {
1490 return NGX_CONF_ERROR;
1491 }
1492
1493 *code = (uintptr_t) NULL;
1494
1495
1496 code = ngx_array_push_n(conf->params, sizeof(uintptr_t));
1497 if (code == NULL) {
1498 return NGX_CONF_ERROR;
1499 }
1500
1501 *code = (uintptr_t) NULL;
1502 }
1503
1504 code = ngx_array_push_n(conf->params_len, sizeof(uintptr_t));
1505 if (code == NULL) {
1506 return NGX_CONF_ERROR;
1507 }
1508
1509 *code = (uintptr_t) NULL;
1510
1511 conf->header_params = headers_names.nelts;
1512
1513 hash.hash = &conf->headers_hash;
1514 hash.key = ngx_hash_key_lc;
1515 hash.max_size = 512;
1516 hash.bucket_size = 64;
1517 hash.name = "uwsgi_params_hash";
1518 hash.pool = cf->pool;
1519 hash.temp_pool = NULL;
1520
1521 if (ngx_hash_init(&hash, headers_names.elts, headers_names.nelts) != NGX_OK)
1522 {
1523 return NGX_CONF_ERROR;
1524 }
1525
1526 return NGX_CONF_OK;
1527 }
1528
1529
1530 static char *
1531 ngx_http_uwsgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1532 {
1533 ngx_http_uwsgi_loc_conf_t *uwcf = conf;
1534
1535 ngx_url_t u;
1536 ngx_str_t *value, *url;
1537 ngx_uint_t n;
1538 ngx_http_core_loc_conf_t *clcf;
1539 ngx_http_script_compile_t sc;
1540
1541 if (uwcf->upstream.upstream || uwcf->uwsgi_lengths) {
1542 return "is duplicate";
1543 }
1544
1545 clcf = ngx_http_conf_get_module_loc_conf (cf, ngx_http_core_module);
1546 clcf->handler = ngx_http_uwsgi_handler;
1547
1548 value = cf->args->elts;
1549
1550 url = &value[1];
1551
1552 n = ngx_http_script_variables_count(url);
1553
1554 if (n) {
1555
1556 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
1557
1558 sc.cf = cf;
1559 sc.source = url;
1560 sc.lengths = &uwcf->uwsgi_lengths;
1561 sc.values = &uwcf->uwsgi_values;
1562 sc.variables = n;
1563 sc.complete_lengths = 1;
1564 sc.complete_values = 1;
1565
1566 if (ngx_http_script_compile(&sc) != NGX_OK) {
1567 return NGX_CONF_ERROR;
1568 }
1569
1570 return NGX_CONF_OK;
1571 }
1572
1573 ngx_memzero(&u, sizeof(ngx_url_t));
1574
1575 u.url = value[1];
1576 u.no_resolve = 1;
1577
1578 uwcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
1579 if (uwcf->upstream.upstream == NULL) {
1580 return NGX_CONF_ERROR;
1581 }
1582
1583 if (clcf->name.data[clcf->name.len - 1] == '/') {
1584 clcf->auto_redirect = 1;
1585 }
1586
1587 return NGX_CONF_OK;
1588 }
1589
1590
1591 static char *
1592 ngx_http_uwsgi_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1593 {
1594 ngx_http_uwsgi_loc_conf_t *uwcf = conf;
1595
1596 ngx_str_t *value;
1597 ngx_http_script_compile_t sc;
1598
1599 if (uwcf->upstream.store != NGX_CONF_UNSET || uwcf->upstream.store_lengths)
1600 {
1601 return "is duplicate";
1602 }
1603
1604 value = cf->args->elts;
1605
1606 if (ngx_strcmp(value[1].data, "off") == 0) {
1607 uwcf->upstream.store = 0;
1608 return NGX_CONF_OK;
1609 }
1610
1611 #if (NGX_HTTP_CACHE)
1612
1613 if (uwcf->upstream.cache != NGX_CONF_UNSET_PTR
1614 && uwcf->upstream.cache != NULL)
1615 {
1616 return "is incompatible with \"uwsgi_cache\"";
1617 }
1618
1619 #endif
1620
1621 if (ngx_strcmp(value[1].data, "on") == 0) {
1622 uwcf->upstream.store = 1;
1623 return NGX_CONF_OK;
1624 }
1625
1626 /* include the terminating '\0' into script */
1627 value[1].len++;
1628
1629 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
1630
1631 sc.cf = cf;
1632 sc.source = &value[1];
1633 sc.lengths = &uwcf->upstream.store_lengths;
1634 sc.values = &uwcf->upstream.store_values;
1635 sc.variables = ngx_http_script_variables_count(&value[1]);;
1636 sc.complete_lengths = 1;
1637 sc.complete_values = 1;
1638
1639 if (ngx_http_script_compile(&sc) != NGX_OK) {
1640 return NGX_CONF_ERROR;
1641 }
1642
1643 return NGX_CONF_OK;
1644 }
1645
1646
1647 #if (NGX_HTTP_CACHE)
1648
1649 static char *
1650 ngx_http_uwsgi_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1651 {
1652 ngx_http_uwsgi_loc_conf_t *uwcf = conf;
1653
1654 ngx_str_t *value;
1655
1656 value = cf->args->elts;
1657
1658 if (uwcf->upstream.cache != NGX_CONF_UNSET_PTR) {
1659 return "is duplicate";
1660 }
1661
1662 if (ngx_strcmp(value[1].data, "off") == 0) {
1663 uwcf->upstream.cache = NULL;
1664 return NGX_CONF_OK;
1665 }
1666
1667 if (uwcf->upstream.store > 0 || uwcf->upstream.store_lengths) {
1668 return "is incompatible with \"uwsgi_store\"";
1669 }
1670
1671 uwcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0,
1672 &ngx_http_uwsgi_module);
1673 if (uwcf->upstream.cache == NULL) {
1674 return NGX_CONF_ERROR;
1675 }
1676
1677 return NGX_CONF_OK;
1678 }
1679
1680
1681 static char *
1682 ngx_http_uwsgi_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1683 {
1684 ngx_http_uwsgi_loc_conf_t *uwcf = conf;
1685
1686 ngx_str_t *value;
1687 ngx_http_compile_complex_value_t ccv;
1688
1689 value = cf->args->elts;
1690
1691 if (uwcf->cache_key.value.len) {
1692 return "is duplicate";
1693 }
1694
1695 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
1696
1697 ccv.cf = cf;
1698 ccv.value = &value[1];
1699 ccv.complex_value = &uwcf->cache_key;
1700
1701 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
1702 return NGX_CONF_ERROR;
1703 }
1704
1705 return NGX_CONF_OK;
1706 }
1707
1708 #endif