-
Notifications
You must be signed in to change notification settings - Fork 255
/
ngx_http_echo_util.c
186 lines (145 loc) · 4.44 KB
/
ngx_http_echo_util.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
#define DDEBUG 0
#include "ddebug.h"
#include "ngx_http_echo_util.h"
#include "ngx_http_echo_sleep.h"
ngx_int_t
ngx_http_echo_init_ctx(ngx_http_request_t *r, ngx_http_echo_ctx_t **ctx_ptr) {
ngx_http_echo_ctx_t *ctx;
*ctx_ptr = ngx_pcalloc(r->pool, sizeof(ngx_http_echo_ctx_t));
if (*ctx_ptr == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ctx = *ctx_ptr;
ctx->sleep.handler = ngx_http_echo_sleep_event_handler;
ctx->sleep.data = r;
ctx->sleep.log = r->connection->log;
return NGX_OK;
}
ngx_int_t
ngx_http_echo_eval_cmd_args(ngx_http_request_t *r,
ngx_http_echo_cmd_t *cmd, ngx_array_t *computed_args) {
ngx_uint_t i;
ngx_array_t *args = cmd->args;
ngx_str_t *computed_arg;
ngx_http_echo_arg_template_t *arg, *arg_elts;
arg_elts = args->elts;
for (i = 0; i < args->nelts; i++) {
computed_arg = ngx_array_push(computed_args);
if (computed_arg == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
arg = &arg_elts[i];
if (arg->lengths == NULL) { /* does not contain vars */
dd("Using raw value \"%s\"", arg->raw_value.data);
*computed_arg = arg->raw_value;
} else {
if (ngx_http_script_run(r, computed_arg, arg->lengths->elts,
0, arg->values->elts) == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
}
}
return NGX_OK;
}
ngx_int_t
ngx_http_echo_send_chain_link(ngx_http_request_t* r,
ngx_http_echo_ctx_t *ctx, ngx_chain_t *cl) {
ngx_int_t rc;
size_t size;
ngx_chain_t *p;
rc = ngx_http_echo_send_header_if_needed(r, ctx);
if (r->header_only || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
if (r->http_version < NGX_HTTP_VERSION_11 && !ctx->headers_sent) {
ctx->headers_sent = 1;
size = 0;
for (p = cl; p; p = p->next) {
if (p->buf->memory) {
size += p->buf->last - p->buf->pos;
}
}
dd("content length for HTTP 1.0: %d", size);
r->headers_out.content_length_n = (off_t) size;
if (r->headers_out.content_length) {
r->headers_out.content_length->hash = 0;
}
r->headers_out.content_length = NULL;
rc = ngx_http_send_header(r);
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
}
if (cl == NULL) {
#if defined(nginx_version) && nginx_version <= 8004
/* earlier versions of nginx does not allow subrequests
to send last_buf themselves */
if (r != r->main) {
return NGX_OK;
}
#endif
rc = ngx_http_send_special(r, NGX_HTTP_LAST);
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
return NGX_OK;
}
return ngx_http_output_filter(r, cl);
}
ngx_int_t
ngx_http_echo_send_header_if_needed(ngx_http_request_t* r,
ngx_http_echo_ctx_t *ctx) {
/* ngx_int_t rc; */
if ( ! ctx->headers_sent ) {
r->headers_out.status = NGX_HTTP_OK;
if (ngx_http_set_content_type(r) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_http_clear_content_length(r);
ngx_http_clear_accept_ranges(r);
if (r->http_version >= NGX_HTTP_VERSION_11) {
ctx->headers_sent = 1;
return ngx_http_send_header(r);
}
}
return NGX_OK;
}
ssize_t
ngx_http_echo_atosz(u_char *line, size_t n) {
ssize_t value;
if (n == 0) {
return NGX_ERROR;
}
for (value = 0; n--; line++) {
if (*line == '_') { /* we ignore undercores */
continue;
}
if (*line < '0' || *line > '9') {
return NGX_ERROR;
}
value = value * 10 + (*line - '0');
}
if (value < 0) {
return NGX_ERROR;
} else {
return value;
}
}
/* Modified from the ngx_strlcasestrn function in ngx_string.h
* Copyright (C) by Igor Sysoev */
u_char *
ngx_http_echo_strlstrn(u_char *s1, u_char *last, u_char *s2, size_t n)
{
ngx_uint_t c1, c2;
c2 = (ngx_uint_t) *s2++;
last -= n;
do {
do {
if (s1 >= last) {
return NULL;
}
c1 = (ngx_uint_t) *s1++;
} while (c1 != c2);
} while (ngx_strncmp(s1, s2, n) != 0);
return --s1;
}