Commit 88396411 by Jin Zhang Committed by Spencer Gibb

improve efficiency of SendResponseFilter#addResponseHeaders() (#1622)

parent 42931f93
......@@ -199,30 +199,28 @@ public class SendResponseFilter extends ZuulFilter {
private void addResponseHeaders() {
RequestContext context = RequestContext.getCurrentContext();
HttpServletResponse servletResponse = context.getResponse();
List<Pair<String, String>> zuulResponseHeaders = context.getZuulResponseHeaders();
@SuppressWarnings("unchecked")
List<String> rd = (List<String>) RequestContext.getCurrentContext()
.get("routingDebug");
if (rd != null) {
StringBuilder debugHeader = new StringBuilder();
for (String it : rd) {
debugHeader.append("[[[" + it + "]]]");
}
if (INCLUDE_DEBUG_HEADER.get()) {
if (INCLUDE_DEBUG_HEADER.get()) {
@SuppressWarnings("unchecked")
List<String> rd = (List<String>) context.get("routingDebug");
if (rd != null) {
StringBuilder debugHeader = new StringBuilder();
for (String it : rd) {
debugHeader.append("[[[" + it + "]]]");
}
servletResponse.addHeader("X-Zuul-Debug-Header", debugHeader.toString());
}
}
List<Pair<String, String>> zuulResponseHeaders = context.getZuulResponseHeaders();
if (zuulResponseHeaders != null) {
for (Pair<String, String> it : zuulResponseHeaders) {
servletResponse.addHeader(it.first(), it.second());
}
}
RequestContext ctx = RequestContext.getCurrentContext();
Long contentLength = ctx.getOriginContentLength();
// Only inserts Content-Length if origin provides it and origin response is not
// gzipped
if (SET_CONTENT_LENGTH.get()) {
if ( contentLength != null && !ctx.getResponseGZipped()) {
Long contentLength = context.getOriginContentLength();
if ( contentLength != null && !context.getResponseGZipped()) {
if(useServlet31) {
servletResponse.setContentLengthLong(contentLength);
} else {
......
......@@ -19,6 +19,9 @@ package org.springframework.cloud.netflix.zuul.filters.post;
import java.io.ByteArrayInputStream;
import javax.servlet.http.HttpServletRequest;
import com.netflix.config.ConfigurationManager;
import com.netflix.zuul.constants.ZuulConstants;
import com.netflix.zuul.context.Debug;
import com.netflix.zuul.context.RequestContext;
import org.junit.After;
......@@ -64,6 +67,33 @@ public class SendResponseFilterTests {
runFilter(characterEncoding, content, true);
}
@Test
public void runWithDebugHeader() throws Exception {
ConfigurationManager.getConfigInstance().setProperty(ZuulConstants.ZUUL_INCLUDE_DEBUG_HEADER, true);
SendResponseFilter filter = createFilter("hello", null, new MockHttpServletResponse(), false);
Debug.addRoutingDebug("test");
filter.run();
String debugHeader = RequestContext.getCurrentContext().getResponse()
.getHeader("X-Zuul-Debug-Header");
assertThat("wrong debug header", debugHeader, equalTo("[[[test]]]"));
}
@Test
public void runWithOriginContentLength() throws Exception {
ConfigurationManager.getConfigInstance().setProperty(ZuulConstants.ZUUL_SET_CONTENT_LENGTH, true);
SendResponseFilter filter = createFilter("hello", null, new MockHttpServletResponse(), false);
RequestContext.getCurrentContext().setOriginContentLength(6L); // for test
RequestContext.getCurrentContext().setResponseGZipped(false);
filter.run();
String contentLength = RequestContext.getCurrentContext().getResponse()
.getHeader("Content-Length");
assertThat("wrong origin content length", contentLength, equalTo("6"));
}
private void runFilter(String characterEncoding, String content, boolean streamContent) throws Exception {
MockHttpServletResponse response = new MockHttpServletResponse();
SendResponseFilter filter = createFilter(content, characterEncoding, response, streamContent);
......
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