Made getting a header by field-name case-insensitive to address the issue from

https://groups.google.com/forum/?fromgroups#!topic/jclouds/lEZjqhbudX4

This is the proper way to handle it as RFC 2616, "Hypertext Transfer Protocol -- HTTP/1.1", Section 4.2, "Message Headers" states,

Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive.
This commit is contained in:
Everett Toews 2012-08-20 15:30:50 -05:00
parent aff96befbe
commit 8d9499b3c1
1 changed files with 14 additions and 1 deletions

View File

@ -32,6 +32,7 @@ import org.jclouds.util.Multimaps2;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper; import com.google.common.base.Objects.ToStringHelper;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
@ -185,13 +186,25 @@ public class HttpMessage extends PayloadEnclosingImpl {
return headers; return headers;
} }
private Multimap<String, String> getLowercaseHeaders() {
Multimap<String, String> lowercaseHeaders = HashMultimap.create(getHeaders().keys().size(), 1);
for (String header: getHeaders().keys()) {
for (String value: getHeaders().get(header)) {
lowercaseHeaders.put(header.toLowerCase(), value);
}
}
return lowercaseHeaders;
}
/** /**
* try to get the value, then try as lowercase. * try to get the value, then try as lowercase.
*/ */
public String getFirstHeaderOrNull(String string) { public String getFirstHeaderOrNull(String string) {
Collection<String> values = headers.get(string); Collection<String> values = headers.get(string);
if (values.size() == 0) if (values.size() == 0)
values = headers.get(string.toLowerCase()); values = getLowercaseHeaders().get(string.toLowerCase());
return (values.size() >= 1) ? values.iterator().next() : null; return (values.size() >= 1) ? values.iterator().next() : null;
} }