ParameterEncoding.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 URL request by encoding parameters and applying them onto an existing request.
  30. ///
  31. /// - parameter urlRequest: The request to have parameters applied.
  32. /// - parameter parameters: The parameters to apply.
  33. ///
  34. /// - throws: An `AFError.parameterEncodingFailed` error if encoding fails.
  35. ///
  36. /// - returns: The encoded request.
  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. ///
  59. /// - methodDependent: Applies encoded query string result to existing query string for `GET`, `HEAD` and `DELETE`
  60. /// requests and sets as the HTTP body for requests with any other HTTP method.
  61. /// - queryString: Sets or appends encoded query string result to existing query string.
  62. /// - httpBody: Sets encoded query string result as the HTTP body of the URL request.
  63. public enum Destination {
  64. case methodDependent, queryString, httpBody
  65. }
  66. /// Configures how `Array` parameters are encoded.
  67. ///
  68. /// - brackets: An empty set of square brackets is appended to the key for every value.
  69. /// This is the default behavior.
  70. /// - noBrackets: No brackets are appended. The key is encoded as is.
  71. public enum ArrayEncoding {
  72. case brackets, noBrackets
  73. func encode(key: String) -> String {
  74. switch self {
  75. case .brackets:
  76. return "\(key)[]"
  77. case .noBrackets:
  78. return key
  79. }
  80. }
  81. }
  82. /// Configures how `Bool` parameters are encoded.
  83. ///
  84. /// - numeric: Encode `true` as `1` and `false` as `0`. This is the default behavior.
  85. /// - literal: Encode `true` and `false` as string literals.
  86. public enum BoolEncoding {
  87. case numeric, literal
  88. func encode(value: Bool) -> String {
  89. switch self {
  90. case .numeric:
  91. return value ? "1" : "0"
  92. case .literal:
  93. return value ? "true" : "false"
  94. }
  95. }
  96. }
  97. // MARK: Properties
  98. /// Returns a default `URLEncoding` instance.
  99. public static var `default`: URLEncoding { return URLEncoding() }
  100. /// Returns a `URLEncoding` instance with a `.methodDependent` destination.
  101. public static var methodDependent: URLEncoding { return URLEncoding() }
  102. /// Returns a `URLEncoding` instance with a `.queryString` destination.
  103. public static var queryString: URLEncoding { return URLEncoding(destination: .queryString) }
  104. /// Returns a `URLEncoding` instance with an `.httpBody` destination.
  105. public static var httpBody: URLEncoding { return URLEncoding(destination: .httpBody) }
  106. /// The destination defining where the encoded query string is to be applied to the URL request.
  107. public let destination: Destination
  108. /// The encoding to use for `Array` parameters.
  109. public let arrayEncoding: ArrayEncoding
  110. /// The encoding to use for `Bool` parameters.
  111. public let boolEncoding: BoolEncoding
  112. // MARK: Initialization
  113. /// Creates a `URLEncoding` instance using the specified destination.
  114. ///
  115. /// - parameter destination: The destination defining where the encoded query string is to be applied.
  116. /// - parameter arrayEncoding: The encoding to use for `Array` parameters.
  117. /// - parameter boolEncoding: The encoding to use for `Bool` parameters.
  118. ///
  119. /// - returns: The new `URLEncoding` instance.
  120. public init(destination: Destination = .methodDependent, arrayEncoding: ArrayEncoding = .brackets, boolEncoding: BoolEncoding = .numeric) {
  121. self.destination = destination
  122. self.arrayEncoding = arrayEncoding
  123. self.boolEncoding = boolEncoding
  124. }
  125. // MARK: Encoding
  126. /// Creates a URL request by encoding parameters and applying them onto an existing request.
  127. ///
  128. /// - parameter urlRequest: The request to have parameters applied.
  129. /// - parameter parameters: The parameters to apply.
  130. ///
  131. /// - throws: An `Error` if the encoding process encounters an error.
  132. ///
  133. /// - returns: The encoded request.
  134. public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
  135. var urlRequest = try urlRequest.asURLRequest()
  136. guard let parameters = parameters else { return urlRequest }
  137. if let method = HTTPMethod(rawValue: urlRequest.httpMethod ?? "GET"), encodesParametersInURL(with: method) {
  138. guard let url = urlRequest.url else {
  139. throw AFError.parameterEncodingFailed(reason: .missingURL)
  140. }
  141. if var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false), !parameters.isEmpty {
  142. let percentEncodedQuery = (urlComponents.percentEncodedQuery.map { $0 + "&" } ?? "") + query(parameters)
  143. urlComponents.percentEncodedQuery = percentEncodedQuery
  144. urlRequest.url = urlComponents.url
  145. }
  146. } else {
  147. if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
  148. urlRequest.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
  149. }
  150. urlRequest.httpBody = Data(query(parameters).utf8)
  151. }
  152. return urlRequest
  153. }
  154. /// Creates percent-escaped, URL encoded query string components from the given key-value pair using recursion.
  155. ///
  156. /// - parameter key: The key of the query component.
  157. /// - parameter value: The 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. /// Returns a percent-escaped string following RFC 3986 for a query string key or value.
  184. ///
  185. /// - parameter string: The 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. private func encodesParametersInURL(with method: HTTPMethod) -> Bool {
  200. switch destination {
  201. case .queryString:
  202. return true
  203. case .httpBody:
  204. return false
  205. default:
  206. break
  207. }
  208. switch method {
  209. case .get, .head, .delete:
  210. return true
  211. default:
  212. return false
  213. }
  214. }
  215. }
  216. // MARK: -
  217. /// Uses `JSONSerialization` to create a JSON representation of the parameters object, which is set as the body of the
  218. /// request. The `Content-Type` HTTP header field of an encoded request is set to `application/json`.
  219. public struct JSONEncoding: ParameterEncoding {
  220. // MARK: Properties
  221. /// Returns a `JSONEncoding` instance with default writing options.
  222. public static var `default`: JSONEncoding { return JSONEncoding() }
  223. /// Returns a `JSONEncoding` instance with `.prettyPrinted` writing options.
  224. public static var prettyPrinted: JSONEncoding { return JSONEncoding(options: .prettyPrinted) }
  225. /// The options for writing the parameters as JSON data.
  226. public let options: JSONSerialization.WritingOptions
  227. // MARK: Initialization
  228. /// Creates a `JSONEncoding` instance using the specified options.
  229. ///
  230. /// - parameter options: The options for writing the parameters as JSON data.
  231. ///
  232. /// - returns: The new `JSONEncoding` instance.
  233. public init(options: JSONSerialization.WritingOptions = []) {
  234. self.options = options
  235. }
  236. // MARK: Encoding
  237. /// Creates a URL request by encoding parameters and applying them onto an existing request.
  238. ///
  239. /// - parameter urlRequest: The request to have parameters applied.
  240. /// - parameter parameters: The parameters to apply.
  241. ///
  242. /// - throws: An `Error` if the encoding process encounters an error.
  243. ///
  244. /// - returns: The encoded request.
  245. public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
  246. var urlRequest = try urlRequest.asURLRequest()
  247. guard let parameters = parameters else { return urlRequest }
  248. do {
  249. let data = try JSONSerialization.data(withJSONObject: parameters, options: options)
  250. if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
  251. urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
  252. }
  253. urlRequest.httpBody = data
  254. } catch {
  255. throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
  256. }
  257. return urlRequest
  258. }
  259. /// Creates a URL request by encoding the JSON object and setting the resulting data on the HTTP body.
  260. ///
  261. /// - parameter urlRequest: The request to apply the JSON object to.
  262. /// - parameter jsonObject: The JSON object to apply to the request.
  263. ///
  264. /// - throws: An `Error` if the encoding process encounters an error.
  265. ///
  266. /// - returns: The encoded request.
  267. public func encode(_ urlRequest: URLRequestConvertible, withJSONObject jsonObject: Any? = nil) throws -> URLRequest {
  268. var urlRequest = try urlRequest.asURLRequest()
  269. guard let jsonObject = jsonObject else { return urlRequest }
  270. do {
  271. let data = try JSONSerialization.data(withJSONObject: jsonObject, options: options)
  272. if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
  273. urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
  274. }
  275. urlRequest.httpBody = data
  276. } catch {
  277. throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
  278. }
  279. return urlRequest
  280. }
  281. }
  282. // MARK: -
  283. extension NSNumber {
  284. fileprivate var isBool: Bool { return CFBooleanGetTypeID() == CFGetTypeID(self) }
  285. }