You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by sc...@apache.org on 2023/10/19 01:46:27 UTC

[tomcat] 04/04: Add a "chomp" capability to optionally remove a trailing newline from a file-based value.

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

schultz pushed a commit to branch 8.5.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit a1b54130f240c5daf68518d2682d636d6efde2ed
Author: Christopher Schultz <ch...@christopherschultz.net>
AuthorDate: Wed Oct 18 21:31:50 2023 -0400

    Add a "chomp" capability to optionally remove a trailing newline from a file-based value.
---
 .../digester/ServiceBindingPropertySource.java     | 34 +++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/tomcat/util/digester/ServiceBindingPropertySource.java b/java/org/apache/tomcat/util/digester/ServiceBindingPropertySource.java
index fd882fcf63..a96e5d8285 100644
--- a/java/org/apache/tomcat/util/digester/ServiceBindingPropertySource.java
+++ b/java/org/apache/tomcat/util/digester/ServiceBindingPropertySource.java
@@ -42,6 +42,7 @@ import org.apache.tomcat.util.security.PermissionCheck;
  *                                            /keyFile
  *                                            /file
  *                                            /chainFile
+ *                                            /keyPassword
  * </pre>
  * <pre>
  *   {@code
@@ -49,10 +50,19 @@ import org.apache.tomcat.util.security.PermissionCheck;
  *           <Certificate certificateKeyFile="${custom-certificate.keyFile}"
  *                        certificateFile="${custom-certificate.file}"
  *                        certificateChainFile="${custom-certificate.chainFile}"
+ *                        certificateKeyPassword="${chomp:custom-certificate.keyPassword}"
  *                        type="RSA" />
  *     </SSLHostConfig> }
  * </pre>
  *
+ * <p>
+ *   The optional <code>chomp:</code> prefix will cause the ServiceBindingPropertySource
+ *   to trim a single newline (<code>\r\n</code>, <code>\r</code>, or <code>\n</code>)
+ *   from the end of the file, if it exists. This is a convenience for hand-edited
+ *   files/values where removing a trailing newline is difficult, and trailing
+ *   whitespace changes the meaning of the value.
+ * </p>
+ *
  * How to configure:
  * <pre>
  * {@code
@@ -98,6 +108,12 @@ public class ServiceBindingPropertySource implements IntrospectionUtils.SecurePr
             return null;
         }
 
+        boolean chomp = false;
+        if (key.startsWith("chomp:")) {
+            chomp = true;
+            key = key.substring(6); // Remove the "chomp:" prefix
+        }
+
         // we expect the keys to be in the format $SERVICE_BINDING_ROOT/<binding-name>/<key>
         String[] parts = key.split("\\.");
         if (parts.length != 2) {
@@ -117,7 +133,23 @@ public class ServiceBindingPropertySource implements IntrospectionUtils.SecurePr
                     return null;
                 }
             }
-            return new String(Files.readAllBytes(path));
+
+            byte[] bytes = Files.readAllBytes(path);
+
+            int length = bytes.length;
+
+            if (chomp) {
+                if(length > 1 && bytes[length - 2] == '\r' && bytes[length - 2] == '\n') {
+                    length -= 2;
+                } else if (length > 0) {
+                    byte c = bytes[length - 1];
+                    if (c == '\r' || c == '\n') {
+                        length -= 1;
+                    }
+                }
+            }
+
+            return new String(bytes, 0, length);
         } catch (IOException e) {
             return null;
         }


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