Skip to content

Commit

Permalink
feat(gzip): send gzip response when Accept-Encoding:* is present (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
munendrasn authored and decebals committed Nov 1, 2018
1 parent 33c4c7b commit 2eadf21
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pippo-core/src/main/java/ro/pippo/core/gzip/GZipFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package ro.pippo.core.gzip;

import ro.pippo.core.util.StringUtils;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
Expand Down Expand Up @@ -62,7 +64,9 @@ public void destroy() {
protected boolean acceptsGZipEncoding(HttpServletRequest request) {
String acceptEncoding = request.getHeader("accept-encoding");

return acceptEncoding != null && acceptEncoding.contains("gzip");
return !StringUtils.isNullOrEmpty(acceptEncoding) && (
acceptEncoding.contains("gzip") || acceptEncoding.contains("*")
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import ro.pippo.core.RequestResponse;
import ro.pippo.core.RequestResponseFactory;
import ro.pippo.core.Response;
import ro.pippo.core.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -53,7 +54,9 @@ public RequestResponse createRequestResponse(HttpServletRequest httpServletReque
protected boolean acceptsGZipEncoding(HttpServletRequest httpServletRequest) {
String acceptEncoding = httpServletRequest.getHeader("accept-encoding");

return acceptEncoding != null && acceptEncoding.contains("gzip");
return !StringUtils.isNullOrEmpty(acceptEncoding) && (
acceptEncoding.contains("gzip") || acceptEncoding.contains("*")
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.pippo.core.gzip;

import org.junit.Test;
import org.mockito.Mockito;
import ro.pippo.core.Application;

import javax.servlet.http.HttpServletRequest;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class GZipRequestResponseFactoryTest {

@Test
public void testAcceptGzipEncoding() {
GZipRequestResponseFactory requestResponseFactory = new GZipRequestResponseFactory(new Application());
HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);

// Accept-Encoding not specified
Mockito.doReturn(null).when(httpServletRequest).getHeader("accept-encoding");
assertFalse(requestResponseFactory.acceptsGZipEncoding(httpServletRequest));

// Accept-Encoding specified
Mockito.doReturn("gzip,deflate,identity").when(httpServletRequest).getHeader("accept-encoding");
assertTrue(requestResponseFactory.acceptsGZipEncoding(httpServletRequest));

// Explicit Accept-Encoding:*
Mockito.doReturn("*").when(httpServletRequest).getHeader("accept-encoding");
assertTrue(requestResponseFactory.acceptsGZipEncoding(httpServletRequest));

// No Gzip
Mockito.doReturn("deflate,identity").when(httpServletRequest).getHeader("accept-encoding");
assertFalse(requestResponseFactory.acceptsGZipEncoding(httpServletRequest));
}
}

0 comments on commit 2eadf21

Please sign in to comment.