|
| 1 | +/* |
| 2 | + * Copyright (c) 2011-2017 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 7 | + * which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 10 | + */ |
| 11 | + |
| 12 | +package io.vertx.core.net.impl; |
| 13 | + |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author <a href="mailto:plopes@redhat.com">Paulo Lopes</a> |
| 18 | + */ |
| 19 | +public final class URIDecoder { |
| 20 | + |
| 21 | + private URIDecoder() { |
| 22 | + throw new RuntimeException("Static Class"); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Decodes a segment of an URI encoded by a browser. |
| 27 | + * |
| 28 | + * The string is expected to be encoded as per RFC 3986, Section 2. This is the encoding used by JavaScript functions |
| 29 | + * encodeURI and encodeURIComponent, but not escape. For example in this encoding, é (in Unicode U+00E9 or in |
| 30 | + * UTF-8 0xC3 0xA9) is encoded as %C3%A9 or %c3%a9. |
| 31 | + * |
| 32 | + * Plus signs '+' will be handled as spaces and encoded using the default JDK URLEncoder class. |
| 33 | + * |
| 34 | + * @param s string to decode |
| 35 | + * |
| 36 | + * @return decoded string |
| 37 | + */ |
| 38 | + public static String decodeURIComponent(String s) { |
| 39 | + return decodeURIComponent(s, true); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Decodes a segment of an URI encoded by a browser. |
| 44 | + * |
| 45 | + * The string is expected to be encoded as per RFC 3986, Section 2. This is the encoding used by JavaScript functions |
| 46 | + * encodeURI and encodeURIComponent, but not escape. For example in this encoding, é (in Unicode U+00E9 or in |
| 47 | + * UTF-8 0xC3 0xA9) is encoded as %C3%A9 or %c3%a9. |
| 48 | + * |
| 49 | + * @param s string to decode |
| 50 | + * @param plus weather or not to transform plus signs into spaces |
| 51 | + * |
| 52 | + * @return decoded string |
| 53 | + */ |
| 54 | + public static String decodeURIComponent(String s, boolean plus) { |
| 55 | + if (s == null) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + final int size = s.length(); |
| 60 | + boolean modified = false; |
| 61 | + int i; |
| 62 | + for (i = 0; i < size; i++) { |
| 63 | + final char c = s.charAt(i); |
| 64 | + if (c == '%' || (plus && c == '+')) { |
| 65 | + modified = true; |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + if (!modified) { |
| 70 | + return s; |
| 71 | + } |
| 72 | + final byte[] buf = s.getBytes(StandardCharsets.UTF_8); |
| 73 | + int pos = i; // position in `buf'. |
| 74 | + for (; i < size; i++) { |
| 75 | + char c = s.charAt(i); |
| 76 | + if (c == '%') { |
| 77 | + if (i == size - 1) { |
| 78 | + throw new IllegalArgumentException("unterminated escape" |
| 79 | + + " sequence at end of string: " + s); |
| 80 | + } |
| 81 | + c = s.charAt(++i); |
| 82 | + if (c == '%') { |
| 83 | + buf[pos++] = '%'; // "%%" -> "%" |
| 84 | + break; |
| 85 | + } |
| 86 | + if (i >= size - 1) { |
| 87 | + throw new IllegalArgumentException("partial escape" |
| 88 | + + " sequence at end of string: " + s); |
| 89 | + } |
| 90 | + c = decodeHexNibble(c); |
| 91 | + final char c2 = decodeHexNibble(s.charAt(++i)); |
| 92 | + if (c == Character.MAX_VALUE || c2 == Character.MAX_VALUE) { |
| 93 | + throw new IllegalArgumentException( |
| 94 | + "invalid escape sequence `%" + s.charAt(i - 1) |
| 95 | + + s.charAt(i) + "' at index " + (i - 2) |
| 96 | + + " of: " + s); |
| 97 | + } |
| 98 | + c = (char) (c * 16 + c2); |
| 99 | + // shouldn't check for plus since it would be a double decoding |
| 100 | + buf[pos++] = (byte) c; |
| 101 | + } else { |
| 102 | + buf[pos++] = (byte) (plus && c == '+' ? ' ' : c); |
| 103 | + } |
| 104 | + } |
| 105 | + return new String(buf, 0, pos, StandardCharsets.UTF_8); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Helper to decode half of a hexadecimal number from a string. |
| 110 | + * @param c The ASCII character of the hexadecimal number to decode. |
| 111 | + * Must be in the range {@code [0-9a-fA-F]}. |
| 112 | + * @return The hexadecimal value represented in the ASCII character |
| 113 | + * given, or {@link Character#MAX_VALUE} if the character is invalid. |
| 114 | + */ |
| 115 | + private static char decodeHexNibble(final char c) { |
| 116 | + if ('0' <= c && c <= '9') { |
| 117 | + return (char) (c - '0'); |
| 118 | + } else if ('a' <= c && c <= 'f') { |
| 119 | + return (char) (c - 'a' + 10); |
| 120 | + } else if ('A' <= c && c <= 'F') { |
| 121 | + return (char) (c - 'A' + 10); |
| 122 | + } else { |
| 123 | + return Character.MAX_VALUE; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments