Update Content-Length if body changed by client interceptor
Prior to this commit, the HTTP interceptor model used for `RestTemplate` and `RestClient` would not update the "Content-Length" request header, even when the request body had been updated by a `ClientHttpRequestInterceptor`. Even though this is the `ClientHttpRequestInterceptor`'s responsibility (along with the content type and encoding changes if needed), this would result in invalid requests. This invalid situation can be detected by `InterceptingClientHttpRequest`. This commit ensures that such situations are detected and fixed automatically by setting the Content-Length header to the actual body size, right before executing the actual request, after all interceptors are done. Closes gh-33459
This commit is contained in:
		
							parent
							
								
									be5d5fa52d
								
							
						
					
					
						commit
						aca264942d
					
				| 
						 | 
					@ -93,6 +93,10 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest {
 | 
				
			||||||
				request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value));
 | 
									request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value));
 | 
				
			||||||
				request.getAttributes().forEach((key, value) -> delegate.getAttributes().put(key, value));
 | 
									request.getAttributes().forEach((key, value) -> delegate.getAttributes().put(key, value));
 | 
				
			||||||
				if (body.length > 0) {
 | 
									if (body.length > 0) {
 | 
				
			||||||
 | 
										long contentLength = delegate.getHeaders().getContentLength();
 | 
				
			||||||
 | 
										if (contentLength > -1 && contentLength != body.length) {
 | 
				
			||||||
 | 
											delegate.getHeaders().setContentLength(body.length);
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
					if (delegate instanceof StreamingHttpOutputMessage streamingOutputMessage) {
 | 
										if (delegate instanceof StreamingHttpOutputMessage streamingOutputMessage) {
 | 
				
			||||||
						streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
 | 
											streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
 | 
				
			||||||
							@Override
 | 
												@Override
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -204,6 +204,7 @@ class InterceptingClientHttpRequestFactoryTests {
 | 
				
			||||||
		ClientHttpRequest request = requestFactory.createRequest(URI.create("https://example.com"), HttpMethod.GET);
 | 
							ClientHttpRequest request = requestFactory.createRequest(URI.create("https://example.com"), HttpMethod.GET);
 | 
				
			||||||
		request.execute();
 | 
							request.execute();
 | 
				
			||||||
		assertThat(Arrays.equals(changedBody, requestMock.getBodyAsBytes())).isTrue();
 | 
							assertThat(Arrays.equals(changedBody, requestMock.getBodyAsBytes())).isTrue();
 | 
				
			||||||
 | 
							assertThat(requestMock.getHeaders().getContentLength()).isEqualTo(changedBody.length);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue