1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 * Andrew Wilson <Andrew.Wilson@cm.cf.ac.uk> 25.Jan.96
25 * This version of mod_cern_meta.c controls Meta File behaviour on a
26 * per-directory basis. Previous versions of the module defined behaviour
27 * on a per-server basis. The upshot is that you'll need to revisit your
28 * configuration files in order to make use of the new module.
31 * Emulate the CERN HTTPD Meta file semantics. Meta files are HTTP
32 * headers that can be output in addition to the normal range of
33 * headers for each file accessed. They appear rather like the Apache
34 * .asis files, and are able to provide a crude way of influencing
35 * the Expires: header, as well as providing other curiosities.
36 * There are many ways to manage meta information, this one was
37 * chosen because there is already a large number of CERN users
38 * who can exploit this module. It should be noted that there are probably
39 * more sensitive ways of managing the Expires: header specifically.
41 * The module obeys the following directives, which can appear
42 * in the server's .conf files and in .htaccess files.
46 * turns on|off meta file processing for any directory.
47 * Default value is off
49 * # turn on MetaFiles in this directory
52 * MetaDir <directory name>
54 * specifies the name of the directory in which Apache can find
55 * meta information files. The directory is usually a 'hidden'
56 * subdirectory of the directory that contains the file being
59 * # .meta files are in the *same* directory as the
60 * # file being accessed
63 * the default is to look in a '.web' subdirectory. This is the
64 * same as for CERN 3.+ webservers and behaviour is the same as
69 * MetaSuffix <meta file suffix>
71 * specifies the file name suffix for the file containing the
72 * meta information. eg:
74 * # our meta files are suffixed with '.cern_meta'
75 * MetaSuffix .cern_meta
77 * the default is to look for files with the suffix '.meta'. This
78 * behaviour is the same as for the directive:
82 * When accessing the file
84 * DOCUMENT_ROOT/somedir/index.html
86 * this module will look for the file
88 * DOCUMENT_ROOT/somedir/.web/index.html.meta
90 * and will use its contents to generate additional MIME header
93 * For more information on the CERN Meta file semantics see:
95 * http://www.w3.org/hypertext/WWW/Daemon/User/Config/General.html#MetaDir
98 * 29.Jan.96 pfopen/pfclose instead of fopen/fclose
99 * DECLINE when real file not found, we may be checking each
100 * of the index.html/index.shtml/index.htm variants and don't
101 * need to report missing ones as spurious errors.
102 * 31.Jan.96 log_error reports about a malformed .meta file, rather
103 * than a script error.
104 * 20.Jun.96 MetaFiles <on|off> default off, added, so that module
105 * can be configured per-directory. Prior to this the module
106 * was running for each request anywhere on the server, naughty..
107 * 29.Jun.96 All directives made per-directory.
111 #include "apr_strings.h"
113 #define APR_WANT_STRFUNC
114 #include "apr_want.h"
116 #if APR_HAVE_SYS_TYPES_H
117 #include <sys/types.h>
120 #include "ap_config.h"
122 #include "http_config.h"
123 #include "util_script.h"
124 #include "http_log.h"
125 #include "http_request.h"
126 #include "http_protocol.h"
129 #define DIR_CMD_PERMS OR_INDEXES
131 #define DEFAULT_METADIR ".web"
132 #define DEFAULT_METASUFFIX ".meta"
133 #define DEFAULT_METAFILES 0
135 module AP_MODULE_DECLARE_DATA cern_meta_module;
139 const char *metasuffix;
141 } cern_meta_dir_config;
143 static void *create_cern_meta_dir_config(apr_pool_t *p, char *dummy)
145 cern_meta_dir_config *new =
146 (cern_meta_dir_config *) apr_palloc(p, sizeof(cern_meta_dir_config));
149 new->metasuffix = NULL;
150 new->metafiles = DEFAULT_METAFILES;
155 static void *merge_cern_meta_dir_configs(apr_pool_t *p, void *basev, void *addv)
157 cern_meta_dir_config *base = (cern_meta_dir_config *) basev;
158 cern_meta_dir_config *add = (cern_meta_dir_config *) addv;
159 cern_meta_dir_config *new =
160 (cern_meta_dir_config *) apr_palloc(p, sizeof(cern_meta_dir_config));
162 new->metadir = add->metadir ? add->metadir : base->metadir;
163 new->metasuffix = add->metasuffix ? add->metasuffix : base->metasuffix;
164 new->metafiles = add->metafiles;
169 static const char *set_metadir(cmd_parms *parms, void *in_dconf, const char *arg)
171 cern_meta_dir_config *dconf = in_dconf;
173 dconf->metadir = arg;
177 static const char *set_metasuffix(cmd_parms *parms, void *in_dconf, const char *arg)
179 cern_meta_dir_config *dconf = in_dconf;
181 dconf->metasuffix = arg;
185 static const char *set_metafiles(cmd_parms *parms, void *in_dconf, int arg)
187 cern_meta_dir_config *dconf = in_dconf;
189 dconf->metafiles = arg;
194 static const command_rec cern_meta_cmds[] =
196 AP_INIT_FLAG("MetaFiles", set_metafiles, NULL, DIR_CMD_PERMS,
197 "Limited to 'on' or 'off'"),
198 AP_INIT_TAKE1("MetaDir", set_metadir, NULL, DIR_CMD_PERMS,
199 "the name of the directory containing meta files"),
200 AP_INIT_TAKE1("MetaSuffix", set_metasuffix, NULL, DIR_CMD_PERMS,
201 "the filename suffix for meta files"),
205 /* XXX: this is very similar to ap_scan_script_header_err_core...
206 * are the differences deliberate, or just a result of bit rot?
208 static int scan_meta_file(request_rec *r, apr_file_t *f)
210 char w[MAX_STRING_LEN];
213 apr_table_t *tmp_headers;
215 tmp_headers = apr_table_make(r->pool, 5);
216 while (apr_file_gets(w, MAX_STRING_LEN - 1, f) == APR_SUCCESS) {
218 /* Delete terminal (CR?)LF */
220 if (p > 0 && w[p - 1] == '\n') {
221 if (p > 1 && w[p - 2] == '\015')
231 /* if we see a bogus header don't ignore it. Shout and scream */
233 if (!(l = strchr(w, ':'))) {
234 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01560)
235 "malformed header in meta file: %s", r->filename);
236 return HTTP_INTERNAL_SERVER_ERROR;
240 while (apr_isspace(*l))
243 if (!strcasecmp(w, "Content-type")) {
245 /* Nuke trailing whitespace */
247 char *endp = l + strlen(l) - 1;
248 while (endp > l && apr_isspace(*endp))
251 tmp = apr_pstrdup(r->pool, l);
252 ap_content_type_tolower(tmp);
253 ap_set_content_type(r, tmp);
255 else if (!strcasecmp(w, "Status")) {
256 sscanf(l, "%d", &r->status);
257 r->status_line = apr_pstrdup(r->pool, l);
260 apr_table_set(tmp_headers, w, l);
263 apr_table_overlap(r->headers_out, tmp_headers, APR_OVERLAP_TABLES_SET);
267 static int add_cern_meta_data(request_rec *r)
274 apr_file_t *f = NULL;
275 apr_status_t retcode;
276 cern_meta_dir_config *dconf;
280 dconf = ap_get_module_config(r->per_dir_config, &cern_meta_module);
282 if (!dconf->metafiles) {
286 /* if ./.web/$1.meta exists then output 'asis' */
288 if (r->finfo.filetype == APR_NOFILE) {
292 /* is this a directory? */
293 if (r->finfo.filetype == APR_DIR || r->uri[strlen(r->uri) - 1] == '/') {
297 /* what directory is this file in? */
298 scrap_book = apr_pstrdup(r->pool, r->filename);
300 leading_slash = strchr(scrap_book, '/');
301 last_slash = strrchr(scrap_book, '/');
302 if ((last_slash != NULL) && (last_slash != leading_slash)) {
303 /* skip over last slash */
304 real_file = last_slash;
309 /* no last slash, buh?! */
310 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01561)
311 "internal error in mod_cern_meta: %s", r->filename);
312 /* should really barf, but hey, let's be friends... */
316 metafilename = apr_pstrcat(r->pool, scrap_book, "/",
317 dconf->metadir ? dconf->metadir : DEFAULT_METADIR,
319 dconf->metasuffix ? dconf->metasuffix : DEFAULT_METASUFFIX,
322 /* It sucks to require this subrequest to complete, because this
323 * means people must leave their meta files accessible to the world.
324 * A better solution might be a "safe open" feature of pfopen to avoid
325 * pipes, symlinks, and crap like that.
327 * In fact, this doesn't suck. Because <Location > blocks are never run
328 * against sub_req_lookup_file, the meta can be somewhat protected by
329 * either masking it with a <Location > directive or alias, or stowing
330 * the file outside of the web document tree, while providing the
331 * appropriate directory blocks to allow access to it as a file.
333 rr = ap_sub_req_lookup_file(metafilename, r, NULL);
334 if (rr->status != HTTP_OK) {
335 ap_destroy_sub_req(rr);
338 ap_destroy_sub_req(rr);
340 retcode = apr_file_open(&f, metafilename, APR_READ, APR_OS_DEFAULT, r->pool);
341 if (retcode != APR_SUCCESS) {
342 if (APR_STATUS_IS_ENOENT(retcode)) {
345 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01562)
346 "meta file permissions deny server access: %s", metafilename);
347 return HTTP_FORBIDDEN;
350 /* read the headers in */
351 rv = scan_meta_file(r, f);
357 static void register_hooks(apr_pool_t *p)
359 ap_hook_fixups(add_cern_meta_data,NULL,NULL,APR_HOOK_MIDDLE);
362 AP_DECLARE_MODULE(cern_meta) =
364 STANDARD20_MODULE_STUFF,
365 create_cern_meta_dir_config, /* dir config creater */
366 merge_cern_meta_dir_configs, /* dir merger --- default is to override */
367 NULL, /* server config */
368 NULL, /* merge server configs */
369 cern_meta_cmds, /* command apr_table_t */
370 register_hooks /* register hooks */