]> granicus.if.org Git - apache/blob - modules/mappers/mod_actions.c
4f2b60aa2029f698e4361233d411cf61b6823b5a
[apache] / modules / mappers / mod_actions.c
1 /* ====================================================================
2  * Copyright (c) 1995-2000 The Apache Software Foundation.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer. 
10  *
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
14  *    distribution.
15  *
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/)."
20  *
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
24  *    apache@apache.org.
25  *
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.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the Apache Software Foundation
33  *    for use in the Apache HTTP server project (http://www.apache.org/)."
34  *
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  * ====================================================================
48  *
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/>.
55  *
56  */
57
58 /*
59  * mod_actions.c: executes scripts based on MIME type or HTTP method
60  *
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
63  *
64  * Usage instructions:
65  *
66  * Action mime/type /cgi-bin/script
67  * 
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.
71  *
72  * Script PUT /cgi-bin/script
73  *
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).
78  */
79
80 #include "ap_config.h"
81 #include "httpd.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"
87 #include "http_log.h"
88 #include "util_script.h"
89
90 typedef struct {
91     ap_table_t *action_types;       /* Added with Action... */
92     char *scripted[METHODS];   /* Added with Script... */
93 } action_dir_config;
94
95 module action_module;
96
97 static void *create_action_dir_config(ap_context_t *p, char *dummy)
98 {
99     action_dir_config *new =
100     (action_dir_config *) ap_palloc(p, sizeof(action_dir_config));
101
102     new->action_types = ap_make_table(p, 4);
103     memset(new->scripted, 0, sizeof(new->scripted));
104
105     return new;
106 }
107
108 static void *merge_action_dir_configs(ap_context_t *p, void *basev, void *addv)
109 {
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));
114     int i;
115
116     new->action_types = ap_overlay_tables(p, add->action_types,
117                                        base->action_types);
118
119     for (i = 0; i < METHODS; ++i) {
120         new->scripted[i] = add->scripted[i] ? add->scripted[i]
121                                             : base->scripted[i];
122     }
123     return new;
124 }
125
126 static const char *add_action(cmd_parms *cmd, action_dir_config * m, char *type,
127                               char *script)
128 {
129     ap_table_setn(m->action_types, type, script);
130     return NULL;
131 }
132
133 static const char *set_script(cmd_parms *cmd, action_dir_config * m,
134                               char *method, char *script)
135 {
136     int methnum;
137
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";
143     else
144         m->scripted[methnum] = script;
145
146     return NULL;
147 }
148
149 static const command_rec action_cmds[] =
150 {
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"},
155     {NULL}
156 };
157
158 static int action_handler(request_rec *r)
159 {
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;
163     const char *script;
164     int i;
165
166     /* Set allowed stuff */
167     for (i = 0; i < METHODS; ++i) {
168         if (conf->scripted[i])
169             r->allowed |= (1 << i);
170     }
171
172     /* First, check for the method-handling scripts */
173     if (r->method_number == M_GET) {
174         if (r->args)
175             script = conf->scripted[M_GET];
176         else
177             script = NULL;
178     }
179     else {
180         script = conf->scripted[r->method_number];
181     }
182
183     /* Check for looping, which can happen if the CGI script isn't */
184     if (script && r->prev && r->prev->prev)
185         return DECLINED;
186
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)))) {
190         script = t;
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);
194             return NOT_FOUND;
195         }
196     }
197
198     if (script == NULL)
199         return DECLINED;
200
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);
203     return OK;
204 }
205
206 static const handler_rec action_handlers[] =
207 {
208     {"*/*", action_handler},
209     {NULL}
210 };
211
212 module action_module =
213 {
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 */
222 };