-
Notifications
You must be signed in to change notification settings - Fork 630
/
Http2StreamBridgeServerHandler.java
169 lines (155 loc) · 5.65 KB
/
Http2StreamBridgeServerHandler.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* 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 java.net.SocketAddress;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import io.netty5.buffer.Buffer;
import io.netty5.channel.Channel;
import io.netty5.channel.ChannelHandlerAdapter;
import io.netty5.channel.ChannelHandlerContext;
import io.netty5.handler.codec.http.DefaultHttpContent;
import io.netty5.handler.codec.http.HttpRequest;
import io.netty5.handler.codec.http.LastHttpContent;
import io.netty5.handler.codec.http2.Http2StreamFrameToHttpObjectCodec;
import io.netty5.handler.ssl.SslHandler;
import io.netty5.util.Resource;
import io.netty5.util.concurrent.Future;
import io.netty5.util.concurrent.FutureContextListener;
import reactor.core.publisher.Mono;
import reactor.netty5.Connection;
import reactor.netty5.ConnectionObserver;
import reactor.util.annotation.Nullable;
import static reactor.netty5.ReactorNetty.format;
/**
* This handler is intended to work together with {@link Http2StreamFrameToHttpObjectCodec}
* it converts the outgoing messages into objects expected by
* {@link Http2StreamFrameToHttpObjectCodec}.
*
* @author Violeta Georgieva
*/
final class Http2StreamBridgeServerHandler extends ChannelHandlerAdapter implements FutureContextListener<Channel, Void> {
final BiPredicate<HttpServerRequest, HttpServerResponse> compress;
final HttpServerFormDecoderProvider formDecoderProvider;
final BiFunction<ConnectionInfo, HttpRequest, ConnectionInfo> forwardedHeaderHandler;
final ConnectionObserver listener;
final BiFunction<? super Mono<Void>, ? super Connection, ? extends Mono<Void>>
mapHandle;
SocketAddress remoteAddress;
Boolean secured;
/**
* Flag to indicate if a request is not yet fully responded.
*/
boolean pendingResponse;
Http2StreamBridgeServerHandler(
@Nullable BiPredicate<HttpServerRequest, HttpServerResponse> compress,
HttpServerFormDecoderProvider formDecoderProvider,
@Nullable BiFunction<ConnectionInfo, HttpRequest, ConnectionInfo> forwardedHeaderHandler,
ConnectionObserver listener,
@Nullable BiFunction<? super Mono<Void>, ? super Connection, ? extends Mono<Void>> mapHandle) {
this.compress = compress;
this.formDecoderProvider = formDecoderProvider;
this.forwardedHeaderHandler = forwardedHeaderHandler;
this.listener = listener;
this.mapHandle = mapHandle;
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "New HTTP/2 stream"));
}
ctx.read();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (secured == null) {
secured = ctx.channel().parent().pipeline().get(SslHandler.class) != null;
}
if (remoteAddress == null) {
remoteAddress =
Optional.ofNullable(HAProxyMessageReader.resolveRemoteAddressFromProxyProtocol(ctx.channel().parent()))
.orElse(ctx.channel().parent().remoteAddress());
}
if (msg instanceof HttpRequest request) {
HttpServerOperations ops;
try {
pendingResponse = true;
ops = new HttpServerOperations(Connection.from(ctx.channel()),
listener,
request,
compress,
ConnectionInfo.from(ctx.channel().parent(),
request,
secured,
remoteAddress,
forwardedHeaderHandler),
formDecoderProvider,
mapHandle,
secured);
}
catch (RuntimeException e) {
pendingResponse = false;
HttpServerOperations.sendDecodingFailures(ctx, listener, secured, e, msg);
return;
}
ops.bind();
listener.onStateChange(ops, ConnectionObserver.State.CONFIGURED);
}
else if (!pendingResponse) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Dropped HTTP content, " +
"since response has been sent already: {}"), msg);
}
Resource.dispose(msg);
ctx.read();
return;
}
ctx.fireChannelRead(msg);
}
@Override
public Future<Void> write(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof Buffer buffer) {
return ctx.write(new DefaultHttpContent(buffer));
}
else {
Future<Void> f = ctx.write(msg);
if (msg instanceof LastHttpContent) {
pendingResponse = false;
f.addListener(ctx.channel(), this);
ctx.read();
}
return f;
}
}
@Override
public void operationComplete(Channel channel, Future<? extends Void> future) {
if (!future.isSuccess()) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(channel,
"Sending last HTTP packet was not successful, terminating the channel"),
future.cause());
}
}
else {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(channel,
"Last HTTP packet was sent, terminating the channel"));
}
}
HttpServerOperations.cleanHandlerTerminate(channel);
}
}