spring-cloud-gateway: WebFilter's doOnSuccessOrError consumer not working when Content-Length header is 0

WebFilter’s doOnSuccessOrError and doAfterSuccessOrError consumers not working when the Content-Length header is 0.

I have a WebFilter as follows:

@Bean
WebFilter webFilter() {
  return (exchange, chain) ->
      chain.filter(exchange)
          .doOnRequest(value -> {
            logger.error("webFilter" + " doOnRequest");
          })
          .doOnSuccessOrError((aVoid, throwable) -> {
            logger.error("webFilter" + " doOnSuccessOrError");
          })
          .doAfterSuccessOrError((aVoid, throwable) -> {
            logger.error("webFilter" + " doAfterSuccessOrError");
          });
}

The gateway routes all traffic to a tomcat application using this configuration:

@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
  return builder.routes()
      .route(r -> r.path("/**")
          .uri("http://localhost:9001").filter(gatewayFilter()))
      .build();
}

I have these three endpoints on spring boot tomcat application:

@GetMapping("/redirect")
public ModelAndView redirectWithUsingRedirectPrefix(ModelMap model) {
  return new ModelAndView("redirect:http://google.com", model);
}

@GetMapping("/empty-response")
public ResponseEntity<Void> responseEntity() {
  return ResponseEntity.ok().build();
}

@GetMapping("/foo")
public ResponseEntity<String> string() {
  return ResponseEntity.ok("foo");
}

Request through the gateway for /foo it works as expected

[ctor-http-nio-2] GatewayConfiguration  : webFilter doOnRequest
[ctor-http-nio-2] GatewayConfiguration  : globalFilter doOnRequest
[ctor-http-nio-3] GatewayConfiguration  : gatewayFilter doOnRequest
[ctor-http-nio-3] GatewayConfiguration  : gatewayFilter doOnSuccessOrError
[ctor-http-nio-3] GatewayConfiguration  : globalFilter doOnSuccessOrError
[ctor-http-nio-3] GatewayConfiguration  : globalFilter doAfterSuccessOrError
[ctor-http-nio-3] GatewayConfiguration  : gatewayFilter doAfterSuccessOrError
[ctor-http-nio-2] GatewayConfiguration  : webFilter doOnSuccessOrError
[ctor-http-nio-2] GatewayConfiguration  : webFilter doAfterSuccessOrError

For /empty-response

[ctor-http-nio-4] GatewayConfiguration  : webFilter doOnRequest
[ctor-http-nio-4] GatewayConfiguration  : globalFilter doOnRequest
[ctor-http-nio-3] GatewayConfiguration  : gatewayFilter doOnRequest
[ctor-http-nio-3] GatewayConfiguration  : gatewayFilter doOnSuccessOrError
[ctor-http-nio-3] GatewayConfiguration  : globalFilter doOnSuccessOrError
[ctor-http-nio-3] GatewayConfiguration  : globalFilter doAfterSuccessOrError
[ctor-http-nio-3] GatewayConfiguration  : gatewayFilter doAfterSuccessOrError

GlobalFilter and GatewayFilter works as expected. But, as you see, webFilter doOnSuccessOrError and webFilter doAfterSuccessOrError are missing for /empty-response. It’s same for /redirect endpoint.

You can use https://github.com/hisener/sc-gateway-test to reproduce it.

StackOverflow question: https://stackoverflow.com/questions/52419284/webfilters-doonsuccessorerror-consumer-not-working-when-content-length-header-i

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 18 (10 by maintainers)

Most upvoted comments

So, that is the expected behavior of reactor-netty?

yes