📄 mod_autoindex.c
字号:
/* Copyright 1999-2005 The Apache Software Foundation or its licensors, as * applicable. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * mod_autoindex.c: Handles the on-the-fly html index generation * * Rob McCool * 3/23/93 * * Adapted to Apache by rst. * * Version sort added by Martin Pool <mbp@humbug.org.au>. */#include "apr_strings.h"#include "apr_fnmatch.h"#include "apr_strings.h"#include "apr_lib.h"#define APR_WANT_STRFUNC#include "apr_want.h"#include "ap_config.h"#include "httpd.h"#include "http_config.h"#include "http_core.h"#include "http_request.h"#include "http_protocol.h"#include "http_log.h"#include "http_main.h"#include "util_script.h"#include "mod_core.h"module AP_MODULE_DECLARE_DATA autoindex_module;/**************************************************************** * * Handling configuration directives... */#define NO_OPTIONS (1 << 0) /* Indexing options */#define ICONS_ARE_LINKS (1 << 1)#define SCAN_HTML_TITLES (1 << 2)#define SUPPRESS_ICON (1 << 3)#define SUPPRESS_LAST_MOD (1 << 4)#define SUPPRESS_SIZE (1 << 5)#define SUPPRESS_DESC (1 << 6)#define SUPPRESS_PREAMBLE (1 << 7)#define SUPPRESS_COLSORT (1 << 8)#define SUPPRESS_RULES (1 << 9)#define FOLDERS_FIRST (1 << 10)#define VERSION_SORT (1 << 11)#define TRACK_MODIFIED (1 << 12)#define FANCY_INDEXING (1 << 13)#define TABLE_INDEXING (1 << 14)#define IGNORE_CLIENT (1 << 15)#define IGNORE_CASE (1 << 16)#define EMIT_XHTML (1 << 17)#define K_NOADJUST 0#define K_ADJUST 1#define K_UNSET 2/* * Define keys for sorting. */#define K_NAME 'N' /* Sort by file name (default) */#define K_LAST_MOD 'M' /* Last modification date */#define K_SIZE 'S' /* Size (absolute, not as displayed) */#define K_DESC 'D' /* Description */#define K_VALID "NMSD" /* String containing _all_ valid K_ opts */#define D_ASCENDING 'A'#define D_DESCENDING 'D'#define D_VALID "AD" /* String containing _all_ valid D_ opts *//* * These are the dimensions of the default icons supplied with Apache. */#define DEFAULT_ICON_WIDTH 20#define DEFAULT_ICON_HEIGHT 22/* * Other default dimensions. */#define DEFAULT_NAME_WIDTH 23#define DEFAULT_DESC_WIDTH 23struct item { char *type; char *apply_to; char *apply_path; char *data;};typedef struct ai_desc_t { char *pattern; char *description; int full_path; int wildcards;} ai_desc_t;typedef struct autoindex_config_struct { char *default_icon; apr_int32_t opts; apr_int32_t incremented_opts; apr_int32_t decremented_opts; int name_width; int name_adjust; int desc_width; int desc_adjust; int icon_width; int icon_height; char default_keyid; char default_direction; apr_array_header_t *icon_list; apr_array_header_t *alt_list; apr_array_header_t *desc_list; apr_array_header_t *ign_list; apr_array_header_t *hdr_list; apr_array_header_t *rdme_list;} autoindex_config_rec;static char c_by_encoding, c_by_type, c_by_path;#define BY_ENCODING &c_by_encoding#define BY_TYPE &c_by_type#define BY_PATH &c_by_path/* * This routine puts the standard HTML header at the top of the index page. * We include the DOCTYPE because we may be using features therefrom (i.e., * HEIGHT and WIDTH attributes on the icons if we're FancyIndexing). */static void emit_preamble(request_rec *r, int xhtml, const char *title){ ap_rvputs(r, xhtml ? DOCTYPE_XHTML_1_0T : DOCTYPE_HTML_3_2, "<html>\n <head>\n <title>Index of ", title, "</title>\n </head>\n <body>\n", NULL);}static void push_item(apr_array_header_t *arr, char *type, const char *to, const char *path, const char *data){ struct item *p = (struct item *) apr_array_push(arr); if (!to) { to = ""; } if (!path) { path = ""; } p->type = type; p->data = data ? apr_pstrdup(arr->pool, data) : NULL; p->apply_path = apr_pstrcat(arr->pool, path, "*", NULL); if ((type == BY_PATH) && (!ap_is_matchexp(to))) { p->apply_to = apr_pstrcat(arr->pool, "*", to, NULL); } else if (to) { p->apply_to = apr_pstrdup(arr->pool, to); } else { p->apply_to = NULL; }}static const char *add_alt(cmd_parms *cmd, void *d, const char *alt, const char *to){ if (cmd->info == BY_PATH) { if (!strcmp(to, "**DIRECTORY**")) { to = "^^DIRECTORY^^"; } } if (cmd->info == BY_ENCODING) { char *tmp = apr_pstrdup(cmd->pool, to); ap_str_tolower(tmp); to = tmp; } push_item(((autoindex_config_rec *) d)->alt_list, cmd->info, to, cmd->path, alt); return NULL;}static const char *add_icon(cmd_parms *cmd, void *d, const char *icon, const char *to){ char *iconbak = apr_pstrdup(cmd->pool, icon); if (icon[0] == '(') { char *alt; char *cl = strchr(iconbak, ')'); if (cl == NULL) { return "missing closing paren"; } alt = ap_getword_nc(cmd->pool, &iconbak, ','); *cl = '\0'; /* Lose closing paren */ add_alt(cmd, d, &alt[1], to); } if (cmd->info == BY_PATH) { if (!strcmp(to, "**DIRECTORY**")) { to = "^^DIRECTORY^^"; } } if (cmd->info == BY_ENCODING) { char *tmp = apr_pstrdup(cmd->pool, to); ap_str_tolower(tmp); to = tmp; } push_item(((autoindex_config_rec *) d)->icon_list, cmd->info, to, cmd->path, iconbak); return NULL;}/* * Add description text for a filename pattern. If the pattern has * wildcards already (or we need to add them), add leading and * trailing wildcards to it to ensure substring processing. If the * pattern contains a '/' anywhere, force wildcard matching mode, * add a slash to the prefix so that "bar/bletch" won't be matched * by "foobar/bletch", and make a note that there's a delimiter; * the matching routine simplifies to just the actual filename * whenever it can. This allows definitions in parent directories * to be made for files in subordinate ones using relative paths. *//* * Absent a strcasestr() function, we have to force wildcards on * systems for which "AAA" and "aaa" mean the same file. */#ifdef CASE_BLIND_FILESYSTEM#define WILDCARDS_REQUIRED 1#else#define WILDCARDS_REQUIRED 0#endifstatic const char *add_desc(cmd_parms *cmd, void *d, const char *desc, const char *to){ autoindex_config_rec *dcfg = (autoindex_config_rec *) d; ai_desc_t *desc_entry; char *prefix = ""; desc_entry = (ai_desc_t *) apr_array_push(dcfg->desc_list); desc_entry->full_path = (ap_strchr_c(to, '/') == NULL) ? 0 : 1; desc_entry->wildcards = (WILDCARDS_REQUIRED || desc_entry->full_path || apr_fnmatch_test(to)); if (desc_entry->wildcards) { prefix = desc_entry->full_path ? "*/" : "*"; desc_entry->pattern = apr_pstrcat(dcfg->desc_list->pool, prefix, to, "*", NULL); } else { desc_entry->pattern = apr_pstrdup(dcfg->desc_list->pool, to); } desc_entry->description = apr_pstrdup(dcfg->desc_list->pool, desc); return NULL;}static const char *add_ignore(cmd_parms *cmd, void *d, const char *ext){ push_item(((autoindex_config_rec *) d)->ign_list, 0, ext, cmd->path, NULL); return NULL;}static const char *add_header(cmd_parms *cmd, void *d, const char *name){ push_item(((autoindex_config_rec *) d)->hdr_list, 0, NULL, cmd->path, name); return NULL;}static const char *add_readme(cmd_parms *cmd, void *d, const char *name){ push_item(((autoindex_config_rec *) d)->rdme_list, 0, NULL, cmd->path, name); return NULL;}static const char *add_opts(cmd_parms *cmd, void *d, const char *optstr){ char *w; apr_int32_t opts; apr_int32_t opts_add; apr_int32_t opts_remove; char action; autoindex_config_rec *d_cfg = (autoindex_config_rec *) d; opts = d_cfg->opts; opts_add = d_cfg->incremented_opts; opts_remove = d_cfg->decremented_opts; while (optstr[0]) { int option = 0; w = ap_getword_conf(cmd->pool, &optstr); if ((*w == '+') || (*w == '-')) { action = *(w++); } else { action = '\0'; } if (!strcasecmp(w, "FancyIndexing")) { option = FANCY_INDEXING; } else if (!strcasecmp(w, "FoldersFirst")) { option = FOLDERS_FIRST; } else if (!strcasecmp(w, "HTMLTable")) { option = TABLE_INDEXING; } else if (!strcasecmp(w, "IconsAreLinks")) { option = ICONS_ARE_LINKS; } else if (!strcasecmp(w, "IgnoreCase")) { option = IGNORE_CASE; } else if (!strcasecmp(w, "IgnoreClient")) { option = IGNORE_CLIENT; } else if (!strcasecmp(w, "ScanHTMLTitles")) { option = SCAN_HTML_TITLES; } else if (!strcasecmp(w, "SuppressColumnSorting")) { option = SUPPRESS_COLSORT; } else if (!strcasecmp(w, "SuppressDescription")) { option = SUPPRESS_DESC; } else if (!strcasecmp(w, "SuppressHTMLPreamble")) { option = SUPPRESS_PREAMBLE; } else if (!strcasecmp(w, "SuppressIcon")) { option = SUPPRESS_ICON; } else if (!strcasecmp(w, "SuppressLastModified")) { option = SUPPRESS_LAST_MOD; } else if (!strcasecmp(w, "SuppressSize")) { option = SUPPRESS_SIZE; } else if (!strcasecmp(w, "SuppressRules")) { option = SUPPRESS_RULES; } else if (!strcasecmp(w, "TrackModified")) { option = TRACK_MODIFIED; } else if (!strcasecmp(w, "VersionSort")) { option = VERSION_SORT; } else if (!strcasecmp(w, "XHTML")) { option = EMIT_XHTML; } else if (!strcasecmp(w, "None")) { if (action != '\0') { return "Cannot combine '+' or '-' with 'None' keyword"; } opts = NO_OPTIONS; opts_add = 0; opts_remove = 0; } else if (!strcasecmp(w, "IconWidth")) { if (action != '-') { d_cfg->icon_width = DEFAULT_ICON_WIDTH; } else { d_cfg->icon_width = 0; } } else if (!strncasecmp(w, "IconWidth=", 10)) { if (action == '-') { return "Cannot combine '-' with IconWidth=n"; } d_cfg->icon_width = atoi(&w[10]); } else if (!strcasecmp(w, "IconHeight")) { if (action != '-') {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -