ParameterEncoding.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //
  2. // ParameterEncoding.swift
  3. //
  4. // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Foundation
  25. /// A dictionary of parameters to apply to a `URLRequest`.
  26. public typealias Parameters = [String: Any]
  27. /// A type used to define how a set of parameters are applied to a `URLRequest`.
  28. public protocol ParameterEncoding {
  29. /// Creates a `URLRequest` by encoding parameters and applying them on the passed request.
  30. ///
  31. /// - Parameters:
  32. /// - urlRequest: `URLRequestConvertible` value onto which parameters will be encoded.
  33. /// - parameters: `Parameters` to encode onto the request.
  34. ///
  35. /// - Returns: The encoded `URLRequest`.
  36. /// - Throws: Any `Error` produced during parameter encoding.
  37. func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest
  38. }
  39. // MARK: -
  40. /// Creates a url-encoded query string to be set as or appended to any existing URL query string or set as the HTTP
  41. /// body of the URL request. Whether the query string is set or appended to any existing URL query string or set as
  42. /// the HTTP body depends on the destination of the encoding.
  43. ///
  44. /// The `Content-Type` HTTP header field of an encoded request with HTTP body is set to
  45. /// `application/x-www-form-urlencoded; charset=utf-8`.
  46. ///
  47. /// There is no published specification for how to encode collection types. By default the convention of appending
  48. /// `[]` to the key for array values (`foo[]=1&foo[]=2`), and appending the key surrounded by square brackets for
  49. /// nested dictionary values (`foo[bar]=baz`) is used. Optionally, `ArrayEncoding` can be used to omit the
  50. /// square brackets appended to array keys.
  51. ///
  52. /// `BoolEncoding` can be used to configure how boolean values are encoded. The default behavior is to encode
  53. /// `true` as 1 and `false` as 0.
  54. public struct URLEncoding: ParameterEncoding {
  55. // MARK: Helper Types
  56. /// Defines whether the url-encoded query string is applied to the existing query string or HTTP body of the
  57. /// resulting URL request.
  58. public enum Destination {
  59. /// Applies encoded query string result to existing query string for `GET`, `HEAD` and `DELETE` requests and
  60. /// sets as the HTTP body for requests with any other HTTP method.
  61. case methodDependent
  62. /// Sets or appends encoded query string result to existing query string.
  63. case queryString
  64. /// Sets encoded query string result as the HTTP body of the URL request.
  65. case httpBody
  66. func encodesParametersInURL(for method: HTTPMethod) -> Bool {
  67. switch self {
  68. case .methodDependent: return [.get, .head, .delete].contains(method)
  69. case .queryString: return true
  70. case .httpBody: return false
  71. }
  72. }
  73. }
  74. /// Configures how `Array` parameters are encoded.
  75. public enum ArrayEncoding {
  76. /// An empty set of square brackets is appended to the key for every value. This is the default behavior.
  77. case brackets
  78. /// No brackets are appended. The key is encoded as is.
  79. case noBrackets
  80. func encode(key: String) -> String {
  81. switch self {
  82. case .brackets:
  83. return "\(key)[]"
  84. case .noBrackets:
  85. return key
  86. }
  87. }
  88. }
  89. /// Configures how `Bool` parameters are encoded.
  90. public enum BoolEncoding {
  91. /// Encode `true` as `1` and `false` as `0`. This is the default behavior.
  92. case numeric
  93. /// Encode `true` and `false` as string literals.
  94. case literal
  95. func encode(value: Bool) -> String {
  96. switch self {
  97. case .numeric:
  98. return value ? "1" : "0"
  99. case .literal:
  100. return value ? "true" : "false"
  101. }
  102. }
  103. }
  104. // MARK: Properties
  105. /// Returns a default `URLEncoding` instance with a `.methodDependent` destination.
  106. public static var `default`: URLEncoding { return URLEncoding() }
  107. /// Returns a `URLEncoding` instance with a `.queryString` destination.
  108. public static var queryString: URLEncoding { return URLEncoding(destination: .queryString) }
  109. /// Returns a `URLEncoding` instance with an `.httpBody` destination.
  110. public static var httpBody: URLEncoding { return URLEncoding(destination: .httpBody) }
  111. /// The destination defining where the encoded query string is to be applied to the URL request.
  112. public let destination: Destination
  113. /// The encoding to use for `Array` parameters.
  114. public let arrayEncoding: ArrayEncoding
  115. /// The encoding to use for `Bool` parameters.
  116. public let boolEncoding: BoolEncoding
  117. // MARK: Initialization
  118. /// Creates an instance using the specified parameters.
  119. ///
  120. /// - Parameters:
  121. /// - destination: `Destination` defining where the encoded query string will be applied. `.methodDependent` by
  122. /// default.
  123. /// - arrayEncoding: `ArrayEncoding` to use. `.brackets` by default.
  124. /// - boolEncoding: `BoolEncoding` to use. `.numeric` by default.
  125. public init(destination: Destination = .methodDependent,
  126. arrayEncoding: ArrayEncoding = .brackets,
  127. boolEncoding: BoolEncoding = .numeric) {
  128. self.destination = destination
  129. self.arrayEncoding = arrayEncoding
  130. self.boolEncoding = boolEncoding
  131. }
  132. // MARK: Encoding
  133. public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
  134. var urlRequest = try urlRequest.asURLRequest()
  135. guard let parameters = parameters else { return urlRequest }
  136. if let method = urlRequest.method, destination.encodesParametersInURL(for: method) {
  137. guard let url = urlRequest.url else {
  138. throw AFError.parameterEncodingFailed(reason: .missingURL)
  139. }
  140. if var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false), !parameters.isEmpty {
  141. let percentEncodedQuery = (urlComponents.percentEncodedQuery.map { $0 + "&" } ?? "") + query(parameters)
  142. urlComponents.percentEncodedQuery = percentEncodedQuery
  143. urlRequest.url = urlComponents.url
  144. }
  145. } else {
  146. if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
  147. urlRequest.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
  148. }
  149. urlRequest.httpBody = Data(query(parameters).utf8)
  150. }
  151. return urlRequest
  152. }
  153. /// Creates a percent-escaped, URL encoded query string components from the given key-value pair recursively.
  154. ///
  155. /// - Parameters:
  156. /// - key: Key of the query component.
  157. /// - value: Value of the query component.
  158. ///
  159. /// - Returns: The percent-escaped, URL encoded query string components.
  160. public func queryComponents(fromKey key: String, value: Any) -> [(String, String)] {
  161. var components: [(String, String)] = []
  162. if let dictionary = value as? [String: Any] {
  163. for (nestedKey, value) in dictionary {
  164. components += queryComponents(fromKey: "\(key)[\(nestedKey)]", value: value)
  165. }
  166. } else if let array = value as? [Any] {
  167. for value in array {
  168. components += queryComponents(fromKey: arrayEncoding.encode(key: key), value: value)
  169. }
  170. } else if let value = value as? NSNumber {
  171. if value.isBool {
  172. components.append((escape(key), escape(boolEncoding.encode(value: value.boolValue))))
  173. } else {
  174. components.append((escape(key), escape("\(value)")))
  175. }
  176. } else if let bool = value as? Bool {
  177. components.append((escape(key), escape(boolEncoding.encode(value: bool))))
  178. } else {
  179. components.append((escape(key), escape("\(value)")))
  180. }
  181. return components
  182. }
  183. /// Creates a percent-escaped string following RFC 3986 for a query string key or value.
  184. ///
  185. /// - Parameter string: `String` to be percent-escaped.
  186. ///
  187. /// - Returns: The percent-escaped `String`.
  188. public func escape(_ string: String) -> String {
  189. return string.addingPercentEncoding(withAllowedCharacters: .afURLQueryAllowed) ?? string
  190. }
  191. private func query(_ parameters: [String: Any]) -> String {
  192. var components: [(String, String)] = []
  193. for key in parameters.keys.sorted(by: <) {
  194. let value = parameters[key]!
  195. components += queryComponents(fromKey: key, value: value)
  196. }
  197. return components.map { "\($0)=\($1)" }.joined(separator: "&")
  198. }
  199. }
  200. // MARK: -
  201. /// Uses `JSONSerialization` to create a JSON representation of the parameters object, which is set as the body of the
  202. /// request. The `Content-Type` HTTP header field of an encoded request is set to `application/json`.
  203. public struct JSONEncoding: ParameterEncoding {
  204. // MARK: Properties
  205. /// Returns a `JSONEncoding` instance with default writing options.
  206. public static var `default`: JSONEncoding { return JSONEncoding() }
  207. /// Returns a `JSONEncoding` instance with `.prettyPrinted` writing options.
  208. public static var prettyPrinted: JSONEncoding { return JSONEncoding(options: .prettyPrinted) }
  209. /// The options for writing the parameters as JSON data.
  210. public let options: JSONSerialization.WritingOptions
  211. // MARK: Initialization
  212. /// Creates an instance using the specified `WritingOptions`.
  213. ///
  214. /// - Parameter options: `JSONSerialization.WritingOptions` to use.
  215. public init(options: JSONSerialization.WritingOptions = []) {
  216. self.options = options
  217. }
  218. // MARK: Encoding
  219. public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
  220. var urlRequest = try urlRequest.asURLRequest()
  221. guard let parameters = parameters else { return urlRequest }
  222. do {
  223. let data = try JSONSerialization.data(withJSONObject: parameters, options: options)
  224. if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
  225. urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
  226. }
  227. urlRequest.httpBody = data
  228. } catch {
  229. throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
  230. }
  231. return urlRequest
  232. }
  233. /// Encodes any JSON compatible object into a `URLRequest`.
  234. ///
  235. /// - Parameters:
  236. /// - urlRequest: `URLRequestConvertible` value into which the object will be encoded.
  237. /// - jsonObject: `Any` value (must be JSON compatible` to be encoded into the `URLRequest`. `nil` by default.
  238. ///
  239. /// - Returns: The encoded `URLRequest`.
  240. /// - Throws: Any `Error` produced during encoding.
  241. public func encode(_ urlRequest: URLRequestConvertible, withJSONObject jsonObject: Any? = nil) throws -> URLRequest {
  242. var urlRequest = try urlRequest.asURLRequest()
  243. guard let jsonObject = jsonObject else { return urlRequest }
  244. do {
  245. let data = try JSONSerialization.data(withJSONObject: jsonObject, options: options)
  246. if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
  247. urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
  248. }
  249. urlRequest.httpBody = data
  250. } catch {
  251. throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
  252. }
  253. return urlRequest
  254. }
  255. }
  256. // MARK: -
  257. extension NSNumber {
  258. fileprivate var isBool: Bool { return CFBooleanGetTypeID() == CFGetTypeID(self) }
  259. }