knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lmc...@apache.org
Subject [knox] branch master updated: [KNOX-2622] Support Deflate Encoding for the Inbound Response (#459)
Date Tue, 22 Jun 2021 02:02:54 GMT
This is an automated email from the ASF dual-hosted git repository.

lmccay pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/knox.git


The following commit(s) were added to refs/heads/master by this push:
     new 1ef048f  [KNOX-2622] Support Deflate Encoding for the Inbound Response (#459)
1ef048f is described below

commit 1ef048fa2d6ad6de566b1435880e3fe998839f59
Author: Zikun Xu <xuzikun2003@gmail.com>
AuthorDate: Mon Jun 21 19:02:44 2021 -0700

    [KNOX-2622] Support Deflate Encoding for the Inbound Response (#459)
---
 .../filter/rewrite/impl/UrlRewriteResponse.java      | 20 ++++++++++++++++++--
 .../filter/rewrite/impl/UrlRewriteResponseTest.java  | 19 +++++++++++++------
 2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponse.java
b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponse.java
index 73c0025..73f16ba 100644
--- a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponse.java
+++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponse.java
@@ -55,7 +55,9 @@ import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.zip.DeflaterOutputStream;
 import java.util.zip.GZIPOutputStream;
+import java.util.zip.InflaterInputStream;
 
 import static org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteUtil.getRewriteFilterConfig;
 import static org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteUtil.pickFirstRuleWithEqualsIgnoreCasePathMatch;
@@ -90,6 +92,7 @@ public class UrlRewriteResponse extends GatewayResponseWrapper implements
Params
   private String xForwardedHostname;
   private String xForwardedPort;
   private String xForwardedScheme;
+  private String contentEncoding;
 
   public UrlRewriteResponse( FilterConfig config, HttpServletRequest request, HttpServletResponse
response ) {
     super( response );
@@ -102,6 +105,7 @@ public class UrlRewriteResponse extends GatewayResponseWrapper implements
Params
     this.bodyFilterName = config.getInitParameter( UrlRewriteServletFilter.RESPONSE_BODY_FILTER_PARAM
);
     this.headersFilterName = config.getInitParameter( UrlRewriteServletFilter.RESPONSE_HEADERS_FILTER_PARAM
);
     this.headersFilterConfig = getRewriteFilterConfig( rewriter.getConfig(), headersFilterName,
UrlRewriteServletFilter.HEADERS_MIME_TYPE );
+    this.contentEncoding = "";
   }
 
   protected boolean ignoreHeader( String name ) {
@@ -121,11 +125,18 @@ public class UrlRewriteResponse extends GatewayResponseWrapper implements
Params
     return value;
   }
 
+  private void setContentEncoding(String name, String value) {
+    if ("Content-Encoding".equalsIgnoreCase(name)) {
+      contentEncoding = value;
+    }
+  }
+
   // Ignore the Content-Length from the dispatch respond since the respond body may be rewritten.
   @Override
   public void setHeader( String name, String value ) {
     if( !ignoreHeader( name) ) {
       value = rewriteValue( value, pickFirstRuleWithEqualsIgnoreCasePathMatch( headersFilterConfig,
name ) );
+      setContentEncoding(name, value);
       super.setHeader( name, value );
     }
   }
@@ -136,6 +147,7 @@ public class UrlRewriteResponse extends GatewayResponseWrapper implements
Params
     if( !ignoreHeader( name ) ) {
       String rule = pickFirstRuleWithEqualsIgnoreCasePathMatch( headersFilterConfig, name
);
       value = rewriteValue( value, rule );
+      setContentEncoding(name, value);
       super.addHeader( name, value );
     }
   }
@@ -175,14 +187,18 @@ public class UrlRewriteResponse extends GatewayResponseWrapper implements
Params
       inBuffer.reset();
 
       final InputStream unFilteredStream;
-      if(isGzip) {
+      if(isGzip || "gzip".equalsIgnoreCase(contentEncoding)) {
         unFilteredStream = new GzipCompressorInputStream(inBuffer, true);
+        outStream = new GZIPOutputStream(output, STREAM_BUFFER_SIZE);
+      } else if ("deflate".equalsIgnoreCase(contentEncoding)) {
+        unFilteredStream = new InflaterInputStream(inBuffer);
+        outStream = new DeflaterOutputStream(output);
       } else {
         unFilteredStream = inBuffer;
+        outStream = output;
       }
       String charset = MimeTypes.getCharset( mimeType, StandardCharsets.UTF_8.name() );
       inStream = filter.filter( unFilteredStream, charset, rewriter, this, UrlRewriter.Direction.OUT,
filterContentConfig );
-      outStream = (isGzip) ? new GZIPOutputStream(output, STREAM_BUFFER_SIZE) : output;
     } else {
       inStream = input;
       outStream = output;
diff --git a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponseTest.java
b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponseTest.java
index 93df8bc..2c53928 100644
--- a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponseTest.java
+++ b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/UrlRewriteResponseTest.java
@@ -35,8 +35,10 @@ import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.List;
+import java.util.zip.DeflaterOutputStream;
 import java.util.zip.GZIPInputStream;
 import java.util.zip.GZIPOutputStream;
+import java.util.zip.InflaterInputStream;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -135,7 +137,7 @@ public class UrlRewriteResponseTest {
 
     UrlRewriteResponse rewriteResponse = new UrlRewriteResponse(config, request, response);
     String content = "<?xml version=\"1.0\" standalone=\"no\"?><data>abc-大数据</data>";
-    testStreamResponse(content, rewriteResponse, false);
+    testStreamResponse(content, rewriteResponse, "");
   }
 
   @Test
@@ -158,16 +160,19 @@ public class UrlRewriteResponseTest {
     UrlRewriteResponse rewriteResponse = new UrlRewriteResponse( config, request, response
);
 
     String content = "content to test gzip streaming";
-    testStreamResponse(content, rewriteResponse, false);
-    testStreamResponse(content, rewriteResponse, true);
+    testStreamResponse(content, rewriteResponse, "");
+    testStreamResponse(content, rewriteResponse, "gzip");
+    testStreamResponse(content, rewriteResponse, "deflate");
   }
 
-  private void testStreamResponse(String content, UrlRewriteResponse rewriteResponse, boolean
isGzip) throws IOException {
+  private void testStreamResponse(String content, UrlRewriteResponse rewriteResponse, String
contentType) throws IOException {
     Path inputFile = Files.createTempFile("input", "test");
     Path outputFile = Files.createTempFile("output", "test");
     try {
       try(OutputStream outputStream = Files.newOutputStream(inputFile);
-          OutputStream outStream = isGzip ? new GZIPOutputStream( outputStream ) : outputStream)
{
+          OutputStream outStream = "gzip".equalsIgnoreCase(contentType) ?
+                  new GZIPOutputStream( outputStream ) :
+                  "deflate".equalsIgnoreCase(contentType) ? new DeflaterOutputStream( outputStream
) : outputStream) {
         outStream.write(content.getBytes(StandardCharsets.UTF_8));
       }
 
@@ -177,7 +182,9 @@ public class UrlRewriteResponseTest {
       }
 
       try(InputStream inputStream = Files.newInputStream(outputFile);
-          InputStream inStream = isGzip ? new GZIPInputStream(inputStream) : inputStream)
{
+          InputStream inStream = "gzip".equalsIgnoreCase(contentType) ?
+                  new GZIPInputStream(inputStream) :
+                  "deflate".equalsIgnoreCase(contentType) ? new InflaterInputStream(inputStream)
: inputStream) {
         assertThat(String.valueOf(IOUtils.toCharArray(inStream, StandardCharsets.UTF_8)),
is(content));
       }
     } finally {

Mime
View raw message