Browse Source

Only sets content type attr if not null.

fixes gh-374
pull/377/merge
Spencer Gibb 7 years ago
parent
commit
28166054c4
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 5
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/NettyRoutingFilter.java
  2. 1
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java
  3. 17
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java

5
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/NettyRoutingFilter.java

@ -42,6 +42,7 @@ import org.springframework.web.server.ServerWebExchange; @@ -42,6 +42,7 @@ import org.springframework.web.server.ServerWebExchange;
import static org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter.filterRequest;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.PRESERVE_HOST_HEADER_ATTRIBUTE;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.isAlreadyRouted;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setAlreadyRouted;
@ -114,7 +115,9 @@ public class NettyRoutingFilter implements GlobalFilter, Ordered { @@ -114,7 +115,9 @@ public class NettyRoutingFilter implements GlobalFilter, Ordered {
res.responseHeaders().forEach(entry -> headers.add(entry.getKey(), entry.getValue()));
exchange.getAttributes().put("original_response_content_type", headers.getContentType());
if (headers.getContentType() != null) {
exchange.getAttributes().put(ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR, headers.getContentType());
}
HttpHeaders filteredResponseHeaders = HttpHeadersFilter.filter(
this.headersFilters.getIfAvailable(), headers, exchange, Type.RESPONSE);

1
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java

@ -48,6 +48,7 @@ public class ServerWebExchangeUtils { @@ -48,6 +48,7 @@ public class ServerWebExchangeUtils {
public static final String GATEWAY_SCHEME_PREFIX_ATTR = qualify("gatewaySchemePrefix");
public static final String GATEWAY_PREDICATE_ROUTE_ATTR = qualify("gatewayPredicateRouteAttr");
public static final String WEIGHT_ATTR = qualify("routeWeight");
public static final String ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR = "original_response_content_type";
/**
* Used when a routing filter has been successfully call. Allows users to write custom

17
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java

@ -39,8 +39,11 @@ import org.springframework.context.annotation.Import; @@ -39,8 +39,11 @@ import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import static org.assertj.core.api.Assertions.assertThat;
@ -146,13 +149,27 @@ public class GatewayIntegrationTests extends BaseWebClientTests { @@ -146,13 +149,27 @@ public class GatewayIntegrationTests extends BaseWebClientTests {
.expectStatus().is5xxServerError();
}
@Test
// gh-374 no content type/empty body causes NPR in NettyRoutingFilter
public void noContentType() {
testClient.get().uri("/nocontenttype")
.exchange()
.expectStatus().is2xxSuccessful();
}
@EnableAutoConfiguration
@SpringBootConfiguration
@Import(DefaultTestConfig.class)
@RestController
public static class TestConfig {
private static final Log log = LogFactory.getLog(TestConfig.class);
@RequestMapping("/httpbin/nocontenttype")
public ResponseEntity<Void> nocontenttype() {
return ResponseEntity.status(204).build();
}
@Bean
@Order(-1)
public GlobalFilter postFilter() {

Loading…
Cancel
Save