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_sleep.h"
14 : #include "ngx_http_echo_handler.h"
15 :
16 : #include <nginx.h>
17 : #include <ngx_log.h>
18 :
19 :
20 : /* event handler for echo_sleep */
21 :
22 : static void ngx_http_echo_post_sleep(ngx_http_request_t *r);
23 : static void ngx_http_echo_sleep_cleanup(void *data);
24 :
25 :
26 : ngx_int_t
27 0 : ngx_http_echo_exec_echo_sleep(ngx_http_request_t *r,
28 : ngx_http_echo_ctx_t *ctx, ngx_array_t *computed_args)
29 : {
30 : ngx_str_t *computed_arg;
31 : ngx_str_t *computed_arg_elts;
32 : ngx_int_t delay; /* in msec */
33 : ngx_http_cleanup_t *cln;
34 :
35 0 : computed_arg_elts = computed_args->elts;
36 0 : computed_arg = &computed_arg_elts[0];
37 :
38 0 : delay = ngx_atofp(computed_arg->data, computed_arg->len, 3);
39 :
40 0 : if (delay == NGX_ERROR) {
41 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
42 : "invalid sleep duration \"%V\"", &computed_arg_elts[0]);
43 :
44 0 : return NGX_HTTP_BAD_REQUEST;
45 : }
46 :
47 : dd("adding timer with delay %lu ms, r:%.*s", (unsigned long) delay,
48 : (int) r->uri.len, r->uri.data);
49 :
50 0 : ngx_add_timer(&ctx->sleep, (ngx_msec_t) delay);
51 :
52 : /* we don't check broken downstream connections
53 : * ourselves so even if the client shuts down
54 : * the connection prematurely, nginx will still
55 : * go on waiting for our timers to get properly
56 : * expired. However, we'd still register a
57 : * cleanup handler for completeness. */
58 :
59 0 : cln = ngx_http_cleanup_add(r, 0);
60 0 : if (cln == NULL) {
61 0 : return NGX_ERROR;
62 : }
63 :
64 0 : cln->handler = ngx_http_echo_sleep_cleanup;
65 0 : cln->data = r;
66 :
67 0 : return NGX_AGAIN;
68 : }
69 :
70 :
71 : static void
72 0 : ngx_http_echo_post_sleep(ngx_http_request_t *r)
73 : {
74 : ngx_http_echo_ctx_t *ctx;
75 : /* ngx_int_t rc; */
76 :
77 : dd("post sleep, r:%.*s", (int) r->uri.len, r->uri.data);
78 :
79 0 : ctx = ngx_http_get_module_ctx(r, ngx_http_echo_module);
80 :
81 0 : if (ctx == NULL) {
82 0 : return;
83 : }
84 :
85 0 : ctx->waiting = 0;
86 0 : ctx->done = 1;
87 :
88 : dd("sleep: after get module ctx");
89 :
90 : dd("timed out? %d", ctx->sleep.timedout);
91 : dd("timer set? %d", ctx->sleep.timer_set);
92 :
93 0 : if (!ctx->sleep.timedout) {
94 : dd("HERE reached!");
95 0 : return;
96 : }
97 :
98 0 : ctx->sleep.timedout = 0;
99 :
100 0 : if (ctx->sleep.timer_set) {
101 : dd("deleting timer for echo_sleep");
102 :
103 0 : ngx_del_timer(&ctx->sleep);
104 : }
105 :
106 : /* r->write_event_handler = ngx_http_request_empty_handler; */
107 :
108 0 : ngx_http_echo_wev_handler(r);
109 : }
110 :
111 :
112 : void
113 0 : ngx_http_echo_sleep_event_handler(ngx_event_t *ev)
114 : {
115 : ngx_connection_t *c;
116 : ngx_http_request_t *r;
117 : ngx_http_log_ctx_t *ctx;
118 :
119 0 : r = ev->data;
120 0 : c = r->connection;
121 :
122 0 : if (c->destroyed) {
123 0 : return;
124 : }
125 :
126 0 : if (c->error) {
127 0 : ngx_http_finalize_request(r, NGX_ERROR);
128 0 : return;
129 : }
130 :
131 0 : ctx = c->log->data;
132 0 : ctx->current_request = r;
133 :
134 : /* XXX when r->done == 1 we should do cleaning immediately
135 : * and delete our timer and then quit. */
136 :
137 0 : ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
138 : "echo sleep event handler: \"%V?%V\"", &r->uri, &r->args);
139 :
140 : /*
141 : if (r->done) {
142 : return;
143 : }
144 : */
145 :
146 0 : ngx_http_echo_post_sleep(r);
147 :
148 : #if defined(nginx_version)
149 :
150 : dd("before run posted requests");
151 :
152 0 : ngx_http_run_posted_requests(c);
153 :
154 : dd("after run posted requests");
155 :
156 : #endif
157 : }
158 :
159 :
160 : ngx_int_t
161 0 : ngx_http_echo_exec_echo_blocking_sleep(ngx_http_request_t *r,
162 : ngx_http_echo_ctx_t *ctx, ngx_array_t *computed_args)
163 : {
164 : ngx_str_t *computed_arg;
165 : ngx_str_t *computed_arg_elts;
166 : ngx_int_t delay; /* in msec */
167 :
168 0 : computed_arg_elts = computed_args->elts;
169 0 : computed_arg = &computed_arg_elts[0];
170 :
171 0 : delay = ngx_atofp(computed_arg->data, computed_arg->len, 3);
172 :
173 0 : if (delay == NGX_ERROR) {
174 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
175 : "invalid sleep duration \"%V\"", &computed_arg_elts[0]);
176 0 : return NGX_HTTP_BAD_REQUEST;
177 : }
178 :
179 : dd("blocking delay: %lu ms", (unsigned long) delay);
180 :
181 0 : ngx_msleep((ngx_msec_t) delay);
182 :
183 0 : return NGX_OK;
184 : }
185 :
186 :
187 : static void
188 0 : ngx_http_echo_sleep_cleanup(void *data)
189 : {
190 0 : ngx_http_request_t *r = data;
191 : ngx_http_echo_ctx_t *ctx;
192 :
193 : dd("echo sleep cleanup");
194 :
195 0 : ctx = ngx_http_get_module_ctx(r, ngx_http_echo_module);
196 0 : if (ctx == NULL) {
197 0 : return;
198 : }
199 :
200 0 : if (ctx->sleep.timer_set) {
201 : dd("cleanup: deleting timer for echo_sleep");
202 :
203 0 : ngx_del_timer(&ctx->sleep);
204 0 : return;
205 : }
206 :
207 : dd("cleanup: timer not set");
208 : }
|