001/* 002 * Copyright (C) 2009 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.net; 016 017import com.google.common.annotations.GwtCompatible; 018import com.google.common.escape.Escaper; 019 020/** 021 * {@code Escaper} instances suitable for strings to be included in particular sections of URLs. 022 * 023 * <p>If the resulting URLs are inserted into an HTML or XML document, they will require additional 024 * escaping with {@link com.google.common.html.HtmlEscapers} or {@link 025 * com.google.common.xml.XmlEscapers}. 026 * 027 * @author David Beaumont 028 * @author Chris Povirk 029 * @since 15.0 030 */ 031@GwtCompatible 032public final class UrlEscapers { 033 private UrlEscapers() {} 034 035 // For each xxxEscaper() method, please add links to external reference pages 036 // that are considered authoritative for the behavior of that escaper. 037 038 static final String URL_FORM_PARAMETER_OTHER_SAFE_CHARS = "-_.*"; 039 040 static final String URL_PATH_OTHER_SAFE_CHARS_LACKING_PLUS = 041 "-._~" // Unreserved characters. 042 + "!$'()*,;&=" // The subdelim characters (excluding '+'). 043 + "@:"; // The gendelim characters permitted in paths. 044 045 /** 046 * Returns an {@link Escaper} instance that escapes strings so they can be safely included in <a 047 * href="https://goo.gl/MplK6I">URL form parameter names and values</a>. Escaping is performed 048 * with the UTF-8 character encoding. The caller is responsible for <a 049 * href="https://goo.gl/9EfkM1">replacing any unpaired carriage return or line feed characters 050 * with a CR+LF pair</a> on any non-file inputs before escaping them with this escaper. 051 * 052 * <p>When escaping a String, the following rules apply: 053 * 054 * <ul> 055 * <li>The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain 056 * the same. 057 * <li>The special characters ".", "-", "*", and "_" remain the same. 058 * <li>The space character " " is converted into a plus sign "+". 059 * <li>All other characters are converted into one or more bytes using UTF-8 encoding and each 060 * byte is then represented by the 3-character string "%XY", where "XY" is the two-digit, 061 * uppercase, hexadecimal representation of the byte value. 062 * </ul> 063 * 064 * <p>This escaper is suitable for escaping parameter names and values even when <a 065 * href="https://goo.gl/utn6M">using the non-standard semicolon</a>, rather than the ampersand, as 066 * a parameter delimiter. Nevertheless, we recommend using the ampersand unless you must 067 * interoperate with systems that require semicolons. 068 * 069 * <p><b>Note:</b> Unlike other escapers, URL escapers produce <a 070 * href="https://url.spec.whatwg.org/#percent-encode">uppercase</a> hexadecimal sequences. 071 * 072 */ 073 public static Escaper urlFormParameterEscaper() { 074 return URL_FORM_PARAMETER_ESCAPER; 075 } 076 077 private static final Escaper URL_FORM_PARAMETER_ESCAPER = 078 new PercentEscaper(URL_FORM_PARAMETER_OTHER_SAFE_CHARS, true); 079 080 /** 081 * Returns an {@link Escaper} instance that escapes strings so they can be safely included in <a 082 * href="https://goo.gl/m2MIf0">URL path segments</a>. The returned escaper escapes all non-ASCII 083 * characters, even though <a href="https://goo.gl/e7E0In">many of these are accepted in modern 084 * URLs</a>. (<a href="https://goo.gl/jfVxXW">If the escaper were to leave these characters 085 * unescaped, they would be escaped by the consumer at parse time, anyway.</a>) Additionally, the 086 * escaper escapes the slash character ("/"). While slashes are acceptable in URL paths, they are 087 * considered by the specification to be separators between "path segments." This implies that, if 088 * you wish for your path to contain slashes, you must escape each segment separately and then 089 * join them. 090 * 091 * <p>When escaping a String, the following rules apply: 092 * 093 * <ul> 094 * <li>The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain 095 * the same. 096 * <li>The unreserved characters ".", "-", "~", and "_" remain the same. 097 * <li>The general delimiters "@" and ":" remain the same. 098 * <li>The subdelimiters "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", and "=" remain 099 * the same. 100 * <li>The space character " " is converted into %20. 101 * <li>All other characters are converted into one or more bytes using UTF-8 encoding and each 102 * byte is then represented by the 3-character string "%XY", where "XY" is the two-digit, 103 * uppercase, hexadecimal representation of the byte value. 104 * </ul> 105 * 106 * <p><b>Note:</b> Unlike other escapers, URL escapers produce <a 107 * href="https://url.spec.whatwg.org/#percent-encode">uppercase</a> hexadecimal sequences. 108 */ 109 public static Escaper urlPathSegmentEscaper() { 110 return URL_PATH_SEGMENT_ESCAPER; 111 } 112 113 private static final Escaper URL_PATH_SEGMENT_ESCAPER = 114 new PercentEscaper(URL_PATH_OTHER_SAFE_CHARS_LACKING_PLUS + "+", false); 115 116 /** 117 * Returns an {@link Escaper} instance that escapes strings so they can be safely included in a <a 118 * href="https://goo.gl/xXEq4p">URL fragment</a>. The returned escaper escapes all non-ASCII 119 * characters, even though <a href="https://goo.gl/e7E0In">many of these are accepted in modern 120 * URLs</a>. 121 * 122 * <p>When escaping a String, the following rules apply: 123 * 124 * <ul> 125 * <li>The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain 126 * the same. 127 * <li>The unreserved characters ".", "-", "~", and "_" remain the same. 128 * <li>The general delimiters "@" and ":" remain the same. 129 * <li>The subdelimiters "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", and "=" remain 130 * the same. 131 * <li>The space character " " is converted into %20. 132 * <li>Fragments allow unescaped "/" and "?", so they remain the same. 133 * <li>All other characters are converted into one or more bytes using UTF-8 encoding and each 134 * byte is then represented by the 3-character string "%XY", where "XY" is the two-digit, 135 * uppercase, hexadecimal representation of the byte value. 136 * </ul> 137 * 138 * <p><b>Note:</b> Unlike other escapers, URL escapers produce <a 139 * href="https://url.spec.whatwg.org/#percent-encode">uppercase</a> hexadecimal sequences. 140 */ 141 public static Escaper urlFragmentEscaper() { 142 return URL_FRAGMENT_ESCAPER; 143 } 144 145 private static final Escaper URL_FRAGMENT_ESCAPER = 146 new PercentEscaper(URL_PATH_OTHER_SAFE_CHARS_LACKING_PLUS + "+/?", false); 147}