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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "apr_strings.h"
20 #include "http_config.h"
22 #include "http_protocol.h"
23 #include "http_request.h"
26 extern module AP_MODULE_DECLARE_DATA log_debug_module;
29 ap_expr_info_t *msg_expr;
30 ap_expr_info_t *condition;
35 apr_array_header_t *entries;
38 const char *allhooks = "all";
39 const char * const hooks[] = {
40 "log_transaction", /* 0 */
41 "quick_handler", /* 1 */
43 "translate_name", /* 3 */
44 "map_to_storage", /* 4 */
46 "type_checker", /* 6 */
47 "check_access", /* 7 */
48 "check_access_ex", /* 8 */
49 "check_authn", /* 9 */
50 "check_authz", /* 10 */
51 "insert_filter", /* 11 */
55 static void do_debug_log(request_rec *r, const char *hookname)
57 log_debug_dirconf *dconf = ap_get_module_config(r->per_dir_config, &log_debug_module);
59 if (dconf->entries == NULL)
62 for (i = 0; i < dconf->entries->nelts; i++) {
63 const char *msg, *err;
64 msg_entry *entry = APR_ARRAY_IDX(dconf->entries, i, msg_entry *);
65 if (entry->hook != allhooks && entry->hook != hookname)
67 if (entry->condition) {
68 int ret = ap_expr_exec(r, entry->condition, &err);
70 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
71 "Can't evaluate condition: %s", err);
77 msg = ap_expr_str_exec(r, entry->msg_expr, &err);
79 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
80 "Can't evaluate message expression: %s", err);
82 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%s (%s hook, %s:%d)",
83 msg, hookname, entry->msg_expr->filename,
84 entry->msg_expr->line_number);
86 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%s", msg);
90 static int log_debug_log_transaction(request_rec *r)
92 do_debug_log(r, hooks[0]);
96 static int log_debug_quick_handler(request_rec *r, int lookup_uri)
98 do_debug_log(r, hooks[1]);
102 static int log_debug_handler(request_rec *r)
104 do_debug_log(r, hooks[2]);
108 static int log_debug_translate_name(request_rec *r)
110 do_debug_log(r, hooks[3]);
114 static int log_debug_map_to_storage(request_rec *r)
116 do_debug_log(r, hooks[4]);
120 static int log_debug_fixups(request_rec *r)
122 do_debug_log(r, hooks[5]);
126 static int log_debug_type_checker(request_rec *r)
128 do_debug_log(r, hooks[6]);
132 static int log_debug_check_access(request_rec *r)
134 do_debug_log(r, hooks[7]);
138 static int log_debug_check_access_ex(request_rec *r)
140 do_debug_log(r, hooks[8]);
144 static int log_debug_check_authn(request_rec *r)
146 do_debug_log(r, hooks[9]);
150 static int log_debug_check_authz(request_rec *r)
152 do_debug_log(r, hooks[10]);
156 static void log_debug_insert_filter(request_rec *r)
158 do_debug_log(r, hooks[11]);
161 static void *log_debug_create_dconf(apr_pool_t *p, char *dirspec)
163 log_debug_dirconf *dconf = apr_pcalloc(p, sizeof(log_debug_dirconf));
167 static void *log_debug_merge_dconf(apr_pool_t *p, void *parent_conf, void *new_conf)
169 log_debug_dirconf *merged = apr_pcalloc(p, sizeof(log_debug_dirconf));
170 const log_debug_dirconf *parent = parent_conf;
171 const log_debug_dirconf *new = new_conf;
173 if (parent->entries == NULL)
174 merged->entries = new->entries;
175 else if (new->entries == NULL)
176 merged->entries = parent->entries;
178 /* apr_array_append actually creates a new array */
179 merged->entries = apr_array_append(p, parent->entries, new->entries);
184 static const char *cmd_log_message(cmd_parms *cmd, void *dconf_, const char *arg1,
185 const char *arg2, const char *arg3)
187 msg_entry *entry = apr_pcalloc(cmd->pool, sizeof(msg_entry));
188 log_debug_dirconf *dconf = dconf_;
195 entry->msg_expr = ap_expr_parse_cmd(cmd, arg1, AP_EXPR_FLAG_STRING_RESULT|
196 AP_EXPR_FLAG_DONT_VARY,
199 return apr_psprintf(cmd->pool,
200 "Could not parse message expression '%s': %s",
203 for (i = 0; i < 2; i++) {
207 if (strncasecmp(args[i], "hook=", 5) == 0) {
208 const char *name = args[i] + 5;
211 if (strcasecmp(hooks[j], name) == 0) {
212 entry->hook = hooks[j];
217 if (entry->hook == NULL) {
218 if (strcmp(name, "*") == 0 || strcasecmp(name, allhooks) == 0)
219 entry->hook = allhooks;
221 return apr_psprintf(cmd->pool, "Invalid hook name: %s", name);
224 else if (strncasecmp(args[i], "expr=", 5) == 0) {
225 const char *expr = args[i] + 5;
226 entry->condition = ap_expr_parse_cmd(cmd, expr,
227 AP_EXPR_FLAG_DONT_VARY,
230 return apr_psprintf(cmd->pool,
231 "Could not parse expression '%s': %s",
235 return apr_psprintf(cmd->pool, "Invalid argument %s", args[i]);
238 if (entry->hook == NULL)
239 entry->hook = hooks[0];
242 dconf->entries = apr_array_make(cmd->pool, 4, sizeof(msg_entry *));
244 APR_ARRAY_PUSH(dconf->entries, msg_entry *) = entry;
249 static const command_rec log_debug_cmds[] =
251 AP_INIT_TAKE123("LogMessage", cmd_log_message, NULL, RSRC_CONF|ACCESS_CONF,
252 "Log a debug message to the error log if this config block is used for "
257 static void register_hooks(apr_pool_t *p)
259 ap_hook_log_transaction(log_debug_log_transaction, NULL, NULL, APR_HOOK_FIRST);
260 ap_hook_quick_handler(log_debug_quick_handler, NULL, NULL, APR_HOOK_FIRST);
261 ap_hook_handler(log_debug_handler, NULL, NULL, APR_HOOK_FIRST);
262 ap_hook_translate_name(log_debug_translate_name, NULL, NULL, APR_HOOK_FIRST);
263 ap_hook_map_to_storage(log_debug_map_to_storage, NULL, NULL, APR_HOOK_FIRST);
264 ap_hook_fixups(log_debug_fixups, NULL, NULL, APR_HOOK_FIRST);
265 ap_hook_type_checker(log_debug_type_checker, NULL, NULL, APR_HOOK_FIRST);
266 ap_hook_check_access(log_debug_check_access, NULL, NULL, APR_HOOK_FIRST, AP_AUTH_INTERNAL_PER_URI);
267 ap_hook_check_access_ex(log_debug_check_access_ex, NULL, NULL, APR_HOOK_FIRST, AP_AUTH_INTERNAL_PER_URI);
268 ap_hook_check_authn(log_debug_check_authn, NULL, NULL, APR_HOOK_FIRST, AP_AUTH_INTERNAL_PER_URI);
269 ap_hook_check_authz(log_debug_check_authz, NULL, NULL, APR_HOOK_FIRST, AP_AUTH_INTERNAL_PER_URI);
270 ap_hook_insert_filter(log_debug_insert_filter, NULL, NULL, APR_HOOK_FIRST);
273 AP_DECLARE_MODULE(log_debug) =
275 STANDARD20_MODULE_STUFF,
276 log_debug_create_dconf, /* create per-dir config */
277 log_debug_merge_dconf, /* merge per-dir config */
278 NULL, /* server config */
279 NULL, /* merge server config */
280 log_debug_cmds, /* command apr_table_t */
281 register_hooks /* register hooks */