/[Apache-SVN]/httpd/httpd/tags/2.3.1/modules/generators/mod_autoindex.c
ViewVC logotype

Contents of /httpd/httpd/tags/2.3.1/modules/generators/mod_autoindex.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 730918 - (show annotations)
Sat Jan 3 06:15:00 2009 UTC (10 months, 3 weeks ago) by pquerna
File MIME type: text/plain
File size: 75686 byte(s)
Tag 2.3.1 from trunk.
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 /*
18 * mod_autoindex.c: Handles the on-the-fly html index generation
19 *
20 * Rob McCool
21 * 3/23/93
22 *
23 * Adapted to Apache by rst.
24 *
25 * Version sort added by Martin Pool <mbp@humbug.org.au>.
26 */
27
28 #include "apr_strings.h"
29 #include "apr_fnmatch.h"
30 #include "apr_strings.h"
31 #include "apr_lib.h"
32
33 #define APR_WANT_STRFUNC
34 #include "apr_want.h"
35
36 #include "ap_config.h"
37 #include "httpd.h"
38 #include "http_config.h"
39 #include "http_core.h"
40 #include "http_request.h"
41 #include "http_protocol.h"
42 #include "http_log.h"
43 #include "http_main.h"
44 #include "util_script.h"
45
46 #include "mod_core.h"
47
48 module AP_MODULE_DECLARE_DATA autoindex_module;
49
50 /****************************************************************
51 *
52 * Handling configuration directives...
53 */
54
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)
74
75 #define K_NOADJUST 0
76 #define K_ADJUST 1
77 #define K_UNSET 2
78
79 /*
80 * Define keys for sorting.
81 */
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 */
87
88 #define D_ASCENDING 'A'
89 #define D_DESCENDING 'D'
90 #define D_VALID "AD" /* String containing _all_ valid D_ opts */
91
92 /*
93 * These are the dimensions of the default icons supplied with Apache.
94 */
95 #define DEFAULT_ICON_WIDTH 20
96 #define DEFAULT_ICON_HEIGHT 22
97
98 /*
99 * Other default dimensions.
100 */
101 #define DEFAULT_NAME_WIDTH 23
102 #define DEFAULT_DESC_WIDTH 23
103
104 struct item {
105 char *type;
106 char *apply_to;
107 char *apply_path;
108 char *data;
109 };
110
111 typedef struct ai_desc_t {
112 char *pattern;
113 char *description;
114 int full_path;
115 int wildcards;
116 } ai_desc_t;
117
118 typedef struct autoindex_config_struct {
119
120 char *default_icon;
121 char *style_sheet;
122 char *head_insert;
123 char *header;
124 char *readme;
125 apr_int32_t opts;
126 apr_int32_t incremented_opts;
127 apr_int32_t decremented_opts;
128 int name_width;
129 int name_adjust;
130 int desc_width;
131 int desc_adjust;
132 int icon_width;
133 int icon_height;
134 char default_keyid;
135 char default_direction;
136
137 apr_array_header_t *icon_list;
138 apr_array_header_t *alt_list;
139 apr_array_header_t *desc_list;
140 apr_array_header_t *ign_list;
141
142 char *ctype;
143 char *charset;
144 } autoindex_config_rec;
145
146 static char c_by_encoding, c_by_type, c_by_path;
147
148 #define BY_ENCODING &c_by_encoding
149 #define BY_TYPE &c_by_type
150 #define BY_PATH &c_by_path
151
152 /*
153 * This routine puts the standard HTML header at the top of the index page.
154 * We include the DOCTYPE because we may be using features therefrom (i.e.,
155 * HEIGHT and WIDTH attributes on the icons if we're FancyIndexing).
156 */
157 static void emit_preamble(request_rec *r, int xhtml, const char *title)
158 {
159 autoindex_config_rec *d;
160
161 d = (autoindex_config_rec *) ap_get_module_config(r->per_dir_config,
162 &autoindex_module);
163
164 if (xhtml) {
165 ap_rvputs(r, DOCTYPE_XHTML_1_0T,
166 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
167 " <head>\n <title>Index of ", title,
168 "</title>\n", NULL);
169 } else {
170 ap_rvputs(r, DOCTYPE_HTML_3_2,
171 "<html>\n <head>\n"
172 " <title>Index of ", title,
173 "</title>\n", NULL);
174 }
175
176 if (d->style_sheet != NULL) {
177 ap_rvputs(r, " <link rel=\"stylesheet\" href=\"", d->style_sheet,
178 "\" type=\"text/css\"", xhtml ? " />\n" : ">\n", NULL);
179 }
180 if (d->head_insert != NULL) {
181 ap_rputs(d->head_insert, r);
182 }
183 ap_rvputs(r, " </head>\n <body>\n", NULL);
184 }
185
186 static void push_item(apr_array_header_t *arr, char *type, const char *to,
187 const char *path, const char *data)
188 {
189 struct item *p = (struct item *) apr_array_push(arr);
190
191 if (!to) {
192 to = "";
193 }
194 if (!path) {
195 path = "";
196 }
197
198 p->type = type;
199 p->data = data ? apr_pstrdup(arr->pool, data) : NULL;
200 p->apply_path = apr_pstrcat(arr->pool, path, "*", NULL);
201
202 if ((type == BY_PATH) && (!ap_is_matchexp(to))) {
203 p->apply_to = apr_pstrcat(arr->pool, "*", to, NULL);
204 }
205 else if (to) {
206 p->apply_to = apr_pstrdup(arr->pool, to);
207 }
208 else {
209 p->apply_to = NULL;
210 }
211 }
212
213 static const char *add_alt(cmd_parms *cmd, void *d, const char *alt,
214 const char *to)
215 {
216 if (cmd->info == BY_PATH) {
217 if (!strcmp(to, "**DIRECTORY**")) {
218 to = "^^DIRECTORY^^";
219 }
220 }
221 if (cmd->info == BY_ENCODING) {
222 char *tmp = apr_pstrdup(cmd->pool, to);
223 ap_str_tolower(tmp);
224 to = tmp;
225 }
226
227 push_item(((autoindex_config_rec *) d)->alt_list, cmd->info, to,
228 cmd->path, alt);
229 return NULL;
230 }
231
232 static const char *add_icon(cmd_parms *cmd, void *d, const char *icon,
233 const char *to)
234 {
235 char *iconbak = apr_pstrdup(cmd->pool, icon);
236
237 if (icon[0] == '(') {
238 char *alt;
239 char *cl = strchr(iconbak, ')');
240
241 if (cl == NULL) {
242 return "missing closing paren";
243 }
244 alt = ap_getword_nc(cmd->pool, &iconbak, ',');
245 *cl = '\0'; /* Lose closing paren */
246 add_alt(cmd, d, &alt[1], to);
247 }
248 if (cmd->info == BY_PATH) {
249 if (!strcmp(to, "**DIRECTORY**")) {
250 to = "^^DIRECTORY^^";
251 }
252 }
253 if (cmd->info == BY_ENCODING) {
254 char *tmp = apr_pstrdup(cmd->pool, to);
255 ap_str_tolower(tmp);
256 to = tmp;
257 }
258
259 push_item(((autoindex_config_rec *) d)->icon_list, cmd->info, to,
260 cmd->path, iconbak);
261 return NULL;
262 }
263
264 /*
265 * Add description text for a filename pattern. If the pattern has
266 * wildcards already (or we need to add them), add leading and
267 * trailing wildcards to it to ensure substring processing. If the
268 * pattern contains a '/' anywhere, force wildcard matching mode,
269 * add a slash to the prefix so that "bar/bletch" won't be matched
270 * by "foobar/bletch", and make a note that there's a delimiter;
271 * the matching routine simplifies to just the actual filename
272 * whenever it can. This allows definitions in parent directories
273 * to be made for files in subordinate ones using relative paths.
274 */
275
276 /*
277 * Absent a strcasestr() function, we have to force wildcards on
278 * systems for which "AAA" and "aaa" mean the same file.
279 */
280 #ifdef CASE_BLIND_FILESYSTEM
281 #define WILDCARDS_REQUIRED 1
282 #else
283 #define WILDCARDS_REQUIRED 0
284 #endif
285
286 static const char *add_desc(cmd_parms *cmd, void *d, const char *desc,
287 const char *to)
288 {
289 autoindex_config_rec *dcfg = (autoindex_config_rec *) d;
290 ai_desc_t *desc_entry;
291 char *prefix = "";
292
293 desc_entry = (ai_desc_t *) apr_array_push(dcfg->desc_list);
294 desc_entry->full_path = (ap_strchr_c(to, '/') == NULL) ? 0 : 1;
295 desc_entry->wildcards = (WILDCARDS_REQUIRED
296 || desc_entry->full_path
297 || apr_fnmatch_test(to));
298 if (desc_entry->wildcards) {
299 prefix = desc_entry->full_path ? "*/" : "*";
300 desc_entry->pattern = apr_pstrcat(dcfg->desc_list->pool,
301 prefix, to, "*", NULL);
302 }
303 else {
304 desc_entry->pattern = apr_pstrdup(dcfg->desc_list->pool, to);
305 }
306 desc_entry->description = apr_pstrdup(dcfg->desc_list->pool, desc);
307 return NULL;
308 }
309
310 static const char *add_ignore(cmd_parms *cmd, void *d, const char *ext)
311 {
312 push_item(((autoindex_config_rec *) d)->ign_list, 0, ext, cmd->path, NULL);
313 return NULL;
314 }
315
316 static const char *add_opts(cmd_parms *cmd, void *d, int argc, char *const argv[])
317 {
318 int i;
319 char *w;
320 apr_int32_t opts;
321 apr_int32_t opts_add;
322 apr_int32_t opts_remove;
323 char action;
324 autoindex_config_rec *d_cfg = (autoindex_config_rec *) d;
325
326 opts = d_cfg->opts;
327 opts_add = d_cfg->incremented_opts;
328 opts_remove = d_cfg->decremented_opts;
329
330 for (i = 0; i < argc; i++) {
331 int option = 0;
332 w = argv[i];
333
334 if ((*w == '+') || (*w == '-')) {
335 action = *(w++);
336 }
337 else {
338 action = '\0';
339 }
340 if (!strcasecmp(w, "FancyIndexing")) {
341 option = FANCY_INDEXING;
342 }
343 else if (!strcasecmp(w, "FoldersFirst")) {
344 option = FOLDERS_FIRST;
345 }
346 else if (!strcasecmp(w, "HTMLTable")) {
347 option = TABLE_INDEXING;
348 }
349 else if (!strcasecmp(w, "IconsAreLinks")) {
350 option = ICONS_ARE_LINKS;
351 }
352 else if (!strcasecmp(w, "IgnoreCase")) {
353 option = IGNORE_CASE;
354 }
355 else if (!strcasecmp(w, "IgnoreClient")) {
356 option = IGNORE_CLIENT;
357 }
358 else if (!strcasecmp(w, "ScanHTMLTitles")) {
359 option = SCAN_HTML_TITLES;
360 }
361 else if (!strcasecmp(w, "SuppressColumnSorting")) {
362 option = SUPPRESS_COLSORT;
363 }
364 else if (!strcasecmp(w, "SuppressDescription")) {
365 option = SUPPRESS_DESC;
366 }
367 else if (!strcasecmp(w, "SuppressHTMLPreamble")) {
368 option = SUPPRESS_PREAMBLE;
369 }
370 else if (!strcasecmp(w, "SuppressIcon")) {
371 option = SUPPRESS_ICON;
372 }
373 else if (!strcasecmp(w, "SuppressLastModified")) {
374 option = SUPPRESS_LAST_MOD;
375 }
376 else if (!strcasecmp(w, "SuppressSize")) {
377 option = SUPPRESS_SIZE;
378 }
379 else if (!strcasecmp(w, "SuppressRules")) {
380 option = SUPPRESS_RULES;
381 }
382 else if (!strcasecmp(w, "TrackModified")) {
383 option = TRACK_MODIFIED;
384 }
385 else if (!strcasecmp(w, "VersionSort")) {
386 option = VERSION_SORT;
387 }
388 else if (!strcasecmp(w, "XHTML")) {
389 option = EMIT_XHTML;
390 }
391 else if (!strcasecmp(w, "ShowForbidden")) {
392 option = SHOW_FORBIDDEN;
393 }
394 else if (!strcasecmp(w, "None")) {
395 if (action != '\0') {
396 return "Cannot combine '+' or '-' with 'None' keyword";
397 }
398 opts = NO_OPTIONS;
399 opts_add = 0;
400 opts_remove = 0;
401 }
402 else if (!strcasecmp(w, "IconWidth")) {
403 if (action != '-') {
404 d_cfg->icon_width = DEFAULT_ICON_WIDTH;
405 }
406 else {
407 d_cfg->icon_width = 0;
408 }
409 }
410 else if (!strncasecmp(w, "IconWidth=", 10)) {
411 if (action == '-') {
412 return "Cannot combine '-' with IconWidth=n";
413 }
414 d_cfg->icon_width = atoi(&w[10]);
415 }
416 else if (!strcasecmp(w, "IconHeight")) {
417 if (action != '-') {
418 d_cfg->icon_height = DEFAULT_ICON_HEIGHT;
419 }
420 else {
421 d_cfg->icon_height = 0;
422 }
423 }
424 else if (!strncasecmp(w, "IconHeight=", 11)) {
425 if (action == '-') {
426 return "Cannot combine '-' with IconHeight=n";
427 }
428 d_cfg->icon_height = atoi(&w[11]);
429 }
430 else if (!strcasecmp(w, "NameWidth")) {
431 if (action != '-') {
432 return "NameWidth with no value may only appear as "
433 "'-NameWidth'";
434 }
435 d_cfg->name_width = DEFAULT_NAME_WIDTH;
436 d_cfg->name_adjust = K_NOADJUST;
437 }
438 else if (!strncasecmp(w, "NameWidth=", 10)) {
439 if (action == '-') {
440 return "Cannot combine '-' with NameWidth=n";
441 }
442 if (w[10] == '*') {
443 d_cfg->name_adjust = K_ADJUST;
444 }
445 else {
446 int width = atoi(&w[10]);
447
448 if (width && (width < 5)) {
449 return "NameWidth value must be greater than 5";
450 }
451 d_cfg->name_width = width;
452 d_cfg->name_adjust = K_NOADJUST;
453 }
454 }
455 else if (!strcasecmp(w, "DescriptionWidth")) {
456 if (action != '-') {
457 return "DescriptionWidth with no value may only appear as "
458 "'-DescriptionWidth'";
459 }
460 d_cfg->desc_width = DEFAULT_DESC_WIDTH;
461 d_cfg->desc_adjust = K_NOADJUST;
462 }
463 else if (!strncasecmp(w, "DescriptionWidth=", 17)) {
464 if (action == '-') {
465 return "Cannot combine '-' with DescriptionWidth=n";
466 }
467 if (w[17] == '*') {
468 d_cfg->desc_adjust = K_ADJUST;
469 }
470 else {
471 int width = atoi(&w[17]);
472
473 if (width && (width < 12)) {
474 return "DescriptionWidth value must be greater than 12";
475 }
476 d_cfg->desc_width = width;
477 d_cfg->desc_adjust = K_NOADJUST;
478 }
479 }
480 else if (!strncasecmp(w, "Type=", 5)) {
481 d_cfg->ctype = apr_pstrdup(cmd->pool, &w[5]);
482 }
483 else if (!strncasecmp(w, "Charset=", 8)) {
484 d_cfg->charset = apr_pstrdup(cmd->pool, &w[8]);
485 }
486 else {
487 return "Invalid directory indexing option";
488 }
489 if (action == '\0') {
490 opts |= option;
491 opts_add = 0;
492 opts_remove = 0;
493 }
494 else if (action == '+') {
495 opts_add |= option;
496 opts_remove &= ~option;
497 }
498 else {
499 opts_remove |= option;
500 opts_add &= ~option;
501 }
502 }
503 if ((opts & NO_OPTIONS) && (opts & ~NO_OPTIONS)) {
504 return "Cannot combine other IndexOptions keywords with 'None'";
505 }
506 d_cfg->incremented_opts = opts_add;
507 d_cfg->decremented_opts = opts_remove;
508 d_cfg->opts = opts;
509 return NULL;
510 }
511
512 static const char *set_default_order(cmd_parms *cmd, void *m,
513 const char *direction, const char *key)
514 {
515 autoindex_config_rec *d_cfg = (autoindex_config_rec *) m;
516
517 if (!strcasecmp(direction, "Ascending")) {
518 d_cfg->default_direction = D_ASCENDING;
519 }
520 else if (!strcasecmp(direction, "Descending")) {
521 d_cfg->default_direction = D_DESCENDING;
522 }
523 else {
524 return "First keyword must be 'Ascending' or 'Descending'";
525 }
526
527 if (!strcasecmp(key, "Name")) {
528 d_cfg->default_keyid = K_NAME;
529 }
530 else if (!strcasecmp(key, "Date")) {
531 d_cfg->default_keyid = K_LAST_MOD;
532 }
533 else if (!strcasecmp(key, "Size")) {
534 d_cfg->default_keyid = K_SIZE;
535 }
536 else if (!strcasecmp(key, "Description")) {
537 d_cfg->default_keyid = K_DESC;
538 }
539 else {
540 return "Second keyword must be 'Name', 'Date', 'Size', or "
541 "'Description'";
542 }
543
544 return NULL;
545 }
546
547 #define DIR_CMD_PERMS OR_INDEXES
548
549 static const command_rec autoindex_cmds[] =
550 {
551 AP_INIT_ITERATE2("AddIcon", add_icon, BY_PATH, DIR_CMD_PERMS,
552 "an icon URL followed by one or more filenames"),
553 AP_INIT_ITERATE2("AddIconByType", add_icon, BY_TYPE, DIR_CMD_PERMS,
554 "an icon URL followed by one or more MIME types"),
555 AP_INIT_ITERATE2("AddIconByEncoding", add_icon, BY_ENCODING, DIR_CMD_PERMS,
556 "an icon URL followed by one or more content encodings"),
557 AP_INIT_ITERATE2("AddAlt", add_alt, BY_PATH, DIR_CMD_PERMS,
558 "alternate descriptive text followed by one or more "
559 "filenames"),
560 AP_INIT_ITERATE2("AddAltByType", add_alt, BY_TYPE, DIR_CMD_PERMS,
561 "alternate descriptive text followed by one or more MIME "
562 "types"),
563 AP_INIT_ITERATE2("AddAltByEncoding", add_alt, BY_ENCODING, DIR_CMD_PERMS,
564 "alternate descriptive text followed by one or more "
565 "content encodings"),
566 AP_INIT_TAKE_ARGV("IndexOptions", add_opts, NULL, DIR_CMD_PERMS,
567 "one or more index options [+|-][]"),
568 AP_INIT_TAKE2("IndexOrderDefault", set_default_order, NULL, DIR_CMD_PERMS,
569 "{Ascending,Descending} {Name,Size,Description,Date}"),
570 AP_INIT_ITERATE("IndexIgnore", add_ignore, NULL, DIR_CMD_PERMS,
571 "one or more file extensions"),
572 AP_INIT_ITERATE2("AddDescription", add_desc, BY_PATH, DIR_CMD_PERMS,
573 "Descriptive text followed by one or more filenames"),
574 AP_INIT_TAKE1("HeaderName", ap_set_string_slot,
575 (void *)APR_OFFSETOF(autoindex_config_rec, header),
576 DIR_CMD_PERMS, "a filename"),
577 AP_INIT_TAKE1("ReadmeName", ap_set_string_slot,
578 (void *)APR_OFFSETOF(autoindex_config_rec, header),
579 DIR_CMD_PERMS, "a filename"),
580 AP_INIT_RAW_ARGS("FancyIndexing", ap_set_deprecated, NULL, OR_ALL,
581 "The FancyIndexing directive is no longer supported. "
582 "Use IndexOptions FancyIndexing."),
583 AP_INIT_TAKE1("DefaultIcon", ap_set_string_slot,
584 (void *)APR_OFFSETOF(autoindex_config_rec, default_icon),
585 DIR_CMD_PERMS, "an icon URL"),
586 AP_INIT_TAKE1("IndexStyleSheet", ap_set_string_slot,
587 (void *)APR_OFFSETOF(autoindex_config_rec, style_sheet),
588 DIR_CMD_PERMS, "URL to style sheet"),
589 AP_INIT_TAKE1("IndexHeadInsert", ap_set_string_slot,
590 (void *)APR_OFFSETOF(autoindex_config_rec, head_insert),
591 DIR_CMD_PERMS, "String to insert in HTML HEAD section"),
592 {NULL}
593 };
594
595 static void *create_autoindex_config(apr_pool_t *p, char *dummy)
596 {
597 autoindex_config_rec *new =
598 (autoindex_config_rec *) apr_pcalloc(p, sizeof(autoindex_config_rec));
599
600 new->icon_width = 0;
601 new->icon_height = 0;
602 new->name_width = DEFAULT_NAME_WIDTH;
603 new->name_adjust = K_UNSET;
604 new->desc_width = DEFAULT_DESC_WIDTH;
605 new->desc_adjust = K_UNSET;
606 new->icon_list = apr_array_make(p, 4, sizeof(struct item));
607 new->alt_list = apr_array_make(p, 4, sizeof(struct item));
608 new->desc_list = apr_array_make(p, 4, sizeof(ai_desc_t));
609 new->ign_list = apr_array_make(p, 4, sizeof(struct item));
610 new->opts = 0;
611 new->incremented_opts = 0;
612 new->decremented_opts = 0;
613 new->default_keyid = '\0';
614 new->default_direction = '\0';
615
616 return (void *) new;
617 }
618
619 static void *merge_autoindex_configs(apr_pool_t *p, void *basev, void *addv)
620 {
621 autoindex_config_rec *new;
622 autoindex_config_rec *base = (autoindex_config_rec *) basev;
623 autoindex_config_rec *add = (autoindex_config_rec *) addv;
624
625 new = (autoindex_config_rec *) apr_pcalloc(p, sizeof(autoindex_config_rec));
626 new->default_icon = add->default_icon ? add->default_icon
627 : base->default_icon;
628 new->style_sheet = add->style_sheet ? add->style_sheet
629 : base->style_sheet;
630 new->head_insert = add->head_insert ? add->head_insert
631 : base->head_insert;
632 new->header = add->header ? add->header
633 : base->header;
634 new->readme = add->readme ? add->readme
635 : base->readme;
636 new->icon_height = add->icon_height ? add->icon_height : base->icon_height;
637 new->icon_width = add->icon_width ? add->icon_width : base->icon_width;
638
639 new->ctype = add->ctype ? add->ctype : base->ctype;
640 new->charset = add->charset ? add->charset : base->charset;
641
642 new->alt_list = apr_array_append(p, add->alt_list, base->alt_list);
643 new->ign_list = apr_array_append(p, add->ign_list, base->ign_list);
644 new->desc_list = apr_array_append(p, add->desc_list, base->desc_list);
645 new->icon_list = apr_array_append(p, add->icon_list, base->icon_list);
646 if (add->opts & NO_OPTIONS) {
647 /*
648 * If the current directory says 'no options' then we also
649 * clear any incremental mods from being inheritable further down.
650 */
651 new->opts = NO_OPTIONS;
652 new->incremented_opts = 0;
653 new->decremented_opts = 0;
654 }
655 else {
656 /*
657 * If there were any nonincremental options selected for
658 * this directory, they dominate and we don't inherit *anything.*
659 * Contrariwise, we *do* inherit if the only settings here are
660 * incremental ones.
661 */
662 if (add->opts == 0) {
663 new->incremented_opts = (base->incremented_opts
664 | add->incremented_opts)
665 & ~add->decremented_opts;
666 new->decremented_opts = (base->decremented_opts
667 | add->decremented_opts);
668 /*
669 * We may have incremental settings, so make sure we don't
670 * inadvertently inherit an IndexOptions None from above.
671 */
672 new->opts = (base->opts & ~NO_OPTIONS);
673 }
674 else {
675 /*
676 * There are local nonincremental settings, which clear
677 * all inheritance from above. They *are* the new base settings.
678 */
679 new->opts = add->opts;;
680 }
681 /*
682 * We're guaranteed that there'll be no overlap between
683 * the add-options and the remove-options.
684 */
685 new->opts |= new->incremented_opts;
686 new->opts &= ~new->decremented_opts;
687 }
688 /*
689 * Inherit the NameWidth settings if there aren't any specific to
690 * the new location; otherwise we'll end up using the defaults set in the
691 * config-rec creation routine.
692 */
693 if (add->name_adjust == K_UNSET) {
694 new->name_width = base->name_width;
695 new->name_adjust = base->name_adjust;
696 }
697 else {
698 new->name_width = add->name_width;
699 new->name_adjust = add->name_adjust;
700 }
701
702 /*
703 * Likewise for DescriptionWidth.
704 */
705 if (add->desc_adjust == K_UNSET) {
706 new->desc_width = base->desc_width;
707 new->desc_adjust = base->desc_adjust;
708 }
709 else {
710 new->desc_width = add->desc_width;
711 new->desc_adjust = add->desc_adjust;
712 }
713
714 new->default_keyid = add->default_keyid ? add->default_keyid
715 : base->default_keyid;
716 new->default_direction = add->default_direction ? add->default_direction
717 : base->default_direction;
718 return new;
719 }
720
721 /****************************************************************
722 *
723 * Looking things up in config entries...
724 */
725
726 /* Structure used to hold entries when we're actually building an index */
727
728 struct ent {
729 char *name;
730 char *icon;
731 char *alt;
732 char *desc;
733 apr_off_t size;
734 apr_time_t lm;
735 struct ent *next;
736 int ascending, ignore_case, version_sort;
737 char key;
738 int isdir;
739 };
740
741 static char *find_item(const char *content_type, const char *content_encoding,
742 char *path, apr_array_header_t *list, int path_only)
743 {
744 struct item *items = (struct item *) list->elts;
745 int i;
746
747 for (i = 0; i < list->nelts; ++i) {
748 struct item *p = &items[i];
749
750 /* Special cased for ^^DIRECTORY^^ and ^^BLANKICON^^ */
751 if ((path[0] == '^') || (!ap_strcmp_match(path, p->apply_path))) {
752 if (!*(p->apply_to)) {
753 return p->data;
754 }
755 else if (p->type == BY_PATH || path[0] == '^') {
756 if (!ap_strcmp_match(path, p->apply_to)) {
757 return p->data;
758 }
759 }
760 else if (!path_only) {
761 if (!content_encoding) {
762 if (p->type == BY_TYPE) {
763 if (content_type
764 && !ap_strcasecmp_match(content_type,
765 p->apply_to)) {
766 return p->data;
767 }
768 }
769 }
770 else {
771 if (p->type == BY_ENCODING) {
772 if (!ap_strcasecmp_match(content_encoding,
773 p->apply_to)) {
774 return p->data;
775 }
776 }
777 }
778 }
779 }
780 }
781 return NULL;
782 }
783
784 static char *find_item_by_request(request_rec *r, apr_array_header_t *list, int path_only)
785 {
786 return find_item(ap_field_noparam(r->pool, r->content_type),
787 r->content_encoding, r->filename, list, path_only);
788 }
789
790 #define find_icon(d,p,t) find_item_by_request(p,d->icon_list,t)
791 #define find_alt(d,p,t) find_item_by_request(p,d->alt_list,t)
792 #define find_default_icon(d,n) find_item(NULL, NULL, n, d->icon_list, 1)
793 #define find_default_alt(d,n) find_item(NULL, NULL, n, d->alt_list, 1)
794
795 /*
796 * Look through the list of pattern/description pairs and return the first one
797 * if any) that matches the filename in the request. If multiple patterns
798 * match, only the first one is used; since the order in the array is the
799 * same as the order in which directives were processed, earlier matching
800 * directives will dominate.
801 */
802
803 #ifdef CASE_BLIND_FILESYSTEM
804 #define MATCH_FLAGS APR_FNM_CASE_BLIND
805 #else
806 #define MATCH_FLAGS 0
807 #endif
808
809 static char *find_desc(autoindex_config_rec *dcfg, const char *filename_full)
810 {
811 int i;
812 ai_desc_t *list = (ai_desc_t *) dcfg->desc_list->elts;
813 const char *filename_only;
814 const char *filename;
815
816 /*
817 * If the filename includes a path, extract just the name itself
818 * for the simple matches.
819 */
820 if ((filename_only = ap_strrchr_c(filename_full, '/')) == NULL) {
821 filename_only = filename_full;
822 }
823 else {
824 filename_only++;
825 }
826 for (i = 0; i < dcfg->desc_list->nelts; ++i) {
827 ai_desc_t *tuple = &list[i];
828 int found;
829
830 /*
831 * Only use the full-path filename if the pattern contains '/'s.
832 */
833 filename = (tuple->full_path) ? filename_full : filename_only;
834 /*
835 * Make the comparison using the cheapest method; only do
836 * wildcard checking if we must.
837 */
838 if (tuple->wildcards) {
839 found = (apr_fnmatch(tuple->pattern, filename, MATCH_FLAGS) == 0);
840 }
841 else {
842 found = (ap_strstr_c(filename, tuple->pattern) != NULL);
843 }
844 if (found) {
845 return tuple->description;
846 }
847 }
848 return NULL;
849 }
850
851 static int ignore_entry(autoindex_config_rec *d, char *path)
852 {
853 apr_array_header_t *list = d->ign_list;
854 struct item *items = (struct item *) list->elts;
855 char *tt;
856 int i;
857
858 if ((tt = strrchr(path, '/')) == NULL) {
859 tt = path;
860 }
861 else {
862 tt++;
863 }
864
865 for (i = 0; i < list->nelts; ++i) {
866 struct item *p = &items[i];
867 char *ap;
868
869 if ((ap = strrchr(p->apply_to, '/')) == NULL) {
870 ap = p->apply_to;
871 }
872 else {
873 ap++;
874 }
875
876 #ifndef CASE_BLIND_FILESYSTEM
877 if (!ap_strcmp_match(path, p->apply_path)
878 && !ap_strcmp_match(tt, ap)) {
879 return 1;
880 }
881 #else /* !CASE_BLIND_FILESYSTEM */
882 /*
883 * On some platforms, the match must be case-blind. This is really
884 * a factor of the filesystem involved, but we can't detect that
885 * reliably - so we have to granularise at the OS level.
886 */
887 if (!ap_strcasecmp_match(path, p->apply_path)
888 && !ap_strcasecmp_match(tt, ap)) {
889 return 1;
890 }
891 #endif /* !CASE_BLIND_FILESYSTEM */
892 }
893 return 0;
894 }
895
896 /*****************************************************************
897 *
898 * Actually generating output
899 */
900
901 /*
902 * Elements of the emitted document:
903 * Preamble
904 * Emitted unless SUPPRESS_PREAMBLE is set AND ap_run_sub_req
905 * succeeds for the (content_type == text/html) header file.
906 * Header file
907 * Emitted if found (and able).
908 * H1 tag line
909 * Emitted if a header file is NOT emitted.
910 * Directory stuff
911 * Always emitted.
912 * HR
913 * Emitted if FANCY_INDEXING is set.
914 * Readme file
915 * Emitted if found (and able).
916 * ServerSig
917 * Emitted if ServerSignature is not Off AND a readme file
918 * is NOT emitted.
919 * Postamble
920 * Emitted unless SUPPRESS_PREAMBLE is set AND ap_run_sub_req
921 * succeeds for the (content_type == text/html) readme file.
922 */
923
924
925 /*
926 * emit a plain text file
927 */
928 static void do_emit_plain(request_rec *r, apr_file_t *f)
929 {
930 char buf[AP_IOBUFSIZE + 1];
931 int ch;
932 apr_size_t i, c, n;
933 apr_status_t rv;
934
935 ap_rputs("<pre>\n", r);
936 while (!apr_file_eof(f)) {
937 do {
938 n = sizeof(char) * AP_IOBUFSIZE;
939 rv = apr_file_read(f, buf, &n);
940 } while (APR_STATUS_IS_EINTR(rv));
941 if (n == 0 || rv != APR_SUCCESS) {
942 /* ###: better error here? */
943 break;
944 }
945 buf[n] = '\0';
946 c = 0;
947 while (c < n) {
948 for (i = c; i < n; i++) {
949 if (buf[i] == '<' || buf[i] == '>' || buf[i] == '&') {
950 break;
951 }
952 }
953 ch = buf[i];
954 buf[i] = '\0';
955 ap_rputs(&buf[c], r);
956 if (ch == '<') {
957 ap_rputs("&lt;", r);
958 }
959 else if (ch == '>') {
960 ap_rputs("&gt;", r);
961 }
962 else if (ch == '&') {
963 ap_rputs("&amp;", r);
964 }
965 c = i + 1;
966 }
967 }
968 ap_rputs("</pre>\n", r);
969 }
970
971 /*
972 * Handle the preamble through the H1 tag line, inclusive. Locate
973 * the file with a subrequests. Process text/html documents by actually
974 * running the subrequest; text/xxx documents get copied verbatim,
975 * and any other content type is ignored. This means that a non-text
976 * document (such as HEADER.gif) might get multiviewed as the result
977 * instead of a text document, meaning nothing will be displayed, but
978 * oh well.
979 */
980 static void emit_head(request_rec *r, char *header_fname, int suppress_amble,
981 int emit_xhtml, char *title)
982 {
983 apr_table_t *hdrs = r->headers_in;
984 apr_file_t *f = NULL;
985 request_rec *rr = NULL;
986 int emit_amble = 1;
987 int emit_H1 = 1;
988 const char *r_accept;
989 const char *r_accept_enc;
990
991 /*
992 * If there's a header file, send a subrequest to look for it. If it's
993 * found and html do the subrequest, otherwise handle it
994 */
995 r_accept = apr_table_get(hdrs, "Accept");
996 r_accept_enc = apr_table_get(hdrs, "Accept-Encoding");
997 apr_table_setn(hdrs, "Accept", "text/html, text/plain");
998 apr_table_unset(hdrs, "Accept-Encoding");
999
1000
1001 if ((header_fname != NULL) && r->args) {
1002 header_fname = apr_pstrcat(r->pool, header_fname, "?", r->args, NULL);
1003 }
1004
1005 if ((header_fname != NULL)
1006 && (rr = ap_sub_req_lookup_uri(header_fname, r, r->output_filters))
1007 && (rr->status == HTTP_OK)
1008 && (rr->filename != NULL)
1009 && (rr->finfo.filetype == APR_REG)) {
1010 /*
1011 * Check for the two specific cases we allow: text/html and
1012 * text/anything-else. The former is allowed to be processed for
1013 * SSIs.
1014 */
1015 if (rr->content_type != NULL) {
1016 if (!strcasecmp(ap_field_noparam(r->pool, rr->content_type),
1017 "text/html")) {
1018 ap_filter_t *f;
1019 /* Hope everything will work... */
1020 emit_amble = 0;
1021 emit_H1 = 0;
1022
1023 if (! suppress_amble) {
1024 emit_preamble(r, emit_xhtml, title);
1025 }
1026 /* This is a hack, but I can't find any better way to do this.
1027 * The problem is that we have already created the sub-request,
1028 * but we just inserted the OLD_WRITE filter, and the
1029 * sub-request needs to pass its data through the OLD_WRITE
1030 * filter, or things go horribly wrong (missing data, data in
1031 * the wrong order, etc). To fix it, if you create a
1032 * sub-request and then insert the OLD_WRITE filter before you
1033 * run the request, you need to make sure that the sub-request
1034 * data goes through the OLD_WRITE filter. Just steal this
1035 * code. The long-term solution is to remove the ap_r*
1036 * functions.
1037 */
1038 for (f=rr->output_filters;
1039 f->frec != ap_subreq_core_filter_handle; f = f->next);
1040 f->next = r->output_filters;
1041
1042 /*
1043 * If there's a problem running the subrequest, display the
1044 * preamble if we didn't do it before -- the header file
1045 * didn't get displayed.
1046 */
1047 if (ap_run_sub_req(rr) != OK) {
1048 /* It didn't work */
1049 emit_amble = suppress_amble;
1050 emit_H1 = 1;
1051 }
1052 }
1053 else if (!strncasecmp("text/", rr->content_type, 5)) {
1054 /*
1055 * If we can open the file, prefix it with the preamble
1056 * regardless; since we'll be sending a <pre> block around
1057 * the file's contents, any HTML header it had won't end up
1058 * where it belongs.
1059 */
1060 if (apr_file_open(&f, rr->filename, APR_READ,
1061 APR_OS_DEFAULT, r->pool) == APR_SUCCESS) {
1062 emit_preamble(r, emit_xhtml, title);
1063 emit_amble = 0;
1064 do_emit_plain(r, f);
1065 apr_file_close(f);
1066 emit_H1 = 0;
1067 }
1068 }
1069 }
1070 }
1071
1072 if (r_accept) {
1073 apr_table_setn(hdrs, "Accept", r_accept);
1074 }
1075 else {
1076 apr_table_unset(hdrs, "Accept");
1077 }
1078
1079 if (r_accept_enc) {
1080 apr_table_setn(hdrs, "Accept-Encoding", r_accept_enc);
1081 }
1082
1083 if (emit_amble) {
1084 emit_preamble(r, emit_xhtml, title);
1085 }
1086 if (emit_H1) {
1087 ap_rvputs(r, "<h1>Index of ", title, "</h1>\n", NULL);
1088 }
1089 if (rr != NULL) {
1090 ap_destroy_sub_req(rr);
1091 }
1092 }
1093
1094
1095 /*
1096 * Handle the Readme file through the postamble, inclusive. Locate
1097 * the file with a subrequests. Process text/html documents by actually
1098 * running the subrequest; text/xxx documents get copied verbatim,
1099 * and any other content type is ignored. This means that a non-text
1100 * document (such as FOOTER.gif) might get multiviewed as the result
1101 * instead of a text document, meaning nothing will be displayed, but
1102 * oh well.
1103 */
1104 static void emit_tail(request_rec *r, char *readme_fname, int suppress_amble)
1105 {
1106 apr_file_t *f = NULL;
1107 request_rec *rr = NULL;
1108 int suppress_post = 0;
1109 int suppress_sig = 0;
1110
1111 /*
1112 * If there's a readme file, send a subrequest to look for it. If it's
1113 * found and a text file, handle it -- otherwise fall through and
1114 * pretend there's nothing there.
1115 */
1116 if ((readme_fname != NULL)
1117 && (rr = ap_sub_req_lookup_uri(readme_fname, r, r->output_filters))
1118 && (rr->status == HTTP_OK)
1119 && (rr->filename != NULL)
1120 && rr->finfo.filetype == APR_REG) {
1121 /*
1122 * Check for the two specific cases we allow: text/html and
1123 * text/anything-else. The former is allowed to be processed for
1124 * SSIs.
1125 */
1126 if (rr->content_type != NULL) {
1127 if (!strcasecmp(ap_field_noparam(r->pool, rr->content_type),
1128 "text/html")) {
1129 ap_filter_t *f;
1130 for (f=rr->output_filters;
1131 f->frec != ap_subreq_core_filter_handle; f = f->next);
1132 f->next = r->output_filters;
1133
1134
1135 if (ap_run_sub_req(rr) == OK) {
1136 /* worked... */
1137 suppress_sig = 1;
1138 suppress_post = suppress_amble;
1139 }
1140 }
1141 else if (!strncasecmp("text/", rr->content_type, 5)) {
1142 /*
1143 * If we can open the file, suppress the signature.
1144 */
1145 if (apr_file_open(&f, rr->filename, APR_READ,
1146 APR_OS_DEFAULT, r->pool) == APR_SUCCESS) {
1147 do_emit_plain(r, f);
1148 apr_file_close(f);
1149 suppress_sig = 1;
1150 }
1151 }
1152 }
1153 }
1154
1155 if (!suppress_sig) {
1156 ap_rputs(ap_psignature("", r), r);
1157 }
1158 if (!suppress_post) {
1159 ap_rputs("</body></html>\n", r);
1160 }
1161 if (rr != NULL) {
1162 ap_destroy_sub_req(rr);
1163 }
1164 }
1165
1166
1167 static char *find_title(request_rec *r)
1168 {
1169 char titlebuf[MAX_STRING_LEN], *find = "<title>";
1170 apr_file_t *thefile = NULL;
1171 int x, y, p;
1172 apr_size_t n;
1173
1174 if (r->status != HTTP_OK) {
1175 return NULL;
1176 }
1177 if ((r->content_type != NULL)
1178 && (!strcasecmp(ap_field_noparam(r->pool, r->content_type),
1179 "text/html")
1180 || !strcmp(r->content_type, INCLUDES_MAGIC_TYPE))
1181 && !r->content_encoding) {
1182 if (apr_file_open(&thefile, r->filename, APR_READ,
1183 APR_OS_DEFAULT, r->pool) != APR_SUCCESS) {
1184 return NULL;
1185 }
1186 n = sizeof(char) * (MAX_STRING_LEN - 1);
1187 apr_file_read(thefile, titlebuf, &n);
1188 if (n <= 0) {
1189 apr_file_close(thefile);
1190 return NULL;
1191 }
1192 titlebuf[n] = '\0';
1193 for (x = 0, p = 0; titlebuf[x]; x++) {
1194 if (apr_tolower(titlebuf[x]) == find[p]) {
1195 if (!find[++p]) {
1196 if ((p = ap_ind(&titlebuf[++x], '<')) != -1) {
1197 titlebuf[x + p] = '\0';
1198 }
1199 /* Scan for line breaks for Tanmoy's secretary */
1200 for (y = x; titlebuf[y]; y++) {
1201 if ((titlebuf[y] == CR) || (titlebuf[y] == LF)) {
1202 if (y == x) {
1203 x++;
1204 }
1205 else {
1206 titlebuf[y] = ' ';
1207 }
1208 }
1209 }
1210 apr_file_close(thefile);
1211 return apr_pstrdup(r->pool, &titlebuf[x]);
1212 }
1213 }
1214 else {
1215 p = 0;
1216 }
1217 }
1218 apr_file_close(thefile);
1219 }
1220 return NULL;
1221 }
1222
1223 static struct ent *make_parent_entry(apr_int32_t autoindex_opts,
1224 autoindex_config_rec *d,
1225 request_rec *r, char keyid,
1226 char direction)
1227 {
1228 struct ent *p = (struct ent *) apr_pcalloc(r->pool, sizeof(struct ent));
1229 char *testpath;
1230 /*
1231 * p->name is now the true parent URI.
1232 * testpath is a crafted lie, so that the syntax '/some/..'
1233 * (or simply '..')be used to describe 'up' from '/some/'
1234 * when processeing IndexIgnore, and Icon|Alt|Desc configs.
1235 */
1236
1237 /* The output has always been to the parent. Don't make ourself
1238 * our own parent (worthless cyclical reference).
1239 */
1240 if (!(p->name = ap_make_full_path(r->pool, r->uri, "../"))) {
1241 return (NULL);
1242 }
1243 ap_getparents(p->name);
1244 if (!*p->name) {
1245 return (NULL);
1246 }
1247
1248 /* IndexIgnore has always compared "/thispath/.." */
1249 testpath = ap_make_full_path(r->pool, r->filename, "..");
1250 if (ignore_entry(d, testpath)) {
1251 return (NULL);
1252 }
1253
1254 p->size = -1;
1255 p->lm = -1;
1256 p->key = apr_toupper(keyid);
1257 p->ascending = (apr_toupper(direction) == D_ASCENDING);
1258 p->version_sort = autoindex_opts & VERSION_SORT;
1259 if (autoindex_opts & FANCY_INDEXING) {
1260 if (!(p->icon = find_default_icon(d, testpath))) {
1261 p->icon = find_default_icon(d, "^^DIRECTORY^^");
1262 }
1263 if (!(p->alt = find_default_alt(d, testpath))) {
1264 if (!(p->alt = find_default_alt(d, "^^DIRECTORY^^"))) {
1265 p->alt = "DIR";
1266 }
1267 }
1268 p->desc = find_desc(d, testpath);
1269 }
1270 return p;
1271 }
1272
1273 static struct ent *make_autoindex_entry(const apr_finfo_t *dirent,
1274 int autoindex_opts,
1275 autoindex_config_rec *d,
1276 request_rec *r, char keyid,
1277 char direction,
1278 const char *pattern)
1279 {
1280 request_rec *rr;
1281 struct ent *p;
1282 int show_forbidden = 0;
1283
1284 /* Dot is ignored, Parent is handled by make_parent_entry() */
1285 if ((dirent->name[0] == '.') && (!dirent->name[1]
1286 || ((dirent->name[1] == '.') && !dirent->name[2])))
1287 return (NULL);
1288
1289 /*
1290 * On some platforms, the match must be case-blind. This is really
1291 * a factor of the filesystem involved, but we can't detect that
1292 * reliably - so we have to granularise at the OS level.
1293 */
1294 if (pattern && (apr_fnmatch(pattern, dirent->name,
1295 APR_FNM_NOESCAPE | APR_FNM_PERIOD
1296 #ifdef CASE_BLIND_FILESYSTEM
1297 | APR_FNM_CASE_BLIND
1298 #endif
1299 )
1300 != APR_SUCCESS)) {
1301 return (NULL);
1302 }
1303
1304 if (ignore_entry(d, ap_make_full_path(r->pool,
1305 r->filename, dirent->name))) {
1306 return (NULL);
1307 }
1308
1309 if (!(rr = ap_sub_req_lookup_dirent(dirent, r, AP_SUBREQ_NO_ARGS, NULL))) {
1310 return (NULL);
1311 }
1312
1313 if((autoindex_opts & SHOW_FORBIDDEN)
1314 && (rr->status == HTTP_UNAUTHORIZED || rr->status == HTTP_FORBIDDEN)) {
1315 show_forbidden = 1;
1316 }
1317
1318 if ((rr->finfo.filetype != APR_DIR && rr->finfo.filetype != APR_REG)
1319 || !(rr->status == OK || ap_is_HTTP_SUCCESS(rr->status)
1320 || ap_is_HTTP_REDIRECT(rr->status)
1321 || show_forbidden == 1)) {
1322 ap_destroy_sub_req(rr);
1323 return (NULL);
1324 }
1325
1326 p = (struct ent *) apr_pcalloc(r->pool, sizeof(struct ent));
1327 if (dirent->filetype == APR_DIR) {
1328 p->name = apr_pstrcat(r->pool, dirent->name, "/", NULL);
1329 }
1330 else {
1331 p->name = apr_pstrdup(r->pool, dirent->name);
1332 }
1333 p->size = -1;
1334 p->icon = NULL;
1335 p->alt = NULL;
1336 p->desc = NULL;
1337 p->lm = -1;
1338 p->isdir = 0;
1339 p->key = apr_toupper(keyid);
1340 p->ascending = (apr_toupper(direction) == D_ASCENDING);
1341 p->version_sort = !!(autoindex_opts & VERSION_SORT);
1342 p->ignore_case = !!(autoindex_opts & IGNORE_CASE);
1343
1344 if (autoindex_opts & (FANCY_INDEXING | TABLE_INDEXING)) {
1345 p->lm = rr->finfo.mtime;
1346 if (dirent->filetype == APR_DIR) {
1347 if (autoindex_opts & FOLDERS_FIRST) {
1348 p->isdir = 1;
1349 }
1350 rr->filename = ap_make_dirstr_parent (rr->pool, rr->filename);
1351
1352 /* omit the trailing slash (1.3 compat) */
1353 rr->filename[strlen(rr->filename) - 1] = '\0';
1354
1355 if (!(p->icon = find_icon(d, rr, 1))) {
1356 p->icon = find_default_icon(d, "^^DIRECTORY^^");
1357 }
1358 if (!(p->alt = find_alt(d, rr, 1))) {
1359 if (!(p->alt = find_default_alt(d, "^^DIRECTORY^^"))) {
1360 p->alt = "DIR";
1361 }
1362 }
1363 }
1364 else {
1365 p->icon = find_icon(d, rr, 0);
1366 p->alt = find_alt(d, rr, 0);
1367 p->size = rr->finfo.size;
1368 }
1369
1370 p->desc = find_desc(d, rr->filename);
1371
1372 if ((!p->desc) && (autoindex_opts & SCAN_HTML_TITLES)) {
1373 p->desc = apr_pstrdup(r->pool, find_title(rr));
1374 }
1375 }
1376 ap_destroy_sub_req(rr);
1377 /*
1378 * We don't need to take any special action for the file size key.
1379 * If we did, it would go here.
1380 */
1381 if (keyid == K_LAST_MOD) {
1382 if (p->lm < 0) {
1383 p->lm = 0;
1384 }
1385 }
1386 return (p);
1387 }
1388
1389 static char *terminate_description(autoindex_config_rec *d, char *desc,
1390 apr_int32_t autoindex_opts, int desc_width)
1391 {
1392 int maxsize = desc_width;
1393 register int x;
1394
1395 /*
1396 * If there's no DescriptionWidth in effect, default to the old
1397 * behaviour of adjusting the description size depending upon
1398 * what else is being displayed. Otherwise, stick with the
1399 * setting.
1400 */
1401 if (d->desc_adjust == K_UNSET) {
1402 if (autoindex_opts & SUPPRESS_ICON) {
1403 maxsize += 6;
1404 }
1405 if (autoindex_opts & SUPPRESS_LAST_MOD) {
1406 maxsize += 19;
1407 }
1408 if (autoindex_opts & SUPPRESS_SIZE) {
1409 maxsize += 7;
1410 }
1411 }
1412 for (x = 0; desc[x] && ((maxsize > 0) || (desc[x] == '<')); x++) {
1413 if (desc[x] == '<') {
1414 while (desc[x] != '>') {
1415 if (!desc[x]) {
1416 maxsize = 0;
1417 break;
1418 }
1419 ++x;
1420 }
1421 }
1422 else if (desc[x] == '&') {
1423 /* entities like &auml; count as one character */
1424 --maxsize;
1425 for ( ; desc[x] != ';'; ++x) {
1426 if (desc[x] == '\0') {
1427 maxsize = 0;
1428 break;
1429 }
1430 }
1431 }
1432 else {
1433 --maxsize;
1434 }
1435 }
1436 if (!maxsize && desc[x] != '\0') {
1437 desc[x - 1] = '>'; /* Grump. */
1438 desc[x] = '\0'; /* Double Grump! */
1439 }
1440 return desc;
1441 }
1442
1443 /*
1444 * Emit the anchor for the specified field. If a field is the key for the
1445 * current request, the link changes its meaning to reverse the order when
1446 * selected again. Non-active fields always start in ascending order.
1447 */
1448 static void emit_link(request_rec *r, const char *anchor, char column,
1449 char curkey, char curdirection,
1450 const char *colargs, int nosort)
1451 {
1452 if (!nosort) {
1453 char qvalue[9];
1454
1455 qvalue[0] = '?';
1456 qvalue[1] = 'C';
1457 qvalue[2] = '=';
1458 qvalue[3] = column;
1459 qvalue[4] = ';';
1460 qvalue[5] = 'O';
1461 qvalue[6] = '=';
1462 /* reverse? */
1463 qvalue[7] = ((curkey == column) && (curdirection == D_ASCENDING))
1464 ? D_DESCENDING : D_ASCENDING;
1465 qvalue[8] = '\0';
1466 ap_rvputs(r, "<a href=\"", qvalue, colargs ? colargs : "",
1467 "\">", anchor, "</a>", NULL);
1468 }
1469 else {
1470 ap_rputs(anchor, r);
1471 }
1472 }
1473
1474 static void output_directories(struct ent **ar, int n,
1475 autoindex_config_rec *d, request_rec *r,
1476 apr_int32_t autoindex_opts, char keyid,
1477 char direction, const char *colargs)
1478 {
1479 int x;
1480 apr_size_t rv;
1481 char *name = r->uri;
1482 char *tp;
1483 int static_columns = !!(autoindex_opts & SUPPRESS_COLSORT);
1484 apr_pool_t *scratch;
1485 int name_width;
1486 int desc_width;
1487 char *name_scratch;
1488 char *pad_scratch;
1489 char *breakrow = "";
1490
1491 apr_pool_create(&scratch, r->pool);
1492 if (name[0] == '\0') {
1493 name = "/";
1494 }
1495
1496 name_width = d->name_width;
1497 desc_width = d->desc_width;
1498
1499 if ((autoindex_opts & (FANCY_INDEXING | TABLE_INDEXING))
1500 == FANCY_INDEXING) {
1501 if (d->name_adjust == K_ADJUST) {
1502 for (x = 0; x < n; x++) {
1503 int t = strlen(ar[x]->name);
1504 if (t > name_width) {
1505 name_width = t;
1506 }
1507 }
1508 }
1509
1510 if (d->desc_adjust == K_ADJUST) {
1511 for (x = 0; x < n; x++) {
1512 if (ar[x]->desc != NULL) {
1513 int t = strlen(ar[x]->desc);
1514 if (t > desc_width) {
1515 desc_width = t;
1516 }
1517 }
1518 }
1519 }
1520 }
1521 name_scratch = apr_palloc(r->pool, name_width + 1);
1522 pad_scratch = apr_palloc(r->pool, name_width + 1);
1523 memset(pad_scratch, ' ', name_width);
1524 pad_scratch[name_width] = '\0';
1525
1526 if (autoindex_opts & TABLE_INDEXING) {
1527 int cols = 1;
1528 ap_rputs("<table><tr>", r);
1529 if (!(autoindex_opts & SUPPRESS_ICON)) {
1530 ap_rputs("<th>", r);
1531 if ((tp = find_default_icon(d, "^^BLANKICON^^"))) {
1532 ap_rvputs(r, "<img src=\"", ap_escape_html(scratch, tp),
1533 "\" alt=\"[ICO]\"", NULL);
1534 if (d->icon_width) {
1535 ap_rprintf(r, " width=\"%d\"", d->icon_width);
1536 }
1537 if (d->icon_height) {
1538 ap_rprintf(r, " height=\"%d\"", d->icon_height);
1539 }
1540
1541 if (autoindex_opts & EMIT_XHTML) {
1542 ap_rputs(" /", r);
1543 }
1544 ap_rputs("></th>", r);
1545 }
1546 else {
1547 ap_rputs("&nbsp;</th>", r);
1548 }
1549
1550 ++cols;
1551 }
1552 ap_rputs("<th>", r);
1553 emit_link(r, "Name", K_NAME, keyid, direction,
1554 colargs, static_columns);
1555 if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1556 ap_rputs("</th><th>", r);
1557 emit_link(r, "Last modified", K_LAST_MOD, keyid, direction,
1558 colargs, static_columns);
1559 ++cols;
1560 }
1561 if (!(autoindex_opts & SUPPRESS_SIZE)) {
1562 ap_rputs("</th><th>", r);
1563 emit_link(r, "Size", K_SIZE, keyid, direction,
1564 colargs, static_columns);
1565 ++cols;
1566 }
1567 if (!(autoindex_opts & SUPPRESS_DESC)) {
1568 ap_rputs("</th><th>", r);
1569 emit_link(r, "Description", K_DESC, keyid, direction,
1570 colargs, static_columns);
1571 ++cols;
1572 }
1573 if (!(autoindex_opts & SUPPRESS_RULES)) {
1574 breakrow = apr_psprintf(r->pool,
1575 "<tr><th colspan=\"%d\">"
1576 "<hr%s></th></tr>\n", cols,
1577 (autoindex_opts & EMIT_XHTML) ? " /" : "");
1578 }
1579 ap_rvputs(r, "</th></tr>", breakrow, NULL);
1580 }
1581 else if (autoindex_opts & FANCY_INDEXING) {
1582 ap_rputs("<pre>", r);
1583 if (!(autoindex_opts & SUPPRESS_ICON)) {
1584 if ((tp = find_default_icon(d, "^^BLANKICON^^"))) {
1585 ap_rvputs(r, "<img src=\"", ap_escape_html(scratch, tp),
1586 "\" alt=\"Icon \"", NULL);
1587 if (d->icon_width) {
1588 ap_rprintf(r, " width=\"%d\"", d->icon_width);
1589 }
1590 if (d->icon_height) {
1591 ap_rprintf(r, " height=\"%d\"", d->icon_height);
1592 }
1593
1594 if (autoindex_opts & EMIT_XHTML) {
1595 ap_rputs(" /", r);
1596 }
1597 ap_rputs("> ", r);
1598 }
1599 else {
1600 ap_rputs(" ", r);
1601 }
1602 }
1603 emit_link(r, "Name", K_NAME, keyid, direction,
1604 colargs, static_columns);
1605 ap_rputs(pad_scratch + 4, r);
1606 /*
1607 * Emit the guaranteed-at-least-one-space-between-columns byte.
1608 */
1609 ap_rputs(" ", r);
1610 if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1611 emit_link(r, "Last modified", K_LAST_MOD, keyid, direction,
1612 colargs, static_columns);
1613 ap_rputs(" ", r);
1614 }
1615 if (!(autoindex_opts & SUPPRESS_SIZE)) {
1616 emit_link(r, "Size", K_SIZE, keyid, direction,
1617 colargs, static_columns);
1618 ap_rputs(" ", r);
1619 }
1620 if (!(autoindex_opts & SUPPRESS_DESC)) {
1621 emit_link(r, "Description", K_DESC, keyid, direction,
1622 colargs, static_columns);
1623 }
1624 if (!(autoindex_opts & SUPPRESS_RULES)) {
1625 ap_rputs("<hr", r);
1626 if (autoindex_opts & EMIT_XHTML) {
1627 ap_rputs(" /", r);
1628 }
1629 ap_rputs(">", r);
1630 }
1631 else {
1632 ap_rputc('\n', r);
1633 }
1634 }
1635 else {
1636 ap_rputs("<ul>", r);
1637 }
1638
1639 for (x = 0; x < n; x++) {
1640 char *anchor, *t, *t2;
1641 int nwidth;
1642
1643 apr_pool_clear(scratch);
1644
1645 t = ar[x]->name;
1646 anchor = ap_escape_html(scratch, ap_os_escape_path(scratch, t, 0));
1647
1648 if (!x && t[0] == '/') {
1649 t2 = "Parent Directory";
1650 }
1651 else {
1652 t2 = t;
1653 }
1654
1655 if (autoindex_opts & TABLE_INDEXING) {
1656 ap_rputs("<tr>", r);
1657 if (!(autoindex_opts & SUPPRESS_ICON)) {
1658 ap_rputs("<td valign=\"top\">", r);
1659 if (autoindex_opts & ICONS_ARE_LINKS) {
1660 ap_rvputs(r, "<a href=\"", anchor, "\">", NULL);
1661 }
1662 if ((ar[x]->icon) || d->default_icon) {
1663 ap_rvputs(r, "<img src=\"",
1664 ap_escape_html(scratch,
1665 ar[x]->icon ? ar[x]->icon
1666 : d->default_icon),
1667 "\" alt=\"[", (ar[x]->alt ? ar[x]->alt : " "),
1668 "]\"", NULL);
1669 if (d->icon_width) {
1670 ap_rprintf(r, " width=\"%d\"", d->icon_width);
1671 }
1672 if (d->icon_height) {
1673 ap_rprintf(r, " height=\"%d\"", d->icon_height);
1674 }
1675
1676 if (autoindex_opts & EMIT_XHTML) {
1677 ap_rputs(" /", r);
1678 }
1679 ap_rputs(">", r);
1680 }
1681 else {
1682 ap_rputs("&nbsp;", r);
1683 }
1684 if (autoindex_opts & ICONS_ARE_LINKS) {
1685 ap_rputs("</a></td>", r);
1686 }
1687 else {
1688 ap_rputs("</td>", r);
1689 }
1690 }
1691 if (d->name_adjust == K_ADJUST) {
1692 ap_rvputs(r, "<td><a href=\"", anchor, "\">",
1693 ap_escape_html(scratch, t2), "</a>", NULL);
1694 }
1695 else {
1696 nwidth = strlen(t2);
1697 if (nwidth > name_width) {
1698 memcpy(name_scratch, t2, name_width - 3);
1699 name_scratch[name_width - 3] = '.';
1700 name_scratch[name_width - 2] = '.';
1701 name_scratch[name_width - 1] = '>';
1702 name_scratch[name_width] = 0;
1703 t2 = name_scratch;
1704 nwidth = name_width;
1705 }
1706 ap_rvputs(r, "<td><a href=\"", anchor, "\">",
1707 ap_escape_html(scratch, t2),
1708 "</a>", pad_scratch + nwidth, NULL);
1709 }
1710 if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1711 if (ar[x]->lm != -1) {
1712 char time_str[MAX_STRING_LEN];
1713 apr_time_exp_t ts;
1714 apr_time_exp_lt(&ts, ar[x]->lm);
1715 apr_strftime(time_str, &rv, MAX_STRING_LEN,
1716 "</td><td align=\"right\">%d-%b-%Y %H:%M ",
1717 &ts);
1718 ap_rputs(time_str, r);
1719 }
1720 else {
1721 ap_rputs("</td><td>&nbsp;", r);
1722 }
1723 }
1724 if (!(autoindex_opts & SUPPRESS_SIZE)) {
1725 char buf[5];
1726 ap_rvputs(r, "</td><td align=\"right\">",
1727 apr_strfsize(ar[x]->size, buf), NULL);
1728 }
1729 if (!(autoindex_opts & SUPPRESS_DESC)) {
1730 if (ar[x]->desc) {
1731 if (d->desc_adjust == K_ADJUST) {
1732 ap_rvputs(r, "</td><td>", ar[x]->desc, NULL);
1733 }
1734 else {
1735 ap_rvputs(r, "</td><td>",
1736 terminate_description(d, ar[x]->desc,
1737 autoindex_opts,
1738 desc_width), NULL);
1739 }
1740 }
1741 }
1742 else {
1743 ap_rputs("</td><td>&nbsp;", r);
1744 }
1745 ap_rputs("</td></tr>\n", r);
1746 }
1747 else if (autoindex_opts & FANCY_INDEXING) {
1748 if (!(autoindex_opts & SUPPRESS_ICON)) {
1749 if (autoindex_opts & ICONS_ARE_LINKS) {
1750 ap_rvputs(r, "<a href=\"", anchor, "\">", NULL);
1751 }
1752 if ((ar[x]->icon) || d->default_icon) {
1753 ap_rvputs(r, "<img src=\"",
1754 ap_escape_html(scratch,
1755 ar[x]->icon ? ar[x]->icon
1756 : d->default_icon),
1757 "\" alt=\"[", (ar[x]->alt ? ar[x]->alt : " "),
1758 "]\"", NULL);
1759 if (d->icon_width) {
1760 ap_rprintf(r, " width=\"%d\"", d->icon_width);
1761 }
1762 if (d->icon_height) {
1763 ap_rprintf(r, " height=\"%d\"", d->icon_height);
1764 }
1765
1766 if (autoindex_opts & EMIT_XHTML) {
1767 ap_rputs(" /", r);
1768 }
1769 ap_rputs(">", r);
1770 }
1771 else {
1772 ap_rputs(" ", r);
1773 }
1774 if (autoindex_opts & ICONS_ARE_LINKS) {
1775 ap_rputs("</a> ", r);
1776 }
1777 else {
1778 ap_rputc(' ', r);
1779 }
1780 }
1781 nwidth = strlen(t2);
1782 if (nwidth > name_width) {
1783 memcpy(name_scratch, t2, name_width - 3);
1784 name_scratch[name_width - 3] = '.';
1785 name_scratch[name_width - 2] = '.';
1786 name_scratch[name_width - 1] = '>';
1787 name_scratch[name_width] = 0;
1788 t2 = name_scratch;
1789 nwidth = name_width;
1790 }
1791 ap_rvputs(r, "<a href=\"", anchor, "\">",
1792 ap_escape_html(scratch, t2),
1793 "</a>", pad_scratch + nwidth, NULL);
1794 /*
1795 * The blank before the storm.. er, before the next field.
1796 */
1797 ap_rputs(" ", r);
1798 if (!(autoindex_opts & SUPPRESS_LAST_MOD)) {
1799 if (ar[x]->lm != -1) {
1800 char time_str[MAX_STRING_LEN];
1801 apr_time_exp_t ts;
1802 apr_time_exp_lt(&ts, ar[x]->lm);
1803 apr_strftime(time_str, &rv, MAX_STRING_LEN,
1804 "%d-%b-%Y %H:%M ", &ts);
1805 ap_rputs(time_str, r);
1806 }
1807 else {
1808 /*Length="22-Feb-1998 23:42 " (see 4 lines above) */
1809 ap_rputs(" ", r);
1810 }
1811 }
1812 if (!(autoindex_opts & SUPPRESS_SIZE)) {
1813 char buf[5];
1814 ap_rputs(apr_strfsize(ar[x]->size, buf), r);
1815 ap_rputs(" ", r);
1816 }
1817 if (!(autoindex_opts & SUPPRESS_DESC)) {
1818 if (ar[x]->desc) {
1819 ap_rputs(terminate_description(d, ar[x]->desc,
1820 autoindex_opts,
1821 desc_width), r);
1822 }
1823 }
1824 ap_rputc('\n', r);
1825 }
1826 else {
1827 ap_rvputs(r, "<li><a href=\"", anchor, "\"> ",
1828 ap_escape_html(scratch, t2),
1829 "</a></li>\n", NULL);
1830 }
1831 }
1832 if (autoindex_opts & TABLE_INDEXING) {
1833 ap_rvputs(r, breakrow, "</table>\n", NULL);
1834 }
1835 else if (autoindex_opts & FANCY_INDEXING) {
1836 if (!(autoindex_opts & SUPPRESS_RULES)) {
1837 ap_rputs("<hr", r);
1838 if (autoindex_opts & EMIT_XHTML) {
1839 ap_rputs(" /", r);
1840 }
1841 ap_rputs("></pre>\n", r);
1842 }
1843 else {
1844 ap_rputs("</pre>\n", r);
1845 }
1846 }
1847 else {
1848 ap_rputs("</ul>\n", r);
1849 }
1850 }
1851
1852 /*
1853 * Compare two file entries according to the sort criteria. The return
1854 * is essentially a signum function value.
1855 */
1856
1857 static int dsortf(struct ent **e1, struct ent **e2)
1858 {
1859 struct ent *c1;
1860 struct ent *c2;
1861 int result = 0;
1862
1863 /*
1864 * First, see if either of the entries is for the parent directory.
1865 * If so, that *always* sorts lower than anything else.
1866 */
1867 if ((*e1)->name[0] == '/') {
1868 return -1;
1869 }
1870 if ((*e2)->name[0] == '/') {
1871 return 1;
1872 }
1873 /*
1874 * Now see if one's a directory and one isn't, if we're set
1875 * isdir for FOLDERS_FIRST.
1876 */
1877 if ((*e1)->isdir != (*e2)->isdir) {
1878 return (*e1)->isdir ? -1 : 1;
1879 }
1880 /*
1881 * All of our comparisons will be of the c1 entry against the c2 one,
1882 * so assign them appropriately to take care of the ordering.
1883 */
1884 if ((*e1)->ascending) {
1885 c1 = *e1;
1886 c2 = *e2;
1887 }
1888 else {
1889 c1 = *e2;
1890 c2 = *e1;
1891 }
1892
1893 switch (c1->key) {
1894 case K_LAST_MOD:
1895 if (c1->lm > c2->lm) {
1896 return 1;
1897 }
1898 else if (c1->lm < c2->lm) {
1899 return -1;
1900 }
1901 break;
1902 case K_SIZE:
1903 if (c1->size > c2->size) {
1904 return 1;
1905 }
1906 else if (c1->size < c2->size) {
1907 return -1;
1908 }
1909 break;
1910 case K_DESC:
1911 if (c1->version_sort) {
1912 result = apr_strnatcmp(c1->desc ? c1->desc : "",
1913 c2->desc ? c2->desc : "");
1914 }
1915 else {
1916 result = strcmp(c1->desc ? c1->desc : "",
1917 c2->desc ? c2->desc : "");
1918 }
1919 if (result) {
1920 return result;
1921 }
1922 break;
1923 }
1924
1925 /* names may identical when treated case-insensitively,
1926 * so always fall back on strcmp() flavors to put entries
1927 * in deterministic order. This means that 'ABC' and 'abc'
1928 * will always appear in the same order, rather than
1929 * variably between 'ABC abc' and 'abc ABC' order.
1930 */
1931
1932 if (c1->version_sort) {
1933 if (c1->ignore_case) {
1934 result = apr_strnatcasecmp (c1->name, c2->name);
1935 }
1936 if (!result) {
1937 result = apr_strnatcmp(c1->name, c2->name);
1938 }
1939 }
1940
1941 /* The names may be identical in respects other other than
1942 * filename case when strnatcmp is used above, so fall back
1943 * to strcmp on conflicts so that fn1.01.zzz and fn1.1.zzz
1944 * are also sorted in a deterministic order.
1945 */
1946
1947 if (!result && c1->ignore_case) {
1948 result = strcasecmp (c1->name, c2->name);
1949 }
1950
1951 if (!result) {
1952 result = strcmp (c1->name, c2->name);
1953 }
1954
1955 return result;
1956 }
1957
1958
1959 static int index_directory(request_rec *r,
1960 autoindex_config_rec *autoindex_conf)
1961 {
1962 char *title_name = ap_escape_html(r->pool, r->uri);
1963 char *title_endp;
1964 char *name = r->filename;
1965 char *pstring = NULL;
1966 apr_finfo_t dirent;
1967 apr_dir_t *thedir;
1968 apr_status_t status;
1969 int num_ent = 0, x;
1970 struct ent *head, *p;
1971 struct ent **ar = NULL;
1972 const char *qstring;
1973 apr_int32_t autoindex_opts = autoindex_conf->opts;
1974 char keyid;
1975 char direction;
1976 char *colargs;
1977 char *fullpath;
1978 apr_size_t dirpathlen;
1979 char *ctype = "text/html";
1980 char *charset;
1981
1982 if ((status = apr_dir_open(&thedir, name, r->pool)) != APR_SUCCESS) {
1983 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
1984 "Can't open directory for index: %s", r->filename);
1985 return HTTP_FORBIDDEN;
1986 }
1987
1988 if (autoindex_conf->ctype) {
1989 ctype = autoindex_conf->ctype;
1990 }
1991 if (autoindex_conf->charset) {
1992 charset = autoindex_conf->charset;
1993 }
1994 else {
1995 #if APR_HAS_UNICODE_FS
1996 charset = "UTF-8";
1997 #else
1998 charset = "ISO-8859-1";
1999 #endif
2000 }
2001 if (*charset) {
2002 ap_set_content_type(r, apr_pstrcat(r->pool, ctype, ";charset=",
2003 charset, NULL));
2004 }
2005 else {
2006 ap_set_content_type(r, ctype);
2007 }
2008
2009 if (autoindex_opts & TRACK_MODIFIED) {
2010 ap_update_mtime(r, r->finfo.mtime);
2011 ap_set_last_modified(r);
2012 ap_set_etag(r);
2013 }
2014 if (r->header_only) {
2015 apr_dir_close(thedir);
2016 return 0;
2017 }
2018
2019 /*
2020 * If there is no specific ordering defined for this directory,
2021 * default to ascending by filename.
2022 */
2023 keyid = autoindex_conf->default_keyid
2024 ? autoindex_conf->default_keyid : K_NAME;
2025 direction = autoindex_conf->default_direction
2026 ? autoindex_conf->default_direction : D_ASCENDING;
2027
2028 /*
2029 * Figure out what sort of indexing (if any) we're supposed to use.
2030 *
2031 * If no QUERY_STRING was specified or client query strings have been
2032 * explicitly disabled.
2033 * If we are ignoring the client, suppress column sorting as well.
2034 */
2035 if (autoindex_opts & IGNORE_CLIENT) {
2036 qstring = NULL;
2037 autoindex_opts |= SUPPRESS_COLSORT;
2038 colargs = "";
2039 }
2040 else {
2041 char fval[5], vval[5], *ppre = "", *epattern = "";
2042 fval[0] = '\0'; vval[0] = '\0';
2043 qstring = r->args;
2044
2045 while (qstring && *qstring) {
2046
2047 /* C= First Sort key Column (N, M, S, D) */
2048 if ( qstring[0] == 'C' && qstring[1] == '='
2049 && qstring[2] && strchr(K_VALID, qstring[2])
2050 && ( qstring[3] == '&' || qstring[3] == ';'
2051 || !qstring[3])) {
2052 keyid = qstring[2];
2053 qstring += qstring[3] ? 4 : 3;
2054 }
2055
2056 /* O= Sort order (A, D) */
2057 else if ( qstring[0] == 'O' && qstring[1] == '='
2058 && ( (qstring[2] == D_ASCENDING)
2059 || (qstring[2] == D_DESCENDING))
2060 && ( qstring[3] == '&' || qstring[3] == ';'
2061 || !qstring[3])) {
2062 direction = qstring[2];
2063 qstring += qstring[3] ? 4 : 3;
2064 }
2065
2066 /* F= Output Format (0 plain, 1 fancy (pre), 2 table) */
2067 else if ( qstring[0] == 'F' && qstring[1] == '='
2068 && qstring[2] && strchr("012", qstring[2])
2069 && ( qstring[3] == '&' || qstring[3] == ';'
2070 || !qstring[3])) {
2071 if (qstring[2] == '0') {
2072 autoindex_opts &= ~(FANCY_INDEXING | TABLE_INDEXING);
2073 }
2074 else if (qstring[2] == '1') {
2075 autoindex_opts = (autoindex_opts | FANCY_INDEXING)
2076 & ~TABLE_INDEXING;
2077 }
2078 else if (qstring[2] == '2') {
2079 autoindex_opts |= FANCY_INDEXING | TABLE_INDEXING;
2080 }
2081 strcpy(fval, ";F= ");
2082 fval[3] = qstring[2];
2083 qstring += qstring[3] ? 4 : 3;
2084 }
2085
2086 /* V= Version sort (0, 1) */
2087 else if ( qstring[0] == 'V' && qstring[1] == '='
2088 && (qstring[2] == '0' || qstring[2] == '1')
2089 && ( qstring[3] == '&' || qstring[3] == ';'
2090 || !qstring[3])) {
2091 if (qstring[2] == '0') {
2092 autoindex_opts &= ~VERSION_SORT;
2093 }
2094 else if (qstring[2] == '1') {
2095 autoindex_opts |= VERSION_SORT;
2096 }
2097 strcpy(vval, ";V= ");
2098 vval[3] = qstring[2];
2099 qstring += qstring[3] ? 4 : 3;
2100 }
2101
2102 /* P= wildcard pattern (*.foo) */
2103 else if (qstring[0] == 'P' && qstring[1] == '=') {
2104 const char *eos = qstring += 2; /* for efficiency */
2105
2106 while (*eos && *eos != '&' && *eos != ';') {
2107 ++eos;
2108 }
2109
2110 if (eos == qstring) {
2111 pstring = NULL;
2112 }
2113 else {
2114 pstring = apr_pstrndup(r->pool, qstring, eos - qstring);
2115 if (ap_unescape_url(pstring) != OK) {
2116 /* ignore the pattern, if it's bad. */
2117 pstring = NULL;
2118 }
2119 else {
2120 ppre = ";P=";
2121 /* be correct */
2122 epattern = ap_escape_uri(r->pool, pstring);
2123 }
2124 }
2125
2126 if (*eos && *++eos) {
2127 qstring = eos;
2128 }
2129 else {
2130 qstring = NULL;
2131 }
2132 }
2133
2134 /* Syntax error? Ignore the remainder! */
2135 else {
2136 qstring = NULL;
2137 }
2138 }
2139 colargs = apr_pstrcat(r->pool, fval, vval, ppre, epattern, NULL);
2140 }
2141
2142 /* Spew HTML preamble */
2143 title_endp = title_name + strlen(title_name) - 1;
2144
2145 while (title_endp > title_name && *title_endp == '/') {
2146 *title_endp-- = '\0';
2147 }
2148
2149 emit_head(r, autoindex_conf->header,
2150 autoindex_opts & SUPPRESS_PREAMBLE,
2151 autoindex_opts & EMIT_XHTML, title_name);
2152
2153 /*
2154 * Since we don't know how many dir. entries there are, put them into a
2155 * linked list and then arrayificate them so qsort can use them.
2156 */
2157 head = NULL;
2158 p = make_parent_entry(autoindex_opts, autoindex_conf, r, keyid, direction);
2159 if (p != NULL) {
2160 p->next = head;
2161 head = p;
2162 num_ent++;
2163 }
2164 fullpath = apr_palloc(r->pool, APR_PATH_MAX);
2165 dirpathlen = strlen(name);
2166 memcpy(fullpath, name, dirpathlen);
2167
2168 do {
2169 status = apr_dir_read(&dirent, APR_FINFO_MIN | APR_FINFO_NAME, thedir);
2170 if (APR_STATUS_IS_INCOMPLETE(status)) {
2171 continue; /* ignore un-stat()able files */
2172 }
2173 else if (status != APR_SUCCESS) {
2174 break;
2175 }
2176
2177 /* We want to explode symlinks here. */
2178 if (dirent.filetype == APR_LNK) {
2179 const char *savename;
2180 apr_finfo_t fi;
2181 /* We *must* have FNAME. */
2182 savename = dirent.name;
2183 apr_cpystrn(fullpath + dirpathlen, dirent.name,
2184 APR_PATH_MAX - dirpathlen);
2185 status = apr_stat(&fi, fullpath,
2186 dirent.valid & ~(APR_FINFO_NAME), r->pool);
2187 if (status != APR_SUCCESS) {
2188 /* Something bad happened, skip this file. */
2189 continue;
2190 }
2191 memcpy(&dirent, &fi, sizeof(fi));
2192 dirent.name = savename;
2193 dirent.valid |= APR_FINFO_NAME;
2194 }
2195 p = make_autoindex_entry(&dirent, autoindex_opts, autoindex_conf, r,
2196 keyid, direction, pstring);
2197 if (p != NULL) {
2198 p->next = head;
2199 head = p;
2200 num_ent++;
2201 }
2202 } while (1);
2203
2204 if (num_ent > 0) {
2205 ar = (struct ent **) apr_palloc(r->pool,
2206 num_ent * sizeof(struct ent *));
2207 p = head;
2208 x = 0;
2209 while (p) {
2210 ar[x++] = p;
2211 p = p->next;
2212 }
2213
2214 qsort((void *) ar, num_ent, sizeof(struct ent *),
2215 (int (*)(const void *, const void *)) dsortf);
2216 }
2217 output_directories(ar, num_ent, autoindex_conf, r, autoindex_opts,
2218 keyid, direction, colargs);
2219 apr_dir_close(thedir);
2220
2221 emit_tail(r, autoindex_conf->readme,
2222 autoindex_opts & SUPPRESS_PREAMBLE);
2223
2224 return 0;
2225 }
2226
2227 /* The formal handler... */
2228
2229 static int handle_autoindex(request_rec *r)
2230 {
2231 autoindex_config_rec *d;
2232 int allow_opts;
2233
2234 if(strcmp(r->handler,DIR_MAGIC_TYPE)) {
2235 return DECLINED;
2236 }
2237
2238 allow_opts = ap_allow_options(r);
2239
2240 d = (autoindex_config_rec *) ap_get_module_config(r->per_dir_config,
2241 &autoindex_module);
2242
2243 r->allowed |= (AP_METHOD_BIT << M_GET);
2244 if (r->method_number != M_GET) {
2245 return DECLINED;
2246 }
2247
2248 /* OK, nothing easy. Trot out the heavy artillery... */
2249
2250 if (allow_opts & OPT_INDEXES) {
2251 int errstatus;
2252
2253 if ((errstatus = ap_discard_request_body(r)) != OK) {
2254 return errstatus;
2255 }
2256
2257 /* KLUDGE --- make the sub_req lookups happen in the right directory.
2258 * Fixing this in the sub_req_lookup functions themselves is difficult,
2259 * and would probably break virtual includes...
2260 */
2261
2262 if (r->filename[strlen(r->filename) - 1] != '/') {
2263 r->filename = apr_pstrcat(r->pool, r->filename, "/", NULL);
2264 }
2265 return index_directory(r, d);
2266 }
2267 else {
2268 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
2269 "Directory index forbidden by "
2270 "Options directive: %s", r->filename);
2271 return HTTP_FORBIDDEN;
2272 }
2273 }
2274
2275 static void register_hooks(apr_pool_t *p)
2276 {
2277 ap_hook_handler(handle_autoindex,NULL,NULL,APR_HOOK_MIDDLE);
2278 }
2279
2280 module AP_MODULE_DECLARE_DATA autoindex_module =
2281 {
2282 STANDARD20_MODULE_STUFF,
2283 create_autoindex_config, /* dir config creater */
2284 merge_autoindex_configs, /* dir merger --- default is to override */
2285 NULL, /* server config */
2286 NULL, /* merge server config */
2287 autoindex_cmds, /* command apr_table_t */
2288 register_hooks /* register hooks */
2289 };

Properties

Name Value
svn:eol-style native

apache@apache.org
ViewVC Help
Powered by ViewVC 1.1.2