From 5d6a1e3025958cdba1cfffa9f227b2c3dc59a3f0 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Sat, 25 Feb 2017 12:37:50 +0900 Subject: [PATCH] Issue #1351 Ignore missing quality --- .../eclipse/jetty/http/QuotedQualityCSV.java | 14 +++++--- .../eclipse/jetty/http/HttpFieldsTest.java | 36 ++++++++++++++++--- .../jetty/http/QuotedQualityCSVTest.java | 26 ++++++++++++-- 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/QuotedQualityCSV.java b/jetty-http/src/main/java/org/eclipse/jetty/http/QuotedQualityCSV.java index b18567c72d9..7aeb7f52193 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/QuotedQualityCSV.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/QuotedQualityCSV.java @@ -67,7 +67,14 @@ public class QuotedQualityCSV extends QuotedCSV implements Iterable @Override protected void parsedParam(StringBuffer buffer, int valueLength, int paramName, int paramValue) { - if (buffer.charAt(paramName)=='q' && paramValue>paramName && buffer.charAt(paramName+1)=='=') + if (paramName<0) + { + if (buffer.charAt(buffer.length()-1)==';') + buffer.setLength(buffer.length()-1); + } + if (paramValue>=0 && + buffer.charAt(paramName)=='q' && paramValue>paramName && + buffer.length()>=paramName && buffer.charAt(paramName+1)=='=') { Double q; try @@ -108,7 +115,6 @@ public class QuotedQualityCSV extends QuotedCSV implements Iterable _sorted=true; Double last = ZERO; - int len = Integer.MIN_VALUE; for (int i = _values.size(); i-- > 0;) { @@ -116,20 +122,18 @@ public class QuotedQualityCSV extends QuotedCSV implements Iterable Double q = _quality.get(i); int compare=last.compareTo(q); - if (compare > 0 || (compare==0 && v.length() 0) { _values.set(i, _values.get(i + 1)); _values.set(i + 1, v); _quality.set(i, _quality.get(i + 1)); _quality.set(i + 1, q); last = ZERO; - len=0; i = _values.size(); continue; } last=q; - len=v.length(); } int last_element=_quality.size(); diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpFieldsTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpFieldsTest.java index 15617711e04..0e0c5ab218a 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpFieldsTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpFieldsTest.java @@ -433,15 +433,41 @@ public class HttpFieldsTest fields.add("name", "nothing;q=0"); fields.add("name", "one;q=0.4"); fields.add("name", "three;x=y;q=0.2;a=b,two;q=0.3"); + fields.add("name", "first;"); List list = fields.getQualityCSV("name"); - assertEquals("zero",HttpFields.valueParameters(list.get(0),null)); - assertEquals("one",HttpFields.valueParameters(list.get(1),null)); - assertEquals("two",HttpFields.valueParameters(list.get(2),null)); - assertEquals("three",HttpFields.valueParameters(list.get(3),null)); - assertEquals("four",HttpFields.valueParameters(list.get(4),null)); + assertEquals("first",HttpFields.valueParameters(list.get(0),null)); + assertEquals("zero",HttpFields.valueParameters(list.get(1),null)); + assertEquals("one",HttpFields.valueParameters(list.get(2),null)); + assertEquals("two",HttpFields.valueParameters(list.get(3),null)); + assertEquals("three",HttpFields.valueParameters(list.get(4),null)); + assertEquals("four",HttpFields.valueParameters(list.get(5),null)); } + + @Test + public void testGetQualityCSVHeader() throws Exception + { + HttpFields fields = new HttpFields(); + + fields.put("some", "value"); + fields.add("Accept", "zero;q=0.9,four;q=0.1"); + fields.put("other", "value"); + fields.add("Accept", "nothing;q=0"); + fields.add("Accept", "one;q=0.4"); + fields.add("Accept", "three;x=y;q=0.2;a=b,two;q=0.3"); + fields.add("Accept", "first;"); + + + List list = fields.getQualityCSV(HttpHeader.ACCEPT); + assertEquals("first",HttpFields.valueParameters(list.get(0),null)); + assertEquals("zero",HttpFields.valueParameters(list.get(1),null)); + assertEquals("one",HttpFields.valueParameters(list.get(2),null)); + assertEquals("two",HttpFields.valueParameters(list.get(3),null)); + assertEquals("three",HttpFields.valueParameters(list.get(4),null)); + assertEquals("four",HttpFields.valueParameters(list.get(5),null)); + } + @Test public void testDateFields() throws Exception diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/QuotedQualityCSVTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/QuotedQualityCSVTest.java index 5c548008332..cbd6cc05647 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/QuotedQualityCSVTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/QuotedQualityCSVTest.java @@ -47,7 +47,9 @@ public class QuotedQualityCSVTest { QuotedQualityCSV values = new QuotedQualityCSV(); values.addValue("text/*, text/plain, text/plain;format=flowed, */*"); - Assert.assertThat(values,Matchers.contains("text/plain;format=flowed","text/plain","text/*","*/*")); + + // Note this sort is only on quality and not the most specific type as per 5.3.2 + Assert.assertThat(values,Matchers.contains("text/*","text/plain","text/plain;format=flowed","*/*")); } @Test @@ -78,9 +80,9 @@ public class QuotedQualityCSVTest Assert.assertThat(values,Matchers.contains( "compress", "gzip", - "gzip", - "gzip", "*", + "gzip", + "gzip", "compress", "identity" )); @@ -144,4 +146,22 @@ public class QuotedQualityCSVTest "value1.0", "value0.5;p=v")); } + + + @Test + public void testSameQuality() + { + QuotedQualityCSV values = new QuotedQualityCSV(); + values.addValue("one;q=0.5,two;q=0.5,three;q=0.5"); + Assert.assertThat(values.getValues(),Matchers.contains("one","two","three")); + } + + @Test + public void testNoQuality() + { + QuotedQualityCSV values = new QuotedQualityCSV(); + values.addValue("one,two;,three;x=y"); + Assert.assertThat(values.getValues(),Matchers.contains("one","two","three;x=y")); + } + }