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