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.
60 * mod_actions.c: executes scripts based on MIME type or HTTP method
62 * by Alexei Kosut; based on mod_cgi.c, mod_mime.c and mod_includes.c,
63 * adapted by rst from original NCSA code by Rob McCool
67 * Action mime/type /cgi-bin/script
69 * will activate /cgi-bin/script when a file of content type mime/type is
70 * requested. It sends the URL and file path of the requested document using
71 * the standard CGI PATH_INFO and PATH_TRANSLATED environment variables.
73 * Script PUT /cgi-bin/script
75 * will activate /cgi-bin/script when a request is received with the
76 * HTTP method "PUT". The available method names are defined in httpd.h.
77 * If the method is GET, the script will only be activated if the requested
78 * URI includes query information (stuff after a ?-mark).
81 #include "apr_strings.h"
82 #include "ap_config.h"
84 #include "http_config.h"
85 #include "http_request.h"
86 #include "http_core.h"
87 #include "http_protocol.h"
88 #include "http_main.h"
90 #include "util_script.h"
93 apr_table_t *action_types; /* Added with Action... */
94 const char *scripted[METHODS]; /* Added with Script... */
99 static void *create_action_dir_config(apr_pool_t *p, char *dummy)
101 action_dir_config *new =
102 (action_dir_config *) apr_pcalloc(p, sizeof(action_dir_config));
104 new->action_types = apr_make_table(p, 4);
109 static void *merge_action_dir_configs(apr_pool_t *p, void *basev, void *addv)
111 action_dir_config *base = (action_dir_config *) basev;
112 action_dir_config *add = (action_dir_config *) addv;
113 action_dir_config *new = (action_dir_config *) apr_palloc(p,
114 sizeof(action_dir_config));
117 new->action_types = apr_overlay_tables(p, add->action_types,
120 for (i = 0; i < METHODS; ++i) {
121 new->scripted[i] = add->scripted[i] ? add->scripted[i]
127 static const char *add_action(cmd_parms *cmd, void *m_v,
128 const char *type, const char *script)
130 action_dir_config *m = (action_dir_config *)m_v;
131 apr_table_setn(m->action_types, type, script);
135 static const char *set_script(cmd_parms *cmd, void *m_v,
136 const char *method, const char *script)
138 action_dir_config *m = (action_dir_config *)m_v;
141 methnum = ap_method_number_of(method);
142 if (methnum == M_TRACE)
143 return "TRACE not allowed for Script";
144 else if (methnum == M_INVALID)
145 return "Unknown method type for Script";
147 m->scripted[methnum] = script;
152 static const command_rec action_cmds[] =
154 AP_INIT_TAKE2("Action", add_action, NULL, OR_FILEINFO,
155 "a media type followed by a script name"),
156 AP_INIT_TAKE2("Script", set_script, NULL, ACCESS_CONF | RSRC_CONF,
157 "a method followed by a script name"),
161 static int action_handler(request_rec *r)
163 action_dir_config *conf = (action_dir_config *)
164 ap_get_module_config(r->per_dir_config, &action_module);
165 const char *t, *action = r->handler ? r->handler :
166 ap_field_noparam(r->pool, r->content_type);
170 /* Note that this handler handles _all_ types, so handler is unchecked */
172 /* Set allowed stuff */
173 for (i = 0; i < METHODS; ++i) {
174 if (conf->scripted[i])
175 r->allowed |= (1 << i);
178 /* First, check for the method-handling scripts */
179 if (r->method_number == M_GET) {
181 script = conf->scripted[M_GET];
186 script = conf->scripted[r->method_number];
189 /* Check for looping, which can happen if the CGI script isn't */
190 if (script && r->prev && r->prev->prev)
193 /* Second, check for actions (which override the method scripts) */
194 if ((t = apr_table_get(conf->action_types,
195 action ? action : ap_default_type(r)))) {
197 if (r->finfo.protection == 0) {
198 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
199 "File does not exist: %s", r->filename);
200 return HTTP_NOT_FOUND;
207 ap_internal_redirect_handler(apr_pstrcat(r->pool, script, ap_escape_uri(r->pool,
208 r->uri), r->args ? "?" : NULL, r->args, NULL), r);
212 static void register_hooks(apr_pool_t *p)
214 ap_hook_handler(action_handler,NULL,NULL,AP_HOOK_LAST);
217 module action_module =
219 STANDARD20_MODULE_STUFF,
220 create_action_dir_config, /* dir config creater */
221 merge_action_dir_configs, /* dir merger --- default is to override */
222 NULL, /* server config */
223 NULL, /* merge server config */
224 action_cmds, /* command apr_table_t */
225 register_hooks /* register hooks */