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:32 UTC
[tomcat] 01/07: Prep for additional backports. Align with master
(excluding deprecation)
This is an automated email from the ASF dual-hosted git repository.
markt pushed a commit to branch 8.5.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 56a1caeb752806f51784a19ef470d7f73e855971
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Mon Jul 29 20:17:39 2019 +0100
Prep for additional backports. Align with master (excluding deprecation)
---
.../tomcat/util/http/ConcurrentDateFormat.java | 72 +++++++++++++
.../tomcat/util/http/FastHttpDateFormat.java | 118 ++++++++++-----------
2 files changed, 126 insertions(+), 64 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..9ce953f
--- /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<>();
+
+ 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 f30c04b..86376d2 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;
@@ -43,23 +42,30 @@ public final class FastHttpDateFormat {
/**
* The only date format permitted when generating HTTP headers.
*/
- public static final String RFC1123_DATE =
- "EEE, dd MMM yyyy HH:mm:ss zzz";
+ public static final String RFC1123_DATE = "EEE, dd MMM yyyy HH:mm:ss zzz";
- private static final SimpleDateFormat format =
- new SimpleDateFormat(RFC1123_DATE, Locale.US);
+ // 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";
+ 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 {
- format.setTimeZone(gmtZone);
- }
+ // All the formats that use a timezone use GMT
+ TimeZone tz = TimeZone.getTimeZone("GMT");
+ 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.
@@ -93,47 +99,41 @@ public final class FastHttpDateFormat {
* @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 Local format to avoid synchronization
+ * @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;
}
@@ -141,49 +141,39 @@ public final class FastHttpDateFormat {
/**
* Try to parse the given date as a HTTP date.
* @param value The HTTP date
- * @param threadLocalformats Local format to avoid synchronization
+ * @param threadLocalformats Ignored. The local array of
+ * ConcurrentDateFormat will always be used.
* @return the date as a long
*/
- public static final long parseDate(String value,
- DateFormat[] threadLocalformats) {
+ 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 {
- throw new IllegalArgumentException();
- }
- 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