Mercurial > hg > nginx-vendor-0-7
comparison src/http/modules/ngx_http_autoindex_module.c @ 50:72eb30262aac NGINX_0_1_25
nginx 0.1.25
*) Bugfix: nginx did run on Linux parisc.
*) Feature: nginx now does not start under FreeBSD if the sysctl
kern.ipc.somaxconn value is too big.
*) Bugfix: if a request was internally redirected by the
ngx_http_index_module module to the ngx_http_proxy_module or
ngx_http_fastcgi_module modules, then the index file was not closed
after request completion.
*) Feature: the "proxy_pass" can be used in location with regular
expression.
*) Feature: the ngx_http_rewrite_filter_module module supports the
condition like "if ($HTTP_USER_AGENT ~ MSIE)".
*) Bugfix: nginx started too slow if the large number of addresses and
text values were used in the "geo" directive.
*) Change: a variable name must be declared as "$name" in the "geo"
directive. The previous variant without "$" is still supported, but
will be removed soon.
*) Feature: the "%{VARIABLE}v" logging parameter.
*) Feature: the "set $name value" directive.
*) Bugfix: gcc 4.0 compatibility.
*) Feature: the --with-openssl-opt=OPTIONS autoconfiguration directive.
author | Igor Sysoev <http://sysoev.ru> |
---|---|
date | Sat, 19 Mar 2005 00:00:00 +0300 |
parents | |
children | 0d75d65c642f |
comparison
equal
deleted
inserted
replaced
49:93dabbc9efb9 | 50:72eb30262aac |
---|---|
1 | |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 | |
12 #if 0 | |
13 | |
14 typedef struct { | |
15 ngx_buf_t *buf; | |
16 size_t size; | |
17 ngx_pool_t *pool; | |
18 size_t alloc_size; | |
19 ngx_chain_t **last_out; | |
20 } ngx_http_autoindex_ctx_t; | |
21 | |
22 #endif | |
23 | |
24 | |
25 typedef struct { | |
26 ngx_str_t name; | |
27 ngx_uint_t escape; | |
28 ngx_uint_t dir; | |
29 time_t mtime; | |
30 off_t size; | |
31 } ngx_http_autoindex_entry_t; | |
32 | |
33 | |
34 typedef struct { | |
35 ngx_flag_t enable; | |
36 } ngx_http_autoindex_loc_conf_t; | |
37 | |
38 | |
39 #define NGX_HTTP_AUTOINDEX_NAME_LEN 50 | |
40 | |
41 | |
42 static int ngx_http_autoindex_cmp_entries(const void *one, const void *two); | |
43 static ngx_int_t ngx_http_autoindex_error(ngx_http_request_t *r, | |
44 ngx_dir_t *dir, u_char *name); | |
45 static ngx_int_t ngx_http_autoindex_init(ngx_cycle_t *cycle); | |
46 static void *ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf); | |
47 static char *ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, | |
48 void *parent, void *child); | |
49 | |
50 | |
51 static ngx_command_t ngx_http_autoindex_commands[] = { | |
52 | |
53 { ngx_string("autoindex"), | |
54 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
55 ngx_conf_set_flag_slot, | |
56 NGX_HTTP_LOC_CONF_OFFSET, | |
57 offsetof(ngx_http_autoindex_loc_conf_t, enable), | |
58 NULL }, | |
59 | |
60 ngx_null_command | |
61 }; | |
62 | |
63 | |
64 ngx_http_module_t ngx_http_autoindex_module_ctx = { | |
65 NULL, /* pre conf */ | |
66 | |
67 NULL, /* create main configuration */ | |
68 NULL, /* init main configuration */ | |
69 | |
70 NULL, /* create server configuration */ | |
71 NULL, /* merge server configuration */ | |
72 | |
73 ngx_http_autoindex_create_loc_conf, /* create location configration */ | |
74 ngx_http_autoindex_merge_loc_conf /* merge location configration */ | |
75 }; | |
76 | |
77 | |
78 ngx_module_t ngx_http_autoindex_module = { | |
79 NGX_MODULE, | |
80 &ngx_http_autoindex_module_ctx, /* module context */ | |
81 ngx_http_autoindex_commands, /* module directives */ | |
82 NGX_HTTP_MODULE, /* module type */ | |
83 ngx_http_autoindex_init, /* init module */ | |
84 NULL /* init process */ | |
85 }; | |
86 | |
87 | |
88 static u_char title[] = | |
89 "<html>" CRLF | |
90 "<head><title>Index of " | |
91 ; | |
92 | |
93 | |
94 static u_char header[] = | |
95 "</title></head>" CRLF | |
96 "<body bgcolor=\"white\">" CRLF | |
97 "<h1>Index of " | |
98 ; | |
99 | |
100 static u_char tail[] = | |
101 "</body>" CRLF | |
102 "</html>" CRLF | |
103 ; | |
104 | |
105 | |
106 static ngx_int_t | |
107 ngx_http_autoindex_handler(ngx_http_request_t *r) | |
108 { | |
109 u_char *last; | |
110 size_t len; | |
111 ngx_tm_t tm; | |
112 ngx_int_t rc; | |
113 ngx_uint_t i, level; | |
114 ngx_err_t err; | |
115 ngx_buf_t *b; | |
116 ngx_chain_t out; | |
117 ngx_str_t dname, fname; | |
118 ngx_dir_t dir; | |
119 ngx_pool_t *pool; | |
120 ngx_array_t entries; | |
121 ngx_http_core_loc_conf_t *clcf; | |
122 ngx_http_autoindex_entry_t *entry; | |
123 ngx_http_autoindex_loc_conf_t *alcf; | |
124 | |
125 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
126 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; | |
127 | |
128 if (r->uri.data[r->uri.len - 1] != '/') { | |
129 return NGX_DECLINED; | |
130 } | |
131 | |
132 /* TODO: Win32 */ | |
133 if (r->zero_in_uri) { | |
134 return NGX_DECLINED; | |
135 } | |
136 | |
137 alcf = ngx_http_get_module_loc_conf(r, ngx_http_autoindex_module); | |
138 | |
139 if (!alcf->enable) { | |
140 return NGX_DECLINED; | |
141 } | |
142 | |
143 /* TODO: pool should be temporary pool */ | |
144 pool = r->pool; | |
145 | |
146 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
147 | |
148 if (clcf->alias) { | |
149 dname.data = ngx_palloc(pool, clcf->root.len + r->uri.len | |
150 + NGX_DIR_MASK_LEN + 1 | |
151 - clcf->name.len); | |
152 if (dname.data == NULL) { | |
153 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
154 } | |
155 | |
156 last = ngx_cpymem(dname.data, clcf->root.data, clcf->root.len); | |
157 last = ngx_cpystrn(last, r->uri.data + clcf->name.len, | |
158 r->uri.len - clcf->name.len + 1); | |
159 | |
160 } else { | |
161 dname.data = ngx_palloc(pool, clcf->root.len + r->uri.len | |
162 + NGX_DIR_MASK_LEN); | |
163 if (dname.data == NULL) { | |
164 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
165 } | |
166 | |
167 last = ngx_cpymem(dname.data, clcf->root.data, clcf->root.len); | |
168 last = ngx_cpystrn(last, r->uri.data, r->uri.len); | |
169 } | |
170 | |
171 dname.len = last - dname.data; | |
172 | |
173 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
174 "http autoindex: \"%s\"", dname.data); | |
175 | |
176 | |
177 if (ngx_open_dir(&dname, &dir) == NGX_ERROR) { | |
178 err = ngx_errno; | |
179 | |
180 if (err == NGX_ENOENT || err == NGX_ENOTDIR) { | |
181 level = NGX_LOG_ERR; | |
182 rc = NGX_HTTP_NOT_FOUND; | |
183 | |
184 } else if (err == NGX_EACCES) { | |
185 level = NGX_LOG_ERR; | |
186 rc = NGX_HTTP_FORBIDDEN; | |
187 | |
188 } else { | |
189 level = NGX_LOG_CRIT; | |
190 rc = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
191 } | |
192 | |
193 ngx_log_error(level, r->connection->log, err, | |
194 ngx_open_dir_n " \"%s\" failed", dname.data); | |
195 | |
196 return rc; | |
197 } | |
198 | |
199 #if (NGX_SUPPRESS_WARN) | |
200 /* MSVC thinks 'entries' may be used without having been initialized */ | |
201 ngx_memzero(&entries, sizeof(ngx_array_t)); | |
202 #endif | |
203 | |
204 if (ngx_array_init(&entries, pool, 50, sizeof(ngx_http_autoindex_entry_t)) | |
205 == NGX_ERROR) | |
206 { | |
207 return ngx_http_autoindex_error(r, &dir, dname.data); | |
208 } | |
209 | |
210 fname.len = 0; | |
211 #if (NGX_SUPPRESS_WARN) | |
212 fname.data = NULL; | |
213 #endif | |
214 | |
215 for ( ;; ) { | |
216 ngx_set_errno(0); | |
217 | |
218 if (ngx_read_dir(&dir) == NGX_ERROR) { | |
219 err = ngx_errno; | |
220 | |
221 if (err != NGX_ENOMOREFILES) { | |
222 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, | |
223 ngx_read_dir_n " \"%s\" failed", dname.data); | |
224 return ngx_http_autoindex_error(r, &dir, dname.data); | |
225 } | |
226 | |
227 break; | |
228 } | |
229 | |
230 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
231 "http autoindex file: \"%s\"", ngx_de_name(&dir)); | |
232 | |
233 len = ngx_de_namelen(&dir); | |
234 | |
235 if (len == 1 && ngx_de_name(&dir)[0] == '.') { | |
236 continue; | |
237 } | |
238 | |
239 if (len == 2 | |
240 && ngx_de_name(&dir)[0] == '.' | |
241 && ngx_de_name(&dir)[1] == '.') | |
242 { | |
243 continue; | |
244 } | |
245 | |
246 if (!dir.valid_info) { | |
247 | |
248 if (dname.len + 1 + len > fname.len) { | |
249 fname.len = dname.len + 1 + len + 32; | |
250 | |
251 fname.data = ngx_palloc(pool, fname.len); | |
252 if (fname.data == NULL) { | |
253 return ngx_http_autoindex_error(r, &dir, dname.data); | |
254 } | |
255 | |
256 last = ngx_cpystrn(fname.data, dname.data, | |
257 dname.len + 1); | |
258 *last++ = '/'; | |
259 } | |
260 | |
261 ngx_cpystrn(last, ngx_de_name(&dir), len + 1); | |
262 | |
263 if (ngx_de_info(fname.data, &dir) == NGX_FILE_ERROR) { | |
264 err = ngx_errno; | |
265 | |
266 if (err != NGX_ENOENT) { | |
267 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, | |
268 ngx_de_info_n " \"%s\" failed", fname.data); | |
269 return ngx_http_autoindex_error(r, &dir, dname.data); | |
270 } | |
271 | |
272 if (ngx_de_link_info(fname.data, &dir) == NGX_FILE_ERROR) { | |
273 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, | |
274 ngx_de_link_info_n " \"%s\" failed", | |
275 fname.data); | |
276 return ngx_http_autoindex_error(r, &dir, dname.data); | |
277 } | |
278 } | |
279 } | |
280 | |
281 entry = ngx_array_push(&entries); | |
282 if (entry == NULL) { | |
283 return ngx_http_autoindex_error(r, &dir, dname.data); | |
284 } | |
285 | |
286 entry->name.len = len; | |
287 entry->escape = 2 * ngx_escape_uri(NULL, ngx_de_name(&dir), len, | |
288 NGX_ESCAPE_HTML); | |
289 | |
290 entry->name.data = ngx_palloc(pool, len + entry->escape + 1); | |
291 if (entry->name.data == NULL) { | |
292 return ngx_http_autoindex_error(r, &dir, dname.data); | |
293 } | |
294 | |
295 ngx_cpystrn(entry->name.data, ngx_de_name(&dir), len + 1); | |
296 | |
297 entry->dir = ngx_de_is_dir(&dir); | |
298 entry->mtime = ngx_de_mtime(&dir); | |
299 entry->size = ngx_de_size(&dir); | |
300 } | |
301 | |
302 if (ngx_close_dir(&dir) == NGX_ERROR) { | |
303 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
304 ngx_close_dir_n " \"%s\" failed", dname.data); | |
305 } | |
306 | |
307 len = sizeof(title) - 1 | |
308 + r->uri.len | |
309 + sizeof(header) - 1 | |
310 + r->uri.len | |
311 + sizeof("</h1>") - 1 | |
312 + sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1 | |
313 + sizeof("</pre><hr>") - 1 | |
314 + sizeof(tail) - 1; | |
315 | |
316 entry = entries.elts; | |
317 for (i = 0; i < entries.nelts; i++) { | |
318 len += sizeof("<a href=\"") - 1 | |
319 + 1 /* 1 is for "/" */ | |
320 + entry[i].name.len + entry[i].escape | |
321 + sizeof("\">") - 1 | |
322 + NGX_HTTP_AUTOINDEX_NAME_LEN + sizeof(">") - 2 | |
323 + sizeof("</a>") - 1 | |
324 + sizeof(" 28-Sep-1970 12:00 ") - 1 | |
325 + 19 | |
326 + 2; | |
327 } | |
328 | |
329 b = ngx_create_temp_buf(r->pool, len); | |
330 if (b == NULL) { | |
331 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
332 } | |
333 | |
334 if (entries.nelts > 1) { | |
335 ngx_qsort(entry, (size_t) entries.nelts, | |
336 sizeof(ngx_http_autoindex_entry_t), | |
337 ngx_http_autoindex_cmp_entries); | |
338 } | |
339 | |
340 b->last = ngx_cpymem(b->last, title, sizeof(title) - 1); | |
341 b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); | |
342 b->last = ngx_cpymem(b->last, header, sizeof(header) - 1); | |
343 b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); | |
344 b->last = ngx_cpymem(b->last, "</h1>", sizeof("</h1>") - 1); | |
345 | |
346 b->last = ngx_cpymem(b->last, "<hr><pre><a href=\"../\">../</a>" CRLF, | |
347 sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1); | |
348 | |
349 for (i = 0; i < entries.nelts; i++) { | |
350 b->last = ngx_cpymem(b->last, "<a href=\"", sizeof("<a href=\"") - 1); | |
351 | |
352 if (entry[i].escape) { | |
353 ngx_escape_uri(b->last, entry[i].name.data, entry[i].name.len, | |
354 NGX_ESCAPE_HTML); | |
355 | |
356 b->last += entry[i].name.len + entry[i].escape; | |
357 | |
358 } else { | |
359 b->last = ngx_cpymem(b->last, entry[i].name.data, | |
360 entry[i].name.len); | |
361 } | |
362 | |
363 if (entry[i].dir) { | |
364 *b->last++ = '/'; | |
365 } | |
366 | |
367 *b->last++ = '"'; | |
368 *b->last++ = '>'; | |
369 | |
370 b->last = ngx_cpystrn(b->last, entry[i].name.data, | |
371 NGX_HTTP_AUTOINDEX_NAME_LEN + 1); | |
372 | |
373 len = entry[i].name.len; | |
374 | |
375 if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) { | |
376 b->last = ngx_cpymem(b->last - 3, "..></a>", | |
377 sizeof("..></a>") - 1); | |
378 | |
379 } else { | |
380 if (entry[i].dir && NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) { | |
381 *b->last++ = '/'; | |
382 len++; | |
383 } | |
384 | |
385 b->last = ngx_cpymem(b->last, "</a>", sizeof("</a>") - 1); | |
386 ngx_memset(b->last, ' ', NGX_HTTP_AUTOINDEX_NAME_LEN - len); | |
387 b->last += NGX_HTTP_AUTOINDEX_NAME_LEN - len; | |
388 } | |
389 | |
390 *b->last++ = ' '; | |
391 | |
392 ngx_gmtime(entry[i].mtime, &tm); | |
393 | |
394 b->last = ngx_sprintf(b->last, "%02d-%s-%d %02d:%02d ", | |
395 tm.ngx_tm_mday, | |
396 months[tm.ngx_tm_mon - 1], | |
397 tm.ngx_tm_year, | |
398 tm.ngx_tm_hour, | |
399 tm.ngx_tm_min); | |
400 | |
401 if (entry[i].dir) { | |
402 b->last = ngx_cpymem(b->last, " -", | |
403 sizeof(" -") - 1); | |
404 | |
405 } else { | |
406 b->last = ngx_sprintf(b->last, "%19O", entry[i].size); | |
407 } | |
408 | |
409 *b->last++ = CR; | |
410 *b->last++ = LF; | |
411 } | |
412 | |
413 /* TODO: free temporary pool */ | |
414 | |
415 b->last = ngx_cpymem(b->last, "</pre><hr>", sizeof("</pre><hr>") - 1); | |
416 | |
417 b->last = ngx_cpymem(b->last, tail, sizeof(tail) - 1); | |
418 | |
419 r->headers_out.status = NGX_HTTP_OK; | |
420 r->headers_out.content_length_n = b->last - b->pos; | |
421 | |
422 r->headers_out.content_type = ngx_list_push(&r->headers_out.headers); | |
423 if (r->headers_out.content_type == NULL) { | |
424 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
425 } | |
426 | |
427 r->headers_out.content_type->key.len = 0; | |
428 r->headers_out.content_type->key.data = NULL; | |
429 r->headers_out.content_type->value.len = sizeof("text/html") - 1; | |
430 r->headers_out.content_type->value.data = (u_char *) "text/html"; | |
431 | |
432 rc = ngx_http_send_header(r); | |
433 | |
434 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
435 return rc; | |
436 } | |
437 | |
438 if (!r->main) { | |
439 b->last_buf = 1; | |
440 } | |
441 | |
442 out.buf = b; | |
443 out.next = NULL; | |
444 | |
445 return ngx_http_output_filter(r, &out); | |
446 } | |
447 | |
448 | |
449 static int | |
450 ngx_http_autoindex_cmp_entries(const void *one, const void *two) | |
451 { | |
452 ngx_http_autoindex_entry_t *first = (ngx_http_autoindex_entry_t *) one; | |
453 ngx_http_autoindex_entry_t *second = (ngx_http_autoindex_entry_t *) two; | |
454 | |
455 if (first->dir && !second->dir) { | |
456 /* move the directories to the start */ | |
457 return -1; | |
458 } | |
459 | |
460 if (!first->dir && second->dir) { | |
461 /* move the directories to the start */ | |
462 return 1; | |
463 } | |
464 | |
465 return (int) ngx_strcmp(first->name.data, second->name.data); | |
466 } | |
467 | |
468 | |
469 #if 0 | |
470 | |
471 static ngx_buf_t * | |
472 ngx_http_autoindex_alloc(ngx_http_autoindex_ctx_t *ctx, size_t size) | |
473 { | |
474 ngx_chain_t *cl; | |
475 | |
476 if (ctx->buf) { | |
477 | |
478 if ((size_t) (ctx->buf->end - ctx->buf->last) >= size) { | |
479 return ctx->buf; | |
480 } | |
481 | |
482 ctx->size += ctx->buf->last - ctx->buf->pos; | |
483 } | |
484 | |
485 ctx->buf = ngx_create_temp_buf(ctx->pool, ctx->alloc_size); | |
486 if (ctx->buf == NULL) { | |
487 return NULL; | |
488 } | |
489 | |
490 cl = ngx_alloc_chain_link(ctx->pool); | |
491 if (cl == NULL) { | |
492 return NULL; | |
493 } | |
494 | |
495 cl->buf = ctx->buf; | |
496 cl->next = NULL; | |
497 | |
498 *ctx->last_out = cl; | |
499 ctx->last_out = &cl->next; | |
500 | |
501 return ctx->buf; | |
502 } | |
503 | |
504 #endif | |
505 | |
506 | |
507 static ngx_int_t | |
508 ngx_http_autoindex_error(ngx_http_request_t *r, ngx_dir_t *dir, u_char *name) | |
509 { | |
510 if (ngx_close_dir(dir) == NGX_ERROR) { | |
511 ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, | |
512 ngx_close_dir_n " \"%s\" failed", name); | |
513 } | |
514 | |
515 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
516 } | |
517 | |
518 | |
519 static ngx_int_t | |
520 ngx_http_autoindex_init(ngx_cycle_t *cycle) | |
521 { | |
522 ngx_http_handler_pt *h; | |
523 ngx_http_core_main_conf_t *cmcf; | |
524 | |
525 cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module); | |
526 | |
527 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); | |
528 if (h == NULL) { | |
529 return NGX_ERROR; | |
530 } | |
531 | |
532 *h = ngx_http_autoindex_handler; | |
533 | |
534 return NGX_OK; | |
535 } | |
536 | |
537 | |
538 static void * | |
539 ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf) | |
540 { | |
541 ngx_http_autoindex_loc_conf_t *conf; | |
542 | |
543 conf = ngx_palloc(cf->pool, sizeof(ngx_http_autoindex_loc_conf_t)); | |
544 if (conf == NULL) { | |
545 return NGX_CONF_ERROR; | |
546 } | |
547 | |
548 conf->enable = NGX_CONF_UNSET; | |
549 | |
550 return conf; | |
551 } | |
552 | |
553 | |
554 static char * | |
555 ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
556 { | |
557 ngx_http_autoindex_loc_conf_t *prev = parent; | |
558 ngx_http_autoindex_loc_conf_t *conf = child; | |
559 | |
560 ngx_conf_merge_value(conf->enable, prev->enable, 0); | |
561 | |
562 return NGX_CONF_OK; | |
563 } |