Skip to content

Commit

Permalink
Code clean-up. No functional change.
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed Apr 11, 2023
1 parent 3e7a97b commit f00b3b9
Show file tree
Hide file tree
Showing 13 changed files with 414 additions and 497 deletions.
5 changes: 2 additions & 3 deletions java/org/apache/tomcat/util/http/ConcurrentDateFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
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.
* 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 {

Expand Down
34 changes: 13 additions & 21 deletions java/org/apache/tomcat/util/http/CookieProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public interface CookieProcessor {
* Parse the provided headers into server cookie objects.
*
* @param headers The HTTP headers to parse
* @param serverCookies The server cookies object to populate with the
* results of the parsing
* @param serverCookies The server cookies object to populate with the results of the parsing
*/
void parseCookieHeader(MimeHeaders headers, ServerCookies serverCookies);

Expand All @@ -37,38 +36,31 @@ public interface CookieProcessor {
*
* @param cookie The cookie for which the header will be generated
*
* @return The header value in a form that can be added directly to the
* response
* @return The header value in a form that can be added directly to the response
*
* @deprecated This method has been replaced with
* {@link #generateHeader(Cookie, HttpServletRequest)} and will
* be removed from Tomcat 10 onwards.
* @deprecated This method has been replaced with {@link #generateHeader(Cookie, HttpServletRequest)} and will be
* removed from Tomcat 10 onwards.
*/
@Deprecated
String generateHeader(Cookie cookie);

/**
* Generate the {@code Set-Cookie} HTTP header value for the given Cookie.
* This method receives as parameter the servlet request so that it can make
* decisions based on request properties. One such use-case is decide if the
* SameSite attribute should be added to the cookie based on the User-Agent
* or other request header because there are browser versions incompatible
* with the SameSite attribute. This is described by <a
* href="https://www.chromium.org/updates/same-site/incompatible-clients">the
* Chromium project</a>.
* Generate the {@code Set-Cookie} HTTP header value for the given Cookie. This method receives as parameter the
* servlet request so that it can make decisions based on request properties. One such use-case is decide if the
* SameSite attribute should be added to the cookie based on the User-Agent or other request header because there
* are browser versions incompatible with the SameSite attribute. This is described by
* <a href="https://www.chromium.org/updates/same-site/incompatible-clients">the Chromium project</a>.
*
* @param request The servlet request
* @param cookie The cookie for which the header will be generated
*
* @param cookie The cookie for which the header will be generated
*
* @return The header value in a form that can be added directly to the
* response
* @return The header value in a form that can be added directly to the response
*/
String generateHeader(Cookie cookie, HttpServletRequest request);

/**
* Obtain the character set that will be used when converting between bytes
* and characters when parsing and/or generating HTTP headers for cookies.
* Obtain the character set that will be used when converting between bytes and characters when parsing and/or
* generating HTTP headers for cookies.
*
* @return The character set used for byte&lt;-&gt;character conversions
*/
Expand Down
13 changes: 5 additions & 8 deletions java/org/apache/tomcat/util/http/CookieProcessorBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ public abstract class CookieProcessorBase implements CookieProcessor {

private static final String COOKIE_DATE_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z";

protected static final ThreadLocal<DateFormat> COOKIE_DATE_FORMAT =
new ThreadLocal<DateFormat>() {
protected static final ThreadLocal<DateFormat> COOKIE_DATE_FORMAT = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
DateFormat df =
new SimpleDateFormat(COOKIE_DATE_PATTERN, Locale.US);
DateFormat df = new SimpleDateFormat(COOKIE_DATE_PATTERN, Locale.US);
df.setTimeZone(TimeZone.getTimeZone("GMT"));
return df;
}
Expand All @@ -59,10 +57,9 @@ public void setSameSiteCookies(String sameSiteCookies) {
/**
* {@inheritDoc}
*
* @deprecated This implementation calls the deprecated
* {@link #generateHeader(Cookie)} method. Implementors should
* not rely on this method as it is present only for
* transitional compatibility and will be removed in Tomcat 9.
* @deprecated This implementation calls the deprecated {@link #generateHeader(Cookie)} method. Implementors should
* not rely on this method as it is present only for transitional compatibility and will be removed
* in Tomcat 9.
*/
@Deprecated
@Override
Expand Down
33 changes: 19 additions & 14 deletions java/org/apache/tomcat/util/http/FastHttpDateFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public final class FastHttpDateFormat {
// -------------------------------------------------------------- Variables


private static final int CACHE_SIZE =
Integer.getInteger("org.apache.tomcat.util.http.FastHttpDateFormat.CACHE_SIZE", 1000).intValue();
private static final int CACHE_SIZE = Integer
.getInteger("org.apache.tomcat.util.http.FastHttpDateFormat.CACHE_SIZE", 1000).intValue();


/**
Expand Down Expand Up @@ -66,8 +66,8 @@ public final class FastHttpDateFormat {
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 };
httpParseFormats = new ConcurrentDateFormat[] { FORMAT_RFC5322, FORMAT_OBSOLETE_RFC850,
FORMAT_OBSOLETE_ASCTIME };
}

/**
Expand Down Expand Up @@ -99,6 +99,7 @@ public final class FastHttpDateFormat {

/**
* Get the current date in HTTP format.
*
* @return the HTTP date
*/
public static String getCurrentDate() {
Expand All @@ -114,9 +115,10 @@ public static String getCurrentDate() {

/**
* Get the HTTP format of the specified date.
* @param value The date
* @param threadLocalformat Ignored. The local ConcurrentDateFormat will
* always be used.
*
* @param value The date
* @param threadLocalformat Ignored. The local ConcurrentDateFormat will always be used.
*
* @return the HTTP date
*
* @deprecated Unused. This will be removed in Tomcat 10
Expand All @@ -129,7 +131,9 @@ public static String formatDate(long value, DateFormat threadLocalformat) {

/**
* Get the HTTP format of the specified date.
*
* @param value The date
*
* @return the HTTP date
*/
public static String formatDate(long value) {
Expand All @@ -147,13 +151,13 @@ public static String formatDate(long value) {

/**
* Try to parse the given date as an HTTP date.
* @param value The HTTP date
* @param threadLocalformats Ignored. The local array of
* ConcurrentDateFormat will always be used.
*
* @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)}
* @deprecated Unused. This will be removed in Tomcat 10 Use {@link #parseDate(String)}
*/
@Deprecated
public static long parseDate(String value, DateFormat[] threadLocalformats) {
Expand All @@ -163,9 +167,10 @@ public static long parseDate(String value, DateFormat[] threadLocalformats) {

/**
* Try to parse the given date as an HTTP date.
*
* @param value The HTTP date
* @return the date as a long or <code>-1</code> if the value cannot be
* parsed
*
* @return the date as a long or <code>-1</code> if the value cannot be parsed
*/
public static long parseDate(String value) {

Expand Down
10 changes: 4 additions & 6 deletions java/org/apache/tomcat/util/http/HeaderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@
public class HeaderUtil {

/**
* Converts an HTTP header line in byte form to a printable String.
* Bytes corresponding to visible ASCII characters will converted to those
* characters. All other bytes (0x00 to 0x1F, 0x7F to OxFF) will be
* represented in 0xNN form.
* Converts an HTTP header line in byte form to a printable String. Bytes corresponding to visible ASCII characters
* will converted to those characters. All other bytes (0x00 to 0x1F, 0x7F to OxFF) will be represented in 0xNN
* form.
*
* @param bytes Contains an HTTP header line
* @param offset The start position of the header line in the array
* @param len The length of the HTTP header line
*
* @return A String with non-printing characters replaced by the 0xNN
* equivalent
* @return A String with non-printing characters replaced by the 0xNN equivalent
*/
public static String toPrintableString(byte[] bytes, int offset, int len) {
StringBuilder result = new StringBuilder();
Expand Down
79 changes: 37 additions & 42 deletions java/org/apache/tomcat/util/http/HttpMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@
*/
public class HttpMessages {

private static final Map<Locale,HttpMessages> instances =
new ConcurrentHashMap<>();
private static final Map<Locale, HttpMessages> instances = new ConcurrentHashMap<>();

private static final HttpMessages DEFAULT = new HttpMessages(
StringManager.getManager("org.apache.tomcat.util.http.res",
Locale.getDefault()));
StringManager.getManager("org.apache.tomcat.util.http.res", Locale.getDefault()));


// XXX move message resources in this package
Expand All @@ -56,55 +54,52 @@ private HttpMessages(StringManager sm) {


/**
* Get the status string associated with a status code. Common messages are
* cached.
* Get the status string associated with a status code. Common messages are cached.
*
* @param status The HTTP status code to retrieve the message for
*
* @return The HTTP status string that conforms to the requirements of the
* HTTP specification
* @return The HTTP status string that conforms to the requirements of the HTTP specification
*/
public String getMessage(int status) {
// method from Response.

// Does HTTP requires/allow international messages or
// are pre-defined? The user doesn't see them most of the time
switch( status ) {
case 200:
if(st_200 == null ) {
st_200 = sm.getString("sc.200");
}
return st_200;
case 302:
if(st_302 == null ) {
st_302 = sm.getString("sc.302");
}
return st_302;
case 400:
if(st_400 == null ) {
st_400 = sm.getString("sc.400");
}
return st_400;
case 404:
if(st_404 == null ) {
st_404 = sm.getString("sc.404");
}
return st_404;
case 500:
if (st_500 == null) {
st_500 = sm.getString("sc.500");
}
return st_500;
switch (status) {
case 200:
if (st_200 == null) {
st_200 = sm.getString("sc.200");
}
return st_200;
case 302:
if (st_302 == null) {
st_302 = sm.getString("sc.302");
}
return st_302;
case 400:
if (st_400 == null) {
st_400 = sm.getString("sc.400");
}
return st_400;
case 404:
if (st_404 == null) {
st_404 = sm.getString("sc.404");
}
return st_404;
case 500:
if (st_500 == null) {
st_500 = sm.getString("sc.500");
}
return st_500;
}
return sm.getString("sc."+ status);
return sm.getString("sc." + status);
}


public static HttpMessages getInstance(Locale locale) {
HttpMessages result = instances.get(locale);
if (result == null) {
StringManager sm = StringManager.getManager(
"org.apache.tomcat.util.http.res", locale);
StringManager sm = StringManager.getManager("org.apache.tomcat.util.http.res", locale);
if (Locale.getDefault().equals(sm.getLocale())) {
result = DEFAULT;
} else {
Expand All @@ -117,12 +112,12 @@ public static HttpMessages getInstance(Locale locale) {


/**
* Is the provided message safe to use in an HTTP header. Safe messages must
* meet the requirements of RFC2616 - i.e. must consist only of TEXT.
* Is the provided message safe to use in an HTTP header. Safe messages must meet the requirements of RFC2616 - i.e.
* must consist only of TEXT.
*
* @param msg The message to test
*
* @param msg The message to test
* @return <code>true</code> if the message is safe to use in an HTTP
* header else <code>false</code>
* @return <code>true</code> if the message is safe to use in an HTTP header else <code>false</code>
*/
public static boolean isSafeInHttpHeader(String msg) {
// Nulls are fine. It is up to the calling code to address any NPE
Expand Down
Loading

0 comments on commit f00b3b9

Please sign in to comment.