Skip to content

fix: media upload to have applicationName as User-Agent #2227

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 4 commits into from
Jan 20, 2023
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 @@ -351,15 +351,42 @@ public final MediaHttpUploader getMediaHttpUploader() {
*/
protected final void initializeMediaUpload(AbstractInputStreamContent mediaContent) {
HttpRequestFactory requestFactory = abstractGoogleClient.getRequestFactory();
String applicationName = abstractGoogleClient.getApplicationName();
HttpRequestInitializer requestInitializer =
mediaUploadRequestUserAgentInitializer(applicationName, requestFactory.getInitializer());
this.uploader =
Comment on lines +354 to 357
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Special handling for applicationName,
Screenshot 2023-01-20 at 9 49 39 AM

new MediaHttpUploader(
mediaContent, requestFactory.getTransport(), requestFactory.getInitializer());
new MediaHttpUploader(mediaContent, requestFactory.getTransport(), requestInitializer);
this.uploader.setInitiationRequestMethod(requestMethod);
if (httpContent != null) {
this.uploader.setMetadata(httpContent);
}
}

private static HttpRequestInitializer mediaUploadRequestUserAgentInitializer(
final String applicationName, final HttpRequestInitializer originalInitializer) {
if (applicationName == null) {
return originalInitializer;
}
if (originalInitializer == null) {
return new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) {
HttpHeaders headers = request.getHeaders();
headers.setUserAgent(applicationName);
}
};
} else {
return new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
originalInitializer.initialize(request);
HttpHeaders headers = request.getHeaders();
headers.setUserAgent(applicationName);
}
};
}
}

/** Returns the media HTTP downloader or {@code null} for none. */
public final MediaHttpDownloader getMediaHttpDownloader() {
return downloader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.api.client.googleapis.testing.services.MockGoogleClient;
import com.google.api.client.googleapis.testing.services.MockGoogleClientRequest;
import com.google.api.client.http.EmptyContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpExecuteInterceptor;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
Expand All @@ -33,6 +34,8 @@
import com.google.api.client.util.Key;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;

/**
Expand Down Expand Up @@ -132,6 +135,7 @@ private static class MediaTransport extends MockHttpTransport {
int bytesUploaded;
int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE;
boolean contentLengthNotSpecified;
List<String> userAgentsRecorded = new ArrayList<>();

protected MediaTransport() {}

Expand Down Expand Up @@ -169,6 +173,7 @@ public LowLevelHttpResponse execute() {
String expectedContentRange = "bytes " + bytesRange + "/" + contentLength;
assertEquals(expectedContentRange, getFirstHeaderValue("Content-Range"));
bytesUploaded += MediaHttpUploader.DEFAULT_CHUNK_SIZE;
userAgentsRecorded.add(getFirstHeaderValue("User-Agent"));

if (bytesUploaded == contentLength) {
// Return 200 since the upload is complete.
Expand Down Expand Up @@ -207,6 +212,32 @@ public void testMediaUpload() throws Exception {
assertEquals("somevalue", result.foo);
}

public void testMediaUpload_applicationNameAsUserAgent() throws Exception {
MediaTransport fakeTransport = new MediaTransport();
String applicationName = "Foo/1.0 (BAR:Baz/1.0) XYZ/1.0";
AbstractGoogleClient client =
new MockGoogleClient.Builder(
fakeTransport, TEST_RESUMABLE_REQUEST_URL, "", JSON_OBJECT_PARSER, null)
.setApplicationName(applicationName)
.build();
InputStream is = new ByteArrayInputStream(new byte[MediaHttpUploader.DEFAULT_CHUNK_SIZE]);
InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
mediaContent.setLength(MediaHttpUploader.DEFAULT_CHUNK_SIZE);
MockGoogleClientRequest<A> rq =
new MockGoogleClientRequest<A>(client, "POST", "", null, A.class);

rq.initializeMediaUpload(mediaContent);
MediaHttpUploader mediaHttpUploader = rq.getMediaHttpUploader();
mediaHttpUploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));

assertEquals(1, fakeTransport.userAgentsRecorded.size());
for (String userAgent : fakeTransport.userAgentsRecorded) {
assertTrue(
"UserAgent header does not have expected value in requests",
userAgent.contains(applicationName));
}
}

private static class GZipCheckerInitializer implements HttpRequestInitializer {

private boolean gzipDisabled;
Expand Down