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