You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2021/03/25 19:47:38 UTC

[mina-sshd] 02/05: [SSHD-1132] Added SFTP client side support for filename-charset extension

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

lgoldstein pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mina-sshd.git

commit 6c0644264fd6e35aaa83ec1fe7374e0de822766f
Author: Lyor Goldstein <lg...@apache.org>
AuthorDate: Tue Mar 23 11:06:06 2021 +0200

    [SSHD-1132] Added SFTP client side support for filename-charset extension
---
 CHANGES.md                                         |   1 +
 .../org/apache/sshd/sftp/common/SftpConstants.java |   1 +
 .../common/extensions/FilenameCharsetParser.java   | 108 +++++++++++++++++++++
 .../sshd/sftp/common/extensions/ParserUtils.java   |   1 +
 4 files changed, 111 insertions(+)

diff --git a/CHANGES.md b/CHANGES.md
index 0255d8d..6aef290 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -48,6 +48,7 @@
 * [SSHD-1123](https://issues.apache.org/jira/browse/SSHD-1123) Add option to chunk data in ChannelAsyncOutputStream if window size is smaller than packet size
 * [SSHD-1125](https://issues.apache.org/jira/browse/SSHD-1125) Added mechanism to throttle pending write requests in BufferedIoOutputStream
 * [SSHD-1127](https://issues.apache.org/jira/browse/SSHD-1127) Added capability to register a custom receiver for SFTP STDERR channel raw or stream data
+* [SSHD-1132](https://issues.apache.org/jira/browse/SSHD-1132) Added SFTP client-side support for 'filename-charset' extension
 * [SSHD-1133](https://issues.apache.org/jira/browse/SSHD-1133) Added capability to specify a custom charset for parsing incoming commands to the `ScpShell`
 * [SSHD-1133](https://issues.apache.org/jira/browse/SSHD-1133) Added capability to specify a custom charset for returning environment variables related data from the `ScpShell`
 * [SSHD-1133](https://issues.apache.org/jira/browse/SSHD-1133) Added capability to specify a custom charset for handling the SCP protocol textual commands and responses
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/common/SftpConstants.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/common/SftpConstants.java
index f7b7c86..8f3ed6e 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/common/SftpConstants.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/common/SftpConstants.java
@@ -250,6 +250,7 @@ public final class SftpConstants {
     public static final String EXT_TEXT_SEEK = "text-seek";
     public static final String EXT_VERSION_SELECT = "version-select";
     public static final String EXT_COPY_FILE = "copy-file";
+    public static final String EXT_FILENAME_CHARSET = "filename-charset";
 
     public static final String EXT_MD5_HASH = "md5-hash";
     public static final String EXT_MD5_HASH_HANDLE = "md5-hash-handle";
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/common/extensions/FilenameCharsetParser.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/common/extensions/FilenameCharsetParser.java
new file mode 100644
index 0000000..2bed4da
--- /dev/null
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/common/extensions/FilenameCharsetParser.java
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.sshd.sftp.common.extensions;
+
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.sftp.common.SftpConstants;
+import org.apache.sshd.sftp.common.extensions.FilenameCharsetParser.FilenameCharset;
+
+/**
+ * Parses the &quot;filename-charset&quot; extension
+ *
+ * @see    <A HREF="https://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#page-16">DRAFT 13 - page-16</A>
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public class FilenameCharsetParser extends AbstractParser<FilenameCharset> {
+    /**
+     * Encapsulates the &quot;filename-charset&quot; extension information
+     *
+     * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+     */
+    public static class FilenameCharset implements Serializable, Cloneable {
+        private static final long serialVersionUID = -4848766176935392024L;
+
+        private String charset;
+
+        public FilenameCharset() {
+            this(null);
+        }
+
+        public FilenameCharset(String charset) {
+            this.charset = charset;
+        }
+
+        public String getCharset() {
+            return charset;
+        }
+
+        public void setCharset(String charset) {
+            this.charset = charset;
+        }
+
+        @Override
+        public FilenameCharset clone() {
+            try {
+                return getClass().cast(super.clone());
+            } catch (CloneNotSupportedException e) {
+                throw new RuntimeException("Failed to clone " + toString() + ": " + e.getMessage(), e);
+            }
+        }
+
+        @Override
+        public int hashCode() {
+            return GenericUtils.hashCode(getCharset(), Boolean.TRUE);
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (o == this) {
+                return true;
+            }
+            if ((o == null) || (o.getClass() != getClass())) {
+                return false;
+            }
+
+            return GenericUtils.safeCompare(this.getCharset(), ((FilenameCharset) o).getCharset(), false) == 0;
+        }
+
+        @Override
+        public String toString() {
+            return getCharset();
+        }
+    }
+
+    public static final FilenameCharsetParser INSTANCE = new FilenameCharsetParser();
+
+    public FilenameCharsetParser() {
+        super(SftpConstants.EXT_FILENAME_CHARSET);
+    }
+
+    @Override
+    public FilenameCharset parse(byte[] input, int offset, int len) {
+        return parse(new String(input, offset, len, StandardCharsets.UTF_8));
+    }
+
+    public FilenameCharset parse(String s) {
+        return new FilenameCharset(s);
+    }
+}
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/common/extensions/ParserUtils.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/common/extensions/ParserUtils.java
index d048aa6..5e28cee 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/common/extensions/ParserUtils.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/common/extensions/ParserUtils.java
@@ -58,6 +58,7 @@ public final class ParserUtils {
                     SupportedParser.INSTANCE,
                     Supported2Parser.INSTANCE,
                     AclSupportedParser.INSTANCE,
+                    FilenameCharsetParser.INSTANCE,
                     // OpenSSH extensions
                     PosixRenameExtensionParser.INSTANCE,
                     StatVfsExtensionParser.INSTANCE,