You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2019/07/30 13:08:48 UTC

[tomcat] 01/06: Prep for additional backports. Align with 8.5.x.

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 7.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit f852fb13e430be3862676b8b7cc691a59da4070e
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Mon Jul 29 20:18:01 2019 +0100

    Prep for additional backports. Align with 8.5.x.
---
 .../tomcat/util/http/ConcurrentDateFormat.java     |  72 +++++++++++
 .../tomcat/util/http/FastHttpDateFormat.java       | 143 +++++++++------------
 2 files changed, 136 insertions(+), 79 deletions(-)

diff --git a/java/org/apache/tomcat/util/http/ConcurrentDateFormat.java b/java/org/apache/tomcat/util/http/ConcurrentDateFormat.java
new file mode 100644
index 0000000..613b1ba
--- /dev/null
+++ b/java/org/apache/tomcat/util/http/ConcurrentDateFormat.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomcat.util.http;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+import java.util.Queue;
+import java.util.TimeZone;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+/**
+ * A thread safe wrapper around {@link SimpleDateFormat} that does not make use
+ * of ThreadLocal and - broadly - only creates enough SimpleDateFormat objects
+ * to satisfy the concurrency requirements.
+ */
+public class ConcurrentDateFormat {
+
+    private final String format;
+    private final Locale locale;
+    private final TimeZone timezone;
+    private final Queue<SimpleDateFormat> queue = new ConcurrentLinkedQueue<SimpleDateFormat>();
+
+    public ConcurrentDateFormat(String format, Locale locale, TimeZone timezone) {
+        this.format = format;
+        this.locale = locale;
+        this.timezone = timezone;
+        SimpleDateFormat initial = createInstance();
+        queue.add(initial);
+    }
+
+    public String format(Date date) {
+        SimpleDateFormat sdf = queue.poll();
+        if (sdf == null) {
+            sdf = createInstance();
+        }
+        String result = sdf.format(date);
+        queue.add(sdf);
+        return result;
+    }
+
+    public Date parse(String source) throws ParseException {
+        SimpleDateFormat sdf = queue.poll();
+        if (sdf == null) {
+            sdf = createInstance();
+        }
+        Date result = sdf.parse(source);
+        queue.add(sdf);
+        return result;
+    }
+
+    private SimpleDateFormat createInstance() {
+        SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
+        sdf.setTimeZone(timezone);
+        return sdf;
+    }
+}
diff --git a/java/org/apache/tomcat/util/http/FastHttpDateFormat.java b/java/org/apache/tomcat/util/http/FastHttpDateFormat.java
index d4af86d..4f787a2 100644
--- a/java/org/apache/tomcat/util/http/FastHttpDateFormat.java
+++ b/java/org/apache/tomcat/util/http/FastHttpDateFormat.java
@@ -18,7 +18,6 @@ package org.apache.tomcat.util.http;
 
 import java.text.DateFormat;
 import java.text.ParseException;
-import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Locale;
 import java.util.Map;
@@ -41,39 +40,33 @@ public final class FastHttpDateFormat {
 
 
     /**
-     * HTTP date format.
+     * The only date format permitted when generating HTTP headers.
      */
-    private static final SimpleDateFormat format =
-        new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
+    public static final String RFC1123_DATE = "EEE, dd MMM yyyy HH:mm:ss zzz";
 
+    // HTTP date formats
+    private static final String DATE_RFC5322 = "EEE, dd MMM yyyy HH:mm:ss z";
+    private static final String DATE_OBSOLETE_RFC850 = "EEEEEE, dd-MMM-yy HH:mm:ss zzz";
+    private static final String DATE_OBSOLETE_ASCTIME = "EEE MMMM d HH:mm:ss yyyy";
 
-    /**
-     * The set of SimpleDateFormat formats to use in getDateHeader().
-     */
-    private static final SimpleDateFormat formats[] = {
-        new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US),
-        new SimpleDateFormat("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US),
-        new SimpleDateFormat("EEE MMMM d HH:mm:ss yyyy", Locale.US)
-    };
-
+    private static final ConcurrentDateFormat FORMAT_RFC5322;
+    private static final ConcurrentDateFormat FORMAT_OBSOLETE_RFC850;
+    private static final ConcurrentDateFormat FORMAT_OBSOLETE_ASCTIME;
 
-    private static final TimeZone gmtZone = TimeZone.getTimeZone("GMT");
+    private static final ConcurrentDateFormat[] httpParseFormats;
 
-
-    /**
-     * GMT timezone - all HTTP dates are on GMT
-     */
     static {
+        // All the formats that use a timezone use GMT
+        TimeZone tz = TimeZone.getTimeZone("GMT");
 
-        format.setTimeZone(gmtZone);
-
-        formats[0].setTimeZone(gmtZone);
-        formats[1].setTimeZone(gmtZone);
-        formats[2].setTimeZone(gmtZone);
+        FORMAT_RFC5322 = new ConcurrentDateFormat(DATE_RFC5322, Locale.US, tz);
+        FORMAT_OBSOLETE_RFC850 = new ConcurrentDateFormat(DATE_OBSOLETE_RFC850, Locale.US, tz);
+        FORMAT_OBSOLETE_ASCTIME = new ConcurrentDateFormat(DATE_OBSOLETE_ASCTIME, Locale.US, tz);
 
+        httpParseFormats = new ConcurrentDateFormat[] {
+                FORMAT_RFC5322, FORMAT_OBSOLETE_RFC850, FORMAT_OBSOLETE_ASCTIME };
     }
 
-
     /**
      * Instant on which the currentDate object was generated.
      */
@@ -89,15 +82,13 @@ public final class FastHttpDateFormat {
     /**
      * Formatter cache.
      */
-    private static final Map<Long, String> formatCache =
-            new ConcurrentHashMap<Long, String>(CACHE_SIZE);
+    private static final Map<Long, String> formatCache = new ConcurrentHashMap<Long, String>(CACHE_SIZE);
 
 
     /**
      * Parser cache.
      */
-    private static final Map<String, Long> parseCache =
-            new ConcurrentHashMap<String, Long>(CACHE_SIZE);
+    private static final Map<String, Long> parseCache = new ConcurrentHashMap<String, Long>(CACHE_SIZE);
 
 
     // --------------------------------------------------------- Public Methods
@@ -105,94 +96,88 @@ public final class FastHttpDateFormat {
 
     /**
      * Get the current date in HTTP format.
+     * @return the HTTP date
      */
     public static final String getCurrentDate() {
-
         long now = System.currentTimeMillis();
         if ((now - currentDateGenerated) > 1000) {
-            synchronized (format) {
-                if ((now - currentDateGenerated) > 1000) {
-                    currentDate = format.format(new Date(now));
-                    currentDateGenerated = now;
-                }
-            }
+            currentDate = FORMAT_RFC5322.format(new Date(now));
+            currentDateGenerated = now;
         }
         return currentDate;
-
     }
 
 
     /**
      * Get the HTTP format of the specified date.
+     * @param value The date
+     * @param threadLocalformat Ignored. The local ConcurrentDateFormat will
+     *                          always be used.
+     * @return the HTTP date
      */
-    public static final String formatDate
-        (long value, DateFormat threadLocalformat) {
+    public static final String formatDate(long value, DateFormat threadLocalformat) {
+        return formatDate(value);
+    }
+
 
+    /**
+     * Get the HTTP format of the specified date.
+     * @param value The date
+     * @return the HTTP date
+     */
+    public static final String formatDate(long value) {
         Long longValue = Long.valueOf(value);
         String cachedDate = formatCache.get(longValue);
         if (cachedDate != null) {
             return cachedDate;
         }
 
-        String newDate = null;
-        Date dateValue = new Date(value);
-        if (threadLocalformat != null) {
-            newDate = threadLocalformat.format(dateValue);
-            updateFormatCache(longValue, newDate);
-        } else {
-            synchronized (format) {
-                newDate = format.format(dateValue);
-            }
-            updateFormatCache(longValue, newDate);
-        }
+        String newDate = FORMAT_RFC5322.format(new Date(value));
+        updateFormatCache(longValue, newDate);
         return newDate;
     }
 
 
     /**
      * Try to parse the given date as a HTTP date.
+     * @param value The HTTP date
+     * @param threadLocalformats Ignored. The local array of
+     *                           ConcurrentDateFormat will always be used.
+     * @return the date as a long
+     *
+     * @deprecated Unused. This will be removed in Tomcat 10
+     *             Use {@link #parseDate(String)}
      */
-    public static final long parseDate(String value,
-                                       DateFormat[] threadLocalformats) {
+    @Deprecated
+    public static final long parseDate(String value, DateFormat[] threadLocalformats) {
+        return parseDate(value);
+    }
+
+
+    /**
+     * Try to parse the given date as a HTTP date.
+     * @param value The HTTP date
+     * @return the date as a long or <code>-1</code> if the value cannot be
+     *         parsed
+     */
+    public static final long parseDate(String value) {
 
         Long cachedDate = parseCache.get(value);
         if (cachedDate != null) {
             return cachedDate.longValue();
         }
 
-        Long date = null;
-        if (threadLocalformats != null) {
-            date = internalParseDate(value, threadLocalformats);
-            updateParseCache(value, date);
-        } else {
-            date = internalParseDate(value, formats);
-            updateParseCache(value, date);
-        }
-        if (date == null) {
-            return (-1L);
-        }
-
-        return date.longValue();
-    }
-
-
-    /**
-     * Parse date with given formatters.
-     */
-    private static final Long internalParseDate
-        (String value, DateFormat[] formats) {
-        Date date = null;
-        for (int i = 0; (date == null) && (i < formats.length); i++) {
+        long date = -1;
+        for (int i = 0; (date == -1) && (i < httpParseFormats.length); i++) {
             try {
-                date = formats[i].parse(value);
+                date = httpParseFormats[i].parse(value).getTime();
+                updateParseCache(value, Long.valueOf(date));
             } catch (ParseException e) {
                 // Ignore
             }
         }
-        if (date == null) {
-            return null;
-        }
-        return Long.valueOf(date.getTime());
+
+        return date;
     }
 
 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org