From: Jim Jagielski Date: Wed, 18 Dec 2002 22:43:08 +0000 (+0000) Subject: Clean up the compact/verbose cookie code to prevent a lot of X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bcac503a2e32fa3717c94bf70f5602bb150bcd8d;p=thirdparty%2Fapache%2Fhttpd.git Clean up the compact/verbose cookie code to prevent a lot of overlap. Who knows, maybe a 3rd format might pop up one day. If this breaks, note who to blame for it PR: Obtained from: Submitted by: Reviewed by: git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x@98034 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/CHANGES b/src/CHANGES index ea1dffd9af1..0176c1ca1f6 100644 --- a/src/CHANGES +++ b/src/CHANGES @@ -15,7 +15,7 @@ Changes with Apache 1.3.28 as well as "compact" version of the tracking cookie (the new 'CookieFormat' directive), and the ability to prepend a string to the cookie via the 'CookiePrefix' directive. - [Pål Løberg ] + [Pål Løberg , with cleanup by Jim Jagielski] *) Certain 3rd party modules would bypass the Apache API and not invoke ap_cleanup_for_exec() before creating sub-processes. diff --git a/src/modules/standard/mod_usertrack.c b/src/modules/standard/mod_usertrack.c index 1368247ce51..eb7ec002dcf 100644 --- a/src/modules/standard/mod_usertrack.c +++ b/src/modules/standard/mod_usertrack.c @@ -138,12 +138,12 @@ typedef struct { #define COOKIE_NAME "Apache" -/* Make normal cookie: Try to make something unique based on +/* Make cookie id: Try to make something unique based on * pid, time, and hostid, plus the user-configurable prefix. * - * This function will make a "verbose" version of the cookie. */ -static char * make_normal_id(char * buffer, int bufsize, request_rec *r) +static char * make_cookie_id(char * buffer, int bufsize, request_rec *r, + cookie_format_e cformat) { #if defined(NO_GETTIMEOFDAY) && !defined(NO_TIMES) clock_t mpe_times; @@ -156,70 +156,15 @@ static char * make_normal_id(char * buffer, int bufsize, request_rec *r) struct timezone tz = {0, 0}; #endif /* defined(NETWARE) */ #endif - const char *rname = ap_get_remote_host(r->connection, r->per_dir_config, - REMOTE_NAME); - cookie_dir_rec *dcfg; - - dcfg = ap_get_module_config(r->per_dir_config, &usertrack_module); - -#if defined(NO_GETTIMEOFDAY) && !defined(NO_TIMES) -/* We lack gettimeofday(), so we must use time() to obtain the epoch - seconds, and then times() to obtain CPU clock ticks (milliseconds). - Combine this together to obtain a hopefully unique cookie ID. */ - - mpe_times = times(&mpe_tms); - - ap_snprintf(buffer, bufsize, "%s%s.%d%ld%ld", - dcfg->prefix_string, rname, (int) getpid(), - (long) r->request_time, (long) mpe_tms.tms_utime); -#elif defined(NETWARE) - ap_snprintf(buffer, bufsize, "%s%s.%d%ld%ld", - dcfg->prefix_string, rname, (int) getpid(), - (long) r->request_time, (long) clock()); -#elif defined(WIN32) - /* - * We lack gettimeofday() and we lack times(). So we'll use a combination - * of time() and GetTickCount(), which returns milliseconds since Windows - * was started. It should be relatively unique. - */ - - ap_snprintf(buffer, bufsize, "%s%s.%d%ld%ld", - dcfg->prefix_string, rname, (int) getpid(), - (long) r->request_time, (long) GetTickCount()); -#else - gettimeofday(&tv, &tz); - - ap_snprintf(buffer, bufsize, "%s%s.%d%ld%d", - dcfg->prefix_string, rname, (int) getpid(), - (long) tv.tv_sec, (int) tv.tv_usec / 1000); -#endif - - return buffer; -} + cookie_dir_rec *dcfg; + long reqtime = (long) r->request_time; + long clocktime; -/* Make normal cookie: Try to make something unique based on - * pid, time, and hostid, plus the user-configurable prefix. - * - * This function will make a "compact" version of the cookie. - */ -static char * make_compact_id(char * buffer, int bufsize, request_rec *r) -{ -#if defined(NO_GETTIMEOFDAY) && !defined(NO_TIMES) - clock_t mpe_times; - struct tms mpe_tms; -#elif !defined(WIN32) - struct timeval tv; -#ifdef NETWARE - time_t tz = 0; -#else - struct timezone tz = {0, 0}; -#endif /* defined(NETWARE) */ -#endif unsigned long ipaddr = ntohl(r->connection->remote_addr.sin_addr.s_addr); - cookie_dir_rec *dcfg; - + const char *rname = ap_get_remote_host(r->connection, r->per_dir_config, + REMOTE_NAME); dcfg = ap_get_module_config(r->per_dir_config, &usertrack_module); #if defined(NO_GETTIMEOFDAY) && !defined(NO_TIMES) @@ -228,37 +173,44 @@ static char * make_compact_id(char * buffer, int bufsize, request_rec *r) Combine this together to obtain a hopefully unique cookie ID. */ mpe_times = times(&mpe_tms); - - ap_snprintf(buffer, bufsize, "%s%lx%x%lx%lx", - dcfg->prefix_string, ipaddr, (int) getpid(), - (long) r->request_time, (long) mpe_tms.tms_utime); + clocktime = (long) mpe_tms.tms_utime; + #elif defined(NETWARE) - ap_snprintf(buffer, bufsize, "%s%lx%x%lx%lx", - dcfg->prefix_string, ipaddr, (int) getpid(), - (long) r->request_time, (long) clock()); + clocktime = (long) clock(); + #elif defined(WIN32) /* - * We lack gettimeofday() and we lack times(). So we'll use a combination - * of time() and GetTickCount(), which returns milliseconds since Windows + * We lack gettimeofday() and we lack times(). So we'll use + * GetTickCount(), which returns milliseconds since Windows * was started. It should be relatively unique. */ - ap_snprintf(buffer, bufsize, "%s%lx%x%lx%lx", - dcfg->prefix_string, ipaddr, (int) getpid(), - (long) r->request_time, (long) GetTickCount()); + clocktime = (long) GetTickCount()); #else gettimeofday(&tv, &tz); - ap_snprintf(buffer, bufsize, "%s%lx%x%lx%x", - dcfg->prefix_string, ipaddr, (int) getpid(), - (long) tv.tv_sec, (int)(tv.tv_usec % 65535)); + reqtime = (long) tv.tv_sec; + if (cformat == CF_COMPACT) + clocktime = (long) (tv.tv_usec % 65535); + else + clocktime = (long) (tv.tv_usec / 1000); #endif + if (cformat == CF_COMPACT) + ap_snprintf(buffer, bufsize, "%s%lx%x%lx%lx", + dcfg->prefix_string, ipaddr, (int) getpid(), + reqtime, clocktime); + else + ap_snprintf(buffer, bufsize, "%s%s.%d%ld%ld", + dcfg->prefix_string, rname, (int) getpid(), + reqtime, clocktime); + return buffer; } + static void make_cookie(request_rec *r) { cookie_log_state *cls = ap_get_module_config(r->server->module_config, @@ -271,11 +223,7 @@ static void make_cookie(request_rec *r) dcfg = ap_get_module_config(r->per_dir_config, &usertrack_module); - if (dcfg->format == CF_COMPACT) { - make_compact_id(cookiebuf, sizeof(cookiebuf), r); - } else { - make_normal_id(cookiebuf, sizeof(cookiebuf), r); - } + make_cookie_id(cookiebuf, sizeof(cookiebuf), r, dcfg->format); if (cls->expires) { struct tm *tms;