fix alowPatch

This commit is contained in:
Looly 2021-07-08 09:29:02 +08:00
parent 5c6e548db2
commit 96e88307f8
3 changed files with 55 additions and 46 deletions

View File

@ -13,8 +13,6 @@ import javax.net.ssl.SSLSocketFactory;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.ProtocolException; import java.net.ProtocolException;
import java.net.Proxy; import java.net.Proxy;
@ -122,7 +120,7 @@ public class HttpConnection {
// 增加PATCH方法支持 // 增加PATCH方法支持
if (Method.PATCH.equals(method)) { if (Method.PATCH.equals(method)) {
allowPatch(); HttpGlobalConfig.allowPatch();
} }
} }
@ -546,23 +544,5 @@ public class HttpConnection {
private URLConnection openConnection() throws IOException { private URLConnection openConnection() throws IOException {
return (null == this.proxy) ? url.openConnection() : url.openConnection(this.proxy); return (null == this.proxy) ? url.openConnection() : url.openConnection(this.proxy);
} }
/**
* 增加支持的METHOD方法
* see: https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch
*
* @since 5.1.6
*/
private static void allowPatch() {
final Field methodsField = ReflectUtil.getField(HttpURLConnection.class, "methods");
if (null != methodsField) {
// 去除final修饰
ReflectUtil.setFieldValue(methodsField, "modifiers", methodsField.getModifiers() & ~Modifier.FINAL);
final String[] methods = {
"GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH"
};
ReflectUtil.setFieldValue(null, methodsField, methods);
}
}
// --------------------------------------------------------------- Private Method end // --------------------------------------------------------------- Private Method end
} }

View File

@ -1,10 +1,14 @@
package cn.hutool.http; package cn.hutool.http;
import java.io.Serializable; import cn.hutool.core.util.ReflectUtil;
import java.net.CookieManager;
import cn.hutool.http.cookie.GlobalCookieManager; import cn.hutool.http.cookie.GlobalCookieManager;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.CookieManager;
import java.net.HttpURLConnection;
/** /**
* HTTP 全局参数配置 * HTTP 全局参数配置
* *
@ -15,6 +19,7 @@ public class HttpGlobalConfig implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
protected static int timeout = -1; protected static int timeout = -1;
private static boolean isAllowPatch = false;
/** /**
* 获取全局默认的超时时长 * 获取全局默认的超时时长
@ -30,7 +35,7 @@ public class HttpGlobalConfig implements Serializable {
* *
* @param customTimeout 超时时长 * @param customTimeout 超时时长
*/ */
public static void setTimeout(int customTimeout) { synchronized public static void setTimeout(int customTimeout) {
timeout = customTimeout; timeout = customTimeout;
} }
@ -65,4 +70,27 @@ public class HttpGlobalConfig implements Serializable {
public static void closeCookie() { public static void closeCookie() {
GlobalCookieManager.setCookieManager(null); GlobalCookieManager.setCookieManager(null);
} }
/**
* 增加支持的METHOD方法<br>
* 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性增加PATCH方法<br>
* see: https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch
*
* @since 5.7.4
*/
synchronized public static void allowPatch() {
if(isAllowPatch){
return;
}
final Field methodsField = ReflectUtil.getField(HttpURLConnection.class, "methods");
if (null != methodsField) {
// 去除final修饰
ReflectUtil.setFieldValue(methodsField, "modifiers", methodsField.getModifiers() & ~Modifier.FINAL);
final String[] methods = {
"GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH"
};
ReflectUtil.setFieldValue(null, methodsField, methods);
isAllowPatch = true;
}
}
} }

View File

@ -35,6 +35,7 @@ public class HttpInputStream extends InputStream {
return this.in.read(); return this.in.read();
} }
@SuppressWarnings("NullableProblems")
@Override @Override
public int read(byte[] b, int off, int len) throws IOException { public int read(byte[] b, int off, int len) throws IOException {
return this.in.read(b, off, len); return this.in.read(b, off, len);