Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to change request uri dynamically in filters #286

Merged
merged 1 commit into from
Apr 23, 2018
Merged

Add the ability to change request uri dynamically in filters #286

merged 1 commit into from
Apr 23, 2018

Conversation

making
Copy link
Contributor

@making making commented Apr 20, 2018

This PR adds AbstractChangeRequestUriGatewayFilterFactory that enables the gateway to change URL dynamically using ServerWebExchange.
As a implementation, RequestHeaderToRequestUriGatewayFilterFactory comes with this PR that determine URL by a request header.
This factory will be pretty useful when implementing Cloud Foundry Route Service.

also fixes #276

https://twitter.com/Fitzoh/status/985423505947754496

@codecov-io
Copy link

codecov-io commented Apr 20, 2018

Codecov Report

Merging #286 into master will increase coverage by 0.44%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #286      +/-   ##
==========================================
+ Coverage   75.88%   76.33%   +0.44%     
==========================================
  Files         105      107       +2     
  Lines        2613     2645      +32     
  Branches      191      191              
==========================================
+ Hits         1983     2019      +36     
+ Misses        524      520       -4     
  Partials      106      106
Impacted Files Coverage Δ
...cloud/gateway/route/builder/GatewayFilterSpec.java 58.97% <100%> (+4.04%) ⬆️
...cloud/gateway/config/GatewayAutoConfiguration.java 97.95% <100%> (+0.02%) ⬆️
...RequestHeaderToRequestUriGatewayFilterFactory.java 100% <100%> (ø)
.../AbstractChangeRequestUriGatewayFilterFactory.java 100% <100%> (ø)
...ork/cloud/gateway/route/builder/PredicateSpec.java 53.65% <0%> (+9.75%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ce76b5c...f719a5f. Read the comment docs.

@cah-andrew-fitzgerald
Copy link

Awesome!

The one thing I might consider would be pulling Function<ServerWebExchange, Optional<URI>> into a functional interface, but that could go either way

@spencergibb spencergibb added this to the 2.0.0.RC1 milestone Apr 23, 2018
@spencergibb spencergibb merged commit 95c7247 into spring-cloud:master Apr 23, 2018
bijukunjummen pushed a commit to bijukunjummen/spring-cloud-gateway that referenced this pull request Jun 8, 2018
@Qsimple
Copy link
Contributor

Qsimple commented Aug 12, 2019

Hi, dear dev team:
how do you think about this code snapshot in RequestHeaderToRequestUriGatewayFilterFactory:

	@Override
	protected Optional<URI> determineRequestUri(ServerWebExchange exchange,
			NameConfig config) {
		String requestUrl = exchange.getRequest().getHeaders().getFirst(config.getName());
		return Optional.ofNullable(requestUrl).map(url -> {
			try {
				return new URL(url).toURI();
			}
			catch (MalformedURLException | URISyntaxException e) {
				log.info("Request url is invalid : url={}, error={}", requestUrl,
						e.getMessage());
				return null;
			}
		});
	}

if we need to change original uri to "lb://XXX", MalformedURLException will be catched and return null. I think this an common scenario.

                                                                                                         Best regards
                                                                                                          simple

@spencergibb
Copy link
Member

Create a URI object directly, not a url first.

@Qsimple
Copy link
Contributor

Qsimple commented Sep 8, 2019

So, may I push a PR to fix it? I think some testcase needed to be modified to allowd "lb://XXX"

@spencergibb
Copy link
Member

PRs welcome

Johnny850807 pushed a commit to Johnny850807/spring-cloud-gateway that referenced this pull request Dec 14, 2020
@shiddarthbista
Copy link

@spencergibb
Is there a way to set the route uri dynamically based on a value in the request body during runtime?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to dynamic determine route uri at runtime?
6 participants