Signal-iOS/SignalServiceKit/src/Network/API/NetworkManager.swift
Evan Hahn 370ff654e7
Change license to AGPL
Change license to AGPL

This commit:

- Updates the `LICENSE` file

- Start every file with something like:

      // Copyright YEAR_FIRST_PUBLISHED Signal Messenger, LLC
      // SPDX-License-Identifier: AGPL-3.0-only

---

First, I removed existing license headers with this Ruby 3.1.2 script:

    require 'set'

    EXTENSIONS_TO_CHECK = Set['.h', '.hpp', '.cpp', '.m', '.mm', '.pch', '.swift']

    same = 0
    different = 0

    all_files = `git ls-files`.lines.map { |line| line.strip }
    all_files.each do |relative_path|
      if relative_path == 'Pods'
        next
      end

      unless EXTENSIONS_TO_CHECK.include? File.extname(relative_path)
        next
      end

      path = File.expand_path(relative_path)

      contents = File.read(path)
      new_contents = contents.sub(/\/\/\n\/\/  Copyright .*\n\/\/\n\n/, '')

      if contents == new_contents
        same += 1
      else
        different += 1
      end

      File.write(path, new_contents)
    end

    puts "updated #{different} file(s), left #{same} untouched"

I'm sure this script could be improved, but it worked well enough.

Then, I created `Scripts/lint/lint-license-headers` and ran it to auto-
fix a lot of files. This changed the mode of some files, but I think
that's actually desirable. For example,
`SignalServiceKit/src/Util/AppContext.m` previously had a mode of
`0755/-rwxr-xr-x`, and it's now `0644/-rw-r--r--`.

Then I fixed some stragglers and updated the precommit script.

See [a similar change in the Desktop app][0].

[0]: 8bfaf598af
2022-10-13 08:25:37 -05:00

92 lines
2.9 KiB
Swift

//
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
// A class used for making HTTP requests against the main service.
@objc
public class NetworkManager: NSObject {
private let restNetworkManager = RESTNetworkManager()
@objc
public override init() {
super.init()
SwiftSingletons.register(self)
}
// This method can be called from any thread.
public func makePromise(request: TSRequest,
websocketSupportsRequest: Bool = false,
remainingRetryCount: Int = 0) -> Promise<HTTPResponse> {
firstly { () -> Promise<HTTPResponse> in
// Fail over to REST if websocket attempt fails.
let shouldUseWebsocket: Bool = {
guard !signalService.isCensorshipCircumventionActive else {
return false
}
return (remainingRetryCount > 0 &&
OWSWebSocket.canAppUseSocketsToMakeRequests &&
websocketSupportsRequest)
}()
return (shouldUseWebsocket
? websocketRequestPromise(request: request)
: restRequestPromise(request: request))
}.recover(on: .global()) { error -> Promise<HTTPResponse> in
if error.isRetryable,
remainingRetryCount > 0 {
// TODO: Backoff?
return self.makePromise(request: request,
remainingRetryCount: remainingRetryCount - 1)
} else {
throw error
}
}
}
private func isRESTOnlyEndpoint(request: TSRequest) -> Bool {
guard let url = request.url else {
owsFailDebug("Missing url.")
return true
}
guard let urlComponents = URLComponents(string: url.absoluteString) else {
owsFailDebug("Missing urlComponents.")
return true
}
let path: String = urlComponents.path
let missingEndpoints = [
"/v1/payments/auth"
]
return missingEndpoints.contains(path)
}
private func restRequestPromise(request: TSRequest) -> Promise<HTTPResponse> {
restNetworkManager.makePromise(request: request)
}
private func websocketRequestPromise(request: TSRequest) -> Promise<HTTPResponse> {
Self.socketManager.makeRequestPromise(request: request)
}
}
// MARK: -
#if TESTABLE_BUILD
@objc
public class OWSFakeNetworkManager: NetworkManager {
public override func makePromise(request: TSRequest,
websocketSupportsRequest: Bool = false,
remainingRetryCount: Int = 0) -> Promise<HTTPResponse> {
Logger.info("Ignoring request: \(request)")
// Never resolve.
let (promise, _) = Promise<HTTPResponse>.pending()
return promise
}
}
#endif