fix url encode bug

This commit is contained in:
Looly 2021-05-09 00:44:19 +08:00
parent d8eebe1879
commit 3f233bae81
7 changed files with 162 additions and 43 deletions

View File

@ -20,6 +20,7 @@
* 【core 】 修复Bcrypt不支持$2y$盐前缀问题pr#1560@Github
* 【system 】 修复isWindows8拼写问题pr#1557@Github
* 【db 】 修复MongoDS默认分组参数失效问题issue#1548@Github
* 【core 】 修复UrlPath编码的字符问题导致的URL编码异常issue#1537@Github
-------------------------------------------------------------------------------------------------------------

View File

@ -25,11 +25,12 @@ public class URLDecoder implements Serializable {
/**
* 解码不对+解码
* <pre>
* 1. %20转换为空格 ;
* 2. "%xy"转换为文本形式,xy是两位16进制的数值;
* 3. 跳过不符合规范的%形式直接输出
* </pre>
*
* <ol>
* <li>%20转换为空格</li>
* <li> "%xy"转换为文本形式,xy是两位16进制的数值</li>
* <li>跳过不符合规范的%形式直接输出</li>
* </ol>
*
* @param str 包含URL编码后的字符串
* @param charset 编码

View File

@ -30,13 +30,25 @@ public class URLEncoder implements Serializable {
* 默认的编码器针对URI路径编码定义如下
*
* <pre>
* pchar = unreserved不处理 / pct-encoded / sub-delims子分隔符 / ":" / "@"
* pchar = unreserved不处理 / pct-encoded / sub-delims子分隔符 / ":" / "@" / "/"
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* sub-delims = "!" / "$" / "&amp;" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
* </pre>
*/
public static final URLEncoder DEFAULT = createDefault();
/**
* URL的Path的每一个Segment URLEncoder<br>
* 默认的编码器针对URI路径编码定义如下
*
* <pre>
* pchar = unreserved不处理 / pct-encoded / sub-delims子分隔符 / "@"
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* sub-delims = "!" / "$" / "&amp;" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
* </pre>
*/
public static final URLEncoder PATH_SEGMENT = createPathSegment();
/**
* 用于查询语句的URLEncoder<br>
* 编码器针对URI路径编码定义如下
@ -55,10 +67,10 @@ public class URLEncoder implements Serializable {
/**
* 全编码的URLEncoder<br>
* <pre>
* 0x2A, 0x2D, 0x2E, 0x30 to 0x39, 0x41 to 0x5A, 0x5F, 0x61 to 0x7A as-is
* '*', '-', '.', '0' to '9', 'A' to 'Z', '_', 'a' to 'z' 不编码
* 其它编码为 %nn 形式
* </pre>
* 0x2A, 0x2D, 0x2E, 0x30 to 0x39, 0x41 to 0x5A, 0x5F, 0x61 to 0x7A as-is
* '*', '-', '.', '0' to '9', 'A' to 'Z', '_', 'a' to 'z' 不编码
* 其它编码为 %nn 形式
* </pre>
*/
public static final URLEncoder ALL = createAll();
@ -67,7 +79,7 @@ public class URLEncoder implements Serializable {
* 默认的编码器针对URI路径编码定义如下
*
* <pre>
* pchar = unreserved不处理 / pct-encoded / sub-delims子分隔符 / ":" / "@"
* pchar = unreserved不处理 / pct-encoded / sub-delims子分隔符 / ":" / "@" / "/"
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* sub-delims = "!" / "$" / "&amp;" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
* </pre>
@ -101,6 +113,42 @@ public class URLEncoder implements Serializable {
return encoder;
}
/**
* URL的Path的每一个Segment URLEncoder<br>
* 默认的编码器针对URI路径的每一段编码定义如下
*
* <pre>
* pchar = unreserved不处理 / pct-encoded / sub-delims子分隔符 / "@"
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* sub-delims = "!" / "$" / "&amp;" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
* </pre>
*
* @return URLEncoder
*/
public static URLEncoder createPathSegment() {
final URLEncoder encoder = new URLEncoder();
encoder.addSafeCharacter('-');
encoder.addSafeCharacter('.');
encoder.addSafeCharacter('_');
encoder.addSafeCharacter('~');
// Add the sub-delims
encoder.addSafeCharacter('!');
encoder.addSafeCharacter('$');
encoder.addSafeCharacter('&');
encoder.addSafeCharacter('\'');
encoder.addSafeCharacter('(');
encoder.addSafeCharacter(')');
encoder.addSafeCharacter('*');
encoder.addSafeCharacter('+');
encoder.addSafeCharacter(',');
encoder.addSafeCharacter(';');
encoder.addSafeCharacter('=');
// Add the remaining literals
encoder.addSafeCharacter('@');
return encoder;
}
/**
* 创建用于查询语句的URLEncoder<br>
* 编码器针对URI路径编码定义如下

View File

@ -127,7 +127,7 @@ public class UrlPath {
final StringBuilder builder = new StringBuilder();
for (String segment : segments) {
builder.append(CharUtil.SLASH).append(URLUtil.encode(segment, charset));
builder.append(CharUtil.SLASH).append(URLUtil.encodePathSegment(segment, charset));
}
if (withEngTag || StrUtil.isEmpty(builder)) {
builder.append(CharUtil.SLASH);

View File

@ -353,20 +353,6 @@ public class URLUtil {
return encode(url, CharsetUtil.CHARSET_UTF_8);
}
/**
* 编码URL默认使用UTF-8编码<br>
* 将需要转换的内容ASCII码形式之外的内容用十六进制表示法转换出来并在之前加上%开头<br>
* 此方法用于POST请求中的请求体自动编码转义大部分特殊字符
*
* @param url URL
* @return 编码后的URL
* @throws UtilException UnsupportedEncodingException
* @since 3.1.2
*/
public static String encodeQuery(String url) throws UtilException {
return encodeQuery(url, CharsetUtil.CHARSET_UTF_8);
}
/**
* 编码字符为 application/x-www-form-urlencoded<br>
* 将需要转换的内容ASCII码形式之外的内容用十六进制表示法转换出来并在之前加上%开头<br>
@ -387,6 +373,39 @@ public class URLUtil {
return URLEncoder.DEFAULT.encode(url, charset);
}
/**
* 编码URL字符为 application/x-www-form-urlencoded<br>
* 将需要转换的内容ASCII码形式之外的内容用十六进制表示法转换出来并在之前加上%开头<br>
* 此方法用于URL自动编码类似于浏览器中键入地址自动编码对于像类似于/的字符不再编码
*
* @param url URL
* @param charset 编码
* @return 编码后的URL
* @throws UtilException UnsupportedEncodingException
* @deprecated 请使用 {@link #encode(String, Charset)}
*/
@Deprecated
public static String encode(String url, String charset) throws UtilException {
if (StrUtil.isEmpty(url)) {
return url;
}
return encode(url, StrUtil.isBlank(charset) ? CharsetUtil.defaultCharset() : CharsetUtil.charset(charset));
}
/**
* 编码URL默认使用UTF-8编码<br>
* 将需要转换的内容ASCII码形式之外的内容用十六进制表示法转换出来并在之前加上%开头<br>
* 此方法用于POST请求中的请求体自动编码转义大部分特殊字符
*
* @param url URL
* @return 编码后的URL
* @throws UtilException UnsupportedEncodingException
* @since 3.1.2
*/
public static String encodeQuery(String url) throws UtilException {
return encodeQuery(url, CharsetUtil.CHARSET_UTF_8);
}
/**
* 编码字符为URL中查询语句<br>
* 将需要转换的内容ASCII码形式之外的内容用十六进制表示法转换出来并在之前加上%开头<br>
@ -407,23 +426,6 @@ public class URLUtil {
return URLEncoder.QUERY.encode(url, charset);
}
/**
* 编码URL字符为 application/x-www-form-urlencoded<br>
* 将需要转换的内容ASCII码形式之外的内容用十六进制表示法转换出来并在之前加上%开头<br>
* 此方法用于URL自动编码类似于浏览器中键入地址自动编码对于像类似于/的字符不再编码
*
* @param url URL
* @param charset 编码
* @return 编码后的URL
* @throws UtilException UnsupportedEncodingException
*/
public static String encode(String url, String charset) throws UtilException {
if (StrUtil.isEmpty(url)) {
return url;
}
return encode(url, StrUtil.isBlank(charset) ? CharsetUtil.defaultCharset() : CharsetUtil.charset(charset));
}
/**
* 编码URL<br>
* 将需要转换的内容ASCII码形式之外的内容用十六进制表示法转换出来并在之前加上%开头<br>
@ -433,11 +435,61 @@ public class URLUtil {
* @param charset 编码
* @return 编码后的URL
* @throws UtilException UnsupportedEncodingException
* @deprecated 请使用 {@link #encodeQuery(String, Charset)}
*/
@Deprecated
public static String encodeQuery(String url, String charset) throws UtilException {
return encodeQuery(url, StrUtil.isBlank(charset) ? CharsetUtil.defaultCharset() : CharsetUtil.charset(charset));
}
/**
* 编码URL默认使用UTF-8编码<br>
* 将需要转换的内容ASCII码形式之外的内容用十六进制表示法转换出来并在之前加上%开头<br>
* 此方法用于URL的Segment中自动编码转义大部分特殊字符
*
* <pre>
* pchar = unreserved不处理 / pct-encoded / sub-delims子分隔符 / "@"
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* sub-delims = "!" / "$" / "&amp;" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
* </pre>
*
* @param url URL
* @return 编码后的URL
* @throws UtilException UnsupportedEncodingException
* @since 5.6.5
*/
public static String encodePathSegment(String url) throws UtilException {
return encodePathSegment(url, CharsetUtil.CHARSET_UTF_8);
}
/**
* 编码字符为URL中查询语句<br>
* 将需要转换的内容ASCII码形式之外的内容用十六进制表示法转换出来并在之前加上%开头<br>
* 此方法用于URL的Segment中自动编码转义大部分特殊字符
*
* <pre>
* pchar = unreserved不处理 / pct-encoded / sub-delims子分隔符 / "@"
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* sub-delims = "!" / "$" / "&amp;" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
* </pre>
*
* @param url 被编码内容
* @param charset 编码
* @return 编码后的字符
* @since 5.6.5
*/
public static String encodePathSegment(String url, Charset charset) {
if (StrUtil.isEmpty(url)) {
return url;
}
if (null == charset) {
charset = CharsetUtil.defaultCharset();
}
return URLEncoder.PATH_SEGMENT.encode(url, charset);
}
//-------------------------------------------------------------------------- decode
/**
* 解码URL<br>
* %开头的16进制表示的内容解码

View File

@ -250,4 +250,13 @@ public class UrlBuilderTest {
final UrlBuilder urlBuilder = UrlBuilder.ofHttp(urlStr, CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals(urlStr, urlBuilder.toString());
}
@Test
public void gimg2Test(){
String url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.jj20.com%2Fup%2Fallimg%2F1114%2F0H320120Z3%2F200H3120Z3-6-1200.jpg&refer=http%3A%2F%2Fpic.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621996490&t=8c384c2823ea453da15a1b9cd5183eea";
final UrlBuilder urlBuilder = UrlBuilder.of(url);
Assert.assertEquals(url, urlBuilder.toString());
}
}

View File

@ -1,5 +1,6 @@
package cn.hutool.http;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.CharsetUtil;
@ -325,4 +326,11 @@ public class HttpUtilTest {
final String s = HttpUtil.get("http://hq.sinajs.cn/list=sh600519");
Console.log(s);
}
@Test
@Ignore
public void gimg2Test(){
byte[] bytes = HttpUtil.downloadBytes("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.jj20.com%2Fup%2Fallimg%2F1114%2F0H320120Z3%2F200H3120Z3-6-1200.jpg&refer=http%3A%2F%2Fpic.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621996490&t=8c384c2823ea453da15a1b9cd5183eea");
Console.log(Base64.encode(bytes));
}
}