-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathngx_http_echo_location.c
182 lines (134 loc) · 4.4 KB
/
ngx_http_echo_location.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_echo_util.h"
#include "ngx_http_echo_location.h"
#include "ngx_http_echo_handler.h"
#include <nginx.h>
static ngx_int_t ngx_http_echo_adjust_subrequest(ngx_http_request_t *sr);
ngx_int_t
ngx_http_echo_exec_echo_location_async(ngx_http_request_t *r,
ngx_http_echo_ctx_t *ctx, ngx_array_t *computed_args)
{
ngx_int_t rc;
ngx_http_request_t *sr; /* subrequest object */
ngx_str_t *computed_arg_elts;
ngx_str_t location;
ngx_str_t *url_args;
ngx_str_t args;
ngx_uint_t flags = 0;
dd_enter();
computed_arg_elts = computed_args->elts;
location = computed_arg_elts[0];
if (location.len == 0) {
return NGX_ERROR;
}
if (computed_args->nelts > 1) {
url_args = &computed_arg_elts[1];
} else {
url_args = NULL;
}
args.data = NULL;
args.len = 0;
if (ngx_http_parse_unsafe_uri(r, &location, &args, &flags) != NGX_OK) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"echo_location_async sees unsafe uri: \"%V\"",
&location);
return NGX_ERROR;
}
if (args.len > 0 && url_args == NULL) {
url_args = &args;
}
rc = ngx_http_echo_send_header_if_needed(r, ctx);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
rc = ngx_http_subrequest(r, &location, url_args, &sr, NULL, 0);
if (rc != NGX_OK) {
return NGX_ERROR;
}
rc = ngx_http_echo_adjust_subrequest(sr);
if (rc != NGX_OK) {
return NGX_ERROR;
}
return NGX_OK;
}
ngx_int_t
ngx_http_echo_exec_echo_location(ngx_http_request_t *r,
ngx_http_echo_ctx_t *ctx, ngx_array_t *computed_args)
{
ngx_int_t rc;
ngx_http_request_t *sr; /* subrequest object */
ngx_str_t *computed_arg_elts;
ngx_str_t location;
ngx_str_t *url_args;
ngx_http_post_subrequest_t *psr;
ngx_str_t args;
ngx_uint_t flags = 0;
ngx_http_echo_ctx_t *sr_ctx;
if (computed_args == NULL) {
return NGX_ERROR;
}
computed_arg_elts = computed_args->elts;
location = computed_arg_elts[0];
if (location.len == 0) {
return NGX_ERROR;
}
if (computed_args->nelts > 1) {
url_args = &computed_arg_elts[1];
} else {
url_args = NULL;
}
args.data = NULL;
args.len = 0;
if (ngx_http_parse_unsafe_uri(r, &location, &args, &flags) != NGX_OK) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"echo_location sees unsafe uri: \"%V\"",
&location);
return NGX_ERROR;
}
if (args.len > 0 && url_args == NULL) {
url_args = &args;
}
rc = ngx_http_echo_send_header_if_needed(r, ctx);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
sr_ctx = ngx_http_echo_create_ctx(r);
psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
if (psr == NULL) {
return NGX_ERROR;
}
psr->handler = ngx_http_echo_post_subrequest;
psr->data = sr_ctx;
rc = ngx_http_subrequest(r, &location, url_args, &sr, psr, 0);
if (rc != NGX_OK) {
return NGX_ERROR;
}
rc = ngx_http_echo_adjust_subrequest(sr);
if (rc != NGX_OK) {
return NGX_ERROR;
}
return NGX_AGAIN;
}
static ngx_int_t
ngx_http_echo_adjust_subrequest(ngx_http_request_t *sr)
{
ngx_http_core_main_conf_t *cmcf;
ngx_http_request_t *r;
/* we do not inherit the parent request's variables */
cmcf = ngx_http_get_module_main_conf(sr, ngx_http_core_module);
r = sr->parent;
sr->header_in = r->header_in;
/* XXX work-around a bug in ngx_http_subrequest */
if (r->headers_in.headers.last == &r->headers_in.headers.part) {
sr->headers_in.headers.last = &sr->headers_in.headers.part;
}
sr->variables = ngx_pcalloc(sr->pool, cmcf->variables.nelts
* sizeof(ngx_http_variable_value_t));
if (sr->variables == NULL) {
return NGX_ERROR;
}
return NGX_OK;
}