Commit d62c788c by Mário Buratto Committed by Ryan Baxter

Fixed content length handling issues. (#2894) (#2920)

* Fixed content length handling issues. (#2894) * Added test setup and cleanup for RequestContext. (#2894)
parent 21ff95e9
......@@ -35,6 +35,7 @@ import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
......@@ -56,6 +57,7 @@ import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.Host;
import org.springframework.cloud.netflix.zuul.util.ZuulRuntimeException;
import org.springframework.context.event.EventListener;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
......@@ -91,6 +93,7 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
private HttpClientConnectionManager connectionManager;
private CloseableHttpClient httpClient;
private boolean customHttpClient = false;
private boolean useServlet31 = true;
@EventListener
public void onPropertyChange(EnvironmentChangeEvent event) {
......@@ -125,6 +128,7 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
.isForceOriginalQueryStringEncoding();
this.connectionManagerFactory = connectionManagerFactory;
this.httpClientFactory = httpClientFactory;
checkServletVersion();
}
public SimpleHostRoutingFilter(ProxyRequestHelper helper, ZuulProperties properties,
......@@ -136,6 +140,7 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
.isForceOriginalQueryStringEncoding();
this.httpClient = httpClient;
this.customHttpClient = true;
checkServletVersion();
}
@PostConstruct
......@@ -191,7 +196,7 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
.buildZuulRequestQueryParams(request);
String verb = getVerb(request);
InputStream requestEntity = getRequestBody(request);
if (request.getContentLength() < 0) {
if (getContentLength(request) < 0) {
context.setChunkedRequestBody();
}
......@@ -209,6 +214,21 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
return null;
}
protected void checkServletVersion() {
// To support Servlet API 3.1 we need to check if getContentLengthLong exists
// Spring 5 minimum support is 3.0, so this stays
try {
HttpServletRequest.class.getMethod("getContentLengthLong");
useServlet31 = true;
} catch(NoSuchMethodException e) {
useServlet31 = false;
}
}
protected void setUseServlet31(boolean useServlet31) {
this.useServlet31 = useServlet31;
}
protected HttpClientConnectionManager getConnectionManager() {
return connectionManager;
}
......@@ -232,7 +252,7 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
URL host = RequestContext.getCurrentContext().getRouteHost();
HttpHost httpHost = getHttpHost(host);
uri = StringUtils.cleanPath((host.getPath() + uri).replaceAll("/{2,}", "/"));
int contentLength = request.getContentLength();
long contentLength = getContentLength(request);
ContentType contentType = null;
......@@ -376,4 +396,19 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
boolean isSslHostnameValidationEnabled() {
return this.sslHostnameValidationEnabled;
}
}
// Get the header value as a long in order to more correctly proxy very large requests
protected long getContentLength(HttpServletRequest request) {
if(useServlet31){
return request.getContentLengthLong();
}
String contentLengthHeader = request.getHeader(HttpHeaders.CONTENT_LENGTH);
if (contentLengthHeader != null) {
try {
return Long.parseLong(contentLengthHeader);
}
catch (NumberFormatException e){}
}
return request.getContentLength();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment