upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / modules / mappers / mod_actions.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /*
18  * mod_actions.c: executes scripts based on MIME type or HTTP method
19  *
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
22  *
23  * Usage instructions:
24  *
25  * Action mime/type /cgi-bin/script
26  * 
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.
30  *
31  * Script PUT /cgi-bin/script
32  *
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).
37  */
38
39 #include "apr_strings.h"
40 #include "ap_config.h"
41 #include "httpd.h"
42 #include "http_config.h"
43 #include "http_request.h"
44 #include "http_core.h"
45 #include "http_protocol.h"
46 #include "http_main.h"
47 #include "http_log.h"
48 #include "util_script.h"
49
50 typedef struct {
51     apr_table_t *action_types;       /* Added with Action... */
52     const char *scripted[METHODS];   /* Added with Script... */
53     int configured;  /* True if Action or Script has been
54                       * called at least once
55                       */
56 } action_dir_config;
57
58 module AP_MODULE_DECLARE_DATA actions_module;
59
60 static void *create_action_dir_config(apr_pool_t *p, char *dummy)
61 {
62     action_dir_config *new =
63     (action_dir_config *) apr_pcalloc(p, sizeof(action_dir_config));
64
65     new->action_types = apr_table_make(p, 4);
66
67     return new;
68 }
69
70 static void *merge_action_dir_configs(apr_pool_t *p, void *basev, void *addv)
71 {
72     action_dir_config *base = (action_dir_config *) basev;
73     action_dir_config *add = (action_dir_config *) addv;
74     action_dir_config *new = (action_dir_config *) apr_palloc(p,
75                                   sizeof(action_dir_config));
76     int i;
77
78     new->action_types = apr_table_overlay(p, add->action_types,
79                                        base->action_types);
80
81     for (i = 0; i < METHODS; ++i) {
82         new->scripted[i] = add->scripted[i] ? add->scripted[i]
83                                             : base->scripted[i];
84     }
85
86     new->configured = (base->configured || add->configured);
87     return new;
88 }
89
90 static const char *add_action(cmd_parms *cmd, void *m_v, 
91                               const char *type, const char *script)
92 {
93     action_dir_config *m = (action_dir_config *)m_v;
94     apr_table_setn(m->action_types, type, script);
95     m->configured = 1;
96     return NULL;
97 }
98
99 static const char *set_script(cmd_parms *cmd, void *m_v,
100                               const char *method, const char *script)
101 {
102     action_dir_config *m = (action_dir_config *)m_v;    
103     int methnum;
104
105     methnum = ap_method_number_of(method);
106     if (methnum == M_TRACE)
107         return "TRACE not allowed for Script";
108     else if (methnum == M_INVALID)
109         return "Unknown method type for Script";
110     else
111         m->scripted[methnum] = script;
112
113     m->configured = 1;
114     return NULL;
115 }
116
117 static const command_rec action_cmds[] =
118 {
119     AP_INIT_TAKE2("Action", add_action, NULL, OR_FILEINFO,
120                   "a media type followed by a script name"),
121     AP_INIT_TAKE2("Script", set_script, NULL, ACCESS_CONF | RSRC_CONF,
122                   "a method followed by a script name"),
123     {NULL}
124 };
125
126 static int action_handler(request_rec *r)
127 {
128     action_dir_config *conf = (action_dir_config *)
129         ap_get_module_config(r->per_dir_config, &actions_module);
130     const char *t, *action;
131     const char *script;
132     int i;
133
134     if (!conf->configured) {
135         return DECLINED;
136     }
137
138     /* Note that this handler handles _all_ types, so handler is unchecked */
139
140     /* Set allowed stuff */
141     for (i = 0; i < METHODS; ++i) {
142         if (conf->scripted[i])
143             r->allowed |= (AP_METHOD_BIT << i);
144     }
145
146     /* First, check for the method-handling scripts */
147     if (r->method_number == M_GET) {
148         if (r->args)
149             script = conf->scripted[M_GET];
150         else
151             script = NULL;
152     }
153     else {
154         script = conf->scripted[r->method_number];
155     }
156
157     /* Check for looping, which can happen if the CGI script isn't */
158     if (script && r->prev && r->prev->prev)
159         return DECLINED;
160
161     /* Second, check for actions (which override the method scripts) */
162     action = r->handler ? r->handler :
163         ap_field_noparam(r->pool, r->content_type);
164     if ((t = apr_table_get(conf->action_types,
165                        action ? action : ap_default_type(r)))) {
166         script = t;
167         if (r->finfo.filetype == 0) {
168             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
169                         "File does not exist: %s", r->filename);
170             return HTTP_NOT_FOUND;
171         }
172     }
173
174     if (script == NULL)
175         return DECLINED;
176
177     ap_internal_redirect_handler(apr_pstrcat(r->pool, script,
178                                              ap_escape_uri(r->pool, r->uri),
179                                              r->args ? "?" : NULL,
180                                              r->args, NULL), r);
181     return OK;
182 }
183
184 static void register_hooks(apr_pool_t *p)
185 {
186     ap_hook_handler(action_handler,NULL,NULL,APR_HOOK_LAST);
187 }
188
189 module AP_MODULE_DECLARE_DATA actions_module =
190 {
191     STANDARD20_MODULE_STUFF,
192     create_action_dir_config,   /* dir config creater */
193     merge_action_dir_configs,   /* dir merger --- default is to override */
194     NULL,                       /* server config */
195     NULL,                       /* merge server config */
196     action_cmds,                /* command apr_table_t */
197     register_hooks              /* register hooks */
198 };