Changeset 9807 in josm for trunk


Ignore:
Timestamp:
2016-02-15T23:39:36+01:00 (9 years ago)
Author:
wiktorn
Message:

Make map returned by getHeaderFields() case insensitive.

RFC 2616, section 4.2, specifies that header names are case insensitive. This is properly handled by getHeaderField(...) in HttpUrlConnection, but in case of getHeaderFields() Map returned by Oracle JDK is case sensitive. By using case insentive map we avoid bigger refactoring of all usages, to use getHeaderField(...) where appropiate.

See #12235: Java Web Start cache returns all headers as lowercase, when real server return mixed case.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/HttpClient.java

    r9529 r9807  
    1212import java.net.HttpURLConnection;
    1313import java.net.URL;
     14import java.util.Collections;
    1415import java.util.List;
    1516import java.util.Locale;
    1617import java.util.Map;
     18import java.util.Map.Entry;
    1719import java.util.Scanner;
    1820import java.util.TreeMap;
     
    385387        /**
    386388         * Returns an unmodifiable Map mapping header keys to a List of header values.
     389         * As per RFC 2616, section 4.2 header names are case insensitive, so returned map is also case insensitive
    387390         * @return unmodifiable Map mapping header keys to a List of header values
    388391         * @see HttpURLConnection#getHeaderFields()
     
    390393         */
    391394        public Map<String, List<String>> getHeaderFields() {
    392             return connection.getHeaderFields();
     395            // returned map from HttpUrlConnection is case sensitive, use case insensitive TreeMap to conform to RFC 2616
     396            Map<String, List<String>> ret = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
     397            for(Entry<String, List<String>> e: connection.getHeaderFields().entrySet()) {
     398                if (e.getKey() != null) {
     399                    ret.put(e.getKey(), e.getValue());
     400                }
     401            }
     402            return Collections.unmodifiableMap(ret);
    393403        }
    394404
  • trunk/test/functional/org/openstreetmap/josm/tools/HttpClientTest.java

    r9669 r9807  
    6969        assertThat(response.getHeaderField("Content-TYPE"), is("application/json"));
    7070        assertThat(response.getHeaderFields().get("Content-Type"), is(Collections.singletonList("application/json")));
    71         assertThat(response.getHeaderFields().get("Content-TYPE"), nullValue());
     71        assertThat(response.getHeaderFields().get("Content-TYPE"), is(Collections.singletonList("application/json")));
    7272        try (final InputStream in = response.getContent();
    7373             final JsonReader json = JsonProvider.provider().createReader(in)) {
Note: See TracChangeset for help on using the changeset viewer.