Line data Source code
1 :
2 : /*
3 : * Copyright (C) Yichun Zhang (agentzh)
4 : */
5 :
6 :
7 : #ifndef DDEBUG
8 : #define DDEBUG 0
9 : #endif
10 : #include "ddebug.h"
11 :
12 :
13 : #include "ngx_http_echo_util.h"
14 : #include "ngx_http_echo_subrequest.h"
15 : #include "ngx_http_echo_handler.h"
16 : #include <nginx.h>
17 :
18 :
19 : #define ngx_http_echo_method_name(m) { sizeof(m) - 1, (u_char *) m " " }
20 :
21 :
22 : ngx_str_t ngx_http_echo_content_length_header_key =
23 : ngx_string("Content-Length");
24 :
25 : ngx_str_t ngx_http_echo_get_method = ngx_http_echo_method_name("GET");
26 : ngx_str_t ngx_http_echo_put_method = ngx_http_echo_method_name("PUT");
27 : ngx_str_t ngx_http_echo_post_method = ngx_http_echo_method_name("POST");
28 : ngx_str_t ngx_http_echo_head_method = ngx_http_echo_method_name("HEAD");
29 : ngx_str_t ngx_http_echo_copy_method = ngx_http_echo_method_name("COPY");
30 : ngx_str_t ngx_http_echo_move_method = ngx_http_echo_method_name("MOVE");
31 : ngx_str_t ngx_http_echo_lock_method = ngx_http_echo_method_name("LOCK");
32 : ngx_str_t ngx_http_echo_mkcol_method = ngx_http_echo_method_name("MKCOL");
33 : ngx_str_t ngx_http_echo_trace_method = ngx_http_echo_method_name("TRACE");
34 : ngx_str_t ngx_http_echo_delete_method = ngx_http_echo_method_name("DELETE");
35 : ngx_str_t ngx_http_echo_unlock_method = ngx_http_echo_method_name("UNLOCK");
36 : ngx_str_t ngx_http_echo_options_method = ngx_http_echo_method_name("OPTIONS");
37 : ngx_str_t ngx_http_echo_propfind_method =
38 : ngx_http_echo_method_name("PROPFIND");
39 : ngx_str_t ngx_http_echo_proppatch_method =
40 : ngx_http_echo_method_name("PROPPATCH");
41 :
42 :
43 : typedef struct ngx_http_echo_subrequest_s {
44 : ngx_uint_t method;
45 : ngx_str_t *method_name;
46 : ngx_str_t *location;
47 : ngx_str_t *query_string;
48 : ssize_t content_length_n;
49 : ngx_http_request_body_t *request_body;
50 : } ngx_http_echo_subrequest_t;
51 :
52 :
53 : static ngx_int_t ngx_http_echo_parse_method_name(ngx_str_t **method_name_ptr);
54 : static ngx_int_t ngx_http_echo_adjust_subrequest(ngx_http_request_t *sr,
55 : ngx_http_echo_subrequest_t *parsed_sr);
56 : static ngx_int_t ngx_http_echo_parse_subrequest_spec(ngx_http_request_t *r,
57 : ngx_array_t *computed_args, ngx_http_echo_subrequest_t **parsed_sr_ptr);
58 : static ngx_int_t ngx_http_echo_set_content_length_header(ngx_http_request_t *r,
59 : off_t len);
60 :
61 :
62 : ngx_int_t
63 0 : ngx_http_echo_exec_echo_subrequest_async(ngx_http_request_t *r,
64 : ngx_http_echo_ctx_t *ctx, ngx_array_t *computed_args)
65 : {
66 : ngx_int_t rc;
67 : ngx_http_echo_subrequest_t *parsed_sr;
68 : ngx_http_request_t *sr; /* subrequest object */
69 : ngx_str_t args;
70 0 : ngx_uint_t flags = 0;
71 :
72 : dd_enter();
73 :
74 0 : rc = ngx_http_echo_parse_subrequest_spec(r, computed_args, &parsed_sr);
75 0 : if (rc != NGX_OK) {
76 0 : return rc;
77 : }
78 :
79 : dd("location: %.*s",
80 : (int) parsed_sr->location->len,
81 : parsed_sr->location->data);
82 :
83 0 : args.data = NULL;
84 0 : args.len = 0;
85 :
86 0 : if (ngx_http_parse_unsafe_uri(r, parsed_sr->location, &args, &flags)
87 : != NGX_OK)
88 : {
89 0 : ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
90 : "echo_subrequest_async sees unsafe uri: \"%V\"",
91 : parsed_sr->location);
92 0 : return NGX_ERROR;
93 : }
94 :
95 0 : if (args.len > 0 && parsed_sr->query_string == NULL) {
96 0 : parsed_sr->query_string = &args;
97 : }
98 :
99 0 : rc = ngx_http_echo_send_header_if_needed(r, ctx);
100 0 : if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
101 0 : return rc;
102 : }
103 :
104 0 : rc = ngx_http_subrequest(r, parsed_sr->location, parsed_sr->query_string,
105 : &sr, NULL, 0);
106 :
107 0 : if (rc != NGX_OK) {
108 0 : return NGX_ERROR;
109 : }
110 :
111 0 : rc = ngx_http_echo_adjust_subrequest(sr, parsed_sr);
112 :
113 0 : if (rc != NGX_OK) {
114 0 : return rc;
115 : }
116 :
117 0 : return NGX_OK;
118 : }
119 :
120 :
121 : ngx_int_t
122 0 : ngx_http_echo_exec_echo_subrequest(ngx_http_request_t *r,
123 : ngx_http_echo_ctx_t *ctx, ngx_array_t *computed_args)
124 : {
125 : ngx_int_t rc;
126 : ngx_http_request_t *sr; /* subrequest object */
127 : ngx_http_post_subrequest_t *psr;
128 : ngx_http_echo_subrequest_t *parsed_sr;
129 : ngx_str_t args;
130 0 : ngx_uint_t flags = 0;
131 : ngx_http_echo_ctx_t *sr_ctx;
132 :
133 : dd_enter();
134 :
135 0 : rc = ngx_http_echo_parse_subrequest_spec(r, computed_args, &parsed_sr);
136 0 : if (rc != NGX_OK) {
137 0 : return rc;
138 : }
139 :
140 0 : args.data = NULL;
141 0 : args.len = 0;
142 :
143 0 : if (ngx_http_parse_unsafe_uri(r, parsed_sr->location, &args, &flags)
144 : != NGX_OK)
145 : {
146 0 : ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
147 : "echo_subrequest sees unsafe uri: \"%V\"",
148 : parsed_sr->location);
149 0 : return NGX_ERROR;
150 : }
151 :
152 0 : if (args.len > 0 && parsed_sr->query_string == NULL) {
153 0 : parsed_sr->query_string = &args;
154 : }
155 :
156 0 : rc = ngx_http_echo_send_header_if_needed(r, ctx);
157 0 : if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
158 0 : return rc;
159 : }
160 :
161 0 : sr_ctx = ngx_http_echo_create_ctx(r);
162 :
163 : /* set by ngx_http_echo_create_ctx
164 : * sr_ctx->run_post_subrequest = 0
165 : */
166 :
167 : dd("creating sr ctx for %.*s: %p", (int) parsed_sr->location->len,
168 : parsed_sr->location->data, sr_ctx);
169 :
170 0 : psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
171 :
172 0 : if (psr == NULL) {
173 0 : return NGX_ERROR;
174 : }
175 :
176 0 : psr->handler = ngx_http_echo_post_subrequest;
177 0 : psr->data = sr_ctx;
178 :
179 0 : rc = ngx_http_subrequest(r, parsed_sr->location, parsed_sr->query_string,
180 : &sr, psr, 0);
181 :
182 0 : if (rc != NGX_OK) {
183 0 : return NGX_ERROR;
184 : }
185 :
186 0 : sr_ctx->sleep.data = sr;
187 :
188 0 : ngx_http_set_ctx(sr, sr_ctx, ngx_http_echo_module);
189 :
190 0 : rc = ngx_http_echo_adjust_subrequest(sr, parsed_sr);
191 :
192 0 : if (rc != NGX_OK) {
193 0 : return NGX_ERROR;
194 : }
195 :
196 0 : return NGX_AGAIN;
197 : }
198 :
199 :
200 : static ngx_int_t
201 0 : ngx_http_echo_parse_subrequest_spec(ngx_http_request_t *r,
202 : ngx_array_t *computed_args, ngx_http_echo_subrequest_t **parsed_sr_ptr)
203 : {
204 : ngx_str_t *computed_arg_elts, *arg;
205 0 : ngx_str_t **to_write = NULL;
206 : ngx_str_t *method_name;
207 0 : ngx_str_t *body_str = NULL;
208 0 : ngx_str_t *body_file = NULL;
209 : ngx_uint_t i;
210 : ngx_flag_t expecting_opt;
211 0 : ngx_http_request_body_t *rb = NULL;
212 : ngx_buf_t *b;
213 : ngx_http_echo_subrequest_t *parsed_sr;
214 : ngx_open_file_info_t of;
215 : ngx_http_core_loc_conf_t *clcf;
216 : size_t len;
217 :
218 0 : *parsed_sr_ptr = ngx_pcalloc(r->pool, sizeof(ngx_http_echo_subrequest_t));
219 0 : if (*parsed_sr_ptr == NULL) {
220 0 : return NGX_ERROR;
221 : }
222 :
223 0 : parsed_sr = *parsed_sr_ptr;
224 0 : computed_arg_elts = computed_args->elts;
225 0 : method_name = &computed_arg_elts[0];
226 0 : parsed_sr->location = &computed_arg_elts[1];
227 :
228 0 : if (parsed_sr->location->len == 0) {
229 0 : return NGX_ERROR;
230 : }
231 :
232 0 : expecting_opt = 1;
233 :
234 0 : for (i = 2; i < computed_args->nelts; i++) {
235 0 : arg = &computed_arg_elts[i];
236 :
237 0 : if (!expecting_opt) {
238 0 : if (to_write == NULL) {
239 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
240 : "echo_subrequest_async: to_write should NOT be NULL");
241 0 : return NGX_ERROR;
242 : }
243 :
244 0 : *to_write = arg;
245 0 : to_write = NULL;
246 :
247 0 : expecting_opt = 1;
248 :
249 0 : continue;
250 : }
251 :
252 0 : if (arg->len == 2) {
253 0 : if (ngx_strncmp("-q", arg->data, arg->len) == 0) {
254 0 : to_write = &parsed_sr->query_string;
255 0 : expecting_opt = 0;
256 0 : continue;
257 : }
258 :
259 0 : if (ngx_strncmp("-b", arg->data, arg->len) == 0) {
260 0 : to_write = &body_str;
261 0 : expecting_opt = 0;
262 0 : continue;
263 : }
264 :
265 0 : if (ngx_strncmp("-f", arg->data, arg->len) == 0) {
266 : dd("found option -f");
267 0 : to_write = &body_file;
268 0 : expecting_opt = 0;
269 0 : continue;
270 : }
271 : }
272 :
273 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
274 : "unknown option for echo_subrequest*: %V", arg);
275 :
276 0 : return NGX_ERROR;
277 : }
278 :
279 0 : if (body_str != NULL && body_str->len) {
280 0 : rb = ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t));
281 :
282 0 : if (rb == NULL) {
283 0 : return NGX_ERROR;
284 : }
285 :
286 0 : parsed_sr->content_length_n = body_str->len;
287 :
288 0 : b = ngx_calloc_buf(r->pool);
289 0 : if (b == NULL) {
290 0 : return NGX_ERROR;
291 : }
292 :
293 0 : b->temporary = 1;
294 : /* b->memory = 1; */
295 0 : b->start = b->pos = body_str->data;
296 0 : b->end = b->last = body_str->data + body_str->len;
297 :
298 0 : rb->bufs = ngx_alloc_chain_link(r->pool);
299 0 : if (rb->bufs == NULL) {
300 0 : return NGX_ERROR;
301 : }
302 :
303 0 : rb->bufs->buf = b;
304 0 : rb->bufs->next = NULL;
305 :
306 0 : rb->buf = b;
307 :
308 0 : } else if (body_file != NULL && body_file->len) {
309 :
310 : dd("body_file defined %.*s", (int) body_file->len, body_file->data);
311 :
312 0 : body_file->data = ngx_http_echo_rebase_path(r->pool, body_file->data,
313 0 : body_file->len, &len);
314 :
315 0 : if (body_file->data == NULL) {
316 0 : return NGX_ERROR;
317 : }
318 :
319 0 : body_file->len = len;
320 :
321 : dd("after rebase, the path becomes %.*s", (int) body_file->len,
322 : body_file->data);
323 :
324 0 : rb = ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t));
325 0 : if (rb == NULL) {
326 0 : return NGX_ERROR;
327 : }
328 :
329 0 : clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
330 0 : ngx_memzero(&of, sizeof(ngx_open_file_info_t));
331 :
332 : #if defined(nginx_version) && nginx_version >= 8018
333 0 : of.read_ahead = clcf->read_ahead;
334 : #endif
335 :
336 0 : of.directio = clcf->directio;
337 0 : of.valid = clcf->open_file_cache_valid;
338 0 : of.min_uses = clcf->open_file_cache_min_uses;
339 0 : of.errors = clcf->open_file_cache_errors;
340 0 : of.events = clcf->open_file_cache_events;
341 :
342 0 : if (ngx_open_cached_file(clcf->open_file_cache, body_file, &of, r->pool)
343 : != NGX_OK)
344 : {
345 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, of.err,
346 : "%s \"%V\" failed",
347 : of.failed, body_file);
348 :
349 0 : return NGX_ERROR;
350 : }
351 :
352 : dd("file content size: %d", (int) of.size);
353 :
354 0 : parsed_sr->content_length_n = (ssize_t) of.size;
355 :
356 0 : b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
357 0 : if (b == NULL) {
358 0 : return NGX_ERROR;
359 : }
360 :
361 0 : b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
362 0 : if (b->file == NULL) {
363 0 : return NGX_ERROR;
364 : }
365 :
366 0 : b->file_pos = 0;
367 0 : b->file_last = of.size;
368 :
369 0 : b->in_file = b->file_last ? 1: 0;
370 :
371 : #if 0
372 : b->last_buf = (r == r->main) ? 1: 0;
373 : b->last_in_chain = 1;
374 : #endif
375 :
376 0 : b->file->fd = of.fd;
377 0 : b->file->name = *body_file;
378 0 : b->file->log = r->connection->log;
379 0 : b->file->directio = of.is_directio;
380 :
381 0 : rb->bufs = ngx_alloc_chain_link(r->pool);
382 0 : if (rb->bufs == NULL) {
383 0 : return NGX_ERROR;
384 : }
385 :
386 0 : rb->bufs->buf = b;
387 0 : rb->bufs->next = NULL;
388 0 : rb->buf = b;
389 : }
390 :
391 0 : parsed_sr->request_body = rb;
392 :
393 0 : parsed_sr->method = ngx_http_echo_parse_method_name(&method_name);
394 0 : parsed_sr->method_name = method_name;
395 :
396 0 : return NGX_OK;
397 : }
398 :
399 :
400 : static ngx_int_t
401 0 : ngx_http_echo_adjust_subrequest(ngx_http_request_t *sr,
402 : ngx_http_echo_subrequest_t *parsed_sr)
403 : {
404 : ngx_http_core_main_conf_t *cmcf;
405 : ngx_http_request_t *r;
406 : ngx_http_request_body_t *body;
407 : ngx_int_t rc;
408 :
409 0 : sr->method = parsed_sr->method;
410 0 : sr->method_name = *(parsed_sr->method_name);
411 :
412 0 : if (sr->method == NGX_HTTP_HEAD) {
413 0 : sr->header_only = 1;
414 : }
415 :
416 0 : r = sr->parent;
417 :
418 0 : sr->header_in = r->header_in;
419 :
420 : /* XXX work-around a bug in ngx_http_subrequest */
421 0 : if (r->headers_in.headers.last == &r->headers_in.headers.part) {
422 0 : sr->headers_in.headers.last = &sr->headers_in.headers.part;
423 : }
424 :
425 : /* we do not inherit the parent request's variables */
426 0 : cmcf = ngx_http_get_module_main_conf(sr, ngx_http_core_module);
427 0 : sr->variables = ngx_pcalloc(sr->pool, cmcf->variables.nelts
428 : * sizeof(ngx_http_variable_value_t));
429 :
430 0 : if (sr->variables == NULL) {
431 0 : return NGX_ERROR;
432 : }
433 :
434 0 : body = parsed_sr->request_body;
435 0 : if (body) {
436 0 : sr->request_body = body;
437 :
438 0 : rc = ngx_http_echo_set_content_length_header(sr, body->buf ?
439 0 : ngx_buf_size(body->buf)
440 : : 0);
441 :
442 0 : if (rc != NGX_OK) {
443 0 : return NGX_ERROR;
444 : }
445 : }
446 :
447 : dd("subrequest body: %p", sr->request_body);
448 :
449 0 : return NGX_OK;
450 : }
451 :
452 :
453 : static ngx_int_t
454 0 : ngx_http_echo_parse_method_name(ngx_str_t **method_name_ptr)
455 : {
456 0 : const ngx_str_t *method_name = *method_name_ptr;
457 :
458 0 : switch (method_name->len) {
459 0 : case 3:
460 0 : if (ngx_http_echo_strcmp_const(method_name->data, "GET") == 0) {
461 0 : *method_name_ptr = &ngx_http_echo_get_method;
462 0 : return NGX_HTTP_GET;
463 : }
464 :
465 0 : if (ngx_http_echo_strcmp_const(method_name->data, "PUT") == 0) {
466 0 : *method_name_ptr = &ngx_http_echo_put_method;
467 0 : return NGX_HTTP_PUT;
468 : }
469 :
470 0 : return NGX_HTTP_UNKNOWN;
471 :
472 0 : case 4:
473 0 : if (ngx_http_echo_strcmp_const(method_name->data, "POST") == 0) {
474 0 : *method_name_ptr = &ngx_http_echo_post_method;
475 0 : return NGX_HTTP_POST;
476 : }
477 :
478 0 : if (ngx_http_echo_strcmp_const(method_name->data, "HEAD") == 0) {
479 0 : *method_name_ptr = &ngx_http_echo_head_method;
480 0 : return NGX_HTTP_HEAD;
481 : }
482 :
483 0 : if (ngx_http_echo_strcmp_const(method_name->data, "COPY") == 0) {
484 0 : *method_name_ptr = &ngx_http_echo_copy_method;
485 0 : return NGX_HTTP_COPY;
486 : }
487 :
488 0 : if (ngx_http_echo_strcmp_const(method_name->data, "MOVE") == 0) {
489 0 : *method_name_ptr = &ngx_http_echo_move_method;
490 0 : return NGX_HTTP_MOVE;
491 : }
492 :
493 0 : if (ngx_http_echo_strcmp_const(method_name->data, "LOCK") == 0) {
494 0 : *method_name_ptr = &ngx_http_echo_lock_method;
495 0 : return NGX_HTTP_LOCK;
496 : }
497 :
498 0 : return NGX_HTTP_UNKNOWN;
499 :
500 0 : case 5:
501 0 : if (ngx_http_echo_strcmp_const(method_name->data, "MKCOL") == 0) {
502 0 : *method_name_ptr = &ngx_http_echo_mkcol_method;
503 0 : return NGX_HTTP_MKCOL;
504 : }
505 :
506 0 : if (ngx_http_echo_strcmp_const(method_name->data, "TRACE") == 0) {
507 0 : *method_name_ptr = &ngx_http_echo_trace_method;
508 0 : return NGX_HTTP_TRACE;
509 : }
510 :
511 0 : return NGX_HTTP_UNKNOWN;
512 :
513 0 : case 6:
514 0 : if (ngx_http_echo_strcmp_const(method_name->data, "DELETE") == 0) {
515 0 : *method_name_ptr = &ngx_http_echo_delete_method;
516 0 : return NGX_HTTP_DELETE;
517 : }
518 :
519 0 : if (ngx_http_echo_strcmp_const(method_name->data, "UNLOCK") == 0) {
520 0 : *method_name_ptr = &ngx_http_echo_unlock_method;
521 0 : return NGX_HTTP_UNLOCK;
522 : }
523 :
524 0 : return NGX_HTTP_UNKNOWN;
525 :
526 0 : case 7:
527 0 : if (ngx_http_echo_strcmp_const(method_name->data, "OPTIONS") == 0) {
528 0 : *method_name_ptr = &ngx_http_echo_options_method;
529 0 : return NGX_HTTP_OPTIONS;
530 : }
531 :
532 0 : return NGX_HTTP_UNKNOWN;
533 :
534 0 : case 8:
535 0 : if (ngx_http_echo_strcmp_const(method_name->data, "PROPFIND") == 0) {
536 0 : *method_name_ptr = &ngx_http_echo_propfind_method;
537 0 : return NGX_HTTP_PROPFIND;
538 : }
539 :
540 0 : return NGX_HTTP_UNKNOWN;
541 :
542 0 : case 9:
543 0 : if (ngx_http_echo_strcmp_const(method_name->data, "PROPPATCH") == 0) {
544 0 : *method_name_ptr = &ngx_http_echo_proppatch_method;
545 0 : return NGX_HTTP_PROPPATCH;
546 : }
547 :
548 0 : return NGX_HTTP_UNKNOWN;
549 :
550 0 : default:
551 0 : return NGX_HTTP_UNKNOWN;
552 : }
553 : }
554 :
555 :
556 : /* XXX extermely evil and not working yet */
557 : ngx_int_t
558 0 : ngx_http_echo_exec_abort_parent(ngx_http_request_t *r,
559 : ngx_http_echo_ctx_t *ctx)
560 : {
561 : #if 0
562 : ngx_http_postponed_request_t *pr, *ppr;
563 : ngx_http_request_t *saved_data = NULL;
564 : ngx_chain_t *out = NULL;
565 : /* ngx_int_t rc; */
566 :
567 : dd("aborting parent...");
568 :
569 : if (r == r->main || r->parent == NULL) {
570 : return NGX_OK;
571 : }
572 :
573 : if (r->parent->postponed) {
574 : dd("Found parent->postponed...");
575 :
576 : saved_data = r->connection->data;
577 : ppr = NULL;
578 : for (pr = r->parent->postponed; pr->next; pr = pr->next) {
579 : if (pr->request == NULL) {
580 : continue;
581 : }
582 :
583 : if (pr->request == r) {
584 : /* r->parent->postponed->next = pr; */
585 : dd("found the current subrequest");
586 : out = pr->out;
587 : continue;
588 : }
589 :
590 : /* r->connection->data = pr->request; */
591 : dd("finalizing the subrequest...");
592 : ngx_http_upstream_create(pr->request);
593 : pr->request->upstream = NULL;
594 :
595 : if (ppr == NULL) {
596 : r->parent->postponed = pr->next;
597 : ppr = pr->next;
598 : } else {
599 : ppr->next = pr->next;
600 : ppr = pr->next;
601 : }
602 : }
603 : }
604 :
605 : r->parent->postponed->next = NULL;
606 :
607 : /*
608 : r->connection->data = r->parent;
609 : r->connection->buffered = 0;
610 :
611 : if (out != NULL) {
612 : dd("trying to send more stuffs for the parent");
613 : ngx_http_output_filter(r->parent, out);
614 : }
615 : */
616 :
617 : /* ngx_http_send_special(r->parent, NGX_HTTP_LAST); */
618 :
619 : if (saved_data) {
620 : r->connection->data = saved_data;
621 : }
622 :
623 : dd("terminating the parent request");
624 :
625 : return ngx_http_echo_send_chain_link(r, ctx, NULL /* indicate LAST */);
626 :
627 : /* ngx_http_upstream_create(r); */
628 :
629 : /* ngx_http_finalize_request(r->parent, NGX_ERROR); */
630 : #endif
631 :
632 0 : return NGX_OK;
633 : }
634 :
635 :
636 : ngx_int_t
637 0 : ngx_http_echo_exec_exec(ngx_http_request_t *r,
638 : ngx_http_echo_ctx_t *ctx, ngx_array_t *computed_args)
639 : {
640 : ngx_str_t *uri;
641 : ngx_str_t *user_args;
642 : ngx_str_t args;
643 : ngx_uint_t flags;
644 : ngx_str_t *computed_arg;
645 :
646 0 : computed_arg = computed_args->elts;
647 :
648 0 : uri = &computed_arg[0];
649 :
650 0 : if (uri->len == 0) {
651 0 : return NGX_HTTP_BAD_REQUEST;
652 : }
653 :
654 0 : if (computed_args->nelts > 1) {
655 0 : user_args = &computed_arg[1];
656 :
657 : } else {
658 0 : user_args = NULL;
659 : }
660 :
661 0 : args.data = NULL;
662 0 : args.len = 0;
663 :
664 0 : if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) {
665 0 : ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
666 : "echo_exec sees unsafe uri: \"%V\"",
667 : uri);
668 0 : return NGX_ERROR;
669 : }
670 :
671 0 : if (args.len > 0 && user_args == NULL) {
672 0 : user_args = &args;
673 : }
674 :
675 0 : r->write_event_handler = ngx_http_request_empty_handler;
676 :
677 0 : if (uri->data[0] == '@') {
678 :
679 0 : if (user_args && user_args->len > 0) {
680 0 : ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
681 : "querystring %V ignored when exec'ing named "
682 : "location %V", user_args, uri);
683 : }
684 :
685 : #if 1
686 : /* clear the modules contexts */
687 0 : ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);
688 : #endif
689 :
690 : dd("named location: %.*s, c:%d", (int) uri->len, uri->data,
691 : (int) r->main->count);
692 :
693 0 : return ngx_http_named_location(r, uri);
694 : }
695 :
696 0 : return ngx_http_internal_redirect(r, uri, user_args);
697 : }
698 :
699 :
700 : static ngx_int_t
701 0 : ngx_http_echo_set_content_length_header(ngx_http_request_t *r, off_t len)
702 : {
703 : ngx_table_elt_t *h, *header;
704 : u_char *p;
705 : ngx_list_part_t *part;
706 : ngx_http_request_t *pr;
707 : ngx_uint_t i;
708 :
709 0 : r->headers_in.content_length_n = len;
710 :
711 0 : if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
712 : sizeof(ngx_table_elt_t))
713 : != NGX_OK)
714 : {
715 0 : return NGX_ERROR;
716 : }
717 :
718 0 : h = ngx_list_push(&r->headers_in.headers);
719 0 : if (h == NULL) {
720 0 : return NGX_ERROR;
721 : }
722 :
723 0 : h->key = ngx_http_echo_content_length_header_key;
724 0 : h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
725 0 : if (h->lowcase_key == NULL) {
726 0 : return NGX_ERROR;
727 : }
728 :
729 0 : ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
730 :
731 0 : r->headers_in.content_length = h;
732 :
733 0 : p = ngx_palloc(r->pool, NGX_OFF_T_LEN);
734 0 : if (p == NULL) {
735 0 : return NGX_ERROR;
736 : }
737 :
738 0 : h->value.data = p;
739 :
740 0 : h->value.len = ngx_sprintf(h->value.data, "%O", len) - h->value.data;
741 :
742 0 : h->hash = ngx_http_echo_content_length_hash;
743 :
744 : dd("r content length: %.*s",
745 : (int) r->headers_in.content_length->value.len,
746 : r->headers_in.content_length->value.data);
747 :
748 0 : pr = r->parent;
749 :
750 0 : if (pr == NULL) {
751 0 : return NGX_OK;
752 : }
753 :
754 : /* forward the parent request's all other request headers */
755 :
756 0 : part = &pr->headers_in.headers.part;
757 0 : header = part->elts;
758 :
759 0 : for (i = 0; /* void */; i++) {
760 :
761 0 : if (i >= part->nelts) {
762 0 : if (part->next == NULL) {
763 0 : break;
764 : }
765 :
766 0 : part = part->next;
767 0 : header = part->elts;
768 0 : i = 0;
769 : }
770 :
771 0 : if (header[i].key.len == sizeof("Content-Length") - 1
772 0 : && ngx_strncasecmp(header[i].key.data, (u_char *) "Content-Length",
773 : sizeof("Content-Length") - 1)
774 : == 0)
775 : {
776 0 : continue;
777 : }
778 :
779 0 : h = ngx_list_push(&r->headers_in.headers);
780 0 : if (h == NULL) {
781 0 : return NGX_ERROR;
782 : }
783 :
784 0 : *h = header[i];
785 : }
786 :
787 : /* XXX maybe we should set those built-in header slot in
788 : * ngx_http_headers_in_t too? */
789 :
790 0 : return NGX_OK;
791 : }
|