Skip to content

Commit

Permalink
Do not reuse WebsocketServerSpec.Builder between requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes-Rost committed Feb 25, 2021
1 parent 329efa8 commit e0eeea4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.time.Duration;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

import io.netty.channel.ChannelOption;
import io.netty.handler.ssl.SslContextBuilder;
Expand Down Expand Up @@ -789,13 +790,16 @@ public ReactorNettyWebSocketClient reactorNettyWebSocketClient(HttpClientPropert
public ReactorNettyRequestUpgradeStrategy reactorNettyRequestUpgradeStrategy(
HttpClientProperties httpClientProperties) {

WebsocketServerSpec.Builder builder = WebsocketServerSpec.builder();
HttpClientProperties.Websocket websocket = httpClientProperties.getWebsocket();
PropertyMapper map = PropertyMapper.get();
map.from(websocket::getMaxFramePayloadLength).whenNonNull().to(builder::maxFramePayloadLength);
map.from(websocket::isProxyPing).to(builder::handlePing);
Supplier<WebsocketServerSpec.Builder> builderSupplier = () -> {
WebsocketServerSpec.Builder builder = WebsocketServerSpec.builder();
HttpClientProperties.Websocket websocket = httpClientProperties.getWebsocket();
PropertyMapper map = PropertyMapper.get();
map.from(websocket::getMaxFramePayloadLength).whenNonNull().to(builder::maxFramePayloadLength);
map.from(websocket::isProxyPing).to(builder::handlePing);
return builder;
};

return new ReactorNettyRequestUpgradeStrategy(builder);
return new ReactorNettyRequestUpgradeStrategy(builderSupplier);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package org.springframework.cloud.gateway.config;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.Test;
import reactor.netty.http.client.HttpClient;
import reactor.netty.http.server.WebsocketServerSpec;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
Expand Down Expand Up @@ -152,6 +155,22 @@ public void verboseActuatorDisabled() {
}
}

@Test // gh-2159
public void reactorNettyRequestUpgradeStrategyWebSocketSpecBuilderIsUniquePerRequest()
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
ReactorNettyRequestUpgradeStrategy strategy = new GatewayAutoConfiguration.NettyConfiguration()
.reactorNettyRequestUpgradeStrategy(new HttpClientProperties());

// Method "buildSpec" was introduced for Tests, but has only default visiblity
Method buildSpec = ReactorNettyRequestUpgradeStrategy.class.getDeclaredMethod("buildSpec", String.class);
buildSpec.setAccessible(true);
WebsocketServerSpec spec1 = (WebsocketServerSpec) buildSpec.invoke(strategy, "p1");
WebsocketServerSpec spec2 = strategy.getWebsocketServerSpec();

assertThat(spec1.protocols()).isEqualTo("p1");
assertThat(spec2.protocols()).isNull();
}

@Test
public void tokenRelayBeansAreCreated() {
new ReactiveWebApplicationContextRunner()
Expand Down

0 comments on commit e0eeea4

Please sign in to comment.