From cfab42b691fec051dd59ff8726d91028fcb65ce4 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Thu, 3 Dec 2026 00:12:58 -0800 Subject: [PATCH] fix: skip GZipEncoding when request content is EmptyContent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The guard that installs GZipEncoding in AbstractGoogleClientRequest.buildHttpRequest() now also requires that the request content is not EmptyContent, so an empty-body request is always sent uncompressed with Content-Length: 0 regardless of the gzip setting. Non-empty content is unaffected: still gzipped by default, and plain when disableGZipContent is set. New tests cover empty content on POST, PUT and PATCH — asserting on the wire that no Content-Encoding header is sent and the content length is 0, including the method-override path where PATCH is rewritten as POST — plus the default-gzip, opt-out and explicit EmptyContent cases for non-empty content. Fixes #1548 --- .../services/AbstractGoogleClientRequest.java | 2 +- .../AbstractGoogleClientRequestTest.java | 83 +++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequest.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequest.java index c9a7b5967..5b4636e6d 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequest.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequest.java @@ -463,7 +463,7 @@ private HttpRequest buildHttpRequest(boolean usingHead) throws IOException { httpRequest.setContent(new EmptyContent()); } httpRequest.getHeaders().putAll(requestHeaders); - if (!disableGZipContent) { + if (!disableGZipContent && !(httpRequest.getContent() instanceof EmptyContent)) { httpRequest.setEncoding(new GZipEncoding()); } httpRequest.setResponseReturnRawInputStream(returnRawInputStream); diff --git a/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequestTest.java b/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequestTest.java index 79e156b34..299e2bea4 100644 --- a/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequestTest.java +++ b/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequestTest.java @@ -16,7 +16,9 @@ import com.google.api.client.googleapis.services.AbstractGoogleClientRequest.ApiClientVersion; import com.google.api.client.googleapis.testing.services.MockGoogleClient; import com.google.api.client.googleapis.testing.services.MockGoogleClientRequest; +import com.google.api.client.http.ByteArrayContent; import com.google.api.client.http.EmptyContent; +import com.google.api.client.http.GZipEncoding; import com.google.api.client.http.HttpMethods; import com.google.api.client.http.HttpRequest; import com.google.api.client.http.HttpResponseException; @@ -163,6 +165,87 @@ private void subtestBuildHttpRequest_emptyContent(String method, boolean expectE } } + public void testBuildHttpRequest_emptyContentDisablesGZip() throws Exception { + for (String method : Arrays.asList("POST", "PUT", "PATCH")) { + subtestBuildHttpRequest_emptyContentDisablesGZip(method); + } + } + + private void subtestBuildHttpRequest_emptyContentDisablesGZip(String method) throws Exception { + HttpTransport transport = + new MockHttpTransport() { + @Override + public LowLevelHttpRequest buildRequest(String method, String url) { + return new MockLowLevelHttpRequest() { + @Override + public LowLevelHttpResponse execute() { + // empty content is sent without Content-Encoding and with Content-Length: 0, + // otherwise some servers reject the request (see GitHub issue 1548) + assertNull(getContentEncoding()); + assertEquals(0, getContentLength()); + return new MockLowLevelHttpResponse(); + } + }; + } + }; + MockGoogleClient client = + new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null) + .setApplicationName("Test Application") + .build(); + MockGoogleClientRequest request = + new MockGoogleClientRequest(client, method, URI_TEMPLATE, null, String.class); + HttpRequest httpRequest = request.buildHttpRequest(); + assertNull(httpRequest.getEncoding()); + request.executeUnparsed(); + } + + public void testBuildHttpRequest_nonEmptyContentGZipEncoded() throws Exception { + HttpTransport transport = new MockHttpTransport(); + MockGoogleClient client = + new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null) + .setApplicationName("Test Application") + .build(); + MockGoogleClientRequest request = + new MockGoogleClientRequest( + client, + HttpMethods.POST, + URI_TEMPLATE, + new ByteArrayContent("application/json", new byte[10]), + String.class); + // non-empty content is GZip encoded by default + assertTrue(request.buildHttpRequest().getEncoding() instanceof GZipEncoding); + // ... unless GZip compression is disabled + request.setDisableGZipContent(true); + assertNull(request.buildHttpRequest().getEncoding()); + // ... and content explicitly set to EmptyContent is never GZip encoded + request = + new MockGoogleClientRequest( + client, HttpMethods.POST, URI_TEMPLATE, new EmptyContent(), String.class); + assertNull(request.buildHttpRequest().getEncoding()); + } + + public void testBuildHttpRequest_methodOverrideEmptyContent() throws Exception { + HttpTransport transport = + new MockHttpTransport() { + @Override + public boolean supportsMethod(String method) throws IOException { + // the transport does not support PATCH, so the request is overridden with POST + return false; + } + }; + MockGoogleClient client = + new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null) + .setApplicationName("Test Application") + .build(); + MockGoogleClientRequest request = + new MockGoogleClientRequest( + client, HttpMethods.PATCH, URI_TEMPLATE, null, String.class); + HttpRequest httpRequest = request.buildHttpRequest(); + assertEquals("POST", httpRequest.getRequestMethod()); + assertTrue(httpRequest.getContent() instanceof EmptyContent); + assertNull(httpRequest.getEncoding()); + } + public void testCheckRequiredParameter() throws Exception { HttpTransport transport = new MockHttpTransport(); MockGoogleClient client =