Skip to content

Commit 08b9e7b

Browse files
committed
Avoid duplicate Content-Length headers in Netty client
This commit checks that a "Content-Length" request header isn't already present before adding one in `Netty4ClientHttpRequestFactory`. `HttpMessageConverter` implementations can write that request header so the Netty request factory should only write that value when the header is missing. If that header is not written (and since we're not dealing with the HTTP exchange in a chunked-based fashion), the HTTP client might not send the request body at all. Issue: SPR-15241
1 parent e24c530 commit 08b9e7b

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

spring-web/src/main/java/org/springframework/http/client/Netty4ClientHttpRequest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -137,14 +137,14 @@ private FullHttpRequest createFullHttpRequest(HttpHeaders headers) {
137137
HttpVersion.HTTP_1_1, nettyMethod, path, this.body.buffer());
138138

139139
nettyRequest.headers().set(HttpHeaders.HOST, this.uri.getHost());
140-
if (this.body.buffer().readableBytes() > 0) {
141-
nettyRequest.headers().set(HttpHeaders.CONTENT_LENGTH, this.body.buffer().readableBytes());
142-
}
143140
nettyRequest.headers().set(HttpHeaders.CONNECTION, "close");
144-
145141
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
146142
nettyRequest.headers().add(entry.getKey(), entry.getValue());
147143
}
144+
if (!nettyRequest.headers().contains(HttpHeaders.CONTENT_LENGTH)
145+
&& this.body.buffer().readableBytes() > 0) {
146+
nettyRequest.headers().set(HttpHeaders.CONTENT_LENGTH, this.body.buffer().readableBytes());
147+
}
148148

149149
return nettyRequest;
150150
}

spring-web/src/test/java/org/springframework/web/client/AbstractMockWebServerTestCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -88,6 +88,7 @@ private MockResponse getRequest(RecordedRequest request, byte[] body, String con
8888
private MockResponse postRequest(RecordedRequest request, String expectedRequestContent,
8989
String location, String contentType, byte[] responseBody) {
9090

91+
assertEquals(1, request.getHeaders().values("Content-Length").size());
9192
assertTrue("Invalid request content-length",
9293
Integer.parseInt(request.getHeader("Content-Length")) > 0);
9394
String requestContentType = request.getHeader("Content-Type");

0 commit comments

Comments
 (0)