httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arnt Gulbrandsen <agul...@troll.no>
Subject updated patch (was Re: renaming after-effects)
Date Sun, 12 Apr 1998 16:24:05 GMT
Dean Gaudet <dgaudet@arctic.org>
> - I've spent 3 hours converting various patches.  This would have been
> avoided with HIDE.  I know this will trip up some other folks as well.

I just updated my mod_usertrack.c patch.  Seems to work just as well
as before.

For new readers, the patch

 - does away with DNS lookups; handy now that apache doesn't do dns
   lookups for logging any more.
 - adds CookieName to set the cookie name to something other than
   "Apache".
 - adds CookieDomain so one can do domain-wide usertracking.

The appended patch is relative to revision 1.34 in the anoncvs
repository.

--Arnt

Index: mod_usertrack.c
===================================================================
RCS file: /cvs/apache-1.3/src/modules/standard/mod_usertrack.c,v
retrieving revision 1.34
diff -u -b -u -b -r1.34 mod_usertrack.c
--- mod_usertrack.c	1998/04/11 12:00:53	1.34
+++ mod_usertrack.c	1998/04/12 16:14:09
@@ -111,6 +111,8 @@
 typedef struct {
     int always;
     time_t expires;
+    const char * name;
+    const char * domain;
 }      cookie_log_state;
 
 /* Define this to allow post-2000 cookies. Cookies use two-digit dates,
@@ -118,11 +120,14 @@
  */
 #define MILLENIAL_COOKIES
 
+/* Default cookie name.  The CookieName directive allows overrriding
+ * this.  Note that the trailing "=" is now gone. */
+
+#define COOKIE_NAME "Apache"
+
 /* Make Cookie: Now we have to generate something that is going to be
  * pretty unique.  We can base it on the pid, time, hostip */
 
-#define COOKIE_NAME "Apache="
-
 static void make_cookie(request_rec *r)
 {
     cookie_log_state *cls = ap_get_module_config(r->server->module_config,
@@ -151,7 +156,7 @@
 
     mpe_times = times(&mpe_tms);
 
-    ap_snprintf(cookiebuf, sizeof(cookiebuf), "%s%d%ld%ld", rname, (int) getpid(),
+    ap_snprintf(cookiebuf, sizeof(cookiebuf), "%d-%ld-%d", (int) getpid(),
                 (long) r->request_time, (long) mpe_tms.tms_utime);
 #elif defined(WIN32)
     /*
@@ -160,13 +165,13 @@
      * was started. It should be relatively unique.
      */
 
-    ap_snprintf(cookiebuf, sizeof(cookiebuf), "%s%d%ld%ld", rname, (int) getpid(),
+    ap_snprintf(cookiebuf, sizeof(cookiebuf), "%d-%ld-%d", (int) getpid(),
                 (long) r->request_time, (long) GetTickCount());
 
 #else
     gettimeofday(&tv, &tz);
 
-    ap_snprintf(cookiebuf, sizeof(cookiebuf), "%s%d%ld%d", rname, (int) getpid(),
+    ap_snprintf(cookiebuf, sizeof(cookiebuf), "%d-%ld-%d", (int) getpid(),
                 (long) tv.tv_sec, (int) tv.tv_usec / 1000);
 #endif
 
@@ -188,14 +193,17 @@
 
         /* Cookie with date; as strftime '%a, %d-%h-%y %H:%M:%S GMT' */
         new_cookie = ap_psprintf(r->pool,
-                "%s%s; path=/; expires=%s, %.2d-%s-%.2d %.2d:%.2d:%.2d GMT",
-                    COOKIE_NAME, cookiebuf, ap_day_snames[tms->tm_wday],
+                "%s=%s; path=/%s; expires=%s, %.2d-%s-%.2d %.2d:%.2d:%.2d GMT",
+		cls->name ? cls->name : COOKIE_NAME, cookiebuf,
+		cls->domain ? cls->domain : "", ap_day_snames[tms->tm_wday],
                     tms->tm_mday, ap_month_snames[tms->tm_mon],
 		    tms->tm_year % 100,
                     tms->tm_hour, tms->tm_min, tms->tm_sec);
+    } else {
+	new_cookie = ap_psprintf(r->pool, "%s=%s; path=/%s",
+		cls->name ? cls->name : COOKIE_NAME, cookiebuf,
+		cls->domain ? cls->domain : COOKIE_NAME);
     }
-    else
-	new_cookie = ap_psprintf(r->pool, "%s%s; path=/", COOKIE_NAME, cookiebuf);
 
     ap_table_setn(r->headers_out, "Set-Cookie", new_cookie);
     ap_table_setn(r->notes, "cookie", ap_pstrdup(r->pool, cookiebuf));   /* log first
time */
@@ -206,6 +214,8 @@
 {
     int *enable = (int *) ap_get_module_config(r->per_dir_config,
                                             &usertrack_module);
+    cookie_log_state *cls = ap_get_module_config(r->server->module_config,
+						 &usertrack_module);
     char *cookie;
     char *value;
 
@@ -213,10 +223,10 @@
         return DECLINED;
 
     if ((cookie = ap_table_get(r->headers_in, "Cookie")))
-        if ((value = strstr(cookie, COOKIE_NAME))) {
+        if ((value = strstr(cookie, cls->name ? cls->name : COOKIE_NAME))) {
             char *cookiebuf, *cookieend;
 
-            value += strlen(COOKIE_NAME);
+            value += strlen(cls->name ? cls->name : COOKIE_NAME);
             cookiebuf = ap_pstrdup(r->pool, value);
             cookieend = strchr(cookiebuf, ';');
             if (cookieend)
@@ -248,7 +258,7 @@
 
 static const char *set_cookie_enable(cmd_parms *cmd, int *c, int arg)
 {
-    *c = arg;
+    *c = arg; /* referred to as cls->always in other places */
     return NULL;
 }
 
@@ -319,9 +329,86 @@
     return NULL;
 }
 
+
+/* Add domain cookie support to Apache.
+ *
+ * By settting "CookieDomain=your.domain.int" you can instruct the
+ * browsers to send this cookie to all servers in the "your.domain.int"
+ * domain.  Note that Apache prepends the necessary "." itself.
+ *
+ * Added by Arnt Gulbrandsen <agulbra@troll.no>, March 1998.  Comments
+ * are welcome; this is my first apache hack and without doubt could
+ * be improved on easily.
+ */
+
+static const char *set_cookie_domain(cmd_parms *parms, void *dummy,
+				     const char *arg)
+{
+    int l;
+    const char * p;
+    int chars;
+    cookie_log_state *cls = ap_get_module_config(parms->server->module_config,
+						 &usertrack_module);
+
+    /* check that arg is a valid-looking domain name */
+
+    p = arg; /* current character */
+    chars = 0; /* characters seen since last dot */
+    while( *p ) {
+	if ( *p == '.' ) {
+	    if ( chars == 0 )
+		return "bad domain name; contains consecutive '.' characters";
+	    else
+		chars = 0;
+	} else if ( isalnum(*p) || *p == '/' || *p == '-' ) {
+	    chars++;
+	} else {
+	    return "bad domain name; contains illegal characters";
+	}
+	p++;
+    }
+
+    if ( chars < 2 || chars > 3 ) {
+	return "bad domain name; top-level domain must contain 2-3 characters";
+    }
+
+    /* at this point we know that arg looks kind of reasonable .
+       so we use it. */
+
+    cls->domain = ap_psprintf( parms->pool, "; domain=.%s", arg );
+
+    return NULL;
+}
+
+
+static const char *set_cookie_name(cmd_parms *parms, void *dummy,
+				   const char *arg)
+{
+    const char * p;
+    cookie_log_state *cls = ap_get_module_config(parms->server->module_config,
+						 &usertrack_module);
+
+    for( p=arg; *p; p++ )
+	if ( *p <= ' ' || *p >= (char)127 ||
+	     *p == '(' || *p == ')' || *p == '<' || *p == '>' ||
+	     *p == '@' || *p == ',' || *p == ';' || *p == ':' ||
+	     *p == '\\' || *p == '"' || *p == '/' || *p == '[' ||
+	     *p == ']' || *p == '?' || *p == '=' || *p == '{' )
+	    return "bad cookie name; contains illegal character";
+
+    cls->name = ap_pstrdup( parms->pool, arg );
+
+    return NULL;
+}
+
+
 static const command_rec cookie_log_cmds[] = {
     {"CookieExpires", set_cookie_exp, NULL, RSRC_CONF, TAKE1,
     "an expiry date code"},
+    {"CookieDomain", set_cookie_domain, NULL, RSRC_CONF, TAKE1,
+    "what domain the cookie applies to"},
+    {"CookieName", set_cookie_name, NULL, RSRC_CONF, TAKE1,
+    "the name of Apache's built-in user-tracking cookie"},
     {"CookieTracking", set_cookie_enable, NULL, OR_FILEINFO, FLAG,
     "whether or not to enable cookies"},
     {NULL}

Mime
View raw message