Skip to content

Commit

Permalink
Support for removing sdch encoding regardless of whether it is stored…
Browse files Browse the repository at this point in the history
… as a single/multi value
  • Loading branch information
danielkyu committed Jan 29, 2017
1 parent faba98f commit 6e0d253
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
32 changes: 16 additions & 16 deletions src/main/java/org/littleshoot/proxy/impl/ProxyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -645,22 +645,22 @@ public static FullHttpResponse createFullHttpResponse(HttpVersion httpVersion,
* Removes all occurrences of 'sdch' from the 'Accept-Encoding' header.
* @param headers The headers to modify.
*/
public static HttpHeaders removeSdchEncoding(HttpHeaders headers) {
if (headers == null) {
throw new IllegalArgumentException("Headers is null");
}

String ae = headers.get(HttpHeaders.Names.ACCEPT_ENCODING);
if (StringUtils.isNotBlank(ae)) {
// The former regex should remove occurrences of 'sdch' while the
// latter regex should take care of the dangling comma case when
// 'sdch' was the first element in the list and there are other
// encodings.
String noSdch = ae.replaceAll(",? *(sdch|SDCH)", "").replaceFirst("^ *, *", "");
headers.set(HttpHeaders.Names.ACCEPT_ENCODING, noSdch);
LOG.debug("Removed sdch and inserted: {}", noSdch);
public static void removeSdchEncoding(HttpHeaders headers) {
List<String> encodings = headers.getAll(HttpHeaders.Names.ACCEPT_ENCODING);
headers.remove(HttpHeaders.Names.ACCEPT_ENCODING);

for (String encoding : encodings) {
if (encoding != null) {
// The former regex should remove occurrences of 'sdch' while the
// latter regex should take care of the dangling comma case when
// 'sdch' was the first element in the list and there are other
// encodings.
encoding = encoding.replaceAll(",? *(sdch|SDCH)", "").replaceFirst("^ *, *", "");

if (StringUtils.isNotBlank(encoding)) {
headers.add(HttpHeaders.Names.ACCEPT_ENCODING, encoding);
}
}
}

return headers;
}
}
48 changes: 32 additions & 16 deletions src/test/java/org/littleshoot/proxy/impl/ProxyUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import io.netty.handler.codec.http.HttpVersion;
import org.junit.Test;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.Matchers.contains;
Expand Down Expand Up @@ -242,21 +244,28 @@ public void testSplitCommaSeparatedHeaderValues() {
*/
@Test
public void testRemoveSdchEncoding() {
final List<String> emptyList = new ArrayList<>();
// Various cases where 'sdch' is not present within the accepted
// encodings list
assertRemoveSdchEncoding("", "");
assertRemoveSdchEncoding("gzip", "gzip");
assertRemoveSdchEncoding("gzip, deflate, br", "gzip, deflate, br");
assertRemoveSdchEncoding(Arrays.asList(""), emptyList);
assertRemoveSdchEncoding(Arrays.asList("gzip"), Arrays.asList("gzip"));

assertRemoveSdchEncoding(Arrays.asList("gzip", "deflate", "br"), Arrays.asList("gzip", "deflate", "br"));
assertRemoveSdchEncoding(Arrays.asList("gzip, deflate, br"), Arrays.asList("gzip, deflate, br"));

// Various cases where 'sdch' is present within the accepted encodings
// list
assertRemoveSdchEncoding("sdch", "");
assertRemoveSdchEncoding("SDCH", "");
assertRemoveSdchEncoding("sdch, gzip", "gzip");
assertRemoveSdchEncoding("gzip,sdch,deflate", "gzip,deflate");
assertRemoveSdchEncoding("gzip, sdch, deflate", "gzip, deflate");
assertRemoveSdchEncoding("gzip,deflate,sdch", "gzip,deflate");
assertRemoveSdchEncoding("gzip, deflate, sdch", "gzip, deflate");
assertRemoveSdchEncoding(Arrays.asList("sdch"), emptyList);
assertRemoveSdchEncoding(Arrays.asList("SDCH"), emptyList);

assertRemoveSdchEncoding(Arrays.asList("sdch", "gzip"), Arrays.asList("gzip"));
assertRemoveSdchEncoding(Arrays.asList("sdch, gzip"), Arrays.asList("gzip"));

assertRemoveSdchEncoding(Arrays.asList("gzip", "sdch", "deflate"), Arrays.asList("gzip", "deflate"));
assertRemoveSdchEncoding(Arrays.asList("gzip, sdch, deflate"), Arrays.asList("gzip, deflate"));
assertRemoveSdchEncoding(Arrays.asList("gzip,deflate,sdch"), Arrays.asList("gzip,deflate"));

assertRemoveSdchEncoding(Arrays.asList("gzip", "deflate, sdch", "br"), Arrays.asList("gzip", "deflate", "br"));
}

/**
Expand All @@ -265,14 +274,21 @@ public void testRemoveSdchEncoding() {
*
* @param inputEncodings The input value of the 'Accept-Encoding' header
* that should be used as the basis for the assertion check.
* @param expectedEncodings The expected value of the 'Accept-Encoding'
* header after the 'sdch' encoding is removed.
* @param inputEncodings The input list that maps to the values of the
* 'Accept-Encoding' header that should be used as the basis for the
* assertion check.
* @param expectedEncodings The list containing the expected values of the
* 'Accept-Encoding' header after the 'sdch' encoding is removed.
*/
private void assertRemoveSdchEncoding(String inputEncodings, String expectedEncodings) {
private void assertRemoveSdchEncoding(List<String> inputEncodings, List<String> expectedEncodings) {
HttpHeaders headers = new DefaultHttpHeaders();

headers.add(HttpHeaders.Names.ACCEPT_ENCODING, inputEncodings);
assertEquals(expectedEncodings,
ProxyUtils.removeSdchEncoding(headers).get(HttpHeaders.Names.ACCEPT_ENCODING));
for (String encoding : inputEncodings) {
headers.add(HttpHeaders.Names.ACCEPT_ENCODING, encoding);
}

ProxyUtils.removeSdchEncoding(headers);
assertEquals(expectedEncodings, headers.getAll(HttpHeaders.Names.ACCEPT_ENCODING));
}
}

0 comments on commit 6e0d253

Please sign in to comment.