Skip to content

Commit

Permalink
- Catching SocketTimeoutException when copying request body inputstre…
Browse files Browse the repository at this point in the history
…am 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).
  • Loading branch information
kerumai committed Apr 22, 2015
1 parent 9224295 commit 195d0d6
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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<String, String[]> parameters;
private byte[] contentData = null;
private HashMap<String, String[]> parameters = null;

public HttpServletRequestWrapper() {
//a trick for Groovy
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 195d0d6

Please sign in to comment.