From 00f7f5ff0a81290a75ac82ca97a06ea894d6dcad Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Thu, 7 Jul 2022 21:57:08 +0900 Subject: [PATCH 1/4] Use MediaType.APPLICATION_FORM_URLENCODED_VALUE See gh-28771 --- .../http/server/ServletServerHttpRequest.java | 2 +- .../web/util/ContentCachingRequestWrapper.java | 6 ++---- .../org/springframework/web/servlet/DispatcherServlet.java | 3 ++- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java index 45ce146f2e..cd1eccf7ab 100644 --- a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java @@ -56,7 +56,7 @@ import org.springframework.util.StringUtils; */ public class ServletServerHttpRequest implements ServerHttpRequest { - protected static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded"; + protected static final String FORM_CONTENT_TYPE = MediaType.APPLICATION_FORM_URLENCODED_VALUE; protected static final Charset FORM_CHARSET = StandardCharsets.UTF_8; diff --git a/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java b/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java index 104f762b45..29d82e4ce4 100644 --- a/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java +++ b/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java @@ -33,6 +33,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; import org.springframework.lang.Nullable; /** @@ -55,9 +56,6 @@ import org.springframework.lang.Nullable; */ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper { - private static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded"; - - private final ByteArrayOutputStream cachedContent; @Nullable @@ -152,7 +150,7 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper { private boolean isFormPost() { String contentType = getContentType(); - return (contentType != null && contentType.contains(FORM_CONTENT_TYPE) && + return (contentType != null && contentType.contains(MediaType.APPLICATION_FORM_URLENCODED_VALUE) && HttpMethod.POST.matches(getMethod())); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java index 59a47a07a6..603f23940f 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java @@ -50,6 +50,7 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.core.log.LogFormatUtils; import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; import org.springframework.http.server.RequestPath; import org.springframework.http.server.ServletServerHttpRequest; import org.springframework.lang.Nullable; @@ -989,7 +990,7 @@ public class DispatcherServlet extends FrameworkServlet { } else { // Avoid request body parsing for form data - params = (StringUtils.startsWithIgnoreCase(contentType, "application/x-www-form-urlencoded") || + params = (StringUtils.startsWithIgnoreCase(contentType, MediaType.APPLICATION_FORM_URLENCODED_VALUE) || !request.getParameterMap().isEmpty() ? "masked" : ""); } From 6e6f8637d135abd633a921b4fc267d4bf7e1dca0 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 26 Jul 2022 14:44:51 +0300 Subject: [PATCH 2/4] Polishing contribution Closes gh-28771 --- .../springframework/http/server/ServletServerHttpRequest.java | 4 +--- .../web/util/ContentCachingRequestWrapper.java | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java index cd1eccf7ab..34a72977fc 100644 --- a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java @@ -56,8 +56,6 @@ import org.springframework.util.StringUtils; */ public class ServletServerHttpRequest implements ServerHttpRequest { - protected static final String FORM_CONTENT_TYPE = MediaType.APPLICATION_FORM_URLENCODED_VALUE; - protected static final Charset FORM_CHARSET = StandardCharsets.UTF_8; @@ -231,7 +229,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest { private static boolean isFormPost(HttpServletRequest request) { String contentType = request.getContentType(); - return (contentType != null && contentType.contains(FORM_CONTENT_TYPE) && + return (contentType != null && contentType.contains(MediaType.APPLICATION_FORM_URLENCODED_VALUE) && HttpMethod.POST.matches(request.getMethod())); } diff --git a/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java b/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java index 29d82e4ce4..748e11f64a 100644 --- a/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java +++ b/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From b6144e5682c966759e7fd16dbac4d4948d332161 Mon Sep 17 00:00:00 2001 From: floriankirmaier Date: Mon, 18 Jul 2022 16:34:32 +0200 Subject: [PATCH 3/4] Improve setting of WebSocket error status See gh-28836 --- .../socket/adapter/AbstractListenerWebSocketSession.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/AbstractListenerWebSocketSession.java b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/AbstractListenerWebSocketSession.java index 8f8e530508..bb6ff6e57d 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/AbstractListenerWebSocketSession.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/AbstractListenerWebSocketSession.java @@ -33,6 +33,7 @@ import org.springframework.http.server.reactive.AbstractListenerReadPublisher; import org.springframework.http.server.reactive.AbstractListenerWriteProcessor; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; import org.springframework.web.reactive.socket.CloseStatus; import org.springframework.web.reactive.socket.HandshakeInfo; import org.springframework.web.reactive.socket.WebSocketHandler; @@ -247,7 +248,12 @@ public abstract class AbstractListenerWebSocketSession extends AbstractWebSoc if (this.handlerCompletionMono != null) { this.handlerCompletionMono.onError(ex); } - close(CloseStatus.SERVER_ERROR.withReason(ex.getMessage())); + if(!StringUtils.hasText(ex.getMessage())) { + close(CloseStatus.SERVER_ERROR); + } + else { + close(CloseStatus.SERVER_ERROR.withReason(ex.getMessage())); + } } @Override From 2172b99fee345f0068e9d1851bf5ed6d0ba36633 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 27 Jul 2022 15:23:40 +0300 Subject: [PATCH 4/4] Polishing contribution Closes gh-28836 --- .../adapter/AbstractListenerWebSocketSession.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/AbstractListenerWebSocketSession.java b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/AbstractListenerWebSocketSession.java index bb6ff6e57d..9bf0246b4e 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/AbstractListenerWebSocketSession.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/AbstractListenerWebSocketSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,7 +33,6 @@ import org.springframework.http.server.reactive.AbstractListenerReadPublisher; import org.springframework.http.server.reactive.AbstractListenerWriteProcessor; import org.springframework.lang.Nullable; import org.springframework.util.Assert; -import org.springframework.util.StringUtils; import org.springframework.web.reactive.socket.CloseStatus; import org.springframework.web.reactive.socket.HandshakeInfo; import org.springframework.web.reactive.socket.WebSocketHandler; @@ -248,12 +247,13 @@ public abstract class AbstractListenerWebSocketSession extends AbstractWebSoc if (this.handlerCompletionMono != null) { this.handlerCompletionMono.onError(ex); } - if(!StringUtils.hasText(ex.getMessage())) { - close(CloseStatus.SERVER_ERROR); + if (logger.isDebugEnabled()) { + logger.debug("WebSocket session completed with error", ex); } - else { - close(CloseStatus.SERVER_ERROR.withReason(ex.getMessage())); + else if (logger.isInfoEnabled()) { + logger.info("WebSocket session completed with error: " + ex.getMessage()); } + close(CloseStatus.SERVER_ERROR); } @Override