Skip to content

使用 HttpURLConnection 替换 HttpClient 实现#6219

Merged
Glavo merged 4 commits into
HMCL-dev:mainfrom
Glavo:download
Jun 26, 2026
Merged

使用 HttpURLConnection 替换 HttpClient 实现#6219
Glavo merged 4 commits into
HMCL-dev:mainfrom
Glavo:download

Conversation

@Glavo

@Glavo Glavo commented Jun 25, 2026

Copy link
Copy Markdown
Member

@Glavo

Glavo commented Jun 25, 2026

Copy link
Copy Markdown
Member Author

/gemini review

@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 replaces the usage of Java's HttpClient and HttpResponse with HttpURLConnection across various download and fetch tasks, updating UrlResponseInfo to wrap HttpURLConnection metadata. Additionally, it increases the default retry count to 5, increases the default buffer size to 32KB, and reduces the network timeout to 10 seconds. The review feedback highlights a potential NullPointerException in UrlResponseInfo when copying header values if they contain null elements, suggesting a safe filtering approach.

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 +52 to +60
LinkedHashMap<String, List<String>> headers = new LinkedHashMap<>();
for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {
String name = entry.getKey();
List<String> values = entry.getValue();
if (name != null && values != null) {
headers.put(name, List.copyOf(values));
}
}
return HttpHeaders.of(headers, (name, value) -> true);

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.

medium

Using List.copyOf(values) can throw a NullPointerException if the values list contains any null elements. While standard HTTP headers typically do not have null values, some custom or mock URLConnection implementations (e.g., in tests or plugins) might return lists containing null elements.

To prevent potential NullPointerExceptions, we can filter out any null elements using Java Streams before collecting them into an unmodifiable list.

Suggested change
LinkedHashMap<String, List<String>> headers = new LinkedHashMap<>();
for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {
String name = entry.getKey();
List<String> values = entry.getValue();
if (name != null && values != null) {
headers.put(name, List.copyOf(values));
}
}
return HttpHeaders.of(headers, (name, value) -> true);
LinkedHashMap<String, List<String>> headers = new LinkedHashMap<>();
for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {
String name = entry.getKey();
List<String> values = entry.getValue();
if (name != null && values != null) {
headers.put(name, values.stream().filter(v -> v != null).toList());
}
}
return HttpHeaders.of(headers, (name, value) -> true);

@Glavo Glavo merged commit 9e2f1d3 into HMCL-dev:main Jun 26, 2026
2 checks passed
@Glavo Glavo deleted the download branch June 26, 2026 12:03
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.

1 participant