Skip to content

Commit f432265

Browse files
committed
align SSLContext's API ... no io/context writers should exist - adapt the internals
1 parent 14806c7 commit f432265

2 files changed

Lines changed: 53 additions & 13 deletions

File tree

src/main/java/org/jruby/ext/openssl/SSLSocket.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
8888
public static void createSSLSocket(final Ruby runtime, final RubyModule SSL) { // OpenSSL::SSL
8989
final ThreadContext context = runtime.getCurrentContext();
9090
RubyClass SSLSocket = SSL.defineClassUnder("SSLSocket", runtime.getObject(), SSLSOCKET_ALLOCATOR);
91-
SSLSocket.addReadWriteAttribute(context, "io");
92-
SSLSocket.addReadWriteAttribute(context, "context");
91+
// SSLSocket.addReadAttribute(context, "io");
92+
// SSLSocket.defineAlias("to_io", "io");
93+
// SSLSocket.addReadAttribute(context, "context");
9394
SSLSocket.addReadWriteAttribute(context, "sync_close");
9495
SSLSocket.addReadWriteAttribute(context, "hostname");
95-
SSLSocket.defineAlias("to_io", "io");
9696
SSLSocket.defineAnnotatedMethods(SSLSocket.class);
9797
}
9898

@@ -156,12 +156,12 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
156156
if ( ! ( args[0] instanceof RubyIO ) ) {
157157
throw runtime.newTypeError("IO expected but got " + args[0].getMetaClass().getName());
158158
}
159-
this.callMethod(context, "io=", this.io = (RubyIO) args[0]);
160-
this.callMethod(context, "context=", sslContext);
159+
setInstanceVariable("@io", this.io = (RubyIO) args[0]); // compat (we do not read @io)
160+
setInstanceVariable("@context", this.sslContext); // only compat (we do not use @context)
161161
// This is a bit of a hack: SSLSocket should share code with
162162
// RubyBasicSocket, which always sets sync to true.
163163
// Instead we set it here for now.
164-
this.io.callMethod(context, "sync=", runtime.getTrue());
164+
this.set_sync(context, runtime.getTrue()); // io.sync = true
165165
this.callMethod(context, "sync_close=", runtime.getFalse());
166166
sslContext.setup(context);
167167
return Utils.invokeSuper(context, this, args, Block.NULL_BLOCK); // super()
@@ -175,7 +175,7 @@ private SSLEngine ossl_ssl_setup(final ThreadContext context)
175175
// Server Name Indication (SNI) RFC 3546
176176
// SNI support will not be attempted unless hostname is explicitly set by the caller
177177
String peerHost = this.callMethod(context, "hostname").toString();
178-
final int peerPort = socketChannelImpl().getSocketPort();
178+
final int peerPort = socketChannelImpl().getRemotePort();
179179
engine = sslContext.createSSLEngine(peerHost, peerPort);
180180

181181
final javax.net.ssl.SSLSession session = engine.getSession();
@@ -189,8 +189,24 @@ private SSLEngine ossl_ssl_setup(final ThreadContext context)
189189
return this.engine = engine;
190190
}
191191

192+
@JRubyMethod(name = "io", alias = "to_io")
193+
public final RubyIO io() { return this.io; }
194+
195+
@JRubyMethod(name = "context")
196+
public final SSLContext context() { return this.sslContext; }
197+
198+
@JRubyMethod(name = "sync")
199+
public IRubyObject sync(final ThreadContext context) {
200+
return this.io.callMethod(context, "sync");
201+
}
202+
203+
@JRubyMethod(name = "sync=")
204+
public IRubyObject set_sync(final ThreadContext context, final IRubyObject sync) {
205+
return this.io.callMethod(context, "sync=", sync);
206+
}
207+
192208
@JRubyMethod
193-
public IRubyObject connect(ThreadContext context) {
209+
public IRubyObject connect(final ThreadContext context) {
194210
return connectImpl(context, true);
195211
}
196212

@@ -728,7 +744,7 @@ private IRubyObject do_syswrite(final ThreadContext context,
728744
written = write(b1, blocking);
729745
}
730746

731-
this.callMethod(context, "io").callMethod(context, "flush");
747+
this.io.callMethod(context, "flush");
732748

733749
return runtime.newFixnum(written);
734750
}
@@ -787,7 +803,7 @@ public IRubyObject sysclose(final ThreadContext context) {
787803
close( sslContext.isProtocolForClient() );
788804

789805
if ( this.callMethod(context, "sync_close").isTrue() ) {
790-
this.callMethod(context, "io").callMethod(context, "close");
806+
this.io.callMethod(context, "close");
791807
}
792808
return context.runtime.getNil();
793809
}
@@ -929,7 +945,7 @@ private static interface SocketChannelImpl {
929945

930946
int write(ByteBuffer src) throws IOException ;
931947

932-
int getSocketPort() ;
948+
int getRemotePort();
933949

934950
boolean isSelectable() ;
935951

@@ -965,7 +981,7 @@ public int write(ByteBuffer src) throws IOException {
965981
return channel.write(src);
966982
}
967983

968-
public int getSocketPort() { return channel.socket().getPort(); }
984+
public int getRemotePort() { return channel.socket().getPort(); }
969985

970986
public boolean isSelectable() {
971987
return true; // return channel instanceof SelectableChannel;

src/test/ruby/ssl/test_socket.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,35 @@ def test_cipher
1111
assert_nil socket.cipher
1212
end
1313

14-
def test_hostname
14+
def test_attr_methods
1515
io_stub = File.new __FILE__
1616
socket = OpenSSL::SSL::SSLSocket.new(io_stub)
1717

18+
assert socket.io
19+
assert_equal socket.io, socket.to_io
20+
assert ! socket.respond_to?(:'io=')
21+
# due compatibility :
22+
assert_equal socket.io, socket.instance_variable_get(:@io)
23+
24+
assert socket.context
25+
assert ! socket.respond_to?(:'context=')
26+
# due compatibility :
27+
assert_equal socket.context, socket.instance_variable_get(:@context)
28+
1829
assert_nil socket.hostname
30+
socket.hostname = '1.1.1.1'
31+
assert_equal '1.1.1.1', socket.hostname
32+
33+
# MRI sync is false by default :
34+
# assert_equal false, socket.sync
35+
socket.sync = true
36+
assert_equal true, socket.sync
37+
38+
# assert_equal false, socket.sync_close
39+
socket.sync_close = true
40+
assert_equal true, socket.sync_close
41+
42+
socket.inspect
1943
end
2044

2145
end

0 commit comments

Comments
 (0)