Mercurial > hg > nginx
comparison src/http/modules/ngx_http_status_handler.c @ 367:ceec87d1c2b3
nginx-0.0.7-2004-06-24-11:53:37 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 24 Jun 2004 07:53:37 +0000 |
parents | src/http/modules/ngx_http_status.c@f1d0e5f09c1e |
children | 15c84a40e87d |
comparison
equal
deleted
inserted
replaced
366:e411b1482ee3 | 367:ceec87d1c2b3 |
---|---|
1 | |
2 #include <ngx_config.h> | |
3 #include <ngx_core.h> | |
4 #include <ngx_http.h> | |
5 | |
6 | |
7 static char *ngx_http_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | |
8 | |
9 static ngx_command_t ngx_http_status_commands[] = { | |
10 | |
11 { ngx_string("status"), | |
12 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
13 ngx_http_status, | |
14 0, | |
15 0, | |
16 NULL }, | |
17 | |
18 ngx_null_command | |
19 }; | |
20 | |
21 | |
22 | |
23 ngx_http_module_t ngx_http_status_module_ctx = { | |
24 NULL, /* pre conf */ | |
25 | |
26 NULL, /* create main configuration */ | |
27 NULL, /* init main configuration */ | |
28 | |
29 NULL, /* create server configuration */ | |
30 NULL, /* merge server configuration */ | |
31 | |
32 NULL, /* create location configuration */ | |
33 NULL /* merge location configuration */ | |
34 }; | |
35 | |
36 | |
37 ngx_module_t ngx_http_status_module = { | |
38 NGX_MODULE, | |
39 &ngx_http_status_module_ctx, /* module context */ | |
40 ngx_http_status_commands, /* module directives */ | |
41 NGX_HTTP_MODULE, /* module type */ | |
42 NULL, /* init module */ | |
43 NULL /* init process */ | |
44 }; | |
45 | |
46 | |
47 static ngx_int_t ngx_http_status_handler(ngx_http_request_t *r) | |
48 { | |
49 size_t len; | |
50 ngx_int_t rc; | |
51 ngx_uint_t i, dash; | |
52 ngx_buf_t *b; | |
53 ngx_chain_t out; | |
54 ngx_connection_t *c; | |
55 ngx_http_request_t *rq; | |
56 | |
57 if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) { | |
58 return NGX_HTTP_NOT_ALLOWED; | |
59 } | |
60 | |
61 rc = ngx_http_discard_body(r); | |
62 | |
63 if (rc != NGX_OK && rc != NGX_AGAIN) { | |
64 return rc; | |
65 } | |
66 | |
67 if (!(r->headers_out.content_type = | |
68 ngx_http_add_header(&r->headers_out, ngx_http_headers_out))) | |
69 { | |
70 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
71 } | |
72 | |
73 r->headers_out.content_type->key.len = 0; | |
74 r->headers_out.content_type->key.data = NULL; | |
75 r->headers_out.content_type->value.len = sizeof("text/plain") - 1; | |
76 r->headers_out.content_type->value.data = (u_char *) "text/plain"; | |
77 | |
78 if (r->method == NGX_HTTP_HEAD) { | |
79 r->headers_out.status = NGX_HTTP_OK; | |
80 | |
81 rc = ngx_http_send_header(r); | |
82 | |
83 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
84 return rc; | |
85 } | |
86 } | |
87 | |
88 len = 0; | |
89 dash = 0; | |
90 | |
91 c = ngx_cycle->connections; | |
92 for (i = 0; i < ngx_cycle->connection_n; i++) { | |
93 rq = c[i].data; | |
94 if (rq && rq->signature == NGX_HTTP_MODULE) { | |
95 | |
96 if (rq->request_line.len) { | |
97 len += NGX_INT32_LEN + 1 + rq->request_line.len + 2 + 2; | |
98 dash = 0; | |
99 } | |
100 | |
101 continue; | |
102 } | |
103 | |
104 if (!dash) { | |
105 len += 3; | |
106 dash = 1; | |
107 } | |
108 } | |
109 | |
110 if (!(b = ngx_create_temp_buf(r->pool, len))) { | |
111 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
112 } | |
113 | |
114 dash = 0; | |
115 | |
116 for (i = 0; i < ngx_cycle->connection_n; i++) { | |
117 rq = c[i].data; | |
118 if (rq && rq->signature == NGX_HTTP_MODULE) { | |
119 | |
120 #if 0 | |
121 switch (rq->http_state) { | |
122 case NGX_HTTP_INITING_REQUEST_STATE: | |
123 | |
124 case NGX_HTTP_KEEPALIVE_STATE: | |
125 } | |
126 #endif | |
127 | |
128 if (rq->request_line.len) { | |
129 b->last += ngx_snprintf((char *) b->last, NGX_INT32_LEN, | |
130 "%u", i); | |
131 *(b->last++) = ' '; | |
132 | |
133 *(b->last++) = '"'; | |
134 b->last = ngx_cpymem(b->last, r->request_line.data, | |
135 r->request_line.len); | |
136 *(b->last++) = '"'; | |
137 | |
138 *(b->last++) = CR; *(b->last++) = LF; | |
139 dash = 0; | |
140 } | |
141 | |
142 continue; | |
143 } | |
144 | |
145 if (!dash) { | |
146 *(b->last++) = '-'; *(b->last++) = CR; *(b->last++) = LF; | |
147 dash = 1; | |
148 } | |
149 } | |
150 | |
151 r->headers_out.status = NGX_HTTP_OK; | |
152 r->headers_out.content_length_n = b->last - b->pos; | |
153 | |
154 rc = ngx_http_send_header(r); | |
155 | |
156 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
157 return rc; | |
158 } | |
159 | |
160 if (!r->main) { | |
161 b->last_buf = 1; | |
162 } | |
163 | |
164 out.buf = b; | |
165 out.next = NULL; | |
166 | |
167 return ngx_http_output_filter(r, &out); | |
168 } | |
169 | |
170 | |
171 static char *ngx_http_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
172 { | |
173 ngx_http_conf_ctx_t *ctx; | |
174 ngx_http_core_loc_conf_t *clcf; | |
175 | |
176 ctx = cf->ctx; | |
177 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index]; | |
178 clcf->handler = ngx_http_status_handler; | |
179 | |
180 return NGX_CONF_OK; | |
181 } |