summaryrefslogtreecommitdiffhomepage
path: root/ios/TunnelProviderMessaging/ProxyURLRequest.swift
blob: acc346f2fcf4e2a77dff13b77d36612534094e40 (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
//
//  ProxyURLRequest.swift
//  TunnelProviderMessaging
//
//  Created by Sajad Vishkai on 2022-10-03.
//  Copyright © 2022 Mullvad VPN AB. All rights reserved.
//

import Foundation

/// Struct describing serializable URLRequest data.
public struct ProxyURLRequest: Codable {
    public let id: UUID
    public let url: URL
    public let method: String?
    public let httpBody: Data?
    public let httpHeaders: [String: String]?

    public var urlRequest: URLRequest {
        var urlRequest = URLRequest(url: url)
        urlRequest.httpMethod = method
        urlRequest.httpBody = httpBody
        urlRequest.allHTTPHeaderFields = httpHeaders
        return urlRequest
    }

    public init(id: UUID, urlRequest: URLRequest) throws {
        guard let url = urlRequest.url else {
            throw InvalidURLRequestError()
        }

        self.id = id
        self.url = url
        method = urlRequest.httpMethod
        httpBody = urlRequest.httpBody
        httpHeaders = urlRequest.allHTTPHeaderFields
    }
}

public struct InvalidURLRequestError: LocalizedError {
    public var errorDescription: String? {
        return "Invalid URLRequest URL."
    }
}