Skip to content

Commit

Permalink
Optimisation for invalid quality case suggested by kkolinko
Browse files Browse the repository at this point in the history
Don't create return values that are going to be ignored

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1618260 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Aug 15, 2014
1 parent 85007e5 commit 478ed7a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
4 changes: 3 additions & 1 deletion java/org/apache/tomcat/util/http/parser/AcceptLanguage.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public static List<AcceptLanguage> parse(StringReader input) throws IOException
quality = HttpParser.readWeight(input, ',');
}

result.add(new AcceptLanguage(Locale.forLanguageTag(languageTag), quality));
if (quality > 0) {
result.add(new AcceptLanguage(Locale.forLanguageTag(languageTag), quality));
}
} while (true);

return result;
Expand Down
25 changes: 12 additions & 13 deletions test/org/apache/tomcat/util/http/parser/TestAcceptLanguage.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public class TestAcceptLanguage {
private static final double Q1_000 = 1;
private static final double Q0_500 = 0.5;
private static final double Q0_050 = 0.05;
private static final double Q0_000 = 0;

@Test
public void testSingle01() throws Exception {
Expand Down Expand Up @@ -153,56 +152,56 @@ public void testSingle13() throws Exception {

@Test
public void testMalformed01() throws Exception {
List<AcceptLanguage> actual = AcceptLanguage.parse(new StringReader("en-gb;x=1"));
List<AcceptLanguage> actual = AcceptLanguage.parse(new StringReader("en-gb;x=1,en-gb;q=0.5"));

Assert.assertEquals(1, actual.size());
Assert.assertEquals(L_EN_GB, actual.get(0).getLocale());
Assert.assertEquals(Q0_000, actual.get(0).getQuality(), 0.0001);
Assert.assertEquals(Q0_500, actual.get(0).getQuality(), 0.0001);
}

@Test
public void testMalformed02() throws Exception {
List<AcceptLanguage> actual = AcceptLanguage.parse(new StringReader("en-gb;q=a"));
List<AcceptLanguage> actual = AcceptLanguage.parse(new StringReader("en-gb;q=a,en-gb;q=0.5"));

Assert.assertEquals(1, actual.size());
Assert.assertEquals(L_EN_GB, actual.get(0).getLocale());
Assert.assertEquals(Q0_000, actual.get(0).getQuality(), 0.0001);
Assert.assertEquals(Q0_500, actual.get(0).getQuality(), 0.0001);
}

@Test
public void testMalformed03() throws Exception {
List<AcceptLanguage> actual = AcceptLanguage.parse(new StringReader("en-gb;q=0.5a"));
List<AcceptLanguage> actual = AcceptLanguage.parse(new StringReader("en-gb;q=0.5a,en-gb;q=0.5"));

Assert.assertEquals(1, actual.size());
Assert.assertEquals(L_EN_GB, actual.get(0).getLocale());
Assert.assertEquals(Q0_000, actual.get(0).getQuality(), 0.0001);
Assert.assertEquals(Q0_500, actual.get(0).getQuality(), 0.0001);
}

@Test
public void testMalformed04() throws Exception {
List<AcceptLanguage> actual = AcceptLanguage.parse(new StringReader("en-gb;q=0.05a"));
List<AcceptLanguage> actual = AcceptLanguage.parse(new StringReader("en-gb;q=0.05a,en-gb;q=0.5"));

Assert.assertEquals(1, actual.size());
Assert.assertEquals(L_EN_GB, actual.get(0).getLocale());
Assert.assertEquals(Q0_000, actual.get(0).getQuality(), 0.0001);
Assert.assertEquals(Q0_500, actual.get(0).getQuality(), 0.0001);
}

@Test
public void testMalformed05() throws Exception {
List<AcceptLanguage> actual = AcceptLanguage.parse(new StringReader("en-gb;q=0.005a"));
List<AcceptLanguage> actual = AcceptLanguage.parse(new StringReader("en-gb;q=0.005a,en-gb;q=0.5"));

Assert.assertEquals(1, actual.size());
Assert.assertEquals(L_EN_GB, actual.get(0).getLocale());
Assert.assertEquals(Q0_000, actual.get(0).getQuality(), 0.0001);
Assert.assertEquals(Q0_500, actual.get(0).getQuality(), 0.0001);
}

@Test
public void testMalformed06() throws Exception {
List<AcceptLanguage> actual = AcceptLanguage.parse(new StringReader("en-gb;q=0.00005a"));
List<AcceptLanguage> actual = AcceptLanguage.parse(new StringReader("en-gb;q=0.00005a,en-gb;q=0.5"));

Assert.assertEquals(1, actual.size());
Assert.assertEquals(L_EN_GB, actual.get(0).getLocale());
Assert.assertEquals(Q0_000, actual.get(0).getQuality(), 0.0001);
Assert.assertEquals(Q0_500, actual.get(0).getQuality(), 0.0001);
}

@Test
Expand Down

0 comments on commit 478ed7a

Please sign in to comment.