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

[jruby] a couple refactorings - avoid copy-ing bytes #2730

Merged
merged 5 commits into from Nov 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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