3333
3434public class VertxCompressorHttp2ConnectionEncoder implements Http2FrameWriter , Http2ConnectionEncoder , Http2SettingsReceivedConsumer {
3535
36- private Http2ConnectionEncoder delegate ;
36+ private final Http2ConnectionEncoder delegate ;
3737 private final Http2ConnectionEncoder plainEncoder ;
38+ private final Http2Connection .PropertyKey encoderKey ;
3839
3940 public VertxCompressorHttp2ConnectionEncoder (Http2ConnectionEncoder plainEncoder , CompressionOptions [] compressionOptions ) {
4041 this .delegate = new CompressorHttp2ConnectionEncoder (plainEncoder , compressionOptions );
4142 this .plainEncoder = plainEncoder ;
43+ this .encoderKey = plainEncoder .connection ().newKey ();
4244 }
4345
44- private void beforeWritingHeaders (ChannelHandlerContext ctx , int streamId , Http2Headers responseHeaders ) {
46+ private Http2ConnectionEncoder selectEncoder (ChannelHandlerContext ctx , int streamId , Http2Headers responseHeaders ) {
4547 String contentEncodingToApply = determineContentEncodingToApply (ctx , streamId , responseHeaders );
4648 if (contentEncodingToApply == null || contentEncodingToApply .equalsIgnoreCase (IDENTITY .toString ())) {
4749 if (responseHeaders .contains (CONTENT_ENCODING , IDENTITY )) {
4850 responseHeaders .remove (CONTENT_ENCODING );
4951 }
50- delegate = plainEncoder ;
52+ return plainEncoder ;
5153 } else {
5254 responseHeaders .set (CONTENT_ENCODING , contentEncodingToApply );
55+ return delegate ;
5356 }
5457 }
5558
59+ private void storeEncoder (int streamId , Http2ConnectionEncoder encoder ) {
60+ io .netty .handler .codec .http2 .Http2Stream stream = delegate .connection ().stream (streamId );
61+ if (stream != null ) {
62+ stream .setProperty (encoderKey , encoder );
63+ }
64+ }
65+
66+ private Http2ConnectionEncoder getStoredEncoder (int streamId ) {
67+ io .netty .handler .codec .http2 .Http2Stream stream = delegate .connection ().stream (streamId );
68+ if (stream != null ) {
69+ Http2ConnectionEncoder encoder = stream .getProperty (encoderKey );
70+ if (encoder != null ) {
71+ return encoder ;
72+ }
73+ }
74+ return delegate ;
75+ }
76+
5677 private String determineContentEncodingToApply (ChannelHandlerContext ctx , int streamId , Http2Headers responseHeaders ) {
5778 if (responseHeaders .contains (CONTENT_ENCODING )) {
5879 return null ;
@@ -69,14 +90,22 @@ private <T, R> R ifType(Object obj, Class<T> type, Function<T, R> then) {
6990
7091 @ Override
7192 public ChannelFuture writeHeaders (ChannelHandlerContext ctx , int streamId , Http2Headers headers , int padding , boolean endStream , ChannelPromise promise ) {
72- beforeWritingHeaders (ctx , streamId , headers );
73- return delegate .writeHeaders (ctx , streamId , headers , padding , endStream , promise );
93+ Http2ConnectionEncoder encoder = selectEncoder (ctx , streamId , headers );
94+ storeEncoder (streamId , encoder );
95+ return encoder .writeHeaders (ctx , streamId , headers , padding , endStream , promise );
7496 }
7597
7698 @ Override
7799 public ChannelFuture writeHeaders (ChannelHandlerContext ctx , int streamId , Http2Headers headers , int streamDependency , short weight , boolean exclusive , int padding , boolean endStream , ChannelPromise promise ) {
78- beforeWritingHeaders (ctx , streamId , headers );
79- return delegate .writeHeaders (ctx , streamId , headers , streamDependency , weight , exclusive , padding , endStream , promise );
100+ Http2ConnectionEncoder encoder = selectEncoder (ctx , streamId , headers );
101+ storeEncoder (streamId , encoder );
102+ return encoder .writeHeaders (ctx , streamId , headers , streamDependency , weight , exclusive , padding , endStream , promise );
103+ }
104+
105+ @ Override
106+ public ChannelFuture writeData (ChannelHandlerContext ctx , int streamId , ByteBuf data , int padding , boolean endStream , ChannelPromise promise ) {
107+ Http2ConnectionEncoder encoder = getStoredEncoder (streamId );
108+ return encoder .writeData (ctx , streamId , data , padding , endStream , promise );
80109 }
81110
82111 @ Override
@@ -164,11 +193,6 @@ public void close() {
164193 delegate .close ();
165194 }
166195
167- @ Override
168- public ChannelFuture writeData (ChannelHandlerContext ctx , int streamId , ByteBuf data , int padding , boolean endStream , ChannelPromise promise ) {
169- return delegate .writeData (ctx , streamId , data , padding , endStream , promise );
170- }
171-
172196 @ Override
173197 public void consumeReceivedSettings (Http2Settings settings ) {
174198 if (delegate instanceof Http2SettingsReceivedConsumer ) {
0 commit comments