mod_actions.c revision 7c2df38555e33d4c30de9973722128cc9646a1b3
6ae232055d4d8a97267517c5e50074c2c819941and/* Licensed to the Apache Software Foundation (ASF) under one or more
6ae232055d4d8a97267517c5e50074c2c819941and * contributor license agreements. See the NOTICE file distributed with
6ae232055d4d8a97267517c5e50074c2c819941and * this work for additional information regarding copyright ownership.
6ae232055d4d8a97267517c5e50074c2c819941and * The ASF licenses this file to You under the Apache License, Version 2.0
6ae232055d4d8a97267517c5e50074c2c819941and * (the "License"); you may not use this file except in compliance with
6ae232055d4d8a97267517c5e50074c2c819941and * the License. You may obtain a copy of the License at
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * http://www.apache.org/licenses/LICENSE-2.0
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * Unless required by applicable law or agreed to in writing, software
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * distributed under the License is distributed on an "AS IS" BASIS,
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * See the License for the specific language governing permissions and
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * limitations under the License.
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and/*
6ae232055d4d8a97267517c5e50074c2c819941and * mod_actions.c: executes scripts based on MIME type or HTTP method
3f08db06526d6901aa08c110b5bc7dde6bc39905nd *
6ae232055d4d8a97267517c5e50074c2c819941and * by Alexei Kosut; based on mod_cgi.c, mod_mime.c and mod_includes.c,
6ae232055d4d8a97267517c5e50074c2c819941and * adapted by rst from original NCSA code by Rob McCool
6ae232055d4d8a97267517c5e50074c2c819941and *
b43f840409794ed298e8634f6284741f193b6c4ftakashi * Usage instructions:
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * Action mime/type /cgi-bin/script
6ae232055d4d8a97267517c5e50074c2c819941and *
b43f840409794ed298e8634f6284741f193b6c4ftakashi * will activate /cgi-bin/script when a file of content type mime/type is
6ae232055d4d8a97267517c5e50074c2c819941and * requested. It sends the URL and file path of the requested document using
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung * the standard CGI PATH_INFO and PATH_TRANSLATED environment variables.
6ae232055d4d8a97267517c5e50074c2c819941and *
b43f840409794ed298e8634f6284741f193b6c4ftakashi * Script PUT /cgi-bin/script
b43f840409794ed298e8634f6284741f193b6c4ftakashi *
b43f840409794ed298e8634f6284741f193b6c4ftakashi * will activate /cgi-bin/script when a request is received with the
b43f840409794ed298e8634f6284741f193b6c4ftakashi * HTTP method "PUT". The available method names are defined in httpd.h.
6ae232055d4d8a97267517c5e50074c2c819941and * If the method is GET, the script will only be activated if the requested
6ae232055d4d8a97267517c5e50074c2c819941and * URI includes query information (stuff after a ?-mark).
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_strings.h"
6ae232055d4d8a97267517c5e50074c2c819941and#define APR_WANT_STRFUNC
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_want.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "ap_config.h"
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi#include "httpd.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "http_config.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "http_request.h"
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi#include "http_core.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "http_protocol.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "http_main.h"
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi#include "http_log.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "util_script.h"
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andtypedef struct {
6ae232055d4d8a97267517c5e50074c2c819941and apr_table_t *action_types; /* Added with Action... */
6ae232055d4d8a97267517c5e50074c2c819941and const char *scripted[METHODS]; /* Added with Script... */
6ae232055d4d8a97267517c5e50074c2c819941and int configured; /* True if Action or Script has been
6ae232055d4d8a97267517c5e50074c2c819941and * called at least once
6ae232055d4d8a97267517c5e50074c2c819941and */
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi} action_dir_config;
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
1d980e5489836e977ba59b419e27b0ec875c4bd3takashimodule AP_MODULE_DECLARE_DATA actions_module;
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
6ae232055d4d8a97267517c5e50074c2c819941andstatic void *create_action_dir_config(apr_pool_t *p, char *dummy)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and action_dir_config *new =
6ae232055d4d8a97267517c5e50074c2c819941and (action_dir_config *) apr_pcalloc(p, sizeof(action_dir_config));
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and new->action_types = apr_table_make(p, 4);
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and return new;
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic void *merge_action_dir_configs(apr_pool_t *p, void *basev, void *addv)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and action_dir_config *base = (action_dir_config *) basev;
6ae232055d4d8a97267517c5e50074c2c819941and action_dir_config *add = (action_dir_config *) addv;
6ae232055d4d8a97267517c5e50074c2c819941and action_dir_config *new = (action_dir_config *) apr_palloc(p,
6ae232055d4d8a97267517c5e50074c2c819941and sizeof(action_dir_config));
6ae232055d4d8a97267517c5e50074c2c819941and int i;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and new->action_types = apr_table_overlay(p, add->action_types,
6ae232055d4d8a97267517c5e50074c2c819941and base->action_types);
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and for (i = 0; i < METHODS; ++i) {
6ae232055d4d8a97267517c5e50074c2c819941and new->scripted[i] = add->scripted[i] ? add->scripted[i]
6ae232055d4d8a97267517c5e50074c2c819941and : base->scripted[i];
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and new->configured = (base->configured || add->configured);
6ae232055d4d8a97267517c5e50074c2c819941and return new;
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic const char *add_action(cmd_parms *cmd, void *m_v,
6ae232055d4d8a97267517c5e50074c2c819941and const char *type, const char *script,
6ae232055d4d8a97267517c5e50074c2c819941and const char *option)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and action_dir_config *m = (action_dir_config *)m_v;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and if (option && strcasecmp(option, "virtual")) {
6ae232055d4d8a97267517c5e50074c2c819941and return apr_pstrcat(cmd->pool,
6ae232055d4d8a97267517c5e50074c2c819941and "unrecognized option '", option, "'", NULL);
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and apr_table_setn(m->action_types, type,
6ae232055d4d8a97267517c5e50074c2c819941and apr_pstrcat(cmd->pool, option ? "1" : "0", script, NULL));
6ae232055d4d8a97267517c5e50074c2c819941and m->configured = 1;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and return NULL;
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic const char *set_script(cmd_parms *cmd, void *m_v,
6ae232055d4d8a97267517c5e50074c2c819941and const char *method, const char *script)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and action_dir_config *m = (action_dir_config *)m_v;
6ae232055d4d8a97267517c5e50074c2c819941and int methnum;
6ae232055d4d8a97267517c5e50074c2c819941and if (cmd->pool == cmd->temp_pool) {
6ae232055d4d8a97267517c5e50074c2c819941and /* In .htaccess, we can't globally register new methods. */
6ae232055d4d8a97267517c5e50074c2c819941and methnum = ap_method_number_of(method);
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and else {
6ae232055d4d8a97267517c5e50074c2c819941and /* ap_method_register recognizes already registered methods,
6ae232055d4d8a97267517c5e50074c2c819941and * so don't bother to check its previous existence explicitely.
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and methnum = ap_method_register(cmd->pool, method);
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and if (methnum == M_TRACE) {
6ae232055d4d8a97267517c5e50074c2c819941and return "TRACE not allowed for Script";
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and else if (methnum == M_INVALID) {
6ae232055d4d8a97267517c5e50074c2c819941and return apr_pstrcat(cmd->pool, "Could not register method '", method,
6ae232055d4d8a97267517c5e50074c2c819941and "' for Script", NULL);
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and m->scripted[methnum] = script;
6ae232055d4d8a97267517c5e50074c2c819941and m->configured = 1;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and return NULL;
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic const command_rec action_cmds[] =
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and AP_INIT_TAKE23("Action", add_action, NULL, OR_FILEINFO,
6ae232055d4d8a97267517c5e50074c2c819941and "a media type followed by a script name"),
6ae232055d4d8a97267517c5e50074c2c819941and AP_INIT_TAKE2("Script", set_script, NULL, ACCESS_CONF | RSRC_CONF,
6ae232055d4d8a97267517c5e50074c2c819941and "a method followed by a script name"),
6ae232055d4d8a97267517c5e50074c2c819941and {NULL}
6ae232055d4d8a97267517c5e50074c2c819941and};
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic int action_handler(request_rec *r)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and action_dir_config *conf = (action_dir_config *)
6ae232055d4d8a97267517c5e50074c2c819941and ap_get_module_config(r->per_dir_config, &actions_module);
6ae232055d4d8a97267517c5e50074c2c819941and const char *t, *action;
6ae232055d4d8a97267517c5e50074c2c819941and const char *script;
6ae232055d4d8a97267517c5e50074c2c819941and int i;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and if (!conf->configured) {
6ae232055d4d8a97267517c5e50074c2c819941and return DECLINED;
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and /* Note that this handler handles _all_ types, so handler is unchecked */
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and /* Set allowed stuff */
6ae232055d4d8a97267517c5e50074c2c819941and for (i = 0; i < METHODS; ++i) {
6ae232055d4d8a97267517c5e50074c2c819941and if (conf->scripted[i])
6ae232055d4d8a97267517c5e50074c2c819941and r->allowed |= (AP_METHOD_BIT << i);
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and /* First, check for the method-handling scripts */
6ae232055d4d8a97267517c5e50074c2c819941and if (r->method_number == M_GET) {
6ae232055d4d8a97267517c5e50074c2c819941and if (r->args)
6ae232055d4d8a97267517c5e50074c2c819941and script = conf->scripted[M_GET];
6ae232055d4d8a97267517c5e50074c2c819941and else
6ae232055d4d8a97267517c5e50074c2c819941and script = NULL;
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and else {
6ae232055d4d8a97267517c5e50074c2c819941and script = conf->scripted[r->method_number];
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and /* Check for looping, which can happen if the CGI script isn't */
6ae232055d4d8a97267517c5e50074c2c819941and if (script && r->prev && r->prev->prev)
6ae232055d4d8a97267517c5e50074c2c819941and return DECLINED;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and /* Second, check for actions (which override the method scripts) */
6ae232055d4d8a97267517c5e50074c2c819941and action = r->handler ? r->handler :
6ae232055d4d8a97267517c5e50074c2c819941and ap_field_noparam(r->pool, r->content_type);
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and if (action && (t = apr_table_get(conf->action_types, action))) {
6ae232055d4d8a97267517c5e50074c2c819941and if (*t++ == '0' && r->finfo.filetype == APR_NOFILE) {
6ae232055d4d8a97267517c5e50074c2c819941and ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
6ae232055d4d8a97267517c5e50074c2c819941and "File does not exist: %s", r->filename);
6ae232055d4d8a97267517c5e50074c2c819941and return HTTP_NOT_FOUND;
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and script = t;
6ae232055d4d8a97267517c5e50074c2c819941and /* propagate the handler name to the script
6ae232055d4d8a97267517c5e50074c2c819941and * (will be REDIRECT_HANDLER there)
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and apr_table_setn(r->subprocess_env, "HANDLER", action);
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and if (script == NULL)
6ae232055d4d8a97267517c5e50074c2c819941and return DECLINED;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and ap_internal_redirect_handler(apr_pstrcat(r->pool, script,
6ae232055d4d8a97267517c5e50074c2c819941and ap_escape_uri(r->pool, r->uri),
6ae232055d4d8a97267517c5e50074c2c819941and r->args ? "?" : NULL,
6ae232055d4d8a97267517c5e50074c2c819941and r->args, NULL), r);
6ae232055d4d8a97267517c5e50074c2c819941and return OK;
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic void register_hooks(apr_pool_t *p)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and ap_hook_handler(action_handler,NULL,NULL,APR_HOOK_LAST);
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andAP_DECLARE_MODULE(actions) =
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and STANDARD20_MODULE_STUFF,
6ae232055d4d8a97267517c5e50074c2c819941and create_action_dir_config, /* dir config creater */
6ae232055d4d8a97267517c5e50074c2c819941and merge_action_dir_configs, /* dir merger --- default is to override */
6ae232055d4d8a97267517c5e50074c2c819941and NULL, /* server config */
6ae232055d4d8a97267517c5e50074c2c819941and NULL, /* merge server config */
6ae232055d4d8a97267517c5e50074c2c819941and action_cmds, /* command apr_table_t */
6ae232055d4d8a97267517c5e50074c2c819941and register_hooks /* register hooks */
6ae232055d4d8a97267517c5e50074c2c819941and};
6ae232055d4d8a97267517c5e50074c2c819941and