Branch data Line data Source code
1 : : #define DDEBUG 0
2 : : #include "ddebug.h"
3 : :
4 : : #include "ngx_http_echo_request_info.h"
5 : : #include "ngx_http_echo_util.h"
6 : : #include "ngx_http_echo_handler.h"
7 : :
8 : : #include <nginx.h>
9 : :
10 : :
11 : : static void ngx_http_echo_post_read_request_body(ngx_http_request_t *r);
12 : :
13 : : ngx_int_t
14 : 4 : ngx_http_echo_exec_echo_read_request_body(
15 : : ngx_http_request_t* r, ngx_http_echo_ctx_t *ctx)
16 : : {
17 : 4 : return ngx_http_read_client_request_body(r,
18 : : ngx_http_echo_post_read_request_body);
19 : : }
20 : :
21 : :
22 : : static void
23 : 4 : ngx_http_echo_post_read_request_body(ngx_http_request_t *r)
24 : : {
25 : : ngx_http_echo_ctx_t *ctx;
26 : :
27 : 4 : ctx = ngx_http_get_module_ctx(r, ngx_http_echo_module);
28 : :
29 : : dd("wait read request body %d", (int) ctx->wait_read_request_body);
30 : :
31 [ - + ]: 4 : if (ctx->wait_read_request_body) {
32 : 0 : ctx->waiting = 0;
33 : 0 : ctx->done = 1;
34 : :
35 : 0 : r->write_event_handler = ngx_http_echo_wev_handler;
36 : :
37 : 0 : ngx_http_echo_wev_handler(r);
38 : : }
39 : 4 : }
40 : :
41 : :
42 : : /* this function's implementation is borrowed from nginx 0.8.20
43 : : * and modified a bit to work with subrequests.
44 : : * Copyrighted (C) by Igor Sysoev */
45 : : ngx_int_t
46 : 0 : ngx_http_echo_request_method_variable(ngx_http_request_t *r,
47 : : ngx_http_variable_value_t *v, uintptr_t data)
48 : : {
49 [ # # ]: 0 : if (r->method_name.data) {
50 : 0 : v->len = r->method_name.len;
51 : 0 : v->valid = 1;
52 : 0 : v->no_cacheable = 0;
53 : 0 : v->not_found = 0;
54 : 0 : v->data = r->method_name.data;
55 : : } else {
56 : 0 : v->not_found = 1;
57 : : }
58 : :
59 : 0 : return NGX_OK;
60 : : }
61 : :
62 : :
63 : : /* this function's implementation is borrowed from nginx 0.8.20
64 : : * and modified a bit to work with subrequests.
65 : : * Copyrighted (C) by Igor Sysoev */
66 : : ngx_int_t
67 : 0 : ngx_http_echo_client_request_method_variable(ngx_http_request_t *r,
68 : : ngx_http_variable_value_t *v, uintptr_t data)
69 : : {
70 [ # # ]: 0 : if (r->main->method_name.data) {
71 : 0 : v->len = r->main->method_name.len;
72 : 0 : v->valid = 1;
73 : 0 : v->no_cacheable = 0;
74 : 0 : v->not_found = 0;
75 : 0 : v->data = r->main->method_name.data;
76 : : } else {
77 : 0 : v->not_found = 1;
78 : : }
79 : :
80 : 0 : return NGX_OK;
81 : : }
82 : :
83 : :
84 : : /* this function's implementation is borrowed from nginx 0.8.20
85 : : * and modified a bit to work with subrequests.
86 : : * Copyrighted (C) by Igor Sysoev */
87 : : ngx_int_t
88 : 0 : ngx_http_echo_request_body_variable(ngx_http_request_t *r,
89 : : ngx_http_variable_value_t *v, uintptr_t data)
90 : : {
91 : : u_char *p;
92 : : size_t len;
93 : : ngx_buf_t *buf, *next;
94 : : ngx_chain_t *cl;
95 : :
96 [ # # ][ # # ]: 0 : if (r->request_body == NULL
[ # # ]
97 : 0 : || r->request_body->bufs == NULL
98 : 0 : || r->request_body->temp_file)
99 : : {
100 : 0 : v->not_found = 1;
101 : :
102 : 0 : return NGX_OK;
103 : : }
104 : :
105 : 0 : cl = r->request_body->bufs;
106 : 0 : buf = cl->buf;
107 : :
108 [ # # ]: 0 : if (cl->next == NULL) {
109 : 0 : v->len = buf->last - buf->pos;
110 : 0 : v->valid = 1;
111 : 0 : v->no_cacheable = 0;
112 : 0 : v->not_found = 0;
113 : 0 : v->data = buf->pos;
114 : :
115 : 0 : return NGX_OK;
116 : : }
117 : :
118 : 0 : next = cl->next->buf;
119 : 0 : len = (buf->last - buf->pos) + (next->last - next->pos);
120 : :
121 : 0 : p = ngx_pnalloc(r->pool, len);
122 [ # # ]: 0 : if (p == NULL) {
123 : 0 : return NGX_ERROR;
124 : : }
125 : :
126 : 0 : v->data = p;
127 : :
128 : 0 : p = ngx_cpymem(p, buf->pos, buf->last - buf->pos);
129 : 0 : ngx_memcpy(p, next->pos, next->last - next->pos);
130 : :
131 : 0 : v->len = len;
132 : 0 : v->valid = 1;
133 : 0 : v->no_cacheable = 0;
134 : 0 : v->not_found = 0;
135 : :
136 : 0 : return NGX_OK;
137 : : }
138 : :
139 : :
140 : : ngx_int_t
141 : 6 : ngx_http_echo_client_request_headers_variable(ngx_http_request_t *r,
142 : : ngx_http_variable_value_t *v, uintptr_t data)
143 : : {
144 : : size_t size;
145 : : u_char *p, *last;
146 : : ngx_buf_t *header_in;
147 : : ngx_flag_t just_seen_crlf;
148 : :
149 [ - + ]: 6 : if (r != r->main) {
150 : 0 : header_in = r->main->header_in;
151 : : } else {
152 : 6 : header_in = r->header_in;
153 : : }
154 : :
155 [ - + ]: 6 : if (header_in == NULL) {
156 : 0 : v->not_found = 1;
157 : 0 : return NGX_OK;
158 : : }
159 : :
160 : 6 : size = header_in->pos - header_in->start;
161 : :
162 : 6 : v->data = ngx_palloc(r->pool, size);
163 : 6 : last = ngx_cpymem(v->data, header_in->start, size);
164 : :
165 : : /* fix \0 introduced by the nginx header parser and
166 : : * locate the end of the header */
167 : 6 : just_seen_crlf = 0;
168 [ + - ]: 766 : for (p = (u_char*)v->data; p != last; p++) {
169 [ + + ]: 766 : if (*p == '\0') {
170 [ + - ][ + + ]: 88 : if (p + 1 != last && *(p + 1) == LF) {
171 : 44 : just_seen_crlf = 1;
172 : 44 : *p = CR;
173 : : } else {
174 : 44 : *p = ':';
175 : 88 : just_seen_crlf = 0;
176 : : }
177 [ + + ]: 678 : } else if (*p == CR) {
178 [ + + ]: 12 : if (just_seen_crlf) {
179 : 6 : *p = '\0';
180 : 6 : last = p;
181 : 12 : break;
182 : : }
183 [ + + ]: 666 : } else if (*p != LF) {
184 : 616 : just_seen_crlf = 0;
185 : : }
186 : : }
187 : :
188 : 6 : v->len = last - v->data;
189 : 6 : v->valid = 1;
190 : 6 : v->no_cacheable = 0;
191 : 6 : v->not_found = 0;
192 : :
193 : 6 : return NGX_OK;
194 : : }
195 : :
196 : :
197 : : ngx_int_t
198 : 0 : ngx_http_echo_cacheable_request_uri_variable(ngx_http_request_t *r,
199 : : ngx_http_variable_value_t *v, uintptr_t data)
200 : : {
201 [ # # ]: 0 : if (r->uri.len) {
202 : 0 : v->len = r->uri.len;
203 : 0 : v->valid = 1;
204 : 0 : v->no_cacheable = 0;
205 : 0 : v->not_found = 0;
206 : 0 : v->data = r->uri.data;
207 : : } else {
208 : 0 : v->not_found = 1;
209 : : }
210 : :
211 : 0 : return NGX_OK;
212 : : }
213 : :
214 : :
215 : : ngx_int_t
216 : 0 : ngx_http_echo_request_uri_variable(ngx_http_request_t *r,
217 : : ngx_http_variable_value_t *v, uintptr_t data)
218 : : {
219 [ # # ]: 0 : if (r->uri.len) {
220 : 0 : v->len = r->uri.len;
221 : 0 : v->valid = 1;
222 : 0 : v->no_cacheable = 1;
223 : 0 : v->not_found = 0;
224 : 0 : v->data = r->uri.data;
225 : : } else {
226 : 0 : v->not_found = 1;
227 : : }
228 : :
229 : 0 : return NGX_OK;
230 : : }
231 : :
232 : :
233 : : ngx_int_t
234 : 0 : ngx_http_echo_response_status_variable(ngx_http_request_t *r,
235 : : ngx_http_variable_value_t *v, uintptr_t data)
236 : : {
237 : : u_char *p;
238 : :
239 [ # # ]: 0 : if (r->headers_out.status) {
240 : : dd("headers out status: %d", (int) r->headers_out.status);
241 : :
242 : 0 : p = ngx_palloc(r->pool, NGX_INT_T_LEN);
243 [ # # ]: 0 : if (p == NULL) {
244 : 0 : return NGX_ERROR;
245 : : }
246 : :
247 : 0 : v->len = ngx_sprintf(p, "%ui", r->headers_out.status) - p;
248 : 0 : v->data = p;
249 : :
250 : 0 : v->valid = 1;
251 : 0 : v->no_cacheable = 1;
252 : 0 : v->not_found = 0;
253 : : } else {
254 : 0 : v->not_found = 1;
255 : : }
256 : :
257 : 0 : return NGX_OK;
258 : : }
259 : :
|