summaryrefslogtreecommitdiffhomepage
path: root/ios/PacketTunnelCore/URLRequestProxy/ProxyURLResponse.swift
blob: e2c6e5065e1c4d3744489703ad2e2bb69692f8f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
//  ProxyURLResponse.swift
//  PacketTunnelCore
//
//  Created by pronebird on 20/10/2022.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation
import MullvadREST

/// Struct describing serializable URLResponse data.
public struct ProxyURLResponse: Codable, Sendable {
    public let data: Data?
    public let response: HTTPURLResponseWrapper?
    public let error: URLErrorWrapper?

    public init(data: Data?, response: URLResponse?, error: Error?) {
        self.data = data
        self.response = response.flatMap { HTTPURLResponseWrapper($0) }
        self.error = error.flatMap { URLErrorWrapper($0) }
    }
}

public struct URLErrorWrapper: Codable, Sendable {
    public let code: Int?
    public let localizedDescription: String

    public init?(_ error: Error) {
        localizedDescription = error.localizedDescription
        code = (error as? URLError)?.errorCode
    }

    public var originalError: Error? {
        guard let code else { return nil }

        return URLError(URLError.Code(rawValue: code))
    }
}

public struct HTTPURLResponseWrapper: Codable, Sendable {
    public let url: URL?
    public let statusCode: Int
    public let headerFields: [String: String]?

    public init?(_ response: URLResponse) {
        guard let response = response as? HTTPURLResponse else { return nil }

        url = response.url
        statusCode = response.statusCode
        headerFields = Dictionary(
            uniqueKeysWithValues: response.allHeaderFields.map { ("\($0)", "\($1)") }
        )
    }

    public var originalResponse: HTTPURLResponse? {
        guard let url else { return nil }

        return HTTPURLResponse(
            url: url,
            statusCode: statusCode,
            httpVersion: nil,
            headerFields: headerFields
        )
    }
}