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

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

commit 76e3fe20dd8a38220f703214a5455d63878e8ac4
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Jan 18 19:15:52 2023 +0000

    Refactor CoyoteRequest to only use CharsetHolder
---
 .../org/apache/catalina/connector/InputBuffer.java |  2 +-
 java/org/apache/catalina/connector/Request.java    | 55 +++++++++-------------
 .../catalina/ssi/SSIServletExternalResolver.java   |  7 +--
 java/org/apache/coyote/Request.java                | 27 +++++++++++
 4 files changed, 52 insertions(+), 39 deletions(-)

diff --git a/java/org/apache/catalina/connector/InputBuffer.java 
b/java/org/apache/catalina/connector/InputBuffer.java
index d193775a2b..4512a8c0be 100644
--- a/java/org/apache/catalina/connector/InputBuffer.java
+++ b/java/org/apache/catalina/connector/InputBuffer.java
@@ -536,7 +536,7 @@ public class InputBuffer extends Reader
 
         Charset charset = null;
         if (coyoteRequest != null) {
-            charset = coyoteRequest.getCharset();
+            charset = coyoteRequest.getCharsetHolder().getValidatedCharset();
         }
 
         if (charset == null) {
diff --git a/java/org/apache/catalina/connector/Request.java 
b/java/org/apache/catalina/connector/Request.java
index bb3d1897d9..b943bfe0e8 100644
--- a/java/org/apache/catalina/connector/Request.java
+++ b/java/org/apache/catalina/connector/Request.java
@@ -95,6 +95,7 @@ import org.apache.tomcat.InstanceManager;
 import org.apache.tomcat.util.ExceptionUtils;
 import org.apache.tomcat.util.buf.B2CConverter;
 import org.apache.tomcat.util.buf.ByteChunk;
+import org.apache.tomcat.util.buf.CharsetHolder;
 import org.apache.tomcat.util.buf.EncodedSolidusHandling;
 import org.apache.tomcat.util.buf.MessageBytes;
 import org.apache.tomcat.util.buf.StringUtils;
@@ -958,44 +959,34 @@ public class Request implements HttpServletRequest {
      */
     @Override
     public String getCharacterEncoding() {
-        String characterEncoding = coyoteRequest.getCharacterEncoding();
-        if (characterEncoding != null) {
-            return characterEncoding;
-        }
+        String characterEncoding = coyoteRequest.getCharsetHolder().getName();
 
-        Context context = getContext();
-        if (context != null) {
-            return context.getRequestCharacterEncoding();
+        if (characterEncoding == null) {
+            Context context = getContext();
+            if (context != null) {
+                characterEncoding = context.getRequestCharacterEncoding();
+            }
         }
 
-        return null;
+        return characterEncoding;
     }
 
 
     private Charset getCharset() {
-        Charset charset = null;
-        try {
-            charset = coyoteRequest.getCharset();
-        } catch (UnsupportedEncodingException e) {
-            // Ignore
-        }
-        if (charset != null) {
-            return charset;
-        }
+        Charset charset = coyoteRequest.getCharsetHolder().getCharset();
 
-        Context context = getContext();
-        if (context != null) {
-            String encoding = context.getRequestCharacterEncoding();
-            if (encoding != null) {
-                try {
-                    return B2CConverter.getCharset(encoding);
-                } catch (UnsupportedEncodingException e) {
-                    // Ignore
-                }
+        if (charset == null) {
+            Context context = getContext();
+            if (context != null) {
+                charset = 
CharsetHolder.getInstance(context.getRequestCharacterEncoding()).getCharset() ;
             }
         }
 
-        return org.apache.coyote.Constants.DEFAULT_BODY_CHARSET;
+        if (charset == null) {
+            charset = org.apache.coyote.Constants.DEFAULT_BODY_CHARSET;
+        }
+
+        return charset;
     }
 
 
@@ -1209,7 +1200,7 @@ public class Request implements HttpServletRequest {
         // to check for a default request character encoding at the Context.
         // Therefore, if a Context default should be used, it is set explicitly
         // here. Need to do this before setting usingReader.
-        if (coyoteRequest.getCharacterEncoding() == null) {
+        if (coyoteRequest.getCharsetHolder().getName() == null) {
             // Nothing currently set explicitly.
             // Check the context
             Context context = getContext();
@@ -1612,11 +1603,11 @@ public class Request implements HttpServletRequest {
             return;
         }
 
-        // Confirm that the encoding name is valid
-        Charset charset = B2CConverter.getCharset(enc);
+        CharsetHolder charsetHolder = CharsetHolder.getInstance(enc);
+        charsetHolder.validate();
 
         // Save the validated encoding
-        coyoteRequest.setCharset(charset);
+        coyoteRequest.setCharsetHolder(charsetHolder);
     }
 
 
@@ -1628,7 +1619,7 @@ public class Request implements HttpServletRequest {
         }
 
         // Save the validated encoding
-        coyoteRequest.setCharset(charset);
+        coyoteRequest.setCharsetHolder(CharsetHolder.getInstance(charset));
     }
 
 
diff --git a/java/org/apache/catalina/ssi/SSIServletExternalResolver.java 
b/java/org/apache/catalina/ssi/SSIServletExternalResolver.java
index b601c9c3b0..12cc6ad2dd 100644
--- a/java/org/apache/catalina/ssi/SSIServletExternalResolver.java
+++ b/java/org/apache/catalina/ssi/SSIServletExternalResolver.java
@@ -18,7 +18,6 @@ package org.apache.catalina.ssi;
 
 
 import java.io.IOException;
-import java.io.UnsupportedEncodingException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.nio.charset.Charset;
@@ -256,11 +255,7 @@ public class SSIServletExternalResolver implements 
SSIExternalResolver {
                         // Get encoding settings from request / connector if
                         // possible
                         if (req instanceof Request) {
-                            try {
-                                requestCharset = 
((Request)req).getCoyoteRequest().getCharset();
-                            } catch (UnsupportedEncodingException e) {
-                                // Ignore
-                            }
+                            requestCharset = 
((Request)req).getCoyoteRequest().getCharsetHolder().getCharset();
                             Connector connector =  
((Request)req).getConnector();
                             uriCharset = connector.getURICharset();
                             useBodyEncodingForURI = 
connector.getUseBodyEncodingForURI();
diff --git a/java/org/apache/coyote/Request.java 
b/java/org/apache/coyote/Request.java
index 8fdcfdb6ed..c57f17f70c 100644
--- a/java/org/apache/coyote/Request.java
+++ b/java/org/apache/coyote/Request.java
@@ -404,7 +404,10 @@ public final class Request {
      * @return The value set via {@link #setCharset(Charset)} or if no
      *         call has been made to that method try to obtain if from the
      *         content type.
+     *
+     * @deprecated Unused. This method will be removed in Tomcat 11.
      */
+    @Deprecated
     public String getCharacterEncoding() {
         if (charsetHolder.getName() == null) {
             charsetHolder = 
CharsetHolder.getInstance(getCharsetFromContentType(getContentType()));
@@ -423,7 +426,10 @@ public final class Request {
      *
      * @throws UnsupportedEncodingException If the user agent has specified an
      *         invalid character encoding
+     *
+     * @deprecated Unused. This method will be removed in Tomcat 11.
      */
+    @Deprecated
     public Charset getCharset() throws UnsupportedEncodingException {
         if (charsetHolder.getName() == null) {
             // Populates charsetHolder
@@ -434,11 +440,32 @@ public final class Request {
     }
 
 
+    /**
+     * Unused.
+     *
+     * @param charset The Charset to use for the request
+     *
+     * @deprecated Unused. This method will be removed in Tomcat 11.
+     */
+    @Deprecated
     public void setCharset(Charset charset) {
         charsetHolder = CharsetHolder.getInstance(charset);
     }
 
 
+    public CharsetHolder getCharsetHolder() {
+        if (charsetHolder.getName() == null) {
+            charsetHolder = 
CharsetHolder.getInstance(getCharsetFromContentType(getContentType()));
+        }
+        return charsetHolder;
+    }
+
+
+    public void setCharsetHolder(CharsetHolder charsetHolder) {
+        this.charsetHolder = charsetHolder;
+    }
+
+
     public void setContentLength(long len) {
         this.contentLength = len;
     }


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

Reply via email to