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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
//
// RESTRequestFactory.swift
// MullvadREST
//
// Created by pronebird on 16/04/2022.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import Foundation
import MullvadTypes
extension REST {
final class RequestFactory {
let hostname: String
let pathPrefix: String
let networkTimeout: Duration
let bodyEncoder: JSONEncoder
static func withDefaultAPICredentials(
pathPrefix: String,
bodyEncoder: JSONEncoder
) -> RequestFactory {
RequestFactory(
hostname: defaultAPIHostname,
pathPrefix: pathPrefix,
networkTimeout: defaultAPINetworkTimeout,
bodyEncoder: bodyEncoder
)
}
init(
hostname: String,
pathPrefix: String,
networkTimeout: Duration,
bodyEncoder: JSONEncoder
) {
self.hostname = hostname
self.pathPrefix = pathPrefix
self.networkTimeout = networkTimeout
self.bodyEncoder = bodyEncoder
}
func createRequest(
endpoint: AnyIPEndpoint,
method: HTTPMethod,
pathTemplate: URLPathTemplate
) throws -> REST.Request {
var urlComponents = URLComponents()
urlComponents.scheme = "https"
urlComponents.path = pathPrefix
urlComponents.host = "\(endpoint.ip)"
urlComponents.port = Int(endpoint.port)
let pathString = try pathTemplate.pathString()
let requestURL = urlComponents.url!.appendingPathComponent(pathString)
var request = URLRequest(
url: requestURL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: networkTimeout.timeInterval
)
request.httpShouldHandleCookies = false
request.addValue(hostname, forHTTPHeaderField: HTTPHeader.host)
request.addValue("application/json", forHTTPHeaderField: HTTPHeader.contentType)
request.addValue("mullvad-app", forHTTPHeaderField: HTTPHeader.userAgent)
request.httpMethod = method.rawValue
let prefixedPathTemplate = URLPathTemplate(stringLiteral: pathPrefix) + pathTemplate
return REST.Request(
urlRequest: request,
pathTemplate: prefixedPathTemplate
)
}
func createRequestBuilder(
endpoint: AnyIPEndpoint,
method: HTTPMethod,
pathTemplate: URLPathTemplate
) throws -> RequestBuilder {
let request = try createRequest(
endpoint: endpoint,
method: method,
pathTemplate: pathTemplate
)
return RequestBuilder(
restRequest: request,
bodyEncoder: bodyEncoder
)
}
}
struct RequestBuilder {
private var restRequest: REST.Request
private let bodyEncoder: JSONEncoder
init(restRequest: REST.Request, bodyEncoder: JSONEncoder) {
self.restRequest = restRequest
self.bodyEncoder = bodyEncoder
}
mutating func setHTTPBody(value: some Encodable) throws {
restRequest.urlRequest.httpBody = try bodyEncoder.encode(value)
}
mutating func setETagHeader(etag: String) {
var etag = etag
// Enforce weak validator to account for some backend caching quirks.
if etag.starts(with: "\"") {
etag.insert(contentsOf: "W/", at: etag.startIndex)
}
restRequest.urlRequest.setValue(etag, forHTTPHeaderField: HTTPHeader.ifNoneMatch)
}
mutating func setAuthorization(_ authorization: REST.Authorization) {
restRequest.urlRequest.addValue(
"Bearer \(authorization)",
forHTTPHeaderField: HTTPHeader.authorization
)
}
mutating func addValue(_ value: String, forHTTPHeaderField: String) {
restRequest.urlRequest.addValue(
value,
forHTTPHeaderField: forHTTPHeaderField
)
}
func getRequest() -> REST.Request {
restRequest
}
}
struct URLPathTemplate: ExpressibleByStringLiteral {
enum Component {
case literal(String)
case placeholder(String)
}
enum Error: LocalizedError {
/// Replacement value is not provided for placeholder.
case noReplacement(_ name: String)
/// Failure to perecent encode replacement value.
case percentEncoding
var errorDescription: String? {
switch self {
case let .noReplacement(placeholder):
return "Replacement is not provided for \(placeholder)."
case .percentEncoding:
return "Failed to percent encode replacement value."
}
}
}
private var components: [Component]
private var replacements = [String: String]()
init(stringLiteral value: StringLiteralType) {
let slashCharset = CharacterSet(charactersIn: "/")
components = value.split(separator: "/").map { subpath -> Component in
if subpath.hasPrefix("{"), subpath.hasSuffix("}") {
let name = String(subpath.dropFirst().dropLast())
return .placeholder(name)
} else {
return .literal(
subpath.trimmingCharacters(in: slashCharset)
)
}
}
}
private init(components: [Component]) {
self.components = components
}
mutating func addPercentEncodedReplacement(
name: String,
value: String,
allowedCharacters: CharacterSet
) throws {
let encoded = value.addingPercentEncoding(
withAllowedCharacters: allowedCharacters
)
if let encoded {
replacements[name] = encoded
} else {
throw Error.percentEncoding
}
}
var templateString: String {
var combinedString = ""
for component in components {
combinedString += "/"
switch component {
case let .literal(string):
combinedString += string
case let .placeholder(name):
combinedString += "{\(name)}"
}
}
return combinedString
}
func pathString() throws -> String {
var combinedPath = ""
for component in components {
combinedPath += "/"
switch component {
case let .literal(string):
combinedPath += string
case let .placeholder(name):
if let string = replacements[name] {
combinedPath += string
} else {
throw Error.noReplacement(name)
}
}
}
return combinedPath
}
static func + (lhs: URLPathTemplate, rhs: URLPathTemplate) -> URLPathTemplate {
URLPathTemplate(components: lhs.components + rhs.components)
}
}
}
|