Apache HTTPD
mod_actions.c
Go to the documentation of this file.
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#define APR_WANT_STRFUNC
41#include "apr_want.h"
42#include "ap_config.h"
43#include "httpd.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"
49#include "http_log.h"
50#include "util_script.h"
51
52typedef struct {
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
57 */
59
60module AP_MODULE_DECLARE_DATA actions_module;
61
63{
66
67 new->action_types = apr_table_make(p, 4);
68
69 return new;
70}
71
72static void *merge_action_dir_configs(apr_pool_t *p, void *basev, void *addv)
73{
77 sizeof(action_dir_config));
78 int i;
79
81 base->action_types);
82
83 for (i = 0; i < METHODS; ++i) {
84 new->scripted[i] = add->scripted[i] ? add->scripted[i]
85 : base->scripted[i];
86 }
87
88 new->configured = (base->configured || add->configured);
89 return new;
90}
91
92static const char *add_action(cmd_parms *cmd, void *m_v,
93 const char *type, const char *script,
94 const char *option)
95{
97
98 if (option && strcasecmp(option, "virtual")) {
99 return apr_pstrcat(cmd->pool,
100 "unrecognized option '", option, "'", NULL);
101 }
102
103 apr_table_setn(m->action_types, type,
104 apr_pstrcat(cmd->pool, option ? "1" : "0", script, NULL));
105 m->configured = 1;
106
107 return NULL;
108}
109
110static const char *set_script(cmd_parms *cmd, void *m_v,
111 const char *method, const char *script)
112{
114 int methnum;
115 if (cmd->pool == cmd->temp_pool) {
116 /* In .htaccess, we can't globally register new methods. */
118 }
119 else {
120 /* ap_method_register recognizes already registered methods,
121 * so don't bother to check its previous existence explicitly.
122 */
124 }
125
126 if (methnum == M_TRACE) {
127 return "TRACE not allowed for Script";
128 }
129 else if (methnum == M_INVALID) {
130 return apr_pstrcat(cmd->pool, "Could not register method '", method,
131 "' for Script", NULL);
132 }
133
134 m->scripted[methnum] = script;
135 m->configured = 1;
136
137 return NULL;
138}
139
140static const command_rec action_cmds[] =
141{
143 "a media type followed by a script name"),
145 "a method followed by a script name"),
146 {NULL}
147};
148
150{
152 ap_get_module_config(r->per_dir_config, &actions_module);
153 const char *t, *action;
154 const char *script;
155 int i;
156
157 if (!conf->configured) {
158 return DECLINED;
159 }
160
161 /* Note that this handler handles _all_ types, so handler is unchecked */
162
163 /* Set allowed stuff */
164 for (i = 0; i < METHODS; ++i) {
165 if (conf->scripted[i])
166 r->allowed |= (AP_METHOD_BIT << i);
167 }
168
169 /* First, check for the method-handling scripts */
170 if (r->method_number == M_GET) {
171 if (r->args)
172 script = conf->scripted[M_GET];
173 else
174 script = NULL;
175 }
176 else {
177 script = conf->scripted[r->method_number];
178 }
179
180 /* Check for looping, which can happen if the CGI script isn't */
181 if (script && r->prev && r->prev->prev)
182 return DECLINED;
183
184 /* Second, check for actions (which override the method scripts) */
185 action = r->handler;
186 if (!action && AP_REQUEST_IS_TRUSTED_CT(r)) {
187 action = ap_field_noparam(r->pool, r->content_type);
188 }
189
190 if (action && (t = apr_table_get(conf->action_types, action))) {
191 int virtual = (*t++ == '0' ? 0 : 1);
192 if (!virtual && r->finfo.filetype == APR_NOFILE) {
194 "File does not exist: %s", r->filename);
195 return HTTP_NOT_FOUND;
196 }
197
198 script = t;
199 /* propagate the handler name to the script
200 * (will be REDIRECT_HANDLER there)
201 */
202 apr_table_setn(r->subprocess_env, "HANDLER", action);
203 if (virtual) {
204 apr_table_setn(r->notes, "virtual_script", "1");
205 }
206 }
207
208 if (script == NULL)
209 return DECLINED;
210
213 r->args ? "?" : NULL,
214 r->args, NULL), r);
215 return OK;
216}
217
222
224{
226 create_action_dir_config, /* dir config creater */
227 merge_action_dir_configs, /* dir merger --- default is to override */
228 NULL, /* server config */
229 NULL, /* merge server config */
230 action_cmds, /* command apr_table_t */
231 register_hooks /* register hooks */
232};
Symbol export macros and hook functions.
APR Strings library.
APR Standard Headers Support.
#define ap_get_module_config(v, m)
#define AP_DECLARE_MODULE(foo)
ap_conf_vector_t * base
void ap_hook_handler(ap_HOOK_handler_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition config.c:170
request_rec * r
#define AP_INIT_TAKE23(directive, func, mconfig, where, help)
#define AP_INIT_TAKE2(directive, func, mconfig, where, help)
#define DECLINED
Definition httpd.h:457
#define OK
Definition httpd.h:456
#define APLOGNO(n)
Definition http_log.h:117
#define APLOG_INFO
Definition http_log.h:70
#define ap_log_rerror
Definition http_log.h:454
#define APLOG_MARK
Definition http_log.h:283
int ap_method_number_of(const char *method)
int ap_method_register(apr_pool_t *p, const char *methname)
void ap_internal_redirect_handler(const char *new_uri, request_rec *r)
void * dummy
Definition http_vhost.h:62
#define AP_REQUEST_IS_TRUSTED_CT(r)
Definition httpd.h:696
#define APR_HOOK_LAST
Definition apr_hooks.h:305
#define ACCESS_CONF
#define RSRC_CONF
#define OR_FILEINFO
#define HTTP_NOT_FOUND
Definition httpd.h:512
#define AP_METHOD_BIT
Definition httpd.h:629
#define METHODS
Definition httpd.h:624
#define M_TRACE
Definition httpd.h:598
#define M_GET
Definition httpd.h:592
#define M_INVALID
Definition httpd.h:618
#define STANDARD20_MODULE_STUFF
#define ap_escape_uri(ppool, path)
Definition httpd.h:1836
char * ap_field_noparam(apr_pool_t *p, const char *intype)
Definition util.c:91
apr_size_t size
@ APR_NOFILE
int type
int strcasecmp(const char *a, const char *b)
apr_interval_time_t t
apr_uint32_t apr_pool_t apr_uint32_t apr_pollset_method_e method
Definition apr_poll.h:195
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
const void * m
apr_cmdtype_e cmd
Apache Configuration.
CORE HTTP Daemon.
Apache Logging library.
Command line options.
HTTP protocol handling.
Apache Request library.
HTTP Daemon routines.
apr_pool_t * p
Definition md_event.c:32
static void * create_action_dir_config(apr_pool_t *p, char *dummy)
Definition mod_actions.c:62
static void * merge_action_dir_configs(apr_pool_t *p, void *basev, void *addv)
Definition mod_actions.c:72
static const char * add_action(cmd_parms *cmd, void *m_v, const char *type, const char *script, const char *option)
Definition mod_actions.c:92
static void register_hooks(apr_pool_t *p)
static int action_handler(request_rec *r)
static const command_rec action_cmds[]
static const char * set_script(cmd_parms *cmd, void *m_v, const char *method, const char *script)
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
apr_table_t * action_types
Definition mod_actions.c:53
const char * scripted[64]
Definition mod_actions.c:54
apr_filetype_e filetype
A structure that represents the current request.
Definition httpd.h:845
char * uri
Definition httpd.h:1016
const char * content_type
Definition httpd.h:992
request_rec * prev
Definition httpd.h:856
const char * handler
Definition httpd.h:994
apr_table_t * notes
Definition httpd.h:985
int method_number
Definition httpd.h:898
apr_pool_t * pool
Definition httpd.h:847
char * filename
Definition httpd.h:1018
apr_finfo_t finfo
Definition httpd.h:1094
apr_int64_t allowed
Definition httpd.h:922
apr_table_t * subprocess_env
Definition httpd.h:983
struct ap_conf_vector_t * per_dir_config
Definition httpd.h:1047
char * args
Definition httpd.h:1026
Apache script tools.