From: markt Date: Fri, 12 Nov 2010 16:52:02 +0000 (+0000) Subject: Additional tests and fixes for https://issues.apache.org/bugzilla/show_bug.cgi?id... X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=3b4574315bd882359b467a3b2fb971de6a9422f3;p=tomcat7.0 Additional tests and fixes for https://issues.apache.org/bugzilla/show_bug.cgi?id=49297 Duplicate attribute rules for the page directive are slightly different. Based on a patch by genspring git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1034468 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/jasper/compiler/Parser.java b/java/org/apache/jasper/compiler/Parser.java index 28c88dca1..c9ef2f5f4 100644 --- a/java/org/apache/jasper/compiler/Parser.java +++ b/java/org/apache/jasper/compiler/Parser.java @@ -150,7 +150,10 @@ class Parser implements TagConstants { * Attributes ::= (S Attribute)* S? */ Attributes parseAttributes() throws JasperException { - UniqueAttributesImpl attrs = new UniqueAttributesImpl(); + return parseAttributes(false); + } + Attributes parseAttributes(boolean pageDirective) throws JasperException { + UniqueAttributesImpl attrs = new UniqueAttributesImpl(pageDirective); reader.skipSpaces(); int ws = 1; @@ -177,7 +180,7 @@ class Parser implements TagConstants { public static Attributes parseAttributes(ParserController pc, JspReader reader) throws JasperException { Parser tmpParser = new Parser(pc, reader, false, false, null); - return tmpParser.parseAttributes(); + return tmpParser.parseAttributes(true); } /** @@ -327,7 +330,7 @@ class Parser implements TagConstants { * Attribute)* */ private void parsePageDirective(Node parent) throws JasperException { - Attributes attrs = parseAttributes(); + Attributes attrs = parseAttributes(true); Node.PageDirective n = new Node.PageDirective(attrs, start, parent); /* diff --git a/java/org/apache/jasper/util/UniqueAttributesImpl.java b/java/org/apache/jasper/util/UniqueAttributesImpl.java index ce3809901..67da6d1a5 100644 --- a/java/org/apache/jasper/util/UniqueAttributesImpl.java +++ b/java/org/apache/jasper/util/UniqueAttributesImpl.java @@ -29,7 +29,19 @@ import org.xml.sax.helpers.AttributesImpl; */ public class UniqueAttributesImpl extends AttributesImpl { - private Set qNames = new HashSet(); + private static final String IMPORT = "import"; + private static final String PAGE_ENCODING = "pageEncoding"; + + private final boolean pageDirective; + private final Set qNames = new HashSet(); + + public UniqueAttributesImpl() { + this.pageDirective = false; + } + + public UniqueAttributesImpl(boolean pageDirective) { + this.pageDirective = pageDirective; + } @Override public void clear() { @@ -41,7 +53,7 @@ public class UniqueAttributesImpl extends AttributesImpl { public void setAttributes(Attributes atts) { for (int i = 0; i < atts.getLength(); i++) { if (!qNames.add(atts.getQName(i))) { - handleDuplicate(atts.getQName(i)); + handleDuplicate(atts.getQName(i), atts.getValue(i)); } } super.setAttributes(atts); @@ -53,7 +65,7 @@ public class UniqueAttributesImpl extends AttributesImpl { if (qNames.add(qName)) { super.addAttribute(uri, localName, qName, type, value); } else { - handleDuplicate(qName); + handleDuplicate(qName, value); } } @@ -64,7 +76,7 @@ public class UniqueAttributesImpl extends AttributesImpl { if (qNames.add(qName)) { super.setAttribute(index, uri, localName, qName, type, value); } else { - handleDuplicate(qName); + handleDuplicate(qName, value); } } @@ -80,8 +92,29 @@ public class UniqueAttributesImpl extends AttributesImpl { super.setQName(index, qName); } - private void handleDuplicate(String qName) { + private void handleDuplicate(String qName, String value) { + if (pageDirective) { + if (IMPORT.equalsIgnoreCase(qName)) { + // Always merge imports + int i = super.getIndex(IMPORT); + String v = super.getValue(i); + super.setValue(i, v + "," + value); + return; + } else if (PAGE_ENCODING.equalsIgnoreCase(qName)) { + // Page encoding can only occur once per file so a second + // attribute - even one with a duplicate value - is an error + } else { + // Other attributes can be repeated if and only if the values + // are identical + String v = super.getValue(qName); + if (v.equals(value)) { + return; + } + } + } + + // Ordinary tag attributes can't be repeated, even with identical values throw new IllegalArgumentException( - Localizer.getMessage("jsp.error.duplicateqname", qName)); + Localizer.getMessage("jsp.error.duplicateqname", qName)); } } diff --git a/test/org/apache/jasper/compiler/TestParser.java b/test/org/apache/jasper/compiler/TestParser.java index 5fcd170c9..0c2fe3b77 100644 --- a/test/org/apache/jasper/compiler/TestParser.java +++ b/test/org/apache/jasper/compiler/TestParser.java @@ -144,6 +144,116 @@ public class TestParser extends TomcatBaseTest { assertEquals(500, sc); } + public void testBug49297MultipleImport1() throws Exception { + + Tomcat tomcat = getTomcatInstance(); + + File appDir = new File("test/webapp-3.0"); + // app dir is relative to server home + tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); + + tomcat.start(); + + ByteChunk res = new ByteChunk(); + int sc = getUrl("http://localhost:" + getPort() + + "/test/bug49nnn/bug49297MultipleImport1.jsp", res, + new HashMap>()); + + assertEquals(200, sc); + assertEcho(res.toString(), "OK"); + } + + public void testBug49297MultipleImport2() throws Exception { + + Tomcat tomcat = getTomcatInstance(); + + File appDir = new File("test/webapp-3.0"); + // app dir is relative to server home + tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); + + tomcat.start(); + + ByteChunk res = new ByteChunk(); + int sc = getUrl("http://localhost:" + getPort() + + "/test/bug49nnn/bug49297MultipleImport2.jsp", res, + new HashMap>()); + + assertEquals(200, sc); + assertEcho(res.toString(), "OK"); + } + + public void testBug49297MultiplePageEncoding1() throws Exception { + + Tomcat tomcat = getTomcatInstance(); + + File appDir = new File("test/webapp-3.0"); + // app dir is relative to server home + tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); + + tomcat.start(); + + ByteChunk res = new ByteChunk(); + int sc = getUrl("http://localhost:" + getPort() + + "/test/bug49nnn/bug49297MultiplePageEncoding1.jsp", res, + new HashMap>()); + + assertEquals(500, sc); + } + + public void testBug49297MultiplePageEncoding2() throws Exception { + + Tomcat tomcat = getTomcatInstance(); + + File appDir = new File("test/webapp-3.0"); + // app dir is relative to server home + tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); + + tomcat.start(); + + ByteChunk res = new ByteChunk(); + int sc = getUrl("http://localhost:" + getPort() + + "/test/bug49nnn/bug49297MultiplePageEncoding2.jsp", res, + new HashMap>()); + + assertEquals(500, sc); + } + + public void testBug49297MultiplePageEncoding3() throws Exception { + + Tomcat tomcat = getTomcatInstance(); + + File appDir = new File("test/webapp-3.0"); + // app dir is relative to server home + tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); + + tomcat.start(); + + ByteChunk res = new ByteChunk(); + int sc = getUrl("http://localhost:" + getPort() + + "/test/bug49nnn/bug49297MultiplePageEncoding3.jsp", res, + new HashMap>()); + + assertEquals(500, sc); + } + + public void testBug49297MultiplePageEncoding4() throws Exception { + + Tomcat tomcat = getTomcatInstance(); + + File appDir = new File("test/webapp-3.0"); + // app dir is relative to server home + tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); + + tomcat.start(); + + ByteChunk res = new ByteChunk(); + int sc = getUrl("http://localhost:" + getPort() + + "/test/bug49nnn/bug49297MultiplePageEncoding4.jsp", res, + new HashMap>()); + + assertEquals(500, sc); + } + /** Assertion for text printed by tags:echo */ private static void assertEcho(String result, String expected) { assertTrue(result.indexOf("

" + expected + "

") > 0); diff --git a/test/webapp-3.0/bug49nnn/bug49297MultipleImport1.jsp b/test/webapp-3.0/bug49nnn/bug49297MultipleImport1.jsp new file mode 100644 index 000000000..63c102b3c --- /dev/null +++ b/test/webapp-3.0/bug49nnn/bug49297MultipleImport1.jsp @@ -0,0 +1,28 @@ +<%-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +--%> +<%@page import="java.util.List" import="java.util.ArrayList" %> + + Bug 49297 multiple import test case + + <% + // Make sure the imports above do work + List l = new ArrayList(); + l.add("OK"); + %> +

<%=l.get(0)%>

+ + \ No newline at end of file diff --git a/test/webapp-3.0/bug49nnn/bug49297MultipleImport2.jsp b/test/webapp-3.0/bug49nnn/bug49297MultipleImport2.jsp new file mode 100644 index 000000000..28629a489 --- /dev/null +++ b/test/webapp-3.0/bug49nnn/bug49297MultipleImport2.jsp @@ -0,0 +1,29 @@ +<%-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +--%> +<%@page import="java.util.List" %> +<%@page import="java.util.ArrayList" %> + + Bug 49297 multiple import test case + + <% + // Make sure the imports above do work + List l = new ArrayList(); + l.add("OK"); + %> +

<%=l.get(0)%>

+ + \ No newline at end of file diff --git a/test/webapp-3.0/bug49nnn/bug49297MultiplePageEncoding1.jsp b/test/webapp-3.0/bug49nnn/bug49297MultiplePageEncoding1.jsp new file mode 100644 index 000000000..0d4381ddf --- /dev/null +++ b/test/webapp-3.0/bug49nnn/bug49297MultiplePageEncoding1.jsp @@ -0,0 +1,24 @@ +<%-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +--%> +<%@page pageEncoding="ISO-8859-1" %> +<%@page pageEncoding="ISO-8859-1" %> + + Bug 49297 multiple pageEncoding test case + +

Should fail

+ + \ No newline at end of file diff --git a/test/webapp-3.0/bug49nnn/bug49297MultiplePageEncoding2.jsp b/test/webapp-3.0/bug49nnn/bug49297MultiplePageEncoding2.jsp new file mode 100644 index 000000000..4ab49e151 --- /dev/null +++ b/test/webapp-3.0/bug49nnn/bug49297MultiplePageEncoding2.jsp @@ -0,0 +1,23 @@ +<%-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +--%> +<%@page pageEncoding="ISO-8859-1" pageEncoding="ISO-8859-1" %> + + Bug 49297 multiple pageEncoding test case + +

Should fail

+ + \ No newline at end of file diff --git a/test/webapp-3.0/bug49nnn/bug49297MultiplePageEncoding3.jsp b/test/webapp-3.0/bug49nnn/bug49297MultiplePageEncoding3.jsp new file mode 100644 index 000000000..2c631ca4e --- /dev/null +++ b/test/webapp-3.0/bug49nnn/bug49297MultiplePageEncoding3.jsp @@ -0,0 +1,24 @@ +<%-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +--%> +<%@page pageEncoding="ISO-8859-1" %> +<%@page pageEncoding="UTF-8" %> + + Bug 49297 multiple pageEncoding test case + +

Should fail

+ + \ No newline at end of file diff --git a/test/webapp-3.0/bug49nnn/bug49297MultiplePageEncoding4.jsp b/test/webapp-3.0/bug49nnn/bug49297MultiplePageEncoding4.jsp new file mode 100644 index 000000000..22e2a65b4 --- /dev/null +++ b/test/webapp-3.0/bug49nnn/bug49297MultiplePageEncoding4.jsp @@ -0,0 +1,23 @@ +<%-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +--%> +<%@page pageEncoding="ISO-8859-1" pageEncoding="UTF-8"%> + + Bug 49297 multiple pageEncoding test case + +

Should fail

+ + \ No newline at end of file diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index bc31f6b87..91785ecb2 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -155,7 +155,9 @@ whitespace before the attribute name. The whitespace test can be disabled by setting the system property org.apache.jasper.compiler.Parser.STRICT_WHITESPACE to - false. (markt) + false. Attributes of the page directive have slightly + different rules. The implementation of that part of the fix is based on + a patch by genspring. (markt) 50105: When processing composite EL expressions use