-
Notifications
You must be signed in to change notification settings - Fork 630
/
SimpleCompressionHandler.java
61 lines (56 loc) · 2.39 KB
/
SimpleCompressionHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* Copyright (c) 2018-2022 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.netty5.http.server;
import io.netty5.buffer.Buffer;
import io.netty5.channel.ChannelHandlerContext;
import io.netty5.handler.codec.http.DefaultHttpContent;
import io.netty5.handler.codec.http.DefaultHttpRequest;
import io.netty5.handler.codec.http.FullHttpRequest;
import io.netty5.handler.codec.http.HttpContentCompressor;
import io.netty5.handler.codec.http.HttpRequest;
import io.netty5.util.concurrent.Future;
/**
* @author Stephane Maldini
*/
final class SimpleCompressionHandler extends HttpContentCompressor {
@Override
public Future<Void> write(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof Buffer buffer) {
return super.write(ctx, new DefaultHttpContent(buffer));
}
return super.write(ctx, msg);
}
void decode(ChannelHandlerContext ctx, HttpRequest msg, boolean release) throws Exception {
HttpRequest request = msg;
if (msg instanceof FullHttpRequest fullHttpRequest &&
(!fullHttpRequest.isAccessible() || fullHttpRequest.payload().readableBytes() == 0)) {
// 1. This can happen only in HTTP2 use case and delayed response
// When the incoming FullHttpRequest content is with 0 readableBytes it is released immediately
// decode(...) will observe a freed content.
// 2. fireChannelRead(...) is invoked at the end of super.decodeAndClose(...) which will end up
// in io.netty5.channel.DefaultChannelPipeline.onUnhandledInboundMessage which closes the msg.
request = new DefaultHttpRequest(msg.protocolVersion(), msg.method(), msg.uri(), msg.headers());
if (release && fullHttpRequest.isAccessible()) {
fullHttpRequest.close();
}
}
super.decodeAndClose(ctx, request);
}
@Override
protected void decodeAndClose(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
decode(ctx, msg, true);
}
}