+ * 此类中的方法非线程安全 + *
* - * This code is licensed LGPLv3 + *+ * String clean = new HTMLFilter().filter(input); + *+ *
+ * 此类来自:http://xss-html-filter.sf.net
*
- * This code is a Java port of the original work in PHP by Cal Hendersen. http://code.iamcal.com/php/lib_filter/
- *
- * The trickiest part of the translation was handling the differences in regex handling between PHP and Java. These resources were helpful in the process:
- *
- * http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html http://us2.php.net/manual/en/reference.pcre.pattern.modifiers.php http://www.regular-expressions.info/modifiers.html
- *
- * A note on naming conventions: instance variables are prefixed with a "v"; global constants are in all caps.
- *
- * Sample use: String input = ... String clean = new HTMLFilter().filter( input );
- *
- * The class is not thread safe. Create a new instance if in doubt.
- *
- * If you find bugs or have suggestions on improvement (especially regarding performance), please contact us. The latest version of this source, and our contact details, can be found at
- * http://xss-html-filter.sf.net
- *
* @author Joseph O'Connell
* @author Cal Hendersen
* @author Michael Semb Wever
*/
public final class HTMLFilter {
- /** regex flag union representing /si modifiers in php **/
+ /**
+ * regex flag union representing /si modifiers in php
+ **/
private static final int REGEX_FLAGS_SI = Pattern.CASE_INSENSITIVE | Pattern.DOTALL;
private static final Pattern P_COMMENTS = Pattern.compile("", Pattern.DOTALL);
private static final Pattern P_COMMENT = Pattern.compile("^!--(.*)--$", REGEX_FLAGS_SI);
@@ -66,29 +61,49 @@ public final class HTMLFilter {
private static final Pattern P_BOTH_ARROWS = Pattern.compile("<>");
// @xxx could grow large... maybe use sesat's ReferenceMap
- private static final ConcurrentMap 注意,此方法只能标准化整个URL,并不适合于单独编码参数值 注意,此方法只能标准化整个URL,并不适合于单独编码参数值") **/
+ /**
+ * html elements which must always be self-closing (e.g. "
")
+ **/
private final String[] vSelfClosingTags;
- /** html elements which must always have separate opening and closing tags (e.g. "") **/
+ /**
+ * html elements which must always have separate opening and closing tags (e.g. "")
+ **/
private final String[] vNeedClosingTags;
- /** set of disallowed html elements **/
+ /**
+ * set of disallowed html elements
+ **/
private final String[] vDisallowed;
- /** attributes which should be checked for valid protocols **/
+ /**
+ * attributes which should be checked for valid protocols
+ **/
private final String[] vProtocolAtts;
- /** allowed protocols **/
+ /**
+ * allowed protocols
+ **/
private final String[] vAllowedProtocols;
- /** tags which should be removed if they contain no content (e.g. "" or "") **/
+ /**
+ * tags which should be removed if they contain no content (e.g. "" or "")
+ **/
private final String[] vRemoveBlanks;
- /** entities allowed within html markup **/
+ /**
+ * entities allowed within html markup
+ **/
private final String[] vAllowedEntities;
- /** flag determining whether comments are allowed in input String. */
+ /**
+ * flag determining whether comments are allowed in input String.
+ */
private final boolean stripComment;
private final boolean encodeQuotes;
private boolean vDebug = false;
@@ -100,36 +115,35 @@ public final class HTMLFilter {
/**
* Default constructor.
- *
*/
public HTMLFilter() {
- vAllowed = new HashMap
* 有些时候htts请求会出现com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl的实现,此为sun内部api,按照普通http请求处理
*
- * @param hostnameVerifier 域名验证器,非https传入null
- * @param ssf SSLSocketFactory,非https传入null
* @return {@link HttpURLConnection},https返回{@link HttpsURLConnection}
*/
private HttpURLConnection openHttp() throws IOException {
@@ -532,7 +529,7 @@ public class HttpConnection {
* 建立连接
*
* @return {@link URLConnection}
- * @throws IOException
+ * @throws IOException IO异常
*/
private URLConnection openConnection() throws IOException {
return (null == this.proxy) ? url.openConnection() : url.openConnection(this.proxy);
diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java b/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java
index 92d9b6065..b366701f4 100755
--- a/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java
+++ b/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java
@@ -1,5 +1,6 @@
package cn.hutool.http;
+import java.io.Serializable;
import java.net.CookieManager;
import cn.hutool.http.cookie.GlobalCookieManager;
@@ -10,10 +11,11 @@ import cn.hutool.http.cookie.GlobalCookieManager;
* @author Looly
* @since 4.6.2
*/
-public class HttpGlobalConfig {
+public class HttpGlobalConfig implements Serializable {
+ private static final long serialVersionUID = 1L;
protected static int timeout = -1;
-
+
/**
* 获取全局默认的超时时长
*
diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpInputStream.java b/hutool-http/src/main/java/cn/hutool/http/HttpInputStream.java
index ea0f4fe49..120164471 100644
--- a/hutool-http/src/main/java/cn/hutool/http/HttpInputStream.java
+++ b/hutool-http/src/main/java/cn/hutool/http/HttpInputStream.java
@@ -18,7 +18,7 @@ import cn.hutool.core.util.StrUtil;
public class HttpInputStream extends InputStream {
/** 原始流 */
- private volatile InputStream in;
+ private InputStream in;
/**
* 构造
diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java
index 42fbb5770..69086a467 100644
--- a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java
+++ b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java
@@ -1,21 +1,5 @@
package cn.hutool.http;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.HttpURLConnection;
-import java.nio.charset.Charset;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.regex.Pattern;
-
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
@@ -25,27 +9,36 @@ import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.StreamProgress;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.StrBuilder;
-import cn.hutool.core.util.CharsetUtil;
-import cn.hutool.core.util.ObjectUtil;
-import cn.hutool.core.util.ReUtil;
-import cn.hutool.core.util.StrUtil;
-import cn.hutool.core.util.URLUtil;
+import cn.hutool.core.util.*;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.nio.charset.Charset;
+import java.util.*;
+import java.util.Map.Entry;
+import java.util.regex.Pattern;
/**
* Http请求工具类
- *
+ *
* @author xiaoleilu
*/
public class HttpUtil {
- /** 正则:Content-Type中的编码信息 */
+ /**
+ * 正则:Content-Type中的编码信息
+ */
public static final Pattern CHARSET_PATTERN = Pattern.compile("charset\\s*=\\s*([a-z0-9-]*)", Pattern.CASE_INSENSITIVE);
- /** 正则:匹配meta标签的编码信息 */
+ /**
+ * 正则:匹配meta标签的编码信息
+ */
public static final Pattern META_CHARSET_PATTERN = Pattern.compile("]*?charset\\s*=\\s*['\"]?([a-z0-9-]*)", Pattern.CASE_INSENSITIVE);
/**
* 检测是否https
- *
+ *
* @param url URL
* @return 是否https
*/
@@ -55,9 +48,9 @@ public class HttpUtil {
/**
* 创建Http请求对象
- *
+ *
* @param method 方法枚举{@link Method}
- * @param url 请求的URL,可以使HTTP或者HTTPS
+ * @param url 请求的URL,可以使HTTP或者HTTPS
* @return {@link HttpRequest}
* @since 3.0.9
*/
@@ -67,7 +60,7 @@ public class HttpUtil {
/**
* 创建Http GET请求对象
- *
+ *
* @param url 请求的URL,可以使HTTP或者HTTPS
* @return {@link HttpRequest}
* @since 3.2.0
@@ -78,7 +71,7 @@ public class HttpUtil {
/**
* 创建Http POST请求对象
- *
+ *
* @param url 请求的URL,可以使HTTP或者HTTPS
* @return {@link HttpRequest}
* @since 3.2.0
@@ -89,8 +82,8 @@ public class HttpUtil {
/**
* 发送get请求
- *
- * @param urlString 网址
+ *
+ * @param urlString 网址
* @param customCharset 自定义请求字符集,如果字符集获取不到,使用此字符集
* @return 返回内容,如果只检查状态码,正常只返回 "",不正常返回 null
*/
@@ -100,7 +93,7 @@ public class HttpUtil {
/**
* 发送get请求
- *
+ *
* @param urlString 网址
* @return 返回内容,如果只检查状态码,正常只返回 "",不正常返回 null
*/
@@ -110,9 +103,9 @@ public class HttpUtil {
/**
* 发送get请求
- *
+ *
* @param urlString 网址
- * @param timeout 超时时长,-1表示默认超时,单位毫秒
+ * @param timeout 超时时长,-1表示默认超时,单位毫秒
* @return 返回内容,如果只检查状态码,正常只返回 "",不正常返回 null
* @since 3.2.0
*/
@@ -122,9 +115,9 @@ public class HttpUtil {
/**
* 发送get请求
- *
+ *
* @param urlString 网址
- * @param paramMap post表单数据
+ * @param paramMap post表单数据
* @return 返回数据
*/
public static String get(String urlString, Map
* 请求体body参数支持两种类型:
- *
+ *
*
* 1. 标准参数,例如 a=1&b=2 这种格式
* 2. Rest模式,此时body需要传入一个JSON或者XML字符串,Hutool会自动绑定其对应的Content-Type
*
- *
+ *
* @param urlString 网址
- * @param body post表单数据
+ * @param body post表单数据
* @return 返回数据
*/
public static String post(String urlString, String body) {
@@ -188,15 +181,15 @@ public class HttpUtil {
/**
* 发送post请求
* 请求体body参数支持两种类型:
- *
+ *
*
* 1. 标准参数,例如 a=1&b=2 这种格式
* 2. Rest模式,此时body需要传入一个JSON或者XML字符串,Hutool会自动绑定其对应的Content-Type
*
- *
+ *
* @param urlString 网址
- * @param body post表单数据
- * @param timeout 超时时长,-1表示默认超时,单位毫秒
+ * @param body post表单数据
+ * @param timeout 超时时长,-1表示默认超时,单位毫秒
* @return 返回数据
* @since 3.2.0
*/
@@ -205,10 +198,11 @@ public class HttpUtil {
}
// ---------------------------------------------------------------------------------------- download
+
/**
* 下载远程文本
- *
- * @param url 请求的url
+ *
+ * @param url 请求的url
* @param customCharsetName 自定义的字符集
* @return 文本
*/
@@ -218,8 +212,8 @@ public class HttpUtil {
/**
* 下载远程文本
- *
- * @param url 请求的url
+ *
+ * @param url 请求的url
* @param customCharset 自定义的字符集,可以使用{@link CharsetUtil#charset} 方法转换
* @return 文本
*/
@@ -229,10 +223,10 @@ public class HttpUtil {
/**
* 下载远程文本
- *
- * @param url 请求的url
+ *
+ * @param url 请求的url
* @param customCharset 自定义的字符集,可以使用{@link CharsetUtil#charset} 方法转换
- * @param streamPress 进度条 {@link StreamProgress}
+ * @param streamPress 进度条 {@link StreamProgress}
* @return 文本
*/
public static String downloadString(String url, Charset customCharset, StreamProgress streamPress) {
@@ -247,8 +241,8 @@ public class HttpUtil {
/**
* 下载远程文件
- *
- * @param url 请求的url
+ *
+ * @param url 请求的url
* @param dest 目标文件或目录,当为目录时,取URL中的文件名,取不到使用编码后的URL做为文件名
* @return 文件大小
*/
@@ -258,8 +252,8 @@ public class HttpUtil {
/**
* 下载远程文件
- *
- * @param url 请求的url
+ *
+ * @param url 请求的url
* @param destFile 目标文件或目录,当为目录时,取URL中的文件名,取不到使用编码后的URL做为文件名
* @return 文件大小
*/
@@ -269,10 +263,10 @@ public class HttpUtil {
/**
* 下载远程文件
- *
- * @param url 请求的url
+ *
+ * @param url 请求的url
* @param destFile 目标文件或目录,当为目录时,取URL中的文件名,取不到使用编码后的URL做为文件名
- * @param timeout 超时,单位毫秒,-1表示默认超时
+ * @param timeout 超时,单位毫秒,-1表示默认超时
* @return 文件大小
* @since 4.0.4
*/
@@ -282,9 +276,9 @@ public class HttpUtil {
/**
* 下载远程文件
- *
- * @param url 请求的url
- * @param destFile 目标文件或目录,当为目录时,取URL中的文件名,取不到使用编码后的URL做为文件名
+ *
+ * @param url 请求的url
+ * @param destFile 目标文件或目录,当为目录时,取URL中的文件名,取不到使用编码后的URL做为文件名
* @param streamProgress 进度条
* @return 文件大小
*/
@@ -294,10 +288,10 @@ public class HttpUtil {
/**
* 下载远程文件
- *
- * @param url 请求的url
- * @param destFile 目标文件或目录,当为目录时,取URL中的文件名,取不到使用编码后的URL做为文件名
- * @param timeout 超时,单位毫秒,-1表示默认超时
+ *
+ * @param url 请求的url
+ * @param destFile 目标文件或目录,当为目录时,取URL中的文件名,取不到使用编码后的URL做为文件名
+ * @param timeout 超时,单位毫秒,-1表示默认超时
* @param streamProgress 进度条
* @return 文件大小
* @since 4.0.4
@@ -318,9 +312,9 @@ public class HttpUtil {
/**
* 下载远程文件
- *
- * @param url 请求的url
- * @param out 将下载内容写到输出流中 {@link OutputStream}
+ *
+ * @param url 请求的url
+ * @param out 将下载内容写到输出流中 {@link OutputStream}
* @param isCloseOut 是否关闭输出流
* @return 文件大小
*/
@@ -330,10 +324,10 @@ public class HttpUtil {
/**
* 下载远程文件
- *
- * @param url 请求的url
- * @param out 将下载内容写到输出流中 {@link OutputStream}
- * @param isCloseOut 是否关闭输出流
+ *
+ * @param url 请求的url
+ * @param out 将下载内容写到输出流中 {@link OutputStream}
+ * @param isCloseOut 是否关闭输出流
* @param streamProgress 进度条
* @return 文件大小
*/
@@ -354,7 +348,7 @@ public class HttpUtil {
/**
* 将Map形式的Form表单数据转换为Url参数形式,不做编码
- *
+ *
* @param paramMap 表单数据
* @return url参数
*/
@@ -365,8 +359,8 @@ public class HttpUtil {
/**
* 将Map形式的Form表单数据转换为Url参数形式
* 编码键和值对
- *
- * @param paramMap 表单数据
+ *
+ * @param paramMap 表单数据
* @param charsetName 编码
* @return url参数
*/
@@ -378,13 +372,13 @@ public class HttpUtil {
* 将Map形式的Form表单数据转换为Url参数形式
* paramMap中如果key为空(null和"")会被忽略,如果value为null,会被做为空白符("")
* 会自动url编码键和值
- *
+ *
*
* key1=v1&key2=&key3=v3
*
- *
+ *
* @param paramMap 表单数据
- * @param charset 编码
+ * @param charset 编码
* @return url参数
*/
public static String toParams(Map
* 提供的值可以是url附带参数,但是不能只是url
- *
+ *
*
* 表单的键值对会被url编码,但是url中原参数不会被编码
- *
- * @param url URL
- * @param form 表单数据
- * @param charset 编码
+ *
+ * @param url URL
+ * @param form 表单数据
+ * @param charset 编码
* @param isEncodeParams 是否对键和值做转义处理
* @return 合成后的URL
*/
@@ -620,11 +613,11 @@ public class HttpUtil {
/**
* 将表单数据字符串加到URL中(用于GET表单提交)
- *
- * @param url URL
+ *
+ * @param url URL
* @param queryString 表单数据字符串
- * @param charset 编码
- * @param isEncode 是否对键和值做转义处理
+ * @param charset 编码
+ * @param isEncode 是否对键和值做转义处理
* @return 拼接后的字符串
*/
public static String urlWithForm(String url, String queryString, Charset charset, boolean isEncode) {
@@ -662,7 +655,7 @@ public class HttpUtil {
/**
* 从Http连接的头信息中获得字符集
* 从ContentType中获取
- *
+ *
* @param conn HTTP连接对象
* @return 字符集
*/
@@ -676,14 +669,13 @@ public class HttpUtil {
/**
* 从流中读取内容
* 首先尝试使用charset编码读取内容(如果为空默认UTF-8),如果isGetCharsetFromContent为true,则通过正则在正文中获取编码信息,转换为指定编码;
- *
- * @param in 输入流
- * @param charset 字符集
+ *
+ * @param in 输入流
+ * @param charset 字符集
* @param isGetCharsetFromContent 是否从返回内容中获得编码信息
* @return 内容
- * @throws IOException IO异常
*/
- public static String getString(InputStream in, Charset charset, boolean isGetCharsetFromContent) throws IOException {
+ public static String getString(InputStream in, Charset charset, boolean isGetCharsetFromContent) {
final byte[] contentBytes = IoUtil.readBytes(in);
return getString(contentBytes, charset, isGetCharsetFromContent);
}
@@ -691,9 +683,9 @@ public class HttpUtil {
/**
* 从流中读取内容
* 首先尝试使用charset编码读取内容(如果为空默认UTF-8),如果isGetCharsetFromContent为true,则通过正则在正文中获取编码信息,转换为指定编码;
- *
- * @param contentBytes 内容byte数组
- * @param charset 字符集
+ *
+ * @param contentBytes 内容byte数组
+ * @param charset 字符集
* @param isGetCharsetFromContent 是否从返回内容中获得编码信息
* @return 内容
*/
@@ -727,11 +719,11 @@ public class HttpUtil {
}
return content;
}
-
+
/**
* 根据文件扩展名获得MimeType
- *
- * @param filePath 文件路径或文件名
+ *
+ * @param filePath 文件路径或文件名
* @param defaultValue 当获取MimeType为null时的默认值
* @return MimeType
* @see FileUtil#getMimeType(String)
@@ -743,7 +735,7 @@ public class HttpUtil {
/**
* 根据文件扩展名获得MimeType
- *
+ *
* @param filePath 文件路径或文件名
* @return MimeType
* @see FileUtil#getMimeType(String)
@@ -754,16 +746,16 @@ public class HttpUtil {
/**
* 从请求参数的body中判断请求的Content-Type类型,支持的类型有:
- *
+ *
*
* 1. application/json
* 1. application/xml
*
- *
+ *
* @param body 请求参数体
* @return Content-Type类型,如果无法判断返回null
- * @since 3.2.0
* @see ContentType#get(String)
+ * @since 3.2.0
*/
public static String getContentTypeByRequestBody(String body) {
final ContentType contentType = ContentType.get(body);
@@ -773,20 +765,17 @@ public class HttpUtil {
/**
* 将键值对加入到值为List类型的Map中
- *
- * @param params 参数
- * @param name key
- * @param value value
+ *
+ * @param params 参数
+ * @param name key
+ * @param value value
* @param charset 编码
*/
private static void addParam(Map