Skip to content

Instantly share code, notes, and snippets.

@bardware
Forked from Leigh-/EncodeRFC3986.cfm
Last active July 20, 2018 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bardware/760d98773b5917417cf3762c23286aaf to your computer and use it in GitHub Desktop.
Save bardware/760d98773b5917417cf3762c23286aaf to your computer and use it in GitHub Desktop.
Comparison of EncodeForURL, URLEncodedFormat and EncodeRFC3986 (UDF)
<cfscript>
/**
* URI encoding per RFC 3986, which has treats the following as unreserved: ALPHA / DIGIT / "-" / "." / "_" / "~"
* @text Text to encode
* @returns URI encoded text
*/
public function encodeRFC3986(required string text) {
// Requires CF10+
Local.encoded = encodeForURL(arguments.text);
// Reverse encoding of tilde "~"
Local.encoded = replace( Local.encoded, "%7E", "~", "all" );
// Change space encoding from "+" to "%20"
Local.encoded = replace( Local.encoded, "+", "%20", "all" );
// Asterisk "*" should be encoded
Local.encoded = replace( Local.encoded, "*", "%2A", "all" );
return Local.encoded;
}
/**
* URI encoding per RFC 3986, which has treats the following as unreserved: ALPHA / DIGIT / "-" / "." / "_" / "~"
* @text Text to encode
* @encoding UTF-8 or ISO-8859-1
* @returns URI encoded text
*/
public function encodeRFC3986JAVA(required string text, required string encoding) {
Local.oEncoder = createObject("java", "java.net.URLEncoder");
<!---
Local.oCharset = createObject("java","java.nio.charset.StandardCharsets");
oCharset.UTF_8.toString()
oCharset.ISO_8859_1.toString()
--->
// Requires CF10+
Local.encoded = oEncoder.encode( arguments.text, encoding );
// Reverse encoding of tilde "~"
Local.encoded = replace( Local.encoded, "%7E", "~", "all" );
// Change space encoding from "+" to "%20"
Local.encoded = replace( Local.encoded, "+", "%20", "all" );
// Asterisk "*" should be encoded
Local.encoded = replace( Local.encoded, "*", "%2A", "all" );
return Local.encoded;
}
// Non-reserved characters
writeOutput("<hr><strong>Non-reserved characters (excluding letters and numbers):</strong><br>");
variables.nonReservedChars = ["-","_",".","~"," "];
for (variables.original in variables.nonReservedChars) {
writeOutput("<br> Original #variables.original# || encodeForURL = [ #encodeForURL( variables.original )# ]");
writeOutput(" urlEncodedFormat = [ #urlEncodedFormat( variables.original )# ]");
writeOutput(" encodeRFC3986 = [ #encodeRFC3986( variables.original )# ]");
writeOutput(" encodeRFC3986JAVA = [ #encodeRFC3986JAVA( variables.original, "UTF-8" )# ]");
writeOutput(" encodeRFC3986JAVA = [ #encodeRFC3986JAVA( variables.original, "ISO-8859-1" )# ]");
}
// Reserved characters
writeOutput("<hr><strong>Reserved characters:</strong><br>");
variables.reservedChars = listToArray(": / ? ## [ ] @ ! $ & ' ( ) * + , ; = ", " ");
for (variables.original in variables.reservedChars) {
writeOutput("<br> Original #variables.original# || encodeForURL = [ #encodeForURL( variables.original )# ]");
writeOutput(" urlEncodedFormat = [ #urlEncodedFormat( variables.original )# ]");
writeOutput(" encodeRFC3986 = [ #encodeRFC3986( variables.original )# ]");
writeOutput(" encodeRFC3986JAVA = [ #encodeRFC3986JAVA( variables.original, "UTF-8" )# ]");
writeOutput(" encodeRFC3986JAVA = [ #encodeRFC3986JAVA( variables.original, "ISO-8859-1" )# ]");
}
// Unicode
variables.unicodeText = ["An preost wes on leoden, Laȝamon was ihoten"
, "He wes Leovenaðes sone -- liðe him be Drihten."
, "He wonede at Ernleȝe at æðelen are chirechen,"
, "Uppen Sevarne staþe, sel þar him þuhte,"
, "Onfest Radestone, þer he bock radde."
];
for (variables.line in variables.unicodeText) {
writeOutput("<hr> <strong>#variables.line#</strong>");
writeOutput("<br><br> 1. [ #encodeForURL( variables.line )# ]");
writeOutput("<br> 2. [ #urlEncodedFormat( variables.line )# ]");
writeOutput("<br> 3. [ #encodeRFC3986( variables.line )# ]");
writeOutput("<br> 4. [ #encodeRFC3986JAVA( variables.line, "UTF-8" )# ]");
writeOutput("<br> 4. [ #encodeRFC3986JAVA( variables.line, "ISO-8859-1" )# ]");
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment