From 5dea9b673e968a98bbeb54355fb23cb1ff458973 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Wed, 7 Mar 2018 00:45:23 +0100 Subject: [PATCH] Fixes #2291 - Expose HTTP/2 close reason in dumps. Signed-off-by: Simone Bordet --- .../org/eclipse/jetty/http2/HTTP2Session.java | 17 ++++++++++------- .../eclipse/jetty/http2/frames/GoAwayFrame.java | 13 +++++++++++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/jetty-http2/http2-common/src/main/java/org/eclipse/jetty/http2/HTTP2Session.java b/jetty-http2/http2-common/src/main/java/org/eclipse/jetty/http2/HTTP2Session.java index 4dd88c5f35e..2ebcb2dc6c1 100644 --- a/jetty-http2/http2-common/src/main/java/org/eclipse/jetty/http2/HTTP2Session.java +++ b/jetty-http2/http2-common/src/main/java/org/eclipse/jetty/http2/HTTP2Session.java @@ -89,6 +89,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio private int initialSessionRecvWindow; private boolean pushEnabled; private long idleTime; + private GoAwayFrame closeFrame; public HTTP2Session(Scheduler scheduler, EndPoint endPoint, Generator generator, Session.Listener listener, FlowControlStrategy flowControl, int initialStreamId) { @@ -430,6 +431,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio { // We received a GO_AWAY, so try to write // what's in the queue and then disconnect. + closeFrame = frame; notifyClose(this, frame, new DisconnectCallback()); return; } @@ -597,8 +599,8 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio { if (closed.compareAndSet(current, CloseState.LOCALLY_CLOSED)) { - GoAwayFrame frame = newGoAwayFrame(error, reason); - control(null, callback, frame); + closeFrame = newGoAwayFrame(CloseState.LOCALLY_CLOSED, error, reason); + control(null, callback, closeFrame); return true; } break; @@ -614,7 +616,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio } } - private GoAwayFrame newGoAwayFrame(int error, String reason) + private GoAwayFrame newGoAwayFrame(CloseState closeState, int error, String reason) { byte[] payload = null; if (reason != null) @@ -623,7 +625,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio reason = reason.substring(0, Math.min(reason.length(), 32)); payload = reason.getBytes(StandardCharsets.UTF_8); } - return new GoAwayFrame(lastStreamId.get(), error, payload); + return new GoAwayFrame(closeState, lastStreamId.get(), error, payload); } @Override @@ -1125,7 +1127,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio @Override public String toString() { - return String.format("%s@%x{l:%s <-> r:%s,sendWindow=%s,recvWindow=%s,streams=%d,%s}", + return String.format("%s@%x{l:%s <-> r:%s,sendWindow=%s,recvWindow=%s,streams=%d,%s,%s}", getClass().getSimpleName(), hashCode(), getEndPoint().getLocalAddress(), @@ -1133,7 +1135,8 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio sendWindow, recvWindow, streams.size(), - closed); + closed, + closeFrame); } private class ControlEntry extends HTTP2Flusher.Entry @@ -1453,7 +1456,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio private void complete() { - frames(null, Callback.NOOP, newGoAwayFrame(ErrorCode.NO_ERROR.code, null), new DisconnectFrame()); + frames(null, Callback.NOOP, newGoAwayFrame(CloseState.CLOSED, ErrorCode.NO_ERROR.code, null), new DisconnectFrame()); } } diff --git a/jetty-http2/http2-common/src/main/java/org/eclipse/jetty/http2/frames/GoAwayFrame.java b/jetty-http2/http2-common/src/main/java/org/eclipse/jetty/http2/frames/GoAwayFrame.java index 7fcf3cb81cb..e603771dee9 100644 --- a/jetty-http2/http2-common/src/main/java/org/eclipse/jetty/http2/frames/GoAwayFrame.java +++ b/jetty-http2/http2-common/src/main/java/org/eclipse/jetty/http2/frames/GoAwayFrame.java @@ -20,17 +20,25 @@ package org.eclipse.jetty.http2.frames; import java.nio.charset.StandardCharsets; +import org.eclipse.jetty.http2.CloseState; import org.eclipse.jetty.http2.ErrorCode; public class GoAwayFrame extends Frame { + private final CloseState closeState; private final int lastStreamId; private final int error; private final byte[] payload; public GoAwayFrame(int lastStreamId, int error, byte[] payload) + { + this(CloseState.REMOTELY_CLOSED, lastStreamId, error, payload); + } + + public GoAwayFrame(CloseState closeState, int lastStreamId, int error, byte[] payload) { super(FrameType.GO_AWAY); + this.closeState = closeState; this.lastStreamId = lastStreamId; this.error = error; this.payload = payload; @@ -69,10 +77,11 @@ public class GoAwayFrame extends Frame public String toString() { ErrorCode errorCode = ErrorCode.from(error); - return String.format("%s,%d/%s/%s", + return String.format("%s,%d/%s/%s/%s", super.toString(), lastStreamId, errorCode != null ? errorCode.toString() : String.valueOf(error), - tryConvertPayload()); + tryConvertPayload(), + closeState); } }