Skip to content

使用 WebURL for Java 解析 URI#6084

Merged
Glavo merged 10 commits into
HMCL-dev:mainfrom
Glavo:weburl
May 12, 2026
Merged

使用 WebURL for Java 解析 URI#6084
Glavo merged 10 commits into
HMCL-dev:mainfrom
Glavo:weburl

Conversation

@Glavo
Copy link
Copy Markdown
Member

@Glavo Glavo commented May 10, 2026

Java 标准库的 URI 类仅支持老旧的 RFC 2396 标准,无法解析大量现实中的 URL。

为了解决这个问题,我开发了 WebURL for Java 库,支持解析符合 WHATWG URL 规范的 URL。

此外,该库还支持宽松解析模式,支持类似浏览器地址栏的启发式算法补全 URL(比如将 glavo.site 解析为 https://glavo.site),更适合处理用户的输入。

@Glavo
Copy link
Copy Markdown
Member Author

Glavo commented May 12, 2026

/gemini review

Copy link
Copy Markdown
Contributor

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

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 integrates the WebURL library to enhance URL parsing and validation across the project. Key changes include updating NetworkUtils to utilize WebURL, refactoring UI components like FXUtils and AccountListPage to use the new parsing logic, and adding the weburl dependency to the build configuration. Review feedback highlights a potential breaking change in FXUtils and suggests maintaining architectural consistency by using NetworkUtils wrappers instead of direct external library calls.

Comment thread HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java Outdated
Comment thread HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountListPage.java Outdated
@Glavo
Copy link
Copy Markdown
Member Author

Glavo commented May 12, 2026

/gemini review

Copy link
Copy Markdown
Contributor

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

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 integrates the WebURL library to enhance URL parsing and validation across the application, replacing manual URI handling in several components including FXUtils, URLValidator, and NetworkUtils. The review feedback identifies opportunities to improve robustness in NetworkUtils by using WebURL's lenient browser-style parsing for user-provided inputs, ensuring consistency with the implementation in other modules.


public static HttpURLConnection createHttpConnection(String url) throws IOException {
return (HttpURLConnection) createConnection(toURI(url));
return (HttpURLConnection) createConnection(WebURL.parse(url));
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

根据 PR 描述,WebURL 库支持宽松解析模式(parseBrowserInput),这更适合处理用户输入的 URL(例如自动补全协议)。为了与 AuthlibInjectorServerDecoratorController 中的改动保持一致,建议在这里也使用 WebURL.parseBrowserInput(url) 而不是严格的 WebURL.parse(url),以提高对各种输入格式的兼容性。

Suggested change
return (HttpURLConnection) createConnection(WebURL.parse(url));
return (HttpURLConnection) createConnection(WebURL.parseBrowserInput(url));

}

return new ResponseInfoImpl(response.statusCode(), response.headers(), response.version());
return WebURL.toURI(uri);
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

同上,如果 toURI(String) 方法预期会处理可能不完整的用户输入字符串,建议使用宽松解析模式。这可以确保像 glavo.site 这样的输入能被正确启发式补全并转换为 URI,从而维持或提升原有代码的鲁棒性。

Suggested change
return WebURL.toURI(uri);
return WebURL.parseBrowserInput(uri).toURI();

@Glavo Glavo merged commit 5803235 into HMCL-dev:main May 12, 2026
2 checks passed
@Glavo Glavo deleted the weburl branch May 12, 2026 13:43
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.

2 participants