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;
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
autoindex_config_rec *d;
158
d = (autoindex_config_rec *) ap_get_module_config(r->per_dir_config,
161
ap_rvputs(r, xhtml ? DOCTYPE_XHTML_1_0T : DOCTYPE_HTML_3_2,
162
"<html>\n <head>\n <title>Index of ", title,
164
if (d->style_sheet != NULL) {
165
ap_rvputs(r, " <link rel=\"stylesheet\" href=\"", d->style_sheet,
166
"\" type=\"text/css\"", xhtml ? " />\n" : ">\n", NULL);
168
ap_rvputs(r, " </head>\n <body>\n", NULL);
171
static void push_item(apr_array_header_t *arr, char *type, const char *to,
172
const char *path, const char *data)
174
struct item *p = (struct item *) apr_array_push(arr);
184
p->data = data ? apr_pstrdup(arr->pool, data) : NULL;
185
p->apply_path = apr_pstrcat(arr->pool, path, "*", NULL);
187
if ((type == BY_PATH) && (!ap_is_matchexp(to))) {
188
p->apply_to = apr_pstrcat(arr->pool, "*", to, NULL);
191
p->apply_to = apr_pstrdup(arr->pool, to);
198
static const char *add_alt(cmd_parms *cmd, void *d, const char *alt,
201
if (cmd->info == BY_PATH) {
202
if (!strcmp(to, "**DIRECTORY**")) {
203
to = "^^DIRECTORY^^";
206
if (cmd->info == BY_ENCODING) {
207
char *tmp = apr_pstrdup(cmd->pool, to);
212
push_item(((autoindex_config_rec *) d)->alt_list, cmd->info, to,
217
static const char *add_icon(cmd_parms *cmd, void *d, const char *icon,
220
char *iconbak = apr_pstrdup(cmd->pool, icon);
222
if (icon[0] == '(') {
224
char *cl = strchr(iconbak, ')');
227
return "missing closing paren";
229
alt = ap_getword_nc(cmd->pool, &iconbak, ',');
230
*cl = '\0'; /* Lose closing paren */
231
add_alt(cmd, d, &alt[1], to);
233
if (cmd->info == BY_PATH) {
234
if (!strcmp(to, "**DIRECTORY**")) {
235
to = "^^DIRECTORY^^";
238
if (cmd->info == BY_ENCODING) {
239
char *tmp = apr_pstrdup(cmd->pool, to);
244
push_item(((autoindex_config_rec *) d)->icon_list, cmd->info, to,
250
* Add description text for a filename pattern. If the pattern has
251
* wildcards already (or we need to add them), add leading and
252
* trailing wildcards to it to ensure substring processing. If the
253
* pattern contains a '/' anywhere, force wildcard matching mode,
254
* add a slash to the prefix so that "bar/bletch" won't be matched
255
* by "foobar/bletch", and make a note that there's a delimiter;
256
* the matching routine simplifies to just the actual filename
257
* whenever it can. This allows definitions in parent directories
258
* to be made for files in subordinate ones using relative paths.
262
* Absent a strcasestr() function, we have to force wildcards on
263
* systems for which "AAA" and "aaa" mean the same file.
265
#ifdef CASE_BLIND_FILESYSTEM
266
#define WILDCARDS_REQUIRED 1
268
#define WILDCARDS_REQUIRED 0
271
static const char *add_desc(cmd_parms *cmd, void *d, const char *desc,
274
autoindex_config_rec *dcfg = (autoindex_config_rec *) d;
275
ai_desc_t *desc_entry;
278
desc_entry = (ai_desc_t *) apr_array_push(dcfg->desc_list);
279
desc_entry->full_path = (ap_strchr_c(to, '/') == NULL) ? 0 : 1;
280
desc_entry->wildcards = (WILDCARDS_REQUIRED
281
|| desc_entry->full_path
282
|| apr_fnmatch_test(to));
283
if (desc_entry->wildcards) {
284
prefix = desc_entry->full_path ? "*/" : "*";
285
desc_entry->pattern = apr_pstrcat(dcfg->desc_list->pool,
286
prefix, to, "*", NULL);
289
desc_entry->pattern = apr_pstrdup(dcfg->desc_list->pool, to);
291
desc_entry->description = apr_pstrdup(dcfg->desc_list->pool, desc);
295
static const char *add_ignore(cmd_parms *cmd, void *d, const char *ext)
297
push_item(((autoindex_config_rec *) d)->ign_list, 0, ext, cmd->path, NULL);
301
static const char *add_header(cmd_parms *cmd, void *d, const char *name)
303
push_item(((autoindex_config_rec *) d)->hdr_list, 0, NULL, cmd->path,
308
static const char *add_readme(cmd_parms *cmd, void *d, const char *name)
310
push_item(((autoindex_config_rec *) d)->rdme_list, 0, NULL, cmd->path,
315
static const char *add_opts(cmd_parms *cmd, void *d, int argc, char *const argv[])
320
apr_int32_t opts_add;
321
apr_int32_t opts_remove;
323
autoindex_config_rec *d_cfg = (autoindex_config_rec *) d;
326
opts_add = d_cfg->incremented_opts;
327
opts_remove = d_cfg->decremented_opts;
329
for (i = 0; i < argc; i++) {
333
if ((*w == '+') || (*w == '-')) {
339
if (!strcasecmp(w, "FancyIndexing")) {
340
option = FANCY_INDEXING;
342
else if (!strcasecmp(w, "FoldersFirst")) {
343
option = FOLDERS_FIRST;
345
else if (!strcasecmp(w, "HTMLTable")) {
346
option = TABLE_INDEXING;
348
else if (!strcasecmp(w, "IconsAreLinks")) {
349
option = ICONS_ARE_LINKS;
351
else if (!strcasecmp(w, "IgnoreCase")) {
352
option = IGNORE_CASE;
354
else if (!strcasecmp(w, "IgnoreClient")) {
355
option = IGNORE_CLIENT;
357
else if (!strcasecmp(w, "ScanHTMLTitles")) {
358
option = SCAN_HTML_TITLES;
360
else if (!strcasecmp(w, "SuppressColumnSorting")) {
361
option = SUPPRESS_COLSORT;
363
else if (!strcasecmp(w, "SuppressDescription")) {
364
option = SUPPRESS_DESC;
366
else if (!strcasecmp(w, "SuppressHTMLPreamble")) {
367
option = SUPPRESS_PREAMBLE;
369
else if (!strcasecmp(w, "SuppressIcon")) {
370
option = SUPPRESS_ICON;
372
else if (!strcasecmp(w, "SuppressLastModified")) {
373
option = SUPPRESS_LAST_MOD;
375
else if (!strcasecmp(w, "SuppressSize")) {
376
option = SUPPRESS_SIZE;
378
else if (!strcasecmp(w, "SuppressRules")) {
379
option = SUPPRESS_RULES;
381
else if (!strcasecmp(w, "TrackModified")) {
382
option = TRACK_MODIFIED;
384
else if (!strcasecmp(w, "VersionSort")) {
385
option = VERSION_SORT;
387
else if (!strcasecmp(w, "XHTML")) {
390
else if (!strcasecmp(w, "ShowForbidden")) {
391
option = SHOW_FORBIDDEN;
393
else if (!strcasecmp(w, "None")) {
394
if (action != '\0') {
395
return "Cannot combine '+' or '-' with 'None' keyword";
401
else if (!strcasecmp(w, "IconWidth")) {
403
d_cfg->icon_width = DEFAULT_ICON_WIDTH;
406
d_cfg->icon_width = 0;
409
else if (!strncasecmp(w, "IconWidth=", 10)) {
411
return "Cannot combine '-' with IconWidth=n";
413
d_cfg->icon_width = atoi(&w[10]);
415
else if (!strcasecmp(w, "IconHeight")) {
417
d_cfg->icon_height = DEFAULT_ICON_HEIGHT;
420
d_cfg->icon_height = 0;
423
else if (!strncasecmp(w, "IconHeight=", 11)) {
425
return "Cannot combine '-' with IconHeight=n";
427
d_cfg->icon_height = atoi(&w[11]);
429
else if (!strcasecmp(w, "NameWidth")) {
431
return "NameWidth with no value may only appear as "
434
d_cfg->name_width = DEFAULT_NAME_WIDTH;
435
d_cfg->name_adjust = K_NOADJUST;
437
else if (!strncasecmp(w, "NameWidth=", 10)) {
439
return "Cannot combine '-' with NameWidth=n";
442
d_cfg->name_adjust = K_ADJUST;
445
int width = atoi(&w[10]);
447
if (width && (width < 5)) {
448
return "NameWidth value must be greater than 5";
450
d_cfg->name_width = width;
451
d_cfg->name_adjust = K_NOADJUST;
454
else if (!strcasecmp(w, "DescriptionWidth")) {
456
return "DescriptionWidth with no value may only appear as "
457
"'-DescriptionWidth'";
459
d_cfg->desc_width = DEFAULT_DESC_WIDTH;
460
d_cfg->desc_adjust = K_NOADJUST;
462
else if (!strncasecmp(w, "DescriptionWidth=", 17)) {
464
return "Cannot combine '-' with DescriptionWidth=n";
467
d_cfg->desc_adjust = K_ADJUST;
470
int width = atoi(&w[17]);
472
if (width && (width < 12)) {
473
return "DescriptionWidth value must be greater than 12";
475
d_cfg->desc_width = width;
476
d_cfg->desc_adjust = K_NOADJUST;
480
return "Invalid directory indexing option";
482
if (action == '\0') {
487
else if (action == '+') {
489
opts_remove &= ~option;
492
opts_remove |= option;
496
if ((opts & NO_OPTIONS) && (opts & ~NO_OPTIONS)) {
497
return "Cannot combine other IndexOptions keywords with 'None'";
499
d_cfg->incremented_opts = opts_add;
500
d_cfg->decremented_opts = opts_remove;
505
static const char *set_default_order(cmd_parms *cmd, void *m,
506
const char *direction, const char *key)
508
autoindex_config_rec *d_cfg = (autoindex_config_rec *) m;
510
if (!strcasecmp(direction, "Ascending")) {
511
d_cfg->default_direction = D_ASCENDING;
513
else if (!strcasecmp(direction, "Descending")) {
514
d_cfg->default_direction = D_DESCENDING;
517
return "First keyword must be 'Ascending' or 'Descending'";
520
if (!strcasecmp(key, "Name")) {
521
d_cfg->default_keyid = K_NAME;
523
else if (!strcasecmp(key, "Date")) {
524
d_cfg->default_keyid = K_LAST_MOD;
526
else if (!strcasecmp(key, "Size")) {
527
d_cfg->default_keyid = K_SIZE;
529
else if (!strcasecmp(key, "Description")) {
530
d_cfg->default_keyid = K_DESC;
533
return "Second keyword must be 'Name', 'Date', 'Size', or "
540
#define DIR_CMD_PERMS OR_INDEXES
542
static const command_rec autoindex_cmds[] =
544
AP_INIT_ITERATE2("AddIcon", add_icon, BY_PATH, DIR_CMD_PERMS,
545
"an icon URL followed by one or more filenames"),
546
AP_INIT_ITERATE2("AddIconByType", add_icon, BY_TYPE, DIR_CMD_PERMS,
547
"an icon URL followed by one or more MIME types"),
548
AP_INIT_ITERATE2("AddIconByEncoding", add_icon, BY_ENCODING, DIR_CMD_PERMS,
549
"an icon URL followed by one or more content encodings"),
550
AP_INIT_ITERATE2("AddAlt", add_alt, BY_PATH, DIR_CMD_PERMS,
551
"alternate descriptive text followed by one or more "
553
AP_INIT_ITERATE2("AddAltByType", add_alt, BY_TYPE, DIR_CMD_PERMS,
554
"alternate descriptive text followed by one or more MIME "
556
AP_INIT_ITERATE2("AddAltByEncoding", add_alt, BY_ENCODING, DIR_CMD_PERMS,
557
"alternate descriptive text followed by one or more "
558
"content encodings"),
559
AP_INIT_TAKE_ARGV("IndexOptions", add_opts, NULL, DIR_CMD_PERMS,
560
"one or more index options [+|-][]"),
561
AP_INIT_TAKE2("IndexOrderDefault", set_default_order, NULL, DIR_CMD_PERMS,
562
"{Ascending,Descending} {Name,Size,Description,Date}"),
563
AP_INIT_ITERATE("IndexIgnore", add_ignore, NULL, DIR_CMD_PERMS,
564
"one or more file extensions"),
565
AP_INIT_ITERATE2("AddDescription", add_desc, BY_PATH, DIR_CMD_PERMS,
566
"Descriptive text followed by one or more filenames"),
567
AP_INIT_TAKE1("HeaderName", add_header, NULL, DIR_CMD_PERMS,
569
AP_INIT_TAKE1("ReadmeName", add_readme, NULL, DIR_CMD_PERMS,
571
AP_INIT_RAW_ARGS("FancyIndexing", ap_set_deprecated, NULL, OR_ALL,
572
"The FancyIndexing directive is no longer supported. "
573
"Use IndexOptions FancyIndexing."),
574
AP_INIT_TAKE1("DefaultIcon", ap_set_string_slot,
575
(void *)APR_OFFSETOF(autoindex_config_rec, default_icon),
576
DIR_CMD_PERMS, "an icon URL"),
577
AP_INIT_TAKE1("IndexStyleSheet", ap_set_string_slot,
578
(void *)APR_OFFSETOF(autoindex_config_rec, style_sheet),
579
DIR_CMD_PERMS, "URL to style sheet"),
583
static void *create_autoindex_config(apr_pool_t *p, char *dummy)
585
autoindex_config_rec *new =
586
(autoindex_config_rec *) apr_pcalloc(p, sizeof(autoindex_config_rec));
589
new->icon_height = 0;
590
new->name_width = DEFAULT_NAME_WIDTH;
591
new->name_adjust = K_UNSET;
592
new->desc_width = DEFAULT_DESC_WIDTH;
593
new->desc_adjust = K_UNSET;
594
new->icon_list = apr_array_make(p, 4, sizeof(struct item));
595
new->alt_list = apr_array_make(p, 4, sizeof(struct item));
596
new->desc_list = apr_array_make(p, 4, sizeof(ai_desc_t));
597
new->ign_list = apr_array_make(p, 4, sizeof(struct item));
598
new->hdr_list = apr_array_make(p, 4, sizeof(struct item));
599
new->rdme_list = apr_array_make(p, 4, sizeof(struct item));
601
new->incremented_opts = 0;
602
new->decremented_opts = 0;
603
new->default_keyid = '\0';
604
new->default_direction = '\0';
609
static void *merge_autoindex_configs(apr_pool_t *p, void *basev, void *addv)
611
autoindex_config_rec *new;
612
autoindex_config_rec *base = (autoindex_config_rec *) basev;
613
autoindex_config_rec *add = (autoindex_config_rec *) addv;
615
new = (autoindex_config_rec *) apr_pcalloc(p, sizeof(autoindex_config_rec));
616
new->default_icon = add->default_icon ? add->default_icon
617
: base->default_icon;
618
new->style_sheet = add->style_sheet ? add->style_sheet
620
new->icon_height = add->icon_height ? add->icon_height : base->icon_height;
621
new->icon_width = add->icon_width ? add->icon_width : base->icon_width;
623
new->alt_list = apr_array_append(p, add->alt_list, base->alt_list);
624
new->ign_list = apr_array_append(p, add->ign_list, base->ign_list);
625
new->hdr_list = apr_array_append(p, add->hdr_list, base->hdr_list);
626
new->desc_list = apr_array_append(p, add->desc_list, base->desc_list);
627
new->icon_list = apr_array_append(p, add->icon_list, base->icon_list);
628
new->rdme_list = apr_array_append(p, add->rdme_list, base->rdme_list);
629
if (add->opts & NO_OPTIONS) {
631
* If the current directory says 'no options' then we also
632
* clear any incremental mods from being inheritable further down.
634
new->opts = NO_OPTIONS;
635
new->incremented_opts = 0;
636
new->decremented_opts = 0;
640
* If there were any nonincremental options selected for
641
* this directory, they dominate and we don't inherit *anything.*
642
* Contrariwise, we *do* inherit if the only settings here are
645
if (add->opts == 0) {
646
new->incremented_opts = (base->incremented_opts
647
| add->incremented_opts)
648
& ~add->decremented_opts;
649
new->decremented_opts = (base->decremented_opts
650
| add->decremented_opts);
652
* We may have incremental settings, so make sure we don't
653
* inadvertently inherit an IndexOptions None from above.
655
new->opts = (base->opts & ~NO_OPTIONS);
659
* There are local nonincremental settings, which clear
660
* all inheritance from above. They *are* the new base settings.
662
new->opts = add->opts;;
665
* We're guaranteed that there'll be no overlap between
666
* the add-options and the remove-options.
668
new->opts |= new->incremented_opts;
669
new->opts &= ~new->decremented_opts;
672
* Inherit the NameWidth settings if there aren't any specific to
673
* the new location; otherwise we'll end up using the defaults set in the
674
* config-rec creation routine.
676
if (add->name_adjust == K_UNSET) {
677
new->name_width = base->name_width;
678
new->name_adjust = base->name_adjust;
681
new->name_width = add->name_width;
682
new->name_adjust = add->name_adjust;
686
* Likewise for DescriptionWidth.
688
if (add->desc_adjust == K_UNSET) {
689
new->desc_width = base->desc_width;
690
new->desc_adjust = base->desc_adjust;
693
new->desc_width = add->desc_width;
694
new->desc_adjust = add->desc_adjust;
697
new->default_keyid = add->default_keyid ? add->default_keyid
698
: base->default_keyid;
699
new->default_direction = add->default_direction ? add->default_direction
700
: base->default_direction;
704
/****************************************************************
706
* Looking things up in config entries...
709
/* Structure used to hold entries when we're actually building an index */
719
int ascending, ignore_case, version_sort;
724
static char *find_item(request_rec *r, apr_array_header_t *list, int path_only)
726
const char *content_type = ap_field_noparam(r->pool, r->content_type);
727
const char *content_encoding = r->content_encoding;
728
char *path = r->filename;
730
struct item *items = (struct item *) list->elts;
733
for (i = 0; i < list->nelts; ++i) {
734
struct item *p = &items[i];
736
/* Special cased for ^^DIRECTORY^^ and ^^BLANKICON^^ */
737
if ((path[0] == '^') || (!ap_strcmp_match(path, p->apply_path))) {
738
if (!*(p->apply_to)) {
741
else if (p->type == BY_PATH || path[0] == '^') {
742
if (!ap_strcmp_match(path, p->apply_to)) {
746
else if (!path_only) {
747
if (!content_encoding) {
748
if (p->type == BY_TYPE) {
750
&& !ap_strcasecmp_match(content_type,
757
if (p->type == BY_ENCODING) {
758
if (!ap_strcasecmp_match(content_encoding,
770
#define find_icon(d,p,t) find_item(p,d->icon_list,t)
771
#define find_alt(d,p,t) find_item(p,d->alt_list,t)
772
#define find_header(d,p) find_item(p,d->hdr_list,0)
773
#define find_readme(d,p) find_item(p,d->rdme_list,0)
775
static char *find_default_item(char *bogus_name, apr_array_header_t *list)
778
/* Bleah. I tried to clean up find_item, and it lead to this bit
779
* of ugliness. Note that the fields initialized are precisely
780
* those that find_item looks at...
782
r.filename = bogus_name;
783
r.content_type = r.content_encoding = NULL;
784
return find_item(&r, list, 1);
787
#define find_default_icon(d,n) find_default_item(n, d->icon_list)
788
#define find_default_alt(d,n) find_default_item(n, d->alt_list)
791
* Look through the list of pattern/description pairs and return the first one
792
* if any) that matches the filename in the request. If multiple patterns
793
* match, only the first one is used; since the order in the array is the
794
* same as the order in which directives were processed, earlier matching
795
* directives will dominate.
798
#ifdef CASE_BLIND_FILESYSTEM
799
#define MATCH_FLAGS APR_FNM_CASE_BLIND
801
#define MATCH_FLAGS 0
804
static char *find_desc(autoindex_config_rec *dcfg, const char *filename_full)
807
ai_desc_t *list = (ai_desc_t *) dcfg->desc_list->elts;
808
const char *filename_only;
809
const char *filename;
812
* If the filename includes a path, extract just the name itself
813
* for the simple matches.
815
if ((filename_only = ap_strrchr_c(filename_full, '/')) == NULL) {
816
filename_only = filename_full;
821
for (i = 0; i < dcfg->desc_list->nelts; ++i) {
822
ai_desc_t *tuple = &list[i];
826
* Only use the full-path filename if the pattern contains '/'s.
828
filename = (tuple->full_path) ? filename_full : filename_only;
830
* Make the comparison using the cheapest method; only do
831
* wildcard checking if we must.
833
if (tuple->wildcards) {
834
found = (apr_fnmatch(tuple->pattern, filename, MATCH_FLAGS) == 0);
837
found = (ap_strstr_c(filename, tuple->pattern) != NULL);
840
return tuple->description;
846
static int ignore_entry(autoindex_config_rec *d, char *path)
848
apr_array_header_t *list = d->ign_list;
849
struct item *items = (struct item *) list->elts;
853
if ((tt = strrchr(path, '/')) == NULL) {
860
for (i = 0; i < list->nelts; ++i) {
861
struct item *p = &items[i];
864
if ((ap = strrchr(p->apply_to, '/')) == NULL) {
871
#ifndef CASE_BLIND_FILESYSTEM
872
if (!ap_strcmp_match(path, p->apply_path)
873
&& !ap_strcmp_match(tt, ap)) {
876
#else /* !CASE_BLIND_FILESYSTEM */
878
* On some platforms, the match must be case-blind. This is really
879
* a factor of the filesystem involved, but we can't detect that
880
* reliably - so we have to granularise at the OS level.
882
if (!ap_strcasecmp_match(path, p->apply_path)
883
&& !ap_strcasecmp_match(tt, ap)) {
886
#endif /* !CASE_BLIND_FILESYSTEM */
891
/*****************************************************************
893
* Actually generating output
897
* Elements of the emitted document:
899
* Emitted unless SUPPRESS_PREAMBLE is set AND ap_run_sub_req
900
* succeeds for the (content_type == text/html) header file.
902
* Emitted if found (and able).
904
* Emitted if a header file is NOT emitted.
908
* Emitted if FANCY_INDEXING is set.
910
* Emitted if found (and able).
912
* Emitted if ServerSignature is not Off AND a readme file
915
* Emitted unless SUPPRESS_PREAMBLE is set AND ap_run_sub_req
916
* succeeds for the (content_type == text/html) readme file.
921
* emit a plain text file
923
static void do_emit_plain(request_rec *r, apr_file_t *f)
925
char buf[AP_IOBUFSIZE + 1];
930
ap_rputs("<pre>\n", r);
931
while (!apr_file_eof(f)) {
933
n = sizeof(char) * AP_IOBUFSIZE;
934
rv = apr_file_read(f, buf, &n);
935
} while (APR_STATUS_IS_EINTR(rv));
936
if (n == 0 || rv != APR_SUCCESS) {
937
/* ###: better error here? */
943
for (i = c; i < n; i++) {
944
if (buf[i] == '<' || buf[i] == '>' || buf[i] == '&') {
950
ap_rputs(&buf[c], r);
954
else if (ch == '>') {
957
else if (ch == '&') {
958
ap_rputs("&", r);
963
ap_rputs("</pre>\n", r);
967
* Handle the preamble through the H1 tag line, inclusive. Locate
968
* the file with a subrequests. Process text/html documents by actually
969
* running the subrequest; text/xxx documents get copied verbatim,
970
* and any other content type is ignored. This means that a non-text
971
* document (such as HEADER.gif) might get multiviewed as the result
972
* instead of a text document, meaning nothing will be displayed, but
975
static void emit_head(request_rec *r, char *header_fname, int suppress_amble,
976
int emit_xhtml, char *title)
978
apr_table_t *hdrs = r->headers_in;
979
apr_file_t *f = NULL;
980
request_rec *rr = NULL;
983
const char *r_accept;
984
const char *r_accept_enc;
987
* If there's a header file, send a subrequest to look for it. If it's
988
* found and html do the subrequest, otherwise handle it
990
r_accept = apr_table_get(hdrs, "Accept");
991
r_accept_enc = apr_table_get(hdrs, "Accept-Encoding");
992
apr_table_setn(hdrs, "Accept", "text/html, text/plain");
993
apr_table_unset(hdrs, "Accept-Encoding");
996
if ((header_fname != NULL) && r->args) {
997
header_fname = apr_pstrcat(r->pool, header_fname, "?", r->args, NULL);
1000
if ((header_fname != NULL)
1001
&& (rr = ap_sub_req_lookup_uri(header_fname, r, r->output_filters))
1002
&& (rr->status == HTTP_OK)
1003
&& (rr->filename != NULL)
1004
&& (rr->finfo.filetype == APR_REG)) {
1006
* Check for the two specific cases we allow: text/html and
1007
* text/anything-else. The former is allowed to be processed for
1010
if (rr->content_type != NULL) {
1011
if (!strcasecmp(ap_field_noparam(r->pool, rr->content_type),
1014
/* Hope everything will work... */
1018
if (! suppress_amble) {
1019
emit_preamble(r, emit_xhtml, title);
1021
/* This is a hack, but I can't find any better way to do this.
1022
* The problem is that we have already created the sub-request,
1023
* but we just inserted the OLD_WRITE filter, and the
1024
* sub-request needs to pass its data through the OLD_WRITE
1025
* filter, or things go horribly wrong (missing data, data in
1026
* the wrong order, etc). To fix it, if you create a
1027
* sub-request and then insert the OLD_WRITE filter before you
1028
* run the request, you need to make sure that the sub-request
1029
* data goes through the OLD_WRITE filter. Just steal this
1030
* code. The long-term solution is to remove the ap_r*
1033
for (f=rr->output_filters;
1034
f->frec != ap_subreq_core_filter_handle; f = f->next);
1035
f->next = r->output_filters;
1038
* If there's a problem running the subrequest, display the
1039
* preamble if we didn't do it before -- the header file
1040
* didn't get displayed.
1042
if (ap_run_sub_req(rr) != OK) {
1043
/* It didn't work */
1044
emit_amble = suppress_amble;
1048
else if (!strncasecmp("text/", rr->content_type, 5)) {
1050
* If we can open the file, prefix it with the preamble
1051
* regardless; since we'll be sending a <pre> block around
1052
* the file's contents, any HTML header it had won't end up
1055
if (apr_file_open(&f, rr->filename, APR_READ,
1056
APR_OS_DEFAULT, r->pool) == APR_SUCCESS) {
1057
emit_preamble(r, emit_xhtml, title);
1059
do_emit_plain(r, f);
1068
apr_table_setn(hdrs, "Accept", r_accept);
1071
apr_table_unset(hdrs, "Accept");
1075
apr_table_setn(hdrs, "Accept-Encoding", r_accept_enc);
1079
emit_preamble(r, emit_xhtml, title);
1082
ap_rvputs(r, "<h1>Index of ", title, "</h1>\n", NULL);
1085
ap_destroy_sub_req(rr);
1091
* Handle the Readme file through the postamble, inclusive. Locate
1092
* the file with a subrequests. Process text/html documents by actually
1093
* running the subrequest; text/xxx documents get copied verbatim,
1094
* and any other content type is ignored. This means that a non-text
1095
* document (such as FOOTER.gif) might get multiviewed as the result
1096
* instead of a text document, meaning nothing will be displayed, but
1099
static void emit_tail(request_rec *r, char *readme_fname, int suppress_amble)
1101
apr_file_t *f = NULL;
1102
request_rec *rr = NULL;
1103
int suppress_post = 0;
1104
int suppress_sig = 0;
1107
* If there's a readme file, send a subrequest to look for it. If it's
1108
* found and a text file, handle it -- otherwise fall through and
1109
* pretend there's nothing there.
1111
if ((readme_fname != NULL)
1112
&& (rr = ap_sub_req_lookup_uri(readme_fname, r, r->output_filters))
1113
&& (rr->status == HTTP_OK)
1114
&& (rr->filename != NULL)
1115
&& rr->finfo.filetype == APR_REG) {
1117
* Check for the two specific cases we allow: text/html and
1118
* text/anything-else. The former is allowed to be processed for
1121
if (rr->content_type != NULL) {
1122
if (!strcasecmp(ap_field_noparam(r->pool, rr->content_type),
1125
for (f=rr->output_filters;
1126
f->frec != ap_subreq_core_filter_handle; f = f->next);
1127
f->next = r->output_filters;
1130
if (ap_run_sub_req(rr) == OK) {
1133
suppress_post = suppress_amble;
1136
else if (!strncasecmp("text/", rr->content_type, 5)) {
1138
* If we can open the file, suppress the signature.
1140
if (apr_file_open(&f, rr->filename, APR_READ,
1141
APR_OS_DEFAULT, r->pool) == APR_SUCCESS) {
1142
do_emit_plain(r, f);
1150
if (!suppress_sig) {
1151
ap_rputs(ap_psignature("", r), r);
1153
if (!suppress_post) {
1154
ap_rputs("</body></html>\n", r);
1157
ap_destroy_sub_req(rr);
1162
static char *find_title(request_rec *r)
1164
char titlebuf[MAX_STRING_LEN], *find = "<title>";
1165
apr_file_t *thefile = NULL;
1169
if (r->status != HTTP_OK) {
1172
if ((r->content_type != NULL)
1173
&& (!strcasecmp(ap_field_noparam(r->pool, r->content_type),
1175
|| !strcmp(r->content_type, INCLUDES_MAGIC_TYPE))
1176
&& !r->content_encoding) {
1177
if (apr_file_open(&thefile, r->filename, APR_READ,
1178
APR_OS_DEFAULT, r->pool) != APR_SUCCESS) {
1181
n = sizeof(char) * (MAX_STRING_LEN - 1);
1182
apr_file_read(thefile, titlebuf, &n);
1184
apr_file_close(thefile);
1188
for (x = 0, p = 0; titlebuf[x]; x++) {
1189
if (apr_tolower(titlebuf[x]) == find[p]) {
1191
if ((p = ap_ind(&titlebuf[++x], '<')) != -1) {
1192
titlebuf[x + p] = '\0';
1194
/* Scan for line breaks for Tanmoy's secretary */
1195
for (y = x; titlebuf[y]; y++) {
1196
if ((titlebuf[y] == CR) || (titlebuf[y] == LF)) {
1205
apr_file_close(thefile);
1206
return apr_pstrdup(r->pool, &titlebuf[x]);
1213
apr_file_close(thefile);
1218
static struct ent *make_parent_entry(apr_int32_t autoindex_opts,
1219
autoindex_config_rec *d,
1220
request_rec *r, char keyid,
1223
struct ent *p = (struct ent *) apr_pcalloc(r->pool, sizeof(struct ent));
1226
* p->name is now the true parent URI.
1227
* testpath is a crafted lie, so that the syntax '/some/..'
1228
* (or simply '..')be used to describe 'up' from '/some/'
1229
* when processeing IndexIgnore, and Icon|Alt|Desc configs.
1232
/* The output has always been to the parent. Don't make ourself
1233
* our own parent (worthless cyclical reference).
1235
if (!(p->name = ap_make_full_path(r->pool, r->uri, "../"))) {
1238
ap_getparents(p->name);
1243
/* IndexIgnore has always compared "/thispath/.." */
1244
testpath = ap_make_full_path(r->pool, r->filename, "..");
1245
if (ignore_entry(d, testpath)) {
1251
p->key = apr_toupper(keyid);
1252
p->ascending = (apr_toupper(direction) == D_ASCENDING);
1253
p->version_sort = autoindex_opts & VERSION_SORT;
1254
if (autoindex_opts & FANCY_INDEXING) {
1255
if (!(p->icon = find_default_icon(d, testpath))) {
1256
p->icon = find_default_icon(d, "^^DIRECTORY^^");
1258
if (!(p->alt = find_default_alt(d, testpath))) {
1259
if (!(p->alt = find_default_alt(d, "^^DIRECTORY^^"))) {
1263
p->desc = find_desc(d, testpath);
1268
static struct ent *make_autoindex_entry(const apr_finfo_t *dirent,
1270
autoindex_config_rec *d,
1271
request_rec *r, char keyid,
1273
const char *pattern)
1277
int show_forbidden = 0;
1279
/* Dot is ignored, Parent is handled by make_parent_entry() */
1280
if ((dirent->name[0] == '.') && (!dirent->name[1]
1281
|| ((dirent->name[1] == '.') && !dirent->name[2])))
1285
* On some platforms, the match must be case-blind. This is really
1286
* a factor of the filesystem involved, but we can't detect that
1287
* reliably - so we have to granularise at the OS level.
1289
if (pattern && (apr_fnmatch(pattern, dirent->name,
1290
APR_FNM_NOESCAPE | APR_FNM_PERIOD
1291
#ifdef CASE_BLIND_FILESYSTEM
1292
| APR_FNM_CASE_BLIND
1299
if (ignore_entry(d, ap_make_full_path(r->pool,
1300
r->filename, dirent->name))) {
1304
if (!(rr = ap_sub_req_lookup_dirent(dirent, r, AP_SUBREQ_NO_ARGS, NULL))) {
1308
if((autoindex_opts & SHOW_FORBIDDEN)
1309
&& (rr->status == HTTP_UNAUTHORIZED || rr->status == HTTP_FORBIDDEN)) {
1313
if ((rr->finfo.filetype != APR_DIR && rr->finfo.filetype != APR_REG)
1314
|| !(rr->status == OK || ap_is_HTTP_SUCCESS(rr->status)
1315
|| ap_is_HTTP_REDIRECT(rr->status)
1316
|| show_forbidden == 1)) {
1317
ap_destroy_sub_req(rr);
1321
p = (struct ent *) apr_pcalloc(r->pool, sizeof(struct ent));
1322
if (dirent->filetype == APR_DIR) {
1323
p->name = apr_pstrcat(r->pool, dirent->name, "/", NULL);
1326
p->name = apr_pstrdup(r->pool, dirent->name);
1334
p->key = apr_toupper(keyid);
1335
p->ascending = (apr_toupper(direction) == D_ASCENDING);
1336
p->version_sort = !!(autoindex_opts & VERSION_SORT);
1337
p->ignore_case = !!(autoindex_opts & IGNORE_CASE);
1339
if (autoindex_opts & (FANCY_INDEXING | TABLE_INDEXING)) {
1340
p->lm = rr->finfo.mtime;
1341
if (dirent->filetype == APR_DIR) {
1342
if (autoindex_opts & FOLDERS_FIRST) {
1345
rr->filename = ap_make_dirstr_parent (rr->pool, rr->filename);
1347
/* omit the trailing slash (1.3 compat) */
1348
rr->filename[strlen(rr->filename) - 1] = '\0';
1350
if (!(p->icon = find_icon(d, rr, 1))) {
1351
p->icon = find_default_icon(d, "^^DIRECTORY^^");
1353
if (!(p->alt = find_alt(d, rr, 1))) {
1354
if (!(p->alt = find_default_alt(d, "^^DIRECTORY^^"))) {
1360
p->icon = find_icon(d, rr, 0);
1361
p->alt = find_alt(d, rr, 0);
1362
p->size = rr->finfo.size;
1365
p->desc = find_desc(d, rr->filename);
1367
if ((!p->desc) && (autoindex_opts & SCAN_HTML_TITLES)) {
1368
p->desc = apr_pstrdup(r->pool, find_title(rr));
1371
ap_destroy_sub_req(rr);
1373
* We don't need to take any special action for the file size key.
1374
* If we did, it would go here.
1376
if (keyid == K_LAST_MOD) {
1384
static char *terminate_description(autoindex_config_rec *d, char *desc,
1385
apr_int32_t autoindex_opts, int desc_width)
1387
int maxsize = desc_width;
1391
* If there's no DescriptionWidth in effect, default to the old
1392
* behaviour of adjusting the description size depending upon
1393
* what else is being displayed. Otherwise, stick with the
1396
if (d->desc_adjust == K_UNSET) {
1397
if (autoindex_opts & SUPPRESS_ICON) {
1400
if (autoindex_opts & SUPPRESS_LAST_MOD) {
1403
if (autoindex_opts & SUPPRESS_SIZE) {
1407
for (x = 0; desc[x] && ((maxsize > 0) || (desc[x] == '<')); x++) {
1408
if (desc[x] == '<') {
1409
while (desc[x] != '>') {
1417
else if (desc[x] == '&') {
1418
/* entities like ä count as one character */
1420
for ( ; desc[x] != ';'; ++x) {
1421
if (desc[x] == '\0') {
1431
if (!maxsize && desc[x] != '\0') {
1432
desc[x - 1] = '>'; /* Grump. */
1433
desc[x] = '\0'; /* Double Grump! */
1439
* Emit the anchor for the specified field. If a field is the key for the
1440
* current request, the link changes its meaning to reverse the order when
1441
* selected again. Non-active fields always start in ascending order.
1443
static void emit_link(request_rec *r, const char *anchor, char column,
1444
char curkey, char curdirection,
1445
const char *colargs, int nosort)
1458
qvalue[7] = ((curkey == column) && (curdirection == D_ASCENDING))
1459
? D_DESCENDING : D_ASCENDING;
1461
ap_rvputs(r, "<a href=\"", qvalue, colargs ? colargs : "",
1462
"\">", anchor, "</a>", NULL);
1465
ap_rputs(anchor, r);
1469
static void output_directories(struct ent **ar, int n,
1470
autoindex_config_rec *d, request_rec *r,
1471
apr_int32_t autoindex_opts, char keyid,
1472
char direction, const char *colargs)
1476
char *name = r->uri;
1478
int static_columns = !!(autoindex_opts & SUPPRESS_COLSORT);
1479
apr_pool_t *scratch;
1484
char *breakrow = "";
1486
apr_pool_create(&scratch, r->pool);
1487
if (name[0] == '\0') {
1491
name_width = d->name_width;
1492
desc_width = d->desc_width;
1494
if ((autoindex_opts & (FANCY_INDEXING | TABLE_INDEXING))
1495
== FANCY_INDEXING) {
1496
if (d->name_adjust == K_ADJUST) {
1497
for (x = 0; x < n; x++) {
1498
int t = strlen(ar[x]->name);
1499
if (t > name_width) {
1505
if (d->desc_adjust == K_ADJUST) {
1506
for (x = 0; x < n; x++) {
1507
if (ar[x]->desc != NULL) {
1508
int t = strlen(ar[x]->desc);
1509
if (t > desc_width) {
1516
name_scratch = apr_palloc(r->pool, name_width + 1);
1517
pad_scratch = apr_palloc(r->pool, name_width + 1);
1518
memset(pad_scratch, ' ', name_width);
1519
pad_scratch[name_width] = '\0';
1521
if (autoindex_opts & TABLE_INDEXING) {
1523
ap_rputs("<table><tr>", r);
1524
if (!(autoindex_opts & SUPPRESS_ICON)) {
1525
ap_rputs("<th>", r);
1526
if ((tp = find_default_icon(d, "^^BLANKICON^^"))) {
1527
ap_rvputs(r, "<img src=\"", ap_escape_html(scratch, tp),
1528
"\" alt=\"[ICO]\"", NULL);
1529
if (d->icon_width) {
1530
ap_rprintf(r, " width=\"%d\"", d->icon_width);
1532
if (d->icon_height) {
1533
ap_rprintf(r, " height=\"%d\"", d->icon_height);
1536
if (autoindex_opts & EMIT_XHTML) {
1539
ap_rputs("></th>", r);
1542
ap_rputs(" </th>", r);
1547
ap_rputs("<th>", r);
1548
emit_link(r, "Name", K_NAME, keyid, direction,
1549
colargs, static_columns);
1550
if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1551
ap_rputs("</th><th>", r);
1552
emit_link(r, "Last modified", K_LAST_MOD, keyid, direction,
1553
colargs, static_columns);
1556
if (!(autoindex_opts & SUPPRESS_SIZE)) {
1557
ap_rputs("</th><th>", r);
1558
emit_link(r, "Size", K_SIZE, keyid, direction,
1559
colargs, static_columns);
1562
if (!(autoindex_opts & SUPPRESS_DESC)) {
1563
ap_rputs("</th><th>", r);
1564
emit_link(r, "Description", K_DESC, keyid, direction,
1565
colargs, static_columns);
1568
if (!(autoindex_opts & SUPPRESS_RULES)) {
1569
breakrow = apr_psprintf(r->pool,
1570
"<tr><th colspan=\"%d\">"
1571
"<hr%s></th></tr>\n", cols,
1572
(autoindex_opts & EMIT_XHTML) ? " /" : "");
1574
ap_rvputs(r, "</th></tr>", breakrow, NULL);
1576
else if (autoindex_opts & FANCY_INDEXING) {
1577
ap_rputs("<pre>", r);
1578
if (!(autoindex_opts & SUPPRESS_ICON)) {
1579
if ((tp = find_default_icon(d, "^^BLANKICON^^"))) {
1580
ap_rvputs(r, "<img src=\"", ap_escape_html(scratch, tp),
1581
"\" alt=\"Icon \"", NULL);
1582
if (d->icon_width) {
1583
ap_rprintf(r, " width=\"%d\"", d->icon_width);
1585
if (d->icon_height) {
1586
ap_rprintf(r, " height=\"%d\"", d->icon_height);
1589
if (autoindex_opts & EMIT_XHTML) {
1598
emit_link(r, "Name", K_NAME, keyid, direction,
1599
colargs, static_columns);
1600
ap_rputs(pad_scratch + 4, r);
1602
* Emit the guaranteed-at-least-one-space-between-columns byte.
1605
if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1606
emit_link(r, "Last modified", K_LAST_MOD, keyid, direction,
1607
colargs, static_columns);
1610
if (!(autoindex_opts & SUPPRESS_SIZE)) {
1611
emit_link(r, "Size", K_SIZE, keyid, direction,
1612
colargs, static_columns);
1615
if (!(autoindex_opts & SUPPRESS_DESC)) {
1616
emit_link(r, "Description", K_DESC, keyid, direction,
1617
colargs, static_columns);
1619
if (!(autoindex_opts & SUPPRESS_RULES)) {
1621
if (autoindex_opts & EMIT_XHTML) {
1631
ap_rputs("<ul>", r);
1634
for (x = 0; x < n; x++) {
1635
char *anchor, *t, *t2;
1638
apr_pool_clear(scratch);
1641
anchor = ap_escape_html(scratch, ap_os_escape_path(scratch, t, 0));
1643
if (!x && t[0] == '/') {
1644
t2 = "Parent Directory";
1650
if (autoindex_opts & TABLE_INDEXING) {
1651
ap_rputs("<tr>", r);
1652
if (!(autoindex_opts & SUPPRESS_ICON)) {
1653
ap_rputs("<td valign=\"top\">", r);
1654
if (autoindex_opts & ICONS_ARE_LINKS) {
1655
ap_rvputs(r, "<a href=\"", anchor, "\">", NULL);
1657
if ((ar[x]->icon) || d->default_icon) {
1658
ap_rvputs(r, "<img src=\"",
1659
ap_escape_html(scratch,
1660
ar[x]->icon ? ar[x]->icon
1662
"\" alt=\"[", (ar[x]->alt ? ar[x]->alt : " "),
1664
if (d->icon_width) {
1665
ap_rprintf(r, " width=\"%d\"", d->icon_width);
1667
if (d->icon_height) {
1668
ap_rprintf(r, " height=\"%d\"", d->icon_height);
1671
if (autoindex_opts & EMIT_XHTML) {
1677
ap_rputs(" ", r);
1679
if (autoindex_opts & ICONS_ARE_LINKS) {
1680
ap_rputs("</a></td>", r);
1683
ap_rputs("</td>", r);
1686
if (d->name_adjust == K_ADJUST) {
1687
ap_rvputs(r, "<td><a href=\"", anchor, "\">",
1688
ap_escape_html(scratch, t2), "</a>", NULL);
1691
nwidth = strlen(t2);
1692
if (nwidth > name_width) {
1693
memcpy(name_scratch, t2, name_width - 3);
1694
name_scratch[name_width - 3] = '.';
1695
name_scratch[name_width - 2] = '.';
1696
name_scratch[name_width - 1] = '>';
1697
name_scratch[name_width] = 0;
1699
nwidth = name_width;
1701
ap_rvputs(r, "<td><a href=\"", anchor, "\">",
1702
ap_escape_html(scratch, t2),
1703
"</a>", pad_scratch + nwidth, NULL);
1705
if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1706
if (ar[x]->lm != -1) {
1707
char time_str[MAX_STRING_LEN];
1709
apr_time_exp_lt(&ts, ar[x]->lm);
1710
apr_strftime(time_str, &rv, MAX_STRING_LEN,
1711
"</td><td align=\"right\">%d-%b-%Y %H:%M ",
1713
ap_rputs(time_str, r);
1716
ap_rputs("</td><td> ", r);
1719
if (!(autoindex_opts & SUPPRESS_SIZE)) {
1721
ap_rvputs(r, "</td><td align=\"right\">",
1722
apr_strfsize(ar[x]->size, buf), NULL);
1724
if (!(autoindex_opts & SUPPRESS_DESC)) {
1726
if (d->desc_adjust == K_ADJUST) {
1727
ap_rvputs(r, "</td><td>", ar[x]->desc, NULL);
1730
ap_rvputs(r, "</td><td>",
1731
terminate_description(d, ar[x]->desc,
1738
ap_rputs("</td><td> ", r);
1740
ap_rputs("</td></tr>\n", r);
1742
else if (autoindex_opts & FANCY_INDEXING) {
1743
if (!(autoindex_opts & SUPPRESS_ICON)) {
1744
if (autoindex_opts & ICONS_ARE_LINKS) {
1745
ap_rvputs(r, "<a href=\"", anchor, "\">", NULL);
1747
if ((ar[x]->icon) || d->default_icon) {
1748
ap_rvputs(r, "<img src=\"",
1749
ap_escape_html(scratch,
1750
ar[x]->icon ? ar[x]->icon
1752
"\" alt=\"[", (ar[x]->alt ? ar[x]->alt : " "),
1754
if (d->icon_width) {
1755
ap_rprintf(r, " width=\"%d\"", d->icon_width);
1757
if (d->icon_height) {
1758
ap_rprintf(r, " height=\"%d\"", d->icon_height);
1761
if (autoindex_opts & EMIT_XHTML) {
1769
if (autoindex_opts & ICONS_ARE_LINKS) {
1770
ap_rputs("</a> ", r);
1776
nwidth = strlen(t2);
1777
if (nwidth > name_width) {
1778
memcpy(name_scratch, t2, name_width - 3);
1779
name_scratch[name_width - 3] = '.';
1780
name_scratch[name_width - 2] = '.';
1781
name_scratch[name_width - 1] = '>';
1782
name_scratch[name_width] = 0;
1784
nwidth = name_width;
1786
ap_rvputs(r, "<a href=\"", anchor, "\">",
1787
ap_escape_html(scratch, t2),
1788
"</a>", pad_scratch + nwidth, NULL);
1790
* The blank before the storm.. er, before the next field.
1793
if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1794
if (ar[x]->lm != -1) {
1795
char time_str[MAX_STRING_LEN];
1797
apr_time_exp_lt(&ts, ar[x]->lm);
1798
apr_strftime(time_str, &rv, MAX_STRING_LEN,
1799
"%d-%b-%Y %H:%M ", &ts);
1800
ap_rputs(time_str, r);
1803
/*Length="22-Feb-1998 23:42 " (see 4 lines above) */
1807
if (!(autoindex_opts & SUPPRESS_SIZE)) {
1809
ap_rputs(apr_strfsize(ar[x]->size, buf), r);
1812
if (!(autoindex_opts & SUPPRESS_DESC)) {
1814
ap_rputs(terminate_description(d, ar[x]->desc,
1822
ap_rvputs(r, "<li><a href=\"", anchor, "\"> ",
1823
ap_escape_html(scratch, t2),
1824
"</a></li>\n", NULL);
1827
if (autoindex_opts & TABLE_INDEXING) {
1828
ap_rvputs(r, breakrow, "</table>\n", NULL);
1830
else if (autoindex_opts & FANCY_INDEXING) {
1831
if (!(autoindex_opts & SUPPRESS_RULES)) {
1833
if (autoindex_opts & EMIT_XHTML) {
1836
ap_rputs("></pre>\n", r);
1839
ap_rputs("</pre>\n", r);
1843
ap_rputs("</ul>\n", r);
1848
* Compare two file entries according to the sort criteria. The return
1849
* is essentially a signum function value.
1852
static int dsortf(struct ent **e1, struct ent **e2)
1859
* First, see if either of the entries is for the parent directory.
1860
* If so, that *always* sorts lower than anything else.
1862
if ((*e1)->name[0] == '/') {
1865
if ((*e2)->name[0] == '/') {
1869
* Now see if one's a directory and one isn't, if we're set
1870
* isdir for FOLDERS_FIRST.
1872
if ((*e1)->isdir != (*e2)->isdir) {
1873
return (*e1)->isdir ? -1 : 1;
1876
* All of our comparisons will be of the c1 entry against the c2 one,
1877
* so assign them appropriately to take care of the ordering.
1879
if ((*e1)->ascending) {
1890
if (c1->lm > c2->lm) {
1893
else if (c1->lm < c2->lm) {
1898
if (c1->size > c2->size) {
1901
else if (c1->size < c2->size) {
1906
if (c1->version_sort) {
1907
result = apr_strnatcmp(c1->desc ? c1->desc : "",
1908
c2->desc ? c2->desc : "");
1911
result = strcmp(c1->desc ? c1->desc : "",
1912
c2->desc ? c2->desc : "");
1920
/* names may identical when treated case-insensitively,
1921
* so always fall back on strcmp() flavors to put entries
1922
* in deterministic order. This means that 'ABC' and 'abc'
1923
* will always appear in the same order, rather than
1924
* variably between 'ABC abc' and 'abc ABC' order.
1927
if (c1->version_sort) {
1928
if (c1->ignore_case) {
1929
result = apr_strnatcasecmp (c1->name, c2->name);
1932
result = apr_strnatcmp(c1->name, c2->name);
1936
/* The names may be identical in respects other other than
1937
* filename case when strnatcmp is used above, so fall back
1938
* to strcmp on conflicts so that fn1.01.zzz and fn1.1.zzz
1939
* are also sorted in a deterministic order.
1942
if (!result && c1->ignore_case) {
1943
result = strcasecmp (c1->name, c2->name);
1947
result = strcmp (c1->name, c2->name);
1954
static int index_directory(request_rec *r,
1955
autoindex_config_rec *autoindex_conf)
1957
char *title_name = ap_escape_html(r->pool, r->uri);
1959
char *name = r->filename;
1960
char *pstring = NULL;
1963
apr_status_t status;
1965
struct ent *head, *p;
1966
struct ent **ar = NULL;
1967
const char *qstring;
1968
apr_int32_t autoindex_opts = autoindex_conf->opts;
1973
apr_size_t dirpathlen;
1975
if ((status = apr_dir_open(&thedir, name, r->pool)) != APR_SUCCESS) {
1976
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
1977
"Can't open directory for index: %s", r->filename);
1978
return HTTP_FORBIDDEN;
1981
#if APR_HAS_UNICODE_FS
1982
ap_set_content_type(r, "text/html;charset=utf-8");
1984
ap_set_content_type(r, "text/html");
1986
if (autoindex_opts & TRACK_MODIFIED) {
1987
ap_update_mtime(r, r->finfo.mtime);
1988
ap_set_last_modified(r);
1991
if (r->header_only) {
1992
apr_dir_close(thedir);
1997
* If there is no specific ordering defined for this directory,
1998
* default to ascending by filename.
2000
keyid = autoindex_conf->default_keyid
2001
? autoindex_conf->default_keyid : K_NAME;
2002
direction = autoindex_conf->default_direction
2003
? autoindex_conf->default_direction : D_ASCENDING;
2006
* Figure out what sort of indexing (if any) we're supposed to use.
2008
* If no QUERY_STRING was specified or client query strings have been
2009
* explicitly disabled.
2010
* If we are ignoring the client, suppress column sorting as well.
2012
if (autoindex_opts & IGNORE_CLIENT) {
2014
autoindex_opts |= SUPPRESS_COLSORT;
2018
char fval[5], vval[5], *ppre = "", *epattern = "";
2019
fval[0] = '\0'; vval[0] = '\0';
2022
while (qstring && *qstring) {
2024
/* C= First Sort key Column (N, M, S, D) */
2025
if ( qstring[0] == 'C' && qstring[1] == '='
2026
&& qstring[2] && strchr(K_VALID, qstring[2])
2027
&& ( qstring[3] == '&' || qstring[3] == ';'
2030
qstring += qstring[3] ? 4 : 3;
2033
/* O= Sort order (A, D) */
2034
else if ( qstring[0] == 'O' && qstring[1] == '='
2035
&& ( (qstring[2] == D_ASCENDING)
2036
|| (qstring[2] == D_DESCENDING))
2037
&& ( qstring[3] == '&' || qstring[3] == ';'
2039
direction = qstring[2];
2040
qstring += qstring[3] ? 4 : 3;
2043
/* F= Output Format (0 plain, 1 fancy (pre), 2 table) */
2044
else if ( qstring[0] == 'F' && qstring[1] == '='
2045
&& qstring[2] && strchr("012", qstring[2])
2046
&& ( qstring[3] == '&' || qstring[3] == ';'
2048
if (qstring[2] == '0') {
2049
autoindex_opts &= ~(FANCY_INDEXING | TABLE_INDEXING);
2051
else if (qstring[2] == '1') {
2052
autoindex_opts = (autoindex_opts | FANCY_INDEXING)
2055
else if (qstring[2] == '2') {
2056
autoindex_opts |= FANCY_INDEXING | TABLE_INDEXING;
2058
strcpy(fval, ";F= ");
2059
fval[3] = qstring[2];
2060
qstring += qstring[3] ? 4 : 3;
2063
/* V= Version sort (0, 1) */
2064
else if ( qstring[0] == 'V' && qstring[1] == '='
2065
&& (qstring[2] == '0' || qstring[2] == '1')
2066
&& ( qstring[3] == '&' || qstring[3] == ';'
2068
if (qstring[2] == '0') {
2069
autoindex_opts &= ~VERSION_SORT;
2071
else if (qstring[2] == '1') {
2072
autoindex_opts |= VERSION_SORT;
2074
strcpy(vval, ";V= ");
2075
vval[3] = qstring[2];
2076
qstring += qstring[3] ? 4 : 3;
2079
/* P= wildcard pattern (*.foo) */
2080
else if (qstring[0] == 'P' && qstring[1] == '=') {
2081
const char *eos = qstring += 2; /* for efficiency */
2083
while (*eos && *eos != '&' && *eos != ';') {
2087
if (eos == qstring) {
2091
pstring = apr_pstrndup(r->pool, qstring, eos - qstring);
2092
if (ap_unescape_url(pstring) != OK) {
2093
/* ignore the pattern, if it's bad. */
2099
epattern = ap_escape_uri(r->pool, pstring);
2103
if (*eos && *++eos) {
2111
/* Syntax error? Ignore the remainder! */
2116
colargs = apr_pstrcat(r->pool, fval, vval, ppre, epattern, NULL);
2119
/* Spew HTML preamble */
2120
title_endp = title_name + strlen(title_name) - 1;
2122
while (title_endp > title_name && *title_endp == '/') {
2123
*title_endp-- = '\0';
2126
emit_head(r, find_header(autoindex_conf, r),
2127
autoindex_opts & SUPPRESS_PREAMBLE,
2128
autoindex_opts & EMIT_XHTML, title_name);
2131
* Since we don't know how many dir. entries there are, put them into a
2132
* linked list and then arrayificate them so qsort can use them.
2135
p = make_parent_entry(autoindex_opts, autoindex_conf, r, keyid, direction);
2141
fullpath = apr_palloc(r->pool, APR_PATH_MAX);
2142
dirpathlen = strlen(name);
2143
memcpy(fullpath, name, dirpathlen);
2146
status = apr_dir_read(&dirent, APR_FINFO_MIN | APR_FINFO_NAME, thedir);
2147
if (APR_STATUS_IS_INCOMPLETE(status)) {
2148
continue; /* ignore un-stat()able files */
2150
else if (status != APR_SUCCESS) {
2154
/* We want to explode symlinks here. */
2155
if (dirent.filetype == APR_LNK) {
2156
const char *savename;
2158
/* We *must* have FNAME. */
2159
savename = dirent.name;
2160
apr_cpystrn(fullpath + dirpathlen, dirent.name,
2161
APR_PATH_MAX - dirpathlen);
2162
status = apr_stat(&fi, fullpath,
2163
dirent.valid & ~(APR_FINFO_NAME), r->pool);
2164
if (status != APR_SUCCESS) {
2165
/* Something bad happened, skip this file. */
2168
memcpy(&dirent, &fi, sizeof(fi));
2169
dirent.name = savename;
2170
dirent.valid |= APR_FINFO_NAME;
2172
p = make_autoindex_entry(&dirent, autoindex_opts, autoindex_conf, r,
2173
keyid, direction, pstring);
2182
ar = (struct ent **) apr_palloc(r->pool,
2183
num_ent * sizeof(struct ent *));
2191
qsort((void *) ar, num_ent, sizeof(struct ent *),
2192
(int (*)(const void *, const void *)) dsortf);
2194
output_directories(ar, num_ent, autoindex_conf, r, autoindex_opts,
2195
keyid, direction, colargs);
2196
apr_dir_close(thedir);
2198
emit_tail(r, find_readme(autoindex_conf, r),
2199
autoindex_opts & SUPPRESS_PREAMBLE);
2204
/* The formal handler... */
2206
static int handle_autoindex(request_rec *r)
2208
autoindex_config_rec *d;
2211
if(strcmp(r->handler,DIR_MAGIC_TYPE)) {
2215
allow_opts = ap_allow_options(r);
2217
d = (autoindex_config_rec *) ap_get_module_config(r->per_dir_config,
2220
r->allowed |= (AP_METHOD_BIT << M_GET);
2221
if (r->method_number != M_GET) {
2225
/* OK, nothing easy. Trot out the heavy artillery... */
2227
if (allow_opts & OPT_INDEXES) {
2230
if ((errstatus = ap_discard_request_body(r)) != OK) {
2234
/* KLUDGE --- make the sub_req lookups happen in the right directory.
2235
* Fixing this in the sub_req_lookup functions themselves is difficult,
2236
* and would probably break virtual includes...
2239
if (r->filename[strlen(r->filename) - 1] != '/') {
2240
r->filename = apr_pstrcat(r->pool, r->filename, "/", NULL);
2242
return index_directory(r, d);
2245
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
2246
"Directory index forbidden by "
2247
"Options directive: %s", r->filename);
2248
return HTTP_FORBIDDEN;
2252
static void register_hooks(apr_pool_t *p)
2254
ap_hook_handler(handle_autoindex,NULL,NULL,APR_HOOK_MIDDLE);
2257
module AP_MODULE_DECLARE_DATA autoindex_module =
2259
STANDARD20_MODULE_STUFF,
2260
create_autoindex_config, /* dir config creater */
2261
merge_autoindex_configs, /* dir merger --- default is to override */
2262
NULL, /* server config */
2263
NULL, /* merge server config */
2264
autoindex_cmds, /* command apr_table_t */
2265
register_hooks /* register hooks */