httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ChristophDorn <christ...@christophdorn.com>
Subject Re: Analyze mod_rewrite log in browser
Date Sat, 19 Apr 2008 23:33:15 GMT


ChristophDorn wrote:
> 
> So if I write a patch for the 2.2.x branch and the trunk is this something
> that could be bundled with the release?
> 

Here is my patch for the 2.2.x branch that does what I need. Could use a bit
of refactoring but it shows the general idea. It introduces a new config
variable "RewriteLogFirePHP On|Off" and will only put the log data into the
header if rewrite logging is enabled and the FirePHP extension is installed
on client (based on User-Agent request header).

So the question is if this is generic enough to be included or if it needs
to be made more generic as mentioned in my other post.

Next step for me would be to work on how this log data is going to be
displayed in Firebug but it would be good to know if the patch can be
included in the general distribution or if users need to patch the source
themselves. I would say that the people who will most likely be using this
feature to troubleshoot their rewrite rules will likely not know how to
patch their own server.

Looking forward to your suggestions.

Christoph




Index: mod_rewrite.c
===================================================================
--- mod_rewrite.c	(revision 649830)
+++ mod_rewrite.c	(working copy)
@@ -281,6 +281,7 @@
     const char   *rewritelogfile;     /* the RewriteLog filename           
*/
     apr_file_t   *rewritelogfp;       /* the RewriteLog open filepointer   
*/
     int           rewriteloglevel;    /* the RewriteLog level of verbosity 
*/
+    int           rewritelogfirephp;  /* the RewriteLogFirePHP state       
*/ 
 #endif
     apr_hash_t         *rewritemaps;  /* the RewriteMap entries            
*/
     apr_array_header_t *rewriteconds; /* the RewriteCond entries (temp.)   
*/
@@ -476,6 +477,10 @@
     request_rec *req;
     va_list ap;
 
+    ap_regmatch_t firephp_regmatch[AP_MAX_REG_MATCH];
+    ap_regex_t *  firephp_preg;
+    char * firephp_regex;
+
     conf = ap_get_module_config(r->server->module_config, &rewrite_module);
 
     if (!conf->rewritelogfp || level > conf->rewriteloglevel) {
@@ -519,6 +524,48 @@
         /* XXX: Maybe this should be fatal? */
     }
 
+
+    /* Set response headers for FirePHP if enabled */
+    if (conf->rewritelogfirephp) {
+
+      /* TODO: Move this somewhere so it only gets compiled once */
+      firephp_regex = "\\sFirePHP\\/[\\.|\\d]*\\s?";
+      firephp_preg = ap_pregcomp(r->pool, firephp_regex, AP_REG_ICASE);
+
+      const apr_array_header_t *headers_out =
apr_table_elts(r->headers_out);
+
+      /* Check User-Agent request header to see if FirePHP is installed on
client */	
+      if(ap_regexec(firephp_preg,
apr_table_get(r->headers_in,"User-Agent"),
+                    AP_MAX_REG_MATCH, firephp_regmatch, 0)==0) {
+
+        /* Add header for logline */
+        apr_table_setn(r->headers_out,
+                       apr_pstrcat(r->pool, "X-FirePHP-Data-40010000",
+                                   apr_psprintf(r->pool, "%04d",
(headers_out->nelts + 2)), NULL), 
+                       apr_pstrcat(r->pool, "[\"LOG\",\"",
+                                   ap_escape_logitem(r->pool, logline),
"\"],", NULL));
+
+        /* Add framing headers (if not already present) to make valid JSON
string */      
+        char * h;
+        if(!apr_table_get(r->headers_out,"X-FirePHP-Data-100000000001")) {
+          h = "{";
+          apr_table_setn(r->headers_out, "X-FirePHP-Data-100000000001", h);
+        }
+        if(!apr_table_get(r->headers_out,"X-FirePHP-Data-400100000000")) {
+          h = "\"org.apache.httpd.mod_rewrite\":[";
+          apr_table_setn(r->headers_out, "X-FirePHP-Data-400100000000", h);
+        }
+        if(!apr_table_get(r->headers_out,"X-FirePHP-Data-400199999999")) {
+          h = "[\"__SKIP__\"]],";
+          apr_table_setn(r->headers_out, "X-FirePHP-Data-400199999999", h);
+        }
+        if(!apr_table_get(r->headers_out,"X-FirePHP-Data-999999999999")) {
+          h = "\"__SKIP__\":\"__SKIP__\"}";
+          apr_table_setn(r->headers_out, "X-FirePHP-Data-999999999999", h);
+        }
+      }
+    }
+
     nbytes = strlen(logline);
     apr_file_write(conf->rewritelogfp, logline, &nbytes);
 
@@ -2615,6 +2662,7 @@
     a->rewritelogfile  = NULL;
     a->rewritelogfp    = NULL;
     a->rewriteloglevel = 0;
+    a->rewritelogfirephp = 0;
 #endif
     a->rewritemaps     = apr_hash_make(p);
     a->rewriteconds    = apr_array_make(p, 2, sizeof(rewritecond_entry));
@@ -2652,6 +2700,9 @@
         a->rewritelogfp    = overrides->rewritelogfp != NULL
                              ? overrides->rewritelogfp
                              : base->rewritelogfp;
+        a->rewritelogfirephp = overrides->rewritelogfirephp != 0
+                               ? overrides->rewritelogfirephp
+                               : base->rewritelogfirephp;
 #endif
         a->rewritemaps     = apr_hash_overlay(p, overrides->rewritemaps,
                                               base->rewritemaps);
@@ -2669,6 +2720,7 @@
         a->rewriteloglevel = overrides->rewriteloglevel;
         a->rewritelogfile  = overrides->rewritelogfile;
         a->rewritelogfp    = overrides->rewritelogfp;
+        a->rewritelogfirephp = overrides->rewritelogfirephp;
 #endif
         a->rewritemaps     = overrides->rewritemaps;
         a->rewriteconds    = overrides->rewriteconds;
@@ -2815,6 +2867,17 @@
 
     return NULL;
 }
+
+static const char *cmd_rewritelogfirephpenable(cmd_parms *cmd, void *dconf,
+                                               int flag)
+{
+    rewrite_server_conf *sconf;
+
+    sconf = ap_get_module_config(cmd->server->module_config,
&rewrite_module);
+    sconf->rewritelogfirephp = ( flag ? 1 : 0 );
+
+    return NULL;
+}
 #endif /* rewritelog */
 
 static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char
*a1,
@@ -4833,6 +4896,8 @@
     AP_INIT_TAKE1(   "RewriteLogLevel", cmd_rewriteloglevel, NULL,
RSRC_CONF,
                      "the level of the rewriting logfile verbosity "
                      "(0=none, 1=std, .., 9=max)"),
+    AP_INIT_FLAG(    "RewriteLogFirePHP", cmd_rewritelogfirephpenable,
NULL,  RSRC_CONF,
+                     "On or Off to enable or disable FirePHP logging
support"),
 #else
     AP_INIT_TAKE1(   "RewriteLog", fake_rewritelog, NULL, RSRC_CONF,
                      "[DISABLED] the filename of the rewriting logfile"),


-- 
View this message in context: http://www.nabble.com/Analyze-mod_rewrite-log-in-browser-tp16732294p16788847.html
Sent from the Apache HTTP Server - Dev mailing list archive at Nabble.com.


Mime
View raw message