bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / modules / metadata / mod_cern_meta.c
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_cern_meta.c
19  * version 0.1.0
20  * status beta
21  * 
22  * Andrew Wilson <Andrew.Wilson@cm.cf.ac.uk> 25.Jan.96
23  *
24  * *** IMPORTANT ***
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.
29  * ***
30  *
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.
40  *
41  * The module obeys the following directives, which can appear 
42  * in the server's .conf files and in .htaccess files.
43  *
44  *  MetaFiles <on|off> 
45  *
46  *    turns on|off meta file processing for any directory.  
47  *    Default value is off
48  *
49  *        # turn on MetaFiles in this directory
50  *        MetaFiles on
51  *
52  *  MetaDir <directory name>
53  *      
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
57  *    accessed.  eg:
58  *
59  *        # .meta files are in the *same* directory as the 
60  *        # file being accessed
61  *        MetaDir .
62  *
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 
65  *    for the directive:
66  *
67  *        MetaDir .web
68  *
69  *  MetaSuffix <meta file suffix>
70  *
71  *    specifies the file name suffix for the file containing the
72  *    meta information.  eg:
73  *
74  *       # our meta files are suffixed with '.cern_meta'
75  *       MetaSuffix .cern_meta
76  *
77  *    the default is to look for files with the suffix '.meta'.  This
78  *    behaviour is the same as for the directive:
79  *
80  *       MetaSuffix .meta
81  *
82  * When accessing the file
83  *
84  *   DOCUMENT_ROOT/somedir/index.html
85  *
86  * this module will look for the file
87  *
88  *   DOCUMENT_ROOT/somedir/.web/index.html.meta
89  *
90  * and will use its contents to generate additional MIME header 
91  * information.
92  *
93  * For more information on the CERN Meta file semantics see:
94  *
95  *   http://www.w3.org/hypertext/WWW/Daemon/User/Config/General.html#MetaDir
96  *
97  * Change-log:
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.
108  */
109
110 #include "apr.h"
111 #include "apr_strings.h"
112
113 #define APR_WANT_STRFUNC
114 #include "apr_want.h"
115
116 #if APR_HAVE_SYS_TYPES_H
117 #include <sys/types.h>
118 #endif
119
120 #include "ap_config.h"
121 #include "httpd.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"
127 #include "apr_lib.h"
128
129 #define DIR_CMD_PERMS OR_INDEXES
130
131 #define DEFAULT_METADIR         ".web"
132 #define DEFAULT_METASUFFIX      ".meta"
133 #define DEFAULT_METAFILES       0
134
135 module AP_MODULE_DECLARE_DATA cern_meta_module;
136
137 typedef struct {
138     const char *metadir;
139     const char *metasuffix;
140     int metafiles;
141 } cern_meta_dir_config;
142
143 static void *create_cern_meta_dir_config(apr_pool_t *p, char *dummy)
144 {
145     cern_meta_dir_config *new =
146     (cern_meta_dir_config *) apr_palloc(p, sizeof(cern_meta_dir_config));
147
148     new->metadir = NULL;
149     new->metasuffix = NULL;
150     new->metafiles = DEFAULT_METAFILES;
151
152     return new;
153 }
154
155 static void *merge_cern_meta_dir_configs(apr_pool_t *p, void *basev, void *addv)
156 {
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));
161
162     new->metadir = add->metadir ? add->metadir : base->metadir;
163     new->metasuffix = add->metasuffix ? add->metasuffix : base->metasuffix;
164     new->metafiles = add->metafiles;
165
166     return new;
167 }
168
169 static const char *set_metadir(cmd_parms *parms, void *in_dconf, const char *arg)
170 {
171     cern_meta_dir_config *dconf = in_dconf;
172
173     dconf->metadir = arg;
174     return NULL;
175 }
176
177 static const char *set_metasuffix(cmd_parms *parms, void *in_dconf, const char *arg)
178 {
179     cern_meta_dir_config *dconf = in_dconf;
180
181     dconf->metasuffix = arg;
182     return NULL;
183 }
184
185 static const char *set_metafiles(cmd_parms *parms, void *in_dconf, int arg)
186 {
187     cern_meta_dir_config *dconf = in_dconf;
188
189     dconf->metafiles = arg;
190     return NULL;
191 }
192
193
194 static const command_rec cern_meta_cmds[] =
195 {
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"),
202     {NULL}
203 };
204
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?
207  */
208 static int scan_meta_file(request_rec *r, apr_file_t *f)
209 {
210     char w[MAX_STRING_LEN];
211     char *l;
212     int p;
213     apr_table_t *tmp_headers;
214
215     tmp_headers = apr_table_make(r->pool, 5);
216     while (apr_file_gets(w, MAX_STRING_LEN - 1, f) == APR_SUCCESS) {
217
218         /* Delete terminal (CR?)LF */
219
220         p = strlen(w);
221         if (p > 0 && w[p - 1] == '\n') {
222             if (p > 1 && w[p - 2] == '\015')
223                 w[p - 2] = '\0';
224             else
225                 w[p - 1] = '\0';
226         }
227
228         if (w[0] == '\0') {
229             return OK;
230         }
231
232         /* if we see a bogus header don't ignore it. Shout and scream */
233
234         if (!(l = strchr(w, ':'))) {
235             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
236                         "malformed header in meta file: %s", r->filename);
237             return HTTP_INTERNAL_SERVER_ERROR;
238         }
239
240         *l++ = '\0';
241         while (*l && apr_isspace(*l))
242             ++l;
243
244         if (!strcasecmp(w, "Content-type")) {
245             char *tmp;
246             /* Nuke trailing whitespace */
247
248             char *endp = l + strlen(l) - 1;
249             while (endp > l && apr_isspace(*endp))
250                 *endp-- = '\0';
251
252             tmp = apr_pstrdup(r->pool, l);
253             ap_content_type_tolower(tmp);
254             ap_set_content_type(r, tmp);
255         }
256         else if (!strcasecmp(w, "Status")) {
257             sscanf(l, "%d", &r->status);
258             r->status_line = apr_pstrdup(r->pool, l);
259         }
260         else {
261             apr_table_set(tmp_headers, w, l);
262         }
263     }
264     apr_table_overlap(r->headers_out, tmp_headers, APR_OVERLAP_TABLES_SET);
265     return OK;
266 }
267
268 static int add_cern_meta_data(request_rec *r)
269 {
270     char *metafilename;
271     char *leading_slash;
272     char *last_slash;
273     char *real_file;
274     char *scrap_book;
275     apr_file_t *f = NULL;
276     apr_status_t retcode;
277     cern_meta_dir_config *dconf;
278     int rv;
279     request_rec *rr;
280
281     dconf = ap_get_module_config(r->per_dir_config, &cern_meta_module);
282
283     if (!dconf->metafiles) {
284         return DECLINED;
285     };
286
287     /* if ./.web/$1.meta exists then output 'asis' */
288
289     if (r->finfo.filetype == 0) {
290         return DECLINED;
291     };
292
293     /* is this a directory? */
294     if (r->finfo.filetype == APR_DIR || r->uri[strlen(r->uri) - 1] == '/') {
295         return DECLINED;
296     };
297
298     /* what directory is this file in? */
299     scrap_book = apr_pstrdup(r->pool, r->filename);
300
301     leading_slash = strchr(scrap_book, '/');
302     last_slash = strrchr(scrap_book, '/');
303     if ((last_slash != NULL) && (last_slash != leading_slash)) {
304         /* skip over last slash */
305         real_file = last_slash;
306         real_file++;
307         *last_slash = '\0';
308     }
309     else {
310         /* no last slash, buh?! */
311         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
312                     "internal error in mod_cern_meta: %s", r->filename);
313         /* should really barf, but hey, let's be friends... */
314         return DECLINED;
315     };
316
317     metafilename = apr_pstrcat(r->pool, scrap_book, "/",
318                            dconf->metadir ? dconf->metadir : DEFAULT_METADIR,
319                            "/", real_file,
320                  dconf->metasuffix ? dconf->metasuffix : DEFAULT_METASUFFIX,
321                            NULL);
322
323     /* It sucks to require this subrequest to complete, because this
324      * means people must leave their meta files accessible to the world.
325      * A better solution might be a "safe open" feature of pfopen to avoid
326      * pipes, symlinks, and crap like that.
327      *
328      * In fact, this doesn't suck.  Because <Location > blocks are never run
329      * against sub_req_lookup_file, the meta can be somewhat protected by
330      * either masking it with a <Location > directive or alias, or stowing
331      * the file outside of the web document tree, while providing the
332      * appropriate directory blocks to allow access to it as a file.
333      */
334     rr = ap_sub_req_lookup_file(metafilename, r, NULL);
335     if (rr->status != HTTP_OK) {
336         ap_destroy_sub_req(rr);
337         return DECLINED;
338     }
339     ap_destroy_sub_req(rr);
340
341     retcode = apr_file_open(&f, metafilename, APR_READ, APR_OS_DEFAULT, r->pool);
342     if (retcode != APR_SUCCESS) {
343         if (APR_STATUS_IS_ENOENT(retcode)) {
344             return DECLINED;
345         }
346         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
347               "meta file permissions deny server access: %s", metafilename);
348         return HTTP_FORBIDDEN;
349     };
350
351     /* read the headers in */
352     rv = scan_meta_file(r, f);
353     apr_file_close(f);
354
355     return rv;
356 }
357
358 static void register_hooks(apr_pool_t *p)
359 {
360     ap_hook_fixups(add_cern_meta_data,NULL,NULL,APR_HOOK_MIDDLE);
361 }
362
363 module AP_MODULE_DECLARE_DATA cern_meta_module =
364 {
365     STANDARD20_MODULE_STUFF,
366     create_cern_meta_dir_config,/* dir config creater */
367     merge_cern_meta_dir_configs,/* dir merger --- default is to override */
368     NULL,                       /* server config */
369     NULL,                       /* merge server configs */
370     cern_meta_cmds,             /* command apr_table_t */
371     register_hooks              /* register hooks */
372 };