Skip to content

Commit 3991f1c

Browse files
authored
Accept compressed responses from the backend. (#817)
1 parent 0778d94 commit 3991f1c

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

transport/transport-backend-cct/src/main/java/com/google/android/datatransport/cct/CctTransportBackend.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.util.List;
5656
import java.util.Map;
5757
import java.util.TimeZone;
58+
import java.util.zip.GZIPInputStream;
5859
import java.util.zip.GZIPOutputStream;
5960

6061
final class CctTransportBackend implements TransportBackend {
@@ -63,6 +64,7 @@ final class CctTransportBackend implements TransportBackend {
6364

6465
private static final int CONNECTION_TIME_OUT = 30000;
6566
private static final int READ_TIME_OUT = 40000;
67+
private static final String ACCEPT_ENCODING_HEADER_KEY = "Accept-Encoding";
6668
private static final String CONTENT_ENCODING_HEADER_KEY = "Content-Encoding";
6769
private static final String GZIP_CONTENT_ENCODING = "gzip";
6870
private static final String CONTENT_TYPE_HEADER_KEY = "Content-Type";
@@ -226,6 +228,7 @@ private HttpResponse doSend(HttpRequest request) throws IOException {
226228
"User-Agent", String.format("datatransport/%s android/", BuildConfig.VERSION_NAME));
227229
connection.setRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING);
228230
connection.setRequestProperty(CONTENT_TYPE_HEADER_KEY, PROTOBUF_CONTENT_TYPE);
231+
connection.setRequestProperty(ACCEPT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING);
229232

230233
if (request.apiKey != null) {
231234
connection.setRequestProperty(API_KEY_HEADER_KEY, request.apiKey);
@@ -244,7 +247,8 @@ private HttpResponse doSend(HttpRequest request) throws IOException {
244247
channel.write(ByteBuffer.wrap(output.toByteArray()));
245248
int responseCode = connection.getResponseCode();
246249
Logging.i(LOG_TAG, "Status Code: " + responseCode);
247-
Logging.i(LOG_TAG, "Content-Type:" + connection.getHeaderField("Content-Type"));
250+
Logging.i(LOG_TAG, "Content-Type: " + connection.getHeaderField("Content-Type"));
251+
Logging.i(LOG_TAG, "Content-Encoding: " + connection.getHeaderField("Content-Encoding"));
248252

249253
if (responseCode == 302 || responseCode == 301) {
250254
String redirect = connection.getHeaderField("Location");
@@ -254,7 +258,13 @@ private HttpResponse doSend(HttpRequest request) throws IOException {
254258
return new HttpResponse(responseCode, null, 0);
255259
}
256260

257-
InputStream inputStream = connection.getInputStream();
261+
InputStream inputStream;
262+
String contentEncoding = connection.getHeaderField(CONTENT_ENCODING_HEADER_KEY);
263+
if (contentEncoding != null && contentEncoding.equals(GZIP_CONTENT_ENCODING)) {
264+
inputStream = new GZIPInputStream(connection.getInputStream());
265+
} else {
266+
inputStream = connection.getInputStream();
267+
}
258268
try {
259269
long nextRequestMillis = LogResponse.parseFrom(inputStream).getNextRequestWaitMillis();
260270
return new HttpResponse(responseCode, null, nextRequestMillis);

transport/transport-backend-cct/src/test/java/com/google/android/datatransport/cct/CctTransportBackendTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
import com.google.android.datatransport.runtime.backends.BackendResponse;
4444
import com.google.android.datatransport.runtime.time.TestClock;
4545
import com.google.protobuf.ByteString;
46+
import java.io.ByteArrayOutputStream;
4647
import java.util.Arrays;
48+
import java.util.zip.GZIPOutputStream;
4749
import org.junit.Rule;
4850
import org.junit.Test;
4951
import org.junit.runner.RunWith;
@@ -368,6 +370,37 @@ public void send_whenBackendRedirectsMoreThan5Times_shouldOnlyRedirect4Times() {
368370
assertEquals(BackendResponse.fatalError(), response);
369371
}
370372

373+
@Test
374+
public void send_CompressedResponseIsUncompressed() throws Exception {
375+
ByteArrayOutputStream output = new ByteArrayOutputStream();
376+
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(output);
377+
gzipOutputStream.write(
378+
LogResponse.newBuilder().setNextRequestWaitMillis(3).build().toByteArray());
379+
gzipOutputStream.close();
380+
381+
stubFor(
382+
post(urlEqualTo("/api"))
383+
.willReturn(
384+
aResponse()
385+
.withStatus(200)
386+
.withHeader("Content-Type", "application/x-protobuf;charset=UTF8;hello=world")
387+
.withHeader("Content-Encoding", "gzip")
388+
.withBody(output.toByteArray())));
389+
390+
BackendRequest backendRequest = getCCTBackendRequest();
391+
wallClock.tick();
392+
uptimeClock.tick();
393+
394+
BackendResponse response = BACKEND.send(backendRequest);
395+
396+
verify(
397+
postRequestedFor(urlEqualTo("/api"))
398+
.withHeader("Content-Type", equalTo("application/x-protobuf"))
399+
.withHeader("Content-Encoding", equalTo("gzip")));
400+
401+
assertEquals(BackendResponse.ok(3), response);
402+
}
403+
371404
// When there is no active network, the ConnectivityManager returns null when
372405
// getActiveNetworkInfo() is called.
373406
@Implements(ConnectivityManager.class)

0 commit comments

Comments
 (0)