瀏覽代碼

Refactored percent escaping to no longer use deprecated method.

Christian Noon 10 年之前
父節點
當前提交
81fd1ce0c4
共有 1 個文件被更改,包括 6 次插入21 次删除
  1. 6 21
      Source/ParameterEncoding.swift

+ 6 - 21
Source/ParameterEncoding.swift

@@ -150,29 +150,13 @@ public enum ParameterEncoding {
     }
 
     /**
-        Returns a percent escaped string following RFC 3986 for query string formatting.
+        Returns a percent escaped string following RFC 3986 for a query string key or value.
 
         RFC 3986 states that the following characters are "reserved" characters.
 
         - General Delimiters: ":", "#", "[", "]", "@", "?", "/"
         - Sub-Delimiters: "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", "="
 
-        Core Foundation interprets RFC 3986 in terms of legal and illegal characters.
-
-        - Legal Numbers: "0123456789"
-        - Legal Letters: "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-        - Legal Characters: "!", "$", "&", "'", "(", ")", "*", "+", ",", "-",
-                            ".", "/", ":", ";", "=", "?", "@", "_", "~", "\""
-        - Illegal Characters: All characters not listed as Legal
-
-        While the Core Foundation `CFURLCreateStringByAddingPercentEscapes` documentation states
-        that it follows RFC 3986, the headers actually point out that it follows RFC 2396. This
-        explains why it does not consider "[", "]" and "#" to be "legal" characters even though 
-        they are specified as "reserved" characters in RFC 3986. The following rdar has been filed
-        to hopefully get the documentation updated.
-
-        - https://openradar.appspot.com/radar?id=5058257274011648
-
         In RFC 3986 - Section 3.4, it states that the "?" and "/" characters should not be escaped to allow
         query strings to include a URL. Therefore, all "reserved" characters with the exception of "?" and "/"
         should be percent escaped in the query string.
@@ -182,11 +166,12 @@ public enum ParameterEncoding {
         - returns: The percent escaped string.
     */
     func escape(string: String) -> String {
-        let generalDelimiters = ":#[]@" // does not include "?" or "/" due to RFC 3986 - Section 3.4
-        let subDelimiters = "!$&'()*+,;="
+        let generalDelimitersToEncode = ":#[]@" // does not include "?" or "/" due to RFC 3986 - Section 3.4
+        let subDelimitersToEncode = "!$&'()*+,;="
 
-        let legalURLCharactersToBeEscaped: CFStringRef = generalDelimiters + subDelimiters
+        let allowedCharacterSet = NSCharacterSet.URLQueryAllowedCharacterSet().mutableCopy() as! NSMutableCharacterSet
+        allowedCharacterSet.removeCharactersInString(generalDelimitersToEncode + subDelimitersToEncode)
 
-        return CFURLCreateStringByAddingPercentEscapes(nil, string, nil, legalURLCharactersToBeEscaped, CFStringBuiltInEncodings.UTF8.rawValue) as String
+        return string.stringByAddingPercentEncodingWithAllowedCharacters(allowedCharacterSet) ?? ""
     }
 }