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


The following commit(s) were added to refs/heads/main by this push:
     new 1ddae6c19a Refactor duplicate code for extracting just the media type 
and subtype
1ddae6c19a is described below

commit 1ddae6c19a3c3d053080992c2823ed802478bbee
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Fri Nov 15 11:08:13 2024 +0000

    Refactor duplicate code for extracting just the media type and subtype
---
 java/org/apache/catalina/connector/Request.java    | 17 +++--------
 java/org/apache/catalina/filters/CorsFilter.java   | 19 ++----------
 .../apache/tomcat/util/http/parser/MediaType.java  | 34 ++++++++++++++++++++++
 3 files changed, 40 insertions(+), 30 deletions(-)

diff --git a/java/org/apache/catalina/connector/Request.java 
b/java/org/apache/catalina/connector/Request.java
index b3758a48f3..3fab3f11e9 100644
--- a/java/org/apache/catalina/connector/Request.java
+++ b/java/org/apache/catalina/connector/Request.java
@@ -114,6 +114,7 @@ import 
org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException;
 import org.apache.tomcat.util.http.fileupload.impl.SizeException;
 import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;
 import org.apache.tomcat.util.http.parser.AcceptLanguage;
+import org.apache.tomcat.util.http.parser.MediaType;
 import org.apache.tomcat.util.http.parser.Upgrade;
 import org.apache.tomcat.util.net.SSLSupport;
 import org.apache.tomcat.util.res.StringManager;
@@ -2792,19 +2793,9 @@ public class Request implements HttpServletRequest {
             return;
         }
 
-        String contentType = getContentType();
-        if (contentType == null) {
-            contentType = "";
-        }
-        int semicolon = contentType.indexOf(';');
-        if (semicolon >= 0) {
-            contentType = contentType.substring(0, semicolon).trim();
-        } else {
-            contentType = contentType.trim();
-        }
-        contentType = contentType.toLowerCase(Locale.ENGLISH);
+        String mediaType = MediaType.parseMediaTypeOnly(getContentType());
 
-        if ("multipart/form-data".equals(contentType)) {
+        if ("multipart/form-data".equals(mediaType)) {
             parseParts();
             if (partsParseException instanceof IllegalStateException) {
                 parametersParseException = (IllegalStateException) 
partsParseException;
@@ -2818,7 +2809,7 @@ public class Request implements HttpServletRequest {
             return;
         }
 
-        if (!("application/x-www-form-urlencoded".equals(contentType))) {
+        if (!("application/x-www-form-urlencoded".equals(mediaType))) {
             return;
         }
 
diff --git a/java/org/apache/catalina/filters/CorsFilter.java 
b/java/org/apache/catalina/filters/CorsFilter.java
index 504af77dcf..a12b394c4e 100644
--- a/java/org/apache/catalina/filters/CorsFilter.java
+++ b/java/org/apache/catalina/filters/CorsFilter.java
@@ -39,6 +39,7 @@ import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.http.RequestUtil;
 import org.apache.tomcat.util.http.ResponseUtil;
+import org.apache.tomcat.util.http.parser.MediaType;
 import org.apache.tomcat.util.res.StringManager;
 
 /**
@@ -566,7 +567,7 @@ public class CorsFilter extends GenericFilter {
                     } else if ("GET".equals(method) || "HEAD".equals(method)) {
                         requestType = CORSRequestType.SIMPLE;
                     } else if ("POST".equals(method)) {
-                        String mediaType = 
getMediaType(request.getContentType());
+                        String mediaType = 
MediaType.parseMediaTypeOnly(request.getContentType());
                         if (mediaType == null) {
                             requestType = CORSRequestType.SIMPLE;
                         } else {
@@ -589,22 +590,6 @@ public class CorsFilter extends GenericFilter {
     }
 
 
-    /**
-     * Return the lower case, trimmed value of the media type from the content 
type.
-     */
-    private String getMediaType(String contentType) {
-        if (contentType == null) {
-            return null;
-        }
-        String result = contentType.toLowerCase(Locale.ENGLISH);
-        int firstSemiColonIndex = result.indexOf(';');
-        if (firstSemiColonIndex > -1) {
-            result = result.substring(0, firstSemiColonIndex);
-        }
-        result = result.trim();
-        return result;
-    }
-
     /**
      * Checks if the Origin is allowed to make a CORS request.
      *
diff --git a/java/org/apache/tomcat/util/http/parser/MediaType.java 
b/java/org/apache/tomcat/util/http/parser/MediaType.java
index f89f12444b..2f1f0c981f 100644
--- a/java/org/apache/tomcat/util/http/parser/MediaType.java
+++ b/java/org/apache/tomcat/util/http/parser/MediaType.java
@@ -169,4 +169,38 @@ public class MediaType {
         return new MediaType(type, subtype, parameters);
     }
 
+
+    /**
+     * A simplified media type parser that removes any parameters and just 
returns the media type and the subtype.
+     *
+     * @param input The input string to parse
+     *
+     * @return The media type and subtype from the input trimmed and converted 
to lower case
+     */
+    public static String parseMediaTypeOnly(String input) {
+
+        if (input == null) {
+            return null;
+        }
+
+        /*
+         * Parsing the media type and subtype as tokens as in the 
parseMediaType() method would further validate the
+         * input but is not currently necessary given how the return value 
from this method is currently used. The
+         * return value from this method is always compared to a set of 
allowed or expected values so any non-compliant
+         * values will be rejected / ignored at that stage.
+         */
+        String result;
+
+        // Remove parameters
+        int semicolon = input.indexOf(';');
+        if (semicolon > -1) {
+            result = input.substring(0, semicolon);
+        } else {
+            result = input;
+        }
+
+        result = result.trim();
+        result = result.toLowerCase(Locale.ENGLISH);
+        return result;
+    }
 }


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

Reply via email to