ParameterEncoding.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 { URLEncoding() }
  107. /// Returns a `URLEncoding` instance with a `.queryString` destination.
  108. public static var queryString: URLEncoding { URLEncoding(destination: .queryString) }
  109. /// Returns a `URLEncoding` instance with an `.httpBody` destination.
  110. public static var httpBody: URLEncoding { 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. {
  129. self.destination = destination
  130. self.arrayEncoding = arrayEncoding
  131. self.boolEncoding = boolEncoding
  132. }
  133. // MARK: Encoding
  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 = urlRequest.method, destination.encodesParametersInURL(for: 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.headers["Content-Type"] == nil {
  148. urlRequest.headers.update(.contentType("application/x-www-form-urlencoded; charset=utf-8"))
  149. }
  150. urlRequest.httpBody = Data(query(parameters).utf8)
  151. }
  152. return urlRequest
  153. }
  154. /// Creates a percent-escaped, URL encoded query string components from the given key-value pair recursively.
  155. ///
  156. /// - Parameters:
  157. /// - key: Key of the query component.
  158. /// - value: Value of the query component.
  159. ///
  160. /// - Returns: The percent-escaped, URL encoded query string components.
  161. public func queryComponents(fromKey key: String, value: Any) -> [(String, String)] {
  162. var components: [(String, String)] = []
  163. switch value {
  164. case let dictionary as [String: Any]:
  165. for (nestedKey, value) in dictionary {
  166. components += queryComponents(fromKey: "\(key)[\(nestedKey)]", value: value)
  167. }
  168. case let array as [Any]:
  169. for value in array {
  170. components += queryComponents(fromKey: arrayEncoding.encode(key: key), value: value)
  171. }
  172. case let number as NSNumber:
  173. if number.isBool {
  174. components.append((escape(key), escape(boolEncoding.encode(value: number.boolValue))))
  175. } else {
  176. components.append((escape(key), escape("\(number)")))
  177. }
  178. case let bool as Bool:
  179. components.append((escape(key), escape(boolEncoding.encode(value: bool))))
  180. default:
  181. components.append((escape(key), escape("\(value)")))
  182. }
  183. return components
  184. }
  185. /// Creates a percent-escaped string following RFC 3986 for a query string key or value.
  186. ///
  187. /// - Parameter string: `String` to be percent-escaped.
  188. ///
  189. /// - Returns: The percent-escaped `String`.
  190. public func escape(_ string: String) -> String {
  191. string.addingPercentEncoding(withAllowedCharacters: .afURLQueryAllowed) ?? string
  192. }
  193. private func query(_ parameters: [String: Any]) -> String {
  194. var components: [(String, String)] = []
  195. for key in parameters.keys.sorted(by: <) {
  196. let value = parameters[key]!
  197. components += queryComponents(fromKey: key, value: value)
  198. }
  199. return components.map { "\($0)=\($1)" }.joined(separator: "&")
  200. }
  201. }
  202. // MARK: -
  203. /// Uses `JSONSerialization` to create a JSON representation of the parameters object, which is set as the body of the
  204. /// request. The `Content-Type` HTTP header field of an encoded request is set to `application/json`.
  205. public struct JSONEncoding: ParameterEncoding {
  206. // MARK: Properties
  207. /// Returns a `JSONEncoding` instance with default writing options.
  208. public static var `default`: JSONEncoding { JSONEncoding() }
  209. /// Returns a `JSONEncoding` instance with `.prettyPrinted` writing options.
  210. public static var prettyPrinted: JSONEncoding { JSONEncoding(options: .prettyPrinted) }
  211. /// The options for writing the parameters as JSON data.
  212. public let options: JSONSerialization.WritingOptions
  213. // MARK: Initialization
  214. /// Creates an instance using the specified `WritingOptions`.
  215. ///
  216. /// - Parameter options: `JSONSerialization.WritingOptions` to use.
  217. public init(options: JSONSerialization.WritingOptions = []) {
  218. self.options = options
  219. }
  220. // MARK: Encoding
  221. public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
  222. var urlRequest = try urlRequest.asURLRequest()
  223. guard let parameters = parameters else { return urlRequest }
  224. do {
  225. let data = try JSONSerialization.data(withJSONObject: parameters, options: options)
  226. if urlRequest.headers["Content-Type"] == nil {
  227. urlRequest.headers.update(.contentType("application/json"))
  228. }
  229. urlRequest.httpBody = data
  230. } catch {
  231. throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
  232. }
  233. return urlRequest
  234. }
  235. /// Encodes any JSON compatible object into a `URLRequest`.
  236. ///
  237. /// - Parameters:
  238. /// - urlRequest: `URLRequestConvertible` value into which the object will be encoded.
  239. /// - jsonObject: `Any` value (must be JSON compatible` to be encoded into the `URLRequest`. `nil` by default.
  240. ///
  241. /// - Returns: The encoded `URLRequest`.
  242. /// - Throws: Any `Error` produced during encoding.
  243. public func encode(_ urlRequest: URLRequestConvertible, withJSONObject jsonObject: Any? = nil) throws -> URLRequest {
  244. var urlRequest = try urlRequest.asURLRequest()
  245. guard let jsonObject = jsonObject else { return urlRequest }
  246. do {
  247. let data = try JSONSerialization.data(withJSONObject: jsonObject, options: options)
  248. if urlRequest.headers["Content-Type"] == nil {
  249. urlRequest.headers.update(.contentType("application/json"))
  250. }
  251. urlRequest.httpBody = data
  252. } catch {
  253. throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
  254. }
  255. return urlRequest
  256. }
  257. }
  258. // MARK: -
  259. private extension NSNumber {
  260. var isBool: Bool {
  261. // Use Obj-C type encoding to check whether the underlying type is a `Bool`, as it's guaranteed as part of
  262. // swift-corelibs-foundation, per [this discussion on the Swift forums](https://forums.swift.org/t/alamofire-on-linux-possible-but-not-release-ready/34553/22).
  263. String(cString: objCType) == "c"
  264. }
  265. }