Skip to content

Accept compressed responses from the backend. #817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

final class CctTransportBackend implements TransportBackend {
Expand All @@ -63,6 +64,7 @@ final class CctTransportBackend implements TransportBackend {

private static final int CONNECTION_TIME_OUT = 30000;
private static final int READ_TIME_OUT = 40000;
private static final String ACCEPT_ENCODING_HEADER_KEY = "Accept-Encoding";
private static final String CONTENT_ENCODING_HEADER_KEY = "Content-Encoding";
private static final String GZIP_CONTENT_ENCODING = "gzip";
private static final String CONTENT_TYPE_HEADER_KEY = "Content-Type";
Expand Down Expand Up @@ -226,6 +228,7 @@ private HttpResponse doSend(HttpRequest request) throws IOException {
"User-Agent", String.format("datatransport/%s android/", BuildConfig.VERSION_NAME));
connection.setRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING);
connection.setRequestProperty(CONTENT_TYPE_HEADER_KEY, PROTOBUF_CONTENT_TYPE);
connection.setRequestProperty(ACCEPT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING);

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

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

InputStream inputStream = connection.getInputStream();
InputStream inputStream;
String contentEncoding = connection.getHeaderField(CONTENT_ENCODING_HEADER_KEY);
if (contentEncoding != null && contentEncoding.equals(GZIP_CONTENT_ENCODING)) {
inputStream = new GZIPInputStream(connection.getInputStream());
} else {
inputStream = connection.getInputStream();
}
try {
long nextRequestMillis = LogResponse.parseFrom(inputStream).getNextRequestWaitMillis();
return new HttpResponse(responseCode, null, nextRequestMillis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
import com.google.android.datatransport.runtime.backends.BackendResponse;
import com.google.android.datatransport.runtime.time.TestClock;
import com.google.protobuf.ByteString;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.zip.GZIPOutputStream;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -368,6 +370,37 @@ public void send_whenBackendRedirectsMoreThan5Times_shouldOnlyRedirect4Times() {
assertEquals(BackendResponse.fatalError(), response);
}

@Test
public void send_CompressedResponseIsUncompressed() throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(output);
gzipOutputStream.write(
LogResponse.newBuilder().setNextRequestWaitMillis(3).build().toByteArray());
gzipOutputStream.close();

stubFor(
post(urlEqualTo("/api"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/x-protobuf;charset=UTF8;hello=world")
.withHeader("Content-Encoding", "gzip")
.withBody(output.toByteArray())));

BackendRequest backendRequest = getCCTBackendRequest();
wallClock.tick();
uptimeClock.tick();

BackendResponse response = BACKEND.send(backendRequest);

verify(
postRequestedFor(urlEqualTo("/api"))
.withHeader("Content-Type", equalTo("application/x-protobuf"))
.withHeader("Content-Encoding", equalTo("gzip")));

assertEquals(BackendResponse.ok(3), response);
}

// When there is no active network, the ConnectivityManager returns null when
// getActiveNetworkInfo() is called.
@Implements(ConnectivityManager.class)
Expand Down