123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- import Foundation
- public enum HTTPMethod: String {
- case options = "OPTIONS"
- case get = "GET"
- case head = "HEAD"
- case post = "POST"
- case put = "PUT"
- case patch = "PATCH"
- case delete = "DELETE"
- case trace = "TRACE"
- case connect = "CONNECT"
- }
- public typealias Parameters = [String: Any]
- public protocol ParameterEncoding {
-
-
-
-
-
-
-
-
- func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest
- }
- public struct URLEncoding: ParameterEncoding {
-
-
-
-
-
-
-
-
- public enum Destination {
- case methodDependent, queryString, httpBody
- }
-
-
-
-
-
- public enum ArrayEncoding {
- case brackets, noBrackets
- func encode(key: String) -> String {
- switch self {
- case .brackets:
- return "\(key)[]"
- case .noBrackets:
- return key
- }
- }
- }
-
-
-
-
- public enum BoolEncoding {
- case numeric, literal
- func encode(value: Bool) -> String {
- switch self {
- case .numeric:
- return value ? "1" : "0"
- case .literal:
- return value ? "true" : "false"
- }
- }
- }
-
-
- public static var `default`: URLEncoding { return URLEncoding() }
-
- public static var methodDependent: URLEncoding { return URLEncoding() }
-
- public static var queryString: URLEncoding { return URLEncoding(destination: .queryString) }
-
- public static var httpBody: URLEncoding { return URLEncoding(destination: .httpBody) }
-
- public let destination: Destination
-
- public let arrayEncoding: ArrayEncoding
-
- public let boolEncoding: BoolEncoding
-
-
-
-
-
-
-
-
- public init(destination: Destination = .methodDependent, arrayEncoding: ArrayEncoding = .brackets, boolEncoding: BoolEncoding = .numeric) {
- self.destination = destination
- self.arrayEncoding = arrayEncoding
- self.boolEncoding = boolEncoding
- }
-
-
-
-
-
-
-
-
-
- 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 = HTTPMethod(rawValue: urlRequest.httpMethod ?? "GET"), encodesParametersInURL(with: 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.value(forHTTPHeaderField: "Content-Type") == nil {
- urlRequest.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
- }
- urlRequest.httpBody = query(parameters).data(using: .utf8, allowLossyConversion: false)
- }
- return urlRequest
- }
-
-
-
-
-
-
- public func queryComponents(fromKey key: String, value: Any) -> [(String, String)] {
- var components: [(String, String)] = []
- if let dictionary = value as? [String: Any] {
- for (nestedKey, value) in dictionary {
- components += queryComponents(fromKey: "\(key)[\(nestedKey)]", value: value)
- }
- } else if let array = value as? [Any] {
- for value in array {
- components += queryComponents(fromKey: arrayEncoding.encode(key: key), value: value)
- }
- } else if let value = value as? NSNumber {
- if value.isBool {
- components.append((escape(key), escape(boolEncoding.encode(value: value.boolValue))))
- } else {
- components.append((escape(key), escape("\(value)")))
- }
- } else if let bool = value as? Bool {
- components.append((escape(key), escape(boolEncoding.encode(value: bool))))
- } else {
- components.append((escape(key), escape("\(value)")))
- }
- return components
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public func escape(_ string: String) -> String {
- let generalDelimitersToEncode = ":#[]@"
- let subDelimitersToEncode = "!$&'()*+,;="
- var allowedCharacterSet = CharacterSet.urlQueryAllowed
- allowedCharacterSet.remove(charactersIn: "\(generalDelimitersToEncode)\(subDelimitersToEncode)")
- var escaped = ""
-
-
-
-
-
-
-
-
-
-
- if #available(iOS 8.3, *) {
- escaped = string.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet) ?? string
- } else {
- let batchSize = 50
- var index = string.startIndex
- while index != string.endIndex {
- let startIndex = index
- let endIndex = string.index(index, offsetBy: batchSize, limitedBy: string.endIndex) ?? string.endIndex
- let range = startIndex..<endIndex
- let substring = string[range]
- escaped += substring.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet) ?? String(substring)
- index = endIndex
- }
- }
- return escaped
- }
- 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: "&")
- }
- private func encodesParametersInURL(with method: HTTPMethod) -> Bool {
- switch destination {
- case .queryString:
- return true
- case .httpBody:
- return false
- default:
- break
- }
- switch method {
- case .get, .head, .delete:
- return true
- default:
- return false
- }
- }
- }
- public struct JSONEncoding: ParameterEncoding {
-
-
- public static var `default`: JSONEncoding { return JSONEncoding() }
-
- public static var prettyPrinted: JSONEncoding { return JSONEncoding(options: .prettyPrinted) }
-
- public let options: JSONSerialization.WritingOptions
-
-
-
-
-
-
- public init(options: JSONSerialization.WritingOptions = []) {
- self.options = options
- }
-
-
-
-
-
-
-
-
-
- public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
- var urlRequest = try urlRequest.asURLRequest()
- guard let parameters = parameters else { return urlRequest }
- do {
- let data = try JSONSerialization.data(withJSONObject: parameters, options: options)
- if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
- urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
- }
- urlRequest.httpBody = data
- } catch {
- throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
- }
- return urlRequest
- }
-
-
-
-
-
-
-
-
- public func encode(_ urlRequest: URLRequestConvertible, withJSONObject jsonObject: Any? = nil) throws -> URLRequest {
- var urlRequest = try urlRequest.asURLRequest()
- guard let jsonObject = jsonObject else { return urlRequest }
- do {
- let data = try JSONSerialization.data(withJSONObject: jsonObject, options: options)
- if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
- urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
- }
- urlRequest.httpBody = data
- } catch {
- throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))
- }
- return urlRequest
- }
- }
- public struct PropertyListEncoding: ParameterEncoding {
-
-
- public static var `default`: PropertyListEncoding { return PropertyListEncoding() }
-
- public static var xml: PropertyListEncoding { return PropertyListEncoding(format: .xml) }
-
- public static var binary: PropertyListEncoding { return PropertyListEncoding(format: .binary) }
-
- public let format: PropertyListSerialization.PropertyListFormat
-
- public let options: PropertyListSerialization.WriteOptions
-
-
-
-
-
-
-
- public init(
- format: PropertyListSerialization.PropertyListFormat = .xml,
- options: PropertyListSerialization.WriteOptions = 0)
- {
- self.format = format
- self.options = options
- }
-
-
-
-
-
-
-
-
-
- public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
- var urlRequest = try urlRequest.asURLRequest()
- guard let parameters = parameters else { return urlRequest }
- do {
- let data = try PropertyListSerialization.data(
- fromPropertyList: parameters,
- format: format,
- options: options
- )
- if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
- urlRequest.setValue("application/x-plist", forHTTPHeaderField: "Content-Type")
- }
- urlRequest.httpBody = data
- } catch {
- throw AFError.parameterEncodingFailed(reason: .propertyListEncodingFailed(error: error))
- }
- return urlRequest
- }
- }
- extension NSNumber {
- fileprivate var isBool: Bool { return CFBooleanGetTypeID() == CFGetTypeID(self) }
- }
|