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 b285e104f7 As per comment, replace custom code with JRE call available
in Java 1.4+
b285e104f7 is described below
commit b285e104f746809cab6b9f58a3bc9afd7dff0f4b
Author: Mark Thomas <[email protected]>
AuthorDate: Thu Mar 6 15:56:30 2025 +0000
As per comment, replace custom code with JRE call available in Java 1.4+
The JRE call does %nn decode a few additional characters. Namely:
'!', '\'', '(', ')' and '~'
Strictly there is no need to encode any of the special characters but
encoding them should not cause any harm.
---
.../apache/jasper/runtime/JspRuntimeLibrary.java | 68 +++-------------------
webapps/docs/changelog.xml | 4 ++
2 files changed, 13 insertions(+), 59 deletions(-)
diff --git a/java/org/apache/jasper/runtime/JspRuntimeLibrary.java
b/java/org/apache/jasper/runtime/JspRuntimeLibrary.java
index 1b6a75fae9..6da1090d8e 100644
--- a/java/org/apache/jasper/runtime/JspRuntimeLibrary.java
+++ b/java/org/apache/jasper/runtime/JspRuntimeLibrary.java
@@ -18,10 +18,11 @@ package org.apache.jasper.runtime;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.io.OutputStreamWriter;
import java.lang.reflect.Method;
+import java.net.URLEncoder;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import jakarta.servlet.RequestDispatcher;
@@ -1002,76 +1003,25 @@ public class JspRuntimeLibrary {
/**
* URL encodes a string, based on the supplied character encoding.
- * This performs the same function as java.next.URLEncode.encode
- * in J2SDK1.4, and should be removed if the only platform supported
- * is 1.4 or higher.
* @param s The String to be URL encoded.
* @param enc The character encoding
* @return The URL encoded String
*/
public static String URLEncode(String s, String enc) {
-
if (s == null) {
return "null";
}
-
if (enc == null) {
enc = "ISO-8859-1"; // The default request encoding
}
-
- StringBuilder out = new StringBuilder(s.length());
- ByteArrayOutputStream buf = new ByteArrayOutputStream();
- OutputStreamWriter writer = null;
+ Charset cs = null;
try {
- writer = new OutputStreamWriter(buf, enc);
- } catch (java.io.UnsupportedEncodingException ex) {
- // Use the default encoding?
- writer = new OutputStreamWriter(buf);
- }
-
- for (int i = 0; i < s.length(); i++) {
- int c = s.charAt(i);
- if (c == ' ') {
- out.append('+');
- } else if (isSafeChar(c)) {
- out.append((char)c);
- } else {
- // convert to external encoding before hex conversion
- try {
- writer.write(c);
- writer.flush();
- } catch(IOException e) {
- buf.reset();
- continue;
- }
- byte[] ba = buf.toByteArray();
- for (byte b : ba) {
- out.append('%');
- // Converting each byte in the buffer
- out.append(Character.forDigit((b >> 4) & 0xf, 16));
- out.append(Character.forDigit(b & 0xf, 16));
- }
- buf.reset();
- }
- }
- return out.toString();
- }
-
- private static boolean isSafeChar(int c) {
- if (c >= 'a' && c <= 'z') {
- return true;
- }
- if (c >= 'A' && c <= 'Z') {
- return true;
- }
- if (c >= '0' && c <= '9') {
- return true;
- }
- if (c == '-' || c == '_' || c == '.' || c == '!' ||
- c == '~' || c == '*' || c == '\'' || c == '(' || c == ')') {
- return true;
+ cs = Charset.forName(enc);
+ } catch (Throwable t) {
+ ExceptionUtils.handleThrowable(t);
+ cs = StandardCharsets.ISO_8859_1;
}
- return false;
+ return URLEncoder.encode(s, cs);
}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index b2d6b20990..39285b90a7 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -188,6 +188,10 @@
21 being the minimum Java version required for Tomcat 12. (markt)
</update>
<!-- Entries for backport and removal before 12.0.0-M1 below this line
-->
+ <scode>
+ Replace custom URL encoding provided by the JSP runtime library with
+ calls to <code>java.net.URLEncoder.encode()</code>. (markt)
+ </scode>
</changelog>
</subsection>
<subsection name="Cluster">
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]