-
Notifications
You must be signed in to change notification settings - Fork 26
chore: make NetworkService sendable - WPB-24336 #4483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
b045708
079bf34
f99104c
ffff31c
683f9f9
7989205
8692255
8be100c
3983ec9
b7e098b
3311a1b
ce13294
b608e5d
44056ec
4171cb5
91abd2f
ae45c70
d1ed453
2a50a07
f124c1f
a4a22a5
604a4a7
4229fdc
0ad7e88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // | ||
| // Wire | ||
| // Copyright (C) 2026 Wire Swiss GmbH | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see http://www.gnu.org/licenses/. | ||
| // | ||
|
|
||
| import Foundation | ||
| import WireLogging | ||
|
|
||
| final class NetworkServiceSessionDelegate: NSObject, URLSessionWebSocketDelegate, URLSessionTaskDelegate, Sendable { | ||
|
|
||
| private let serverTrustValidator: ServerTrustValidator | ||
| private let webSocketStore: WebSocketStore | ||
|
|
||
| init( | ||
| serverTrustValidator: ServerTrustValidator, | ||
| webSocketStore: WebSocketStore | ||
| ) { | ||
| self.serverTrustValidator = serverTrustValidator | ||
| self.webSocketStore = webSocketStore | ||
| super.init() | ||
| } | ||
|
|
||
| // MARK: - URLSessionWebSocketDelegate | ||
|
|
||
| func urlSession( | ||
| _ session: URLSession, | ||
|
Check warning on line 39 in WireNetwork/Sources/WireNetwork/Network/NetworkService/NetworkServiceSessionDelegate.swift
|
||
| webSocketTask: URLSessionWebSocketTask, | ||
| didOpenWithProtocol protocol: String? | ||
|
Check warning on line 41 in WireNetwork/Sources/WireNetwork/Network/NetworkService/NetworkServiceSessionDelegate.swift
|
||
| ) { | ||
| WireLogger.network.debug("web socket task did open") | ||
| if let request = webSocketTask.currentRequest { | ||
| WireLogger.network.log(request) | ||
| } | ||
| } | ||
|
|
||
| func urlSession( | ||
| _ session: URLSession, | ||
|
Check warning on line 50 in WireNetwork/Sources/WireNetwork/Network/NetworkService/NetworkServiceSessionDelegate.swift
|
||
| webSocketTask: URLSessionWebSocketTask, | ||
| didCloseWith closeCode: URLSessionWebSocketTask.CloseCode, | ||
| reason: Data? | ||
| ) { | ||
| WireLogger.network | ||
| .debug( | ||
| "web socket task did close. Close code: \(closeCode), Reason: \(String(data: reason ?? Data(), encoding: .utf8) ?? "No reason")" | ||
| ) | ||
| Task { | ||
| await webSocketStore.retrieve(for: webSocketTask)?.close() | ||
| await webSocketStore.remove(for: webSocketTask) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - URLSessionTaskDelegate | ||
|
|
||
| func urlSession( | ||
| _ session: URLSession, | ||
|
Check warning on line 68 in WireNetwork/Sources/WireNetwork/Network/NetworkService/NetworkServiceSessionDelegate.swift
|
||
| task: URLSessionTask, | ||
|
Check warning on line 69 in WireNetwork/Sources/WireNetwork/Network/NetworkService/NetworkServiceSessionDelegate.swift
|
||
| didCompleteWithError error: (any Error)? | ||
| ) { | ||
| // NOTE: This method is not called when when using async/await APIs. | ||
| if let error { | ||
| WireLogger.network.error("task did complete with error: \(error)") | ||
| } else { | ||
| WireLogger.network.debug("task did complete") | ||
| } | ||
| } | ||
|
|
||
| func urlSession( | ||
| _ session: URLSession, | ||
|
Check warning on line 81 in WireNetwork/Sources/WireNetwork/Network/NetworkService/NetworkServiceSessionDelegate.swift
|
||
| task: URLSessionTask, | ||
|
Check warning on line 82 in WireNetwork/Sources/WireNetwork/Network/NetworkService/NetworkServiceSessionDelegate.swift
|
||
| didReceive challenge: | ||
| URLAuthenticationChallenge | ||
| ) async -> (URLSession.AuthChallengeDisposition, URLCredential?) { | ||
|
|
||
| let protectionSpace = challenge.protectionSpace | ||
| if protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust { | ||
| guard let trust = challenge.protectionSpace.serverTrust else { | ||
| // If this is missing it is Apple breaking its API contract so crash. | ||
| fatalError("Missing server trust") | ||
| } | ||
|
|
||
| do { | ||
| try await serverTrustValidator.validate(trust: trust, host: protectionSpace.host) | ||
| return (.performDefaultHandling, challenge.proposedCredential) | ||
| } catch { | ||
| return (.cancelAuthenticationChallenge, nil) | ||
| } | ||
| } else { | ||
| return (.performDefaultHandling, challenge.proposedCredential) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.