1 /* $Id: functions20.h 171 2006-11-07 02:44:33Z urkle@drip.ws $ */
2 
extract_bytes_sent(request_rec * r,char * a)3 static const char *extract_bytes_sent(request_rec *r, char *a)
4 {
5 	if (!r->sent_bodyct || !r->bytes_sent) {
6 		return "-";
7 	} else {
8 		return apr_psprintf(r->pool, "%" APR_OFF_T_FMT, r->bytes_sent);
9 	}
10 }
11 
extract_request_time_custom(request_rec * r,char * a,apr_time_exp_t * xt)12 static const char *extract_request_time_custom(request_rec *r, char *a,
13                                            apr_time_exp_t *xt)
14 {
15     apr_size_t retcode;
16     char tstr[MAX_STRING_LEN];
17     apr_strftime(tstr, &retcode, sizeof(tstr), a, xt);
18     return apr_pstrdup(r->pool, tstr);
19 }
20 
21 #define DEFAULT_REQUEST_TIME_SIZE 32
22 typedef struct {
23     unsigned t;
24     char timestr[DEFAULT_REQUEST_TIME_SIZE];
25     unsigned t_validate;
26 } cached_request_time;
27 
28 #define TIME_CACHE_SIZE 4
29 #define TIME_CACHE_MASK 3
30 static cached_request_time request_time_cache[TIME_CACHE_SIZE];
31 
extract_request_time(request_rec * r,char * a)32 static const char *extract_request_time(request_rec *r, char *a)
33 {
34 	apr_time_exp_t xt;
35 
36 	/* Please read comments in mod_log_config.h for more info about
37 	 * the I_INSIST....COMPLIANCE define
38 	 */
39 	if (a && *a) {     /* Custom format */
40 #ifdef I_INSIST_ON_EXTRA_CYCLES_FOR_CLF_COMPLIANCE
41         ap_explode_recent_localtime(&xt, apr_time_now());
42 #else
43         ap_explode_recent_localtime(&xt, r->request_time);
44 #endif
45         return extract_request_time_custom(r, a, &xt);
46 	} else {		   /* CLF format */
47         /* This code uses the same technique as ap_explode_recent_localtime():
48          * optimistic caching with logic to detect and correct race conditions.
49          * See the comments in server/util_time.c for more information.
50          */
51         cached_request_time* cached_time = apr_palloc(r->pool,
52                                                       sizeof(*cached_time));
53 #ifdef I_INSIST_ON_EXTRA_CYCLES_FOR_CLF_COMPLIANCE
54         apr_time_t request_time = apr_time_now();
55 #else
56         apr_time_t request_time = r->request_time;
57 #endif
58         unsigned t_seconds = (unsigned)apr_time_sec(request_time);
59         unsigned i = t_seconds & TIME_CACHE_MASK;
60         memcpy(cached_time, &(request_time_cache[i]), sizeof(*cached_time));
61         if ((t_seconds != cached_time->t) ||
62             (t_seconds != cached_time->t_validate)) {
63 
64             /* Invalid or old snapshot, so compute the proper time string
65              * and store it in the cache
66              */
67             char sign;
68             int timz;
69 
70             ap_explode_recent_localtime(&xt, r->request_time);
71             timz = xt.tm_gmtoff;
72             if (timz < 0) {
73                 timz = -timz;
74                 sign = '-';
75             }
76             else {
77                 sign = '+';
78             }
79             cached_time->t = t_seconds;
80             apr_snprintf(cached_time->timestr, DEFAULT_REQUEST_TIME_SIZE,
81                          "[%02d/%s/%d:%02d:%02d:%02d %c%.2d%.2d]",
82                          xt.tm_mday, apr_month_snames[xt.tm_mon],
83                          xt.tm_year+1900, xt.tm_hour, xt.tm_min, xt.tm_sec,
84                          sign, timz / (60*60), timz % (60*60));
85             cached_time->t_validate = t_seconds;
86             memcpy(&(request_time_cache[i]), cached_time,
87                    sizeof(*cached_time));
88 		}
89 		return cached_time->timestr;
90 	}
91 }
92 
extract_request_duration(request_rec * r,char * a)93 static const char *extract_request_duration(request_rec *r, char *a)
94 {
95 	apr_time_t duration = apr_time_now() - r->request_time;
96 	return apr_psprintf(r->pool, "%" APR_TIME_T_FMT, apr_time_sec(duration));
97 }
98 
extract_request_timestamp(request_rec * r,char * a)99 static const char *extract_request_timestamp(request_rec *r, char *a)
100 {
101 	return apr_psprintf(r->pool, "%"APR_TIME_T_FMT, apr_time_sec(apr_time_now()));
102 }
103 
104 static const char *extract_connection_status(request_rec *r, char *a) __attribute__((unused));
extract_connection_status(request_rec * r,char * a)105 static const char *extract_connection_status(request_rec *r, char *a)
106 {
107     if (r->connection->aborted)
108         return "X";
109 
110     if (r->connection->keepalive == AP_CONN_KEEPALIVE &&
111         (!r->server->keep_alive_max ||
112          (r->server->keep_alive_max - r->connection->keepalives) > 0)) {
113         return "+";
114     }
115     return "-";
116 }
117