From 4df89ddef46edd144e5389eff6b145b76daa2e0f Mon Sep 17 00:00:00 2001 From: kares Date: Wed, 27 Oct 2021 11:28:24 +0200 Subject: [PATCH 1/5] [jruby][refactor] avoid byte[] copy-ing --- ext/puma_http11/org/jruby/puma/MiniSSL.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ext/puma_http11/org/jruby/puma/MiniSSL.java b/ext/puma_http11/org/jruby/puma/MiniSSL.java index 243adf8e41..dfde588638 100644 --- a/ext/puma_http11/org/jruby/puma/MiniSSL.java +++ b/ext/puma_http11/org/jruby/puma/MiniSSL.java @@ -80,11 +80,11 @@ public ByteBuffer getRawBuffer() { /** * Writes bytes to the buffer after ensuring there's room */ - public void put(byte[] bytes) { - if (buffer.remaining() < bytes.length) { - resize(buffer.limit() + bytes.length); + private void put(byte[] bytes, final int offset, final int length) { + if (buffer.remaining() < length) { + resize(buffer.limit() + length); } - buffer.put(bytes); + buffer.put(bytes, offset, length); } /** @@ -231,8 +231,8 @@ public IRubyObject initialize(ThreadContext threadContext, IRubyObject miniSSLCo @JRubyMethod public IRubyObject inject(IRubyObject arg) { try { - byte[] bytes = arg.convertToString().getBytes(); - inboundNetData.put(bytes); + ByteList bytes = arg.convertToString().getByteList(); + inboundNetData.put(bytes.unsafeBytes(), bytes.getBegin(), bytes.getRealSize()); return this; } catch (Exception e) { e.printStackTrace(); From f21eeeb758ec83fa0a5f59766332877108250eb2 Mon Sep 17 00:00:00 2001 From: kares Date: Wed, 27 Oct 2021 11:29:25 +0200 Subject: [PATCH 2/5] [jruby][refactor] proper ASCII string creation --- ext/puma_http11/org/jruby/puma/MiniSSL.java | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/ext/puma_http11/org/jruby/puma/MiniSSL.java b/ext/puma_http11/org/jruby/puma/MiniSSL.java index dfde588638..d11862ff17 100644 --- a/ext/puma_http11/org/jruby/puma/MiniSSL.java +++ b/ext/puma_http11/org/jruby/puma/MiniSSL.java @@ -297,7 +297,7 @@ private SSLEngineResult doOp(SSLOperation sslOp, MiniSSLBuffer src, MiniSSLBuffe } @JRubyMethod - public IRubyObject read() throws Exception { + public IRubyObject read() { try { inboundNetData.flip(); @@ -342,9 +342,7 @@ public IRubyObject read() throws Exception { return getRuntime().getNil(); } - RubyString str = getRuntime().newString(""); - str.setValue(appDataByteList); - return str; + return RubyString.newString(getRuntime(), appDataByteList); } catch (Exception e) { throw getRuntime().newEOFError(e.getMessage()); } @@ -364,30 +362,25 @@ public IRubyObject write(IRubyObject arg) { } @JRubyMethod - public IRubyObject extract() throws SSLException { + public IRubyObject extract(ThreadContext context) { try { ByteList dataByteList = outboundNetData.asByteList(); if (dataByteList != null) { - RubyString str = getRuntime().newString(""); - str.setValue(dataByteList); - return str; + return RubyString.newString(context.runtime, dataByteList); } if (!outboundAppData.hasRemaining()) { - return getRuntime().getNil(); + return context.nil; } outboundNetData.clear(); doOp(SSLOperation.WRAP, outboundAppData, outboundNetData); dataByteList = outboundNetData.asByteList(); if (dataByteList == null) { - return getRuntime().getNil(); + return context.nil; } - RubyString str = getRuntime().newString(""); - str.setValue(dataByteList); - - return str; + return RubyString.newString(context.runtime, dataByteList); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); From 59df6462ed3e09c1362443eaa183b6eb88d62587 Mon Sep 17 00:00:00 2001 From: kares Date: Wed, 27 Oct 2021 11:36:38 +0200 Subject: [PATCH 3/5] [jruby][refactor] drop unused KeyStore instances --- ext/puma_http11/org/jruby/puma/MiniSSL.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/ext/puma_http11/org/jruby/puma/MiniSSL.java b/ext/puma_http11/org/jruby/puma/MiniSSL.java index d11862ff17..8bf74cf193 100644 --- a/ext/puma_http11/org/jruby/puma/MiniSSL.java +++ b/ext/puma_http11/org/jruby/puma/MiniSSL.java @@ -174,8 +174,6 @@ public static synchronized IRubyObject server(ThreadContext context, IRubyObject @JRubyMethod public IRubyObject initialize(ThreadContext threadContext, IRubyObject miniSSLContext) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { - KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); - KeyStore ts = KeyStore.getInstance(KeyStore.getDefaultType()); String keystoreFile = miniSSLContext.callMethod(threadContext, "keystore").convertToString().asJavaString(); KeyManagerFactory kmf = keyManagerFactoryMap.get(keystoreFile); From b36e32ad9fc44728fd51f721737e9cfe334c36e7 Mon Sep 17 00:00:00 2001 From: kares Date: Wed, 27 Oct 2021 11:37:50 +0200 Subject: [PATCH 4/5] [jruby][refactor] review exception handling (catch less) --- ext/puma_http11/org/jruby/puma/MiniSSL.java | 38 +++++++++------------ 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/ext/puma_http11/org/jruby/puma/MiniSSL.java b/ext/puma_http11/org/jruby/puma/MiniSSL.java index 8bf74cf193..bbe80a396f 100644 --- a/ext/puma_http11/org/jruby/puma/MiniSSL.java +++ b/ext/puma_http11/org/jruby/puma/MiniSSL.java @@ -6,6 +6,7 @@ import org.jruby.RubyObject; import org.jruby.RubyString; import org.jruby.anno.JRubyMethod; +import org.jruby.exceptions.RaiseException; import org.jruby.javasupport.JavaEmbedUtils; import org.jruby.runtime.Block; import org.jruby.runtime.ObjectAllocator; @@ -228,14 +229,9 @@ public IRubyObject initialize(ThreadContext threadContext, IRubyObject miniSSLCo @JRubyMethod public IRubyObject inject(IRubyObject arg) { - try { - ByteList bytes = arg.convertToString().getByteList(); - inboundNetData.put(bytes.unsafeBytes(), bytes.getBegin(), bytes.getRealSize()); - return this; - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException(e); - } + ByteList bytes = arg.convertToString().getByteList(); + inboundNetData.put(bytes.unsafeBytes(), bytes.getBegin(), bytes.getRealSize()); + return this; } private enum SSLOperation { @@ -341,22 +337,19 @@ public IRubyObject read() { } return RubyString.newString(getRuntime(), appDataByteList); - } catch (Exception e) { - throw getRuntime().newEOFError(e.getMessage()); + } catch (SSLException e) { + RaiseException re = getRuntime().newEOFError(e.getMessage()); + re.initCause(e); + throw re; } } @JRubyMethod public IRubyObject write(IRubyObject arg) { - try { - byte[] bls = arg.convertToString().getBytes(); - outboundAppData = new MiniSSLBuffer(bls); + byte[] bls = arg.convertToString().getBytes(); + outboundAppData = new MiniSSLBuffer(bls); - return getRuntime().newFixnum(bls.length); - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException(e); - } + return getRuntime().newFixnum(bls.length); } @JRubyMethod @@ -379,9 +372,10 @@ public IRubyObject extract(ThreadContext context) { } return RubyString.newString(context.runtime, dataByteList); - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException(e); + } catch (SSLException e) { + RaiseException ex = context.runtime.newRuntimeError(e.toString()); + ex.initCause(e); + throw ex; } } @@ -389,7 +383,7 @@ public IRubyObject extract(ThreadContext context) { public IRubyObject peercert() throws CertificateEncodingException { try { return JavaEmbedUtils.javaToRuby(getRuntime(), engine.getSession().getPeerCertificates()[0].getEncoded()); - } catch (SSLPeerUnverifiedException ex) { + } catch (SSLPeerUnverifiedException e) { return getRuntime().getNil(); } } From 2719840224d4f0d9aff5a27b9d671d290c605b2d Mon Sep 17 00:00:00 2001 From: kares Date: Wed, 27 Oct 2021 13:56:17 +0200 Subject: [PATCH 5/5] [jruby][refactor] no need to copy local byte[] array --- ext/puma_http11/org/jruby/puma/MiniSSL.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/puma_http11/org/jruby/puma/MiniSSL.java b/ext/puma_http11/org/jruby/puma/MiniSSL.java index bbe80a396f..6371e9908d 100644 --- a/ext/puma_http11/org/jruby/puma/MiniSSL.java +++ b/ext/puma_http11/org/jruby/puma/MiniSSL.java @@ -116,7 +116,7 @@ public ByteList asByteList() { buffer.get(bss); buffer.clear(); - return new ByteList(bss); + return new ByteList(bss, false); } @Override