HTTP header adapters print header values

Issue: SPR-17546
This commit is contained in:
Rossen Stoyanchev 2018-12-12 11:10:10 -05:00
parent 270099383b
commit 8eef97da33
6 changed files with 47 additions and 2 deletions

View File

@ -1640,7 +1640,26 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
@Override
public String toString() {
return this.headers.toString();
return formatHeaders(this.headers);
}
/**
* Helps to format HTTP header values, as HTTP header values themselves can
* contain comma-separated values, can become confusing with regular
* {@link Map} formatting that also uses commas between entries.
* @param headers the headers to format
* @return the headers to a String
* @since 5.1.4
*/
public static String formatHeaders(MultiValueMap<String, String> headers) {
return headers.entrySet().stream()
.map(entry -> {
List<String> values = entry.getValue();
return entry.getKey() + ":" + (values.size() == 1 ?
"\"" + values.get(0) + "\"" :
values.stream().map(s -> "\"" + s + "\"").collect(Collectors.joining(", ")));
})
.collect(Collectors.joining(", ", "[", "]"));
}

View File

@ -29,6 +29,7 @@ import java.util.stream.Collectors;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.springframework.http.HttpHeaders;
import org.springframework.lang.Nullable;
import org.springframework.util.MultiValueMap;
@ -176,6 +177,12 @@ class JettyHeadersAdapter implements MultiValueMap<String, String> {
}
@Override
public String toString() {
return HttpHeaders.formatHeaders(this);
}
private class EntryIterator implements Iterator<Entry<String, List<String>>> {
private Enumeration<String> names = headers.getFieldNames();

View File

@ -176,6 +176,12 @@ class NettyHeadersAdapter implements MultiValueMap<String, String> {
}
@Override
public String toString() {
return org.springframework.http.HttpHeaders.formatHeaders(this);
}
private class EntryIterator implements Iterator<Entry<String, List<String>>> {
private Iterator<String> names = headers.names().iterator();

View File

@ -31,6 +31,7 @@ import java.util.stream.Collectors;
import org.apache.tomcat.util.buf.MessageBytes;
import org.apache.tomcat.util.http.MimeHeaders;
import org.springframework.http.HttpHeaders;
import org.springframework.lang.Nullable;
import org.springframework.util.MultiValueMap;
@ -195,6 +196,12 @@ class TomcatHeadersAdapter implements MultiValueMap<String, String> {
}
@Override
public String toString() {
return HttpHeaders.formatHeaders(this);
}
private class EntryIterator implements Iterator<Entry<String, List<String>>> {
private Enumeration<String> names = headers.names();

View File

@ -177,6 +177,12 @@ class UndertowHeadersAdapter implements MultiValueMap<String, String> {
}
@Override
public String toString() {
return org.springframework.http.HttpHeaders.formatHeaders(this);
}
private class EntryIterator implements Iterator<Entry<String, List<String>>> {
private Iterator<HttpString> names = headers.getHeaderNames().iterator();

View File

@ -2,7 +2,7 @@
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{1.} - %msg%n" />
</Console>
</Appenders>
<Loggers>