Created
April 18, 2016 05:57
-
-
Save Leigh-/0ff1eba1e9cb82d4bf0917ca286679d9 to your computer and use it in GitHub Desktop.
Comparison of EncodeForURL, URLEncodedFormat and EncodeRFC3986 (UDF)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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; | |
} | |
// 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 )# ]"); | |
} | |
// 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 )# ]"); | |
} | |
// 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 )# ]"); | |
} | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment