Skip to content

Commit

Permalink
Fixed content length handling issues. (#2894) (#2920)
Browse files Browse the repository at this point in the history
* Fixed content length handling issues. (#2894)

* Added test setup and cleanup for RequestContext. (#2894)
  • Loading branch information
marioburatto authored and ryanjbaxter committed May 14, 2018
1 parent 21ff95e commit d62c788
Show file tree
Hide file tree
Showing 2 changed files with 361 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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;
Expand All @@ -56,6 +57,7 @@
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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -125,6 +128,7 @@ public SimpleHostRoutingFilter(ProxyRequestHelper helper, ZuulProperties propert
.isForceOriginalQueryStringEncoding();
this.connectionManagerFactory = connectionManagerFactory;
this.httpClientFactory = httpClientFactory;
checkServletVersion();
}

public SimpleHostRoutingFilter(ProxyRequestHelper helper, ZuulProperties properties,
Expand All @@ -136,6 +140,7 @@ public SimpleHostRoutingFilter(ProxyRequestHelper helper, ZuulProperties propert
.isForceOriginalQueryStringEncoding();
this.httpClient = httpClient;
this.customHttpClient = true;
checkServletVersion();
}

@PostConstruct
Expand Down Expand Up @@ -191,7 +196,7 @@ public Object run() {
.buildZuulRequestQueryParams(request);
String verb = getVerb(request);
InputStream requestEntity = getRequestBody(request);
if (request.getContentLength() < 0) {
if (getContentLength(request) < 0) {
context.setChunkedRequestBody();
}

Expand All @@ -209,6 +214,21 @@ public Object run() {
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;
}
Expand All @@ -232,7 +252,7 @@ private CloseableHttpResponse forward(CloseableHttpClient httpclient, String ver
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;

Expand Down Expand Up @@ -376,4 +396,19 @@ protected void addIgnoredHeaders(String... names) {
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();
}
}
Loading

0 comments on commit d62c788

Please sign in to comment.