This is an automated email from the ASF dual-hosted git repository.
markt pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/9.0.x by this push:
new 68dbf03802 Refactor duplicate code for extracting just the media type
and subtype
68dbf03802 is described below
commit 68dbf03802f73e33c448d089c971207bb539f9ea
Author: Mark Thomas <[email protected]>
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 ++++++++++++++++++++++
webapps/docs/changelog.xml | 4 +++
4 files changed, 44 insertions(+), 30 deletions(-)
diff --git a/java/org/apache/catalina/connector/Request.java
b/java/org/apache/catalina/connector/Request.java
index 37514101df..7952176216 100644
--- a/java/org/apache/catalina/connector/Request.java
+++ b/java/org/apache/catalina/connector/Request.java
@@ -115,6 +115,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;
@@ -2942,19 +2943,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(false);
success = true;
return;
@@ -2965,7 +2956,7 @@ public class Request implements HttpServletRequest {
return;
}
- if (!("application/x-www-form-urlencoded".equals(contentType))) {
+ if (!("application/x-www-form-urlencoded".equals(mediaType))) {
success = true;
return;
}
diff --git a/java/org/apache/catalina/filters/CorsFilter.java
b/java/org/apache/catalina/filters/CorsFilter.java
index 05dee30c9f..5dc9582aeb 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;
+ }
}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 9af1bfda7f..99d7e64c7a 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -119,6 +119,10 @@
<bug>69442</bug>: Fix case sensitive check on <code>content-type</code>
when parsing request parameters. (remm)
</fix>
+ <scode>
+ Refactor duplicate code for extracting media type and subtype from
+ <code>content-type</code> into a single method. (markt)
+ </scode>
</changelog>
</subsection>
<subsection name="Coyote">
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]