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