📄 mod_autoindex.c
字号:
/* ==================================================================== * Copyright (c) 1995-1998 The Apache Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the Apache Group * for use in the Apache HTTP server project (http://www.apache.org/)." * * 4. The names "Apache Server" and "Apache Group" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the Apache Group * for use in the Apache HTTP server project (http://www.apache.org/)." * * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Group and was originally based * on public domain software written at the National Center for * Supercomputing Applications, University of Illinois, Urbana-Champaign. * For more information on the Apache Group and the Apache HTTP server * project, please see <http://www.apache.org/>. * *//* * mod_autoindex.c: Handles the on-the-fly html index generation * * Rob McCool * 3/23/93 * * Adapted to Apache by rst. */#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"module MODULE_VAR_EXPORT autoindex_module;/**************************************************************** * * Handling configuration directives... */#define HRULE 1#define NO_HRULE 0#define FRONT_MATTER 1#define END_MATTER 0#define FANCY_INDEXING 1 /* Indexing options */#define ICONS_ARE_LINKS 2#define SCAN_HTML_TITLES 4#define SUPPRESS_LAST_MOD 8#define SUPPRESS_SIZE 16#define SUPPRESS_DESC 32#define SUPPRESS_PREAMBLE 64#define SUPPRESS_COLSORT 128#define NO_OPTIONS 256#define K_PAD 1#define K_NOPAD 0/* * 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 D_ASCENDING 'A'#define D_DESCENDING 'D'/* * 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 23struct item { char *type; char *apply_to; char *apply_path; char *data;};typedef struct autoindex_config_struct { char *default_icon; int opts; int incremented_opts; int decremented_opts; int name_width; int name_adjust; int icon_width; int icon_height; array_header *icon_list, *alt_list, *desc_list, *ign_list; array_header *hdr_list, *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/* * Return true if the specified string refers to the parent directory (i.e., * matches ".." or "../"). Hopefully this one call is significantly less * expensive than multiple strcmp() calls. */static ap_inline int is_parent(const char *name){ /* * Now, IFF the first two bytes are dots, and the third byte is either * EOS (\0) or a slash followed by EOS, we have a match. */ if (((name[0] == '.') && (name[1] == '.')) && ((name[2] == '\0') || ((name[2] == '/') && (name[3] == '\0')))) { return 1; } return 0;}/* * 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, char *title){ ap_rvputs(r, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n", "<HTML>\n <HEAD>\n <TITLE>Index of ", title, "</TITLE>\n </HEAD>\n <BODY>\n", NULL);}static void push_item(array_header *arr, char *type, char *to, char *path, char *data){ struct item *p = (struct item *) ap_push_array(arr); if (!to) { to = ""; } if (!path) { path = ""; } p->type = type; p->data = data ? ap_pstrdup(arr->pool, data) : NULL; p->apply_path = ap_pstrcat(arr->pool, path, "*", NULL); if ((type == BY_PATH) && (!ap_is_matchexp(to))) { p->apply_to = ap_pstrcat(arr->pool, "*", to, NULL); } else if (to) { p->apply_to = ap_pstrdup(arr->pool, to); } else { p->apply_to = NULL; }}static const char *add_alt(cmd_parms *cmd, void *d, char *alt, char *to){ if (cmd->info == BY_PATH) { if (!strcmp(to, "**DIRECTORY**")) { to = "^^DIRECTORY^^"; } } if (cmd->info == BY_ENCODING) { ap_str_tolower(to); } 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, char *icon, char *to){ char *iconbak = ap_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) { ap_str_tolower(to); } push_item(((autoindex_config_rec *) d)->icon_list, cmd->info, to, cmd->path, iconbak); return NULL;}static const char *add_desc(cmd_parms *cmd, void *d, char *desc, char *to){ push_item(((autoindex_config_rec *) d)->desc_list, cmd->info, to, cmd->path, desc); return NULL;}static const char *add_ignore(cmd_parms *cmd, void *d, 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, char *name){ if (strchr(name, '/')) { return "HeaderName cannot contain a /"; } 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, char *name){ if (strchr(name, '/')) { return "ReadmeName cannot contain a /"; } push_item(((autoindex_config_rec *) d)->rdme_list, 0, NULL, cmd->path, name); return NULL;}/* A legacy directive, FancyIndexing is superseded by the IndexOptions * keyword. But for compatibility.. */static const char *fancy_indexing(cmd_parms *cmd, void *d, int arg){ int curopts; int newopts; autoindex_config_rec *cfg; cfg = (autoindex_config_rec *) d; curopts = cfg->opts; if (curopts & NO_OPTIONS) { return "FancyIndexing directive conflicts with existing " "IndexOptions None"; } newopts = (arg ? (curopts | FANCY_INDEXING) : (curopts & ~FANCY_INDEXING)); cfg->opts = newopts; return NULL;}static const char *add_opts(cmd_parms *cmd, void *d, const char *optstr){ char *w; int opts; int opts_add; int 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, "IconsAreLinks")) { option = ICONS_ARE_LINKS; } else if (!strcasecmp(w, "ScanHTMLTitles")) { option = SCAN_HTML_TITLES; } else if (!strcasecmp(w, "SuppressLastModified")) { option = SUPPRESS_LAST_MOD; } else if (!strcasecmp(w, "SuppressSize")) { option = SUPPRESS_SIZE; } else if (!strcasecmp(w, "SuppressDescription")) { option = SUPPRESS_DESC; } else if (!strcasecmp(w, "SuppressHTMLPreamble")) { option = SUPPRESS_PREAMBLE; } else if (!strcasecmp(w, "SuppressColumnSorting")) { option = SUPPRESS_COLSORT; } 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 != '\0') { return "Cannot combine '+' or '-' with IconWidth=n"; } d_cfg->icon_width = atoi(&w[10]); } else if (!strcasecmp(w, "IconHeight")) { if (action != '-') { d_cfg->icon_height = DEFAULT_ICON_HEIGHT; } else { d_cfg->icon_height = 0; } } else if (!strncasecmp(w, "IconHeight=", 11)) { if (action != '\0') { return "Cannot combine '+' or '-' with IconHeight=n"; } d_cfg->icon_height = atoi(&w[11]); } else if (!strcasecmp(w, "NameWidth")) { if (action != '-') { return "NameWidth with no value may only appear as " "'-NameWidth'"; } d_cfg->name_width = DEFAULT_NAME_WIDTH; d_cfg->name_adjust = 0; } else if (!strncasecmp(w, "NameWidth=", 10)) { if (action != '\0') { return "Cannot combine '+' or '-' with NameWidth=n"; } if (w[10] == '*') { d_cfg->name_adjust = 1; } else { int width = atoi(&w[10]); if (width < 1) { return "NameWidth value must be greater than 1"; } d_cfg->name_width = width; } } else { return "Invalid directory indexing option"; } if (action == '\0') { opts |= option; opts_add = 0; opts_remove = 0; } else if (action == '+') { opts_add |= option; opts_remove &= ~option; } else { opts_remove |= option; opts_add &= ~option; } } if ((opts & NO_OPTIONS) && (opts & ~NO_OPTIONS)) { return "Cannot combine other IndexOptions keywords with 'None'"; } d_cfg->incremented_opts = opts_add; d_cfg->decremented_opts = opts_remove; d_cfg->opts = opts; return NULL;}#define DIR_CMD_PERMS OR_INDEXESstatic const command_rec autoindex_cmds[] ={ {"AddIcon", add_icon, BY_PATH, DIR_CMD_PERMS, ITERATE2, "an icon URL followed by one or more filenames"}, {"AddIconByType", add_icon, BY_TYPE, DIR_CMD_PERMS, ITERATE2, "an icon URL followed by one or more MIME types"}, {"AddIconByEncoding", add_icon, BY_ENCODING, DIR_CMD_PERMS, ITERATE2, "an icon URL followed by one or more content encodings"}, {"AddAlt", add_alt, BY_PATH, DIR_CMD_PERMS, ITERATE2, "alternate descriptive text followed by one or more filenames"}, {"AddAltByType", add_alt, BY_TYPE, DIR_CMD_PERMS, ITERATE2, "alternate descriptive text followed by one or more MIME types"}, {"AddAltByEncoding", add_alt, BY_ENCODING, DIR_CMD_PERMS, ITERATE2, "alternate descriptive text followed by one or more content encodings"}, {"IndexOptions", add_opts, NULL, DIR_CMD_PERMS, RAW_ARGS, "one or more index options"}, {"IndexIgnore", add_ignore, NULL, DIR_CMD_PERMS, ITERATE, "one or more file extensions"}, {"AddDescription", add_desc, BY_PATH, DIR_CMD_PERMS, ITERATE2, "Descriptive text followed by one or more filenames"}, {"HeaderName", add_header, NULL, DIR_CMD_PERMS, TAKE1, "a filename"}, {"ReadmeName", add_readme, NULL, DIR_CMD_PERMS, TAKE1, "a filename"}, {"FancyIndexing", fancy_indexing, NULL, DIR_CMD_PERMS, FLAG, "Limited to 'on' or 'off' (superseded by IndexOptions FancyIndexing)"}, {"DefaultIcon", ap_set_string_slot, (void *) XtOffsetOf(autoindex_config_rec, default_icon), DIR_CMD_PERMS, TAKE1, "an icon URL"}, {NULL}};static void *create_autoindex_config(pool *p, char *dummy){ autoindex_config_rec *new = (autoindex_config_rec *) ap_pcalloc(p, sizeof(autoindex_config_rec)); new->icon_width = 0; new->icon_height = 0; new->name_width = DEFAULT_NAME_WIDTH; new->name_adjust = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -