Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContentCachingResponseWrapper should not add “Content-Length” when “Transfer-Encoding” is present #26182

Closed
KasunAbey opened this issue Dec 1, 2020 · 5 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Milestone

Comments

@KasunAbey
Copy link

Affects: <4.3.21>


I'm using ContentCachingResponseWrapper to read response information in a inherited OncePerRequestFilter.doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) level.
At the end I'm calling ContentCachingResponseWrapper.copyBodyToResponse() to copy response body.

When my API returns ResponseEntity with custom body type, I'm getting following Parse Error;
Parse Error: The response headers can't include "Content-Length" with chunked encoding

This happens when I use Spring Cloud OpenFeign web client to do the API call. So in the OpenFeign GET response includes "transfer-encoding" in the headers and then when I call ContentCachingResponseWrapper.copyBodyToResponse() it is adding "Content-Length" to the headers.

My code is as follows;

OncePerRequestFilter overriden method;

@Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException
    {
        ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
        ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);

        try
        {
            if (ocpHttpTraceService.isLoggingPropertyEnabled(TracingConstants.BODY, TraceType.INBOUND))
            {
                filterChain.doFilter(requestWrapper, responseWrapper);
            }
            else
            {
                filterChain.doFilter(request, response);
            }

            System.out.println(getRequestBody(requestWrapper));
            System.out.println(getResponseBody(responseWrapper));
        }
        finally
        {
            responseWrapper.copyBodyToResponse();
        }
    }
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 1, 2020
@rstoyanchev rstoyanchev self-assigned this Dec 1, 2020
@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 1, 2020
@rstoyanchev rstoyanchev added this to the 5.3.2 milestone Dec 1, 2020
@rstoyanchev
Copy link
Contributor

We can improve this to ensure Content-Length is not added if Transfer-Encoding is "chunked".

@rstoyanchev
Copy link
Contributor

By the way you should try and be on at least the latest 4.3.x release which at this point is 4.3.29. I'm wondering also if you have any plans to upgrade to a 5.x release? I think this is reasonably safe to backport but nevertheless I'd rather minimize any potential for unintended impact.

@KasunAbey
Copy link
Author

Hi @rstoyanchev ,
For the moment we don't have any plans to upgrade to 5.x.
Please backport if possible to 4.3.29. We will use it.
Thanks

@rstoyanchev rstoyanchev changed the title ContentCachingResponseWrapper.copyBodyToResponse() add “Content-Length” to response headers even if “transfer-encoding” is there, throws Parse Error ContentCachingResponseWrapper should not add “Content-Length” when “Transfer-Encoding” is present Dec 1, 2020
@rstoyanchev rstoyanchev added the for: backport-to-5.2.x Marks an issue as a candidate for backport to 5.2.x label Dec 1, 2020
@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.2.x Marks an issue as a candidate for backport to 5.2.x labels Dec 1, 2020
@rstoyanchev rstoyanchev added the for: backport-to-5.1.x Marks an issue as a candidate for backport to 5.1.x label Dec 1, 2020
@spring-projects-issues spring-projects-issues removed the for: backport-to-5.1.x Marks an issue as a candidate for backport to 5.1.x label Dec 1, 2020
@rstoyanchev
Copy link
Contributor

I'll backport to 4.3.x but the fix depends on the ability to get a header from the response which will work only in a Servlet 3+ environment.

@KasunAbey
Copy link
Author

Noted, thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants