3535
3636public class VertxCompressorHttp2ConnectionEncoder implements Http2FrameWriter , Http2ConnectionEncoder , Http2SettingsReceivedConsumer {
3737
38- private Http2ConnectionEncoder delegate ;
38+ private final Http2ConnectionEncoder delegate ;
3939 private final Http2ConnectionEncoder plainEncoder ;
40+ private final Http2Connection .PropertyKey encoderKey ;
4041
4142 public VertxCompressorHttp2ConnectionEncoder (Http2ConnectionEncoder plainEncoder , CompressionOptions [] compressionOptions ) {
4243 this .delegate = new CompressorHttp2ConnectionEncoder (plainEncoder , compressionOptions );
4344 this .plainEncoder = plainEncoder ;
45+ this .encoderKey = plainEncoder .connection ().newKey ();
4446 }
4547
46- private void beforeWritingHeaders (ChannelHandlerContext ctx , int streamId , Http2Headers responseHeaders ) {
48+ private Http2ConnectionEncoder selectEncoder (ChannelHandlerContext ctx , int streamId , Http2Headers responseHeaders ) {
4749 String contentEncodingToApply = determineContentEncodingToApply (ctx , streamId , responseHeaders );
4850 if (contentEncodingToApply == null || contentEncodingToApply .equalsIgnoreCase (IDENTITY .toString ())) {
4951 if (responseHeaders .contains (CONTENT_ENCODING , IDENTITY )) {
5052 responseHeaders .remove (CONTENT_ENCODING );
5153 }
52- delegate = plainEncoder ;
54+ return plainEncoder ;
5355 } else {
5456 responseHeaders .set (CONTENT_ENCODING , contentEncodingToApply );
57+ return delegate ;
5558 }
5659 }
5760
@@ -69,16 +72,42 @@ private <T, R> R ifType(Object obj, Class<T> type, Function<T, R> then) {
6972 return obj != null && type .isAssignableFrom (obj .getClass ()) ? then .apply (type .cast (obj )) : null ;
7073 }
7174
75+ private void storeEncoder (int streamId , Http2ConnectionEncoder encoder ) {
76+ io .netty .handler .codec .http2 .Http2Stream stream = delegate .connection ().stream (streamId );
77+ if (stream != null ) {
78+ stream .setProperty (encoderKey , encoder );
79+ }
80+ }
81+
82+ private Http2ConnectionEncoder getStoredEncoder (int streamId ) {
83+ io .netty .handler .codec .http2 .Http2Stream stream = delegate .connection ().stream (streamId );
84+ if (stream != null ) {
85+ Http2ConnectionEncoder encoder = stream .getProperty (encoderKey );
86+ if (encoder != null ) {
87+ return encoder ;
88+ }
89+ }
90+ return delegate ;
91+ }
92+
7293 @ Override
7394 public ChannelFuture writeHeaders (ChannelHandlerContext ctx , int streamId , Http2Headers headers , int padding , boolean endStream , ChannelPromise promise ) {
74- beforeWritingHeaders (ctx , streamId , headers );
75- return delegate .writeHeaders (ctx , streamId , headers , padding , endStream , promise );
95+ Http2ConnectionEncoder encoder = selectEncoder (ctx , streamId , headers );
96+ storeEncoder (streamId , encoder );
97+ return encoder .writeHeaders (ctx , streamId , headers , padding , endStream , promise );
7698 }
7799
78100 @ Override
79101 public ChannelFuture writeHeaders (ChannelHandlerContext ctx , int streamId , Http2Headers headers , int streamDependency , short weight , boolean exclusive , int padding , boolean endStream , ChannelPromise promise ) {
80- beforeWritingHeaders (ctx , streamId , headers );
81- return delegate .writeHeaders (ctx , streamId , headers , streamDependency , weight , exclusive , padding , endStream , promise );
102+ Http2ConnectionEncoder encoder = selectEncoder (ctx , streamId , headers );
103+ storeEncoder (streamId , encoder );
104+ return encoder .writeHeaders (ctx , streamId , headers , streamDependency , weight , exclusive , padding , endStream , promise );
105+ }
106+
107+ @ Override
108+ public ChannelFuture writeData (ChannelHandlerContext ctx , int streamId , ByteBuf data , int padding , boolean endStream , ChannelPromise promise ) {
109+ Http2ConnectionEncoder encoder = getStoredEncoder (streamId );
110+ return encoder .writeData (ctx , streamId , data , padding , endStream , promise );
82111 }
83112
84113 @ Override
@@ -166,11 +195,6 @@ public void close() {
166195 delegate .close ();
167196 }
168197
169- @ Override
170- public ChannelFuture writeData (ChannelHandlerContext ctx , int streamId , ByteBuf data , int padding , boolean endStream , ChannelPromise promise ) {
171- return delegate .writeData (ctx , streamId , data , padding , endStream , promise );
172- }
173-
174198 @ Override
175199 public void consumeReceivedSettings (Http2Settings settings ) {
176200 if (delegate instanceof Http2SettingsReceivedConsumer ) {
0 commit comments