This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/10.1.x by this push:
new f3471e97d4 Improve performance of String based URI normalization
f3471e97d4 is described below
commit f3471e97d4b2d98190d4759044ae21c40e1504ff
Author: Mark Thomas <[email protected]>
AuthorDate: Thu Nov 26 09:04:36 2026 +0000
Improve performance of String based URI normalization
Skip runs of adjacent slashes when searching for the context path in
RequestUtil.getContextPathEncoded() and make
RequestUtil.normalize() collapse "//", "/./" and "/../" in a single pass
rather than rebuilding the string for each occurrence.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
java/org/apache/catalina/util/RequestUtil.java | 4 ++
java/org/apache/tomcat/util/http/RequestUtil.java | 64 ++++++++++++++---------
webapps/docs/changelog.xml | 3 ++
3 files changed, 47 insertions(+), 24 deletions(-)
diff --git a/java/org/apache/catalina/util/RequestUtil.java
b/java/org/apache/catalina/util/RequestUtil.java
index 23e9e48050..a923522871 100644
--- a/java/org/apache/catalina/util/RequestUtil.java
+++ b/java/org/apache/catalina/util/RequestUtil.java
@@ -357,6 +357,10 @@ public final class RequestUtil {
EncodedSolidusHandling encodedReverseSolidusHandling) {
int len = uri.length;
int pos = startPos;
+ // Skip any run of adjacent slashes so each candidate prefix ends
after a non-empty path segment.
+ while (pos < len && uri[pos] == '/') {
+ pos++;
+ }
while (pos < len) {
if (uri[pos] == '/') {
return pos;
diff --git a/java/org/apache/tomcat/util/http/RequestUtil.java
b/java/org/apache/tomcat/util/http/RequestUtil.java
index 8da10e3f98..1a2bed8ef3 100644
--- a/java/org/apache/tomcat/util/http/RequestUtil.java
+++ b/java/org/apache/tomcat/util/http/RequestUtil.java
@@ -90,34 +90,50 @@ public class RequestUtil {
}
// Resolve occurrences of "//" in the normalized path
- while (true) {
- int index = normalized.indexOf("//");
- if (index < 0) {
- break;
+ // Single pass so that a long run of '/' is not O(n^2)
+ int doubleSlash = normalized.indexOf("//");
+ if (doubleSlash >= 0) {
+ StringBuilder sb = new StringBuilder(normalized.length());
+ sb.append(normalized, 0, doubleSlash + 1);
+ for (int i = doubleSlash + 2; i < normalized.length(); i++) {
+ char c = normalized.charAt(i);
+ if (c == '/' && sb.charAt(sb.length() - 1) == '/') {
+ continue;
+ }
+ sb.append(c);
}
- normalized = normalized.substring(0, index) +
normalized.substring(index + 1);
+ normalized = sb.toString();
}
- // Resolve occurrences of "/./" in the normalized path
- while (true) {
- int index = normalized.indexOf("/./");
- if (index < 0) {
- break;
+ // Resolve occurrences of "/./" and "/../" in the normalized path.
Single pass over the path segments so that
+ // many such segments are not O(n^2). At this point there are no empty
segments except after a trailing '/'.
+ if (normalized.contains("/./") || normalized.contains("/../")) {
+ int length = normalized.length();
+ StringBuilder sb = new StringBuilder(length);
+ int start = 1;
+ while (start <= length) {
+ int end = normalized.indexOf('/', start);
+ if (end < 0) {
+ end = length;
+ }
+ int segmentLength = end - start;
+ if (segmentLength == 0) {
+ // Only occurs for the trailing '/'
+ sb.append('/');
+ } else if (segmentLength == 1 && normalized.charAt(start) ==
'.') {
+ // "/./" - ignore
+ } else if (segmentLength == 2 && normalized.charAt(start) ==
'.' &&
+ normalized.charAt(start + 1) == '.') {
+ if (sb.length() == 0) {
+ return null; // Trying to go outside our context
+ }
+ sb.setLength(sb.lastIndexOf("/"));
+ } else {
+ sb.append('/').append(normalized, start, end);
+ }
+ start = end + 1;
}
- normalized = normalized.substring(0, index) +
normalized.substring(index + 2);
- }
-
- // Resolve occurrences of "/../" in the normalized path
- while (true) {
- int index = normalized.indexOf("/../");
- if (index < 0) {
- break;
- }
- if (index == 0) {
- return null; // Trying to go outside our context
- }
- int index2 = normalized.lastIndexOf('/', index - 1);
- normalized = normalized.substring(0, index2) +
normalized.substring(index + 3);
+ normalized = sb.toString();
}
if (normalized.length() > 1 && addedTrailingSlash) {
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index e577bfc373..9d37543ce8 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -352,6 +352,9 @@
<code>destroy()</code> is only called if <code>init()</code> was called
for the handler. (markt)
</fix>
+ <fix>
+ Improve performance of String based URI normalization. (markt)
+ </fix>
</changelog>
</subsection>
<subsection name="Jasper">
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]