Skip to content

Commit

Permalink
Fix Socket.gethostname and IPSocket.getaddress encoding.
Browse files Browse the repository at this point in the history
Fixes #2558.
  • Loading branch information
headius committed Jul 2, 2015
1 parent cca1a6f commit 0429799
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/org/jruby/ext/socket/SocketUtils.java
Expand Up @@ -76,12 +76,12 @@ public static IRubyObject gethostname(ThreadContext context) {
Ruby runtime = context.runtime;

try {
return runtime.newString(InetAddress.getLocalHost().getHostName());
return RubyString.newInternalFromJavaExternal(context.runtime, InetAddress.getLocalHost().getHostName());

} catch(UnknownHostException e) {

try {
return runtime.newString(InetAddress.getByAddress(new byte[]{0,0,0,0}).getHostName());
return RubyString.newInternalFromJavaExternal(context.runtime, InetAddress.getByAddress(new byte[]{0, 0, 0, 0}).getHostName());

} catch(UnknownHostException e2) {
throw sockerr(runtime, "gethostname: name or service not known");
Expand Down Expand Up @@ -510,7 +510,7 @@ public static InetAddress getRubyInetAddress(ByteList address) throws UnknownHos

public static IRubyObject getaddress(ThreadContext context, IRubyObject hostname) {
try {
return context.runtime.newString(InetAddress.getByName(hostname.convertToString().toString()).getHostAddress());
return RubyString.newInternalFromJavaExternal(context.runtime, InetAddress.getByName(hostname.convertToString().toString()).getHostAddress());
} catch(UnknownHostException e) {
throw sockerr(context.runtime, "getaddress: name or service not known");
}
Expand Down
@@ -0,0 +1,15 @@
require 'socket'

if RUBY_VERSION >= '1.9'
describe "Socket.gethostname" do
it "returns current host name with UTF-8 encoding" do
Socket.gethostname.encoding.should == Encoding::UTF_8
end
end

describe "IPSocket.getaddress" do
it "returns hostname for specified address with UTF-8 encoding" do
IPSocket.getaddress('localhost').encoding.should == Encoding::UTF_8
end
end
end

0 comments on commit 0429799

Please sign in to comment.