Jelajahi Sumber

优化http请求体缓存逻辑

Woody 3 minggu lalu
induk
melakukan
31ed913aa9

+ 1 - 1
framework-base/src/main/java/com/chelvc/framework/base/context/LoggingContextHolder.java

@@ -124,7 +124,7 @@ public final class LoggingContextHolder {
         if (StringUtils.notEmpty(query)) {
             endpoint.append("?").append(query);
         }
-        if (HttpUtils.hasBody(request) && !HttpUtils.isMultipartBody(request)) {
+        if (HttpUtils.isBodyType(request) && !HttpUtils.isStreamBody(request)) {
             String body = HttpUtils.getBody(request);
             if (StringUtils.notEmpty(body)) {
                 endpoint.append(StringUtils.SPACE);

+ 2 - 2
framework-base/src/main/java/com/chelvc/framework/base/interceptor/RequestInvokeInterceptor.java

@@ -162,7 +162,7 @@ public class RequestInvokeInterceptor extends OncePerRequestFilter {
                 // Servlet 规范规定如果调用了getInputStream()或getReader()方法,则getParameter*()方法的行为是未定义的
                 // Tomcat、Jetty、Undertow容器均支持在调用getParameter*()方法后能正常调用getInputStream()方法
                 this.parameters = request.getParameterMap();
-                if (HttpUtils.hasBody(request)) {
+                if (HttpUtils.isBodyType(request)) {
                     try (InputStream input = request.getInputStream()) {
                         if (input instanceof CachingInputStream) {
                             this.bytes = ((CachingInputStream) input).bytes;
@@ -239,7 +239,7 @@ public class RequestInvokeInterceptor extends OncePerRequestFilter {
     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
             throws ServletException, IOException {
         // 如果不是文件上传请求则对原生请求进行缓存包装处理
-        if (!HttpUtils.isMultipartBody(request)) {
+        if (!HttpUtils.isStreamBody(request)) {
             request = new CachingRequestWrapper(request);
         }
 

+ 9 - 6
framework-base/src/main/java/com/chelvc/framework/base/util/HttpUtils.java

@@ -250,12 +250,12 @@ public final class HttpUtils {
     }
 
     /**
-     * 判断是否有请求体
+     * 判断是否是请求体类型
      *
      * @param request 请求对象
      * @return true/false
      */
-    public static boolean hasBody(@NonNull ServletRequest request) {
+    public static boolean isBodyType(@NonNull ServletRequest request) {
         if (isJsonBody(request)) {
             return true;
         } else if (!(request instanceof HttpServletRequest)) {
@@ -273,18 +273,21 @@ public final class HttpUtils {
      */
     public static boolean isJsonBody(@NonNull ServletRequest request) {
         String type = request.getContentType();
-        return StringUtils.notEmpty(type) && type.contains(MediaType.APPLICATION_JSON_VALUE);
+        return StringUtils.notEmpty(type) && type.startsWith(MediaType.APPLICATION_JSON_VALUE);
     }
 
     /**
-     * 判断是否是文件流请求体
+     * 判断是否是数据流请求体
      *
      * @param request 请求对象
      * @return true/false
      */
-    public static boolean isMultipartBody(@NonNull ServletRequest request) {
+    public static boolean isStreamBody(@NonNull ServletRequest request) {
         String type = request.getContentType();
-        return StringUtils.notEmpty(type) && type.startsWith("multipart/");
+        return StringUtils.notEmpty(type) && (type.startsWith("image/") || type.startsWith("audio/")
+                || type.startsWith("video/") || type.startsWith("multipart/") || type.startsWith("application/pdf")
+                || type.startsWith("application/zip") || type.startsWith("application/vnd.")
+                || type.startsWith("application/grpc") || type.startsWith("application/octet-stream"));
     }
 
     /**