1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
64 * Andrew Wilson <Andrew.Wilson@cm.cf.ac.uk> 25.Jan.96
67 * This version of mod_cern_meta.c controls Meta File behaviour on a
68 * per-directory basis. Previous versions of the module defined behaviour
69 * on a per-server basis. The upshot is that you'll need to revisit your
70 * configuration files in order to make use of the new module.
73 * Emulate the CERN HTTPD Meta file semantics. Meta files are HTTP
74 * headers that can be output in addition to the normal range of
75 * headers for each file accessed. They appear rather like the Apache
76 * .asis files, and are able to provide a crude way of influencing
77 * the Expires: header, as well as providing other curiosities.
78 * There are many ways to manage meta information, this one was
79 * chosen because there is already a large number of CERN users
80 * who can exploit this module. It should be noted that there are probably
81 * more sensitive ways of managing the Expires: header specifically.
83 * The module obeys the following directives, which can appear
84 * in the server's .conf files and in .htaccess files.
88 * turns on|off meta file processing for any directory.
89 * Default value is off
91 * # turn on MetaFiles in this directory
94 * MetaDir <directory name>
96 * specifies the name of the directory in which Apache can find
97 * meta information files. The directory is usually a 'hidden'
98 * subdirectory of the directory that contains the file being
101 * # .meta files are in the *same* directory as the
102 * # file being accessed
105 * the default is to look in a '.web' subdirectory. This is the
106 * same as for CERN 3.+ webservers and behaviour is the same as
111 * MetaSuffix <meta file suffix>
113 * specifies the file name suffix for the file containing the
114 * meta information. eg:
116 * # our meta files are suffixed with '.cern_meta'
117 * MetaSuffix .cern_meta
119 * the default is to look for files with the suffix '.meta'. This
120 * behaviour is the same as for the directive:
124 * When accessing the file
126 * DOCUMENT_ROOT/somedir/index.html
128 * this module will look for the file
130 * DOCUMENT_ROOT/somedir/.web/index.html.meta
132 * and will use its contents to generate additional MIME header
135 * For more information on the CERN Meta file semantics see:
137 * http://www.w3.org/hypertext/WWW/Daemon/User/Config/General.html#MetaDir
140 * 29.Jan.96 pfopen/pfclose instead of fopen/fclose
141 * DECLINE when real file not found, we may be checking each
142 * of the index.html/index.shtml/index.htm variants and don't
143 * need to report missing ones as spurious errors.
144 * 31.Jan.96 log_error reports about a malformed .meta file, rather
145 * than a script error.
146 * 20.Jun.96 MetaFiles <on|off> default off, added, so that module
147 * can be configured per-directory. Prior to this the module
148 * was running for each request anywhere on the server, naughty..
149 * 29.Jun.96 All directives made per-directory.
152 #include "ap_config.h"
154 #include "http_config.h"
155 #include "util_script.h"
156 #include "http_log.h"
157 #include "http_request.h"
158 #include "apr_strings.h"
160 #ifdef HAVE_SYS_TYPES_H
161 #include <sys/types.h>
163 #ifdef HAVE_STRINGS_H
167 #define DIR_CMD_PERMS OR_INDEXES
169 #define DEFAULT_METADIR ".web"
170 #define DEFAULT_METASUFFIX ".meta"
171 #define DEFAULT_METAFILES 0
173 module AP_MODULE_DECLARE_DATA cern_meta_module;
177 const char *metasuffix;
179 } cern_meta_dir_config;
181 static void *create_cern_meta_dir_config(apr_pool_t *p, char *dummy)
183 cern_meta_dir_config *new =
184 (cern_meta_dir_config *) apr_palloc(p, sizeof(cern_meta_dir_config));
187 new->metasuffix = NULL;
188 new->metafiles = DEFAULT_METAFILES;
193 static void *merge_cern_meta_dir_configs(apr_pool_t *p, void *basev, void *addv)
195 cern_meta_dir_config *base = (cern_meta_dir_config *) basev;
196 cern_meta_dir_config *add = (cern_meta_dir_config *) addv;
197 cern_meta_dir_config *new =
198 (cern_meta_dir_config *) apr_palloc(p, sizeof(cern_meta_dir_config));
200 new->metadir = add->metadir ? add->metadir : base->metadir;
201 new->metasuffix = add->metasuffix ? add->metasuffix : base->metasuffix;
202 new->metafiles = add->metafiles;
207 static const char *set_metadir(cmd_parms *parms, void *in_dconf, const char *arg)
209 cern_meta_dir_config *dconf = in_dconf;
211 dconf->metadir = arg;
215 static const char *set_metasuffix(cmd_parms *parms, void *in_dconf, const char *arg)
217 cern_meta_dir_config *dconf = in_dconf;
219 dconf->metasuffix = arg;
223 static const char *set_metafiles(cmd_parms *parms, void *in_dconf, int arg)
225 cern_meta_dir_config *dconf = in_dconf;
227 dconf->metafiles = arg;
232 static const command_rec cern_meta_cmds[] =
234 AP_INIT_FLAG("MetaFiles", set_metafiles, NULL, DIR_CMD_PERMS,
235 "Limited to 'on' or 'off'"),
236 AP_INIT_TAKE1("MetaDir", set_metadir, NULL, DIR_CMD_PERMS,
237 "the name of the directory containing meta files"),
238 AP_INIT_TAKE1("MetaSuffix", set_metasuffix, NULL, DIR_CMD_PERMS,
239 "the filename suffix for meta files"),
243 /* XXX: this is very similar to ap_scan_script_header_err_core...
244 * are the differences deliberate, or just a result of bit rot?
246 static int scan_meta_file(request_rec *r, apr_file_t *f)
248 char w[MAX_STRING_LEN];
251 apr_table_t *tmp_headers;
253 tmp_headers = apr_make_table(r->pool, 5);
254 while (apr_fgets(w, MAX_STRING_LEN - 1, f) == APR_SUCCESS) {
256 /* Delete terminal (CR?)LF */
259 if (p > 0 && w[p - 1] == '\n') {
260 if (p > 1 && w[p - 2] == '\015')
270 /* if we see a bogus header don't ignore it. Shout and scream */
272 if (!(l = strchr(w, ':'))) {
273 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
274 "malformed header in meta file: %s", r->filename);
275 return HTTP_INTERNAL_SERVER_ERROR;
279 while (*l && apr_isspace(*l))
282 if (!strcasecmp(w, "Content-type")) {
284 /* Nuke trailing whitespace */
286 char *endp = l + strlen(l) - 1;
287 while (endp > l && apr_isspace(*endp))
290 tmp = apr_pstrdup(r->pool, l);
291 ap_content_type_tolower(tmp);
292 r->content_type = tmp;
294 else if (!strcasecmp(w, "Status")) {
295 sscanf(l, "%d", &r->status);
296 r->status_line = apr_pstrdup(r->pool, l);
299 apr_table_set(tmp_headers, w, l);
302 apr_overlap_tables(r->headers_out, tmp_headers, APR_OVERLAP_TABLES_SET);
306 static int add_cern_meta_data(request_rec *r)
312 apr_file_t *f = NULL;
313 apr_status_t retcode;
314 cern_meta_dir_config *dconf;
318 dconf = ap_get_module_config(r->per_dir_config, &cern_meta_module);
320 if (!dconf->metafiles) {
324 /* if ./.web/$1.meta exists then output 'asis' */
326 if (r->finfo.protection == 0) {
330 /* is this a directory? */
331 if (r->finfo.filetype == APR_DIR || r->uri[strlen(r->uri) - 1] == '/') {
335 /* what directory is this file in? */
336 scrap_book = apr_pstrdup(r->pool, r->filename);
337 /* skip leading slash, recovered in later processing */
339 last_slash = strrchr(scrap_book, '/');
340 if (last_slash != NULL) {
341 /* skip over last slash */
342 real_file = last_slash;
347 /* no last slash, buh?! */
348 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
349 "internal error in mod_cern_meta: %s", r->filename);
350 /* should really barf, but hey, let's be friends... */
354 metafilename = apr_pstrcat(r->pool, "/", scrap_book, "/",
355 dconf->metadir ? dconf->metadir : DEFAULT_METADIR,
357 dconf->metasuffix ? dconf->metasuffix : DEFAULT_METASUFFIX,
360 /* XXX: it sucks to require this subrequest to complete, because this
361 * means people must leave their meta files accessible to the world.
362 * A better solution might be a "safe open" feature of pfopen to avoid
363 * pipes, symlinks, and crap like that.
365 rr = ap_sub_req_lookup_file(metafilename, r, NULL);
366 if (rr->status != HTTP_OK) {
367 ap_destroy_sub_req(rr);
370 ap_destroy_sub_req(rr);
372 retcode = apr_open(&f, metafilename, APR_READ | APR_CREATE, APR_OS_DEFAULT, r->pool);
373 if (retcode != APR_SUCCESS) {
374 if (APR_STATUS_IS_ENOENT(retcode)) {
377 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
378 "meta file permissions deny server access: %s", metafilename);
379 return HTTP_FORBIDDEN;
382 /* read the headers in */
383 rv = scan_meta_file(r, f);
388 static void register_hooks(void)
390 ap_hook_fixups(add_cern_meta_data,NULL,NULL,AP_HOOK_MIDDLE);
392 module AP_MODULE_DECLARE_DATA cern_meta_module =
394 STANDARD20_MODULE_STUFF,
395 create_cern_meta_dir_config,/* dir config creater */
396 merge_cern_meta_dir_configs,/* dir merger --- default is to override */
397 NULL, /* server config */
398 NULL, /* merge server configs */
399 cern_meta_cmds, /* command apr_table_t */
400 register_hooks /* register hooks */