Skip to content

Commit de6bbe7

Browse files
committed
Handle null header value property
When using an Apache Http components based infrastructure, a null header value is handled as the empty string. The exact same infrastructure using HttpURLConnection generates a header with no colon. This is actually not proper HTTP and some components fail to read such request. We now make sure to call HttpURLConnection#addRequestProperty with the empty String for a null header value. Issue: SPR-13225
1 parent bdb6348 commit de6bbe7

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ static void addHeaders(HttpURLConnection connection, HttpHeaders headers) {
100100
}
101101
else {
102102
for (String headerValue : entry.getValue()) {
103-
connection.addRequestProperty(headerName, headerValue);
103+
String actualHeaderValue = headerValue != null ? headerValue : "";
104+
connection.addRequestProperty(headerName, actualHeaderValue);
104105
}
105106
}
106107
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2002-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.client;
18+
19+
import java.net.HttpURLConnection;
20+
21+
import org.junit.Test;
22+
23+
import org.springframework.http.HttpHeaders;
24+
25+
import static org.mockito.Mockito.*;
26+
27+
/**
28+
* @author Stephane Nicoll
29+
*/
30+
public class SimpleClientHttpRequestFactoryTests {
31+
32+
@Test // SPR-13225
33+
public void headerWithNullValue() {
34+
HttpURLConnection urlConnection = mock(HttpURLConnection.class);
35+
HttpHeaders headers = new HttpHeaders();
36+
headers.set("foo", null);
37+
SimpleBufferingClientHttpRequest.addHeaders(urlConnection, headers);
38+
verify(urlConnection, times(1)).addRequestProperty("foo", "");
39+
}
40+
41+
}

0 commit comments

Comments
 (0)