diff options
author | Narayan Kamath <narayan@google.com> | 2012-09-24 11:35:16 +0100 |
---|---|---|
committer | Brian Carlstrom <bdc@google.com> | 2012-09-24 23:36:44 -0700 |
commit | 6d41a7cc3cb4cc684c8bece69ddc55954812ad6e (patch) | |
tree | 90048e8fadeb6eb1c55666304d83207e2615b803 | |
parent | fce022be38a2c79f9cdc8d9fea33c72cf2f16528 (diff) | |
download | libcore-6d41a7cc3cb4cc684c8bece69ddc55954812ad6e.zip libcore-6d41a7cc3cb4cc684c8bece69ddc55954812ad6e.tar.gz libcore-6d41a7cc3cb4cc684c8bece69ddc55954812ad6e.tar.bz2 |
Strip content length in requests with "transparent" gzip handling.
We need to strip both the Content-Length and the Content-Encoding
for such requests. In such requests, it will be the length of the
compressed response. We hide the fact that compression is taking place
from clients, so we shouldn't give them the content length either.
(cherry picked from commit f9d60aed414ae21811a6488f603333517f257b22)
Change-Id: Ic2776ecf020c34b1c55e2fb7e2f0728e553187a0
3 files changed, 16 insertions, 2 deletions
diff --git a/luni/src/main/java/libcore/net/http/HttpEngine.java b/luni/src/main/java/libcore/net/http/HttpEngine.java index a370956..8f97c81 100644 --- a/luni/src/main/java/libcore/net/http/HttpEngine.java +++ b/luni/src/main/java/libcore/net/http/HttpEngine.java @@ -524,8 +524,13 @@ public class HttpEngine { /* * If the response was transparently gzipped, remove the gzip header field * so clients don't double decompress. http://b/3009828 + * + * Also remove the Content-Length in this case because it contains the length + * of the gzipped response. This isn't terribly useful and is dangerous because + * clients can query the content length, but not the content encoding. */ responseHeaders.stripContentEncoding(); + responseHeaders.stripContentLength(); responseBodyIn = new GZIPInputStream(transferStream); } else { responseBodyIn = transferStream; diff --git a/luni/src/main/java/libcore/net/http/ResponseHeaders.java b/luni/src/main/java/libcore/net/http/ResponseHeaders.java index 003b445..c0b4200 100644 --- a/luni/src/main/java/libcore/net/http/ResponseHeaders.java +++ b/luni/src/main/java/libcore/net/http/ResponseHeaders.java @@ -187,6 +187,11 @@ public final class ResponseHeaders { headers.removeAll("Content-Encoding"); } + public void stripContentLength() { + contentLength = -1; + headers.removeAll("Content-Length"); + } + public boolean isChunked() { return "chunked".equalsIgnoreCase(transferEncoding); } diff --git a/luni/src/test/java/libcore/java/net/URLConnectionTest.java b/luni/src/test/java/libcore/java/net/URLConnectionTest.java index 762bac4..d7a4544 100644 --- a/luni/src/test/java/libcore/java/net/URLConnectionTest.java +++ b/luni/src/test/java/libcore/java/net/URLConnectionTest.java @@ -989,21 +989,25 @@ public final class URLConnectionTest extends TestCase { URLConnection connection = server.getUrl("/").openConnection(); assertEquals("ABCABCABC", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); assertNull(connection.getContentEncoding()); + assertEquals(-1, connection.getContentLength()); RecordedRequest request = server.takeRequest(); assertContains(request.getHeaders(), "Accept-Encoding: gzip"); } public void testClientConfiguredGzipContentEncoding() throws Exception { + byte[] bodyBytes = gzip("ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes("UTF-8")); server.enqueue(new MockResponse() - .setBody(gzip("ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes("UTF-8"))) - .addHeader("Content-Encoding: gzip")); + .setBody(bodyBytes) + .addHeader("Content-Encoding: gzip") + .addHeader("Content-Length: " + bodyBytes.length)); server.play(); URLConnection connection = server.getUrl("/").openConnection(); connection.addRequestProperty("Accept-Encoding", "gzip"); InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream()); assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", readAscii(gunzippedIn, Integer.MAX_VALUE)); + assertEquals(bodyBytes.length, connection.getContentLength()); RecordedRequest request = server.takeRequest(); assertContains(request.getHeaders(), "Accept-Encoding: gzip"); |