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