From 7a3cfdde7c575d36640b5fedda34c34fac0107a5 Mon Sep 17 00:00:00 2001 From: Blank Date: Tue, 22 Sep 2026 15:28:04 +0800 Subject: [PATCH] =?UTF-8?q?fix(network):=20EndpointParser.ParseAuthority?= =?UTF-8?q?=20=E6=8B=86=E4=B8=BA=E5=8D=95=E4=B8=80=E8=81=8C=E8=B4=A3?= =?UTF-8?q?=E6=B4=BE=E5=8F=91=E4=BB=A5=E6=B6=88=E9=99=A4=20Sonar=20S3776?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 把 ParseAuthority(Cognitive Complexity 17)按方括号 IPv6 vs 末冒号 host:port 两路径拆为 ParseBracketedIpv6Authority + ParseHostPortAuthority 两个 private 辅助 方法;主方法仅保留空 authority 拒收 + 按 authority[0] 派发。对外签名 / 行为 / 异常消息文本完全等价;端口解析复用既有 ParsePort,DNS 校验复用既有 ValidateDnsHost。EndpointParserTests 38/38 通过。 Linear: GFX-860 --- .../Discovery/EndpointParser.cs | 68 ++++++++++++++----- 1 file changed, 50 insertions(+), 18 deletions(-) diff --git a/GameFrameX.NetWork.RemoteMessaging/Discovery/EndpointParser.cs b/GameFrameX.NetWork.RemoteMessaging/Discovery/EndpointParser.cs index e63f72ed..f20ce4a8 100644 --- a/GameFrameX.NetWork.RemoteMessaging/Discovery/EndpointParser.cs +++ b/GameFrameX.NetWork.RemoteMessaging/Discovery/EndpointParser.cs @@ -121,29 +121,61 @@ private static ParsedEndpoint ParseAuthority(string originalEndpoint, string sch // IPv6 方括号字面量:[::1]:port if (authority[0] == '[') { - var closingBracketIndex = authority.IndexOf(']'); - if (closingBracketIndex < 0 || closingBracketIndex == 1) - { - throw new EndpointFormatException($"The endpoint '{originalEndpoint}' has a malformed bracketed IPv6 host: expected '[]:'."); - } + return ParseBracketedIpv6Authority(originalEndpoint, scheme, authority); + } - var ipv6Host = authority.Substring(1, closingBracketIndex - 1); - if (!IPAddress.TryParse(ipv6Host, out var bracketedAddress) || bracketedAddress.AddressFamily != AddressFamily.InterNetworkV6) - { - throw new EndpointFormatException($"The endpoint '{originalEndpoint}' has a bracketed host '{ipv6Host}' that is not a valid IPv6 literal."); - } + return ParseHostPortAuthority(originalEndpoint, scheme, authority); + } - var remainder = authority.Substring(closingBracketIndex + 1); - if (remainder.Length == 0 || remainder[0] != ':') - { - throw new EndpointFormatException($"The endpoint '{originalEndpoint}' is missing the port after the bracketed IPv6 host: expected '[]:'."); - } + /// + /// 解析方括号 IPv6 字面量形式的 authority([ipv6 literal]:port)。 + /// + /// + /// Parses the bracketed IPv6 literal form of authority. + /// + /// 原始输入(仅用于异常消息)/ The original input (used in exception messages only) + /// 已校验的小写 scheme / The validated lower-cased scheme + /// 以 [ 开头的 authority 段 / The authority part starting with [ + /// 解析后的三元组 / The parsed endpoint triple + /// 当结构违规时抛出 / Thrown on structural violations + private static ParsedEndpoint ParseBracketedIpv6Authority(string originalEndpoint, string scheme, string authority) + { + var closingBracketIndex = authority.IndexOf(']'); + if (closingBracketIndex < 0 || closingBracketIndex == 1) + { + throw new EndpointFormatException($"The endpoint '{originalEndpoint}' has a malformed bracketed IPv6 host: expected '[]:'."); + } - var port = ParsePort(originalEndpoint, remainder.Substring(1)); - return new ParsedEndpoint(scheme, ipv6Host, port, EndpointAddressKind.IPv6); + var ipv6Host = authority.Substring(1, closingBracketIndex - 1); + if (!IPAddress.TryParse(ipv6Host, out var bracketedAddress) || bracketedAddress.AddressFamily != AddressFamily.InterNetworkV6) + { + throw new EndpointFormatException($"The endpoint '{originalEndpoint}' has a bracketed host '{ipv6Host}' that is not a valid IPv6 literal."); } - // 域名/容器名/Service 名/IPv4:最后一个冒号分隔端口 + var remainder = authority.Substring(closingBracketIndex + 1); + if (remainder.Length == 0 || remainder[0] != ':') + { + throw new EndpointFormatException($"The endpoint '{originalEndpoint}' is missing the port after the bracketed IPv6 host: expected '[]:'."); + } + + var port = ParsePort(originalEndpoint, remainder.Substring(1)); + return new ParsedEndpoint(scheme, ipv6Host, port, EndpointAddressKind.IPv6); + } + + /// + /// 解析非方括号形式的 authority(域名/容器名/Service 名/IPv4,最后一个冒号分隔端口)。 + /// + /// + /// Parses the non-bracketed form of authority (domain / container / Service name + /// / IPv4 literal), splitting on the last colon into host and port. + /// + /// 原始输入(仅用于异常消息)/ The original input (used in exception messages only) + /// 已校验的小写 scheme / The validated lower-cased scheme + /// 非方括号开头的 authority 段 / The authority part not starting with [ + /// 解析后的三元组 / The parsed endpoint triple + /// 当结构违规时抛出 / Thrown on structural violations + private static ParsedEndpoint ParseHostPortAuthority(string originalEndpoint, string scheme, string authority) + { var lastColonIndex = authority.LastIndexOf(':'); if (lastColonIndex < 0 || lastColonIndex == authority.Length - 1) {