Skip to content

Commit 5c32469

Browse files
author
Yaniv Inbar
committed
[api] changed to support http issue 115
http://codereview.appspot.com/6458156/
1 parent 5849028 commit 5c32469

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

google-api-client/src/main/java/com/google/api/client/googleapis/MethodOverride.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.api.client.googleapis;
1616

17+
import com.google.api.client.http.EmptyContent;
1718
import com.google.api.client.http.HttpExecuteInterceptor;
1819
import com.google.api.client.http.HttpMethod;
1920
import com.google.api.client.http.HttpRequest;
@@ -79,8 +80,10 @@ public void intercept(HttpRequest request) {
7980
HttpMethod method = request.getMethod();
8081
request.setMethod(HttpMethod.POST);
8182
request.getHeaders().set("X-HTTP-Method-Override", method.name());
82-
// Google servers will fail to process a POST unless the Content-Length header >= 1
83-
request.setAllowEmptyContent(false);
83+
// Google servers will fail to process a POST unless the Content-Length header is specified
84+
if (request.getContent() == null) {
85+
request.setContent(new EmptyContent());
86+
}
8487
}
8588
}
8689

google-api-client/src/main/java/com/google/api/client/googleapis/media/MediaHttpUploader.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import com.google.api.client.googleapis.GoogleHeaders;
1818
import com.google.api.client.googleapis.MethodOverride;
1919
import com.google.api.client.http.AbstractInputStreamContent;
20-
import com.google.api.client.http.ByteArrayContent;
20+
import com.google.api.client.http.EmptyContent;
2121
import com.google.api.client.http.GenericUrl;
2222
import com.google.api.client.http.HttpContent;
2323
import com.google.api.client.http.HttpMethod;
@@ -268,7 +268,6 @@ public HttpResponse upload(GenericUrl initiationRequestUrl) throws IOException {
268268
while (true) {
269269
currentRequest = requestFactory.buildPutRequest(uploadUrl, null);
270270
new MethodOverride().intercept(currentRequest); // needed for PUT
271-
currentRequest.setAllowEmptyContent(false);
272271
setContentAndHeadersOnCurrentRequest(bytesUploaded);
273272
if (backOffPolicyEnabled) {
274273
// Set MediaExponentialBackOffPolicy as the BackOffPolicy of the HTTP Request which will
@@ -326,13 +325,13 @@ private HttpResponse executeUploadInitiation(GenericUrl initiationRequestUrl) th
326325
updateStateAndNotifyListener(UploadState.INITIATION_STARTED);
327326

328327
initiationRequestUrl.put("uploadType", "resumable");
328+
HttpContent content = metadata == null ? new EmptyContent() : metadata;
329329
HttpRequest request =
330-
requestFactory.buildRequest(initiationMethod, initiationRequestUrl, metadata);
330+
requestFactory.buildRequest(initiationMethod, initiationRequestUrl, content);
331331
addMethodOverride(request);
332332
initiationHeaders.setUploadContentType(mediaContent.getType());
333333
initiationHeaders.setUploadContentLength(getMediaContentLength());
334334
request.setHeaders(initiationHeaders);
335-
request.setAllowEmptyContent(false);
336335
request.setRetryOnExecuteIOException(true);
337336
request.setEnableGZipContent(true);
338337
HttpResponse response = request.execute();
@@ -400,9 +399,6 @@ public void serverErrorCallback() throws IOException {
400399
// Query the current status of the upload by issuing an empty POST request on the upload URI.
401400
HttpRequest request = requestFactory.buildPutRequest(currentRequest.getUrl(), null);
402401
new MethodOverride().intercept(request); // needed for PUT
403-
// The resumable media upload protocol requires Content-Length to be 0.
404-
request.setAllowEmptyContent(true);
405-
request.setContent(new ByteArrayContent(null, new byte[0]));
406402

407403
request.getHeaders().setContentRange("bytes */" + getMediaContentLength());
408404
request.setThrowExceptionOnExecuteError(false);

google-api-client/src/main/java/com/google/api/client/googleapis/services/GoogleClient.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.api.client.googleapis.batch.BatchRequest;
1919
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
2020
import com.google.api.client.googleapis.subscriptions.SubscriptionManager;
21+
import com.google.api.client.http.EmptyContent;
2122
import com.google.api.client.http.GenericUrl;
2223
import com.google.api.client.http.HttpMethod;
2324
import com.google.api.client.http.HttpRequest;
@@ -215,8 +216,10 @@ protected HttpRequest buildHttpRequest(HttpMethod method, GenericUrl url, Object
215216
throws IOException {
216217
HttpRequest httpRequest = super.buildHttpRequest(method, url, body);
217218
new MethodOverride().intercept(httpRequest);
218-
// Google servers will fail to process a POST/PUT/PATCH unless the Content-Length header >= 1
219-
httpRequest.setAllowEmptyContent(false);
219+
// custom methods may use POST with no content but require a Content-Length header
220+
if (body == null && method.equals(HttpMethod.POST)) {
221+
httpRequest.setContent(new EmptyContent());
222+
}
220223
return httpRequest;
221224
}
222225

0 commit comments

Comments
 (0)