]> granicus.if.org Git - apache/blob - modules/mappers/mod_actions.c
make sure we have prototype for strcasecmp()
[apache] / modules / mappers / mod_actions.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
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
17  *    distribution.
18  *
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.
25  *
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.
30  *
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.
34  *
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
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
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/>.
53  *
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.
57  */
58
59 /*
60  * mod_actions.c: executes scripts based on MIME type or HTTP method
61  *
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
64  *
65  * Usage instructions:
66  *
67  * Action mime/type /cgi-bin/script
68  * 
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.
72  *
73  * Script PUT /cgi-bin/script
74  *
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).
79  */
80
81 #include "apr_strings.h"
82 #define APR_WANT_STRFUNC
83 #include "apr_want.h"
84 #include "ap_config.h"
85 #include "httpd.h"
86 #include "http_config.h"
87 #include "http_request.h"
88 #include "http_core.h"
89 #include "http_protocol.h"
90 #include "http_main.h"
91 #include "http_log.h"
92 #include "util_script.h"
93
94 typedef struct {
95     apr_table_t *action_types;       /* Added with Action... */
96     const char *scripted[METHODS];   /* Added with Script... */
97     int configured;  /* True if Action or Script has been
98                       * called at least once
99                       */
100 } action_dir_config;
101
102 module AP_MODULE_DECLARE_DATA actions_module;
103
104 static void *create_action_dir_config(apr_pool_t *p, char *dummy)
105 {
106     action_dir_config *new =
107     (action_dir_config *) apr_pcalloc(p, sizeof(action_dir_config));
108
109     new->action_types = apr_table_make(p, 4);
110
111     return new;
112 }
113
114 static void *merge_action_dir_configs(apr_pool_t *p, void *basev, void *addv)
115 {
116     action_dir_config *base = (action_dir_config *) basev;
117     action_dir_config *add = (action_dir_config *) addv;
118     action_dir_config *new = (action_dir_config *) apr_palloc(p,
119                                   sizeof(action_dir_config));
120     int i;
121
122     new->action_types = apr_table_overlay(p, add->action_types,
123                                        base->action_types);
124
125     for (i = 0; i < METHODS; ++i) {
126         new->scripted[i] = add->scripted[i] ? add->scripted[i]
127                                             : base->scripted[i];
128     }
129
130     new->configured = (base->configured || add->configured);
131     return new;
132 }
133
134 static const char *add_action(cmd_parms *cmd, void *m_v, 
135                               const char *type, const char *script,
136                               const char *option)
137 {
138     action_dir_config *m = (action_dir_config *)m_v;
139
140     if (option && strcasecmp(option, "virtual")) {
141         return apr_pstrcat(cmd->pool,
142                            "unrecognized option '", option, "'", NULL);
143     }
144
145     apr_table_setn(m->action_types, type,
146                    apr_pstrcat(cmd->pool, option ? "1" : "0", script, NULL));
147     m->configured = 1;
148
149     return NULL;
150 }
151
152 static const char *set_script(cmd_parms *cmd, void *m_v,
153                               const char *method, const char *script)
154 {
155     action_dir_config *m = (action_dir_config *)m_v;    
156     
157     /* ap_method_register recognizes already registered methods,
158      * so don't bother to check its previous existence explicitely.
159      */
160     int methnum = ap_method_register(cmd->pool, method);
161
162     if (methnum == M_TRACE) {
163         return "TRACE not allowed for Script";
164     }
165     else if (methnum == M_INVALID) {
166         return apr_pstrcat(cmd->pool, "Could not register method '", method,
167                            "' for Script", NULL);
168     }
169
170     m->scripted[methnum] = script;
171     m->configured = 1;
172
173     return NULL;
174 }
175
176 static const command_rec action_cmds[] =
177 {
178     AP_INIT_TAKE23("Action", add_action, NULL, OR_FILEINFO,
179                   "a media type followed by a script name"),
180     AP_INIT_TAKE2("Script", set_script, NULL, ACCESS_CONF | RSRC_CONF,
181                   "a method followed by a script name"),
182     {NULL}
183 };
184
185 static int action_handler(request_rec *r)
186 {
187     action_dir_config *conf = (action_dir_config *)
188         ap_get_module_config(r->per_dir_config, &actions_module);
189     const char *t, *action;
190     const char *script;
191     int i;
192
193     if (!conf->configured) {
194         return DECLINED;
195     }
196
197     /* Note that this handler handles _all_ types, so handler is unchecked */
198
199     /* Set allowed stuff */
200     for (i = 0; i < METHODS; ++i) {
201         if (conf->scripted[i])
202             r->allowed |= (AP_METHOD_BIT << i);
203     }
204
205     /* First, check for the method-handling scripts */
206     if (r->method_number == M_GET) {
207         if (r->args)
208             script = conf->scripted[M_GET];
209         else
210             script = NULL;
211     }
212     else {
213         script = conf->scripted[r->method_number];
214     }
215
216     /* Check for looping, which can happen if the CGI script isn't */
217     if (script && r->prev && r->prev->prev)
218         return DECLINED;
219
220     /* Second, check for actions (which override the method scripts) */
221     action = r->handler ? r->handler :
222         ap_field_noparam(r->pool, r->content_type);
223     action = action ? action : ap_default_type(r);
224
225     if ((t = apr_table_get(conf->action_types, action))) {
226         if (*t++ == '0' && r->finfo.filetype == 0) {
227             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
228                           "File does not exist: %s", r->filename);
229             return HTTP_NOT_FOUND;
230         }
231
232         script = t;
233         /* propagate the handler name to the script
234          * (will be REDIRECT_HANDLER there)
235          */
236         apr_table_setn(r->subprocess_env, "HANDLER", action);
237     }
238
239     if (script == NULL)
240         return DECLINED;
241
242     ap_internal_redirect_handler(apr_pstrcat(r->pool, script,
243                                              ap_escape_uri(r->pool, r->uri),
244                                              r->args ? "?" : NULL,
245                                              r->args, NULL), r);
246     return OK;
247 }
248
249 static void register_hooks(apr_pool_t *p)
250 {
251     ap_hook_handler(action_handler,NULL,NULL,APR_HOOK_LAST);
252 }
253
254 module AP_MODULE_DECLARE_DATA actions_module =
255 {
256     STANDARD20_MODULE_STUFF,
257     create_action_dir_config,   /* dir config creater */
258     merge_action_dir_configs,   /* dir merger --- default is to override */
259     NULL,                       /* server config */
260     NULL,                       /* merge server config */
261     action_cmds,                /* command apr_table_t */
262     register_hooks              /* register hooks */
263 };