Zuul RibbonRoutingFilter Content-Encoding gzip response issue
In our setup, We are accessing the Node JS server thorough ZUUL and Sidecar.
When we invoke the java scripts resources with Accept-Encoding=[gzip] header, Node JS returns compressed file with Content-Encoding=[gzip] header. But if the same request is routed through Zuul we are getting decompressed response.
While I am debugging it, I found that the response (ClientHttpResponse response = command.execute()) retrived in RibbonRoutingFilter class doesnt have the Content-Encoding=[gzip] header because of that SendResponseFilter class is not adding the Content-Encoding header in response.
Based on further analysis we found that
When the request is forwarded to downstream systems based on service id, zuul is using the ribbon load balanced routing filter. In this process the apache http client removes the below headers from response in ResponseContentEncoding class o "Content-Length" o "Content-Encoding" o "Content-MD5" Because of that the content is automatically decompressed in zuul and send it to the caller.
When the request is forwarded to downstream systems based on URL, zuul is using simple host routing filter. In this flow, disableContentCompression method is used while building the http client so the content is send to the caller without decompressing.
I believe if we add the method call disableContentCompression in RibbonLoadBalancingHttpClient class createDelegate method, this issue will be resolved.
Please let me know of any specific reason for not using disableContentCompression in ribbonloadbalancedroutingfilter route and let me know of any workaround to resolve this ?
Environment : Spring Cloud version : Dalston.SR2 Also tried with Dalston.SR4 Spring Boot : 1.5.4.RELEASE
As a work around we are planning to use okhttp client by adding the dependency. Let us know if we have to be cautious on any specific feature while using okhttp client.
I'm dealing with the same issue and I can't find a clean way around it. When debugging through requests I noticed that if could change HttpClientBuilder.contentCompressionDisabled to true, the ResponseContentEncoding interceptor isn't added and the GZIP response is preserved.
Switching to okhttp fixes the problem, but now some of the response headers are lowercase. That's fine as per the HTTP/1.1 spec, but I'm nervous about any clients with an expectation on header name case.
So here's what I have to work around it with httpcomponents: First, I extend RibbonLoadBalancingHttpClient and override the createDelegate method to call HttpClientBuilder.disableContentCompression(). Next, I extend AbstractRibbonCommandFactory to create my custom RibbonLoadBalancingHttpClient. Finally, I create a @Bean for my custom command factory. It's janky, but it seems to work.
We are experiencing this issue as well and have confirmed that requests NOT having an Accept-Encoding header return a response where the content-encoding header is gzip and the body is plain text
We are able to work around this issue by implementing a custom ApacheHttpClientFactory which does not disable content compression on the HttpClientBuilder. This will ensure the response body is gzipped and has the appropriate content-encoding header value when the request Accept-Encoding header is either undefined or application/gzip.
public class CustomHttpClientFactory implements ApacheHttpClientFactory {
private HttpClientBuilder builder;
public CustomHttpClientFactory(HttpClientBuilder builder) {
this.builder = builder;
}
/**
* A custom {@link HttpClientBuilder}. The {@link HttpClientBuilder} returned will
* have content compression enabled, cookie management disabled, and use system properties.
*/
@Override
public HttpClientBuilder createBuilder() {
return this.builder.disableCookieManagement().useSystemProperties();
}
}
After working with @pway99 on the cause of our issue, I determined that this commit https://github.com/spring-cloud/spring-cloud-netflix/commit/1bca9371dc01f7ae482da2b9d7e1add64b7996ad was the cause of the issue. Because this commit, the Content-Encoding from the origin response will ALWAYS be forwarded to the client, regardless of if the response to the client is gzipped or not. I traced the execution and found this:
- Client makes a request to sidecar without Accept-Encoding
- Zuul forwards the request to the HttpClient
- HttpClient makes a request to origin with Accept-Encoding: gzip header
- Origin responds with Content-Encoding: gzip and compressed content
- We loop through the response headers @ https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-zuul/src/main/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelper.java#L186 and forward any response headers including Content-Encoding header to the zuul response headers. Before the change above, the Content-Encoding header would not be forwarded here.
- All of the zuul response headers are fowarded to the client response @ https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-zuul/src/main/java/org/springframework/cloud/netflix/zuul/filters/post/SendResponseFilter.java#L255 . Note that this includes the Content-Encoding header from Step 5
- We check if the response was encoded in this block https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-zuul/src/main/java/org/springframework/cloud/netflix/zuul/filters/post/SendResponseFilter.java#L137-L147
- The response is encoded (since Content-Encoding was in the origin response headers) so we enter the first if on line 138
- The client did not request encoding, so take the else at line 145
- The InputStream from the origin response is wrapped in a GZIPInputStream to decompress the incoming origin content.
- The wrapped InputStream is then copied to the client response output stream.
@pway99 @jsmithbuild I doubt this is the same issue as what was originally reported here since that code change was introduced recently. Regardless any interest in submitting a PR since you have done the investigation?
@ryanjbaxter I can create a PR for the issue I described. Do you want me to create a separate github issue for it as well?
PR only is fine @jsmithbuild
This module has entered maintenance mode. This means that the Spring Cloud team will no longer be adding new features to the module. We will fix blocker bugs and security issues, and we will also consider and review small pull requests from the community.