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://url.spec.whatwg.org/#application-x-www-form-urlencoded-percent-encode-set">URL
048   * form parameter names and values</a>. Escaping is performed with the UTF-8 character encoding.
049   * The caller is responsible for <a
050   * href="https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#multipart-form-data">replacing
051   * any unpaired carriage return or line feed characters with a CR+LF pair</a> on any non-file
052   * inputs before escaping them with this escaper.
053   *
054   * <p>When escaping a String, the following rules apply:
055   *
056   * <ul>
057   *   <li>The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain
058   *       the same.
059   *   <li>The special characters ".", "-", "*", and "_" remain the same.
060   *   <li>The space character " " is converted into a plus sign "+".
061   *   <li>All other characters are converted into one or more bytes using UTF-8 encoding and each
062   *       byte is then represented by the 3-character string "%XY", where "XY" is the two-digit,
063   *       uppercase, hexadecimal representation of the byte value.
064   * </ul>
065   *
066   * <p>This escaper is suitable for escaping parameter names and values even when <a
067   * href="https://www.w3.org/TR/html401/appendix/notes.html#h-B.2.2">using the non-standard
068   * semicolon</a>, rather than the ampersand, as a parameter delimiter. Nevertheless, we recommend
069   * using the ampersand unless you must interoperate with systems that require semicolons.
070   *
071   * <p><b>Note:</b> Unlike other escapers, URL escapers produce <a
072   * href="https://url.spec.whatwg.org/#percent-encode">uppercase</a> hexadecimal sequences.
073   *
074   */
075  public static Escaper urlFormParameterEscaper() {
076    return URL_FORM_PARAMETER_ESCAPER;
077  }
078
079  private static final Escaper URL_FORM_PARAMETER_ESCAPER =
080      new PercentEscaper(URL_FORM_PARAMETER_OTHER_SAFE_CHARS, true);
081
082  /**
083   * Returns an {@link Escaper} instance that escapes strings so they can be safely included in <a
084   * href="https://url.spec.whatwg.org/#syntax-url-path-segment">URL path segments</a>. The returned
085   * escaper escapes all non-ASCII characters, even though <a
086   * href="https://url.spec.whatwg.org/#url-code-points">many of these are accepted in modern
087   * URLs</a>. (<a href="https://url.spec.whatwg.org/#path-state">If the escaper were to leave these
088   * characters unescaped, they would be escaped by the consumer at parse time, anyway.</a>)
089   * Additionally, the escaper escapes the slash character ("/"). While slashes are acceptable in
090   * URL paths, they are considered by the specification to be separators between "path segments."
091   * This implies that, if you wish for your path to contain slashes, you must escape each segment
092   * separately and then join them.
093   *
094   * <p>When escaping a String, the following rules apply:
095   *
096   * <ul>
097   *   <li>The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain
098   *       the same.
099   *   <li>The unreserved characters ".", "-", "~", and "_" remain the same.
100   *   <li>The general delimiters "@" and ":" remain the same.
101   *   <li>The subdelimiters "!", "$", "&amp;", "'", "(", ")", "*", "+", ",", ";", and "=" remain
102   *       the same.
103   *   <li>The space character " " is converted into %20.
104   *   <li>All other characters are converted into one or more bytes using UTF-8 encoding and each
105   *       byte is then represented by the 3-character string "%XY", where "XY" is the two-digit,
106   *       uppercase, hexadecimal representation of the byte value.
107   * </ul>
108   *
109   * <p><b>Note:</b> Unlike other escapers, URL escapers produce <a
110   * href="https://url.spec.whatwg.org/#percent-encode">uppercase</a> hexadecimal sequences.
111   */
112  public static Escaper urlPathSegmentEscaper() {
113    return URL_PATH_SEGMENT_ESCAPER;
114  }
115
116  private static final Escaper URL_PATH_SEGMENT_ESCAPER =
117      new PercentEscaper(URL_PATH_OTHER_SAFE_CHARS_LACKING_PLUS + "+", false);
118
119  /**
120   * Returns an {@link Escaper} instance that escapes strings so they can be safely included in a <a
121   * href="https://url.spec.whatwg.org/#concept-url-fragment">URL fragment</a>. The returned escaper
122   * escapes all non-ASCII characters.
123   *
124   * <p>When escaping a String, the following rules apply:
125   *
126   * <ul>
127   *   <li>The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain
128   *       the same.
129   *   <li>The unreserved characters ".", "-", "~", and "_" remain the same.
130   *   <li>The general delimiters "@" and ":" remain the same.
131   *   <li>The subdelimiters "!", "$", "&amp;", "'", "(", ")", "*", "+", ",", ";", and "=" remain
132   *       the same.
133   *   <li>The space character " " is converted into %20.
134   *   <li>Fragments allow unescaped "/" and "?", so they remain the same.
135   *   <li>All other characters are converted into one or more bytes using UTF-8 encoding and each
136   *       byte is then represented by the 3-character string "%XY", where "XY" is the two-digit,
137   *       uppercase, hexadecimal representation of the byte value.
138   * </ul>
139   *
140   * <p><b>Note:</b> Unlike other escapers, URL escapers produce <a
141   * href="https://url.spec.whatwg.org/#percent-encode">uppercase</a> hexadecimal sequences.
142   */
143  public static Escaper urlFragmentEscaper() {
144    return URL_FRAGMENT_ESCAPER;
145  }
146
147  private static final Escaper URL_FRAGMENT_ESCAPER =
148      new PercentEscaper(URL_PATH_OTHER_SAFE_CHARS_LACKING_PLUS + "+/?", false);
149}