1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
60 * mod_autoindex.c: Handles the on-the-fly html index generation
65 * Adapted to Apache by rst.
67 * Version sort added by Martin Pool <mbp@humbug.org.au>. */
69 #include "apr_strings.h"
70 #include "ap_config.h"
72 #include "http_config.h"
73 #include "http_core.h"
74 #include "http_request.h"
75 #include "http_protocol.h"
77 #include "http_main.h"
78 #include "util_script.h"
79 #include "apr_fnmatch.h"
80 #include "apr_strings.h"
88 module AP_MODULE_DECLARE_DATA autoindex_module;
90 /****************************************************************
92 * Handling configuration directives...
97 #define FRONT_MATTER 1
100 #define FANCY_INDEXING 1 /* Indexing options */
101 #define ICONS_ARE_LINKS 2
102 #define SCAN_HTML_TITLES 4
103 #define SUPPRESS_LAST_MOD 8
104 #define SUPPRESS_SIZE 16
105 #define SUPPRESS_DESC 32
106 #define SUPPRESS_PREAMBLE 64
107 #define SUPPRESS_COLSORT 128
108 #define NO_OPTIONS 256
109 #define VERSION_SORT 512
119 * Define keys for sorting.
121 #define K_NAME 'N' /* Sort by file name (default) */
122 #define K_LAST_MOD 'M' /* Last modification date */
123 #define K_SIZE 'S' /* Size (absolute, not as displayed) */
124 #define K_DESC 'D' /* Description */
126 #define D_ASCENDING 'A'
127 #define D_DESCENDING 'D'
130 * These are the dimensions of the default icons supplied with Apache.
132 #define DEFAULT_ICON_WIDTH 20
133 #define DEFAULT_ICON_HEIGHT 22
136 * Other default dimensions.
138 #define DEFAULT_NAME_WIDTH 23
147 typedef struct ai_desc_t {
154 typedef struct autoindex_config_struct {
158 int incremented_opts;
159 int decremented_opts;
166 apr_array_header_t *icon_list;
167 apr_array_header_t *alt_list;
168 apr_array_header_t *desc_list;
169 apr_array_header_t *ign_list;
170 apr_array_header_t *hdr_list;
171 apr_array_header_t *rdme_list;
173 } autoindex_config_rec;
175 static char c_by_encoding, c_by_type, c_by_path;
177 #define BY_ENCODING &c_by_encoding
178 #define BY_TYPE &c_by_type
179 #define BY_PATH &c_by_path
182 * Return true if the specified string refers to the parent directory (i.e.,
183 * matches ".." or "../"). Hopefully this one call is significantly less
184 * expensive than multiple strcmp() calls.
186 static apr_inline int is_parent(const char *name)
189 * Now, IFF the first two bytes are dots, and the third byte is either
190 * EOS (\0) or a slash followed by EOS, we have a match.
192 if (((name[0] == '.') && (name[1] == '.'))
193 && ((name[2] == '\0')
194 || ((name[2] == '/') && (name[3] == '\0')))) {
201 * This routine puts the standard HTML header at the top of the index page.
202 * We include the DOCTYPE because we may be using features therefrom (i.e.,
203 * HEIGHT and WIDTH attributes on the icons if we're FancyIndexing).
205 static void emit_preamble(request_rec *r, char *title)
207 ap_rvputs(r, DOCTYPE_HTML_3_2,
208 "<HTML>\n <HEAD>\n <TITLE>Index of ", title,
209 "</TITLE>\n </HEAD>\n <BODY>\n", NULL);
212 static void push_item(apr_array_header_t *arr, char *type, const char *to,
213 const char *path, const char *data)
215 struct item *p = (struct item *) apr_push_array(arr);
225 p->data = data ? apr_pstrdup(arr->cont, data) : NULL;
226 p->apply_path = apr_pstrcat(arr->cont, path, "*", NULL);
228 if ((type == BY_PATH) && (!ap_is_matchexp(to))) {
229 p->apply_to = apr_pstrcat(arr->cont, "*", to, NULL);
232 p->apply_to = apr_pstrdup(arr->cont, to);
239 static const char *add_alt(cmd_parms *cmd, void *d, const char *alt,
242 if (cmd->info == BY_PATH) {
243 if (!strcmp(to, "**DIRECTORY**")) {
244 to = "^^DIRECTORY^^";
247 if (cmd->info == BY_ENCODING) {
248 char *tmp = apr_pstrdup(cmd->pool, to);
253 push_item(((autoindex_config_rec *) d)->alt_list, cmd->info, to,
258 static const char *add_icon(cmd_parms *cmd, void *d, const char *icon,
261 char *iconbak = apr_pstrdup(cmd->pool, icon);
263 if (icon[0] == '(') {
265 char *cl = strchr(iconbak, ')');
268 return "missing closing paren";
270 alt = ap_getword_nc(cmd->pool, &iconbak, ',');
271 *cl = '\0'; /* Lose closing paren */
272 add_alt(cmd, d, &alt[1], to);
274 if (cmd->info == BY_PATH) {
275 if (!strcmp(to, "**DIRECTORY**")) {
276 to = "^^DIRECTORY^^";
279 if (cmd->info == BY_ENCODING) {
280 char *tmp = apr_pstrdup(cmd->pool, to);
285 push_item(((autoindex_config_rec *) d)->icon_list, cmd->info, to,
291 * Add description text for a filename pattern. If the pattern has
292 * wildcards already (or we need to add them), add leading and
293 * trailing wildcards to it to ensure substring processing. If the
294 * pattern contains a '/' anywhere, force wildcard matching mode,
295 * add a slash to the prefix so that "bar/bletch" won't be matched
296 * by "foobar/bletch", and make a note that there's a delimiter;
297 * the matching routine simplifies to just the actual filename
298 * whenever it can. This allows definitions in parent directories
299 * to be made for files in subordinate ones using relative paths.
303 * Absent a strcasestr() function, we have to force wildcards on
304 * systems for which "AAA" and "aaa" mean the same file.
306 #ifdef CASE_BLIND_FILESYSTEM
307 #define WILDCARDS_REQUIRED 1
309 #define WILDCARDS_REQUIRED 0
312 static const char *add_desc(cmd_parms *cmd, void *d, const char *desc,
315 autoindex_config_rec *dcfg = (autoindex_config_rec *) d;
316 ai_desc_t *desc_entry;
319 desc_entry = (ai_desc_t *) apr_push_array(dcfg->desc_list);
320 desc_entry->full_path = (ap_strchr_c(to, '/') == NULL) ? 0 : 1;
321 desc_entry->wildcards = (WILDCARDS_REQUIRED
322 || desc_entry->full_path
323 || apr_is_fnmatch(to));
324 if (desc_entry->wildcards) {
325 prefix = desc_entry->full_path ? "*/" : "*";
326 desc_entry->pattern = apr_pstrcat(dcfg->desc_list->cont,
327 prefix, to, "*", NULL);
330 desc_entry->pattern = apr_pstrdup(dcfg->desc_list->cont, to);
332 desc_entry->description = apr_pstrdup(dcfg->desc_list->cont, desc);
336 static const char *add_ignore(cmd_parms *cmd, void *d, const char *ext)
338 push_item(((autoindex_config_rec *) d)->ign_list, 0, ext, cmd->path, NULL);
342 static const char *add_header(cmd_parms *cmd, void *d, const char *name)
344 push_item(((autoindex_config_rec *) d)->hdr_list, 0, NULL, cmd->path,
349 static const char *add_readme(cmd_parms *cmd, void *d, const char *name)
351 push_item(((autoindex_config_rec *) d)->rdme_list, 0, NULL, cmd->path,
356 /* A legacy directive, FancyIndexing is superseded by the IndexOptions
357 * keyword. But for compatibility..
359 static const char *fancy_indexing(cmd_parms *cmd, void *d, int arg)
363 autoindex_config_rec *cfg;
365 cfg = (autoindex_config_rec *) d;
367 if (curopts & NO_OPTIONS) {
368 return "FancyIndexing directive conflicts with existing "
371 newopts = (arg ? (curopts | FANCY_INDEXING) : (curopts & ~FANCY_INDEXING));
376 static const char *add_opts(cmd_parms *cmd, void *d, const char *optstr)
383 autoindex_config_rec *d_cfg = (autoindex_config_rec *) d;
386 opts_add = d_cfg->incremented_opts;
387 opts_remove = d_cfg->decremented_opts;
391 w = ap_getword_conf(cmd->pool, &optstr);
392 if ((*w == '+') || (*w == '-')) {
398 if (!strcasecmp(w, "FancyIndexing")) {
399 option = FANCY_INDEXING;
401 else if (!strcasecmp(w, "IconsAreLinks")) {
402 option = ICONS_ARE_LINKS;
404 else if (!strcasecmp(w, "ScanHTMLTitles")) {
405 option = SCAN_HTML_TITLES;
407 else if (!strcasecmp(w, "SuppressLastModified")) {
408 option = SUPPRESS_LAST_MOD;
410 else if (!strcasecmp(w, "SuppressSize")) {
411 option = SUPPRESS_SIZE;
413 else if (!strcasecmp(w, "SuppressDescription")) {
414 option = SUPPRESS_DESC;
416 else if (!strcasecmp(w, "SuppressHTMLPreamble")) {
417 option = SUPPRESS_PREAMBLE;
419 else if (!strcasecmp(w, "SuppressColumnSorting")) {
420 option = SUPPRESS_COLSORT;
422 else if (!strcasecmp(w, "VersionSort")) {
423 option = VERSION_SORT;
425 else if (!strcasecmp(w, "None")) {
426 if (action != '\0') {
427 return "Cannot combine '+' or '-' with 'None' keyword";
433 else if (!strcasecmp(w, "IconWidth")) {
435 d_cfg->icon_width = DEFAULT_ICON_WIDTH;
438 d_cfg->icon_width = 0;
441 else if (!strncasecmp(w, "IconWidth=", 10)) {
443 return "Cannot combine '-' with IconWidth=n";
445 d_cfg->icon_width = atoi(&w[10]);
447 else if (!strcasecmp(w, "IconHeight")) {
449 d_cfg->icon_height = DEFAULT_ICON_HEIGHT;
452 d_cfg->icon_height = 0;
455 else if (!strncasecmp(w, "IconHeight=", 11)) {
457 return "Cannot combine '-' with IconHeight=n";
459 d_cfg->icon_height = atoi(&w[11]);
461 else if (!strcasecmp(w, "NameWidth")) {
463 return "NameWidth with no value may only appear as "
466 d_cfg->name_width = DEFAULT_NAME_WIDTH;
467 d_cfg->name_adjust = K_NOADJUST;
469 else if (!strncasecmp(w, "NameWidth=", 10)) {
471 return "Cannot combine '-' with NameWidth=n";
474 d_cfg->name_adjust = K_ADJUST;
477 int width = atoi(&w[10]);
480 return "NameWidth value must be greater than 5";
482 d_cfg->name_width = width;
483 d_cfg->name_adjust = K_NOADJUST;
487 return "Invalid directory indexing option";
489 if (action == '\0') {
494 else if (action == '+') {
496 opts_remove &= ~option;
499 opts_remove |= option;
503 if ((opts & NO_OPTIONS) && (opts & ~NO_OPTIONS)) {
504 return "Cannot combine other IndexOptions keywords with 'None'";
506 d_cfg->incremented_opts = opts_add;
507 d_cfg->decremented_opts = opts_remove;
512 static const char *set_default_order(cmd_parms *cmd, void *m, const char *direction,
516 autoindex_config_rec *d_cfg = (autoindex_config_rec *) m;
518 apr_cpystrn(temp, "k=d", sizeof(temp));
519 if (!strcasecmp(direction, "Ascending")) {
520 temp[2] = D_ASCENDING;
522 else if (!strcasecmp(direction, "Descending")) {
523 temp[2] = D_DESCENDING;
526 return "First keyword must be 'Ascending' or 'Descending'";
529 if (!strcasecmp(key, "Name")) {
532 else if (!strcasecmp(key, "Date")) {
533 temp[0] = K_LAST_MOD;
535 else if (!strcasecmp(key, "Size")) {
538 else if (!strcasecmp(key, "Description")) {
542 return "Second keyword must be 'Name', 'Date', 'Size', or "
546 if (d_cfg->default_order == NULL) {
547 d_cfg->default_order = apr_palloc(cmd->pool, 4);
548 d_cfg->default_order[3] = '\0';
550 apr_cpystrn(d_cfg->default_order, temp, sizeof(temp));
554 #define DIR_CMD_PERMS OR_INDEXES
556 static const command_rec autoindex_cmds[] =
558 AP_INIT_ITERATE2("AddIcon", add_icon, BY_PATH, DIR_CMD_PERMS,
559 "an icon URL followed by one or more filenames"),
560 AP_INIT_ITERATE2("AddIconByType", add_icon, BY_TYPE, DIR_CMD_PERMS,
561 "an icon URL followed by one or more MIME types"),
562 AP_INIT_ITERATE2("AddIconByEncoding", add_icon, BY_ENCODING, DIR_CMD_PERMS,
563 "an icon URL followed by one or more content encodings"),
564 AP_INIT_ITERATE2("AddAlt", add_alt, BY_PATH, DIR_CMD_PERMS,
565 "alternate descriptive text followed by one or more filenames"),
566 AP_INIT_ITERATE2("AddAltByType", add_alt, BY_TYPE, DIR_CMD_PERMS,
567 "alternate descriptive text followed by one or more MIME types"),
568 AP_INIT_ITERATE2("AddAltByEncoding", add_alt, BY_ENCODING, DIR_CMD_PERMS,
569 "alternate descriptive text followed by one or more content encodings"),
570 AP_INIT_RAW_ARGS("IndexOptions", add_opts, NULL, DIR_CMD_PERMS,
571 "one or more index options"),
572 AP_INIT_TAKE2("IndexOrderDefault", set_default_order, NULL, DIR_CMD_PERMS,
573 "{Ascending,Descending} {Name,Size,Description,Date}"),
574 AP_INIT_ITERATE("IndexIgnore", add_ignore, NULL, DIR_CMD_PERMS,
575 "one or more file extensions"),
576 AP_INIT_ITERATE2("AddDescription", add_desc, BY_PATH, DIR_CMD_PERMS,
577 "Descriptive text followed by one or more filenames"),
578 AP_INIT_TAKE1("HeaderName", add_header, NULL, DIR_CMD_PERMS,
580 AP_INIT_TAKE1("ReadmeName", add_readme, NULL, DIR_CMD_PERMS,
582 AP_INIT_FLAG("FancyIndexing", fancy_indexing, NULL, DIR_CMD_PERMS,
583 "Limited to 'on' or 'off' (superseded by IndexOptions FancyIndexing)"),
584 AP_INIT_TAKE1("DefaultIcon", ap_set_string_slot,
585 (void *) XtOffsetOf(autoindex_config_rec, default_icon),
586 DIR_CMD_PERMS, "an icon URL"),
590 static void *create_autoindex_config(apr_pool_t *p, char *dummy)
592 autoindex_config_rec *new =
593 (autoindex_config_rec *) apr_pcalloc(p, sizeof(autoindex_config_rec));
596 new->icon_height = 0;
597 new->name_width = DEFAULT_NAME_WIDTH;
598 new->name_adjust = K_UNSET;
599 new->icon_list = apr_make_array(p, 4, sizeof(struct item));
600 new->alt_list = apr_make_array(p, 4, sizeof(struct item));
601 new->desc_list = apr_make_array(p, 4, sizeof(ai_desc_t));
602 new->ign_list = apr_make_array(p, 4, sizeof(struct item));
603 new->hdr_list = apr_make_array(p, 4, sizeof(struct item));
604 new->rdme_list = apr_make_array(p, 4, sizeof(struct item));
606 new->incremented_opts = 0;
607 new->decremented_opts = 0;
608 new->default_order = NULL;
613 static void *merge_autoindex_configs(apr_pool_t *p, void *basev, void *addv)
615 autoindex_config_rec *new;
616 autoindex_config_rec *base = (autoindex_config_rec *) basev;
617 autoindex_config_rec *add = (autoindex_config_rec *) addv;
619 new = (autoindex_config_rec *) apr_pcalloc(p, sizeof(autoindex_config_rec));
620 new->default_icon = add->default_icon ? add->default_icon
621 : base->default_icon;
622 new->icon_height = add->icon_height ? add->icon_height : base->icon_height;
623 new->icon_width = add->icon_width ? add->icon_width : base->icon_width;
625 new->alt_list = apr_append_arrays(p, add->alt_list, base->alt_list);
626 new->ign_list = apr_append_arrays(p, add->ign_list, base->ign_list);
627 new->hdr_list = apr_append_arrays(p, add->hdr_list, base->hdr_list);
628 new->desc_list = apr_append_arrays(p, add->desc_list, base->desc_list);
629 new->icon_list = apr_append_arrays(p, add->icon_list, base->icon_list);
630 new->rdme_list = apr_append_arrays(p, add->rdme_list, base->rdme_list);
631 if (add->opts & NO_OPTIONS) {
633 * If the current directory says 'no options' then we also
634 * clear any incremental mods from being inheritable further down.
636 new->opts = NO_OPTIONS;
637 new->incremented_opts = 0;
638 new->decremented_opts = 0;
642 * If there were any nonincremental options selected for
643 * this directory, they dominate and we don't inherit *anything.*
644 * Contrariwise, we *do* inherit if the only settings here are
647 if (add->opts == 0) {
648 new->incremented_opts = (base->incremented_opts
649 | add->incremented_opts)
650 & ~add->decremented_opts;
651 new->decremented_opts = (base->decremented_opts
652 | add->decremented_opts);
654 * We may have incremental settings, so make sure we don't
655 * inadvertently inherit an IndexOptions None from above.
657 new->opts = (base->opts & ~NO_OPTIONS);
661 * There are local nonincremental settings, which clear
662 * all inheritance from above. They *are* the new base settings.
664 new->opts = add->opts;;
667 * We're guaranteed that there'll be no overlap between
668 * the add-options and the remove-options.
670 new->opts |= new->incremented_opts;
671 new->opts &= ~new->decremented_opts;
674 * Inherit the NameWidth settings if there aren't any specific to
675 * the new location; otherwise we'll end up using the defaults set in the
676 * config-rec creation routine.
678 if (add->name_adjust == K_UNSET) {
679 new->name_width = base->name_width;
680 new->name_adjust = base->name_adjust;
683 new->name_width = add->name_width;
684 new->name_adjust = add->name_adjust;
687 new->default_order = (add->default_order != NULL)
688 ? add->default_order : base->default_order;
692 /****************************************************************
694 * Looking things up in config entries...
697 /* Structure used to hold entries when we're actually building an index */
707 int ascending, version_sort;
711 static char *find_item(request_rec *r, apr_array_header_t *list, int path_only)
713 const char *content_type = ap_field_noparam(r->pool, r->content_type);
714 const char *content_encoding = r->content_encoding;
715 char *path = r->filename;
717 struct item *items = (struct item *) list->elts;
720 for (i = 0; i < list->nelts; ++i) {
721 struct item *p = &items[i];
723 /* Special cased for ^^DIRECTORY^^ and ^^BLANKICON^^ */
724 if ((path[0] == '^') || (!ap_strcmp_match(path, p->apply_path))) {
725 if (!*(p->apply_to)) {
728 else if (p->type == BY_PATH || path[0] == '^') {
729 if (!ap_strcmp_match(path, p->apply_to)) {
733 else if (!path_only) {
734 if (!content_encoding) {
735 if (p->type == BY_TYPE) {
737 && !ap_strcasecmp_match(content_type,
744 if (p->type == BY_ENCODING) {
745 if (!ap_strcasecmp_match(content_encoding,
757 #define find_icon(d,p,t) find_item(p,d->icon_list,t)
758 #define find_alt(d,p,t) find_item(p,d->alt_list,t)
759 #define find_header(d,p) find_item(p,d->hdr_list,0)
760 #define find_readme(d,p) find_item(p,d->rdme_list,0)
762 static char *find_default_icon(autoindex_config_rec *d, char *bogus_name)
766 /* Bleah. I tried to clean up find_item, and it lead to this bit
767 * of ugliness. Note that the fields initialized are precisely
768 * those that find_item looks at...
771 r.filename = bogus_name;
772 r.content_type = r.content_encoding = NULL;
774 return find_item(&r, d->icon_list, 1);
778 * Look through the list of pattern/description pairs and return the first one
779 * if any) that matches the filename in the request. If multiple patterns
780 * match, only the first one is used; since the order in the array is the
781 * same as the order in which directives were processed, earlier matching
782 * directives will dominate.
785 #ifdef CASE_BLIND_FILESYSTEM
786 #define MATCH_FLAGS FNM_CASE_BLIND
788 #define MATCH_FLAGS 0
791 static char *find_desc(autoindex_config_rec *dcfg, request_rec *r)
794 ai_desc_t *list = (ai_desc_t *) dcfg->desc_list->elts;
795 const char *filename_full = r->filename;
796 const char *filename_only;
797 const char *filename;
800 * If the filename includes a path, extract just the name itself
801 * for the simple matches.
803 if ((filename_only = ap_strrchr_c(filename_full, '/')) == NULL) {
804 filename_only = filename_full;
809 for (i = 0; i < dcfg->desc_list->nelts; ++i) {
810 ai_desc_t *tuple = &list[i];
814 * Only use the full-path filename if the pattern contains '/'s.
816 filename = (tuple->full_path) ? filename_full : filename_only;
818 * Make the comparison using the cheapest method; only do
819 * wildcard checking if we must.
821 if (tuple->wildcards) {
822 found = (apr_fnmatch(tuple->pattern, filename, MATCH_FLAGS) == 0);
825 found = (ap_strstr_c(filename, tuple->pattern) != NULL);
828 return tuple->description;
834 static int ignore_entry(autoindex_config_rec *d, char *path)
836 apr_array_header_t *list = d->ign_list;
837 struct item *items = (struct item *) list->elts;
841 if ((tt = strrchr(path, '/')) == NULL) {
848 for (i = 0; i < list->nelts; ++i) {
849 struct item *p = &items[i];
852 if ((ap = strrchr(p->apply_to, '/')) == NULL) {
859 #ifndef CASE_BLIND_FILESYSTEM
860 if (!ap_strcmp_match(path, p->apply_path)
861 && !ap_strcmp_match(tt, ap)) {
864 #else /* !CASE_BLIND_FILESYSTEM */
866 * On some platforms, the match must be case-blind. This is really
867 * a factor of the filesystem involved, but we can't detect that
868 * reliably - so we have to granularise at the OS level.
870 if (!ap_strcasecmp_match(path, p->apply_path)
871 && !ap_strcasecmp_match(tt, ap)) {
874 #endif /* !CASE_BLIND_FILESYSTEM */
879 /*****************************************************************
881 * Actually generating output
885 * Elements of the emitted document:
887 * Emitted unless SUPPRESS_PREAMBLE is set AND ap_run_sub_req
888 * succeeds for the (content_type == text/html) header file.
890 * Emitted if found (and able).
892 * Emitted if a header file is NOT emitted.
896 * Emitted if FANCY_INDEXING is set.
898 * Emitted if found (and able).
900 * Emitted if ServerSignature is not Off AND a readme file
903 * Emitted unless SUPPRESS_PREAMBLE is set AND ap_run_sub_req
904 * succeeds for the (content_type == text/html) readme file.
909 * emit a plain text file
911 static void do_emit_plain(request_rec *r, apr_file_t *f)
913 char buf[IOBUFSIZE + 1];
918 ap_rputs("<PRE>\n", r);
919 while (!apr_eof(f)) {
921 n = sizeof(char) * IOBUFSIZE;
922 stat = apr_read(f, buf, &n);
924 while (stat != APR_SUCCESS && APR_STATUS_IS_EINTR(stat));
925 if (n == -1 || n == 0) {
931 for (i = c; i < n; i++) {
932 if (buf[i] == '<' || buf[i] == '>' || buf[i] == '&') {
938 ap_rputs(&buf[c], r);
942 else if (ch == '>') {
945 else if (ch == '&') {
946 ap_rputs("&", r);
951 ap_rputs("</PRE>\n", r);
955 * Handle the preamble through the H1 tag line, inclusive. Locate
956 * the file with a subrequests. Process text/html documents by actually
957 * running the subrequest; text/xxx documents get copied verbatim,
958 * and any other content type is ignored. This means that a non-text
959 * document (such as HEADER.gif) might get multiviewed as the result
960 * instead of a text document, meaning nothing will be displayed, but
963 static void emit_head(request_rec *r, char *header_fname, int suppress_amble,
966 apr_file_t *f = NULL;
967 request_rec *rr = NULL;
972 * If there's a header file, send a subrequest to look for it. If it's
973 * found and a text file, handle it -- otherwise fall through and
974 * pretend there's nothing there.
976 if ((header_fname != NULL)
977 && (rr = ap_sub_req_lookup_uri(header_fname, r, NULL))
978 && (rr->status == HTTP_OK)
979 && (rr->filename != NULL)
980 && rr->finfo.filetype == APR_REG) {
982 * Check for the two specific cases we allow: text/html and
983 * text/anything-else. The former is allowed to be processed for
986 if (rr->content_type != NULL) {
987 if (!strcasecmp(ap_field_noparam(r->pool, rr->content_type),
989 /* Hope everything will work... */
993 if (! suppress_amble) {
994 emit_preamble(r, title);
997 * If there's a problem running the subrequest, display the
998 * preamble if we didn't do it before -- the header file
999 * didn't get displayed.
1001 if (ap_run_sub_req(rr) != OK) {
1002 /* It didn't work */
1003 emit_amble = suppress_amble;
1007 else if (!strncasecmp("text/", rr->content_type, 5)) {
1009 * If we can open the file, prefix it with the preamble
1010 * regardless; since we'll be sending a <PRE> block around
1011 * the file's contents, any HTML header it had won't end up
1014 if (apr_open(&f, rr->filename, APR_READ,
1015 APR_OS_DEFAULT, r->pool) == APR_SUCCESS) {
1016 emit_preamble(r, title);
1018 do_emit_plain(r, f);
1027 emit_preamble(r, title);
1030 ap_rvputs(r, "<H1>Index of ", title, "</H1>\n", NULL);
1033 ap_destroy_sub_req(rr);
1039 * Handle the Readme file through the postamble, inclusive. Locate
1040 * the file with a subrequests. Process text/html documents by actually
1041 * running the subrequest; text/xxx documents get copied verbatim,
1042 * and any other content type is ignored. This means that a non-text
1043 * document (such as FOOTER.gif) might get multiviewed as the result
1044 * instead of a text document, meaning nothing will be displayed, but
1047 static void emit_tail(request_rec *r, char *readme_fname, int suppress_amble)
1049 apr_file_t *f = NULL;
1050 request_rec *rr = NULL;
1051 int suppress_post = 0;
1052 int suppress_sig = 0;
1055 * If there's a readme file, send a subrequest to look for it. If it's
1056 * found and a text file, handle it -- otherwise fall through and
1057 * pretend there's nothing there.
1059 if ((readme_fname != NULL)
1060 && (rr = ap_sub_req_lookup_uri(readme_fname, r, NULL))
1061 && (rr->status == HTTP_OK)
1062 && (rr->filename != NULL)
1063 && rr->finfo.filetype == APR_REG) {
1065 * Check for the two specific cases we allow: text/html and
1066 * text/anything-else. The former is allowed to be processed for
1069 if (rr->content_type != NULL) {
1070 if (!strcasecmp(ap_field_noparam(r->pool, rr->content_type),
1072 if (ap_run_sub_req(rr) == OK) {
1075 suppress_post = suppress_amble;
1078 else if (!strncasecmp("text/", rr->content_type, 5)) {
1080 * If we can open the file, suppress the signature.
1082 if (apr_open(&f, rr->filename, APR_READ,
1083 APR_OS_DEFAULT, r->pool) == APR_SUCCESS) {
1084 do_emit_plain(r, f);
1092 if (!suppress_sig) {
1093 ap_rputs(ap_psignature("", r), r);
1095 if (!suppress_post) {
1096 ap_rputs("</BODY></HTML>\n", r);
1099 ap_destroy_sub_req(rr);
1104 static char *find_title(request_rec *r)
1106 char titlebuf[MAX_STRING_LEN], *find = "<TITLE>";
1107 apr_file_t *thefile = NULL;
1111 if (r->status != HTTP_OK) {
1114 if ((r->content_type != NULL)
1115 && (!strcasecmp(ap_field_noparam(r->pool, r->content_type),
1117 || !strcmp(r->content_type, INCLUDES_MAGIC_TYPE))
1118 && !r->content_encoding) {
1119 if (apr_open(&thefile, r->filename, APR_READ,
1120 APR_OS_DEFAULT, r->pool) != APR_SUCCESS) {
1123 n = sizeof(char) * (MAX_STRING_LEN - 1);
1124 apr_read(thefile, titlebuf, &n);
1130 for (x = 0, p = 0; titlebuf[x]; x++) {
1131 if (apr_toupper(titlebuf[x]) == find[p]) {
1133 if ((p = ap_ind(&titlebuf[++x], '<')) != -1) {
1134 titlebuf[x + p] = '\0';
1136 /* Scan for line breaks for Tanmoy's secretary */
1137 for (y = x; titlebuf[y]; y++) {
1138 if ((titlebuf[y] == CR) || (titlebuf[y] == LF)) {
1148 return apr_pstrdup(r->pool, &titlebuf[x]);
1160 static struct ent *make_autoindex_entry(const char *name, int autoindex_opts,
1161 autoindex_config_rec *d,
1162 request_rec *r, char keyid,
1167 if ((name[0] == '.') && (!name[1])) {
1171 if (ignore_entry(d, ap_make_full_path(r->pool, r->filename, name))) {
1175 p = (struct ent *) apr_pcalloc(r->pool, sizeof(struct ent));
1176 p->name = apr_pstrdup(r->pool, name);
1182 p->key = apr_toupper(keyid);
1183 p->ascending = (apr_toupper(direction) == D_ASCENDING);
1184 p->version_sort = autoindex_opts & VERSION_SORT;
1186 if (autoindex_opts & FANCY_INDEXING) {
1187 request_rec *rr = ap_sub_req_lookup_file(name, r, NULL);
1189 if (rr->finfo.filetype != 0) {
1190 p->lm = rr->finfo.mtime;
1191 if (rr->finfo.filetype == APR_DIR) {
1192 if (!(p->icon = find_icon(d, rr, 1))) {
1193 p->icon = find_default_icon(d, "^^DIRECTORY^^");
1195 if (!(p->alt = find_alt(d, rr, 1))) {
1199 p->name = apr_pstrcat(r->pool, name, "/", NULL);
1202 p->icon = find_icon(d, rr, 0);
1203 p->alt = find_alt(d, rr, 0);
1204 p->size = rr->finfo.size;
1208 p->desc = find_desc(d, rr);
1210 if ((!p->desc) && (autoindex_opts & SCAN_HTML_TITLES)) {
1211 p->desc = apr_pstrdup(r->pool, find_title(rr));
1214 ap_destroy_sub_req(rr);
1217 * We don't need to take any special action for the file size key. If
1218 * we did, it would go here.
1220 if (keyid == K_LAST_MOD) {
1228 static char *terminate_description(autoindex_config_rec *d, char *desc,
1234 if (autoindex_opts & SUPPRESS_LAST_MOD) {
1237 if (autoindex_opts & SUPPRESS_SIZE) {
1241 for (x = 0; desc[x] && (maxsize > 0 || desc[x]=='<'); x++) {
1242 if (desc[x] == '<') {
1243 while (desc[x] != '>') {
1251 else if (desc[x] == '&') {
1252 /* entities like ä count as one character */
1254 for ( ; desc[x] != ';'; ++x) {
1255 if (desc[x] == '\0') {
1265 if (!maxsize && desc[x] != '\0') {
1266 desc[x - 1] = '>'; /* Grump. */
1267 desc[x] = '\0'; /* Double Grump! */
1273 * Emit the anchor for the specified field. If a field is the key for the
1274 * current request, the link changes its meaning to reverse the order when
1275 * selected again. Non-active fields always start in ascending order.
1277 static void emit_link(request_rec *r, char *anchor, char fname, char curkey,
1278 char curdirection, int nosort)
1288 reverse = ((curkey == fname) && (curdirection == D_ASCENDING));
1289 qvalue[3] = reverse ? D_DESCENDING : D_ASCENDING;
1290 ap_rvputs(r, "<A HREF=\"", qvalue, "\">", anchor, "</A>", NULL);
1293 ap_rputs(anchor, r);
1297 static void output_directories(struct ent **ar, int n,
1298 autoindex_config_rec *d, request_rec *r,
1299 int autoindex_opts, char keyid, char direction)
1303 char *name = r->uri;
1305 int static_columns = (autoindex_opts & SUPPRESS_COLSORT);
1306 apr_pool_t *scratch;
1311 apr_create_pool(&scratch, r->pool);
1312 if (name[0] == '\0') {
1316 name_width = d->name_width;
1317 if (d->name_adjust == K_ADJUST) {
1318 for (x = 0; x < n; x++) {
1319 int t = strlen(ar[x]->name);
1320 if (t > name_width) {
1325 name_scratch = apr_palloc(r->pool, name_width + 1);
1326 pad_scratch = apr_palloc(r->pool, name_width + 1);
1327 memset(pad_scratch, ' ', name_width);
1328 pad_scratch[name_width] = '\0';
1330 if (autoindex_opts & FANCY_INDEXING) {
1331 ap_rputs("<PRE>", r);
1332 if ((tp = find_default_icon(d, "^^BLANKICON^^"))) {
1333 ap_rvputs(r, "<IMG SRC=\"", ap_escape_html(scratch, tp),
1334 "\" ALT=\" \"", NULL);
1335 if (d->icon_width && d->icon_height) {
1339 " HEIGHT=\"%d\" WIDTH=\"%d\"",
1346 emit_link(r, "Name", K_NAME, keyid, direction, static_columns);
1347 ap_rputs(pad_scratch + 4, r);
1349 * Emit the guaranteed-at-least-one-space-between-columns byte.
1352 if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1353 emit_link(r, "Last modified", K_LAST_MOD, keyid, direction,
1357 if (!(autoindex_opts & SUPPRESS_SIZE)) {
1358 emit_link(r, "Size", K_SIZE, keyid, direction, static_columns);
1361 if (!(autoindex_opts & SUPPRESS_DESC)) {
1362 emit_link(r, "Description", K_DESC, keyid, direction,
1365 ap_rputs("\n<HR>\n", r);
1368 ap_rputs("<UL>", r);
1371 for (x = 0; x < n; x++) {
1372 char *anchor, *t, *t2;
1375 apr_clear_pool(scratch);
1377 if (is_parent(ar[x]->name)) {
1378 t = ap_make_full_path(scratch, name, "../");
1383 t2 = "Parent Directory";
1384 anchor = ap_escape_html(scratch, ap_os_escape_path(scratch, t, 0));
1389 anchor = ap_escape_html(scratch, ap_os_escape_path(scratch, t, 0));
1392 if (autoindex_opts & FANCY_INDEXING) {
1393 if (autoindex_opts & ICONS_ARE_LINKS) {
1394 ap_rvputs(r, "<A HREF=\"", anchor, "\">", NULL);
1396 if ((ar[x]->icon) || d->default_icon) {
1397 ap_rvputs(r, "<IMG SRC=\"",
1398 ap_escape_html(scratch,
1399 ar[x]->icon ? ar[x]->icon
1401 "\" ALT=\"[", (ar[x]->alt ? ar[x]->alt : " "),
1403 if (d->icon_width && d->icon_height) {
1404 ap_rprintf(r, " HEIGHT=\"%d\" WIDTH=\"%d\"",
1405 d->icon_height, d->icon_width);
1409 if (autoindex_opts & ICONS_ARE_LINKS) {
1410 ap_rputs("</A>", r);
1413 nwidth = strlen(t2);
1414 if (nwidth > name_width) {
1415 memcpy(name_scratch, t2, name_width - 3);
1416 name_scratch[name_width - 3] = '.';
1417 name_scratch[name_width - 2] = '.';
1418 name_scratch[name_width - 1] = '>';
1419 name_scratch[name_width] = 0;
1421 nwidth = name_width;
1423 ap_rvputs(r, " <A HREF=\"", anchor, "\">",
1424 ap_escape_html(scratch, t2), "</A>", pad_scratch + nwidth,
1427 * The blank before the storm.. er, before the next field.
1430 if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1431 if (ar[x]->lm != -1) {
1432 char time_str[MAX_STRING_LEN];
1433 apr_exploded_time_t ts;
1434 apr_explode_localtime(&ts, ar[x]->lm);
1435 apr_strftime(time_str, &rv, MAX_STRING_LEN,
1436 "%d-%b-%Y %H:%M ", &ts);
1437 ap_rputs(time_str, r);
1440 /*Length="22-Feb-1998 23:42 " (see 4 lines above) */
1444 if (!(autoindex_opts & SUPPRESS_SIZE)) {
1445 ap_send_size(ar[x]->size, r);
1448 if (!(autoindex_opts & SUPPRESS_DESC)) {
1450 ap_rputs(terminate_description(d, ar[x]->desc,
1451 autoindex_opts), r);
1456 ap_rvputs(r, "<LI><A HREF=\"", anchor, "\"> ", t2,
1461 if (autoindex_opts & FANCY_INDEXING) {
1462 ap_rputs("</PRE>", r);
1465 ap_rputs("</UL>", r);
1470 * Compare two file entries according to the sort criteria. The return
1471 * is essentially a signum function value.
1474 static int dsortf(struct ent **e1, struct ent **e2)
1481 * First, see if either of the entries is for the parent directory.
1482 * If so, that *always* sorts lower than anything else.
1484 if (is_parent((*e1)->name)) {
1487 if (is_parent((*e2)->name)) {
1491 * All of our comparisons will be of the c1 entry against the c2 one,
1492 * so assign them appropriately to take care of the ordering.
1494 if ((*e1)->ascending) {
1505 if (c1->lm > c2->lm) {
1508 else if (c1->lm < c2->lm) {
1513 if (c1->size > c2->size) {
1516 else if (c1->size < c2->size) {
1521 if (c1->version_sort)
1522 result = apr_strnatcmp(c1->desc ? c1->desc : "", c2->desc ? c2->desc : "");
1524 result = strcmp(c1->desc ? c1->desc : "", c2->desc ? c2->desc : "");
1530 if (c1->version_sort)
1531 return apr_strnatcmp(c1->name, c2->name);
1533 return strcmp(c1->name, c2->name);
1537 static int index_directory(request_rec *r,
1538 autoindex_config_rec *autoindex_conf)
1540 char *title_name = ap_escape_html(r->pool, r->uri);
1542 char *name = r->filename;
1545 apr_status_t status;
1547 struct ent *head, *p;
1548 struct ent **ar = NULL;
1549 const char *qstring;
1550 int autoindex_opts = autoindex_conf->opts;
1554 if ((status = apr_dir_open(&thedir, name, r->pool)) != APR_SUCCESS) {
1555 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
1556 "Can't open directory for index: %s", r->filename);
1557 return HTTP_FORBIDDEN;
1560 #if APR_HAS_UNICODE_FS
1561 r->content_type = "text/html;charset=utf-8";
1563 r->content_type = "text/html";
1565 ap_update_mtime(r, r->finfo.mtime);
1566 ap_set_last_modified(r);
1569 ap_send_http_header(r);
1571 if (r->header_only) {
1572 apr_dir_close(thedir);
1576 /* Spew HTML preamble */
1578 title_endp = title_name + strlen(title_name) - 1;
1580 while (title_endp > title_name && *title_endp == '/') {
1581 *title_endp-- = '\0';
1584 emit_head(r, find_header(autoindex_conf, r),
1585 autoindex_opts & SUPPRESS_PREAMBLE, title_name);
1588 * Figure out what sort of indexing (if any) we're supposed to use.
1590 * If no QUERY_STRING was specified or column sorting has been
1591 * explicitly disabled, we use the default specified by the
1592 * IndexOrderDefault directive (if there is one); otherwise,
1593 * we fall back to ascending by name.
1596 if ((autoindex_opts & SUPPRESS_COLSORT)
1597 || ((qstring == NULL) || (*qstring == '\0'))) {
1598 qstring = autoindex_conf->default_order;
1601 * If there is no specific ordering defined for this directory,
1602 * default to ascending by filename.
1604 if ((qstring == NULL) || (*qstring == '\0')) {
1606 direction = D_ASCENDING;
1610 ap_getword(r->pool, &qstring, '=');
1611 if (qstring != '\0') {
1612 direction = *qstring;
1615 direction = D_ASCENDING;
1620 * Since we don't know how many dir. entries there are, put them into a
1621 * linked list and then arrayificate them so qsort can use them.
1624 while (apr_dir_read(&dirent, APR_FINFO_DIRENT, thedir) == APR_SUCCESS) {
1625 p = make_autoindex_entry(dirent.name, autoindex_opts,
1626 autoindex_conf, r, keyid, direction);
1634 ar = (struct ent **) apr_palloc(r->pool,
1635 num_ent * sizeof(struct ent *));
1643 qsort((void *) ar, num_ent, sizeof(struct ent *),
1644 (int (*)(const void *, const void *)) dsortf);
1646 output_directories(ar, num_ent, autoindex_conf, r, autoindex_opts, keyid,
1648 apr_dir_close(thedir);
1650 if (autoindex_opts & FANCY_INDEXING) {
1651 ap_rputs("<HR>\n", r);
1653 emit_tail(r, find_readme(autoindex_conf, r),
1654 autoindex_opts & SUPPRESS_PREAMBLE);
1659 /* The formal handler... */
1661 static int handle_autoindex(request_rec *r)
1663 autoindex_config_rec *d;
1666 if(strcmp(r->handler,DIR_MAGIC_TYPE))
1669 allow_opts = ap_allow_options(r);
1671 d = (autoindex_config_rec *) ap_get_module_config(r->per_dir_config,
1674 r->allowed |= (1 << M_GET);
1675 if (r->method_number != M_GET) {
1679 /* OK, nothing easy. Trot out the heavy artillery... */
1681 if (allow_opts & OPT_INDEXES) {
1682 /* KLUDGE --- make the sub_req lookups happen in the right directory.
1683 * Fixing this in the sub_req_lookup functions themselves is difficult,
1684 * and would probably break virtual includes...
1687 if (r->filename[strlen(r->filename) - 1] != '/') {
1688 r->filename = apr_pstrcat(r->pool, r->filename, "/", NULL);
1690 return index_directory(r, d);
1693 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1694 "Directory index forbidden by rule: %s", r->filename);
1695 return HTTP_FORBIDDEN;
1699 static void register_hooks(apr_pool_t *p)
1701 ap_hook_handler(handle_autoindex,NULL,NULL,APR_HOOK_MIDDLE);
1704 module AP_MODULE_DECLARE_DATA autoindex_module =
1706 STANDARD20_MODULE_STUFF,
1707 create_autoindex_config, /* dir config creater */
1708 merge_autoindex_configs, /* dir merger --- default is to override */
1709 NULL, /* server config */
1710 NULL, /* merge server config */
1711 autoindex_cmds, /* command apr_table_t */
1712 register_hooks /* register hooks */