You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2015/10/15 23:44:02 UTC
svn commit: r1708896 - in /tomcat/trunk:
java/org/apache/catalina/core/ApplicationPushBuilder.java
test/org/apache/catalina/core/TestApplicationPushBuilder.java
Author: markt
Date: Thu Oct 15 21:44:01 2015
New Revision: 1708896
URL: http://svn.apache.org/viewvc?rev=1708896&view=rev
Log:
Servlet 4.0
Implement PushBuilder a TODO
Handle %nn encoded paths
Added:
tomcat/trunk/test/org/apache/catalina/core/TestApplicationPushBuilder.java (with props)
Modified:
tomcat/trunk/java/org/apache/catalina/core/ApplicationPushBuilder.java
Modified: tomcat/trunk/java/org/apache/catalina/core/ApplicationPushBuilder.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/ApplicationPushBuilder.java?rev=1708896&r1=1708895&r2=1708896&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/ApplicationPushBuilder.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/ApplicationPushBuilder.java Thu Oct 15 21:44:01 2015
@@ -38,6 +38,7 @@ import org.apache.catalina.connector.Req
import org.apache.catalina.util.SessionConfig;
import org.apache.coyote.ActionCode;
import org.apache.tomcat.util.buf.B2CConverter;
+import org.apache.tomcat.util.buf.HexUtils;
import org.apache.tomcat.util.collections.CaseInsensitiveKeyMap;
import org.apache.tomcat.util.res.StringManager;
@@ -397,7 +398,16 @@ public class ApplicationPushBuilder impl
}
- private static String decode(String input, String charsetName) {
+ // Package private so it can be tested
+ static String decode(String input, String charsetName) {
+ int start = input.indexOf('%');
+ int end = 0;
+
+ // Shortcut
+ if (start == -1) {
+ return input;
+ }
+
Charset charset;
try {
charset = B2CConverter.getCharset(charsetName);
@@ -407,7 +417,32 @@ public class ApplicationPushBuilder impl
throw new IllegalStateException(uee);
}
- // TODO implement %nn decoding
- return input;
+ StringBuilder result = new StringBuilder(input.length());
+ while (start != -1) {
+ // Found the start of a %nn sequence. Copy everything form the last
+ // end to this start to the output.
+ result.append(input.substring(end, start));
+ // Advance the end 3 characters: %nn
+ end = start + 3;
+ while (end <input.length() && input.charAt(end) == '%') {
+ end += 3;
+ }
+ result.append(decode(input.substring(start, end), charset));
+ start = input.indexOf('%', end);
+ }
+ // Append the remaining text
+ result.append(input.substring(end));
+
+ return result.toString();
+ }
+
+ private static String decode(String percentSequence, Charset charset) {
+ byte[] bytes = new byte[percentSequence.length()/3];
+ for (int i = 0; i < bytes.length; i += 3) {
+ bytes[i] = (byte) (HexUtils.getDec(percentSequence.charAt(1 + 3 * i)) << 4 +
+ HexUtils.getDec(percentSequence.charAt(2 + 3 * i)));
+ }
+
+ return new String(bytes, charset);
}
}
Added: tomcat/trunk/test/org/apache/catalina/core/TestApplicationPushBuilder.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TestApplicationPushBuilder.java?rev=1708896&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/core/TestApplicationPushBuilder.java (added)
+++ tomcat/trunk/test/org/apache/catalina/core/TestApplicationPushBuilder.java Thu Oct 15 21:44:01 2015
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+package org.apache.catalina.core;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestApplicationPushBuilder {
+
+ @Test
+ public void test01() {
+ doTest("foo", "UTF-8", "foo");
+ }
+
+ @Test
+ public void test02() {
+ doTest("/foo", "UTF-8", "/foo");
+ }
+
+ @Test
+ public void test03() {
+ doTest("%20foo", "UTF-8", " foo");
+ }
+
+ @Test
+ public void test04() {
+ doTest("fo%20o", "UTF-8", "fo o");
+ }
+
+ @Test
+ public void test05() {
+ doTest("foo%20", "UTF-8", "foo ");
+ }
+
+
+ private void doTest(String input, String charset, String expected) {
+ String result = ApplicationPushBuilder.decode(input, charset);
+ Assert.assertEquals(expected, result);
+ }
+}
Propchange: tomcat/trunk/test/org/apache/catalina/core/TestApplicationPushBuilder.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org