001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.net; 020 021import java.io.UnsupportedEncodingException; 022import java.nio.charset.Charset; 023 024import lucee.commons.lang.StringUtil; 025 026/** 027 * Utility class for HTML form encoding. This class contains static methods 028 * for converting a String to the <CODE>application/x-www-form-urlencoded</CODE> MIME 029 * format. For more information about HTML form encoding, consult the HTML 030 * <A HREF="http://www.w3.org/TR/html4/">specification</A>. 031 * 032 * <p> 033 * When encoding a String, the following rules apply: 034 * 035 * <p> 036 * <ul> 037 * <li>The alphanumeric characters "<code>a</code>" through 038 * "<code>z</code>", "<code>A</code>" through 039 * "<code>Z</code>" and "<code>0</code>" 040 * through "<code>9</code>" remain the same. 041 * <li>The special characters "<code>.</code>", 042 * "<code>-</code>", "<code>*</code>", and 043 * "<code>_</code>" remain the same. 044 * <li>The space character "<code> </code>" is 045 * converted into a plus sign "<code>+</code>". 046 * <li>All other characters are unsafe and are first converted into 047 * one or more bytes using some encoding scheme. Then each byte is 048 * represented by the 3-character string 049 * "<code>%<i>xy</i></code>", where <i>xy</i> is the 050 * two-digit hexadecimal representation of the byte. 051 * The recommended encoding scheme to use is UTF-8. However, 052 * for compatibility reasons, if an encoding is not specified, 053 * then the default encoding of the platform is used. 054 * </ul> 055 * 056 * <p> 057 * For example using UTF-8 as the encoding scheme the string "The 058 * string ü@foo-bar" would get converted to 059 * "The+string+%C3%BC%40foo-bar" because in UTF-8 the character 060 * ü is encoded as two bytes C3 (hex) and BC (hex), and the 061 * character @ is encoded as one byte 40 (hex). 062 * 063 * @author Herb Jellinek 064 * @version 1.25, 12/03/01 065 * @since JDK1.0 066 */ 067public class URLEncoder { 068 069 /** 070 * You can't call the constructor. 071 */ 072 private URLEncoder() { } 073 074 /** 075 * Translates a string into <code>x-www-form-urlencoded</code> 076 * format. This method uses the platform's default encoding 077 * as the encoding scheme to obtain the bytes for unsafe characters. 078 * 079 * @param s <code>String</code> to be translated. 080 * @deprecated The resulting string may vary depending on the platform's 081 * default encoding. Instead, use the encode(String,String) 082 * method to specify the encoding. 083 * @return the translated <code>String</code>. 084 */ 085 public static String encode(String s) { 086 s= java.net.URLEncoder.encode(s); 087 if(s.indexOf('+')!=-1)s=StringUtil.replace(s, "+", "%20",false); 088 return s; 089 } 090 091 /** 092 * @deprecated use instead <code>encode(String s, Charset cs)</code> 093 * @param s 094 * @param enc 095 * @return 096 * @throws UnsupportedEncodingException 097 */ 098 public static String encode(String s, String enc) throws UnsupportedEncodingException { 099 s= java.net.URLEncoder.encode(s, enc); 100 if(s.indexOf('+')!=-1)s=StringUtil.replace(s, "+", "%20",false); 101 return s; 102 } 103 104 public static String encode(String s, Charset cs) throws UnsupportedEncodingException { 105 s= java.net.URLEncoder.encode(s, cs.name()); 106 if(s.indexOf('+')!=-1)s=StringUtil.replace(s, "+", "%20",false); 107 return s; 108 } 109}