Merge remote-tracking branch 'origin/2.1.x'
This commit is contained in:
@@ -88,7 +88,7 @@ credentials and you already have those.
|
||||
|
||||
The projects that require middleware generally include a
|
||||
`docker-compose.yml`, so consider using
|
||||
https://compose.docker.io/[Docker Compose] to run the middeware servers
|
||||
https://docs.docker.com/compose/[Docker Compose] to run the middeware servers
|
||||
in Docker containers. See the README in the
|
||||
https://github.com/spring-cloud-samples/scripts[scripts demo
|
||||
repository] for specific instructions about the common cases of mongo,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" standalone="no"?>
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "https://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
|
||||
<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<metadata></metadata>
|
||||
<defs>
|
||||
|
||||
|
Before Width: | Height: | Size: 83 KiB After Width: | Height: | Size: 83 KiB |
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" standalone="no"?>
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "https://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
|
||||
<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<metadata></metadata>
|
||||
<defs>
|
||||
|
||||
|
Before Width: | Height: | Size: 362 KiB After Width: | Height: | Size: 362 KiB |
@@ -129,6 +129,7 @@ public class SendResponseFilter extends ZuulFilter {
|
||||
servletResponse.setCharacterEncoding("UTF-8");
|
||||
}
|
||||
|
||||
String servletResponseContentEncoding = getResponseContentEncoding(context);
|
||||
OutputStream outStream = servletResponse.getOutputStream();
|
||||
InputStream is = null;
|
||||
try {
|
||||
@@ -144,13 +145,18 @@ public class SendResponseFilter extends ZuulFilter {
|
||||
// decompress stream before sending to client
|
||||
// else, stream gzip directly to client
|
||||
if (isGzipRequested(context)) {
|
||||
servletResponse.setHeader(ZuulHeaders.CONTENT_ENCODING, "gzip");
|
||||
servletResponseContentEncoding = "gzip";
|
||||
}
|
||||
else {
|
||||
servletResponseContentEncoding = null;
|
||||
is = handleGzipStream(is);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (servletResponseContentEncoding != null) {
|
||||
servletResponse.setHeader(ZuulHeaders.CONTENT_ENCODING,
|
||||
servletResponseContentEncoding);
|
||||
}
|
||||
|
||||
if (is != null) {
|
||||
writeResponse(is, outStream);
|
||||
@@ -230,6 +236,18 @@ public class SendResponseFilter extends ZuulFilter {
|
||||
&& HTTPRequestUtils.getInstance().isGzipped(requestEncoding);
|
||||
}
|
||||
|
||||
private String getResponseContentEncoding(RequestContext context) {
|
||||
List<Pair<String, String>> zuulResponseHeaders = context.getZuulResponseHeaders();
|
||||
if (zuulResponseHeaders != null) {
|
||||
for (Pair<String, String> it : zuulResponseHeaders) {
|
||||
if (ZuulHeaders.CONTENT_ENCODING.equalsIgnoreCase(it.first())) {
|
||||
return it.second();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void writeResponse(InputStream zin, OutputStream out) throws Exception {
|
||||
byte[] bytes = buffers.get();
|
||||
int bytesRead = -1;
|
||||
@@ -255,7 +273,9 @@ public class SendResponseFilter extends ZuulFilter {
|
||||
List<Pair<String, String>> zuulResponseHeaders = context.getZuulResponseHeaders();
|
||||
if (zuulResponseHeaders != null) {
|
||||
for (Pair<String, String> it : zuulResponseHeaders) {
|
||||
servletResponse.addHeader(it.first(), it.second());
|
||||
if (!ZuulHeaders.CONTENT_ENCODING.equalsIgnoreCase(it.first())) {
|
||||
servletResponse.addHeader(it.first(), it.second());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (includeContentLengthHeader(context)) {
|
||||
|
||||
@@ -128,6 +128,29 @@ public class SendResponseFilterTests {
|
||||
assertThat(contentLength).as("wrong origin content length").isEqualTo("6");
|
||||
}
|
||||
|
||||
/*
|
||||
* Unknown encoding requested and NOT a GZip response -> Content-Length forwarded asis
|
||||
*/
|
||||
@Test
|
||||
public void runWithOriginContentLength_content_encoding_header() throws Exception {
|
||||
ZuulProperties properties = new ZuulProperties();
|
||||
properties.setSetContentLength(true);
|
||||
|
||||
SendResponseFilter filter = createFilter(properties, "hello", null,
|
||||
new MockHttpServletResponse(), false);
|
||||
RequestContext.getCurrentContext().addZuulResponseHeader("Content-Encoding",
|
||||
"unknown");
|
||||
RequestContext.getCurrentContext().setOriginContentLength(6L); // for test
|
||||
RequestContext.getCurrentContext().setResponseGZipped(false);
|
||||
filter.run();
|
||||
|
||||
MockHttpServletResponse response = (MockHttpServletResponse) RequestContext
|
||||
.getCurrentContext().getResponse();
|
||||
assertThat(response.getHeader("Content-Length")).as("wrong origin content length")
|
||||
.isEqualTo("6");
|
||||
assertThat(response.getHeader("Content-Encoding")).isEqualTo("unknown");
|
||||
}
|
||||
|
||||
/*
|
||||
* GZip requested and GZip response -> Content-Length forwarded asis, response
|
||||
* compressed
|
||||
@@ -193,6 +216,37 @@ public class SendResponseFilterTests {
|
||||
assertThat(response.getContentAsString()).as("wrong content").isEqualTo("hello");
|
||||
}
|
||||
|
||||
/*
|
||||
* GZip NOT requested and GZip response -> Content-Length discarded and response
|
||||
* uncompressed
|
||||
*/
|
||||
@Test
|
||||
public void runWithOriginContentLength_gzipNotRequested_gzipResponse_content_encoding_header()
|
||||
throws Exception {
|
||||
ZuulProperties properties = new ZuulProperties();
|
||||
properties.setSetContentLength(true);
|
||||
|
||||
SendResponseFilter filter = new SendResponseFilter(properties);
|
||||
|
||||
byte[] gzipData = gzipData("hello");
|
||||
|
||||
RequestContext.getCurrentContext().addZuulResponseHeader("Content-Encoding",
|
||||
"gzip");
|
||||
RequestContext.getCurrentContext().setOriginContentLength((long) gzipData.length); // for
|
||||
// test
|
||||
RequestContext.getCurrentContext().setResponseGZipped(true);
|
||||
RequestContext.getCurrentContext()
|
||||
.setResponseDataStream(new ByteArrayInputStream(gzipData));
|
||||
|
||||
filter.run();
|
||||
|
||||
MockHttpServletResponse response = (MockHttpServletResponse) RequestContext
|
||||
.getCurrentContext().getResponse();
|
||||
assertThat(response.getHeader("Content-Length")).isNull();
|
||||
assertThat(response.getHeader("Content-Encoding")).isNull();
|
||||
assertThat(response.getContentAsString()).as("wrong content").isEqualTo("hello");
|
||||
}
|
||||
|
||||
/*
|
||||
* Origin sends a non gzip response with Content-Encoding: gzip Request does not
|
||||
* support GZIP -> filter fails to uncompress and send stream "asis". Content-Length
|
||||
|
||||
Reference in New Issue
Block a user