upstream nginx-0.7.37
[nginx.git] / nginx / src / http / modules / ngx_http_autoindex_module.c
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     size_t         utf_len;
28     ngx_uint_t     escape;
29     ngx_uint_t     dir;
30     time_t         mtime;
31     off_t          size;
32 } ngx_http_autoindex_entry_t;
33
34
35 typedef struct {
36     ngx_flag_t     enable;
37     ngx_flag_t     localtime;
38     ngx_flag_t     exact_size;
39 } ngx_http_autoindex_loc_conf_t;
40
41
42 #define NGX_HTTP_AUTOINDEX_PREALLOCATE  50
43
44 #define NGX_HTTP_AUTOINDEX_NAME_LEN     50
45
46
47 static int ngx_libc_cdecl ngx_http_autoindex_cmp_entries(const void *one,
48     const void *two);
49 static ngx_int_t ngx_http_autoindex_error(ngx_http_request_t *r,
50     ngx_dir_t *dir, ngx_str_t *name);
51 static ngx_int_t ngx_http_autoindex_init(ngx_conf_t *cf);
52 static void *ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf);
53 static char *ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf,
54     void *parent, void *child);
55
56
57 static ngx_command_t  ngx_http_autoindex_commands[] = {
58
59     { ngx_string("autoindex"),
60       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
61       ngx_conf_set_flag_slot,
62       NGX_HTTP_LOC_CONF_OFFSET,
63       offsetof(ngx_http_autoindex_loc_conf_t, enable),
64       NULL },
65
66     { ngx_string("autoindex_localtime"),
67       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
68       ngx_conf_set_flag_slot,
69       NGX_HTTP_LOC_CONF_OFFSET,
70       offsetof(ngx_http_autoindex_loc_conf_t, localtime),
71       NULL },
72
73     { ngx_string("autoindex_exact_size"),
74       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
75       ngx_conf_set_flag_slot,
76       NGX_HTTP_LOC_CONF_OFFSET,
77       offsetof(ngx_http_autoindex_loc_conf_t, exact_size),
78       NULL },
79
80       ngx_null_command
81 };
82
83
84 static ngx_http_module_t  ngx_http_autoindex_module_ctx = {
85     NULL,                                  /* preconfiguration */
86     ngx_http_autoindex_init,               /* postconfiguration */
87
88     NULL,                                  /* create main configuration */
89     NULL,                                  /* init main configuration */
90
91     NULL,                                  /* create server configuration */
92     NULL,                                  /* merge server configuration */
93
94     ngx_http_autoindex_create_loc_conf,    /* create location configration */
95     ngx_http_autoindex_merge_loc_conf      /* merge location configration */
96 };
97
98
99 ngx_module_t  ngx_http_autoindex_module = {
100     NGX_MODULE_V1,
101     &ngx_http_autoindex_module_ctx,        /* module context */
102     ngx_http_autoindex_commands,           /* module directives */
103     NGX_HTTP_MODULE,                       /* module type */
104     NULL,                                  /* init master */
105     NULL,                                  /* init module */
106     NULL,                                  /* init process */
107     NULL,                                  /* init thread */
108     NULL,                                  /* exit thread */
109     NULL,                                  /* exit process */
110     NULL,                                  /* exit master */
111     NGX_MODULE_V1_PADDING
112 };
113
114
115 static u_char title[] =
116 "<html>" CRLF
117 "<head><title>Index of "
118 ;
119
120
121 static u_char header[] =
122 "</title></head>" CRLF
123 "<body bgcolor=\"white\">" CRLF
124 "<h1>Index of "
125 ;
126
127 static u_char tail[] =
128 "</body>" CRLF
129 "</html>" CRLF
130 ;
131
132
133 static ngx_int_t
134 ngx_http_autoindex_handler(ngx_http_request_t *r)
135 {
136     u_char                         *last, *filename, scale;
137     off_t                           length;
138     size_t                          len, utf_len, allocated, root;
139     ngx_tm_t                        tm;
140     ngx_err_t                       err;
141     ngx_buf_t                      *b;
142     ngx_int_t                       rc, size;
143     ngx_str_t                       path;
144     ngx_dir_t                       dir;
145     ngx_uint_t                      i, level;
146     ngx_pool_t                     *pool;
147     ngx_time_t                     *tp;
148     ngx_chain_t                     out;
149     ngx_array_t                     entries;
150     ngx_http_autoindex_entry_t     *entry;
151     ngx_http_autoindex_loc_conf_t  *alcf;
152
153     static char  *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
154                                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
155
156     if (r->uri.data[r->uri.len - 1] != '/') {
157         return NGX_DECLINED;
158     }
159
160     /* TODO: Win32 */
161     if (r->zero_in_uri) {
162         return NGX_DECLINED;
163     }
164
165     if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
166         return NGX_DECLINED;
167     }
168
169     alcf = ngx_http_get_module_loc_conf(r, ngx_http_autoindex_module);
170
171     if (!alcf->enable) {
172         return NGX_DECLINED;
173     }
174
175     /* NGX_DIR_MASK_LEN is lesser than NGX_HTTP_AUTOINDEX_PREALLOCATE */
176
177     last = ngx_http_map_uri_to_path(r, &path, &root,
178                                     NGX_HTTP_AUTOINDEX_PREALLOCATE);
179     if (last == NULL) {
180         return NGX_HTTP_INTERNAL_SERVER_ERROR;
181     }
182
183     allocated = path.len;
184     path.len = last - path.data;
185     if (path.len > 1) {
186         path.len--;
187     }
188     path.data[path.len] = '\0';
189
190     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
191                    "http autoindex: \"%s\"", path.data);
192
193     if (ngx_open_dir(&path, &dir) == NGX_ERROR) {
194         err = ngx_errno;
195
196         if (err == NGX_ENOENT
197             || err == NGX_ENOTDIR
198             || err == NGX_ENAMETOOLONG)
199         {
200             level = NGX_LOG_ERR;
201             rc = NGX_HTTP_NOT_FOUND;
202
203         } else if (err == NGX_EACCES) {
204             level = NGX_LOG_ERR;
205             rc = NGX_HTTP_FORBIDDEN;
206
207         } else {
208             level = NGX_LOG_CRIT;
209             rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
210         }
211
212         ngx_log_error(level, r->connection->log, err,
213                       ngx_open_dir_n " \"%s\" failed", path.data);
214
215         return rc;
216     }
217
218 #if (NGX_SUPPRESS_WARN)
219
220     /* MSVC thinks 'entries' may be used without having been initialized */
221     ngx_memzero(&entries, sizeof(ngx_array_t));
222
223 #endif
224
225     /* TODO: pool should be temporary pool */
226     pool = r->pool;
227
228     if (ngx_array_init(&entries, pool, 40, sizeof(ngx_http_autoindex_entry_t))
229         != NGX_OK)
230     {
231         return ngx_http_autoindex_error(r, &dir, &path);
232     }
233
234     r->headers_out.status = NGX_HTTP_OK;
235     r->headers_out.content_type_len = sizeof("text/html") - 1;
236     r->headers_out.content_type.len = sizeof("text/html") - 1;
237     r->headers_out.content_type.data = (u_char *) "text/html";
238
239     rc = ngx_http_send_header(r);
240
241     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
242         if (ngx_close_dir(&dir) == NGX_ERROR) {
243             ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
244                           ngx_close_dir_n " \"%V\" failed", &path);
245         }
246
247         return rc;
248     }
249
250     filename = path.data;
251     filename[path.len] = '/';
252
253     for ( ;; ) {
254         ngx_set_errno(0);
255
256         if (ngx_read_dir(&dir) == NGX_ERROR) {
257             err = ngx_errno;
258
259             if (err != NGX_ENOMOREFILES) {
260                 ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
261                               ngx_read_dir_n " \"%V\" failed", &path);
262                 return ngx_http_autoindex_error(r, &dir, &path);
263             }
264
265             break;
266         }
267
268         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
269                        "http autoindex file: \"%s\"", ngx_de_name(&dir));
270
271         len = ngx_de_namelen(&dir);
272
273         if (ngx_de_name(&dir)[0] == '.') {
274             continue;
275         }
276
277         if (!dir.valid_info) {
278
279             /* 1 byte for '/' and 1 byte for terminating '\0' */
280
281             if (path.len + 1 + len + 1 > allocated) {
282                 allocated = path.len + 1 + len + 1
283                                      + NGX_HTTP_AUTOINDEX_PREALLOCATE;
284
285                 filename = ngx_pnalloc(pool, allocated);
286                 if (filename == NULL) {
287                     return ngx_http_autoindex_error(r, &dir, &path);
288                 }
289
290                 last = ngx_cpystrn(filename, path.data, path.len + 1);
291                 *last++ = '/';
292             }
293
294             ngx_cpystrn(last, ngx_de_name(&dir), len + 1);
295
296             if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) {
297                 err = ngx_errno;
298
299                 if (err != NGX_ENOENT) {
300                     ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
301                                   ngx_de_info_n " \"%s\" failed", filename);
302
303                     if (err == NGX_EACCES) {
304                         continue;
305                     }
306
307                     return ngx_http_autoindex_error(r, &dir, &path);
308                 }
309
310                 if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) {
311                     ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
312                                   ngx_de_link_info_n " \"%s\" failed",
313                                   filename);
314                     return ngx_http_autoindex_error(r, &dir, &path);
315                 }
316             }
317         }
318
319         entry = ngx_array_push(&entries);
320         if (entry == NULL) {
321             return ngx_http_autoindex_error(r, &dir, &path);
322         }
323
324         entry->name.len = len;
325
326         entry->name.data = ngx_pnalloc(pool, len + 1);
327         if (entry->name.data == NULL) {
328             return ngx_http_autoindex_error(r, &dir, &path);
329         }
330
331         ngx_cpystrn(entry->name.data, ngx_de_name(&dir), len + 1);
332
333         entry->escape = 2 * ngx_escape_uri(NULL, ngx_de_name(&dir), len,
334                                            NGX_ESCAPE_HTML);
335
336         if (r->utf8) {
337             entry->utf_len = ngx_utf8_length(entry->name.data, entry->name.len);
338         } else {
339             entry->utf_len = len;
340         }
341
342         entry->dir = ngx_de_is_dir(&dir);
343         entry->mtime = ngx_de_mtime(&dir);
344         entry->size = ngx_de_size(&dir);
345     }
346
347     if (ngx_close_dir(&dir) == NGX_ERROR) {
348         ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
349                       ngx_close_dir_n " \"%s\" failed", &path);
350     }
351
352     len = sizeof(title) - 1
353           + r->uri.len
354           + sizeof(header) - 1
355           + r->uri.len
356           + sizeof("</h1>") - 1
357           + sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1
358           + sizeof("</pre><hr>") - 1
359           + sizeof(tail) - 1;
360
361     entry = entries.elts;
362     for (i = 0; i < entries.nelts; i++) {
363         len += sizeof("<a href=\"") - 1
364             + entry[i].name.len + entry[i].escape
365             + 1                                          /* 1 is for "/" */
366             + sizeof("\">") - 1
367             + entry[i].name.len - entry[i].utf_len
368             + NGX_HTTP_AUTOINDEX_NAME_LEN + sizeof("&gt;") - 2
369             + sizeof("</a>") - 1
370             + sizeof(" 28-Sep-1970 12:00 ") - 1
371             + 20                                         /* the file size */
372             + 2;
373     }
374
375     b = ngx_create_temp_buf(r->pool, len);
376     if (b == NULL) {
377         return NGX_HTTP_INTERNAL_SERVER_ERROR;
378     }
379
380     if (entries.nelts > 1) {
381         ngx_qsort(entry, (size_t) entries.nelts,
382                   sizeof(ngx_http_autoindex_entry_t),
383                   ngx_http_autoindex_cmp_entries);
384     }
385
386     b->last = ngx_cpymem(b->last, title, sizeof(title) - 1);
387     b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len);
388     b->last = ngx_cpymem(b->last, header, sizeof(header) - 1);
389     b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len);
390     b->last = ngx_cpymem(b->last, "</h1>", sizeof("</h1>") - 1);
391
392     b->last = ngx_cpymem(b->last, "<hr><pre><a href=\"../\">../</a>" CRLF,
393                          sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1);
394
395     tp = ngx_timeofday();
396
397     for (i = 0; i < entries.nelts; i++) {
398         b->last = ngx_cpymem(b->last, "<a href=\"", sizeof("<a href=\"") - 1);
399
400         if (entry[i].escape) {
401             ngx_escape_uri(b->last, entry[i].name.data, entry[i].name.len,
402                            NGX_ESCAPE_HTML);
403
404             b->last += entry[i].name.len + entry[i].escape;
405
406         } else {
407             b->last = ngx_cpymem(b->last, entry[i].name.data,
408                                  entry[i].name.len);
409         }
410
411         if (entry[i].dir) {
412             *b->last++ = '/';
413         }
414
415         *b->last++ = '"';
416         *b->last++ = '>';
417
418         len = entry[i].utf_len;
419
420         if (entry[i].name.len != len) {
421             if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) {
422                 utf_len = NGX_HTTP_AUTOINDEX_NAME_LEN - 3 + 1;
423
424             } else {
425                 utf_len = NGX_HTTP_AUTOINDEX_NAME_LEN + 1;
426             }
427
428             b->last = ngx_utf8_cpystrn(b->last, entry[i].name.data,
429                                        utf_len, entry[i].name.len + 1);
430             last = b->last;
431
432         } else {
433             b->last = ngx_cpystrn(b->last, entry[i].name.data,
434                                   NGX_HTTP_AUTOINDEX_NAME_LEN + 1);
435             last = b->last - 3;
436         }
437
438         if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) {
439             b->last = ngx_cpymem(last, "..&gt;</a>", sizeof("..&gt;</a>") - 1);
440
441         } else {
442             if (entry[i].dir && NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) {
443                 *b->last++ = '/';
444                 len++;
445             }
446
447             b->last = ngx_cpymem(b->last, "</a>", sizeof("</a>") - 1);
448             ngx_memset(b->last, ' ', NGX_HTTP_AUTOINDEX_NAME_LEN - len);
449             b->last += NGX_HTTP_AUTOINDEX_NAME_LEN - len;
450         }
451
452         *b->last++ = ' ';
453
454         ngx_gmtime(entry[i].mtime + tp->gmtoff * 60 * alcf->localtime, &tm);
455
456         b->last = ngx_sprintf(b->last, "%02d-%s-%d %02d:%02d ",
457                               tm.ngx_tm_mday,
458                               months[tm.ngx_tm_mon - 1],
459                               tm.ngx_tm_year,
460                               tm.ngx_tm_hour,
461                               tm.ngx_tm_min);
462
463         if (alcf->exact_size) {
464             if (entry[i].dir) {
465                 b->last = ngx_cpymem(b->last,  "                  -",
466                                      sizeof("                  -") - 1);
467             } else {
468                 b->last = ngx_sprintf(b->last, "%19O", entry[i].size);
469             }
470
471         } else {
472             if (entry[i].dir) {
473                 b->last = ngx_cpymem(b->last,  "      -",
474                                      sizeof("      -") - 1);
475
476             } else {
477                 length = entry[i].size;
478
479                 if (length > 1024 * 1024 * 1024 - 1) {
480                     size = (ngx_int_t) (length / (1024 * 1024 * 1024));
481                     if ((length % (1024 * 1024 * 1024))
482                                                 > (1024 * 1024 * 1024 / 2 - 1))
483                     {
484                         size++;
485                     }
486                     scale = 'G';
487
488                 } else if (length > 1024 * 1024 - 1) {
489                     size = (ngx_int_t) (length / (1024 * 1024));
490                     if ((length % (1024 * 1024)) > (1024 * 1024 / 2 - 1)) {
491                         size++;
492                     }
493                     scale = 'M';
494
495                 } else if (length > 9999) {
496                     size = (ngx_int_t) (length / 1024);
497                     if (length % 1024 > 511) {
498                         size++;
499                     }
500                     scale = 'K';
501
502                 } else {
503                     size = (ngx_int_t) length;
504                     scale = '\0';
505                 }
506
507                 if (scale) {
508                     b->last = ngx_sprintf(b->last, "%6i%c", size, scale);
509
510                 } else {
511                     b->last = ngx_sprintf(b->last, " %6i", size);
512                 }
513             }
514         }
515
516         *b->last++ = CR;
517         *b->last++ = LF;
518     }
519
520     /* TODO: free temporary pool */
521
522     b->last = ngx_cpymem(b->last, "</pre><hr>", sizeof("</pre><hr>") - 1);
523
524     b->last = ngx_cpymem(b->last, tail, sizeof(tail) - 1);
525
526     if (r == r->main) {
527         b->last_buf = 1;
528     }
529
530     b->last_in_chain = 1;
531
532     out.buf = b;
533     out.next = NULL;
534
535     return ngx_http_output_filter(r, &out);
536 }
537
538
539 static int ngx_libc_cdecl
540 ngx_http_autoindex_cmp_entries(const void *one, const void *two)
541 {
542     ngx_http_autoindex_entry_t *first = (ngx_http_autoindex_entry_t *) one;
543     ngx_http_autoindex_entry_t *second = (ngx_http_autoindex_entry_t *) two;
544
545     if (first->dir && !second->dir) {
546         /* move the directories to the start */
547         return -1;
548     }
549
550     if (!first->dir && second->dir) {
551         /* move the directories to the start */
552         return 1;
553     }
554
555     return (int) ngx_strcmp(first->name.data, second->name.data);
556 }
557
558
559 #if 0
560
561 static ngx_buf_t *
562 ngx_http_autoindex_alloc(ngx_http_autoindex_ctx_t *ctx, size_t size)
563 {
564     ngx_chain_t  *cl;
565
566     if (ctx->buf) {
567
568         if ((size_t) (ctx->buf->end - ctx->buf->last) >= size) {
569             return ctx->buf;
570         }
571
572         ctx->size += ctx->buf->last - ctx->buf->pos;
573     }
574
575     ctx->buf = ngx_create_temp_buf(ctx->pool, ctx->alloc_size);
576     if (ctx->buf == NULL) {
577         return NULL;
578     }
579
580     cl = ngx_alloc_chain_link(ctx->pool);
581     if (cl == NULL) {
582         return NULL;
583     }
584
585     cl->buf = ctx->buf;
586     cl->next = NULL;
587
588     *ctx->last_out = cl;
589     ctx->last_out = &cl->next;
590
591     return ctx->buf;
592 }
593
594 #endif
595
596
597 static ngx_int_t
598 ngx_http_autoindex_error(ngx_http_request_t *r, ngx_dir_t *dir, ngx_str_t *name)
599 {
600     if (ngx_close_dir(dir) == NGX_ERROR) {
601         ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
602                       ngx_close_dir_n " \"%V\" failed", name);
603     }
604
605     return NGX_HTTP_INTERNAL_SERVER_ERROR;
606 }
607
608
609 static void *
610 ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf)
611 {
612     ngx_http_autoindex_loc_conf_t  *conf;
613
614     conf = ngx_palloc(cf->pool, sizeof(ngx_http_autoindex_loc_conf_t));
615     if (conf == NULL) {
616         return NGX_CONF_ERROR;
617     }
618
619     conf->enable = NGX_CONF_UNSET;
620     conf->localtime = NGX_CONF_UNSET;
621     conf->exact_size = NGX_CONF_UNSET;
622
623     return conf;
624 }
625
626
627 static char *
628 ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
629 {
630     ngx_http_autoindex_loc_conf_t *prev = parent;
631     ngx_http_autoindex_loc_conf_t *conf = child;
632
633     ngx_conf_merge_value(conf->enable, prev->enable, 0);
634     ngx_conf_merge_value(conf->localtime, prev->localtime, 0);
635     ngx_conf_merge_value(conf->exact_size, prev->exact_size, 1);
636
637     return NGX_CONF_OK;
638 }
639
640
641 static ngx_int_t
642 ngx_http_autoindex_init(ngx_conf_t *cf)
643 {
644     ngx_http_handler_pt        *h;
645     ngx_http_core_main_conf_t  *cmcf;
646
647     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
648
649     h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
650     if (h == NULL) {
651         return NGX_ERROR;
652     }
653
654     *h = ngx_http_autoindex_handler;
655
656     return NGX_OK;
657 }