123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- //
- // ParameterEncoding.swift
- //
- // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- import Foundation
- /// A dictionary of parameters to apply to a `URLRequest`.
- public typealias Parameters = [String: Any]
- /// A type used to define how a set of parameters are applied to a `URLRequest`.
- public protocol ParameterEncoding {
- /// Creates a `URLRequest` by encoding parameters and applying them on the passed request.
- ///
- /// - Parameters:
- /// - urlRequest: `URLRequestConvertible` value onto which parameters will be encoded.
- /// - parameters: `Parameters` to encode onto the request.
- ///
- /// - Returns: The encoded `URLRequest`.
- /// - Throws: Any `Error` produced during parameter encoding.
- func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest
- }
- // MARK: -
- /// Creates a url-encoded query string to be set as or appended to any existing URL query string or set as the HTTP
- /// body of the URL request. Whether the query string is set or appended to any existing URL query string or set as
- /// the HTTP body depends on the destination of the encoding.
- ///
- /// The `Content-Type` HTTP header field of an encoded request with HTTP body is set to
- /// `application/x-www-form-urlencoded; charset=utf-8`.
- ///
- /// There is no published specification for how to encode collection types. By default the convention of appending
- /// `[]` to the key for array values (`foo[]=1&foo[]=2`), and appending the key surrounded by square brackets for
- /// nested dictionary values (`foo[bar]=baz`) is used. Optionally, `ArrayEncoding` can be used to omit the
- /// square brackets appended to array keys.
- ///
- /// `BoolEncoding` can be used to configure how boolean values are encoded. The default behavior is to encode
- /// `true` as 1 and `false` as 0.
- public struct URLEncoding: ParameterEncoding {
- // MARK: Helper Types
- /// Defines whether the url-encoded query string is applied to the existing query string or HTTP body of the
- /// resulting URL request.
- public enum Destination {
- /// Applies encoded query string result to existing query string for `GET`, `HEAD` and `DELETE` requests and
- /// sets as the HTTP body for requests with any other HTTP method.
- case methodDependent
- /// Sets or appends encoded query string result to existing query string.
- case queryString
- /// Sets encoded query string result as the HTTP body of the URL request.
- case httpBody
- func encodesParametersInURL(for method: HTTPMethod) -> Bool {
- switch self {
- case .methodDependent: return [.get, .head, .delete].contains(method)
- case .queryString: return true
- case .httpBody: return false
- }
- }
- }
- /// Configures how `Array` parameters are encoded.
- public enum ArrayEncoding {
- /// An empty set of square brackets is appended to the key for every value. This is the default behavior.
- case brackets
- /// No brackets are appended. The key is encoded as is.
- case noBrackets
- /// Brackets containing the item index are appended. This matches the jQuery and Node.js behavior.
- case indexInBrackets
- /// Provide a custom array key encoding with the given closure.
- case custom((_ key: String, _ index: Int) -> String)
- func encode(key: String, atIndex index: Int) -> String {
- switch self {
- case .brackets:
- return "\(key)[]"
- case .noBrackets:
- return key
- case .indexInBrackets:
- return "\(key)[\(index)]"
- case let .custom(encoding):
- return encoding(key, index)
- }
- }
- }
- /// Configures how `Bool` parameters are encoded.
- public enum BoolEncoding {
- /// Encode `true` as `1` and `false` as `0`. This is the default behavior.
- case numeric
- /// Encode `true` and `false` as string literals.
- case literal
- func encode(value: Bool) -> String {
- switch self {
- case .numeric:
- return value ? "1" : "0"
- case .literal:
- return value ? "true" : "false"
- }
- }
- }
- // MARK: Properties
- /// Returns a default `URLEncoding` instance with a `.methodDependent` destination.
- public static var `default`: URLEncoding { URLEncoding() }
- /// Returns a `URLEncoding` instance with a `.queryString` destination.
- public static var queryString: URLEncoding { URLEncoding(destination: .queryString) }
- /// Returns a `URLEncoding` instance with an `.httpBody` destination.
- public static var httpBody: URLEncoding { URLEncoding(destination: .httpBody) }
- /// The destination defining where the encoded query string is to be applied to the URL request.
- public let destination: Destination
- /// The encoding to use for `Array` parameters.
- public let arrayEncoding: ArrayEncoding
- /// The encoding to use for `Bool` parameters.
- public let boolEncoding: BoolEncoding
- // MARK: Initialization
- /// Creates an instance using the specified parameters.
- ///
- /// - Parameters:
- /// - destination: `Destination` defining where the encoded query string will be applied. `.methodDependent` by
- /// default.
- /// - arrayEncoding: `ArrayEncoding` to use. `.brackets` by default.
- /// - boolEncoding: `BoolEncoding` to use. `.numeric` by default.
- public init(destination: Destination = .methodDependent,
- arrayEncoding: ArrayEncoding = .brackets,
- boolEncoding: BoolEncoding = .numeric) {
- self.destination = destination
- self.arrayEncoding = arrayEncoding
- self.boolEncoding = boolEncoding
- }
- // MARK: Encoding
- public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
- var urlRequest = try urlRequest.asURLRequest()
- guard let parameters = parameters else { return urlRequest }
- if let method = urlRequest.method, destination.encodesParametersInURL(for: method) {
- guard let url = urlRequest.url else {
- throw AFError.parameterEncodingFailed(reason: .missingURL)
- }
- if var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false), !parameters.isEmpty {
- let percentEncodedQuery = (urlComponents.percentEncodedQuery.map { $0 + "&" } ?? "") + query(parameters)
- urlComponents.percentEncodedQuery = percentEncodedQuery
- urlRequest.url = urlComponents.url
- }
- } else {
- if urlRequest.headers["Content-Type"] == nil {
- urlRequest.headers.update(.contentType("application/x-www-form-urlencoded; charset=utf-8"))
- }
- urlRequest.httpBody = Data(query(parameters).utf8)
- }
- return urlRequest
- }
- /// Creates a percent-escaped, URL encoded query string components from the given key-value pair recursively.
- ///
- /// - Parameters:
- /// - key: Key of the query component.
- /// - value: Value of the query component.
- ///
- /// - Returns: The percent-escaped, URL encoded query string components.
- public func queryComponents(fromKey key: String, value: Any) -> [(String, String)] {
- var components: [(String, String)] = []
- switch value {
- case let dictionary as [String: Any]:
- for (nestedKey, value) in dictionary {
- components += queryComponents(fromKey: "\(key)[\(nestedKey)]", value: value)
- }
- case let array as [Any]:
- for (index, value) in array.enumerated() {
- components += queryComponents(fromKey: arrayEncoding.encode(key: key, atIndex: index), value: value)
- }
- case let number as NSNumber:
- if number.isBool {
- components.append((escape(key), escape(boolEncoding.encode(value: number.boolValue))))
- } else {
- components.append((escape(key), escape("\(number)")))
- }
- case let bool as Bool:
- components.append((escape(key), escape(boolEncoding.encode(value: bool))))
- default:
- components.append((escape(key), escape("\(value)")))
- }
- return components
- }
- /// Creates a percent-escaped string following RFC 3986 for a query string key or value.
- ///
- /// - Parameter string: `String` to be percent-escaped.
- ///
- /// - Returns: The percent-escaped `String`.
- public func escape(_ string: String) -> String {
- string.addingPercentEncoding(withAllowedCharacters: .afURLQueryAllowed) ?? string
- }
- private func query(_ parameters: [String: Any]) -> String {
- var components: [(String, String)] = []
- for key in parameters.keys.sorted(by: <) {
- let value = parameters[key]!
- components += queryComponents(fromKey: key, value: value)
- }
- return components.map { "\($0)=\($1)" }.joined(separator: "&")
- }
- }
- // MARK: -
- /// Uses `JSONSerialization` to create a JSON representation of the parameters object, which is set as the body of the
- /// request. The `Content-Type` HTTP header field of an encoded request is set to `application/json`.
- public struct JSONEncoding: ParameterEncoding {
- public enum Error: Swift.Error {
- case invalidJSONObject
- }
- // MARK: Properties
- /// Returns a `JSONEncoding` instance with default writing options.
- public static var `default`: JSONEncoding { JSONEncoding() }
- /// Returns a `JSONEncoding` instance with `.prettyPrinted` writing options.
- public static var prettyPrinted: JSONEncoding { JSONEncoding(options: .prettyPrinted) }
- /// The options for writing the parameters as JSON data.
- public let options: JSONSerialization.WritingOptions
- // MARK: Initialization
- /// Creates an instance using the specified `WritingOptions`.
- ///
- /// - Parameter options: `JSONSerialization.WritingOptions` to use.
- public init(options: JSONSerialization.WritingOptions = []) {
- self.options = options
- }
- // MARK: Encoding
- public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
- var urlRequest = try urlRequest.asURLRequest()
- guard let parameters = parameters else { return urlRequest }
- guard JSONSerialization.isValidJSONObject(parameters) else {
- throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: Error.invalidJSONObject))
- }
- do {
- let data = try JSONSerialization.data(withJSONObject: parameters, options: options)
- if urlRequest.headers["Content-Type"] == nil {
- urlRequest.headers.update(.contentType("application/json"))
- }
- urlRequest.httpBody = data
- } catch {
- throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
- }
- return urlRequest
- }
- /// Encodes any JSON compatible object into a `URLRequest`.
- ///
- /// - Parameters:
- /// - urlRequest: `URLRequestConvertible` value into which the object will be encoded.
- /// - jsonObject: `Any` value (must be JSON compatible` to be encoded into the `URLRequest`. `nil` by default.
- ///
- /// - Returns: The encoded `URLRequest`.
- /// - Throws: Any `Error` produced during encoding.
- public func encode(_ urlRequest: URLRequestConvertible, withJSONObject jsonObject: Any? = nil) throws -> URLRequest {
- var urlRequest = try urlRequest.asURLRequest()
- guard let jsonObject = jsonObject else { return urlRequest }
- guard JSONSerialization.isValidJSONObject(jsonObject) else {
- throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: Error.invalidJSONObject))
- }
- do {
- let data = try JSONSerialization.data(withJSONObject: jsonObject, options: options)
- if urlRequest.headers["Content-Type"] == nil {
- urlRequest.headers.update(.contentType("application/json"))
- }
- urlRequest.httpBody = data
- } catch {
- throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
- }
- return urlRequest
- }
- }
- extension JSONEncoding.Error {
- public var localizedDescription: String {
- """
- Invalid JSON object provided for parameter or object encoding. \
- This is most likely due to a value which can't be represented in Objective-C.
- """
- }
- }
- // MARK: -
- extension NSNumber {
- fileprivate var isBool: Bool {
- // Use Obj-C type encoding to check whether the underlying type is a `Bool`, as it's guaranteed as part of
- // 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).
- String(cString: objCType) == "c"
- }
- }
|