1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Merge pull request #761 from looker/no-log

Eliminate logging overhead from JRuby SSL
This commit is contained in:
Evan Phoenix 2015-08-14 16:22:08 -07:00
commit 4163c13a15

View file

@ -1,7 +1,6 @@
package org.jruby.puma; package org.jruby.puma;
import org.jruby.Ruby; import org.jruby.Ruby;
import org.jruby.RubyBoolean;
import org.jruby.RubyClass; import org.jruby.RubyClass;
import org.jruby.RubyModule; import org.jruby.RubyModule;
import org.jruby.RubyObject; import org.jruby.RubyObject;
@ -39,9 +38,6 @@ public class MiniSSL extends RubyObject {
} }
}; };
// set to true to switch on our low-fi trace logging
private static boolean DEBUG = false;
public static void createMiniSSL(Ruby runtime) { public static void createMiniSSL(Ruby runtime) {
RubyModule mPuma = runtime.defineModule("Puma"); RubyModule mPuma = runtime.defineModule("Puma");
RubyModule ssl = mPuma.defineModuleUnder("MiniSSL"); RubyModule ssl = mPuma.defineModuleUnder("MiniSSL");
@ -170,12 +166,7 @@ public class MiniSSL extends RubyObject {
public IRubyObject inject(IRubyObject arg) { public IRubyObject inject(IRubyObject arg) {
try { try {
byte[] bytes = arg.convertToString().getBytes(); byte[] bytes = arg.convertToString().getBytes();
log("Net Data post pre-inject: " + inboundNetData);
inboundNetData.put(bytes); inboundNetData.put(bytes);
log("Net Data post post-inject: " + inboundNetData);
log("inject(): " + bytes.length + " encrypted bytes from request");
return this; return this;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -205,8 +196,6 @@ public class MiniSSL extends RubyObject {
switch (res.getStatus()) { switch (res.getStatus()) {
case BUFFER_OVERFLOW: case BUFFER_OVERFLOW:
log("SSLOp#doRun(): overflow");
log("SSLOp#doRun(): dst data at overflow: " + dst);
// increase the buffer size to accommodate the overflowing data // increase the buffer size to accommodate the overflowing data
int newSize = Math.max(engine.getSession().getPacketBufferSize(), engine.getSession().getApplicationBufferSize()); int newSize = Math.max(engine.getSession().getPacketBufferSize(), engine.getSession().getApplicationBufferSize());
dst.resize(newSize + dst.position()); dst.resize(newSize + dst.position());
@ -214,8 +203,6 @@ public class MiniSSL extends RubyObject {
retryOp = true; retryOp = true;
break; break;
case BUFFER_UNDERFLOW: case BUFFER_UNDERFLOW:
log("SSLOp#doRun(): underflow");
log("SSLOp#doRun(): src data at underflow: " + src);
// need to wait for more data to come in before we retry // need to wait for more data to come in before we retry
retryOp = false; retryOp = false;
break; break;
@ -245,25 +232,18 @@ public class MiniSSL extends RubyObject {
return getRuntime().getNil(); return getRuntime().getNil();
} }
log("read(): inboundNetData prepped for read: " + inboundNetData);
MiniSSLBuffer inboundAppData = new MiniSSLBuffer(engine.getSession().getApplicationBufferSize()); MiniSSLBuffer inboundAppData = new MiniSSLBuffer(engine.getSession().getApplicationBufferSize());
SSLEngineResult res = doOp(SSLOperation.UNWRAP, inboundNetData, inboundAppData); doOp(SSLOperation.UNWRAP, inboundNetData, inboundAppData);
log("read(): after initial unwrap", engine, res);
log("read(): Net Data post unwrap: " + inboundNetData);
HandshakeStatus handshakeStatus = engine.getHandshakeStatus(); HandshakeStatus handshakeStatus = engine.getHandshakeStatus();
boolean done = false; boolean done = false;
while (!done) { while (!done) {
switch (handshakeStatus) { switch (handshakeStatus) {
case NEED_WRAP: case NEED_WRAP:
res = doOp(SSLOperation.WRAP, inboundAppData, outboundNetData); doOp(SSLOperation.WRAP, inboundAppData, outboundNetData);
log("read(): after handshake wrap", engine, res);
break; break;
case NEED_UNWRAP: case NEED_UNWRAP:
res = doOp(SSLOperation.UNWRAP, inboundNetData, inboundAppData); SSLEngineResult res = doOp(SSLOperation.UNWRAP, inboundNetData, inboundAppData);
log("read(): after handshake unwrap", engine, res);
if (res.getStatus() == Status.BUFFER_UNDERFLOW) { if (res.getStatus() == Status.BUFFER_UNDERFLOW) {
// need more data before we can shake more hands // need more data before we can shake more hands
done = true; done = true;
@ -276,13 +256,9 @@ public class MiniSSL extends RubyObject {
} }
if (inboundNetData.hasRemaining()) { if (inboundNetData.hasRemaining()) {
log("Net Data post pre-compact: " + inboundNetData);
inboundNetData.compact(); inboundNetData.compact();
log("Net Data post post-compact: " + inboundNetData);
} else { } else {
log("Net Data post pre-reset: " + inboundNetData);
inboundNetData.clear(); inboundNetData.clear();
log("Net Data post post-reset: " + inboundNetData);
} }
ByteList appDataByteList = inboundAppData.asByteList(); ByteList appDataByteList = inboundAppData.asByteList();
@ -292,54 +268,15 @@ public class MiniSSL extends RubyObject {
RubyString str = getRuntime().newString(""); RubyString str = getRuntime().newString("");
str.setValue(appDataByteList); str.setValue(appDataByteList);
logPlain("\n");
log("read(): begin dump of request data >>>>\n");
if (str.asJavaString().getBytes().length < 1000) {
logPlain(str.asJavaString() + "\n");
}
logPlain("Num bytes: " + str.asJavaString().getBytes().length + "\n");
log("read(): end dump of request data <<<<\n");
return str; return str;
} catch (Exception e) { } catch (Exception e) {
if (DEBUG) {
e.printStackTrace();
}
throw getRuntime().newEOFError(e.getMessage()); throw getRuntime().newEOFError(e.getMessage());
} }
} }
private static void log(String str, SSLEngine engine, SSLEngineResult result) {
if (DEBUG) {
log(str + " " + result.getStatus() + "/" + engine.getHandshakeStatus() +
"---bytes consumed: " + result.bytesConsumed() +
", bytes produced: " + result.bytesProduced());
}
}
private static void log(String str) {
if (DEBUG) {
System.out.println("MiniSSL.java: " + str);
}
}
private static void logPlain(String str) {
if (DEBUG) {
System.out.println(str);
}
}
@JRubyMethod @JRubyMethod
public IRubyObject write(IRubyObject arg) { public IRubyObject write(IRubyObject arg) {
try { try {
log("write(): begin dump of response data >>>>\n");
logPlain("\n");
if (arg.asJavaString().getBytes().length < 1000) {
logPlain(arg.asJavaString() + "\n");
}
logPlain("Num bytes: " + arg.asJavaString().getBytes().length + "\n");
log("write(): end dump of response data <<<<\n");
byte[] bls = arg.convertToString().getBytes(); byte[] bls = arg.convertToString().getBytes();
outboundAppData = new MiniSSLBuffer(bls); outboundAppData = new MiniSSLBuffer(bls);
@ -365,9 +302,7 @@ public class MiniSSL extends RubyObject {
} }
outboundNetData.clear(); outboundNetData.clear();
SSLEngineResult res = doOp(SSLOperation.WRAP, outboundAppData, outboundNetData); doOp(SSLOperation.WRAP, outboundAppData, outboundNetData);
log("extract(): bytes consumed: " + res.bytesConsumed() + "\n");
log("extract(): bytes produced: " + res.bytesProduced() + "\n");
dataByteList = outboundNetData.asByteList(); dataByteList = outboundNetData.asByteList();
if (dataByteList == null) { if (dataByteList == null) {
return getRuntime().getNil(); return getRuntime().getNil();
@ -376,8 +311,6 @@ public class MiniSSL extends RubyObject {
RubyString str = getRuntime().newString(""); RubyString str = getRuntime().newString("");
str.setValue(dataByteList); str.setValue(dataByteList);
log("extract(): " + dataByteList.getRealSize() + " encrypted bytes for response");
return str; return str;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();