Skip to content

Commit

Permalink
Do not reuse WebsocketServerSpec.Builder between requests
Browse files Browse the repository at this point in the history
Fixes gh-2159
Fixes gh-2160
  • Loading branch information
Johannes-Rost authored and spencergibb committed Mar 12, 2021
1 parent cd59c65 commit b1f1e75
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
Expand Up @@ -19,6 +19,7 @@
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

import com.netflix.hystrix.HystrixObservableCommand;
import io.netty.channel.ChannelOption;
Expand All @@ -28,6 +29,7 @@
import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Flux;
import reactor.netty.http.client.HttpClient;
import reactor.netty.http.server.WebsocketServerSpec;
import reactor.netty.resources.ConnectionProvider;
import reactor.netty.tcp.ProxyProvider;
import rx.RxReactiveStreams;
Expand Down Expand Up @@ -812,17 +814,17 @@ public ReactorNettyWebSocketClient reactorNettyWebSocketClient(
@Bean
public ReactorNettyRequestUpgradeStrategy reactorNettyRequestUpgradeStrategy(
HttpClientProperties httpClientProperties) {
ReactorNettyRequestUpgradeStrategy requestUpgradeStrategy = new ReactorNettyRequestUpgradeStrategy();

HttpClientProperties.Websocket websocket = httpClientProperties
.getWebsocket();
PropertyMapper map = PropertyMapper.get();
map.from(websocket::getMaxFramePayloadLength).whenNonNull()
.to(requestUpgradeStrategy::setMaxFramePayloadLength);
map.from(websocket::isProxyPing).to(requestUpgradeStrategy::setHandlePing);
return requestUpgradeStrategy;
}

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(builderSupplier);
}

@Configuration(proxyBeanMethods = false)
Expand Down
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 @@ -160,6 +163,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();
}

@EnableAutoConfiguration
@SpringBootConfiguration
protected static class Config {
Expand Down

0 comments on commit b1f1e75

Please sign in to comment.