0 | |
|
1 | |
/*
|
2 | |
* Copyright (C) Igor Sysoev
|
3 | |
*/
|
4 | |
|
5 | |
|
6 | |
#include <ngx_config.h>
|
7 | |
#include <ngx_core.h>
|
8 | |
#include <ngx_http.h>
|
9 | |
|
10 | |
|
11 | |
typedef struct {
|
12 | |
ngx_http_request_t *request;
|
13 | |
ngx_pool_t *pool;
|
14 | |
ngx_chain_t *head;
|
15 | |
ngx_buf_t *last;
|
16 | |
size_t size;
|
17 | |
} ngx_http_status_ctx_t;
|
18 | |
|
19 | |
|
20 | |
static ngx_int_t ngx_http_status(ngx_http_status_ctx_t *ctx);
|
21 | |
static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd,
|
22 | |
void *conf);
|
23 | |
|
24 | |
static ngx_command_t ngx_http_status_commands[] = {
|
25 | |
|
26 | |
{ ngx_string("status"),
|
27 | |
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
28 | |
ngx_http_set_status,
|
29 | |
0,
|
30 | |
0,
|
31 | |
NULL },
|
32 | |
|
33 | |
ngx_null_command
|
34 | |
};
|
35 | |
|
36 | |
|
37 | |
|
38 | |
static ngx_http_module_t ngx_http_status_module_ctx = {
|
39 | |
NULL, /* pre conf */
|
40 | |
|
41 | |
NULL, /* create main configuration */
|
42 | |
NULL, /* init main configuration */
|
43 | |
|
44 | |
NULL, /* create server configuration */
|
45 | |
NULL, /* merge server configuration */
|
46 | |
|
47 | |
NULL, /* create location configuration */
|
48 | |
NULL /* merge location configuration */
|
49 | |
};
|
50 | |
|
51 | |
|
52 | |
ngx_module_t ngx_http_status_module = {
|
53 | |
NGX_MODULE,
|
54 | |
&ngx_http_status_module_ctx, /* module context */
|
55 | |
ngx_http_status_commands, /* module directives */
|
56 | |
NGX_HTTP_MODULE, /* module type */
|
57 | |
NULL, /* init module */
|
58 | |
NULL /* init process */
|
59 | |
};
|
60 | |
|
61 | |
|
62 | |
static ngx_int_t ngx_http_status_handler(ngx_http_request_t *r)
|
63 | |
{
|
64 | |
ngx_int_t rc;
|
65 | |
ngx_http_status_ctx_t ctx;
|
66 | |
|
67 | |
if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
|
68 | |
return NGX_HTTP_NOT_ALLOWED;
|
69 | |
}
|
70 | |
|
71 | |
rc = ngx_http_discard_body(r);
|
72 | |
|
73 | |
if (rc != NGX_OK && rc != NGX_AGAIN) {
|
74 | |
return rc;
|
75 | |
}
|
76 | |
|
77 | |
r->headers_out.content_type = ngx_list_push(&r->headers_out.headers);
|
78 | |
if (r->headers_out.content_type == NULL) {
|
79 | |
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
80 | |
}
|
81 | |
|
82 | |
r->headers_out.content_type->key.len = 0;
|
83 | |
r->headers_out.content_type->key.data = NULL;
|
84 | |
r->headers_out.content_type->value.len = sizeof("text/plain") - 1;
|
85 | |
r->headers_out.content_type->value.data = (u_char *) "text/plain";
|
86 | |
|
87 | |
if (r->method == NGX_HTTP_HEAD) {
|
88 | |
r->headers_out.status = NGX_HTTP_OK;
|
89 | |
|
90 | |
rc = ngx_http_send_header(r);
|
91 | |
|
92 | |
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
93 | |
return rc;
|
94 | |
}
|
95 | |
}
|
96 | |
|
97 | |
ctx.request = r;
|
98 | |
ctx.pool = r->pool;
|
99 | |
ctx.head = NULL;
|
100 | |
ctx.size = 0;
|
101 | |
|
102 | |
if (ngx_http_status(&ctx) != NGX_OK) {
|
103 | |
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
104 | |
}
|
105 | |
|
106 | |
r->headers_out.status = NGX_HTTP_OK;
|
107 | |
r->headers_out.content_length_n = ctx.size;
|
108 | |
|
109 | |
rc = ngx_http_send_header(r);
|
110 | |
|
111 | |
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
112 | |
return rc;
|
113 | |
}
|
114 | |
|
115 | |
if (!r->main) {
|
116 | |
ctx.last->last_buf = 1;
|
117 | |
}
|
118 | |
|
119 | |
return ngx_http_output_filter(r, ctx.head);
|
120 | |
}
|
121 | |
|
122 | |
|
123 | |
static ngx_int_t ngx_http_status(ngx_http_status_ctx_t *ctx)
|
124 | |
{
|
125 | |
u_char ch;
|
126 | |
size_t len, n;
|
127 | |
ngx_uint_t i, dash;
|
128 | |
ngx_buf_t *b;
|
129 | |
ngx_chain_t *cl, **ll;
|
130 | |
ngx_connection_t *c;
|
131 | |
ngx_http_request_t *r;
|
132 | |
ngx_http_core_main_conf_t *cmcf;
|
133 | |
|
134 | |
cmcf = ngx_http_get_module_main_conf(ctx->request, ngx_http_core_module);
|
135 | |
|
136 | |
#if (NGX_SUPPRESS_WARN)
|
137 | |
b = NULL;
|
138 | |
ll = NULL;
|
139 | |
#endif
|
140 | |
|
141 | |
dash = 0;
|
142 | |
|
143 | |
/* TODO: old connections */
|
144 | |
|
145 | |
c = ngx_cycle->connections;
|
146 | |
for (i = 0; i < ngx_cycle->connection_n; i++) {
|
147 | |
|
148 | |
/* TODO: trylock connection mutex */
|
149 | |
|
150 | |
r = c[i].data;
|
151 | |
if (r && r->signature == NGX_HTTP_MODULE) {
|
152 | |
|
153 | |
/* STUB: should be NGX_PID_T_LEN */
|
154 | |
len = NGX_INT64_LEN /* pid */
|
155 | |
+ 1 + NGX_INT32_LEN /* connection */
|
156 | |
+ 1 + 1 /* state */
|
157 | |
+ 1 + NGX_INET_ADDRSTRLEN
|
158 | |
+ 1 + (r->server_name ? cmcf->max_server_name_len : 1)
|
159 | |
+ 2; /* "\r\n" */
|
160 | |
|
161 | |
/* BUG: cmcf->max_server_name_len and "*.domain.tld" */
|
162 | |
|
163 | |
|
164 | |
if (r->request_line.len) {
|
165 | |
len += 1 + 1 + r->request_line.len + 1;
|
166 | |
}
|
167 | |
|
168 | |
if (!(b = ngx_create_temp_buf(ctx->pool, len))) {
|
169 | |
/* TODO: unlock mutex */
|
170 | |
return NGX_ERROR;
|
171 | |
}
|
172 | |
|
173 | |
b->last = ngx_sprintf(b->last, "%P %5ui", ngx_pid, i);
|
174 | |
|
175 | |
switch (r->http_state) {
|
176 | |
case NGX_HTTP_INITING_REQUEST_STATE:
|
177 | |
ch = 'I';
|
178 | |
break;
|
179 | |
|
180 | |
case NGX_HTTP_READING_REQUEST_STATE:
|
181 | |
ch = 'R';
|
182 | |
break;
|
183 | |
|
184 | |
case NGX_HTTP_PROCESS_REQUEST_STATE:
|
185 | |
ch = 'P';
|
186 | |
break;
|
187 | |
|
188 | |
case NGX_HTTP_WRITING_REQUEST_STATE:
|
189 | |
ch = 'W';
|
190 | |
break;
|
191 | |
|
192 | |
case NGX_HTTP_KEEPALIVE_STATE:
|
193 | |
ch = 'K';
|
194 | |
break;
|
195 | |
|
196 | |
default:
|
197 | |
ch = '?';
|
198 | |
}
|
199 | |
|
200 | |
*(b->last++) = ' ';
|
201 | |
*(b->last++) = ch;
|
202 | |
|
203 | |
*(b->last++) = ' ';
|
204 | |
b->last = ngx_cpymem(b->last, c[i].addr_text.data,
|
205 | |
c[i].addr_text.len);
|
206 | |
for (n = c[i].addr_text.len; n < NGX_INET_ADDRSTRLEN; n++) {
|
207 | |
*(b->last++) = ' ';
|
208 | |
}
|
209 | |
|
210 | |
*(b->last++) = ' ';
|
211 | |
if (r->server_name) {
|
212 | |
b->last = ngx_cpymem(b->last, r->server_name->data,
|
213 | |
r->server_name->len);
|
214 | |
for (n = r->server_name->len;
|
215 | |
n < cmcf->max_server_name_len;
|
216 | |
n++)
|
217 | |
{
|
218 | |
*(b->last++) = ' ';
|
219 | |
}
|
220 | |
|
221 | |
} else {
|
222 | |
*(b->last++) = '?';
|
223 | |
}
|
224 | |
|
225 | |
if (r->request_line.len) {
|
226 | |
*(b->last++) = ' ';
|
227 | |
*(b->last++) = '"';
|
228 | |
b->last = ngx_cpymem(b->last, r->request_line.data,
|
229 | |
r->request_line.len);
|
230 | |
*(b->last++) = '"';
|
231 | |
|
232 | |
}
|
233 | |
|
234 | |
*(b->last++) = CR; *(b->last++) = LF;
|
235 | |
|
236 | |
dash = 0;
|
237 | |
|
238 | |
} else if (c[i].fd != -1) {
|
239 | |
len = NGX_INT64_LEN /* pid */
|
240 | |
+ 1 + NGX_INT32_LEN /* connection */
|
241 | |
+ 1 + 1 /* state */
|
242 | |
+ 2; /* "\r\n" */
|
243 | |
|
244 | |
if (!(b = ngx_create_temp_buf(ctx->pool, len))) {
|
245 | |
/* TODO: unlock mutex */
|
246 | |
return NGX_ERROR;
|
247 | |
}
|
248 | |
|
249 | |
b->last = ngx_sprintf(b->last, "%P %5ui", ngx_pid, i);
|
250 | |
|
251 | |
*(b->last++) = ' ';
|
252 | |
*(b->last++) = 's';
|
253 | |
|
254 | |
*(b->last++) = CR; *(b->last++) = LF;
|
255 | |
|
256 | |
dash = 0;
|
257 | |
|
258 | |
} else if (!dash) {
|
259 | |
len = 3;
|
260 | |
|
261 | |
if (!(b = ngx_create_temp_buf(ctx->pool, len))) {
|
262 | |
/* TODO: unlock mutex */
|
263 | |
return NGX_ERROR;
|
264 | |
}
|
265 | |
|
266 | |
*(b->last++) = '-'; *(b->last++) = CR; *(b->last++) = LF;
|
267 | |
|
268 | |
dash = 1;
|
269 | |
|
270 | |
} else {
|
271 | |
continue;
|
272 | |
}
|
273 | |
|
274 | |
/* TODO: unlock mutex */
|
275 | |
|
276 | |
if (!(cl = ngx_alloc_chain_link(ctx->pool))) {
|
277 | |
return NGX_ERROR;
|
278 | |
}
|
279 | |
|
280 | |
if (ctx->head) {
|
281 | |
*ll = cl;
|
282 | |
|
283 | |
} else {
|
284 | |
ctx->head = cl;
|
285 | |
}
|
286 | |
|
287 | |
cl->buf = b;
|
288 | |
cl->next = NULL;
|
289 | |
ll = &cl->next;
|
290 | |
|
291 | |
ctx->size += b->last - b->pos;
|
292 | |
}
|
293 | |
|
294 | |
ctx->last = b;
|
295 | |
|
296 | |
return NGX_OK;
|
297 | |
}
|
298 | |
|
299 | |
|
300 | |
static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
301 | |
{
|
302 | |
ngx_http_core_loc_conf_t *clcf;
|
303 | |
|
304 | |
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
305 | |
clcf->handler = ngx_http_status_handler;
|
306 | |
|
307 | |
return NGX_CONF_OK;
|
308 | |
}
|