Blame server/util_time.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "util_time.h"
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/* Number of characters needed to format the microsecond part of a timestamp.
Packit 90a5c9
 * Microseconds have 6 digits plus one separator character makes 7.
Packit 90a5c9
 *   */
Packit 90a5c9
#define AP_CTIME_USEC_LENGTH      7
Packit 90a5c9
Packit 90a5c9
/* Length of ISO 8601 date/time */
Packit 90a5c9
#define AP_CTIME_COMPACT_LEN      20
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/* Cache for exploded values of recent timestamps
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
struct exploded_time_cache_element {
Packit 90a5c9
    apr_int64_t t;
Packit 90a5c9
    apr_time_exp_t xt;
Packit 90a5c9
    apr_int64_t t_validate; /* please see comments in cached_explode() */
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
/* the "+ 1" is for the current second: */
Packit 90a5c9
#define TIME_CACHE_SIZE (AP_TIME_RECENT_THRESHOLD + 1)
Packit 90a5c9
Packit 90a5c9
/* Note that AP_TIME_RECENT_THRESHOLD is defined to
Packit 90a5c9
 * be a power of two minus one in util_time.h, so that
Packit 90a5c9
 * we can replace a modulo operation with a bitwise AND
Packit 90a5c9
 * when hashing items into a cache of size
Packit 90a5c9
 * AP_TIME_RECENT_THRESHOLD+1
Packit 90a5c9
 */
Packit 90a5c9
#define TIME_CACHE_MASK (AP_TIME_RECENT_THRESHOLD)
Packit 90a5c9
Packit 90a5c9
static struct exploded_time_cache_element exploded_cache_localtime[TIME_CACHE_SIZE];
Packit 90a5c9
static struct exploded_time_cache_element exploded_cache_gmt[TIME_CACHE_SIZE];
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static apr_status_t cached_explode(apr_time_exp_t *xt, apr_time_t t,
Packit 90a5c9
                                   struct exploded_time_cache_element *cache,
Packit 90a5c9
                                   int use_gmt)
Packit 90a5c9
{
Packit 90a5c9
    apr_int64_t seconds = apr_time_sec(t);
Packit 90a5c9
    struct exploded_time_cache_element *cache_element =
Packit 90a5c9
        &(cache[seconds & TIME_CACHE_MASK]);
Packit 90a5c9
    struct exploded_time_cache_element cache_element_snapshot;
Packit 90a5c9
Packit 90a5c9
    /* The cache is implemented as a ring buffer.  Each second,
Packit 90a5c9
     * it uses a different element in the buffer.  The timestamp
Packit 90a5c9
     * in the element indicates whether the element contains the
Packit 90a5c9
     * exploded time for the current second (vs the time
Packit 90a5c9
     * 'now - AP_TIME_RECENT_THRESHOLD' seconds ago).  If the
Packit 90a5c9
     * cached value is for the current time, we use it.  Otherwise,
Packit 90a5c9
     * we compute the apr_time_exp_t and store it in this
Packit 90a5c9
     * cache element. Note that the timestamp in the cache
Packit 90a5c9
     * element is updated only after the exploded time.  Thus
Packit 90a5c9
     * if two threads hit this cache element simultaneously
Packit 90a5c9
     * at the start of a new second, they'll both explode the
Packit 90a5c9
     * time and store it.  I.e., the writers will collide, but
Packit 90a5c9
     * they'll be writing the same value.
Packit 90a5c9
     */
Packit 90a5c9
    if (cache_element->t >= seconds) {
Packit 90a5c9
        /* There is an intentional race condition in this design:
Packit 90a5c9
         * in a multithreaded app, one thread might be reading
Packit 90a5c9
         * from this cache_element to resolve a timestamp from
Packit 90a5c9
         * TIME_CACHE_SIZE seconds ago at the same time that
Packit 90a5c9
         * another thread is copying the exploded form of the
Packit 90a5c9
         * current time into the same cache_element.  (I.e., the
Packit 90a5c9
         * first thread might hit this element of the ring buffer
Packit 90a5c9
         * just as the element is being recycled.)  This can
Packit 90a5c9
         * also happen at the start of a new second, if a
Packit 90a5c9
         * reader accesses the cache_element after a writer
Packit 90a5c9
         * has updated cache_element.t but before the writer
Packit 90a5c9
         * has finished updating the whole cache_element.
Packit 90a5c9
         *
Packit 90a5c9
         * Rather than trying to prevent this race condition
Packit 90a5c9
         * with locks, we allow it to happen and then detect
Packit 90a5c9
         * and correct it.  The detection works like this:
Packit 90a5c9
         *   Step 1: Take a "snapshot" of the cache element by
Packit 90a5c9
         *           copying it into a temporary buffer.
Packit 90a5c9
         *   Step 2: Check whether the snapshot contains consistent
Packit 90a5c9
         *           data: the timestamps at the start and end of
Packit 90a5c9
         *           the cache_element should both match the 'seconds'
Packit 90a5c9
         *           value that we computed from the input time.
Packit 90a5c9
         *           If these three don't match, then the snapshot
Packit 90a5c9
         *           shows the cache_element in the middle of an
Packit 90a5c9
         *           update, and its contents are invalid.
Packit 90a5c9
         *   Step 3: If the snapshot is valid, use it.  Otherwise,
Packit 90a5c9
         *           just give up on the cache and explode the
Packit 90a5c9
         *           input time.
Packit 90a5c9
         */
Packit 90a5c9
        memcpy(&cache_element_snapshot, cache_element,
Packit 90a5c9
               sizeof(struct exploded_time_cache_element));
Packit 90a5c9
        if ((seconds != cache_element_snapshot.t) ||
Packit 90a5c9
            (seconds != cache_element_snapshot.t_validate)) {
Packit 90a5c9
            /* Invalid snapshot */
Packit 90a5c9
            if (use_gmt) {
Packit 90a5c9
                return apr_time_exp_gmt(xt, t);
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                return apr_time_exp_lt(xt, t);
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            /* Valid snapshot */
Packit 90a5c9
            memcpy(xt, &(cache_element_snapshot.xt),
Packit 90a5c9
                   sizeof(apr_time_exp_t));
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        apr_status_t r;
Packit 90a5c9
        if (use_gmt) {
Packit 90a5c9
            r = apr_time_exp_gmt(xt, t);
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            r = apr_time_exp_lt(xt, t);
Packit 90a5c9
        }
Packit 90a5c9
        if (r != APR_SUCCESS) {
Packit 90a5c9
            return r;
Packit 90a5c9
        }
Packit 90a5c9
        cache_element->t = seconds;
Packit 90a5c9
        memcpy(&(cache_element->xt), xt, sizeof(apr_time_exp_t));
Packit 90a5c9
        cache_element->t_validate = seconds;
Packit 90a5c9
    }
Packit 90a5c9
    xt->tm_usec = (int)apr_time_usec(t);
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_explode_recent_localtime(apr_time_exp_t * tm,
Packit 90a5c9
                                                     apr_time_t t)
Packit 90a5c9
{
Packit 90a5c9
    return cached_explode(tm, t, exploded_cache_localtime, 0);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_explode_recent_gmt(apr_time_exp_t * tm,
Packit 90a5c9
                                               apr_time_t t)
Packit 90a5c9
{
Packit 90a5c9
    return cached_explode(tm, t, exploded_cache_gmt, 1);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_recent_ctime(char *date_str, apr_time_t t)
Packit 90a5c9
{
Packit 90a5c9
    int len = APR_CTIME_LEN;
Packit 90a5c9
    return ap_recent_ctime_ex(date_str, t, AP_CTIME_OPTION_NONE, &len;;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_recent_ctime_ex(char *date_str, apr_time_t t,
Packit 90a5c9
                                            int option, int *len)
Packit 90a5c9
{
Packit 90a5c9
    /* ### This code is a clone of apr_ctime(), except that it
Packit 90a5c9
     * uses ap_explode_recent_localtime() instead of apr_time_exp_lt().
Packit 90a5c9
     */
Packit 90a5c9
    apr_time_exp_t xt;
Packit 90a5c9
    const char *s;
Packit 90a5c9
    int real_year;
Packit 90a5c9
    int needed;
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
    /* Calculate the needed buffer length */
Packit 90a5c9
    if (option & AP_CTIME_OPTION_COMPACT)
Packit 90a5c9
        needed = AP_CTIME_COMPACT_LEN;
Packit 90a5c9
    else
Packit 90a5c9
        needed = APR_CTIME_LEN;
Packit 90a5c9
Packit 90a5c9
    if (option & AP_CTIME_OPTION_USEC) {
Packit 90a5c9
        needed += AP_CTIME_USEC_LENGTH;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Check the provided buffer length */
Packit 90a5c9
    if (len && *len >= needed) {
Packit 90a5c9
        *len = needed;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        if (len != NULL) {
Packit 90a5c9
            *len = 0;
Packit 90a5c9
        }
Packit 90a5c9
        return APR_ENOMEM;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* example without options: "Wed Jun 30 21:49:08 1993" */
Packit 90a5c9
    /*                           123456789012345678901234  */
Packit 90a5c9
    /* example for compact format: "1993-06-30 21:49:08" */
Packit 90a5c9
    /*                              1234567890123456789  */
Packit 90a5c9
Packit 90a5c9
    ap_explode_recent_localtime(&xt, t);
Packit 90a5c9
    real_year = 1900 + xt.tm_year;
Packit 90a5c9
    if (option & AP_CTIME_OPTION_COMPACT) {
Packit 90a5c9
        int real_month = xt.tm_mon + 1;
Packit 90a5c9
        *date_str++ = real_year / 1000 + '0';
Packit 90a5c9
        *date_str++ = real_year % 1000 / 100 + '0';
Packit 90a5c9
        *date_str++ = real_year % 100 / 10 + '0';
Packit 90a5c9
        *date_str++ = real_year % 10 + '0';
Packit 90a5c9
        *date_str++ = '-';
Packit 90a5c9
        *date_str++ = real_month / 10 + '0';
Packit 90a5c9
        *date_str++ = real_month % 10 + '0';
Packit 90a5c9
        *date_str++ = '-';
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        s = &apr_day_snames[xt.tm_wday][0];
Packit 90a5c9
        *date_str++ = *s++;
Packit 90a5c9
        *date_str++ = *s++;
Packit 90a5c9
        *date_str++ = *s++;
Packit 90a5c9
        *date_str++ = ' ';
Packit 90a5c9
        s = &apr_month_snames[xt.tm_mon][0];
Packit 90a5c9
        *date_str++ = *s++;
Packit 90a5c9
        *date_str++ = *s++;
Packit 90a5c9
        *date_str++ = *s++;
Packit 90a5c9
        *date_str++ = ' ';
Packit 90a5c9
    }
Packit 90a5c9
    *date_str++ = xt.tm_mday / 10 + '0';
Packit 90a5c9
    *date_str++ = xt.tm_mday % 10 + '0';
Packit 90a5c9
    *date_str++ = ' ';
Packit 90a5c9
    *date_str++ = xt.tm_hour / 10 + '0';
Packit 90a5c9
    *date_str++ = xt.tm_hour % 10 + '0';
Packit 90a5c9
    *date_str++ = ':';
Packit 90a5c9
    *date_str++ = xt.tm_min / 10 + '0';
Packit 90a5c9
    *date_str++ = xt.tm_min % 10 + '0';
Packit 90a5c9
    *date_str++ = ':';
Packit 90a5c9
    *date_str++ = xt.tm_sec / 10 + '0';
Packit 90a5c9
    *date_str++ = xt.tm_sec % 10 + '0';
Packit 90a5c9
    if (option & AP_CTIME_OPTION_USEC) {
Packit 90a5c9
        int div;
Packit 90a5c9
        int usec = (int)xt.tm_usec;
Packit 90a5c9
        *date_str++ = '.';
Packit 90a5c9
        for (div=100000; div>0; div=div/10) {
Packit 90a5c9
            *date_str++ = usec / div + '0';
Packit 90a5c9
            usec = usec % div;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    if (!(option & AP_CTIME_OPTION_COMPACT)) {
Packit 90a5c9
        *date_str++ = ' ';
Packit 90a5c9
        *date_str++ = real_year / 1000 + '0';
Packit 90a5c9
        *date_str++ = real_year % 1000 / 100 + '0';
Packit 90a5c9
        *date_str++ = real_year % 100 / 10 + '0';
Packit 90a5c9
        *date_str++ = real_year % 10 + '0';
Packit 90a5c9
    }
Packit 90a5c9
    *date_str++ = 0;
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_recent_rfc822_date(char *date_str, apr_time_t t)
Packit 90a5c9
{
Packit 90a5c9
    /* ### This code is a clone of apr_rfc822_date(), except that it
Packit 90a5c9
     * uses ap_explode_recent_gmt() instead of apr_time_exp_gmt().
Packit 90a5c9
     */
Packit 90a5c9
    apr_time_exp_t xt;
Packit 90a5c9
    const char *s;
Packit 90a5c9
    int real_year;
Packit 90a5c9
Packit 90a5c9
    ap_explode_recent_gmt(&xt, t);
Packit 90a5c9
Packit 90a5c9
    /* example: "Sat, 08 Jan 2000 18:31:41 GMT" */
Packit 90a5c9
    /*           12345678901234567890123456789  */
Packit 90a5c9
Packit 90a5c9
    s = &apr_day_snames[xt.tm_wday][0];
Packit 90a5c9
    *date_str++ = *s++;
Packit 90a5c9
    *date_str++ = *s++;
Packit 90a5c9
    *date_str++ = *s++;
Packit 90a5c9
    *date_str++ = ',';
Packit 90a5c9
    *date_str++ = ' ';
Packit 90a5c9
    *date_str++ = xt.tm_mday / 10 + '0';
Packit 90a5c9
    *date_str++ = xt.tm_mday % 10 + '0';
Packit 90a5c9
    *date_str++ = ' ';
Packit 90a5c9
    s = &apr_month_snames[xt.tm_mon][0];
Packit 90a5c9
    *date_str++ = *s++;
Packit 90a5c9
    *date_str++ = *s++;
Packit 90a5c9
    *date_str++ = *s++;
Packit 90a5c9
    *date_str++ = ' ';
Packit 90a5c9
    real_year = 1900 + xt.tm_year;
Packit 90a5c9
    /* This routine isn't y10k ready. */
Packit 90a5c9
    *date_str++ = real_year / 1000 + '0';
Packit 90a5c9
    *date_str++ = real_year % 1000 / 100 + '0';
Packit 90a5c9
    *date_str++ = real_year % 100 / 10 + '0';
Packit 90a5c9
    *date_str++ = real_year % 10 + '0';
Packit 90a5c9
    *date_str++ = ' ';
Packit 90a5c9
    *date_str++ = xt.tm_hour / 10 + '0';
Packit 90a5c9
    *date_str++ = xt.tm_hour % 10 + '0';
Packit 90a5c9
    *date_str++ = ':';
Packit 90a5c9
    *date_str++ = xt.tm_min / 10 + '0';
Packit 90a5c9
    *date_str++ = xt.tm_min % 10 + '0';
Packit 90a5c9
    *date_str++ = ':';
Packit 90a5c9
    *date_str++ = xt.tm_sec / 10 + '0';
Packit 90a5c9
    *date_str++ = xt.tm_sec % 10 + '0';
Packit 90a5c9
    *date_str++ = ' ';
Packit 90a5c9
    *date_str++ = 'G';
Packit 90a5c9
    *date_str++ = 'M';
Packit 90a5c9
    *date_str++ = 'T';
Packit 90a5c9
    *date_str++ = 0;
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}