-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathngx_http_echo_echo.c
342 lines (252 loc) · 8.05 KB
/
ngx_http_echo_echo.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_echo_echo.h"
#include "ngx_http_echo_util.h"
#include "ngx_http_echo_filter.h"
#include <nginx.h>
static ngx_buf_t ngx_http_echo_space_buf;
static ngx_buf_t ngx_http_echo_newline_buf;
ngx_int_t
ngx_http_echo_echo_init(ngx_conf_t *cf)
{
static u_char space_str[] = " ";
static u_char newline_str[] = "\n";
dd("global init...");
ngx_memzero(&ngx_http_echo_space_buf, sizeof(ngx_buf_t));
ngx_http_echo_space_buf.memory = 1;
ngx_http_echo_space_buf.start =
ngx_http_echo_space_buf.pos =
space_str;
ngx_http_echo_space_buf.end =
ngx_http_echo_space_buf.last =
space_str + sizeof(space_str) - 1;
ngx_memzero(&ngx_http_echo_newline_buf, sizeof(ngx_buf_t));
ngx_http_echo_newline_buf.memory = 1;
ngx_http_echo_newline_buf.start =
ngx_http_echo_newline_buf.pos =
newline_str;
ngx_http_echo_newline_buf.end =
ngx_http_echo_newline_buf.last =
newline_str + sizeof(newline_str) - 1;
return NGX_OK;
}
ngx_int_t
ngx_http_echo_exec_echo_sync(ngx_http_request_t *r,
ngx_http_echo_ctx_t *ctx)
{
ngx_buf_t *buf;
ngx_chain_t *cl = NULL; /* the head of the chain link */
buf = ngx_calloc_buf(r->pool);
if (buf == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
buf->sync = 1;
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
cl->buf = buf;
cl->next = NULL;
return ngx_http_echo_send_chain_link(r, ctx, cl);
}
ngx_int_t
ngx_http_echo_exec_echo(ngx_http_request_t *r,
ngx_http_echo_ctx_t *ctx, ngx_array_t *computed_args,
ngx_flag_t in_filter, ngx_array_t *opts)
{
ngx_uint_t i;
ngx_buf_t *space_buf;
ngx_buf_t *newline_buf;
ngx_buf_t *buf;
ngx_str_t *computed_arg;
ngx_str_t *computed_arg_elts;
ngx_str_t *opt;
ngx_chain_t *cl = NULL; /* the head of the chain link */
ngx_chain_t **ll = &cl; /* always point to the address of the last link */
dd_enter();
if (computed_args == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
computed_arg_elts = computed_args->elts;
for (i = 0; i < computed_args->nelts; i++) {
computed_arg = &computed_arg_elts[i];
if (computed_arg->len == 0) {
buf = NULL;
} else {
buf = ngx_calloc_buf(r->pool);
if (buf == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
buf->start = buf->pos = computed_arg->data;
buf->last = buf->end = computed_arg->data +
computed_arg->len;
buf->memory = 1;
}
if (cl == NULL) {
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
cl->buf = buf;
cl->next = NULL;
ll = &cl->next;
} else {
/* append a space first */
*ll = ngx_alloc_chain_link(r->pool);
if (*ll == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
space_buf = ngx_calloc_buf(r->pool);
if (space_buf == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* nginx clears buf flags at the end of each request handling,
* so we have to make a clone here. */
*space_buf = ngx_http_echo_space_buf;
(*ll)->buf = space_buf;
(*ll)->next = NULL;
ll = &(*ll)->next;
/* then append the buf only if it's non-empty */
if (buf) {
*ll = ngx_alloc_chain_link(r->pool);
if (*ll == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
(*ll)->buf = buf;
(*ll)->next = NULL;
ll = &(*ll)->next;
}
}
} /* end for */
if (cl && cl->buf == NULL) {
cl = cl->next;
}
if (opts && opts->nelts > 0) {
opt = opts->elts;
/* FIXME handle other unrecognized options here */
if (opt[0].len == 1 && opt[0].data[0] == 'n') {
goto done;
}
}
/* append the newline character */
newline_buf = ngx_calloc_buf(r->pool);
if (newline_buf == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
*newline_buf = ngx_http_echo_newline_buf;
if (cl == NULL) {
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
cl->buf = newline_buf;
cl->next = NULL;
/* ll = &cl->next; */
} else {
*ll = ngx_alloc_chain_link(r->pool);
if (*ll == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
(*ll)->buf = newline_buf;
(*ll)->next = NULL;
/* ll = &(*ll)->next; */
}
done:
if (cl == NULL || cl->buf == NULL) {
return NGX_OK;
}
if (in_filter) {
return ngx_http_echo_next_body_filter(r, cl);
}
return ngx_http_echo_send_chain_link(r, ctx, cl);
}
ngx_int_t
ngx_http_echo_exec_echo_flush(ngx_http_request_t *r, ngx_http_echo_ctx_t *ctx)
{
return ngx_http_send_special(r, NGX_HTTP_FLUSH);
}
ngx_int_t
ngx_http_echo_exec_echo_request_body(ngx_http_request_t *r,
ngx_http_echo_ctx_t *ctx)
{
ngx_buf_t *b;
ngx_chain_t *out, *cl, **ll;
if (r->request_body == NULL || r->request_body->bufs == NULL) {
return NGX_OK;
}
out = NULL;
ll = &out;
for (cl = r->request_body->bufs; cl; cl = cl->next) {
if (ngx_buf_special(cl->buf)) {
/* we do not want to create zero-size bufs */
continue;
}
*ll = ngx_alloc_chain_link(r->pool);
if (*ll == NULL) {
return NGX_ERROR;
}
b = ngx_alloc_buf(r->pool);
if (b == NULL) {
return NGX_ERROR;
}
(*ll)->buf = b;
(*ll)->next = NULL;
ngx_memcpy(b, cl->buf, sizeof(ngx_buf_t));
b->tag = (ngx_buf_tag_t) &ngx_http_echo_exec_echo_request_body;
b->last_buf = 0;
b->last_in_chain = 0;
ll = &(*ll)->next;
}
if (out == NULL) {
return NGX_OK;
}
return ngx_http_echo_send_chain_link(r, ctx, out);
}
ngx_int_t
ngx_http_echo_exec_echo_duplicate(ngx_http_request_t *r,
ngx_http_echo_ctx_t *ctx, ngx_array_t *computed_args)
{
ngx_str_t *computed_arg;
ngx_str_t *computed_arg_elts;
ssize_t i, count;
ngx_str_t *str;
u_char *p;
ngx_int_t rc;
ngx_buf_t *buf;
ngx_chain_t *cl;
dd_enter();
computed_arg_elts = computed_args->elts;
computed_arg = &computed_arg_elts[0];
count = ngx_http_echo_atosz(computed_arg->data, computed_arg->len);
if (count == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"invalid size specified: \"%V\"", computed_arg);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
str = &computed_arg_elts[1];
if (count == 0 || str->len == 0) {
rc = ngx_http_echo_send_header_if_needed(r, ctx);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
return NGX_OK;
}
buf = ngx_create_temp_buf(r->pool, count * str->len);
if (buf == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
p = buf->pos;
for (i = 0; i < count; i++) {
p = ngx_copy(p, str->data, str->len);
}
buf->last = p;
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
cl->next = NULL;
cl->buf = buf;
return ngx_http_echo_send_chain_link(r, ctx, cl);
}