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)
79 * Define keys for sorting.
81 #define K_NAME 'N' /* Sort by file name (default) */
82 #define K_LAST_MOD 'M' /* Last modification date */
83 #define K_SIZE 'S' /* Size (absolute, not as displayed) */
84 #define K_DESC 'D' /* Description */
85 #define K_VALID "NMSD" /* String containing _all_ valid K_ opts */
87 #define D_ASCENDING 'A'
88 #define D_DESCENDING 'D'
89 #define D_VALID "AD" /* String containing _all_ valid D_ opts */
92 * These are the dimensions of the default icons supplied with Apache.
94 #define DEFAULT_ICON_WIDTH 20
95 #define DEFAULT_ICON_HEIGHT 22
98 * Other default dimensions.
100 #define DEFAULT_NAME_WIDTH 23
101 #define DEFAULT_DESC_WIDTH 23
110 typedef struct ai_desc_t {
117 typedef struct autoindex_config_struct {
121 apr_int32_t incremented_opts;
122 apr_int32_t decremented_opts;
130 char default_direction;
132 apr_array_header_t *icon_list;
133 apr_array_header_t *alt_list;
134 apr_array_header_t *desc_list;
135 apr_array_header_t *ign_list;
136 apr_array_header_t *hdr_list;
137 apr_array_header_t *rdme_list;
141 } autoindex_config_rec;
143 static char c_by_encoding, c_by_type, c_by_path;
145 #define BY_ENCODING &c_by_encoding
146 #define BY_TYPE &c_by_type
147 #define BY_PATH &c_by_path
150 * This routine puts the standard HTML header at the top of the index page.
151 * We include the DOCTYPE because we may be using features therefrom (i.e.,
152 * HEIGHT and WIDTH attributes on the icons if we're FancyIndexing).
154 static void emit_preamble(request_rec *r, int xhtml, const char *title)
156 ap_rvputs(r, xhtml ? DOCTYPE_XHTML_1_0T : DOCTYPE_HTML_3_2,
157 "<html>\n <head>\n <title>Index of ", title,
158 "</title>\n </head>\n <body>\n", NULL);
161 static void push_item(apr_array_header_t *arr, char *type, const char *to,
162 const char *path, const char *data)
164 struct item *p = (struct item *) apr_array_push(arr);
174 p->data = data ? apr_pstrdup(arr->pool, data) : NULL;
175 p->apply_path = apr_pstrcat(arr->pool, path, "*", NULL);
177 if ((type == BY_PATH) && (!ap_is_matchexp(to))) {
178 p->apply_to = apr_pstrcat(arr->pool, "*", to, NULL);
181 p->apply_to = apr_pstrdup(arr->pool, to);
188 static const char *add_alt(cmd_parms *cmd, void *d, const char *alt,
191 if (cmd->info == BY_PATH) {
192 if (!strcmp(to, "**DIRECTORY**")) {
193 to = "^^DIRECTORY^^";
196 if (cmd->info == BY_ENCODING) {
197 char *tmp = apr_pstrdup(cmd->pool, to);
202 push_item(((autoindex_config_rec *) d)->alt_list, cmd->info, to,
207 static const char *add_icon(cmd_parms *cmd, void *d, const char *icon,
210 char *iconbak = apr_pstrdup(cmd->pool, icon);
212 if (icon[0] == '(') {
214 char *cl = strchr(iconbak, ')');
217 return "missing closing paren";
219 alt = ap_getword_nc(cmd->pool, &iconbak, ',');
220 *cl = '\0'; /* Lose closing paren */
221 add_alt(cmd, d, &alt[1], to);
223 if (cmd->info == BY_PATH) {
224 if (!strcmp(to, "**DIRECTORY**")) {
225 to = "^^DIRECTORY^^";
228 if (cmd->info == BY_ENCODING) {
229 char *tmp = apr_pstrdup(cmd->pool, to);
234 push_item(((autoindex_config_rec *) d)->icon_list, cmd->info, to,
240 * Add description text for a filename pattern. If the pattern has
241 * wildcards already (or we need to add them), add leading and
242 * trailing wildcards to it to ensure substring processing. If the
243 * pattern contains a '/' anywhere, force wildcard matching mode,
244 * add a slash to the prefix so that "bar/bletch" won't be matched
245 * by "foobar/bletch", and make a note that there's a delimiter;
246 * the matching routine simplifies to just the actual filename
247 * whenever it can. This allows definitions in parent directories
248 * to be made for files in subordinate ones using relative paths.
252 * Absent a strcasestr() function, we have to force wildcards on
253 * systems for which "AAA" and "aaa" mean the same file.
255 #ifdef CASE_BLIND_FILESYSTEM
256 #define WILDCARDS_REQUIRED 1
258 #define WILDCARDS_REQUIRED 0
261 static const char *add_desc(cmd_parms *cmd, void *d, const char *desc,
264 autoindex_config_rec *dcfg = (autoindex_config_rec *) d;
265 ai_desc_t *desc_entry;
268 desc_entry = (ai_desc_t *) apr_array_push(dcfg->desc_list);
269 desc_entry->full_path = (ap_strchr_c(to, '/') == NULL) ? 0 : 1;
270 desc_entry->wildcards = (WILDCARDS_REQUIRED
271 || desc_entry->full_path
272 || apr_fnmatch_test(to));
273 if (desc_entry->wildcards) {
274 prefix = desc_entry->full_path ? "*/" : "*";
275 desc_entry->pattern = apr_pstrcat(dcfg->desc_list->pool,
276 prefix, to, "*", NULL);
279 desc_entry->pattern = apr_pstrdup(dcfg->desc_list->pool, to);
281 desc_entry->description = apr_pstrdup(dcfg->desc_list->pool, desc);
285 static const char *add_ignore(cmd_parms *cmd, void *d, const char *ext)
287 push_item(((autoindex_config_rec *) d)->ign_list, 0, ext, cmd->path, NULL);
291 static const char *add_header(cmd_parms *cmd, void *d, const char *name)
293 push_item(((autoindex_config_rec *) d)->hdr_list, 0, NULL, cmd->path,
298 static const char *add_readme(cmd_parms *cmd, void *d, const char *name)
300 push_item(((autoindex_config_rec *) d)->rdme_list, 0, NULL, cmd->path,
305 static const char *add_opts(cmd_parms *cmd, void *d, const char *optstr)
309 apr_int32_t opts_add;
310 apr_int32_t opts_remove;
312 autoindex_config_rec *d_cfg = (autoindex_config_rec *) d;
315 opts_add = d_cfg->incremented_opts;
316 opts_remove = d_cfg->decremented_opts;
320 w = ap_getword_conf(cmd->pool, &optstr);
321 if ((*w == '+') || (*w == '-')) {
327 if (!strcasecmp(w, "FancyIndexing")) {
328 option = FANCY_INDEXING;
330 else if (!strcasecmp(w, "FoldersFirst")) {
331 option = FOLDERS_FIRST;
333 else if (!strcasecmp(w, "HTMLTable")) {
334 option = TABLE_INDEXING;
336 else if (!strcasecmp(w, "IconsAreLinks")) {
337 option = ICONS_ARE_LINKS;
339 else if (!strcasecmp(w, "IgnoreCase")) {
340 option = IGNORE_CASE;
342 else if (!strcasecmp(w, "IgnoreClient")) {
343 option = IGNORE_CLIENT;
345 else if (!strcasecmp(w, "ScanHTMLTitles")) {
346 option = SCAN_HTML_TITLES;
348 else if (!strcasecmp(w, "SuppressColumnSorting")) {
349 option = SUPPRESS_COLSORT;
351 else if (!strcasecmp(w, "SuppressDescription")) {
352 option = SUPPRESS_DESC;
354 else if (!strcasecmp(w, "SuppressHTMLPreamble")) {
355 option = SUPPRESS_PREAMBLE;
357 else if (!strcasecmp(w, "SuppressIcon")) {
358 option = SUPPRESS_ICON;
360 else if (!strcasecmp(w, "SuppressLastModified")) {
361 option = SUPPRESS_LAST_MOD;
363 else if (!strcasecmp(w, "SuppressSize")) {
364 option = SUPPRESS_SIZE;
366 else if (!strcasecmp(w, "SuppressRules")) {
367 option = SUPPRESS_RULES;
369 else if (!strcasecmp(w, "TrackModified")) {
370 option = TRACK_MODIFIED;
372 else if (!strcasecmp(w, "VersionSort")) {
373 option = VERSION_SORT;
375 else if (!strcasecmp(w, "XHTML")) {
378 else if (!strcasecmp(w, "None")) {
379 if (action != '\0') {
380 return "Cannot combine '+' or '-' with 'None' keyword";
386 else if (!strcasecmp(w, "IconWidth")) {
388 d_cfg->icon_width = DEFAULT_ICON_WIDTH;
391 d_cfg->icon_width = 0;
394 else if (!strncasecmp(w, "IconWidth=", 10)) {
396 return "Cannot combine '-' with IconWidth=n";
398 d_cfg->icon_width = atoi(&w[10]);
400 else if (!strcasecmp(w, "IconHeight")) {
402 d_cfg->icon_height = DEFAULT_ICON_HEIGHT;
405 d_cfg->icon_height = 0;
408 else if (!strncasecmp(w, "IconHeight=", 11)) {
410 return "Cannot combine '-' with IconHeight=n";
412 d_cfg->icon_height = atoi(&w[11]);
414 else if (!strcasecmp(w, "NameWidth")) {
416 return "NameWidth with no value may only appear as "
419 d_cfg->name_width = DEFAULT_NAME_WIDTH;
420 d_cfg->name_adjust = K_NOADJUST;
422 else if (!strncasecmp(w, "NameWidth=", 10)) {
424 return "Cannot combine '-' with NameWidth=n";
427 d_cfg->name_adjust = K_ADJUST;
430 int width = atoi(&w[10]);
432 if (width && (width < 5)) {
433 return "NameWidth value must be greater than 5";
435 d_cfg->name_width = width;
436 d_cfg->name_adjust = K_NOADJUST;
439 else if (!strcasecmp(w, "DescriptionWidth")) {
441 return "DescriptionWidth with no value may only appear as "
442 "'-DescriptionWidth'";
444 d_cfg->desc_width = DEFAULT_DESC_WIDTH;
445 d_cfg->desc_adjust = K_NOADJUST;
447 else if (!strncasecmp(w, "DescriptionWidth=", 17)) {
449 return "Cannot combine '-' with DescriptionWidth=n";
452 d_cfg->desc_adjust = K_ADJUST;
455 int width = atoi(&w[17]);
457 if (width && (width < 12)) {
458 return "DescriptionWidth value must be greater than 12";
460 d_cfg->desc_width = width;
461 d_cfg->desc_adjust = K_NOADJUST;
464 else if (!strncasecmp(w, "Type=", 5)) {
465 d_cfg->ctype = apr_pstrdup(cmd->pool, &w[5]);
467 else if (!strncasecmp(w, "Charset=", 8)) {
468 d_cfg->charset = apr_pstrdup(cmd->pool, &w[8]);
471 return "Invalid directory indexing option";
473 if (action == '\0') {
478 else if (action == '+') {
480 opts_remove &= ~option;
483 opts_remove |= option;
487 if ((opts & NO_OPTIONS) && (opts & ~NO_OPTIONS)) {
488 return "Cannot combine other IndexOptions keywords with 'None'";
490 d_cfg->incremented_opts = opts_add;
491 d_cfg->decremented_opts = opts_remove;
496 static const char *set_default_order(cmd_parms *cmd, void *m,
497 const char *direction, const char *key)
499 autoindex_config_rec *d_cfg = (autoindex_config_rec *) m;
501 if (!strcasecmp(direction, "Ascending")) {
502 d_cfg->default_direction = D_ASCENDING;
504 else if (!strcasecmp(direction, "Descending")) {
505 d_cfg->default_direction = D_DESCENDING;
508 return "First keyword must be 'Ascending' or 'Descending'";
511 if (!strcasecmp(key, "Name")) {
512 d_cfg->default_keyid = K_NAME;
514 else if (!strcasecmp(key, "Date")) {
515 d_cfg->default_keyid = K_LAST_MOD;
517 else if (!strcasecmp(key, "Size")) {
518 d_cfg->default_keyid = K_SIZE;
520 else if (!strcasecmp(key, "Description")) {
521 d_cfg->default_keyid = K_DESC;
524 return "Second keyword must be 'Name', 'Date', 'Size', or "
531 #define DIR_CMD_PERMS OR_INDEXES
533 static const command_rec autoindex_cmds[] =
535 AP_INIT_ITERATE2("AddIcon", add_icon, BY_PATH, DIR_CMD_PERMS,
536 "an icon URL followed by one or more filenames"),
537 AP_INIT_ITERATE2("AddIconByType", add_icon, BY_TYPE, DIR_CMD_PERMS,
538 "an icon URL followed by one or more MIME types"),
539 AP_INIT_ITERATE2("AddIconByEncoding", add_icon, BY_ENCODING, DIR_CMD_PERMS,
540 "an icon URL followed by one or more content encodings"),
541 AP_INIT_ITERATE2("AddAlt", add_alt, BY_PATH, DIR_CMD_PERMS,
542 "alternate descriptive text followed by one or more "
544 AP_INIT_ITERATE2("AddAltByType", add_alt, BY_TYPE, DIR_CMD_PERMS,
545 "alternate descriptive text followed by one or more MIME "
547 AP_INIT_ITERATE2("AddAltByEncoding", add_alt, BY_ENCODING, DIR_CMD_PERMS,
548 "alternate descriptive text followed by one or more "
549 "content encodings"),
550 AP_INIT_RAW_ARGS("IndexOptions", add_opts, NULL, DIR_CMD_PERMS,
551 "one or more index options [+|-][]"),
552 AP_INIT_TAKE2("IndexOrderDefault", set_default_order, NULL, DIR_CMD_PERMS,
553 "{Ascending,Descending} {Name,Size,Description,Date}"),
554 AP_INIT_ITERATE("IndexIgnore", add_ignore, NULL, DIR_CMD_PERMS,
555 "one or more file extensions"),
556 AP_INIT_ITERATE2("AddDescription", add_desc, BY_PATH, DIR_CMD_PERMS,
557 "Descriptive text followed by one or more filenames"),
558 AP_INIT_TAKE1("HeaderName", add_header, NULL, DIR_CMD_PERMS,
560 AP_INIT_TAKE1("ReadmeName", add_readme, NULL, DIR_CMD_PERMS,
562 AP_INIT_RAW_ARGS("FancyIndexing", ap_set_deprecated, NULL, OR_ALL,
563 "The FancyIndexing directive is no longer supported. "
564 "Use IndexOptions FancyIndexing."),
565 AP_INIT_TAKE1("DefaultIcon", ap_set_string_slot,
566 (void *)APR_OFFSETOF(autoindex_config_rec, default_icon),
567 DIR_CMD_PERMS, "an icon URL"),
571 static void *create_autoindex_config(apr_pool_t *p, char *dummy)
573 autoindex_config_rec *new =
574 (autoindex_config_rec *) apr_pcalloc(p, sizeof(autoindex_config_rec));
577 new->icon_height = 0;
578 new->name_width = DEFAULT_NAME_WIDTH;
579 new->name_adjust = K_UNSET;
580 new->desc_width = DEFAULT_DESC_WIDTH;
581 new->desc_adjust = K_UNSET;
582 new->icon_list = apr_array_make(p, 4, sizeof(struct item));
583 new->alt_list = apr_array_make(p, 4, sizeof(struct item));
584 new->desc_list = apr_array_make(p, 4, sizeof(ai_desc_t));
585 new->ign_list = apr_array_make(p, 4, sizeof(struct item));
586 new->hdr_list = apr_array_make(p, 4, sizeof(struct item));
587 new->rdme_list = apr_array_make(p, 4, sizeof(struct item));
589 new->incremented_opts = 0;
590 new->decremented_opts = 0;
591 new->default_keyid = '\0';
592 new->default_direction = '\0';
597 static void *merge_autoindex_configs(apr_pool_t *p, void *basev, void *addv)
599 autoindex_config_rec *new;
600 autoindex_config_rec *base = (autoindex_config_rec *) basev;
601 autoindex_config_rec *add = (autoindex_config_rec *) addv;
603 new = (autoindex_config_rec *) apr_pcalloc(p, sizeof(autoindex_config_rec));
604 new->default_icon = add->default_icon ? add->default_icon
605 : base->default_icon;
606 new->icon_height = add->icon_height ? add->icon_height : base->icon_height;
607 new->icon_width = add->icon_width ? add->icon_width : base->icon_width;
609 new->ctype = add->ctype ? add->ctype : base->ctype;
610 new->charset = add->charset ? add->charset : base->charset;
612 new->alt_list = apr_array_append(p, add->alt_list, base->alt_list);
613 new->ign_list = apr_array_append(p, add->ign_list, base->ign_list);
614 new->hdr_list = apr_array_append(p, add->hdr_list, base->hdr_list);
615 new->desc_list = apr_array_append(p, add->desc_list, base->desc_list);
616 new->icon_list = apr_array_append(p, add->icon_list, base->icon_list);
617 new->rdme_list = apr_array_append(p, add->rdme_list, base->rdme_list);
618 if (add->opts & NO_OPTIONS) {
620 * If the current directory says 'no options' then we also
621 * clear any incremental mods from being inheritable further down.
623 new->opts = NO_OPTIONS;
624 new->incremented_opts = 0;
625 new->decremented_opts = 0;
629 * If there were any nonincremental options selected for
630 * this directory, they dominate and we don't inherit *anything.*
631 * Contrariwise, we *do* inherit if the only settings here are
634 if (add->opts == 0) {
635 new->incremented_opts = (base->incremented_opts
636 | add->incremented_opts)
637 & ~add->decremented_opts;
638 new->decremented_opts = (base->decremented_opts
639 | add->decremented_opts);
641 * We may have incremental settings, so make sure we don't
642 * inadvertently inherit an IndexOptions None from above.
644 new->opts = (base->opts & ~NO_OPTIONS);
648 * There are local nonincremental settings, which clear
649 * all inheritance from above. They *are* the new base settings.
651 new->opts = add->opts;;
654 * We're guaranteed that there'll be no overlap between
655 * the add-options and the remove-options.
657 new->opts |= new->incremented_opts;
658 new->opts &= ~new->decremented_opts;
661 * Inherit the NameWidth settings if there aren't any specific to
662 * the new location; otherwise we'll end up using the defaults set in the
663 * config-rec creation routine.
665 if (add->name_adjust == K_UNSET) {
666 new->name_width = base->name_width;
667 new->name_adjust = base->name_adjust;
670 new->name_width = add->name_width;
671 new->name_adjust = add->name_adjust;
675 * Likewise for DescriptionWidth.
677 if (add->desc_adjust == K_UNSET) {
678 new->desc_width = base->desc_width;
679 new->desc_adjust = base->desc_adjust;
682 new->desc_width = add->desc_width;
683 new->desc_adjust = add->desc_adjust;
686 new->default_keyid = add->default_keyid ? add->default_keyid
687 : base->default_keyid;
688 new->default_direction = add->default_direction ? add->default_direction
689 : base->default_direction;
693 /****************************************************************
695 * Looking things up in config entries...
698 /* Structure used to hold entries when we're actually building an index */
708 int ascending, ignore_case, version_sort;
713 static char *find_item(request_rec *r, apr_array_header_t *list, int path_only)
715 const char *content_type = ap_field_noparam(r->pool, r->content_type);
716 const char *content_encoding = r->content_encoding;
717 char *path = r->filename;
719 struct item *items = (struct item *) list->elts;
722 for (i = 0; i < list->nelts; ++i) {
723 struct item *p = &items[i];
725 /* Special cased for ^^DIRECTORY^^ and ^^BLANKICON^^ */
726 if ((path[0] == '^') || (!ap_strcmp_match(path, p->apply_path))) {
727 if (!*(p->apply_to)) {
730 else if (p->type == BY_PATH || path[0] == '^') {
731 if (!ap_strcmp_match(path, p->apply_to)) {
735 else if (!path_only) {
736 if (!content_encoding) {
737 if (p->type == BY_TYPE) {
739 && !ap_strcasecmp_match(content_type,
746 if (p->type == BY_ENCODING) {
747 if (!ap_strcasecmp_match(content_encoding,
759 #define find_icon(d,p,t) find_item(p,d->icon_list,t)
760 #define find_alt(d,p,t) find_item(p,d->alt_list,t)
761 #define find_header(d,p) find_item(p,d->hdr_list,0)
762 #define find_readme(d,p) find_item(p,d->rdme_list,0)
764 static char *find_default_item(char *bogus_name, apr_array_header_t *list)
767 /* Bleah. I tried to clean up find_item, and it lead to this bit
768 * of ugliness. Note that the fields initialized are precisely
769 * those that find_item looks at...
771 r.filename = bogus_name;
772 r.content_type = r.content_encoding = NULL;
773 return find_item(&r, list, 1);
776 #define find_default_icon(d,n) find_default_item(n, d->icon_list)
777 #define find_default_alt(d,n) find_default_item(n, d->alt_list)
780 * Look through the list of pattern/description pairs and return the first one
781 * if any) that matches the filename in the request. If multiple patterns
782 * match, only the first one is used; since the order in the array is the
783 * same as the order in which directives were processed, earlier matching
784 * directives will dominate.
787 #ifdef CASE_BLIND_FILESYSTEM
788 #define MATCH_FLAGS FNM_CASE_BLIND
790 #define MATCH_FLAGS 0
793 static char *find_desc(autoindex_config_rec *dcfg, const char *filename_full)
796 ai_desc_t *list = (ai_desc_t *) dcfg->desc_list->elts;
797 const char *filename_only;
798 const char *filename;
801 * If the filename includes a path, extract just the name itself
802 * for the simple matches.
804 if ((filename_only = ap_strrchr_c(filename_full, '/')) == NULL) {
805 filename_only = filename_full;
810 for (i = 0; i < dcfg->desc_list->nelts; ++i) {
811 ai_desc_t *tuple = &list[i];
815 * Only use the full-path filename if the pattern contains '/'s.
817 filename = (tuple->full_path) ? filename_full : filename_only;
819 * Make the comparison using the cheapest method; only do
820 * wildcard checking if we must.
822 if (tuple->wildcards) {
823 found = (apr_fnmatch(tuple->pattern, filename, MATCH_FLAGS) == 0);
826 found = (ap_strstr_c(filename, tuple->pattern) != NULL);
829 return tuple->description;
835 static int ignore_entry(autoindex_config_rec *d, char *path)
837 apr_array_header_t *list = d->ign_list;
838 struct item *items = (struct item *) list->elts;
842 if ((tt = strrchr(path, '/')) == NULL) {
849 for (i = 0; i < list->nelts; ++i) {
850 struct item *p = &items[i];
853 if ((ap = strrchr(p->apply_to, '/')) == NULL) {
860 #ifndef CASE_BLIND_FILESYSTEM
861 if (!ap_strcmp_match(path, p->apply_path)
862 && !ap_strcmp_match(tt, ap)) {
865 #else /* !CASE_BLIND_FILESYSTEM */
867 * On some platforms, the match must be case-blind. This is really
868 * a factor of the filesystem involved, but we can't detect that
869 * reliably - so we have to granularise at the OS level.
871 if (!ap_strcasecmp_match(path, p->apply_path)
872 && !ap_strcasecmp_match(tt, ap)) {
875 #endif /* !CASE_BLIND_FILESYSTEM */
880 /*****************************************************************
882 * Actually generating output
886 * Elements of the emitted document:
888 * Emitted unless SUPPRESS_PREAMBLE is set AND ap_run_sub_req
889 * succeeds for the (content_type == text/html) header file.
891 * Emitted if found (and able).
893 * Emitted if a header file is NOT emitted.
897 * Emitted if FANCY_INDEXING is set.
899 * Emitted if found (and able).
901 * Emitted if ServerSignature is not Off AND a readme file
904 * Emitted unless SUPPRESS_PREAMBLE is set AND ap_run_sub_req
905 * succeeds for the (content_type == text/html) readme file.
910 * emit a plain text file
912 static void do_emit_plain(request_rec *r, apr_file_t *f)
914 char buf[AP_IOBUFSIZE + 1];
919 ap_rputs("<pre>\n", r);
920 while (!apr_file_eof(f)) {
922 n = sizeof(char) * AP_IOBUFSIZE;
923 rv = apr_file_read(f, buf, &n);
924 } while (APR_STATUS_IS_EINTR(rv));
925 if (n == 0 || rv != APR_SUCCESS) {
926 /* ###: better error here? */
932 for (i = c; i < n; i++) {
933 if (buf[i] == '<' || buf[i] == '>' || buf[i] == '&') {
939 ap_rputs(&buf[c], r);
943 else if (ch == '>') {
946 else if (ch == '&') {
947 ap_rputs("&", r);
952 ap_rputs("</pre>\n", r);
956 * Handle the preamble through the H1 tag line, inclusive. Locate
957 * the file with a subrequests. Process text/html documents by actually
958 * running the subrequest; text/xxx documents get copied verbatim,
959 * and any other content type is ignored. This means that a non-text
960 * document (such as HEADER.gif) might get multiviewed as the result
961 * instead of a text document, meaning nothing will be displayed, but
964 static void emit_head(request_rec *r, char *header_fname, int suppress_amble,
965 int emit_xhtml, char *title)
967 apr_table_t *hdrs = r->headers_in;
968 apr_file_t *f = NULL;
969 request_rec *rr = NULL;
972 const char *r_accept;
973 const char *r_accept_enc;
976 * If there's a header file, send a subrequest to look for it. If it's
977 * found and html do the subrequest, otherwise handle it
979 r_accept = apr_table_get(hdrs, "Accept");
980 r_accept_enc = apr_table_get(hdrs, "Accept-Encoding");
981 apr_table_setn(hdrs, "Accept", "text/html, text/plain");
982 apr_table_unset(hdrs, "Accept-Encoding");
985 if ((header_fname != NULL) && r->args) {
986 header_fname = apr_pstrcat(r->pool, header_fname, "?", r->args, NULL);
989 if ((header_fname != NULL)
990 && (rr = ap_sub_req_lookup_uri(header_fname, r, r->output_filters))
991 && (rr->status == HTTP_OK)
992 && (rr->filename != NULL)
993 && (rr->finfo.filetype == APR_REG)) {
995 * Check for the two specific cases we allow: text/html and
996 * text/anything-else. The former is allowed to be processed for
999 if (rr->content_type != NULL) {
1000 if (!strcasecmp(ap_field_noparam(r->pool, rr->content_type),
1003 /* Hope everything will work... */
1007 if (! suppress_amble) {
1008 emit_preamble(r, emit_xhtml, title);
1010 /* This is a hack, but I can't find any better way to do this.
1011 * The problem is that we have already created the sub-request,
1012 * but we just inserted the OLD_WRITE filter, and the
1013 * sub-request needs to pass its data through the OLD_WRITE
1014 * filter, or things go horribly wrong (missing data, data in
1015 * the wrong order, etc). To fix it, if you create a
1016 * sub-request and then insert the OLD_WRITE filter before you
1017 * run the request, you need to make sure that the sub-request
1018 * data goes through the OLD_WRITE filter. Just steal this
1019 * code. The long-term solution is to remove the ap_r*
1022 for (f=rr->output_filters;
1023 f->frec != ap_subreq_core_filter_handle; f = f->next);
1024 f->next = r->output_filters;
1027 * If there's a problem running the subrequest, display the
1028 * preamble if we didn't do it before -- the header file
1029 * didn't get displayed.
1031 if (ap_run_sub_req(rr) != OK) {
1032 /* It didn't work */
1033 emit_amble = suppress_amble;
1037 else if (!strncasecmp("text/", rr->content_type, 5)) {
1039 * If we can open the file, prefix it with the preamble
1040 * regardless; since we'll be sending a <pre> block around
1041 * the file's contents, any HTML header it had won't end up
1044 if (apr_file_open(&f, rr->filename, APR_READ,
1045 APR_OS_DEFAULT, r->pool) == APR_SUCCESS) {
1046 emit_preamble(r, emit_xhtml, title);
1048 do_emit_plain(r, f);
1057 apr_table_setn(hdrs, "Accept", r_accept);
1060 apr_table_unset(hdrs, "Accept");
1064 apr_table_setn(hdrs, "Accept-Encoding", r_accept_enc);
1068 emit_preamble(r, emit_xhtml, title);
1071 ap_rvputs(r, "<h1>Index of ", title, "</h1>\n", NULL);
1074 ap_destroy_sub_req(rr);
1080 * Handle the Readme file through the postamble, inclusive. Locate
1081 * the file with a subrequests. Process text/html documents by actually
1082 * running the subrequest; text/xxx documents get copied verbatim,
1083 * and any other content type is ignored. This means that a non-text
1084 * document (such as FOOTER.gif) might get multiviewed as the result
1085 * instead of a text document, meaning nothing will be displayed, but
1088 static void emit_tail(request_rec *r, char *readme_fname, int suppress_amble)
1090 apr_file_t *f = NULL;
1091 request_rec *rr = NULL;
1092 int suppress_post = 0;
1093 int suppress_sig = 0;
1096 * If there's a readme file, send a subrequest to look for it. If it's
1097 * found and a text file, handle it -- otherwise fall through and
1098 * pretend there's nothing there.
1100 if ((readme_fname != NULL)
1101 && (rr = ap_sub_req_lookup_uri(readme_fname, r, r->output_filters))
1102 && (rr->status == HTTP_OK)
1103 && (rr->filename != NULL)
1104 && rr->finfo.filetype == APR_REG) {
1106 * Check for the two specific cases we allow: text/html and
1107 * text/anything-else. The former is allowed to be processed for
1110 if (rr->content_type != NULL) {
1111 if (!strcasecmp(ap_field_noparam(r->pool, rr->content_type),
1114 for (f=rr->output_filters;
1115 f->frec != ap_subreq_core_filter_handle; f = f->next);
1116 f->next = r->output_filters;
1119 if (ap_run_sub_req(rr) == OK) {
1122 suppress_post = suppress_amble;
1125 else if (!strncasecmp("text/", rr->content_type, 5)) {
1127 * If we can open the file, suppress the signature.
1129 if (apr_file_open(&f, rr->filename, APR_READ,
1130 APR_OS_DEFAULT, r->pool) == APR_SUCCESS) {
1131 do_emit_plain(r, f);
1139 if (!suppress_sig) {
1140 ap_rputs(ap_psignature("", r), r);
1142 if (!suppress_post) {
1143 ap_rputs("</body></html>\n", r);
1146 ap_destroy_sub_req(rr);
1151 static char *find_title(request_rec *r)
1153 char titlebuf[MAX_STRING_LEN], *find = "<title>";
1154 apr_file_t *thefile = NULL;
1158 if (r->status != HTTP_OK) {
1161 if ((r->content_type != NULL)
1162 && (!strcasecmp(ap_field_noparam(r->pool, r->content_type),
1164 || !strcmp(r->content_type, INCLUDES_MAGIC_TYPE))
1165 && !r->content_encoding) {
1166 if (apr_file_open(&thefile, r->filename, APR_READ,
1167 APR_OS_DEFAULT, r->pool) != APR_SUCCESS) {
1170 n = sizeof(char) * (MAX_STRING_LEN - 1);
1171 apr_file_read(thefile, titlebuf, &n);
1173 apr_file_close(thefile);
1177 for (x = 0, p = 0; titlebuf[x]; x++) {
1178 if (apr_tolower(titlebuf[x]) == find[p]) {
1180 if ((p = ap_ind(&titlebuf[++x], '<')) != -1) {
1181 titlebuf[x + p] = '\0';
1183 /* Scan for line breaks for Tanmoy's secretary */
1184 for (y = x; titlebuf[y]; y++) {
1185 if ((titlebuf[y] == CR) || (titlebuf[y] == LF)) {
1194 apr_file_close(thefile);
1195 return apr_pstrdup(r->pool, &titlebuf[x]);
1202 apr_file_close(thefile);
1207 static struct ent *make_parent_entry(apr_int32_t autoindex_opts,
1208 autoindex_config_rec *d,
1209 request_rec *r, char keyid,
1212 struct ent *p = (struct ent *) apr_pcalloc(r->pool, sizeof(struct ent));
1215 * p->name is now the true parent URI.
1216 * testpath is a crafted lie, so that the syntax '/some/..'
1217 * (or simply '..')be used to describe 'up' from '/some/'
1218 * when processeing IndexIgnore, and Icon|Alt|Desc configs.
1221 /* The output has always been to the parent. Don't make ourself
1222 * our own parent (worthless cyclical reference).
1224 if (!(p->name = ap_make_full_path(r->pool, r->uri, "../"))) {
1227 ap_getparents(p->name);
1232 /* IndexIgnore has always compared "/thispath/.." */
1233 testpath = ap_make_full_path(r->pool, r->filename, "..");
1234 if (ignore_entry(d, testpath)) {
1240 p->key = apr_toupper(keyid);
1241 p->ascending = (apr_toupper(direction) == D_ASCENDING);
1242 p->version_sort = autoindex_opts & VERSION_SORT;
1243 if (autoindex_opts & FANCY_INDEXING) {
1244 if (!(p->icon = find_default_icon(d, testpath))) {
1245 p->icon = find_default_icon(d, "^^DIRECTORY^^");
1247 if (!(p->alt = find_default_alt(d, testpath))) {
1248 if (!(p->alt = find_default_alt(d, "^^DIRECTORY^^"))) {
1252 p->desc = find_desc(d, testpath);
1257 static struct ent *make_autoindex_entry(const apr_finfo_t *dirent,
1259 autoindex_config_rec *d,
1260 request_rec *r, char keyid,
1262 const char *pattern)
1267 /* Dot is ignored, Parent is handled by make_parent_entry() */
1268 if ((dirent->name[0] == '.') && (!dirent->name[1]
1269 || ((dirent->name[1] == '.') && !dirent->name[2])))
1272 #ifndef CASE_BLIND_FILESYSTEM
1273 if (pattern && (apr_fnmatch(pattern, dirent->name,
1274 FNM_NOESCAPE | FNM_PERIOD)
1277 #else /* !CASE_BLIND_FILESYSTEM */
1279 * On some platforms, the match must be case-blind. This is really
1280 * a factor of the filesystem involved, but we can't detect that
1281 * reliably - so we have to granularise at the OS level.
1283 if (pattern && (apr_fnmatch(pattern, dirent->name,
1284 FNM_NOESCAPE | FNM_PERIOD | FNM_CASE_BLIND)
1287 #endif /* !CASE_BLIND_FILESYSTEM */
1289 if (ignore_entry(d, ap_make_full_path(r->pool,
1290 r->filename, dirent->name))) {
1294 if (!(rr = ap_sub_req_lookup_dirent(dirent, r, AP_SUBREQ_NO_ARGS, NULL))) {
1298 if ((rr->finfo.filetype != APR_DIR && rr->finfo.filetype != APR_REG)
1299 || !(rr->status == OK || ap_is_HTTP_SUCCESS(rr->status)
1300 || ap_is_HTTP_REDIRECT(rr->status))) {
1301 ap_destroy_sub_req(rr);
1305 p = (struct ent *) apr_pcalloc(r->pool, sizeof(struct ent));
1306 if (dirent->filetype == APR_DIR) {
1307 p->name = apr_pstrcat(r->pool, dirent->name, "/", NULL);
1310 p->name = apr_pstrdup(r->pool, dirent->name);
1318 p->key = apr_toupper(keyid);
1319 p->ascending = (apr_toupper(direction) == D_ASCENDING);
1320 p->version_sort = !!(autoindex_opts & VERSION_SORT);
1321 p->ignore_case = !!(autoindex_opts & IGNORE_CASE);
1323 if (autoindex_opts & (FANCY_INDEXING | TABLE_INDEXING)) {
1324 p->lm = rr->finfo.mtime;
1325 if (dirent->filetype == APR_DIR) {
1326 if (autoindex_opts & FOLDERS_FIRST) {
1329 rr->filename = ap_make_dirstr_parent (rr->pool, rr->filename);
1331 /* omit the trailing slash (1.3 compat) */
1332 rr->filename[strlen(rr->filename) - 1] = '\0';
1334 if (!(p->icon = find_icon(d, rr, 1))) {
1335 p->icon = find_default_icon(d, "^^DIRECTORY^^");
1337 if (!(p->alt = find_alt(d, rr, 1))) {
1338 if (!(p->alt = find_default_alt(d, "^^DIRECTORY^^"))) {
1344 p->icon = find_icon(d, rr, 0);
1345 p->alt = find_alt(d, rr, 0);
1346 p->size = rr->finfo.size;
1349 p->desc = find_desc(d, rr->filename);
1351 if ((!p->desc) && (autoindex_opts & SCAN_HTML_TITLES)) {
1352 p->desc = apr_pstrdup(r->pool, find_title(rr));
1355 ap_destroy_sub_req(rr);
1357 * We don't need to take any special action for the file size key.
1358 * If we did, it would go here.
1360 if (keyid == K_LAST_MOD) {
1368 static char *terminate_description(autoindex_config_rec *d, char *desc,
1369 apr_int32_t autoindex_opts, int desc_width)
1371 int maxsize = desc_width;
1375 * If there's no DescriptionWidth in effect, default to the old
1376 * behaviour of adjusting the description size depending upon
1377 * what else is being displayed. Otherwise, stick with the
1380 if (d->desc_adjust == K_UNSET) {
1381 if (autoindex_opts & SUPPRESS_ICON) {
1384 if (autoindex_opts & SUPPRESS_LAST_MOD) {
1387 if (autoindex_opts & SUPPRESS_SIZE) {
1391 for (x = 0; desc[x] && ((maxsize > 0) || (desc[x] == '<')); x++) {
1392 if (desc[x] == '<') {
1393 while (desc[x] != '>') {
1401 else if (desc[x] == '&') {
1402 /* entities like ä count as one character */
1404 for ( ; desc[x] != ';'; ++x) {
1405 if (desc[x] == '\0') {
1415 if (!maxsize && desc[x] != '\0') {
1416 desc[x - 1] = '>'; /* Grump. */
1417 desc[x] = '\0'; /* Double Grump! */
1423 * Emit the anchor for the specified field. If a field is the key for the
1424 * current request, the link changes its meaning to reverse the order when
1425 * selected again. Non-active fields always start in ascending order.
1427 static void emit_link(request_rec *r, const char *anchor, char column,
1428 char curkey, char curdirection,
1429 const char *colargs, int nosort)
1442 qvalue[7] = ((curkey == column) && (curdirection == D_ASCENDING))
1443 ? D_DESCENDING : D_ASCENDING;
1445 ap_rvputs(r, "<a href=\"", qvalue, colargs ? colargs : "",
1446 "\">", anchor, "</a>", NULL);
1449 ap_rputs(anchor, r);
1453 static void output_directories(struct ent **ar, int n,
1454 autoindex_config_rec *d, request_rec *r,
1455 apr_int32_t autoindex_opts, char keyid,
1456 char direction, const char *colargs)
1460 char *name = r->uri;
1462 int static_columns = !!(autoindex_opts & SUPPRESS_COLSORT);
1463 apr_pool_t *scratch;
1468 char *breakrow = "";
1470 apr_pool_create(&scratch, r->pool);
1471 if (name[0] == '\0') {
1475 name_width = d->name_width;
1476 desc_width = d->desc_width;
1478 if ((autoindex_opts & (FANCY_INDEXING | TABLE_INDEXING))
1479 == FANCY_INDEXING) {
1480 if (d->name_adjust == K_ADJUST) {
1481 for (x = 0; x < n; x++) {
1482 int t = strlen(ar[x]->name);
1483 if (t > name_width) {
1489 if (d->desc_adjust == K_ADJUST) {
1490 for (x = 0; x < n; x++) {
1491 if (ar[x]->desc != NULL) {
1492 int t = strlen(ar[x]->desc);
1493 if (t > desc_width) {
1500 name_scratch = apr_palloc(r->pool, name_width + 1);
1501 pad_scratch = apr_palloc(r->pool, name_width + 1);
1502 memset(pad_scratch, ' ', name_width);
1503 pad_scratch[name_width] = '\0';
1505 if (autoindex_opts & TABLE_INDEXING) {
1507 ap_rputs("<table><tr>", r);
1508 if (!(autoindex_opts & SUPPRESS_ICON)) {
1509 ap_rputs("<th>", r);
1510 if ((tp = find_default_icon(d, "^^BLANKICON^^"))) {
1511 ap_rvputs(r, "<img src=\"", ap_escape_html(scratch, tp),
1512 "\" alt=\"[ICO]\"", NULL);
1513 if (d->icon_width) {
1514 ap_rprintf(r, " width=\"%d\"", d->icon_width);
1516 if (d->icon_height) {
1517 ap_rprintf(r, " height=\"%d\"", d->icon_height);
1520 if (autoindex_opts & EMIT_XHTML) {
1523 ap_rputs("></th>", r);
1526 ap_rputs(" </th>", r);
1531 ap_rputs("<th>", r);
1532 emit_link(r, "Name", K_NAME, keyid, direction,
1533 colargs, static_columns);
1534 if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1535 ap_rputs("</th><th>", r);
1536 emit_link(r, "Last modified", K_LAST_MOD, keyid, direction,
1537 colargs, static_columns);
1540 if (!(autoindex_opts & SUPPRESS_SIZE)) {
1541 ap_rputs("</th><th>", r);
1542 emit_link(r, "Size", K_SIZE, keyid, direction,
1543 colargs, static_columns);
1546 if (!(autoindex_opts & SUPPRESS_DESC)) {
1547 ap_rputs("</th><th>", r);
1548 emit_link(r, "Description", K_DESC, keyid, direction,
1549 colargs, static_columns);
1552 if (!(autoindex_opts & SUPPRESS_RULES)) {
1553 breakrow = apr_psprintf(r->pool,
1554 "<tr><th colspan=\"%d\">"
1555 "<hr%s></th></tr>\n", cols,
1556 (autoindex_opts & EMIT_XHTML) ? " /" : "");
1558 ap_rvputs(r, "</th></tr>", breakrow, NULL);
1560 else if (autoindex_opts & FANCY_INDEXING) {
1561 ap_rputs("<pre>", r);
1562 if (!(autoindex_opts & SUPPRESS_ICON)) {
1563 if ((tp = find_default_icon(d, "^^BLANKICON^^"))) {
1564 ap_rvputs(r, "<img src=\"", ap_escape_html(scratch, tp),
1565 "\" alt=\"Icon \"", NULL);
1566 if (d->icon_width) {
1567 ap_rprintf(r, " width=\"%d\"", d->icon_width);
1569 if (d->icon_height) {
1570 ap_rprintf(r, " height=\"%d\"", d->icon_height);
1573 if (autoindex_opts & EMIT_XHTML) {
1582 emit_link(r, "Name", K_NAME, keyid, direction,
1583 colargs, static_columns);
1584 ap_rputs(pad_scratch + 4, r);
1586 * Emit the guaranteed-at-least-one-space-between-columns byte.
1589 if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1590 emit_link(r, "Last modified", K_LAST_MOD, keyid, direction,
1591 colargs, static_columns);
1594 if (!(autoindex_opts & SUPPRESS_SIZE)) {
1595 emit_link(r, "Size", K_SIZE, keyid, direction,
1596 colargs, static_columns);
1599 if (!(autoindex_opts & SUPPRESS_DESC)) {
1600 emit_link(r, "Description", K_DESC, keyid, direction,
1601 colargs, static_columns);
1603 if (!(autoindex_opts & SUPPRESS_RULES)) {
1605 if (autoindex_opts & EMIT_XHTML) {
1615 ap_rputs("<ul>", r);
1618 for (x = 0; x < n; x++) {
1619 char *anchor, *t, *t2;
1622 apr_pool_clear(scratch);
1625 anchor = ap_escape_html(scratch, ap_os_escape_path(scratch, t, 0));
1627 if (!x && t[0] == '/') {
1628 t2 = "Parent Directory";
1634 if (autoindex_opts & TABLE_INDEXING) {
1635 ap_rputs("<tr>", r);
1636 if (!(autoindex_opts & SUPPRESS_ICON)) {
1637 ap_rputs("<td valign=\"top\">", r);
1638 if (autoindex_opts & ICONS_ARE_LINKS) {
1639 ap_rvputs(r, "<a href=\"", anchor, "\">", NULL);
1641 if ((ar[x]->icon) || d->default_icon) {
1642 ap_rvputs(r, "<img src=\"",
1643 ap_escape_html(scratch,
1644 ar[x]->icon ? ar[x]->icon
1646 "\" alt=\"[", (ar[x]->alt ? ar[x]->alt : " "),
1648 if (d->icon_width) {
1649 ap_rprintf(r, " width=\"%d\"", d->icon_width);
1651 if (d->icon_height) {
1652 ap_rprintf(r, " height=\"%d\"", d->icon_height);
1655 if (autoindex_opts & EMIT_XHTML) {
1661 ap_rputs(" ", r);
1663 if (autoindex_opts & ICONS_ARE_LINKS) {
1664 ap_rputs("</a></td>", r);
1667 ap_rputs("</td>", r);
1670 if (d->name_adjust == K_ADJUST) {
1671 ap_rvputs(r, "<td><a href=\"", anchor, "\">",
1672 ap_escape_html(scratch, t2), "</a>", NULL);
1675 nwidth = strlen(t2);
1676 if (nwidth > name_width) {
1677 memcpy(name_scratch, t2, name_width - 3);
1678 name_scratch[name_width - 3] = '.';
1679 name_scratch[name_width - 2] = '.';
1680 name_scratch[name_width - 1] = '>';
1681 name_scratch[name_width] = 0;
1683 nwidth = name_width;
1685 ap_rvputs(r, "<td><a href=\"", anchor, "\">",
1686 ap_escape_html(scratch, t2),
1687 "</a>", pad_scratch + nwidth, NULL);
1689 if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1690 if (ar[x]->lm != -1) {
1691 char time_str[MAX_STRING_LEN];
1693 apr_time_exp_lt(&ts, ar[x]->lm);
1694 apr_strftime(time_str, &rv, MAX_STRING_LEN,
1695 "</td><td align=\"right\">%d-%b-%Y %H:%M ",
1697 ap_rputs(time_str, r);
1700 ap_rputs("</td><td> ", r);
1703 if (!(autoindex_opts & SUPPRESS_SIZE)) {
1705 ap_rvputs(r, "</td><td align=\"right\">",
1706 apr_strfsize(ar[x]->size, buf), NULL);
1708 if (!(autoindex_opts & SUPPRESS_DESC)) {
1710 if (d->desc_adjust == K_ADJUST) {
1711 ap_rvputs(r, "</td><td>", ar[x]->desc, NULL);
1714 ap_rvputs(r, "</td><td>",
1715 terminate_description(d, ar[x]->desc,
1722 ap_rputs("</td><td> ", r);
1724 ap_rputs("</td></tr>\n", r);
1726 else if (autoindex_opts & FANCY_INDEXING) {
1727 if (!(autoindex_opts & SUPPRESS_ICON)) {
1728 if (autoindex_opts & ICONS_ARE_LINKS) {
1729 ap_rvputs(r, "<a href=\"", anchor, "\">", NULL);
1731 if ((ar[x]->icon) || d->default_icon) {
1732 ap_rvputs(r, "<img src=\"",
1733 ap_escape_html(scratch,
1734 ar[x]->icon ? ar[x]->icon
1736 "\" alt=\"[", (ar[x]->alt ? ar[x]->alt : " "),
1738 if (d->icon_width) {
1739 ap_rprintf(r, " width=\"%d\"", d->icon_width);
1741 if (d->icon_height) {
1742 ap_rprintf(r, " height=\"%d\"", d->icon_height);
1745 if (autoindex_opts & EMIT_XHTML) {
1753 if (autoindex_opts & ICONS_ARE_LINKS) {
1754 ap_rputs("</a> ", r);
1760 nwidth = strlen(t2);
1761 if (nwidth > name_width) {
1762 memcpy(name_scratch, t2, name_width - 3);
1763 name_scratch[name_width - 3] = '.';
1764 name_scratch[name_width - 2] = '.';
1765 name_scratch[name_width - 1] = '>';
1766 name_scratch[name_width] = 0;
1768 nwidth = name_width;
1770 ap_rvputs(r, "<a href=\"", anchor, "\">",
1771 ap_escape_html(scratch, t2),
1772 "</a>", pad_scratch + nwidth, NULL);
1774 * The blank before the storm.. er, before the next field.
1777 if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1778 if (ar[x]->lm != -1) {
1779 char time_str[MAX_STRING_LEN];
1781 apr_time_exp_lt(&ts, ar[x]->lm);
1782 apr_strftime(time_str, &rv, MAX_STRING_LEN,
1783 "%d-%b-%Y %H:%M ", &ts);
1784 ap_rputs(time_str, r);
1787 /*Length="22-Feb-1998 23:42 " (see 4 lines above) */
1791 if (!(autoindex_opts & SUPPRESS_SIZE)) {
1793 ap_rputs(apr_strfsize(ar[x]->size, buf), r);
1796 if (!(autoindex_opts & SUPPRESS_DESC)) {
1798 ap_rputs(terminate_description(d, ar[x]->desc,
1806 ap_rvputs(r, "<li><a href=\"", anchor, "\"> ", t2,
1807 "</a></li>\n", NULL);
1810 if (autoindex_opts & TABLE_INDEXING) {
1811 ap_rvputs(r, breakrow, "</table>\n", NULL);
1813 else if (autoindex_opts & FANCY_INDEXING) {
1814 if (!(autoindex_opts & SUPPRESS_RULES)) {
1816 if (autoindex_opts & EMIT_XHTML) {
1819 ap_rputs("></pre>\n", r);
1822 ap_rputs("</pre>\n", r);
1826 ap_rputs("</ul>\n", r);
1831 * Compare two file entries according to the sort criteria. The return
1832 * is essentially a signum function value.
1835 static int dsortf(struct ent **e1, struct ent **e2)
1842 * First, see if either of the entries is for the parent directory.
1843 * If so, that *always* sorts lower than anything else.
1845 if ((*e1)->name[0] == '/') {
1848 if ((*e2)->name[0] == '/') {
1852 * Now see if one's a directory and one isn't, if we're set
1853 * isdir for FOLDERS_FIRST.
1855 if ((*e1)->isdir != (*e2)->isdir) {
1856 return (*e1)->isdir ? -1 : 1;
1859 * All of our comparisons will be of the c1 entry against the c2 one,
1860 * so assign them appropriately to take care of the ordering.
1862 if ((*e1)->ascending) {
1873 if (c1->lm > c2->lm) {
1876 else if (c1->lm < c2->lm) {
1881 if (c1->size > c2->size) {
1884 else if (c1->size < c2->size) {
1889 if (c1->version_sort) {
1890 result = apr_strnatcmp(c1->desc ? c1->desc : "",
1891 c2->desc ? c2->desc : "");
1894 result = strcmp(c1->desc ? c1->desc : "",
1895 c2->desc ? c2->desc : "");
1903 /* names may identical when treated case-insensitively,
1904 * so always fall back on strcmp() flavors to put entries
1905 * in deterministic order. This means that 'ABC' and 'abc'
1906 * will always appear in the same order, rather than
1907 * variably between 'ABC abc' and 'abc ABC' order.
1910 if (c1->version_sort) {
1911 if (c1->ignore_case) {
1912 result = apr_strnatcasecmp (c1->name, c2->name);
1915 result = apr_strnatcmp(c1->name, c2->name);
1919 /* The names may be identical in respects other other than
1920 * filename case when strnatcmp is used above, so fall back
1921 * to strcmp on conflicts so that fn1.01.zzz and fn1.1.zzz
1922 * are also sorted in a deterministic order.
1925 if (!result && c1->ignore_case) {
1926 result = strcasecmp (c1->name, c2->name);
1929 result = strcmp (c1->name, c2->name);
1935 static int index_directory(request_rec *r,
1936 autoindex_config_rec *autoindex_conf)
1938 char *title_name = ap_escape_html(r->pool, r->uri);
1940 char *name = r->filename;
1941 char *pstring = NULL;
1944 apr_status_t status;
1946 struct ent *head, *p;
1947 struct ent **ar = NULL;
1948 const char *qstring;
1949 apr_int32_t autoindex_opts = autoindex_conf->opts;
1954 apr_size_t dirpathlen;
1955 char *ctype = "text/html";
1958 if ((status = apr_dir_open(&thedir, name, r->pool)) != APR_SUCCESS) {
1959 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
1960 "Can't open directory for index: %s", r->filename);
1961 return HTTP_FORBIDDEN;
1964 if (autoindex_conf->ctype) {
1965 ctype = autoindex_conf->ctype;
1967 if (autoindex_conf->charset) {
1968 charset = autoindex_conf->charset;
1971 #if APR_HAS_UNICODE_FS
1974 charset = "ISO-8859-1";
1978 ap_set_content_type(r, apr_pstrcat(r->pool, ctype, ";charset=",
1982 ap_set_content_type(r, ctype);
1985 if (autoindex_opts & TRACK_MODIFIED) {
1986 ap_update_mtime(r, r->finfo.mtime);
1987 ap_set_last_modified(r);
1990 if (r->header_only) {
1991 apr_dir_close(thedir);
1996 * If there is no specific ordering defined for this directory,
1997 * default to ascending by filename.
1999 keyid = autoindex_conf->default_keyid
2000 ? autoindex_conf->default_keyid : K_NAME;
2001 direction = autoindex_conf->default_direction
2002 ? autoindex_conf->default_direction : D_ASCENDING;
2005 * Figure out what sort of indexing (if any) we're supposed to use.
2007 * If no QUERY_STRING was specified or client query strings have been
2008 * explicitly disabled.
2009 * If we are ignoring the client, suppress column sorting as well.
2011 if (autoindex_opts & IGNORE_CLIENT) {
2013 autoindex_opts |= SUPPRESS_COLSORT;
2017 char fval[5], vval[5], *ppre = "", *epattern = "";
2018 fval[0] = '\0'; vval[0] = '\0';
2021 while (qstring && *qstring) {
2022 if (qstring[0] == 'C' && qstring[1] == '='
2023 && qstring[2] && strchr(K_VALID, qstring[2])
2024 && (qstring[3] == '&' || qstring[3] == ';'
2027 qstring += qstring[3] ? 4 : 3;
2029 else if (qstring[0] == 'O' && qstring[1] == '='
2030 && ((qstring[2] == D_ASCENDING)
2031 || (qstring[2] == D_DESCENDING))
2032 && (qstring[3] == '&' || qstring[3] == ';'
2034 direction = qstring[2];
2035 qstring += qstring[3] ? 4 : 3;
2037 else if (qstring[0] == 'F' && qstring[1] == '='
2038 && qstring[2] && strchr("012", qstring[2])
2039 && (qstring[3] == '&' || qstring[3] == ';'
2041 if (qstring[2] == '0') {
2042 autoindex_opts &= ~(FANCY_INDEXING | TABLE_INDEXING);
2044 else if (qstring[2] == '1') {
2045 autoindex_opts = (autoindex_opts | FANCY_INDEXING)
2048 else if (qstring[2] == '2') {
2049 autoindex_opts |= FANCY_INDEXING | TABLE_INDEXING;
2051 strcpy(fval, ";F= ");
2052 fval[3] = qstring[2];
2053 qstring += qstring[3] ? 4 : 3;
2055 else if (qstring[0] == 'V' && qstring[1] == '='
2056 && (qstring[2] == '0' || qstring[2] == '1')
2057 && (qstring[3] == '&' || qstring[3] == ';'
2059 if (qstring[2] == '0') {
2060 autoindex_opts &= ~VERSION_SORT;
2062 else if (qstring[2] == '1') {
2063 autoindex_opts |= VERSION_SORT;
2065 strcpy(vval, ";V= ");
2066 vval[3] = qstring[2];
2067 qstring += qstring[3] ? 4 : 3;
2069 else if (qstring[0] == 'P' && qstring[1] == '=') {
2070 const char *eos = qstring += 2; /* for efficiency */
2072 while (*eos && *eos != '&' && *eos != ';') {
2076 if (eos == qstring) {
2080 pstring = apr_pstrndup(r->pool, qstring, eos - qstring);
2081 if (ap_unescape_url(pstring) != OK) {
2082 /* ignore the pattern, if it's bad. */
2088 epattern = ap_escape_uri(r->pool, pstring);
2092 if (*eos && *++eos) {
2099 else { /* Syntax error? Ignore the remainder! */
2103 colargs = apr_pstrcat(r->pool, fval, vval, ppre, epattern, NULL);
2106 /* Spew HTML preamble */
2107 title_endp = title_name + strlen(title_name) - 1;
2109 while (title_endp > title_name && *title_endp == '/') {
2110 *title_endp-- = '\0';
2113 emit_head(r, find_header(autoindex_conf, r),
2114 autoindex_opts & SUPPRESS_PREAMBLE,
2115 autoindex_opts & EMIT_XHTML, title_name);
2118 * Since we don't know how many dir. entries there are, put them into a
2119 * linked list and then arrayificate them so qsort can use them.
2122 p = make_parent_entry(autoindex_opts, autoindex_conf, r, keyid, direction);
2128 fullpath = apr_palloc(r->pool, APR_PATH_MAX);
2129 dirpathlen = strlen(name);
2130 memcpy(fullpath, name, dirpathlen);
2133 status = apr_dir_read(&dirent, APR_FINFO_MIN | APR_FINFO_NAME, thedir);
2134 if (APR_STATUS_IS_INCOMPLETE(status)) {
2135 continue; /* ignore un-stat()able files */
2137 else if (status != APR_SUCCESS) {
2141 /* We want to explode symlinks here. */
2142 if (dirent.filetype == APR_LNK) {
2143 const char *savename;
2145 /* We *must* have FNAME. */
2146 savename = dirent.name;
2147 apr_cpystrn(fullpath + dirpathlen, dirent.name,
2148 APR_PATH_MAX - dirpathlen);
2149 status = apr_stat(&fi, fullpath,
2150 dirent.valid & ~(APR_FINFO_NAME), r->pool);
2151 if (status != APR_SUCCESS) {
2152 /* Something bad happened, skip this file. */
2155 memcpy(&dirent, &fi, sizeof(fi));
2156 dirent.name = savename;
2157 dirent.valid |= APR_FINFO_NAME;
2159 p = make_autoindex_entry(&dirent, autoindex_opts, autoindex_conf, r,
2160 keyid, direction, pstring);
2169 ar = (struct ent **) apr_palloc(r->pool,
2170 num_ent * sizeof(struct ent *));
2178 qsort((void *) ar, num_ent, sizeof(struct ent *),
2179 (int (*)(const void *, const void *)) dsortf);
2181 output_directories(ar, num_ent, autoindex_conf, r, autoindex_opts,
2182 keyid, direction, colargs);
2183 apr_dir_close(thedir);
2185 emit_tail(r, find_readme(autoindex_conf, r),
2186 autoindex_opts & SUPPRESS_PREAMBLE);
2191 /* The formal handler... */
2193 static int handle_autoindex(request_rec *r)
2195 autoindex_config_rec *d;
2198 if(strcmp(r->handler,DIR_MAGIC_TYPE)) {
2202 allow_opts = ap_allow_options(r);
2204 d = (autoindex_config_rec *) ap_get_module_config(r->per_dir_config,
2207 r->allowed |= (AP_METHOD_BIT << M_GET);
2208 if (r->method_number != M_GET) {
2212 /* OK, nothing easy. Trot out the heavy artillery... */
2214 if (allow_opts & OPT_INDEXES) {
2217 if ((errstatus = ap_discard_request_body(r)) != OK) {
2221 /* KLUDGE --- make the sub_req lookups happen in the right directory.
2222 * Fixing this in the sub_req_lookup functions themselves is difficult,
2223 * and would probably break virtual includes...
2226 if (r->filename[strlen(r->filename) - 1] != '/') {
2227 r->filename = apr_pstrcat(r->pool, r->filename, "/", NULL);
2229 return index_directory(r, d);
2232 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
2233 "Directory index forbidden by rule: %s", r->filename);
2234 return HTTP_FORBIDDEN;
2238 static void register_hooks(apr_pool_t *p)
2240 ap_hook_handler(handle_autoindex,NULL,NULL,APR_HOOK_MIDDLE);
2243 module AP_MODULE_DECLARE_DATA autoindex_module =
2245 STANDARD20_MODULE_STUFF,
2246 create_autoindex_config, /* dir config creater */
2247 merge_autoindex_configs, /* dir merger --- default is to override */
2248 NULL, /* server config */
2249 NULL, /* merge server config */
2250 autoindex_cmds, /* command apr_table_t */
2251 register_hooks /* register hooks */