Skip to content

Commit

Permalink
[jruby] a couple refactorings - avoid copy-ing bytes (puma#2730)
Browse files Browse the repository at this point in the history
* [jruby][refactor] avoid byte[] copy-ing

* [jruby][refactor] proper ASCII string creation

* [jruby][refactor] drop unused KeyStore instances

* [jruby][refactor] review exception handling (catch less)

* [jruby][refactor] no need to copy local byte[] array
  • Loading branch information
kares authored and JuanitoFatas committed Sep 9, 2022
1 parent 7d05f56 commit 79f3f6d
Showing 1 changed file with 28 additions and 43 deletions.
71 changes: 28 additions & 43 deletions ext/puma_http11/org/jruby/puma/MiniSSL.java
Expand Up @@ -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;
Expand Down Expand Up @@ -80,11 +81,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);
}

/**
Expand Down Expand Up @@ -115,7 +116,7 @@ public ByteList asByteList() {

buffer.get(bss);
buffer.clear();
return new ByteList(bss);
return new ByteList(bss, false);
}

@Override
Expand Down Expand Up @@ -174,8 +175,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);
Expand Down Expand Up @@ -230,14 +229,9 @@ public IRubyObject initialize(ThreadContext threadContext, IRubyObject miniSSLCo

@JRubyMethod
public IRubyObject inject(IRubyObject arg) {
try {
byte[] bytes = arg.convertToString().getBytes();
inboundNetData.put(bytes);
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 {
Expand Down Expand Up @@ -297,7 +291,7 @@ private SSLEngineResult doOp(SSLOperation sslOp, MiniSSLBuffer src, MiniSSLBuffe
}

@JRubyMethod
public IRubyObject read() throws Exception {
public IRubyObject read() {
try {
inboundNetData.flip();

Expand Down Expand Up @@ -342,63 +336,54 @@ public IRubyObject read() throws Exception {
return getRuntime().getNil();
}

RubyString str = getRuntime().newString("");
str.setValue(appDataByteList);
return str;
} catch (Exception e) {
throw getRuntime().newEOFError(e.getMessage());
return RubyString.newString(getRuntime(), appDataByteList);
} 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
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;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
return RubyString.newString(context.runtime, dataByteList);
} catch (SSLException e) {
RaiseException ex = context.runtime.newRuntimeError(e.toString());
ex.initCause(e);
throw ex;
}
}

@JRubyMethod
public IRubyObject peercert() throws CertificateEncodingException {
try {
return JavaEmbedUtils.javaToRuby(getRuntime(), engine.getSession().getPeerCertificates()[0].getEncoded());
} catch (SSLPeerUnverifiedException ex) {
} catch (SSLPeerUnverifiedException e) {
return getRuntime().getNil();
}
}
Expand Down

0 comments on commit 79f3f6d

Please sign in to comment.