1 /* ====================================================================
2 * Copyright (c) 1995-2000 The Apache Software Foundation. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the Apache Software Foundation
19 * for use in the Apache HTTP server project (http://www.apache.org/)."
21 * 4. The names "Apache Server" and "Apache Software Foundation" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
26 * 5. Products derived from this software may not be called "Apache"
27 * nor may "Apache" appear in their names without prior written
28 * permission of the Apache Software Foundation.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the Apache Software Foundation
33 * for use in the Apache HTTP server project (http://www.apache.org/)."
35 * THIS SOFTWARE IS PROVIDED BY THE Apache Software Foundation ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE 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
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation and was originally based
51 * on public domain software written at the National Center for
52 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
53 * For more information on the Apache Software Foundation and the Apache HTTP server
54 * project, please see <http://www.apache.org/>.
59 * mod_actions.c: executes scripts based on MIME type or HTTP method
61 * by Alexei Kosut; based on mod_cgi.c, mod_mime.c and mod_includes.c,
62 * adapted by rst from original NCSA code by Rob McCool
66 * Action mime/type /cgi-bin/script
68 * will activate /cgi-bin/script when a file of content type mime/type is
69 * requested. It sends the URL and file path of the requested document using
70 * the standard CGI PATH_INFO and PATH_TRANSLATED environment variables.
72 * Script PUT /cgi-bin/script
74 * will activate /cgi-bin/script when a request is received with the
75 * HTTP method "PUT". The available method names are defined in httpd.h.
76 * If the method is GET, the script will only be activated if the requested
77 * URI includes query information (stuff after a ?-mark).
80 #include "ap_config.h"
82 #include "http_config.h"
83 #include "http_request.h"
84 #include "http_core.h"
85 #include "http_protocol.h"
86 #include "http_main.h"
88 #include "util_script.h"
91 ap_table_t *action_types; /* Added with Action... */
92 char *scripted[METHODS]; /* Added with Script... */
97 static void *create_action_dir_config(ap_context_t *p, char *dummy)
99 action_dir_config *new =
100 (action_dir_config *) ap_palloc(p, sizeof(action_dir_config));
102 new->action_types = ap_make_table(p, 4);
103 memset(new->scripted, 0, sizeof(new->scripted));
108 static void *merge_action_dir_configs(ap_context_t *p, void *basev, void *addv)
110 action_dir_config *base = (action_dir_config *) basev;
111 action_dir_config *add = (action_dir_config *) addv;
112 action_dir_config *new = (action_dir_config *) ap_palloc(p,
113 sizeof(action_dir_config));
116 new->action_types = ap_overlay_tables(p, add->action_types,
119 for (i = 0; i < METHODS; ++i) {
120 new->scripted[i] = add->scripted[i] ? add->scripted[i]
126 static const char *add_action(cmd_parms *cmd, action_dir_config * m, char *type,
129 ap_table_setn(m->action_types, type, script);
133 static const char *set_script(cmd_parms *cmd, action_dir_config * m,
134 char *method, char *script)
138 methnum = ap_method_number_of(method);
139 if (methnum == M_TRACE)
140 return "TRACE not allowed for Script";
141 else if (methnum == M_INVALID)
142 return "Unknown method type for Script";
144 m->scripted[methnum] = script;
149 static const command_rec action_cmds[] =
151 {"Action", add_action, NULL, OR_FILEINFO, TAKE2,
152 "a media type followed by a script name"},
153 {"Script", set_script, NULL, ACCESS_CONF | RSRC_CONF, TAKE2,
154 "a method followed by a script name"},
158 static int action_handler(request_rec *r)
160 action_dir_config *conf = (action_dir_config *)
161 ap_get_module_config(r->per_dir_config, &action_module);
162 const char *t, *action = r->handler ? r->handler : r->content_type;
166 /* Set allowed stuff */
167 for (i = 0; i < METHODS; ++i) {
168 if (conf->scripted[i])
169 r->allowed |= (1 << i);
172 /* First, check for the method-handling scripts */
173 if (r->method_number == M_GET) {
175 script = conf->scripted[M_GET];
180 script = conf->scripted[r->method_number];
183 /* Check for looping, which can happen if the CGI script isn't */
184 if (script && r->prev && r->prev->prev)
187 /* Second, check for actions (which override the method scripts) */
188 if ((t = ap_table_get(conf->action_types,
189 action ? action : ap_default_type(r)))) {
191 if (r->finfo.protection == 0) {
192 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
193 "File does not exist: %s", r->filename);
201 ap_internal_redirect_handler(ap_pstrcat(r->pool, script, ap_escape_uri(r->pool,
202 r->uri), r->args ? "?" : NULL, r->args, NULL), r);
206 static const handler_rec action_handlers[] =
208 {"*/*", action_handler},
212 module action_module =
214 STANDARD20_MODULE_STUFF,
215 create_action_dir_config, /* dir config creater */
216 merge_action_dir_configs, /* dir merger --- default is to override */
217 NULL, /* server config */
218 NULL, /* merge server config */
219 action_cmds, /* command ap_table_t */
220 action_handlers, /* handlers */
221 NULL /* register hooks */