Skip to content

[Chore] CF 下载文件也使用 API Key#6169

Open
ToobLac wants to merge 3 commits into
HMCL-dev:mainfrom
ToobLac:cf-key
Open

[Chore] CF 下载文件也使用 API Key#6169
ToobLac wants to merge 3 commits into
HMCL-dev:mainfrom
ToobLac:cf-key

Conversation

@ToobLac

@ToobLac ToobLac commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Closes #6167

可能需要 backport 到 3.6 等版本

@ToobLac ToobLac changed the title CF 下载文件也使用 API CF 下载文件也使用 API Key Jun 13, 2026
@ToobLac ToobLac marked this pull request as ready for review June 13, 2026 06:15
@ToobLac ToobLac changed the title CF 下载文件也使用 API Key [Chore] CF 下载文件也使用 API Key Jun 13, 2026
@Minecraft269

Copy link
Copy Markdown

要审查就自己运行/gemini review [doge]

@3gf8jv4dv

3gf8jv4dv commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

可能需要 backport 到 3.7

不应该是 3.6 么?

3.15 下个月底就过期了,要给这个分支移植吗?

Comment thread HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java
@ToobLac

ToobLac commented Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

不应该是 3.6 么?

额那就是我记错了

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the handling of the CurseForge API key by exposing it as a public constant and introducing a centralized helper method, NetworkUtils.newRequestBuilder, to automatically inject the API key header for CurseForge domains. Feedback on the changes highlights a potential NullPointerException in NetworkUtils.newRequestBuilder when processing URIs with a null host, recommending a defensive null-check.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +172 to +178
public static java.net.http.HttpRequest.Builder newRequestBuilder(URI uri) {
var builder = java.net.http.HttpRequest.newBuilder(uri);
var apiKey = API_KEYS.get(uri.getHost().toLowerCase(Locale.ROOT));
if (StringUtils.isNotBlank(apiKey))
builder.header("x-api-key", apiKey);
return builder;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

If uri.getHost() returns null (which can happen for non-hierarchical URIs or URIs without a host component), calling toLowerCase() on it will throw a NullPointerException. Guard against a null host to ensure defensive programming and avoid runtime crashes.

    public static java.net.http.HttpRequest.Builder newRequestBuilder(URI uri) {
        var builder = java.net.http.HttpRequest.newBuilder(uri);
        String host = uri.getHost();
        if (host != null) {
            var apiKey = API_KEYS.get(host.toLowerCase(Locale.ROOT));
            if (StringUtils.isNotBlank(apiKey)) {
                builder.header("x-api-key", apiKey);
            }
        }
        return builder;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

对 CurseForge 下载携带 API Key

6 participants