From 1b21755455a00ec06c8efe149234bd3ef47cedb4 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 14 Oct 2021 11:31:08 -0300 Subject: [PATCH] Simplify AFNetworking usage. --- .../attachments/SignalAttachmentTest.swift | 1 + .../src/Network/OWSURLSession.swift | 38 ++++---- .../tests/Network/OWSURLSessionTest.swift | 86 +++++++++---------- 3 files changed, 64 insertions(+), 61 deletions(-) diff --git a/Signal/test/attachments/SignalAttachmentTest.swift b/Signal/test/attachments/SignalAttachmentTest.swift index e5b49eea6c..1e78467936 100644 --- a/Signal/test/attachments/SignalAttachmentTest.swift +++ b/Signal/test/attachments/SignalAttachmentTest.swift @@ -3,6 +3,7 @@ // import XCTest +import CoreServices @testable import SignalMessaging class SignalAttachmentTest: SignalBaseTest { diff --git a/SignalServiceKit/src/Network/OWSURLSession.swift b/SignalServiceKit/src/Network/OWSURLSession.swift index 9348ffa8b0..9d14ff0782 100644 --- a/SignalServiceKit/src/Network/OWSURLSession.swift +++ b/SignalServiceKit/src/Network/OWSURLSession.swift @@ -406,9 +406,11 @@ public class OWSURLSession: NSObject { // Ensure certain invariants for all requests. private func prepareRequest(request: URLRequest) -> URLRequest { + var request = request + request.httpShouldHandleCookies = httpShouldHandleCookies.get() - var request = OWSHttpHeaders.fillInMissingDefaultHeaders(request: request) + request = OWSHttpHeaders.fillInMissingDefaultHeaders(request: request) if signalService.isCensorshipCircumventionActive, let frontingURL = self.frontingURL, @@ -440,22 +442,22 @@ public class OWSURLSession: NSObject { var baseUrl: URL? = self.baseUrl if signalService.isCensorshipCircumventionActive { - if let censorshipCircumventionPrefix = customCensorshipCircumventionPrefix?.nilIfEmpty { - // When we are domain fronting, we target a fronting host and add a path prefix. - let ccBaseUrl: URL = signalService.domainFrontBaseURL.appendingPathComponent(censorshipCircumventionPrefix) - baseUrl = ccBaseUrl - if !Self.isValidUrlForCensorshipCircumvention(urlString, - frontingURL: ccBaseUrl) { - Logger.warn("Unfronted URL: \(urlString), ccBaseUrl: \(ccBaseUrl)") - } - } else if let frontingURL = self.frontingURL { - // Only requests to Signal services require CC. - // If frontingHost is nil, this instance of OWSURLSession does not perform CC. - if !Self.isValidUrlForCensorshipCircumvention(urlString, - frontingURL: frontingURL) { - Logger.warn("Unfronted URL: \(urlString), frontingURL: \(frontingURL)") - } - } + if let censorshipCircumventionPrefix = customCensorshipCircumventionPrefix?.nilIfEmpty { + // When we are domain fronting, we target a fronting host and add a path prefix. + let ccBaseUrl: URL = signalService.domainFrontBaseURL.appendingPathComponent(censorshipCircumventionPrefix) + baseUrl = ccBaseUrl + if !Self.isValidUrlForCensorshipCircumvention(urlString, + frontingURL: ccBaseUrl) { + Logger.warn("Unfronted URL: \(urlString), ccBaseUrl: \(ccBaseUrl)") + } + } else if let frontingURL = self.frontingURL { + // Only requests to Signal services require CC. + // If frontingHost is nil, this instance of OWSURLSession does not perform CC. + if !Self.isValidUrlForCensorshipCircumvention(urlString, + frontingURL: frontingURL) { + Logger.warn("Unfronted URL: \(urlString), frontingURL: \(frontingURL)") + } + } } else if let customHost = customHost?.nilIfEmpty { // For some requests (CDS, KBS, remote attestation) we target a "custom host". guard let customBaseUrl = URL(string: customHost) else { @@ -492,7 +494,7 @@ public class OWSURLSession: NSObject { return false } if let scheme = url.scheme?.nilIfEmpty, - let host = url.host?.nilIfEmpty { + let host = url.host?.nilIfEmpty { // Absolute URLs which already have the correct prefix are valid. return (scheme.lowercased() == "https" && host.lowercased() == frontingHost) diff --git a/SignalServiceKit/tests/Network/OWSURLSessionTest.swift b/SignalServiceKit/tests/Network/OWSURLSessionTest.swift index 7d9fa512d8..101bd36df6 100644 --- a/SignalServiceKit/tests/Network/OWSURLSessionTest.swift +++ b/SignalServiceKit/tests/Network/OWSURLSessionTest.swift @@ -1,5 +1,5 @@ // -// Copyright (c) 2020 Open Whisper Systems. All rights reserved. +// Copyright (c) 2021 Open Whisper Systems. All rights reserved. // import Foundation @@ -11,79 +11,79 @@ class OWSURLSessionTest: SSKBaseTestSwift { func test_buildUrl() { // No base url XCTAssertEqual(URL(string: "https://e.f.com")!, - OWSURLSession.buildUrl(urlString: "https://e.f.com", - baseUrl: nil)!) + OWSURLSession.joinUrl(urlString: "https://e.f.com", + baseUrl: nil)!) XCTAssertEqual(URL(string: "https://e.f.com/")!, - OWSURLSession.buildUrl(urlString: "https://e.f.com/", - baseUrl: nil)!) + OWSURLSession.joinUrl(urlString: "https://e.f.com/", + baseUrl: nil)!) XCTAssertEqual(URL(string: "https://e.f.com/a/b/c")!, - OWSURLSession.buildUrl(urlString: "https://e.f.com/a/b/c", - baseUrl: nil)!) + OWSURLSession.joinUrl(urlString: "https://e.f.com/a/b/c", + baseUrl: nil)!) // * baseUrl with just host, no trailing /. XCTAssertEqual(URL(string: "https://e.f.com/a/b/c")!, - OWSURLSession.buildUrl(urlString: "a/b/c", - baseUrl: URL(string: "https://e.f.com"))) + OWSURLSession.joinUrl(urlString: "a/b/c", + baseUrl: URL(string: "https://e.f.com"))) XCTAssertEqual(URL(string: "https://e.f.com/a/b/c")!, - OWSURLSession.buildUrl(urlString: "/a/b/c", - baseUrl: URL(string: "https://e.f.com"))) + OWSURLSession.joinUrl(urlString: "/a/b/c", + baseUrl: URL(string: "https://e.f.com"))) XCTAssertEqual(URL(string: "https://e.f.com/a/b/c")!, - OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c", - baseUrl: URL(string: "https://e.f.com"))) + OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c", + baseUrl: URL(string: "https://e.f.com"))) // * baseUrl with host & trailing /. XCTAssertEqual(URL(string: "https://e.f.com/a/b/c")!, - OWSURLSession.buildUrl(urlString: "a/b/c", - baseUrl: URL(string: "https://e.f.com/"))) + OWSURLSession.joinUrl(urlString: "a/b/c", + baseUrl: URL(string: "https://e.f.com/"))) XCTAssertEqual(URL(string: "https://e.f.com/a/b/c")!, - OWSURLSession.buildUrl(urlString: "/a/b/c", - baseUrl: URL(string: "https://e.f.com/"))) + OWSURLSession.joinUrl(urlString: "/a/b/c", + baseUrl: URL(string: "https://e.f.com/"))) XCTAssertEqual(URL(string: "https://e.f.com/a/b/c")!, - OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c", - baseUrl: URL(string: "https://e.f.com/"))) + OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c", + baseUrl: URL(string: "https://e.f.com/"))) // * baseUrl with host and path, no trailing /. XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!, - OWSURLSession.buildUrl(urlString: "a/b/c", - baseUrl: URL(string: "https://e.f.com/x"))) + OWSURLSession.joinUrl(urlString: "a/b/c", + baseUrl: URL(string: "https://e.f.com/x"))) XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!, - OWSURLSession.buildUrl(urlString: "/a/b/c", - baseUrl: URL(string: "https://e.f.com/x"))) + OWSURLSession.joinUrl(urlString: "/a/b/c", + baseUrl: URL(string: "https://e.f.com/x"))) XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!, - OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c", - baseUrl: URL(string: "https://e.f.com/x"))) + OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c", + baseUrl: URL(string: "https://e.f.com/x"))) // * baseUrl with host and path & trailing /. XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!, - OWSURLSession.buildUrl(urlString: "a/b/c", - baseUrl: URL(string: "https://e.f.com/x/"))) + OWSURLSession.joinUrl(urlString: "a/b/c", + baseUrl: URL(string: "https://e.f.com/x/"))) XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!, - OWSURLSession.buildUrl(urlString: "/a/b/c", - baseUrl: URL(string: "https://e.f.com/x/"))) + OWSURLSession.joinUrl(urlString: "/a/b/c", + baseUrl: URL(string: "https://e.f.com/x/"))) XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!, - OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c", - baseUrl: URL(string: "https://e.f.com/x/"))) + OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c", + baseUrl: URL(string: "https://e.f.com/x/"))) // Querystring XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c?i=j&k=l")!, - OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c?i=j&k=l", - baseUrl: URL(string: "https://e.f.com/x/"))) + OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c?i=j&k=l", + baseUrl: URL(string: "https://e.f.com/x/"))) XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c?i=j&k=l")!, - OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c?i=j&k=l", - baseUrl: URL(string: "https://e.f.com/x/?m=m"))) + OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c?i=j&k=l", + baseUrl: URL(string: "https://e.f.com/x/?m=m"))) XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!, - OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c", - baseUrl: URL(string: "https://e.f.com/x/?m=m"))) + OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c", + baseUrl: URL(string: "https://e.f.com/x/?m=m"))) // Fragment XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c#ooo")!, - OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c#ooo", - baseUrl: URL(string: "https://e.f.com/x/"))) + OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c#ooo", + baseUrl: URL(string: "https://e.f.com/x/"))) XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c#ooo")!, - OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c#ooo", - baseUrl: URL(string: "https://e.f.com/x/#ppp"))) + OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c#ooo", + baseUrl: URL(string: "https://e.f.com/x/#ppp"))) XCTAssertEqual(URL(string: "https://e.f.com/x/a/b/c")!, - OWSURLSession.buildUrl(urlString: "http://g.h.com/a/b/c", - baseUrl: URL(string: "https://e.f.com/x/#ppp"))) + OWSURLSession.joinUrl(urlString: "http://g.h.com/a/b/c", + baseUrl: URL(string: "https://e.f.com/x/#ppp"))) } }