/* * 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.tomcat.util.buf;
/** * All URL decoding happens here. This way we can reuse, review, optimize without adding complexity to the buffers. The * conversion will modify the original buffer. * * @author Costin Manolache
*/ publicfinalclass UDecoder {
privatestaticfinal StringManager sm = StringManager.getManager(UDecoder.class);
@Override publicsynchronized Throwable fillInStackTrace() { // This class does not provide a stack trace returnthis;
}
}
/** Unexpected end of data. */ privatestaticfinal IOException EXCEPTION_EOF = new DecodeException(sm.getString("uDecoder.eof"));
/** %xx with not-hex digit */ privatestaticfinal IOException EXCEPTION_NOT_HEX_DIGIT = new DecodeException(sm.getString("uDecoder.isHexDigit"));
/** %-encoded slash is forbidden in resource path */ privatestaticfinal IOException EXCEPTION_SLASH = new DecodeException(sm.getString("uDecoder.noSlash"));
/** * URLDecode, will modify the source. Assumes source bytes are encoded using a superset of US-ASCII as per RFC 7230. * "%2f" will be rejected unless the input is a query string. * * @param mb The URL encoded bytes * @param query {@code true} if this is a query string. For a query string '+' will be decoded to ' ' * * @throws IOException Invalid %xx URL encoding
*/ publicvoid convert(ByteChunk mb, boolean query) throws IOException { if (query) {
convert(mb, true, EncodedSolidusHandling.DECODE);
} else {
convert(mb, false, EncodedSolidusHandling.REJECT);
}
}
/** * URLDecode, will modify the source. Assumes source bytes are encoded using a superset of US-ASCII as per RFC 7230. * * @param mb The URL encoded bytes * @param encodedSolidusHandling How should the %2f sequence handled by the decoder? For query strings this * parameter will be ignored and the %2f sequence will be decoded * * @throws IOException Invalid %xx URL encoding
*/ publicvoid convert(ByteChunk mb, EncodedSolidusHandling encodedSolidusHandling) throws IOException {
convert(mb, false, encodedSolidusHandling);
}
/** * Decode and return the specified URL-encoded String. It is assumed the string is not a query string. * * @param str The url-encoded string * @param charset The character encoding to use; if null, UTF-8 is used. * * @return the decoded string * * @exception IllegalArgumentException if a '%' character is not followed by a valid 2-digit hexadecimal number
*/ publicstatic String URLDecode(String str, Charset charset) { if (str == null) { returnnull;
}
if (str.indexOf('%') == -1) { // No %nn sequences, so return string unchanged return str;
}
if (charset == null) {
charset = StandardCharsets.UTF_8;
}
/* * Decoding is required. * * Potential complications: * * - The source String may be partially decoded so it is not valid to assume that the source String is ASCII. * * - Have to process as characters since there is no guarantee that the byte sequence for '%' is going to be the * same in all character sets. * * - We don't know how many '%nn' sequences are required for a single character. It varies between character * sets and some use a variable length.
*/
// This isn't perfect but it is a reasonable guess for the size of the // array required
ByteArrayOutputStream baos = new ByteArrayOutputStream(str.length() * 2);
OutputStreamWriter osw = new OutputStreamWriter(baos, charset);
char[] sourceChars = str.toCharArray(); int len = sourceChars.length; int ix = 0;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.