sentry-java icon indicating copy to clipboard operation
sentry-java copied to clipboard

Lazily cache request body for Spring requests instead of eagerly caching it

Open lee-jinhwan opened this issue 1 year ago • 8 comments

:scroll: Description

  • Spring provides a ContentCachingRequestWrapper that caches the body.
  • ~Support other Content-types, including JSON.~

:bulb: Motivation and Context

  • The goal of this PR is to fix Request#getParameter not returning a result if the POST request is application/x-www-form-urlencoded.
  • In Request in embed-Tomcat, if the InputStream is read first(body cached), the parameter will not be parsed.
// class org.apache.catalina.connector.Request  
 @Override
    public ServletInputStream getInputStream() throws IOException {

        if (usingReader) {
            throw new IllegalStateException(sm.getString("coyoteRequest.getInputStream.ise"));
        }

        usingInputStream = true;
        if (inputStream == null) {
            inputStream = new CoyoteInputStream(inputBuffer);
        }
        return inputStream;

    }

   ...

 protected void parseParameters() {
   ...
            if (usingInputStream || usingReader) {
                success = true;
                return;
            }
  ...

:green_heart: How did you test it?

:pencil: Checklist

  • [X] I reviewed the submitted code.
  • [X] I added tests to verify the changes.
  • [ ] No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • [ ] I updated the docs if needed.
  • [ ] Review from the native team if needed.
  • [ ] No breaking change or entry added to the changelog.
  • [ ] No breaking change for hybrid SDKs or communicated to hybrid SDKs.

:crystal_ball: Next steps

lee-jinhwan avatar Aug 16 '24 11:08 lee-jinhwan