Skip to content
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

Socket.gethostname ignores encoding settings #2558

Closed
jsvd opened this issue Feb 4, 2015 · 10 comments
Closed

Socket.gethostname ignores encoding settings #2558

jsvd opened this issue Feb 4, 2015 · 10 comments

Comments

@jsvd
Copy link

@jsvd jsvd commented Feb 4, 2015

tested on windows but also happens on macosx. I can't make Socket.gethostname return a UTF-8 string even when fiddling with Encoding.default_internal and external settings.

Example:

C:\Users\Jsvd>irb
io/console not supported; tty will not be manipulated
irb(main):001:0>  puts ''.encoding; require 'socket'; puts Socket.gethostname.encoding
Windows-1252
Windows-1252
=> nil
irb(main):002:0>

C:\Users\Jsvd>irb -EUTF-8
io/console not supported; tty will not be manipulated
irb(main):001:0> puts ''.encoding; require 'socket'; puts Socket.gethostname.encoding
UTF-8
Windows-1252
=> nil
irb(main):002:0>

C:\Users\Jsvd>jruby -v
jruby 1.7.17 (1.9.3p392) 2014-12-09 fafd1a7 on Java HotSpot(TM) 64-Bit Server VM 1.8.0_31-b13 +jit [Windows 7-amd64]
@jordansissel
Copy link
Contributor

@jordansissel jordansissel commented Feb 5, 2015

MRI 2.1.4 also has this bug.

@headius
Copy link
Member

@headius headius commented Feb 24, 2015

I believe this would fix it. I noticed gethostbyname and others are not using this path either.

If someone can confirm this fix (I'm on Windows and struggling with dev env at the moment), we can get it merged in.

diff --git a/core/src/main/java/org/jruby/ext/socket/SocketUtils.java b/core/src/main/java/org/jruby/ext/socket/SocketUtils.java        
index b1a457e..65110a7 100644                                                                                                           
--- a/core/src/main/java/org/jruby/ext/socket/SocketUtils.java                                                                          
+++ b/core/src/main/java/org/jruby/ext/socket/SocketUtils.java                                                                          
@@ -74,12 +74,12 @@ public class SocketUtils {                                                                                          
         Ruby runtime = context.runtime;                                                                                                

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

         } catch(UnknownHostException e) {                                                                                              

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

             } catch(UnknownHostException e2) {                                                                                         
                 throw sockerr(runtime, "gethostname: name or service not known");                                                      

I'm confused why it would be Windows-1252, though. At least on 9k, the logic for runtime.newString above will always use UTF-8 as the string encoding by default.

@headius headius added this to the 9.0.0.0.pre2 milestone Feb 24, 2015
@headius headius added this to the 9.0.0.0.rc1 milestone Apr 17, 2015
@headius headius removed this from the 9.0.0.0.pre2 milestone Apr 17, 2015
@enebo enebo removed this from the JRuby 9.0.0.0.rc1 milestone Jun 10, 2015
@enebo enebo added this to the JRuby 9.0.0.0.rc2 milestone Jun 10, 2015
@enebo enebo added this to the JRuby 9.0.0.0.rc2 milestone Jun 10, 2015
@enebo enebo removed this from the JRuby 9.0.0.0.rc1 milestone Jun 10, 2015
headius added a commit that referenced this issue Jul 2, 2015
@headius headius closed this in 0429799 Jul 2, 2015
@headius
Copy link
Member

@headius headius commented Jul 2, 2015

Fixed in 9k.rc2 and 1.7.21.

@jsvd
Copy link
Author

@jsvd jsvd commented Oct 26, 2015

This is still an issue on 1.7.22

C:\Users\Jsvd>jruby -v
jruby 1.7.22 (1.9.3p551) 2015-08-20 c28f492 on Java HotSpot(TM) 64-Bit Server VM 1.8.0_31-b13 +jit [Windows 7-amd64]

C:\Users\Jsvd>jruby -S irb -EUTF-8
io/console not supported; tty will not be manipulated
irb(main):001:0> puts ''.encoding; require 'socket'; puts Socket.gethostname.encoding
UTF-8
Windows-1252
=> nil
irb(main):003:0> s = TCPServer.new(3335)
=> #<TCPServer:fd 15>
irb(main):005:0> c = TCPSocket.new("localhost", 3335)
=> #<TCPSocket:fd 16>
irb(main):006:0> c.peeraddr
=> ["AF_INET", 3335, "127.0.0.1", "127.0.0.1"]
irb(main):007:0> c.peeraddr.first.encoding
=> #<Encoding:Windows-1252>
irb(main):008:0> c.peeraddr.last.encoding
=> #<Encoding:Windows-1252>
irb(main):009:0> "".encoding
=> #<Encoding:UTF-8>
irb(main):010:0>

@jsvd
Copy link
Author

@jsvd jsvd commented Apr 12, 2016

a shout out that this is still an issue in 1.7.25-snapshot as of 12th of april 2016:

C:\projects\logstash>irb -EUTF-8
irb(main):001:0> RUBY_DESCRIPTION
=> "jruby 1.7.25-SNAPSHOT (1.9.3p551) 2016-04-12 5693183 on Java HotSpot(TM) 64-Bit Server VM 1.8.0_65-b17 +jit [Windows 7-amd64]"
irb(main):002:0> puts ''.encoding; require 'socket'; puts Socket.gethostname.encoding
UTF-8
Windows-1252
=> nil
irb(main):003:0> s = TCPServer.new(3335); c = TCPSocket.new("localhost", 3335)
=> #<TCPSocket:fd 14>
irb(main):004:0> c.peeraddr
=> ["AF_INET", 3335, "127.0.0.1", "127.0.0.1"]
irb(main):005:0> c.peeraddr.first.encoding
=> #<Encoding:Windows-1252>
irb(main):006:0> c.peeraddr.last.encoding
=> #<Encoding:Windows-1252>
irb(main):007:0> "".encoding
=> #<Encoding:UTF-8>

@nirvdrum
Copy link
Contributor

@nirvdrum nirvdrum commented Apr 13, 2016

While I agree that it's a bit confusing, neither Encoding.default_internal nor Encoding.default_external are documented to do anything with sockets. MRI also appears to ignore them for the same socket calls. Changing that behavior in JRuby would then break compatibility with MRI, as far as I can tell.

@kares
Copy link
Member

@kares kares commented Mar 31, 2020

an update here, most of these respect default_external, Socket.gethostname is still problematic :

jruby 9.2.11.1 (2.5.7) 2020-03-25 b1f55b1a40 OpenJDK 64-Bit Server VM 11+28 on 11+28 +jit [mswin32-x86_64]
2020-03-31T02:40:37.913-07:00 [main] WARN FilenoUtil : Native subprocess control requires open access to sun.nio.ch
Pass '--add-opens java.base/sun.nio.ch=org.jruby.dist' or '=org.jruby.core' to enable.
irb(main):001:0> puts ''.encoding; require 'socket'; puts Socket.gethostname.encoding
Windows-1252
Windows-1252
=> nil
irb(main):002:0>
irb(main):003:0> s = TCPServer.new(3335); c = TCPSocket.new("localhost", 3335)
=> #<TCPSocket:fd 100027>
irb(main):004:0> s
=> #<TCPServer:fd 100026>
irb(main):006:0> c.peeraddr
=> ["AF_INET", 3335, "127.0.0.1", "127.0.0.1"]
irb(main):008:0> c.peeraddr.first.encoding
=> #<Encoding:UTF-8>
irb(main):009:0> c.peeraddr.map { |addr| addr.is_a?(String) ? addr.encoding : addr }
=> [#<Encoding:UTF-8>, 3335, #<Encoding:UTF-8>, #<Encoding:UTF-8>]
irb(main):010:0> ''.encoding
=> #<Encoding:Windows-1252>
C:\jruby-9.2.11.1>bin\jruby -v -S irb -EUTF-8
jruby 9.2.11.1 (2.5.7) 2020-03-25 b1f55b1a40 OpenJDK 64-Bit Server VM 11+28 on 11+28 +jit [mswin32-x86_64]
2020-03-31T02:45:02.680-07:00 [main] WARN FilenoUtil : Native subprocess control requires open access to sun.nio.ch
Pass '--add-opens java.base/sun.nio.ch=org.jruby.dist' or '=org.jruby.core' to enable.
irb(main):001:0>  puts ''.encoding; require 'socket'; puts Socket.gethostname.encoding
UTF-8
Windows-1252
=> nil
irb(main):002:0>
irb(main):003:0> s = TCPServer.new(3335); c = TCPSocket.new("localhost", 3335)
=> #<TCPSocket:fd 100027>
irb(main):004:0> c.peeraddr
=> ["AF_INET", 3335, "127.0.0.1", "127.0.0.1"]
irb(main):005:0> c.peeraddr.first
=> "AF_INET"
irb(main):006:0> c.peeraddr.map { |addr| addr.is_a?(String) ? addr.encoding : addr }
=> [#<Encoding:UTF-8>, 3335, #<Encoding:UTF-8>, #<Encoding:UTF-8>]

@headius
Copy link
Member

@headius headius commented Mar 31, 2020

@kares And CRuby does this differently I assume? There's no examples on MRI.

@kares
Copy link
Member

@kares kares commented Apr 1, 2020

sorry, did not want to get into the hustle of installing MRI on Windows 😒
assumed JRuby could follow ~ same logic as on non-Windows for consistency...

in either case, ENV keys/values are platform specific regardless of setting external.
hostname is ASCII-8BIT (sounds like a bug) would have assumed US-ASCII if not respecting external :

C:\Ruby26-x64>bin\irb -EUTF-8
irb(main):001:0> ENV['USERNAME'].encoding
=> #<Encoding:IBM437>
irb(main):002:0> Encoding.default_external
=> #<Encoding:UTF-8>
irb(main):003:0> ENV.keys.first.encoding
=> #<Encoding:IBM437>

irb(main):005:0> require 'socket'
=> true
irb(main):006:0> Socket.gethostname
=> "MSEDGEWIN10"
irb(main):007:0> Socket.gethostname.encoding
=> #<Encoding:ASCII-8BIT>

@headius
Copy link
Member

@headius headius commented Apr 2, 2020

Still unclear whether there's a difference from MRI here. If so, someone should open a separate bug for that since the original issue here was patched and closed (though maybe not fixed the way everyone wants).

If both JRuby and MRI have the same bug here, then we'd need to work with ruby-core to determine if this should be fixed. JRuby will follow MRI's lead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

6 participants