From 195d0d6805a65c1c323ebca638d201734ff8f4c3 Mon Sep 17 00:00:00 2001 From: Mike Smith Date: Tue, 21 Apr 2015 22:16:07 -0700 Subject: [PATCH] - Catching SocketTimeoutException when copying request body inputstream and treating as empty body, with a log statement. This is to more gracefully handle the case of truncated post bodies when using tomcat APR connector (which fails differently to BIO connector). --- .../zuul/http/HttpServletRequestWrapper.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/zuul-core/src/main/java/com/netflix/zuul/http/HttpServletRequestWrapper.java b/zuul-core/src/main/java/com/netflix/zuul/http/HttpServletRequestWrapper.java index c5305c8eaa..cf52afbe5c 100755 --- a/zuul-core/src/main/java/com/netflix/zuul/http/HttpServletRequestWrapper.java +++ b/zuul-core/src/main/java/com/netflix/zuul/http/HttpServletRequestWrapper.java @@ -33,6 +33,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.io.*; +import java.net.SocketTimeoutException; import java.net.URLDecoder; import java.security.Principal; import java.util.*; @@ -60,8 +61,8 @@ public class HttpServletRequestWrapper implements HttpServletRequest { protected static final Logger LOG = LoggerFactory.getLogger(HttpServletRequestWrapper.class); private HttpServletRequest req; - private byte[] contentData; - private HashMap parameters; + private byte[] contentData = null; + private HashMap parameters = null; public HttpServletRequestWrapper() { //a trick for Groovy @@ -148,8 +149,17 @@ private void parseRequest() throws IOException { // Read the request body inputstream into a byte array. ByteArrayOutputStream baos = new ByteArrayOutputStream(); - IOUtils.copy(req.getInputStream(), baos); - contentData = baos.toByteArray(); + try { + IOUtils.copy(req.getInputStream(), baos); + contentData = baos.toByteArray(); + } catch (SocketTimeoutException e) { + // This can happen if the request body is smaller than the size specified in the + // Content-Length header, and using tomcat APR connector. + LOG.error("SocketTimeoutException reading request body from inputstream. error=" + String.valueOf(e.getMessage())); + if (contentData == null) { + contentData = new byte[0]; + } + } try { LOG.debug("Length of contentData byte array = " + contentData.length);