aliyun / aliyun/alibabacloud-java-async-sdk

[SLS SDK] GetLogsV2 fails to decompress response when acceptEncoding is set (lz4/gzip/deflate)

Open
#16 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
34
Forks
29
PR merge metrics
No merged PRs in 30d

Description

Description

When using GetLogsV2Request with acceptEncoding("lz4") (or gzip/deflate), the SDK fails to parse the response with MalformedJsonException because the compressed response body is not decompressed before JSON parsing.

Environment

  • SDK Version: com.aliyun:alibabacloud-sls20201230:4.0.12
  • Java Version: 8
  • OS: macOS

Steps to Reproduce

GetLogsV2Request request = GetLogsV2Request.builder()
        .acceptEncoding("lz4")  // or "gzip", "deflate"
        .project("xxx")
        .logstore("xxx")
        .from(xxx)
        .to(xxx)
        .query("*")
        .build();

CompletableFuture<GetLogsV2Response> response = client.getLogsV2(request);
GetLogsV2Response resp = response.get();  // throws exception

Expected Behavior

The SDK should decompress the response body based on Content-Encoding header before parsing JSON.

Actual Behavior

darabonba.core.exception.ClientException: Parsing response body fail
Caused by: com.google.gson.JsonSyntaxException: MalformedJsonException at line 1 column 30

Root Cause Analysis

In ResponseBodyInterceptor.handleResponseBody():

private void handleResponseBody(InterceptorContext context, TeaResponse response) {
    String bodyStr = response.httpResponse().getBodyAsString();  // ← No decompression!
    if (bodyStr != null && !bodyStr.isEmpty()) {
        Object _body = ParseUtil.parseJSON(bodyStr);  // ← Fails on compressed data
        // ...
    }
}

The code directly converts the response body to string without checking Content-Encoding header and decompressing.

Suggested Fix

private void handleResponseBody(InterceptorContext context, TeaResponse response) {
    String encoding = response.httpResponse().getHeader("Content-Encoding");
    byte[] bodyBytes = response.httpResponse().getBodyAsBytes();
    
    if ("lz4".equalsIgnoreCase(encoding)) {
        bodyBytes = Lz4Utils.decompress(bodyBytes);
    } else if ("gzip".equalsIgnoreCase(encoding)) {
        bodyBytes = GzipUtils.decompress(bodyBytes);
    } else if ("deflate".equalsIgnoreCase(encoding)) {
        bodyBytes = DeflateUtils.decompress(bodyBytes);
    }
    
    String bodyStr = new String(bodyBytes, StandardCharsets.UTF_8);
    // ... rest of the code
}

Workaround

Currently using acceptEncoding("") to disable compression.

Additional Finding

  • SLS GetLogsV2 API only supports lz4 compression (gzip/deflate returns "Unsupported compress type")
  • The web console can display lz4 results correctly (it decompresses internally)
  • SDK with acceptEncoding("") works, but the API documentation says it's required
  • SDK with acceptEncoding("lz4") fails because ResponseBodyInterceptor doesn't decompress lz4

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in ResponseBodyInterceptor.handleResponseBody(), where the issue identifies getBodyAsString() and ParseUtil.parseJSON() as the failing path. Reproduce the GetLogsV2Request with acceptEncoding("lz4"), then verify that the response is decompressed according to Content-Encoding before JSON parsing while uncompressed responses still work.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.