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.
18 * mod_actions.c: executes scripts based on MIME type or HTTP method
20 * by Alexei Kosut; based on mod_cgi.c, mod_mime.c and mod_includes.c,
21 * adapted by rst from original NCSA code by Rob McCool
25 * Action mime/type /cgi-bin/script
27 * will activate /cgi-bin/script when a file of content type mime/type is
28 * requested. It sends the URL and file path of the requested document using
29 * the standard CGI PATH_INFO and PATH_TRANSLATED environment variables.
31 * Script PUT /cgi-bin/script
33 * will activate /cgi-bin/script when a request is received with the
34 * HTTP method "PUT". The available method names are defined in httpd.h.
35 * If the method is GET, the script will only be activated if the requested
36 * URI includes query information (stuff after a ?-mark).
39 #include "apr_strings.h"
40 #define APR_WANT_STRFUNC
42 #include "ap_config.h"
44 #include "http_config.h"
45 #include "http_request.h"
46 #include "http_core.h"
47 #include "http_protocol.h"
48 #include "http_main.h"
50 #include "util_script.h"
53 apr_table_t *action_types; /* Added with Action... */
54 const char *scripted[METHODS]; /* Added with Script... */
55 int configured; /* True if Action or Script has been
56 * called at least once
60 module AP_MODULE_DECLARE_DATA actions_module;
62 static void *create_action_dir_config(apr_pool_t *p, char *dummy)
64 action_dir_config *new =
65 (action_dir_config *) apr_pcalloc(p, sizeof(action_dir_config));
67 new->action_types = apr_table_make(p, 4);
72 static void *merge_action_dir_configs(apr_pool_t *p, void *basev, void *addv)
74 action_dir_config *base = (action_dir_config *) basev;
75 action_dir_config *add = (action_dir_config *) addv;
76 action_dir_config *new = (action_dir_config *) apr_palloc(p,
77 sizeof(action_dir_config));
80 new->action_types = apr_table_overlay(p, add->action_types,
83 for (i = 0; i < METHODS; ++i) {
84 new->scripted[i] = add->scripted[i] ? add->scripted[i]
88 new->configured = (base->configured || add->configured);
92 static const char *add_action(cmd_parms *cmd, void *m_v,
93 const char *type, const char *script,
96 action_dir_config *m = (action_dir_config *)m_v;
98 if (option && strcasecmp(option, "virtual")) {
99 return apr_pstrcat(cmd->pool,
100 "unrecognized option '", option, "'", NULL);
103 apr_table_setn(m->action_types, type,
104 apr_pstrcat(cmd->pool, option ? "1" : "0", script, NULL));
110 static const char *set_script(cmd_parms *cmd, void *m_v,
111 const char *method, const char *script)
113 action_dir_config *m = (action_dir_config *)m_v;
115 if (cmd->pool == cmd->temp_pool) {
116 /* In .htaccess, we can't globally register new methods. */
117 methnum = ap_method_number_of(method);
120 /* ap_method_register recognizes already registered methods,
121 * so don't bother to check its previous existence explicitely.
123 methnum = ap_method_register(cmd->pool, method);
126 if (methnum == M_TRACE) {
127 return "TRACE not allowed for Script";
129 else if (methnum == M_INVALID) {
130 return apr_pstrcat(cmd->pool, "Could not register method '", method,
131 "' for Script", NULL);
134 m->scripted[methnum] = script;
140 static const command_rec action_cmds[] =
142 AP_INIT_TAKE23("Action", add_action, NULL, OR_FILEINFO,
143 "a media type followed by a script name"),
144 AP_INIT_TAKE2("Script", set_script, NULL, ACCESS_CONF | RSRC_CONF,
145 "a method followed by a script name"),
149 static int action_handler(request_rec *r)
151 action_dir_config *conf = (action_dir_config *)
152 ap_get_module_config(r->per_dir_config, &actions_module);
153 const char *t, *action;
157 if (!conf->configured) {
161 /* Note that this handler handles _all_ types, so handler is unchecked */
163 /* Set allowed stuff */
164 for (i = 0; i < METHODS; ++i) {
165 if (conf->scripted[i])
166 r->allowed |= (AP_METHOD_BIT << i);
169 /* First, check for the method-handling scripts */
170 if (r->method_number == M_GET) {
172 script = conf->scripted[M_GET];
177 script = conf->scripted[r->method_number];
180 /* Check for looping, which can happen if the CGI script isn't */
181 if (script && r->prev && r->prev->prev)
184 /* Second, check for actions (which override the method scripts) */
185 action = r->handler ? r->handler :
186 ap_field_noparam(r->pool, r->content_type);
188 if (action && (t = apr_table_get(conf->action_types, action))) {
189 if (*t++ == '0' && r->finfo.filetype == APR_NOFILE) {
190 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
191 "File does not exist: %s", r->filename);
192 return HTTP_NOT_FOUND;
196 /* propagate the handler name to the script
197 * (will be REDIRECT_HANDLER there)
199 apr_table_setn(r->subprocess_env, "HANDLER", action);
205 ap_internal_redirect_handler(apr_pstrcat(r->pool, script,
206 ap_escape_uri(r->pool, r->uri),
207 r->args ? "?" : NULL,
212 static void register_hooks(apr_pool_t *p)
214 ap_hook_handler(action_handler,NULL,NULL,APR_HOOK_LAST);
217 AP_DECLARE_MODULE(actions) =
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 */