Changeset 15742 in josm


Ignore:
Timestamp:
2020-01-20T23:18:42+01:00 (4 years ago)
Author:
simon04
Message:

HttpClient: add support for Content-Encoding: deflate

Location:
trunk
Files:
2 edited

Legend:

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

    r15741 r15742  
    2323import java.util.regex.Pattern;
    2424import java.util.zip.GZIPInputStream;
     25import java.util.zip.InflaterInputStream;
    2526
    2627import org.openstreetmap.josm.data.validation.routines.DomainValidator;
     
    112113        }
    113114        this.requestMethod = requestMethod;
    114         this.headers.put("Accept-Encoding", "gzip");
     115        this.headers.put("Accept-Encoding", "gzip, deflate");
    115116    }
    116117
     
    376377            InputStream in = getInputStream();
    377378            in = new ProgressInputStream(in, getContentLength(), monitor);
    378             in = "gzip".equalsIgnoreCase(getContentEncoding()) ? new GZIPInputStream(in) : in;
     379            in = "gzip".equalsIgnoreCase(getContentEncoding())
     380                    ? new GZIPInputStream(in)
     381                    : "deflate".equalsIgnoreCase(getContentEncoding())
     382                    ? new InflaterInputStream(in)
     383                    : in;
    379384            Compression compression = Compression.NONE;
    380385            if (uncompress) {
  • trunk/test/functional/org/openstreetmap/josm/tools/HttpClientTest.java

    r15741 r15742  
    316316    public void testTakesTooLong() throws IOException {
    317317        HttpClient.create(new URL("https://httpbin.org/delay/1")).setReadTimeout(500).connect(progress);
     318    }
     319
     320    /**
     321     * Test reading Deflate-encoded data.
     322     * @throws IOException if any I/O error occurs
     323     */
     324    @Test
     325    public void testDeflate() throws IOException {
     326        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/deflate")).connect(progress);
     327        assertThat(response.getResponseCode(), is(200));
     328        try (InputStream in = response.getContent();
     329             JsonReader json = JsonProvider.provider().createReader(in)) {
     330            assertThat(json.readObject().getBoolean("deflated"), is(true));
     331        }
     332    }
     333
     334    /**
     335     * Test reading Gzip-encoded data.
     336     * @throws IOException if any I/O error occurs
     337     */
     338    @Test
     339    public void testGzip() throws IOException {
     340        final HttpClient.Response response = HttpClient.create(new URL("https://httpbin.org/gzip")).connect(progress);
     341        assertThat(response.getResponseCode(), is(200));
     342        try (InputStream in = response.getContent();
     343             JsonReader json = JsonProvider.provider().createReader(in)) {
     344            assertThat(json.readObject().getBoolean("gzipped"), is(true));
     345        }
    318346    }
    319347
Note: See TracChangeset for help on using the changeset viewer.