1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * mod_autoindex.c: Handles the on-the-fly html index generation
23 * Adapted to Apache by rst.
25 * Version sort added by Martin Pool <mbp@humbug.org.au>.
28 #include "apr_strings.h"
29 #include "apr_fnmatch.h"
30 #include "apr_strings.h"
33 #define APR_WANT_STRFUNC
36 #include "ap_config.h"
38 #include "http_config.h"
39 #include "http_core.h"
40 #include "http_request.h"
41 #include "http_protocol.h"
43 #include "http_main.h"
44 #include "util_script.h"
48 module AP_MODULE_DECLARE_DATA autoindex_module;
50 /****************************************************************
52 * Handling configuration directives...
55 #define NO_OPTIONS (1 << 0) /* Indexing options */
56 #define ICONS_ARE_LINKS (1 << 1)
57 #define SCAN_HTML_TITLES (1 << 2)
58 #define SUPPRESS_ICON (1 << 3)
59 #define SUPPRESS_LAST_MOD (1 << 4)
60 #define SUPPRESS_SIZE (1 << 5)
61 #define SUPPRESS_DESC (1 << 6)
62 #define SUPPRESS_PREAMBLE (1 << 7)
63 #define SUPPRESS_COLSORT (1 << 8)
64 #define SUPPRESS_RULES (1 << 9)
65 #define FOLDERS_FIRST (1 << 10)
66 #define VERSION_SORT (1 << 11)
67 #define TRACK_MODIFIED (1 << 12)
68 #define FANCY_INDEXING (1 << 13)
69 #define TABLE_INDEXING (1 << 14)
70 #define IGNORE_CLIENT (1 << 15)
71 #define IGNORE_CASE (1 << 16)
72 #define EMIT_XHTML (1 << 17)
73 #define SHOW_FORBIDDEN (1 << 18)
80 * Define keys for sorting.
82 #define K_NAME 'N' /* Sort by file name (default) */
83 #define K_LAST_MOD 'M' /* Last modification date */
84 #define K_SIZE 'S' /* Size (absolute, not as displayed) */
85 #define K_DESC 'D' /* Description */
86 #define K_VALID "NMSD" /* String containing _all_ valid K_ opts */
88 #define D_ASCENDING 'A'
89 #define D_DESCENDING 'D'
90 #define D_VALID "AD" /* String containing _all_ valid D_ opts */
93 * These are the dimensions of the default icons supplied with Apache.
95 #define DEFAULT_ICON_WIDTH 20
96 #define DEFAULT_ICON_HEIGHT 22
99 * Other default dimensions.
101 #define DEFAULT_NAME_WIDTH 23
102 #define DEFAULT_DESC_WIDTH 23
111 typedef struct ai_desc_t {
118 typedef struct autoindex_config_struct {
123 apr_int32_t incremented_opts;
124 apr_int32_t decremented_opts;
132 char default_direction;
134 apr_array_header_t *icon_list;
135 apr_array_header_t *alt_list;
136 apr_array_header_t *desc_list;
137 apr_array_header_t *ign_list;
138 apr_array_header_t *hdr_list;
139 apr_array_header_t *rdme_list;
143 } autoindex_config_rec;
145 static char c_by_encoding, c_by_type, c_by_path;
147 #define BY_ENCODING &c_by_encoding
148 #define BY_TYPE &c_by_type
149 #define BY_PATH &c_by_path
152 * This routine puts the standard HTML header at the top of the index page.
153 * We include the DOCTYPE because we may be using features therefrom (i.e.,
154 * HEIGHT and WIDTH attributes on the icons if we're FancyIndexing).
156 static void emit_preamble(request_rec *r, int xhtml, const char *title)
158 autoindex_config_rec *d;
160 d = (autoindex_config_rec *) ap_get_module_config(r->per_dir_config,
164 ap_rvputs(r, DOCTYPE_XHTML_1_0T,
165 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
166 "<head>\n <title>Index of ", title,
169 ap_rvputs(r, DOCTYPE_HTML_3_2,
171 "<title>Index of ", title,
175 if (d->style_sheet != NULL) {
176 ap_rvputs(r, " <link rel=\"stylesheet\" href=\"", d->style_sheet,
177 "\" type=\"text/css\"", xhtml ? " />\n" : ">\n", NULL);
179 ap_rvputs(r, " </head>\n <body>\n", NULL);
182 static void push_item(apr_array_header_t *arr, char *type, const char *to,
183 const char *path, const char *data)
185 struct item *p = (struct item *) apr_array_push(arr);
195 p->data = data ? apr_pstrdup(arr->pool, data) : NULL;
196 p->apply_path = apr_pstrcat(arr->pool, path, "*", NULL);
198 if ((type == BY_PATH) && (!ap_is_matchexp(to))) {
199 p->apply_to = apr_pstrcat(arr->pool, "*", to, NULL);
202 p->apply_to = apr_pstrdup(arr->pool, to);
209 static const char *add_alt(cmd_parms *cmd, void *d, const char *alt,
212 if (cmd->info == BY_PATH) {
213 if (!strcmp(to, "**DIRECTORY**")) {
214 to = "^^DIRECTORY^^";
217 if (cmd->info == BY_ENCODING) {
218 char *tmp = apr_pstrdup(cmd->pool, to);
223 push_item(((autoindex_config_rec *) d)->alt_list, cmd->info, to,
228 static const char *add_icon(cmd_parms *cmd, void *d, const char *icon,
231 char *iconbak = apr_pstrdup(cmd->pool, icon);
233 if (icon[0] == '(') {
235 char *cl = strchr(iconbak, ')');
238 return "missing closing paren";
240 alt = ap_getword_nc(cmd->pool, &iconbak, ',');
241 *cl = '\0'; /* Lose closing paren */
242 add_alt(cmd, d, &alt[1], to);
244 if (cmd->info == BY_PATH) {
245 if (!strcmp(to, "**DIRECTORY**")) {
246 to = "^^DIRECTORY^^";
249 if (cmd->info == BY_ENCODING) {
250 char *tmp = apr_pstrdup(cmd->pool, to);
255 push_item(((autoindex_config_rec *) d)->icon_list, cmd->info, to,
261 * Add description text for a filename pattern. If the pattern has
262 * wildcards already (or we need to add them), add leading and
263 * trailing wildcards to it to ensure substring processing. If the
264 * pattern contains a '/' anywhere, force wildcard matching mode,
265 * add a slash to the prefix so that "bar/bletch" won't be matched
266 * by "foobar/bletch", and make a note that there's a delimiter;
267 * the matching routine simplifies to just the actual filename
268 * whenever it can. This allows definitions in parent directories
269 * to be made for files in subordinate ones using relative paths.
273 * Absent a strcasestr() function, we have to force wildcards on
274 * systems for which "AAA" and "aaa" mean the same file.
276 #ifdef CASE_BLIND_FILESYSTEM
277 #define WILDCARDS_REQUIRED 1
279 #define WILDCARDS_REQUIRED 0
282 static const char *add_desc(cmd_parms *cmd, void *d, const char *desc,
285 autoindex_config_rec *dcfg = (autoindex_config_rec *) d;
286 ai_desc_t *desc_entry;
289 desc_entry = (ai_desc_t *) apr_array_push(dcfg->desc_list);
290 desc_entry->full_path = (ap_strchr_c(to, '/') == NULL) ? 0 : 1;
291 desc_entry->wildcards = (WILDCARDS_REQUIRED
292 || desc_entry->full_path
293 || apr_fnmatch_test(to));
294 if (desc_entry->wildcards) {
295 prefix = desc_entry->full_path ? "*/" : "*";
296 desc_entry->pattern = apr_pstrcat(dcfg->desc_list->pool,
297 prefix, to, "*", NULL);
300 desc_entry->pattern = apr_pstrdup(dcfg->desc_list->pool, to);
302 desc_entry->description = apr_pstrdup(dcfg->desc_list->pool, desc);
306 static const char *add_ignore(cmd_parms *cmd, void *d, const char *ext)
308 push_item(((autoindex_config_rec *) d)->ign_list, 0, ext, cmd->path, NULL);
312 static const char *add_header(cmd_parms *cmd, void *d, const char *name)
314 push_item(((autoindex_config_rec *) d)->hdr_list, 0, NULL, cmd->path,
319 static const char *add_readme(cmd_parms *cmd, void *d, const char *name)
321 push_item(((autoindex_config_rec *) d)->rdme_list, 0, NULL, cmd->path,
326 static const char *add_opts(cmd_parms *cmd, void *d, int argc, char *const argv[])
331 apr_int32_t opts_add;
332 apr_int32_t opts_remove;
334 autoindex_config_rec *d_cfg = (autoindex_config_rec *) d;
337 opts_add = d_cfg->incremented_opts;
338 opts_remove = d_cfg->decremented_opts;
340 for (i = 0; i < argc; i++) {
344 if ((*w == '+') || (*w == '-')) {
350 if (!strcasecmp(w, "FancyIndexing")) {
351 option = FANCY_INDEXING;
353 else if (!strcasecmp(w, "FoldersFirst")) {
354 option = FOLDERS_FIRST;
356 else if (!strcasecmp(w, "HTMLTable")) {
357 option = TABLE_INDEXING;
359 else if (!strcasecmp(w, "IconsAreLinks")) {
360 option = ICONS_ARE_LINKS;
362 else if (!strcasecmp(w, "IgnoreCase")) {
363 option = IGNORE_CASE;
365 else if (!strcasecmp(w, "IgnoreClient")) {
366 option = IGNORE_CLIENT;
368 else if (!strcasecmp(w, "ScanHTMLTitles")) {
369 option = SCAN_HTML_TITLES;
371 else if (!strcasecmp(w, "SuppressColumnSorting")) {
372 option = SUPPRESS_COLSORT;
374 else if (!strcasecmp(w, "SuppressDescription")) {
375 option = SUPPRESS_DESC;
377 else if (!strcasecmp(w, "SuppressHTMLPreamble")) {
378 option = SUPPRESS_PREAMBLE;
380 else if (!strcasecmp(w, "SuppressIcon")) {
381 option = SUPPRESS_ICON;
383 else if (!strcasecmp(w, "SuppressLastModified")) {
384 option = SUPPRESS_LAST_MOD;
386 else if (!strcasecmp(w, "SuppressSize")) {
387 option = SUPPRESS_SIZE;
389 else if (!strcasecmp(w, "SuppressRules")) {
390 option = SUPPRESS_RULES;
392 else if (!strcasecmp(w, "TrackModified")) {
393 option = TRACK_MODIFIED;
395 else if (!strcasecmp(w, "VersionSort")) {
396 option = VERSION_SORT;
398 else if (!strcasecmp(w, "XHTML")) {
401 else if (!strcasecmp(w, "ShowForbidden")) {
402 option = SHOW_FORBIDDEN;
404 else if (!strcasecmp(w, "None")) {
405 if (action != '\0') {
406 return "Cannot combine '+' or '-' with 'None' keyword";
412 else if (!strcasecmp(w, "IconWidth")) {
414 d_cfg->icon_width = DEFAULT_ICON_WIDTH;
417 d_cfg->icon_width = 0;
420 else if (!strncasecmp(w, "IconWidth=", 10)) {
422 return "Cannot combine '-' with IconWidth=n";
424 d_cfg->icon_width = atoi(&w[10]);
426 else if (!strcasecmp(w, "IconHeight")) {
428 d_cfg->icon_height = DEFAULT_ICON_HEIGHT;
431 d_cfg->icon_height = 0;
434 else if (!strncasecmp(w, "IconHeight=", 11)) {
436 return "Cannot combine '-' with IconHeight=n";
438 d_cfg->icon_height = atoi(&w[11]);
440 else if (!strcasecmp(w, "NameWidth")) {
442 return "NameWidth with no value may only appear as "
445 d_cfg->name_width = DEFAULT_NAME_WIDTH;
446 d_cfg->name_adjust = K_NOADJUST;
448 else if (!strncasecmp(w, "NameWidth=", 10)) {
450 return "Cannot combine '-' with NameWidth=n";
453 d_cfg->name_adjust = K_ADJUST;
456 int width = atoi(&w[10]);
458 if (width && (width < 5)) {
459 return "NameWidth value must be greater than 5";
461 d_cfg->name_width = width;
462 d_cfg->name_adjust = K_NOADJUST;
465 else if (!strcasecmp(w, "DescriptionWidth")) {
467 return "DescriptionWidth with no value may only appear as "
468 "'-DescriptionWidth'";
470 d_cfg->desc_width = DEFAULT_DESC_WIDTH;
471 d_cfg->desc_adjust = K_NOADJUST;
473 else if (!strncasecmp(w, "DescriptionWidth=", 17)) {
475 return "Cannot combine '-' with DescriptionWidth=n";
478 d_cfg->desc_adjust = K_ADJUST;
481 int width = atoi(&w[17]);
483 if (width && (width < 12)) {
484 return "DescriptionWidth value must be greater than 12";
486 d_cfg->desc_width = width;
487 d_cfg->desc_adjust = K_NOADJUST;
490 else if (!strncasecmp(w, "Type=", 5)) {
491 d_cfg->ctype = apr_pstrdup(cmd->pool, &w[5]);
493 else if (!strncasecmp(w, "Charset=", 8)) {
494 d_cfg->charset = apr_pstrdup(cmd->pool, &w[8]);
497 return "Invalid directory indexing option";
499 if (action == '\0') {
504 else if (action == '+') {
506 opts_remove &= ~option;
509 opts_remove |= option;
513 if ((opts & NO_OPTIONS) && (opts & ~NO_OPTIONS)) {
514 return "Cannot combine other IndexOptions keywords with 'None'";
516 d_cfg->incremented_opts = opts_add;
517 d_cfg->decremented_opts = opts_remove;
522 static const char *set_default_order(cmd_parms *cmd, void *m,
523 const char *direction, const char *key)
525 autoindex_config_rec *d_cfg = (autoindex_config_rec *) m;
527 if (!strcasecmp(direction, "Ascending")) {
528 d_cfg->default_direction = D_ASCENDING;
530 else if (!strcasecmp(direction, "Descending")) {
531 d_cfg->default_direction = D_DESCENDING;
534 return "First keyword must be 'Ascending' or 'Descending'";
537 if (!strcasecmp(key, "Name")) {
538 d_cfg->default_keyid = K_NAME;
540 else if (!strcasecmp(key, "Date")) {
541 d_cfg->default_keyid = K_LAST_MOD;
543 else if (!strcasecmp(key, "Size")) {
544 d_cfg->default_keyid = K_SIZE;
546 else if (!strcasecmp(key, "Description")) {
547 d_cfg->default_keyid = K_DESC;
550 return "Second keyword must be 'Name', 'Date', 'Size', or "
557 #define DIR_CMD_PERMS OR_INDEXES
559 static const command_rec autoindex_cmds[] =
561 AP_INIT_ITERATE2("AddIcon", add_icon, BY_PATH, DIR_CMD_PERMS,
562 "an icon URL followed by one or more filenames"),
563 AP_INIT_ITERATE2("AddIconByType", add_icon, BY_TYPE, DIR_CMD_PERMS,
564 "an icon URL followed by one or more MIME types"),
565 AP_INIT_ITERATE2("AddIconByEncoding", add_icon, BY_ENCODING, DIR_CMD_PERMS,
566 "an icon URL followed by one or more content encodings"),
567 AP_INIT_ITERATE2("AddAlt", add_alt, BY_PATH, DIR_CMD_PERMS,
568 "alternate descriptive text followed by one or more "
570 AP_INIT_ITERATE2("AddAltByType", add_alt, BY_TYPE, DIR_CMD_PERMS,
571 "alternate descriptive text followed by one or more MIME "
573 AP_INIT_ITERATE2("AddAltByEncoding", add_alt, BY_ENCODING, DIR_CMD_PERMS,
574 "alternate descriptive text followed by one or more "
575 "content encodings"),
576 AP_INIT_TAKE_ARGV("IndexOptions", add_opts, NULL, DIR_CMD_PERMS,
577 "one or more index options [+|-][]"),
578 AP_INIT_TAKE2("IndexOrderDefault", set_default_order, NULL, DIR_CMD_PERMS,
579 "{Ascending,Descending} {Name,Size,Description,Date}"),
580 AP_INIT_ITERATE("IndexIgnore", add_ignore, NULL, DIR_CMD_PERMS,
581 "one or more file extensions"),
582 AP_INIT_ITERATE2("AddDescription", add_desc, BY_PATH, DIR_CMD_PERMS,
583 "Descriptive text followed by one or more filenames"),
584 AP_INIT_TAKE1("HeaderName", add_header, NULL, DIR_CMD_PERMS,
586 AP_INIT_TAKE1("ReadmeName", add_readme, NULL, DIR_CMD_PERMS,
588 AP_INIT_RAW_ARGS("FancyIndexing", ap_set_deprecated, NULL, OR_ALL,
589 "The FancyIndexing directive is no longer supported. "
590 "Use IndexOptions FancyIndexing."),
591 AP_INIT_TAKE1("DefaultIcon", ap_set_string_slot,
592 (void *)APR_OFFSETOF(autoindex_config_rec, default_icon),
593 DIR_CMD_PERMS, "an icon URL"),
594 AP_INIT_TAKE1("IndexStyleSheet", ap_set_string_slot,
595 (void *)APR_OFFSETOF(autoindex_config_rec, style_sheet),
596 DIR_CMD_PERMS, "URL to style sheet"),
600 static void *create_autoindex_config(apr_pool_t *p, char *dummy)
602 autoindex_config_rec *new =
603 (autoindex_config_rec *) apr_pcalloc(p, sizeof(autoindex_config_rec));
606 new->icon_height = 0;
607 new->name_width = DEFAULT_NAME_WIDTH;
608 new->name_adjust = K_UNSET;
609 new->desc_width = DEFAULT_DESC_WIDTH;
610 new->desc_adjust = K_UNSET;
611 new->icon_list = apr_array_make(p, 4, sizeof(struct item));
612 new->alt_list = apr_array_make(p, 4, sizeof(struct item));
613 new->desc_list = apr_array_make(p, 4, sizeof(ai_desc_t));
614 new->ign_list = apr_array_make(p, 4, sizeof(struct item));
615 new->hdr_list = apr_array_make(p, 4, sizeof(struct item));
616 new->rdme_list = apr_array_make(p, 4, sizeof(struct item));
618 new->incremented_opts = 0;
619 new->decremented_opts = 0;
620 new->default_keyid = '\0';
621 new->default_direction = '\0';
626 static void *merge_autoindex_configs(apr_pool_t *p, void *basev, void *addv)
628 autoindex_config_rec *new;
629 autoindex_config_rec *base = (autoindex_config_rec *) basev;
630 autoindex_config_rec *add = (autoindex_config_rec *) addv;
632 new = (autoindex_config_rec *) apr_pcalloc(p, sizeof(autoindex_config_rec));
633 new->default_icon = add->default_icon ? add->default_icon
634 : base->default_icon;
635 new->style_sheet = add->style_sheet ? add->style_sheet
637 new->icon_height = add->icon_height ? add->icon_height : base->icon_height;
638 new->icon_width = add->icon_width ? add->icon_width : base->icon_width;
640 new->ctype = add->ctype ? add->ctype : base->ctype;
641 new->charset = add->charset ? add->charset : base->charset;
643 new->alt_list = apr_array_append(p, add->alt_list, base->alt_list);
644 new->ign_list = apr_array_append(p, add->ign_list, base->ign_list);
645 new->hdr_list = apr_array_append(p, add->hdr_list, base->hdr_list);
646 new->desc_list = apr_array_append(p, add->desc_list, base->desc_list);
647 new->icon_list = apr_array_append(p, add->icon_list, base->icon_list);
648 new->rdme_list = apr_array_append(p, add->rdme_list, base->rdme_list);
649 if (add->opts & NO_OPTIONS) {
651 * If the current directory says 'no options' then we also
652 * clear any incremental mods from being inheritable further down.
654 new->opts = NO_OPTIONS;
655 new->incremented_opts = 0;
656 new->decremented_opts = 0;
660 * If there were any nonincremental options selected for
661 * this directory, they dominate and we don't inherit *anything.*
662 * Contrariwise, we *do* inherit if the only settings here are
665 if (add->opts == 0) {
666 new->incremented_opts = (base->incremented_opts
667 | add->incremented_opts)
668 & ~add->decremented_opts;
669 new->decremented_opts = (base->decremented_opts
670 | add->decremented_opts);
672 * We may have incremental settings, so make sure we don't
673 * inadvertently inherit an IndexOptions None from above.
675 new->opts = (base->opts & ~NO_OPTIONS);
679 * There are local nonincremental settings, which clear
680 * all inheritance from above. They *are* the new base settings.
682 new->opts = add->opts;;
685 * We're guaranteed that there'll be no overlap between
686 * the add-options and the remove-options.
688 new->opts |= new->incremented_opts;
689 new->opts &= ~new->decremented_opts;
692 * Inherit the NameWidth settings if there aren't any specific to
693 * the new location; otherwise we'll end up using the defaults set in the
694 * config-rec creation routine.
696 if (add->name_adjust == K_UNSET) {
697 new->name_width = base->name_width;
698 new->name_adjust = base->name_adjust;
701 new->name_width = add->name_width;
702 new->name_adjust = add->name_adjust;
706 * Likewise for DescriptionWidth.
708 if (add->desc_adjust == K_UNSET) {
709 new->desc_width = base->desc_width;
710 new->desc_adjust = base->desc_adjust;
713 new->desc_width = add->desc_width;
714 new->desc_adjust = add->desc_adjust;
717 new->default_keyid = add->default_keyid ? add->default_keyid
718 : base->default_keyid;
719 new->default_direction = add->default_direction ? add->default_direction
720 : base->default_direction;
724 /****************************************************************
726 * Looking things up in config entries...
729 /* Structure used to hold entries when we're actually building an index */
739 int ascending, ignore_case, version_sort;
744 static char *find_item(request_rec *r, apr_array_header_t *list, int path_only)
746 const char *content_type = ap_field_noparam(r->pool, r->content_type);
747 const char *content_encoding = r->content_encoding;
748 char *path = r->filename;
750 struct item *items = (struct item *) list->elts;
753 for (i = 0; i < list->nelts; ++i) {
754 struct item *p = &items[i];
756 /* Special cased for ^^DIRECTORY^^ and ^^BLANKICON^^ */
757 if ((path[0] == '^') || (!ap_strcmp_match(path, p->apply_path))) {
758 if (!*(p->apply_to)) {
761 else if (p->type == BY_PATH || path[0] == '^') {
762 if (!ap_strcmp_match(path, p->apply_to)) {
766 else if (!path_only) {
767 if (!content_encoding) {
768 if (p->type == BY_TYPE) {
770 && !ap_strcasecmp_match(content_type,
777 if (p->type == BY_ENCODING) {
778 if (!ap_strcasecmp_match(content_encoding,
790 #define find_icon(d,p,t) find_item(p,d->icon_list,t)
791 #define find_alt(d,p,t) find_item(p,d->alt_list,t)
792 #define find_header(d,p) find_item(p,d->hdr_list,0)
793 #define find_readme(d,p) find_item(p,d->rdme_list,0)
795 static char *find_default_item(char *bogus_name, apr_array_header_t *list)
798 /* Bleah. I tried to clean up find_item, and it lead to this bit
799 * of ugliness. Note that the fields initialized are precisely
800 * those that find_item looks at...
802 r.filename = bogus_name;
803 r.content_type = r.content_encoding = NULL;
804 return find_item(&r, list, 1);
807 #define find_default_icon(d,n) find_default_item(n, d->icon_list)
808 #define find_default_alt(d,n) find_default_item(n, d->alt_list)
811 * Look through the list of pattern/description pairs and return the first one
812 * if any) that matches the filename in the request. If multiple patterns
813 * match, only the first one is used; since the order in the array is the
814 * same as the order in which directives were processed, earlier matching
815 * directives will dominate.
818 #ifdef CASE_BLIND_FILESYSTEM
819 #define MATCH_FLAGS APR_FNM_CASE_BLIND
821 #define MATCH_FLAGS 0
824 static char *find_desc(autoindex_config_rec *dcfg, const char *filename_full)
827 ai_desc_t *list = (ai_desc_t *) dcfg->desc_list->elts;
828 const char *filename_only;
829 const char *filename;
832 * If the filename includes a path, extract just the name itself
833 * for the simple matches.
835 if ((filename_only = ap_strrchr_c(filename_full, '/')) == NULL) {
836 filename_only = filename_full;
841 for (i = 0; i < dcfg->desc_list->nelts; ++i) {
842 ai_desc_t *tuple = &list[i];
846 * Only use the full-path filename if the pattern contains '/'s.
848 filename = (tuple->full_path) ? filename_full : filename_only;
850 * Make the comparison using the cheapest method; only do
851 * wildcard checking if we must.
853 if (tuple->wildcards) {
854 found = (apr_fnmatch(tuple->pattern, filename, MATCH_FLAGS) == 0);
857 found = (ap_strstr_c(filename, tuple->pattern) != NULL);
860 return tuple->description;
866 static int ignore_entry(autoindex_config_rec *d, char *path)
868 apr_array_header_t *list = d->ign_list;
869 struct item *items = (struct item *) list->elts;
873 if ((tt = strrchr(path, '/')) == NULL) {
880 for (i = 0; i < list->nelts; ++i) {
881 struct item *p = &items[i];
884 if ((ap = strrchr(p->apply_to, '/')) == NULL) {
891 #ifndef CASE_BLIND_FILESYSTEM
892 if (!ap_strcmp_match(path, p->apply_path)
893 && !ap_strcmp_match(tt, ap)) {
896 #else /* !CASE_BLIND_FILESYSTEM */
898 * On some platforms, the match must be case-blind. This is really
899 * a factor of the filesystem involved, but we can't detect that
900 * reliably - so we have to granularise at the OS level.
902 if (!ap_strcasecmp_match(path, p->apply_path)
903 && !ap_strcasecmp_match(tt, ap)) {
906 #endif /* !CASE_BLIND_FILESYSTEM */
911 /*****************************************************************
913 * Actually generating output
917 * Elements of the emitted document:
919 * Emitted unless SUPPRESS_PREAMBLE is set AND ap_run_sub_req
920 * succeeds for the (content_type == text/html) header file.
922 * Emitted if found (and able).
924 * Emitted if a header file is NOT emitted.
928 * Emitted if FANCY_INDEXING is set.
930 * Emitted if found (and able).
932 * Emitted if ServerSignature is not Off AND a readme file
935 * Emitted unless SUPPRESS_PREAMBLE is set AND ap_run_sub_req
936 * succeeds for the (content_type == text/html) readme file.
941 * emit a plain text file
943 static void do_emit_plain(request_rec *r, apr_file_t *f)
945 char buf[AP_IOBUFSIZE + 1];
950 ap_rputs("<pre>\n", r);
951 while (!apr_file_eof(f)) {
953 n = sizeof(char) * AP_IOBUFSIZE;
954 rv = apr_file_read(f, buf, &n);
955 } while (APR_STATUS_IS_EINTR(rv));
956 if (n == 0 || rv != APR_SUCCESS) {
957 /* ###: better error here? */
963 for (i = c; i < n; i++) {
964 if (buf[i] == '<' || buf[i] == '>' || buf[i] == '&') {
970 ap_rputs(&buf[c], r);
974 else if (ch == '>') {
977 else if (ch == '&') {
978 ap_rputs("&", r);
983 ap_rputs("</pre>\n", r);
987 * Handle the preamble through the H1 tag line, inclusive. Locate
988 * the file with a subrequests. Process text/html documents by actually
989 * running the subrequest; text/xxx documents get copied verbatim,
990 * and any other content type is ignored. This means that a non-text
991 * document (such as HEADER.gif) might get multiviewed as the result
992 * instead of a text document, meaning nothing will be displayed, but
995 static void emit_head(request_rec *r, char *header_fname, int suppress_amble,
996 int emit_xhtml, char *title)
998 apr_table_t *hdrs = r->headers_in;
999 apr_file_t *f = NULL;
1000 request_rec *rr = NULL;
1003 const char *r_accept;
1004 const char *r_accept_enc;
1007 * If there's a header file, send a subrequest to look for it. If it's
1008 * found and html do the subrequest, otherwise handle it
1010 r_accept = apr_table_get(hdrs, "Accept");
1011 r_accept_enc = apr_table_get(hdrs, "Accept-Encoding");
1012 apr_table_setn(hdrs, "Accept", "text/html, text/plain");
1013 apr_table_unset(hdrs, "Accept-Encoding");
1016 if ((header_fname != NULL) && r->args) {
1017 header_fname = apr_pstrcat(r->pool, header_fname, "?", r->args, NULL);
1020 if ((header_fname != NULL)
1021 && (rr = ap_sub_req_lookup_uri(header_fname, r, r->output_filters))
1022 && (rr->status == HTTP_OK)
1023 && (rr->filename != NULL)
1024 && (rr->finfo.filetype == APR_REG)) {
1026 * Check for the two specific cases we allow: text/html and
1027 * text/anything-else. The former is allowed to be processed for
1030 if (rr->content_type != NULL) {
1031 if (!strcasecmp(ap_field_noparam(r->pool, rr->content_type),
1034 /* Hope everything will work... */
1038 if (! suppress_amble) {
1039 emit_preamble(r, emit_xhtml, title);
1041 /* This is a hack, but I can't find any better way to do this.
1042 * The problem is that we have already created the sub-request,
1043 * but we just inserted the OLD_WRITE filter, and the
1044 * sub-request needs to pass its data through the OLD_WRITE
1045 * filter, or things go horribly wrong (missing data, data in
1046 * the wrong order, etc). To fix it, if you create a
1047 * sub-request and then insert the OLD_WRITE filter before you
1048 * run the request, you need to make sure that the sub-request
1049 * data goes through the OLD_WRITE filter. Just steal this
1050 * code. The long-term solution is to remove the ap_r*
1053 for (f=rr->output_filters;
1054 f->frec != ap_subreq_core_filter_handle; f = f->next);
1055 f->next = r->output_filters;
1058 * If there's a problem running the subrequest, display the
1059 * preamble if we didn't do it before -- the header file
1060 * didn't get displayed.
1062 if (ap_run_sub_req(rr) != OK) {
1063 /* It didn't work */
1064 emit_amble = suppress_amble;
1068 else if (!strncasecmp("text/", rr->content_type, 5)) {
1070 * If we can open the file, prefix it with the preamble
1071 * regardless; since we'll be sending a <pre> block around
1072 * the file's contents, any HTML header it had won't end up
1075 if (apr_file_open(&f, rr->filename, APR_READ,
1076 APR_OS_DEFAULT, r->pool) == APR_SUCCESS) {
1077 emit_preamble(r, emit_xhtml, title);
1079 do_emit_plain(r, f);
1088 apr_table_setn(hdrs, "Accept", r_accept);
1091 apr_table_unset(hdrs, "Accept");
1095 apr_table_setn(hdrs, "Accept-Encoding", r_accept_enc);
1099 emit_preamble(r, emit_xhtml, title);
1102 ap_rvputs(r, "<h1>Index of ", title, "</h1>\n", NULL);
1105 ap_destroy_sub_req(rr);
1111 * Handle the Readme file through the postamble, inclusive. Locate
1112 * the file with a subrequests. Process text/html documents by actually
1113 * running the subrequest; text/xxx documents get copied verbatim,
1114 * and any other content type is ignored. This means that a non-text
1115 * document (such as FOOTER.gif) might get multiviewed as the result
1116 * instead of a text document, meaning nothing will be displayed, but
1119 static void emit_tail(request_rec *r, char *readme_fname, int suppress_amble)
1121 apr_file_t *f = NULL;
1122 request_rec *rr = NULL;
1123 int suppress_post = 0;
1124 int suppress_sig = 0;
1127 * If there's a readme file, send a subrequest to look for it. If it's
1128 * found and a text file, handle it -- otherwise fall through and
1129 * pretend there's nothing there.
1131 if ((readme_fname != NULL)
1132 && (rr = ap_sub_req_lookup_uri(readme_fname, r, r->output_filters))
1133 && (rr->status == HTTP_OK)
1134 && (rr->filename != NULL)
1135 && rr->finfo.filetype == APR_REG) {
1137 * Check for the two specific cases we allow: text/html and
1138 * text/anything-else. The former is allowed to be processed for
1141 if (rr->content_type != NULL) {
1142 if (!strcasecmp(ap_field_noparam(r->pool, rr->content_type),
1145 for (f=rr->output_filters;
1146 f->frec != ap_subreq_core_filter_handle; f = f->next);
1147 f->next = r->output_filters;
1150 if (ap_run_sub_req(rr) == OK) {
1153 suppress_post = suppress_amble;
1156 else if (!strncasecmp("text/", rr->content_type, 5)) {
1158 * If we can open the file, suppress the signature.
1160 if (apr_file_open(&f, rr->filename, APR_READ,
1161 APR_OS_DEFAULT, r->pool) == APR_SUCCESS) {
1162 do_emit_plain(r, f);
1170 if (!suppress_sig) {
1171 ap_rputs(ap_psignature("", r), r);
1173 if (!suppress_post) {
1174 ap_rputs("</body></html>\n", r);
1177 ap_destroy_sub_req(rr);
1182 static char *find_title(request_rec *r)
1184 char titlebuf[MAX_STRING_LEN], *find = "<title>";
1185 apr_file_t *thefile = NULL;
1189 if (r->status != HTTP_OK) {
1192 if ((r->content_type != NULL)
1193 && (!strcasecmp(ap_field_noparam(r->pool, r->content_type),
1195 || !strcmp(r->content_type, INCLUDES_MAGIC_TYPE))
1196 && !r->content_encoding) {
1197 if (apr_file_open(&thefile, r->filename, APR_READ,
1198 APR_OS_DEFAULT, r->pool) != APR_SUCCESS) {
1201 n = sizeof(char) * (MAX_STRING_LEN - 1);
1202 apr_file_read(thefile, titlebuf, &n);
1204 apr_file_close(thefile);
1208 for (x = 0, p = 0; titlebuf[x]; x++) {
1209 if (apr_tolower(titlebuf[x]) == find[p]) {
1211 if ((p = ap_ind(&titlebuf[++x], '<')) != -1) {
1212 titlebuf[x + p] = '\0';
1214 /* Scan for line breaks for Tanmoy's secretary */
1215 for (y = x; titlebuf[y]; y++) {
1216 if ((titlebuf[y] == CR) || (titlebuf[y] == LF)) {
1225 apr_file_close(thefile);
1226 return apr_pstrdup(r->pool, &titlebuf[x]);
1233 apr_file_close(thefile);
1238 static struct ent *make_parent_entry(apr_int32_t autoindex_opts,
1239 autoindex_config_rec *d,
1240 request_rec *r, char keyid,
1243 struct ent *p = (struct ent *) apr_pcalloc(r->pool, sizeof(struct ent));
1246 * p->name is now the true parent URI.
1247 * testpath is a crafted lie, so that the syntax '/some/..'
1248 * (or simply '..')be used to describe 'up' from '/some/'
1249 * when processeing IndexIgnore, and Icon|Alt|Desc configs.
1252 /* The output has always been to the parent. Don't make ourself
1253 * our own parent (worthless cyclical reference).
1255 if (!(p->name = ap_make_full_path(r->pool, r->uri, "../"))) {
1258 ap_getparents(p->name);
1263 /* IndexIgnore has always compared "/thispath/.." */
1264 testpath = ap_make_full_path(r->pool, r->filename, "..");
1265 if (ignore_entry(d, testpath)) {
1271 p->key = apr_toupper(keyid);
1272 p->ascending = (apr_toupper(direction) == D_ASCENDING);
1273 p->version_sort = autoindex_opts & VERSION_SORT;
1274 if (autoindex_opts & FANCY_INDEXING) {
1275 if (!(p->icon = find_default_icon(d, testpath))) {
1276 p->icon = find_default_icon(d, "^^DIRECTORY^^");
1278 if (!(p->alt = find_default_alt(d, testpath))) {
1279 if (!(p->alt = find_default_alt(d, "^^DIRECTORY^^"))) {
1283 p->desc = find_desc(d, testpath);
1288 static struct ent *make_autoindex_entry(const apr_finfo_t *dirent,
1290 autoindex_config_rec *d,
1291 request_rec *r, char keyid,
1293 const char *pattern)
1297 int show_forbidden = 0;
1299 /* Dot is ignored, Parent is handled by make_parent_entry() */
1300 if ((dirent->name[0] == '.') && (!dirent->name[1]
1301 || ((dirent->name[1] == '.') && !dirent->name[2])))
1305 * On some platforms, the match must be case-blind. This is really
1306 * a factor of the filesystem involved, but we can't detect that
1307 * reliably - so we have to granularise at the OS level.
1309 if (pattern && (apr_fnmatch(pattern, dirent->name,
1310 APR_FNM_NOESCAPE | APR_FNM_PERIOD
1311 #ifdef CASE_BLIND_FILESYSTEM
1312 | APR_FNM_CASE_BLIND
1319 if (ignore_entry(d, ap_make_full_path(r->pool,
1320 r->filename, dirent->name))) {
1324 if (!(rr = ap_sub_req_lookup_dirent(dirent, r, AP_SUBREQ_NO_ARGS, NULL))) {
1328 if((autoindex_opts & SHOW_FORBIDDEN)
1329 && (rr->status == HTTP_UNAUTHORIZED || rr->status == HTTP_FORBIDDEN)) {
1333 if ((rr->finfo.filetype != APR_DIR && rr->finfo.filetype != APR_REG)
1334 || !(rr->status == OK || ap_is_HTTP_SUCCESS(rr->status)
1335 || ap_is_HTTP_REDIRECT(rr->status)
1336 || show_forbidden == 1)) {
1337 ap_destroy_sub_req(rr);
1341 p = (struct ent *) apr_pcalloc(r->pool, sizeof(struct ent));
1342 if (dirent->filetype == APR_DIR) {
1343 p->name = apr_pstrcat(r->pool, dirent->name, "/", NULL);
1346 p->name = apr_pstrdup(r->pool, dirent->name);
1354 p->key = apr_toupper(keyid);
1355 p->ascending = (apr_toupper(direction) == D_ASCENDING);
1356 p->version_sort = !!(autoindex_opts & VERSION_SORT);
1357 p->ignore_case = !!(autoindex_opts & IGNORE_CASE);
1359 if (autoindex_opts & (FANCY_INDEXING | TABLE_INDEXING)) {
1360 p->lm = rr->finfo.mtime;
1361 if (dirent->filetype == APR_DIR) {
1362 if (autoindex_opts & FOLDERS_FIRST) {
1365 rr->filename = ap_make_dirstr_parent (rr->pool, rr->filename);
1367 /* omit the trailing slash (1.3 compat) */
1368 rr->filename[strlen(rr->filename) - 1] = '\0';
1370 if (!(p->icon = find_icon(d, rr, 1))) {
1371 p->icon = find_default_icon(d, "^^DIRECTORY^^");
1373 if (!(p->alt = find_alt(d, rr, 1))) {
1374 if (!(p->alt = find_default_alt(d, "^^DIRECTORY^^"))) {
1380 p->icon = find_icon(d, rr, 0);
1381 p->alt = find_alt(d, rr, 0);
1382 p->size = rr->finfo.size;
1385 p->desc = find_desc(d, rr->filename);
1387 if ((!p->desc) && (autoindex_opts & SCAN_HTML_TITLES)) {
1388 p->desc = apr_pstrdup(r->pool, find_title(rr));
1391 ap_destroy_sub_req(rr);
1393 * We don't need to take any special action for the file size key.
1394 * If we did, it would go here.
1396 if (keyid == K_LAST_MOD) {
1404 static char *terminate_description(autoindex_config_rec *d, char *desc,
1405 apr_int32_t autoindex_opts, int desc_width)
1407 int maxsize = desc_width;
1411 * If there's no DescriptionWidth in effect, default to the old
1412 * behaviour of adjusting the description size depending upon
1413 * what else is being displayed. Otherwise, stick with the
1416 if (d->desc_adjust == K_UNSET) {
1417 if (autoindex_opts & SUPPRESS_ICON) {
1420 if (autoindex_opts & SUPPRESS_LAST_MOD) {
1423 if (autoindex_opts & SUPPRESS_SIZE) {
1427 for (x = 0; desc[x] && ((maxsize > 0) || (desc[x] == '<')); x++) {
1428 if (desc[x] == '<') {
1429 while (desc[x] != '>') {
1437 else if (desc[x] == '&') {
1438 /* entities like ä count as one character */
1440 for ( ; desc[x] != ';'; ++x) {
1441 if (desc[x] == '\0') {
1451 if (!maxsize && desc[x] != '\0') {
1452 desc[x - 1] = '>'; /* Grump. */
1453 desc[x] = '\0'; /* Double Grump! */
1459 * Emit the anchor for the specified field. If a field is the key for the
1460 * current request, the link changes its meaning to reverse the order when
1461 * selected again. Non-active fields always start in ascending order.
1463 static void emit_link(request_rec *r, const char *anchor, char column,
1464 char curkey, char curdirection,
1465 const char *colargs, int nosort)
1478 qvalue[7] = ((curkey == column) && (curdirection == D_ASCENDING))
1479 ? D_DESCENDING : D_ASCENDING;
1481 ap_rvputs(r, "<a href=\"", qvalue, colargs ? colargs : "",
1482 "\">", anchor, "</a>", NULL);
1485 ap_rputs(anchor, r);
1489 static void output_directories(struct ent **ar, int n,
1490 autoindex_config_rec *d, request_rec *r,
1491 apr_int32_t autoindex_opts, char keyid,
1492 char direction, const char *colargs)
1496 char *name = r->uri;
1498 int static_columns = !!(autoindex_opts & SUPPRESS_COLSORT);
1499 apr_pool_t *scratch;
1504 char *breakrow = "";
1506 apr_pool_create(&scratch, r->pool);
1507 if (name[0] == '\0') {
1511 name_width = d->name_width;
1512 desc_width = d->desc_width;
1514 if ((autoindex_opts & (FANCY_INDEXING | TABLE_INDEXING))
1515 == FANCY_INDEXING) {
1516 if (d->name_adjust == K_ADJUST) {
1517 for (x = 0; x < n; x++) {
1518 int t = strlen(ar[x]->name);
1519 if (t > name_width) {
1525 if (d->desc_adjust == K_ADJUST) {
1526 for (x = 0; x < n; x++) {
1527 if (ar[x]->desc != NULL) {
1528 int t = strlen(ar[x]->desc);
1529 if (t > desc_width) {
1536 name_scratch = apr_palloc(r->pool, name_width + 1);
1537 pad_scratch = apr_palloc(r->pool, name_width + 1);
1538 memset(pad_scratch, ' ', name_width);
1539 pad_scratch[name_width] = '\0';
1541 if (autoindex_opts & TABLE_INDEXING) {
1543 ap_rputs("<table><tr>", r);
1544 if (!(autoindex_opts & SUPPRESS_ICON)) {
1545 ap_rputs("<th>", r);
1546 if ((tp = find_default_icon(d, "^^BLANKICON^^"))) {
1547 ap_rvputs(r, "<img src=\"", ap_escape_html(scratch, tp),
1548 "\" alt=\"[ICO]\"", NULL);
1549 if (d->icon_width) {
1550 ap_rprintf(r, " width=\"%d\"", d->icon_width);
1552 if (d->icon_height) {
1553 ap_rprintf(r, " height=\"%d\"", d->icon_height);
1556 if (autoindex_opts & EMIT_XHTML) {
1559 ap_rputs("></th>", r);
1562 ap_rputs(" </th>", r);
1567 ap_rputs("<th>", r);
1568 emit_link(r, "Name", K_NAME, keyid, direction,
1569 colargs, static_columns);
1570 if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1571 ap_rputs("</th><th>", r);
1572 emit_link(r, "Last modified", K_LAST_MOD, keyid, direction,
1573 colargs, static_columns);
1576 if (!(autoindex_opts & SUPPRESS_SIZE)) {
1577 ap_rputs("</th><th>", r);
1578 emit_link(r, "Size", K_SIZE, keyid, direction,
1579 colargs, static_columns);
1582 if (!(autoindex_opts & SUPPRESS_DESC)) {
1583 ap_rputs("</th><th>", r);
1584 emit_link(r, "Description", K_DESC, keyid, direction,
1585 colargs, static_columns);
1588 if (!(autoindex_opts & SUPPRESS_RULES)) {
1589 breakrow = apr_psprintf(r->pool,
1590 "<tr><th colspan=\"%d\">"
1591 "<hr%s></th></tr>\n", cols,
1592 (autoindex_opts & EMIT_XHTML) ? " /" : "");
1594 ap_rvputs(r, "</th></tr>", breakrow, NULL);
1596 else if (autoindex_opts & FANCY_INDEXING) {
1597 ap_rputs("<pre>", r);
1598 if (!(autoindex_opts & SUPPRESS_ICON)) {
1599 if ((tp = find_default_icon(d, "^^BLANKICON^^"))) {
1600 ap_rvputs(r, "<img src=\"", ap_escape_html(scratch, tp),
1601 "\" alt=\"Icon \"", NULL);
1602 if (d->icon_width) {
1603 ap_rprintf(r, " width=\"%d\"", d->icon_width);
1605 if (d->icon_height) {
1606 ap_rprintf(r, " height=\"%d\"", d->icon_height);
1609 if (autoindex_opts & EMIT_XHTML) {
1618 emit_link(r, "Name", K_NAME, keyid, direction,
1619 colargs, static_columns);
1620 ap_rputs(pad_scratch + 4, r);
1622 * Emit the guaranteed-at-least-one-space-between-columns byte.
1625 if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1626 emit_link(r, "Last modified", K_LAST_MOD, keyid, direction,
1627 colargs, static_columns);
1630 if (!(autoindex_opts & SUPPRESS_SIZE)) {
1631 emit_link(r, "Size", K_SIZE, keyid, direction,
1632 colargs, static_columns);
1635 if (!(autoindex_opts & SUPPRESS_DESC)) {
1636 emit_link(r, "Description", K_DESC, keyid, direction,
1637 colargs, static_columns);
1639 if (!(autoindex_opts & SUPPRESS_RULES)) {
1641 if (autoindex_opts & EMIT_XHTML) {
1651 ap_rputs("<ul>", r);
1654 for (x = 0; x < n; x++) {
1655 char *anchor, *t, *t2;
1658 apr_pool_clear(scratch);
1661 anchor = ap_escape_html(scratch, ap_os_escape_path(scratch, t, 0));
1663 if (!x && t[0] == '/') {
1664 t2 = "Parent Directory";
1670 if (autoindex_opts & TABLE_INDEXING) {
1671 ap_rputs("<tr>", r);
1672 if (!(autoindex_opts & SUPPRESS_ICON)) {
1673 ap_rputs("<td valign=\"top\">", r);
1674 if (autoindex_opts & ICONS_ARE_LINKS) {
1675 ap_rvputs(r, "<a href=\"", anchor, "\">", NULL);
1677 if ((ar[x]->icon) || d->default_icon) {
1678 ap_rvputs(r, "<img src=\"",
1679 ap_escape_html(scratch,
1680 ar[x]->icon ? ar[x]->icon
1682 "\" alt=\"[", (ar[x]->alt ? ar[x]->alt : " "),
1684 if (d->icon_width) {
1685 ap_rprintf(r, " width=\"%d\"", d->icon_width);
1687 if (d->icon_height) {
1688 ap_rprintf(r, " height=\"%d\"", d->icon_height);
1691 if (autoindex_opts & EMIT_XHTML) {
1697 ap_rputs(" ", r);
1699 if (autoindex_opts & ICONS_ARE_LINKS) {
1700 ap_rputs("</a></td>", r);
1703 ap_rputs("</td>", r);
1706 if (d->name_adjust == K_ADJUST) {
1707 ap_rvputs(r, "<td><a href=\"", anchor, "\">",
1708 ap_escape_html(scratch, t2), "</a>", NULL);
1711 nwidth = strlen(t2);
1712 if (nwidth > name_width) {
1713 memcpy(name_scratch, t2, name_width - 3);
1714 name_scratch[name_width - 3] = '.';
1715 name_scratch[name_width - 2] = '.';
1716 name_scratch[name_width - 1] = '>';
1717 name_scratch[name_width] = 0;
1719 nwidth = name_width;
1721 ap_rvputs(r, "<td><a href=\"", anchor, "\">",
1722 ap_escape_html(scratch, t2),
1723 "</a>", pad_scratch + nwidth, NULL);
1725 if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1726 if (ar[x]->lm != -1) {
1727 char time_str[MAX_STRING_LEN];
1729 apr_time_exp_lt(&ts, ar[x]->lm);
1730 apr_strftime(time_str, &rv, MAX_STRING_LEN,
1731 "</td><td align=\"right\">%d-%b-%Y %H:%M ",
1733 ap_rputs(time_str, r);
1736 ap_rputs("</td><td> ", r);
1739 if (!(autoindex_opts & SUPPRESS_SIZE)) {
1741 ap_rvputs(r, "</td><td align=\"right\">",
1742 apr_strfsize(ar[x]->size, buf), NULL);
1744 if (!(autoindex_opts & SUPPRESS_DESC)) {
1746 if (d->desc_adjust == K_ADJUST) {
1747 ap_rvputs(r, "</td><td>", ar[x]->desc, NULL);
1750 ap_rvputs(r, "</td><td>",
1751 terminate_description(d, ar[x]->desc,
1758 ap_rputs("</td><td> ", r);
1760 ap_rputs("</td></tr>\n", r);
1762 else if (autoindex_opts & FANCY_INDEXING) {
1763 if (!(autoindex_opts & SUPPRESS_ICON)) {
1764 if (autoindex_opts & ICONS_ARE_LINKS) {
1765 ap_rvputs(r, "<a href=\"", anchor, "\">", NULL);
1767 if ((ar[x]->icon) || d->default_icon) {
1768 ap_rvputs(r, "<img src=\"",
1769 ap_escape_html(scratch,
1770 ar[x]->icon ? ar[x]->icon
1772 "\" alt=\"[", (ar[x]->alt ? ar[x]->alt : " "),
1774 if (d->icon_width) {
1775 ap_rprintf(r, " width=\"%d\"", d->icon_width);
1777 if (d->icon_height) {
1778 ap_rprintf(r, " height=\"%d\"", d->icon_height);
1781 if (autoindex_opts & EMIT_XHTML) {
1789 if (autoindex_opts & ICONS_ARE_LINKS) {
1790 ap_rputs("</a> ", r);
1796 nwidth = strlen(t2);
1797 if (nwidth > name_width) {
1798 memcpy(name_scratch, t2, name_width - 3);
1799 name_scratch[name_width - 3] = '.';
1800 name_scratch[name_width - 2] = '.';
1801 name_scratch[name_width - 1] = '>';
1802 name_scratch[name_width] = 0;
1804 nwidth = name_width;
1806 ap_rvputs(r, "<a href=\"", anchor, "\">",
1807 ap_escape_html(scratch, t2),
1808 "</a>", pad_scratch + nwidth, NULL);
1810 * The blank before the storm.. er, before the next field.
1813 if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1814 if (ar[x]->lm != -1) {
1815 char time_str[MAX_STRING_LEN];
1817 apr_time_exp_lt(&ts, ar[x]->lm);
1818 apr_strftime(time_str, &rv, MAX_STRING_LEN,
1819 "%d-%b-%Y %H:%M ", &ts);
1820 ap_rputs(time_str, r);
1823 /*Length="22-Feb-1998 23:42 " (see 4 lines above) */
1827 if (!(autoindex_opts & SUPPRESS_SIZE)) {
1829 ap_rputs(apr_strfsize(ar[x]->size, buf), r);
1832 if (!(autoindex_opts & SUPPRESS_DESC)) {
1834 ap_rputs(terminate_description(d, ar[x]->desc,
1842 ap_rvputs(r, "<li><a href=\"", anchor, "\"> ",
1843 ap_escape_html(scratch, t2),
1844 "</a></li>\n", NULL);
1847 if (autoindex_opts & TABLE_INDEXING) {
1848 ap_rvputs(r, breakrow, "</table>\n", NULL);
1850 else if (autoindex_opts & FANCY_INDEXING) {
1851 if (!(autoindex_opts & SUPPRESS_RULES)) {
1853 if (autoindex_opts & EMIT_XHTML) {
1856 ap_rputs("></pre>\n", r);
1859 ap_rputs("</pre>\n", r);
1863 ap_rputs("</ul>\n", r);
1868 * Compare two file entries according to the sort criteria. The return
1869 * is essentially a signum function value.
1872 static int dsortf(struct ent **e1, struct ent **e2)
1879 * First, see if either of the entries is for the parent directory.
1880 * If so, that *always* sorts lower than anything else.
1882 if ((*e1)->name[0] == '/') {
1885 if ((*e2)->name[0] == '/') {
1889 * Now see if one's a directory and one isn't, if we're set
1890 * isdir for FOLDERS_FIRST.
1892 if ((*e1)->isdir != (*e2)->isdir) {
1893 return (*e1)->isdir ? -1 : 1;
1896 * All of our comparisons will be of the c1 entry against the c2 one,
1897 * so assign them appropriately to take care of the ordering.
1899 if ((*e1)->ascending) {
1910 if (c1->lm > c2->lm) {
1913 else if (c1->lm < c2->lm) {
1918 if (c1->size > c2->size) {
1921 else if (c1->size < c2->size) {
1926 if (c1->version_sort) {
1927 result = apr_strnatcmp(c1->desc ? c1->desc : "",
1928 c2->desc ? c2->desc : "");
1931 result = strcmp(c1->desc ? c1->desc : "",
1932 c2->desc ? c2->desc : "");
1940 /* names may identical when treated case-insensitively,
1941 * so always fall back on strcmp() flavors to put entries
1942 * in deterministic order. This means that 'ABC' and 'abc'
1943 * will always appear in the same order, rather than
1944 * variably between 'ABC abc' and 'abc ABC' order.
1947 if (c1->version_sort) {
1948 if (c1->ignore_case) {
1949 result = apr_strnatcasecmp (c1->name, c2->name);
1952 result = apr_strnatcmp(c1->name, c2->name);
1956 /* The names may be identical in respects other other than
1957 * filename case when strnatcmp is used above, so fall back
1958 * to strcmp on conflicts so that fn1.01.zzz and fn1.1.zzz
1959 * are also sorted in a deterministic order.
1962 if (!result && c1->ignore_case) {
1963 result = strcasecmp (c1->name, c2->name);
1967 result = strcmp (c1->name, c2->name);
1974 static int index_directory(request_rec *r,
1975 autoindex_config_rec *autoindex_conf)
1977 char *title_name = ap_escape_html(r->pool, r->uri);
1979 char *name = r->filename;
1980 char *pstring = NULL;
1983 apr_status_t status;
1985 struct ent *head, *p;
1986 struct ent **ar = NULL;
1987 const char *qstring;
1988 apr_int32_t autoindex_opts = autoindex_conf->opts;
1993 apr_size_t dirpathlen;
1994 char *ctype = "text/html";
1997 if ((status = apr_dir_open(&thedir, name, r->pool)) != APR_SUCCESS) {
1998 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
1999 "Can't open directory for index: %s", r->filename);
2000 return HTTP_FORBIDDEN;
2003 if (autoindex_conf->ctype) {
2004 ctype = autoindex_conf->ctype;
2006 if (autoindex_conf->charset) {
2007 charset = autoindex_conf->charset;
2010 #if APR_HAS_UNICODE_FS
2013 charset = "ISO-8859-1";
2017 ap_set_content_type(r, apr_pstrcat(r->pool, ctype, ";charset=",
2021 ap_set_content_type(r, ctype);
2024 if (autoindex_opts & TRACK_MODIFIED) {
2025 ap_update_mtime(r, r->finfo.mtime);
2026 ap_set_last_modified(r);
2029 if (r->header_only) {
2030 apr_dir_close(thedir);
2035 * If there is no specific ordering defined for this directory,
2036 * default to ascending by filename.
2038 keyid = autoindex_conf->default_keyid
2039 ? autoindex_conf->default_keyid : K_NAME;
2040 direction = autoindex_conf->default_direction
2041 ? autoindex_conf->default_direction : D_ASCENDING;
2044 * Figure out what sort of indexing (if any) we're supposed to use.
2046 * If no QUERY_STRING was specified or client query strings have been
2047 * explicitly disabled.
2048 * If we are ignoring the client, suppress column sorting as well.
2050 if (autoindex_opts & IGNORE_CLIENT) {
2052 autoindex_opts |= SUPPRESS_COLSORT;
2056 char fval[5], vval[5], *ppre = "", *epattern = "";
2057 fval[0] = '\0'; vval[0] = '\0';
2060 while (qstring && *qstring) {
2062 /* C= First Sort key Column (N, M, S, D) */
2063 if ( qstring[0] == 'C' && qstring[1] == '='
2064 && qstring[2] && strchr(K_VALID, qstring[2])
2065 && ( qstring[3] == '&' || qstring[3] == ';'
2068 qstring += qstring[3] ? 4 : 3;
2071 /* O= Sort order (A, D) */
2072 else if ( qstring[0] == 'O' && qstring[1] == '='
2073 && ( (qstring[2] == D_ASCENDING)
2074 || (qstring[2] == D_DESCENDING))
2075 && ( qstring[3] == '&' || qstring[3] == ';'
2077 direction = qstring[2];
2078 qstring += qstring[3] ? 4 : 3;
2081 /* F= Output Format (0 plain, 1 fancy (pre), 2 table) */
2082 else if ( qstring[0] == 'F' && qstring[1] == '='
2083 && qstring[2] && strchr("012", qstring[2])
2084 && ( qstring[3] == '&' || qstring[3] == ';'
2086 if (qstring[2] == '0') {
2087 autoindex_opts &= ~(FANCY_INDEXING | TABLE_INDEXING);
2089 else if (qstring[2] == '1') {
2090 autoindex_opts = (autoindex_opts | FANCY_INDEXING)
2093 else if (qstring[2] == '2') {
2094 autoindex_opts |= FANCY_INDEXING | TABLE_INDEXING;
2096 strcpy(fval, ";F= ");
2097 fval[3] = qstring[2];
2098 qstring += qstring[3] ? 4 : 3;
2101 /* V= Version sort (0, 1) */
2102 else if ( qstring[0] == 'V' && qstring[1] == '='
2103 && (qstring[2] == '0' || qstring[2] == '1')
2104 && ( qstring[3] == '&' || qstring[3] == ';'
2106 if (qstring[2] == '0') {
2107 autoindex_opts &= ~VERSION_SORT;
2109 else if (qstring[2] == '1') {
2110 autoindex_opts |= VERSION_SORT;
2112 strcpy(vval, ";V= ");
2113 vval[3] = qstring[2];
2114 qstring += qstring[3] ? 4 : 3;
2117 /* P= wildcard pattern (*.foo) */
2118 else if (qstring[0] == 'P' && qstring[1] == '=') {
2119 const char *eos = qstring += 2; /* for efficiency */
2121 while (*eos && *eos != '&' && *eos != ';') {
2125 if (eos == qstring) {
2129 pstring = apr_pstrndup(r->pool, qstring, eos - qstring);
2130 if (ap_unescape_url(pstring) != OK) {
2131 /* ignore the pattern, if it's bad. */
2137 epattern = ap_escape_uri(r->pool, pstring);
2141 if (*eos && *++eos) {
2149 /* Syntax error? Ignore the remainder! */
2154 colargs = apr_pstrcat(r->pool, fval, vval, ppre, epattern, NULL);
2157 /* Spew HTML preamble */
2158 title_endp = title_name + strlen(title_name) - 1;
2160 while (title_endp > title_name && *title_endp == '/') {
2161 *title_endp-- = '\0';
2164 emit_head(r, find_header(autoindex_conf, r),
2165 autoindex_opts & SUPPRESS_PREAMBLE,
2166 autoindex_opts & EMIT_XHTML, title_name);
2169 * Since we don't know how many dir. entries there are, put them into a
2170 * linked list and then arrayificate them so qsort can use them.
2173 p = make_parent_entry(autoindex_opts, autoindex_conf, r, keyid, direction);
2179 fullpath = apr_palloc(r->pool, APR_PATH_MAX);
2180 dirpathlen = strlen(name);
2181 memcpy(fullpath, name, dirpathlen);
2184 status = apr_dir_read(&dirent, APR_FINFO_MIN | APR_FINFO_NAME, thedir);
2185 if (APR_STATUS_IS_INCOMPLETE(status)) {
2186 continue; /* ignore un-stat()able files */
2188 else if (status != APR_SUCCESS) {
2192 /* We want to explode symlinks here. */
2193 if (dirent.filetype == APR_LNK) {
2194 const char *savename;
2196 /* We *must* have FNAME. */
2197 savename = dirent.name;
2198 apr_cpystrn(fullpath + dirpathlen, dirent.name,
2199 APR_PATH_MAX - dirpathlen);
2200 status = apr_stat(&fi, fullpath,
2201 dirent.valid & ~(APR_FINFO_NAME), r->pool);
2202 if (status != APR_SUCCESS) {
2203 /* Something bad happened, skip this file. */
2206 memcpy(&dirent, &fi, sizeof(fi));
2207 dirent.name = savename;
2208 dirent.valid |= APR_FINFO_NAME;
2210 p = make_autoindex_entry(&dirent, autoindex_opts, autoindex_conf, r,
2211 keyid, direction, pstring);
2220 ar = (struct ent **) apr_palloc(r->pool,
2221 num_ent * sizeof(struct ent *));
2229 qsort((void *) ar, num_ent, sizeof(struct ent *),
2230 (int (*)(const void *, const void *)) dsortf);
2232 output_directories(ar, num_ent, autoindex_conf, r, autoindex_opts,
2233 keyid, direction, colargs);
2234 apr_dir_close(thedir);
2236 emit_tail(r, find_readme(autoindex_conf, r),
2237 autoindex_opts & SUPPRESS_PREAMBLE);
2242 /* The formal handler... */
2244 static int handle_autoindex(request_rec *r)
2246 autoindex_config_rec *d;
2249 if(strcmp(r->handler,DIR_MAGIC_TYPE)) {
2253 allow_opts = ap_allow_options(r);
2255 d = (autoindex_config_rec *) ap_get_module_config(r->per_dir_config,
2258 r->allowed |= (AP_METHOD_BIT << M_GET);
2259 if (r->method_number != M_GET) {
2263 /* OK, nothing easy. Trot out the heavy artillery... */
2265 if (allow_opts & OPT_INDEXES) {
2268 if ((errstatus = ap_discard_request_body(r)) != OK) {
2272 /* KLUDGE --- make the sub_req lookups happen in the right directory.
2273 * Fixing this in the sub_req_lookup functions themselves is difficult,
2274 * and would probably break virtual includes...
2277 if (r->filename[strlen(r->filename) - 1] != '/') {
2278 r->filename = apr_pstrcat(r->pool, r->filename, "/", NULL);
2280 return index_directory(r, d);
2283 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
2284 "Directory index forbidden by "
2285 "Options directive: %s", r->filename);
2286 return HTTP_FORBIDDEN;
2290 static void register_hooks(apr_pool_t *p)
2292 ap_hook_handler(handle_autoindex,NULL,NULL,APR_HOOK_MIDDLE);
2295 module AP_MODULE_DECLARE_DATA autoindex_module =
2297 STANDARD20_MODULE_STUFF,
2298 create_autoindex_config, /* dir config creater */
2299 merge_autoindex_configs, /* dir merger --- default is to override */
2300 NULL, /* server config */
2301 NULL, /* merge server config */
2302 autoindex_cmds, /* command apr_table_t */
2303 register_hooks /* register hooks */