-
Notifications
You must be signed in to change notification settings - Fork 601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added charset support, centralized UTF-8 usage #305
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,7 @@ | |
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
import java.nio.charset.Charset; | ||
import java.security.KeyPair; | ||
import java.security.PublicKey; | ||
import java.util.*; | ||
|
@@ -128,6 +129,9 @@ public class SSHClient | |
|
||
private final List<LocalPortForwarder> forwarders = new ArrayList<LocalPortForwarder>(); | ||
|
||
/** character set of the remote machine */ | ||
protected Charset remoteCharset; | ||
|
||
/** Default constructor. Initializes this object using {@link DefaultConfig}. */ | ||
public SSHClient() { | ||
this(new DefaultConfig()); | ||
|
@@ -440,6 +444,16 @@ public Connection getConnection() { | |
return conn; | ||
} | ||
|
||
/** | ||
* Returns the character set used to communicate with the remote machine for certain strings (like paths). | ||
* | ||
* @return remote character set or {@code null} for default | ||
*/ | ||
public Charset getRemoteCharset() | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use the same formatting as the rest of the code, ie. open-curly on end-of-line |
||
return remoteCharset; | ||
} | ||
|
||
/** @return a {@link RemotePortForwarder} that allows requesting remote forwarding over this connection. */ | ||
public RemotePortForwarder getRemotePortForwarder() { | ||
synchronized (conn) { | ||
|
@@ -708,12 +722,23 @@ public void rekey() | |
doKex(); | ||
} | ||
|
||
/** | ||
* Sets the character set used to communicate with the remote machine for certain strings (like paths) | ||
* | ||
* @param remoteCharset | ||
* remote character set or {@code null} for default | ||
*/ | ||
public void setRemoteCharset(Charset remoteCharset) | ||
{ | ||
this.remoteCharset = remoteCharset; | ||
} | ||
|
||
@Override | ||
public Session startSession() | ||
throws ConnectionException, TransportException { | ||
checkConnected(); | ||
checkAuthenticated(); | ||
final SessionChannel sess = new SessionChannel(conn); | ||
final SessionChannel sess = (remoteCharset != null)? new SessionChannel(conn, remoteCharset) : new SessionChannel(conn); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to do the if in the case that we default to UTF_8 here already. |
||
sess.open(); | ||
return sess; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
import net.schmizz.sshj.transport.TransportException; | ||
|
||
import java.io.InputStream; | ||
import java.nio.charset.Charset; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
@@ -47,6 +48,10 @@ public SessionChannel(Connection conn) { | |
super(conn, "session"); | ||
} | ||
|
||
public SessionChannel(Connection conn, Charset remoteCharset) { | ||
super(conn, "session", remoteCharset); | ||
} | ||
|
||
@Override | ||
public void allocateDefaultPTY() | ||
throws ConnectionException, TransportException { | ||
|
@@ -93,7 +98,7 @@ public Command exec(String command) | |
throws ConnectionException, TransportException { | ||
checkReuse(); | ||
log.debug("Will request to exec `{}`", command); | ||
sendChannelRequest("exec", true, new Buffer.PlainBuffer().putString(command)) | ||
sendChannelRequest("exec", true, new Buffer.PlainBuffer().putString(command.getBytes(getRemoteCharset()))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main question I have when reading this, should every string be written using the remote charset? If so, it is nicer to make it a property of the Buffer, rather than converting it everywhere we call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or make a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The remote side seems to decode the command using its own charset instead of utf-8. Commands containing filenames with special characters won't work properly using utf-8 on non-utf-8 remote machines. |
||
.await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS); | ||
usedUp = true; | ||
return this; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assign the default
IOUtils.UTF_8
here.