You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# fixed ver of application.ymlspring:
cloud:
gateway:
routes:
- id: testuri: http://localhost:8080 # prepended http://predicates:
- Path=/api/v?/test
Then it works fine.
Sample
FYI.. took 6m40s to get response is because I set the breakpoint and debug, nothing related to performance issue
Trouble Shoot
As @cstraw01 said from #1764
while on bootstrap, URI in RouteDefinition gets initialized with scheme of localhost
After that, RouteToRequestUrlFilter merge an url from URL instance which has invalid scheme and put it as an attribute of exchange GATEWAY_REQUEST_URL_ATTR
Followed by filter chain, it will move on to NettyRoutingFilter from where request should be forwarded, but since GATEWAY_REQUEST_URL_ATTR in exchange attribute is in invalid format so it won't move on.
For now on, I added custom filter in my project to throws an error if http:// is not prepended on uri configuration of route to fix current problem, but is its just an temporarily solution.
@Component
public class RouteDefinitionFilter implements GlobalFilter, Ordered {
private static final int ORDER = NettyRoutingFilter.ORDER - 1;
private static final String LOCALHOST = "localhost";
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
URI requestUrl = exchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
if (requestUrl.getScheme().equals(LOCALHOST)) {
throw new IllegalArgumentException(
String.format("Unexpected uri scheme for path: \"%s\"", requestUrl.getPath())
);
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return ORDER;
}
}
Suggestion
I have checked that If uri configuration starts with localhost, host is always null
In RouteToRequestUrlFilter, if scheme of uri is lb it checks whether host is null or not
since valid URI must contains non-null host part so I would like to change code like...
/*if ("lb".equalsIgnoreCase(routeUri.getScheme()) && routeUri.getHost() == null) { // Load balanced URIs should always have a host. If the host is null it is // most // likely because the host name was invalid (for example included an // underscore) throw new IllegalStateException("Invalid host: " + routeUri.toString());}*/// not just when scheme is lb, always checks host is non-nullif (routeUri.getHost() == null) {
thrownewIllegalStateException("Invalid host: " + routeUri.toString());
}
If so, it could also filter my situation as when uri configuration is given as localhost:8080
thank you :)
The text was updated successfully, but these errors were encountered:
I see, localhost is the key. Because the scheme isn't http{s}, no request is ever sent.
spencergibb
changed the title
Gateway Routes URI without http:// prefix returns empty body with 200 status code
Gateway Routes URI of localhost:port without http:// returns empty body with 200 status code
Mar 12, 2024
Version
Describe the bug
Exact same issue #1764 report from Aug.2020 by @Bryksin
Since @spencergibb guided to update Spring Cloud version to hoxton.sr5 will solve the problem, BUT I am using the version 2021.0.3 which is further updated then hoxton.sr5 and still has the same issue.
Cloud Gateway is not forwarding requests, but even worse it just returns an empty body of status code 200 even for invalid or non-existing endpoints.
The reason for it is because I set the
uri
configuration without the URI scheme in itif I correct my configuration to
Then it works fine.
Sample
FYI.. took 6m40s to get response is because I set the breakpoint and debug, nothing related to performance issue
Trouble Shoot
As @cstraw01 said from #1764
while on bootstrap,
URI
inRouteDefinition
gets initialized with scheme oflocalhost
After that,
RouteToRequestUrlFilter
merge an url from URL instance which has invalid scheme and put it as an attribute of exchangeGATEWAY_REQUEST_URL_ATTR
Followed by filter chain, it will move on to
NettyRoutingFilter
from where request should be forwarded, but sinceGATEWAY_REQUEST_URL_ATTR
in exchange attribute is in invalid format so it won't move on.For now on, I added custom filter in my project to throws an error if
http://
is not prepended onuri
configuration of route to fix current problem, but is its just an temporarily solution.Suggestion
I have checked that If
uri
configuration starts withlocalhost
,host
is always nullIn RouteToRequestUrlFilter, if scheme of uri is
lb
it checks whether host is null or notspring-cloud-gateway/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilter.java
Lines 80 to 86 in 84fd7cb
since valid URI must contains non-null host part so I would like to change code like...
If so, it could also filter my situation as when uri configuration is given as
localhost:8080
thank you :)
The text was updated successfully, but these errors were encountered: