Skip to content
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

AbstractGoogleClientRequest.initializeMediaUpload is not using applicationName to User-Agent HTTP header #2222

Closed
suztomo opened this issue Jan 17, 2023 · 1 comment · Fixed by #2227
Assignees
Labels
priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@suztomo
Copy link
Member

suztomo commented Jan 17, 2023

Internal bug b/252963877. mediaHttpUploader is not adding a proper User-Agent HTTP header.

        String userAgent = "Foo/1.0";
        Storage storage = new Storage.Builder(rf.getTransport(), jsonFactory, rf.getInitializer())
            .setApplicationName(userAgent)
            .build();
...
        Insert insert = storage.objects().insert(bucket, storageObject, content);
        GenericUrl url = insert.buildHttpRequestUrl();
        MediaHttpUploader mediaHttpUploader = insert.getMediaHttpUploader();

        // This does not add the expected user-agent header to the request.
        HttpResponse upload = mediaHttpUploader.upload(url);

Environment details

  1. Specify the API at the beginning of the title. For example, "BigQuery: ...").
    General, Core, and Other are also allowed as types
  2. OS type and version:
  3. Java version:
  4. version(s):

The com.google.apis:google-api-services-storage:v1-rev20220705-2.0.0 and google-api-client:2.0.0.

Steps to reproduce

Declare dependencies:

    <dependency>
      <groupId>com.google.apis</groupId>
      <artifactId>google-api-services-storage</artifactId>
      <version>v1-rev20220705-2.0.0</version>
    </dependency>

Code example

    // Run with VM option to have -Djava.util.logging.config.file=src/main/resources/logging.properties
    public static void main( String[] args ) throws Exception {
        NetHttpTransport transport = new NetHttpTransport.Builder()
            .trustCertificates(GoogleUtils.getCertificateTrustStore())
            .build();
        HttpRequestFactory rf = transport.createRequestFactory();
        JsonFactory jsonFactory = new GsonFactory();

        String userAgent = "Foo/1.0";
        Storage storage = new Storage.Builder(rf.getTransport(), jsonFactory, rf.getInitializer())
            .setApplicationName(userAgent)
            .build();


        // Project suztomo-terraform-4f9e48 has this bucket.
        String bucket = "suztomo_test_b252963877"; // TODO: replace with your bucket

        StorageObject storageObject = new StorageObject().setName("temp.txt").setBucket(bucket);
        byte[] bytes = "hi".getBytes(StandardCharsets.UTF_8);
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        InputStreamContent content = new InputStreamContent("text/plain", bais);

        Insert insert = storage.objects().insert(bucket, storageObject, content);
        GenericUrl url = insert.buildHttpRequestUrl();
        MediaHttpUploader mediaHttpUploader = insert.getMediaHttpUploader();

        // This does not add the expected user-agent header to the request.
        HttpResponse upload = mediaHttpUploader.upload(url);
        System.out.println("Upload response code: " + upload.getStatusCode() + " " + upload.getStatusMessage());
    }

This does not send the user agent to the server.

External references such as API reference guides

  • ?

Any additional information below

Internal bug b/252963877.

Setting request initializer as the 3rd argument to Storage.Builder is the workaround:

        HttpCredentialsAdapter credentialsInitializer = new HttpCredentialsAdapter(credentials);
        HttpRequestInitializer requestInitializer = new HttpRequestInitializer() {
            @Override
            public void initialize(HttpRequest request) throws IOException {
                credentialsInitializer.initialize(request);
                HttpHeaders headers = request.getHeaders();
                headers.setUserAgent(userAgent);
            }
        };

...(omit)...

        Storage storage = new Storage.Builder(rf.getTransport(), jsonFactory, requestInitializer)
            .setApplicationName(userAgent)
            .build();

But the library users shouldn't need to do that.

@suztomo suztomo self-assigned this Jan 17, 2023
@suztomo suztomo changed the title AbstractGoogleClientRequest.initializeMediaUpload is not copying user-agent HTTP headers AbstractGoogleClientRequest.initializeMediaUpload is not copying User-Agent HTTP header Jan 19, 2023
@suztomo suztomo added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. labels Jan 19, 2023
@suztomo suztomo changed the title AbstractGoogleClientRequest.initializeMediaUpload is not copying User-Agent HTTP header AbstractGoogleClientRequest.initializeMediaUpload is not using applicationName to User-Agent HTTP header Jan 20, 2023
@suztomo
Copy link
Member Author

suztomo commented Jan 20, 2023

At initializeMediaUpload, abstractGoogleClient holds applicationName value.

Screenshot 2023-01-20 at 9 09 26 AM

Which test calls the method? testMediaUpload_XXX in AbstractGoogleClientTest.

AbstractGoogleClientRequest has logic to set requestHeaders

Screenshot 2023-01-20 at 9 49 39 AM

It needs something similar in insert.getMediaHttpUploader().

Should it be done at MediaHttpUploader.resumableUpload (and directUpload)?

Screenshot 2023-01-20 at 9 59 45 AM

Should it be done at MediaHttpUploader's 3rd argument httpRequestInitializer?

=> #2227 to implement it as request initializer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant