mirror of
https://gitee.com/dromara/hutool.git
synced 2025-06-28 13:34:09 +08:00
增加PunyCode处理完整域名
This commit is contained in:
parent
2dd90253e2
commit
5ca54bb054
@ -11,6 +11,7 @@
|
|||||||
* 【core 】 NumberUtil.parseInt忽略科学计数法(issue#I5M55F@Gitee)
|
* 【core 】 NumberUtil.parseInt忽略科学计数法(issue#I5M55F@Gitee)
|
||||||
* 【core 】 IterUtil.getFirst优化(pr#753@Gitee)
|
* 【core 】 IterUtil.getFirst优化(pr#753@Gitee)
|
||||||
* 【core 】 增加Tree add 类型校验(pr#2542@Github)
|
* 【core 】 增加Tree add 类型校验(pr#2542@Github)
|
||||||
|
* 【core 】 增加PunyCode处理完整域名(pr#2543@Github)
|
||||||
*
|
*
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【http 】 修复https下可能的Patch、Get请求失效问题(issue#I3Z3DH@Gitee)
|
* 【http 】 修复https下可能的Patch、Get请求失效问题(issue#I3Z3DH@Gitee)
|
||||||
|
@ -2,8 +2,11 @@ package cn.hutool.core.codec;
|
|||||||
|
|
||||||
import cn.hutool.core.exceptions.UtilException;
|
import cn.hutool.core.exceptions.UtilException;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import cn.hutool.core.util.CharUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Punycode是一个根据RFC 3492标准而制定的编码系统,主要用于把域名从地方语言所采用的Unicode编码转换成为可用于DNS系统的编码
|
* Punycode是一个根据RFC 3492标准而制定的编码系统,主要用于把域名从地方语言所采用的Unicode编码转换成为可用于DNS系统的编码
|
||||||
* <p>
|
* <p>
|
||||||
@ -26,31 +29,23 @@ public class PunyCode {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* punycode转码域名
|
* punycode转码域名
|
||||||
* @param domain
|
*
|
||||||
* @return
|
* @param domain 域名
|
||||||
* @throws UtilException
|
* @return 编码后的域名
|
||||||
|
* @throws UtilException 计算异常
|
||||||
*/
|
*/
|
||||||
public static String encodeDomain(String domain) throws UtilException{
|
public static String encodeDomain(String domain) throws UtilException {
|
||||||
Assert.notNull(domain, "domain must not be null!");
|
Assert.notNull(domain, "domain must not be null!");
|
||||||
String[] split = domain.split("\\.");
|
final List<String> split = StrUtil.split(domain, CharUtil.DOT);
|
||||||
StringBuilder outStringBuilder = new StringBuilder();
|
final StringBuilder result = new StringBuilder(domain.length() * 4);
|
||||||
for (String string: split) {
|
for (final String str : split) {
|
||||||
boolean encode = false;
|
if (result.length() != 0) {
|
||||||
for (int index=0; index<string.length(); index++) {
|
result.append(CharUtil.DOT);
|
||||||
char c = string.charAt(index);
|
|
||||||
if (!isBasic(c)) {
|
|
||||||
encode = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (encode) {
|
result.append(encode(str, true));
|
||||||
outStringBuilder.append(PunyCode.encode(string, true));
|
|
||||||
} else {
|
|
||||||
outStringBuilder.append(string);
|
|
||||||
}
|
|
||||||
outStringBuilder.append(".");
|
|
||||||
}
|
}
|
||||||
return outStringBuilder.substring(0, outStringBuilder.length() - 1);
|
|
||||||
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -150,23 +145,23 @@ public class PunyCode {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 解码punycode域名
|
* 解码punycode域名
|
||||||
* @param domain
|
*
|
||||||
* @return
|
* @param domain PunyCode域名
|
||||||
* @throws UtilException
|
* @return 解码后的域名
|
||||||
|
* @throws UtilException 计算异常
|
||||||
*/
|
*/
|
||||||
public static String decodeDomain(String domain) throws UtilException{
|
public static String decodeDomain(String domain) throws UtilException {
|
||||||
Assert.notNull(domain, "domain must not be null!");
|
Assert.notNull(domain, "domain must not be null!");
|
||||||
String[] split = domain.split("\\.");
|
final List<String> split = StrUtil.split(domain, CharUtil.DOT);
|
||||||
StringBuilder outStringBuilder = new StringBuilder();
|
final StringBuilder result = new StringBuilder(domain.length() / 4 + 1);
|
||||||
for (String string: split) {
|
for (final String str : split) {
|
||||||
if (string.startsWith(PUNY_CODE_PREFIX)) {
|
if (result.length() != 0) {
|
||||||
outStringBuilder.append(decode(string));
|
result.append(CharUtil.DOT);
|
||||||
} else {
|
|
||||||
outStringBuilder.append(string);
|
|
||||||
}
|
}
|
||||||
outStringBuilder.append(".");
|
result.append(decode(str));
|
||||||
}
|
}
|
||||||
return outStringBuilder.substring(0, outStringBuilder.length() - 1);
|
|
||||||
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user