HttpClient.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. public class HttpClient: NSObject {
  22. private var authenticationMethod: String?
  23. fileprivate var user: String?
  24. fileprivate var password: String?
  25. public enum ContentType {
  26. case json
  27. case octetStream
  28. func propertyValue() -> String {
  29. switch self {
  30. case .json:
  31. return "application/json"
  32. case .octetStream:
  33. return "application/octet-stream"
  34. }
  35. }
  36. }
  37. private static let bgSessionsMutationLock: DispatchQueue = DispatchQueue(label: "bgSessionsMutationLock")
  38. private static var bgSessions: [Int: URLSession] = [Int: URLSession]()
  39. override init() {
  40. super.init()
  41. self.authenticationMethod = NSURLAuthenticationMethodDefault
  42. }
  43. /// Initialize HttpClient
  44. /// - parameter user: Username for Basic Authentication
  45. /// - parameter password: Password for Basic Authentication
  46. init(user: String?, password: String?) {
  47. if let user = user, let password = password {
  48. self.authenticationMethod = NSURLAuthenticationMethodHTTPBasic
  49. self.user = user
  50. self.password = password
  51. } else {
  52. self.authenticationMethod = NSURLAuthenticationMethodDefault
  53. }
  54. }
  55. func delete(url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Swift.Void) {
  56. let request = getRequest(url: url, httpMethod: "DELETE")
  57. let task = getSession(delegate: self.authenticationMethod != NSURLAuthenticationMethodHTTPBasic ? nil : self, background: false).dataTask(with: request, completionHandler: completionHandler)
  58. task.resume()
  59. }
  60. func downloadData(url: URL, contentType: ContentType, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Swift.Void) -> Void {
  61. var request = getRequest(url: url, httpMethod: "GET")
  62. request.setValue(contentType.propertyValue(), forHTTPHeaderField: "Accept")
  63. let task = getSession(delegate: self.authenticationMethod != NSURLAuthenticationMethodHTTPBasic ? nil : self, background: false).dataTask(with: request, completionHandler: completionHandler)
  64. task.resume()
  65. }
  66. func downloadData(url: URL, delegate: URLSessionDelegate) -> Void {
  67. var request = getRequest(url: url, httpMethod: "GET")
  68. request.setValue("application/octet-stream", forHTTPHeaderField: "Accept")
  69. let task = getSession(delegate: delegate, background: true).dataTask(with: request)
  70. task.resume()
  71. }
  72. func uploadData(url: URL, data: Data, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Swift.Void) -> Void {
  73. var request = getRequest(url: url, httpMethod: "PUT")
  74. request.setValue(ContentType.octetStream.propertyValue(), forHTTPHeaderField: "Content-Type")
  75. let task = getSession(delegate: self.authenticationMethod != NSURLAuthenticationMethodHTTPBasic ? nil : self, background: false).uploadTask(with: request, from: data, completionHandler: completionHandler)
  76. task.resume()
  77. }
  78. func uploadData(url: URL, file: URL, delegate: URLSessionDelegate) -> Void {
  79. var request = getRequest(url: url, httpMethod: "PUT")
  80. request.setValue(ContentType.octetStream.propertyValue(), forHTTPHeaderField: "Content-Type")
  81. let task = getSession(delegate: delegate, background: true).uploadTask(with: request, fromFile: file)
  82. task.resume()
  83. }
  84. private func getRequest(url: URL, httpMethod: String) -> URLRequest {
  85. var request = URLRequest(url: url, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 90.0)
  86. request.httpMethod = httpMethod
  87. request.setValue("Threema", forHTTPHeaderField: "User-Agent")
  88. return request
  89. }
  90. private func getSession(identifier: Int, delegate: URLSessionDelegate, background: Bool) -> URLSession {
  91. var bgSession: URLSession? = nil
  92. HttpClient.bgSessionsMutationLock.sync {
  93. if let session = HttpClient.bgSessions[identifier] {
  94. bgSession = session
  95. } else {
  96. let configuration = background ? URLSessionConfiguration.background(withIdentifier: String(identifier)) : URLSessionConfiguration.ephemeral
  97. configuration.allowsCellularAccess = true
  98. configuration.sessionSendsLaunchEvents = true
  99. configuration.urlCache = nil
  100. configuration.urlCredentialStorage = nil
  101. let session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
  102. HttpClient.bgSessions[identifier] = session
  103. bgSession = session
  104. }
  105. }
  106. return bgSession!
  107. }
  108. private func getSession(delegate: URLSessionDelegate?, background: Bool) -> URLSession {
  109. if let delegate = delegate {
  110. let objectHash: Int = delegate.hash
  111. return getSession(identifier: objectHash, delegate: delegate, background: background)
  112. }
  113. URLSession.shared.configuration.allowsCellularAccess = true
  114. URLSession.shared.configuration.urlCache = nil
  115. URLSession.shared.configuration.urlCredentialStorage = nil
  116. if #available(iOS 11.0, *) {
  117. URLSession.shared.configuration.waitsForConnectivity = true
  118. }
  119. return URLSession.shared
  120. }
  121. }
  122. extension HttpClient: URLSessionTaskDelegate {
  123. public func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  124. // log authentication mode and response data if is possible
  125. ValidationLogger.shared()?.logString("HttpClient authentication method: \(String(describing: challenge.protectionSpace.authenticationMethod))")
  126. if let error = task.error {
  127. ValidationLogger.shared()?.logString("HttpClient error: \(error.localizedDescription)")
  128. }
  129. // set credentials depends on authentication mode
  130. switch (challenge.protectionSpace.authenticationMethod) {
  131. case NSURLAuthenticationMethodHTTPBasic,
  132. NSURLAuthenticationMethodHTTPDigest:
  133. if (challenge.previousFailureCount < 7), let user = self.user, let password = self.password {
  134. let credential = URLCredential(user: user, password: password, persistence: .forSession)
  135. completionHandler(.useCredential, credential)
  136. } else {
  137. completionHandler(.performDefaultHandling, nil)
  138. }
  139. default:
  140. completionHandler(.performDefaultHandling, nil)
  141. }
  142. }
  143. }