Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,12 @@ protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPlu
Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.CANNOT_FIND_HEALTHY_UPSTREAM_URL);
return WebFluxResultUtils.result(exchange, error);
}
// set the http url
String domain = upstream.buildDomain();
List<String> specifyDomains = exchange.getRequest().getHeaders().get(Constants.SPECIFY_DOMAIN);
if (CollectionUtils.isNotEmpty(specifyDomains)) {
upstream.setUrl(specifyDomains.get(0));
domain = buildDomain(upstream, specifyDomains.get(0));
}
// set domain
String domain = upstream.buildDomain();
exchange.getAttributes().put(Constants.HTTP_DOMAIN, domain);
// set the http timeout
exchange.getAttributes().put(Constants.HTTP_TIME_OUT, ruleHandle.getTimeout());
Expand Down Expand Up @@ -158,6 +157,14 @@ private DivideRuleHandle buildRuleHandle(final RuleData rule) {
return DividePluginDataHandler.CACHED_HANDLE.get().obtainHandle(CacheKeyUtils.INST.getKey(rule));
}

private String buildDomain(final Upstream upstream, final String url) {
String protocol = upstream.getProtocol();
if (StringUtils.isBlank(protocol)) {
protocol = "http://";
}
return protocol + StringUtils.trim(url);
}

private void responseTrigger(final Upstream upstream) {
long now = System.currentTimeMillis();
upstream.getInflight().decrementAndGet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,24 @@ public void doPostExecuteTest() {
StepVerifier.create(result).expectSubscription().verifyComplete();
}

@Test
public void doExecuteWithSpecifyDomainShouldNotMutateCachedUpstream() {
ServerWebExchange specifyDomainExchange = MockServerWebExchange.from(MockServerHttpRequest.get("localhost")
.remoteAddress(new InetSocketAddress(8090))
.header(Constants.SPECIFY_DOMAIN, "mock-specified")
.build());
ShenyuContext context = mock(ShenyuContext.class);
specifyDomainExchange.getAttributes().put(Constants.CONTEXT, context);
when(chain.execute(specifyDomainExchange)).thenReturn(Mono.empty());

Mono<Void> result = dividePlugin.doExecute(specifyDomainExchange, chain, selectorData, ruleData);
StepVerifier.create(result).expectSubscription().verifyComplete();

List<Upstream> upstreams = UpstreamCacheManager.getInstance().findUpstreamListBySelectorId(selectorData.getId());
assertEquals("http://mock-specified", specifyDomainExchange.getAttribute(Constants.HTTP_DOMAIN));
assertEquals("mock-3", upstreams.get(0).getUrl());
}

/**
* Skip.
*/
Expand Down