@maplibre/maplibre-react-native 11.0.0-beta.19 → 11.0.0-beta.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/org/maplibre/reactnative/MLRNPackage.kt +13 -0
- package/android/src/main/java/org/maplibre/reactnative/modules/MLRNLogModule.kt +2 -0
- package/android/src/main/java/org/maplibre/reactnative/modules/MLRNNetworkModule.kt +0 -42
- package/android/src/main/java/org/maplibre/reactnative/modules/MLRNTransformRequestModule.kt +144 -0
- package/android/src/main/java/org/maplibre/reactnative/modules/TransformRequestInterceptor.kt +184 -0
- package/ios/modules/logging/MLRNLogging.h +4 -0
- package/ios/modules/logging/MLRNLogging.m +8 -0
- package/ios/modules/network/MLRNNetworkModule.mm +0 -10
- package/ios/modules/transform-request/MLRNTransformRequest.h +38 -0
- package/ios/modules/transform-request/MLRNTransformRequest.m +413 -0
- package/ios/modules/transform-request/MLRNTransformRequestModule.h +9 -0
- package/ios/modules/transform-request/MLRNTransformRequestModule.mm +72 -0
- package/lib/commonjs/index.js +7 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/modules/network/NativeNetworkModule.js.map +1 -1
- package/lib/commonjs/modules/network/NetworkManager.js +1 -39
- package/lib/commonjs/modules/network/NetworkManager.js.map +1 -1
- package/lib/commonjs/modules/transform-request/NativeTransformRequestModule.js +9 -0
- package/lib/commonjs/modules/transform-request/NativeTransformRequestModule.js.map +1 -0
- package/lib/commonjs/modules/transform-request/TransformRequestManager.js +223 -0
- package/lib/commonjs/modules/transform-request/TransformRequestManager.js.map +1 -0
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/modules/network/NativeNetworkModule.js.map +1 -1
- package/lib/module/modules/network/NetworkManager.js +1 -39
- package/lib/module/modules/network/NetworkManager.js.map +1 -1
- package/lib/module/modules/transform-request/NativeTransformRequestModule.js +5 -0
- package/lib/module/modules/transform-request/NativeTransformRequestModule.js.map +1 -0
- package/lib/module/modules/transform-request/TransformRequestManager.js +220 -0
- package/lib/module/modules/transform-request/TransformRequestManager.js.map +1 -0
- package/lib/typescript/commonjs/index.d.ts +1 -0
- package/lib/typescript/commonjs/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/modules/network/NativeNetworkModule.d.ts +0 -2
- package/lib/typescript/commonjs/modules/network/NativeNetworkModule.d.ts.map +1 -1
- package/lib/typescript/commonjs/modules/network/NetworkManager.d.ts +1 -33
- package/lib/typescript/commonjs/modules/network/NetworkManager.d.ts.map +1 -1
- package/lib/typescript/commonjs/modules/transform-request/NativeTransformRequestModule.d.ts +15 -0
- package/lib/typescript/commonjs/modules/transform-request/NativeTransformRequestModule.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/transform-request/TransformRequestManager.d.ts +201 -0
- package/lib/typescript/commonjs/modules/transform-request/TransformRequestManager.d.ts.map +1 -0
- package/lib/typescript/module/index.d.ts +1 -0
- package/lib/typescript/module/index.d.ts.map +1 -1
- package/lib/typescript/module/modules/network/NativeNetworkModule.d.ts +0 -2
- package/lib/typescript/module/modules/network/NativeNetworkModule.d.ts.map +1 -1
- package/lib/typescript/module/modules/network/NetworkManager.d.ts +1 -33
- package/lib/typescript/module/modules/network/NetworkManager.d.ts.map +1 -1
- package/lib/typescript/module/modules/transform-request/NativeTransformRequestModule.d.ts +15 -0
- package/lib/typescript/module/modules/transform-request/NativeTransformRequestModule.d.ts.map +1 -0
- package/lib/typescript/module/modules/transform-request/TransformRequestManager.d.ts +201 -0
- package/lib/typescript/module/modules/transform-request/TransformRequestManager.d.ts.map +1 -0
- package/package.json +1 -12
- package/src/index.ts +8 -0
- package/src/modules/network/NativeNetworkModule.ts +0 -4
- package/src/modules/network/NetworkManager.ts +1 -47
- package/src/modules/transform-request/NativeTransformRequestModule.ts +41 -0
- package/src/modules/transform-request/TransformRequestManager.ts +292 -0
- package/android/src/main/java/org/maplibre/reactnative/http/RequestHeadersInterceptor.kt +0 -65
- package/ios/modules/network/MLRNNetworkHTTPHeaders.h +0 -14
- package/ios/modules/network/MLRNNetworkHTTPHeaders.m +0 -97
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { TurboModule } from "react-native";
|
|
2
|
+
import { TurboModuleRegistry } from "react-native";
|
|
3
|
+
|
|
4
|
+
export interface Spec extends TurboModule {
|
|
5
|
+
addUrlTransform(
|
|
6
|
+
id: string,
|
|
7
|
+
match: string | null,
|
|
8
|
+
find: string,
|
|
9
|
+
replace: string,
|
|
10
|
+
): void;
|
|
11
|
+
|
|
12
|
+
removeUrlTransform(id: string): void;
|
|
13
|
+
|
|
14
|
+
clearUrlTransforms(): void;
|
|
15
|
+
|
|
16
|
+
addUrlSearchParam(
|
|
17
|
+
id: string,
|
|
18
|
+
match: string | null,
|
|
19
|
+
name: string,
|
|
20
|
+
value: string,
|
|
21
|
+
): void;
|
|
22
|
+
|
|
23
|
+
removeUrlSearchParam(id: string): void;
|
|
24
|
+
|
|
25
|
+
clearUrlSearchParams(): void;
|
|
26
|
+
|
|
27
|
+
addHeader(
|
|
28
|
+
id: string,
|
|
29
|
+
match: string | null,
|
|
30
|
+
name: string,
|
|
31
|
+
value: string,
|
|
32
|
+
): void;
|
|
33
|
+
|
|
34
|
+
removeHeader(id: string): void;
|
|
35
|
+
|
|
36
|
+
clearHeaders(): void;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export default TurboModuleRegistry.getEnforcing<Spec>(
|
|
40
|
+
"MLRNTransformRequestModule",
|
|
41
|
+
);
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import NativeTransformRequestModule from "./NativeTransformRequestModule";
|
|
2
|
+
|
|
3
|
+
export interface TransformOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The id is used to identify a transform. Can be used to remove or update it.
|
|
6
|
+
* When omitted it will be auto-generated.
|
|
7
|
+
*/
|
|
8
|
+
id?: string;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Optional regex to be used as the condition, if the transform should be
|
|
12
|
+
* applied. When omitted the transform will always be applied.
|
|
13
|
+
*
|
|
14
|
+
* Global flags are (like `/i`) are not supported. Supports only inline flags, e.g. `(?i)` for case-insensitive matching.
|
|
15
|
+
*/
|
|
16
|
+
match?: RegExp | string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A serializable transform for rewriting MapLibre request URLs.
|
|
21
|
+
*
|
|
22
|
+
* Transforms are applied as a pipeline in the order they were added: transform N+1 sees
|
|
23
|
+
* the URL *after* transform N has possibly changed it.
|
|
24
|
+
*/
|
|
25
|
+
export interface UrlTransformOptions extends TransformOptions {
|
|
26
|
+
/**
|
|
27
|
+
* Regex to find the portion of the URL to replace.
|
|
28
|
+
* Supports capture groups that can be back-referenced in `replace` as `$1`, `$2`, …
|
|
29
|
+
*
|
|
30
|
+
* Global flags are (like `/i`) are not supported. Supports only inline flags, e.g. `(?i)` for case-insensitive matching.
|
|
31
|
+
*/
|
|
32
|
+
find: RegExp | string;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Replacement string. Reference capture groups from `find` with `$1`, `$2`, …
|
|
36
|
+
*/
|
|
37
|
+
replace: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A URL query parameter to append to matching map resource requests.
|
|
42
|
+
*/
|
|
43
|
+
export interface UrlSearchParamOptions extends TransformOptions {
|
|
44
|
+
/** The query parameter name (e.g., `"apiKey"`). */
|
|
45
|
+
name: string;
|
|
46
|
+
|
|
47
|
+
/** The query parameter value (e.g., your API key). */
|
|
48
|
+
value: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A HTTP header to send with matching map resource requests.
|
|
53
|
+
*/
|
|
54
|
+
export interface HeaderOptions extends TransformOptions {
|
|
55
|
+
/** The header name (e.g., `"Authorization"`). */
|
|
56
|
+
name: string;
|
|
57
|
+
|
|
58
|
+
/** The header value (e.g., `"Bearer token123"`). */
|
|
59
|
+
value: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* TransformRequestManager provides methods for managing HTTP requests made by MapLibre.
|
|
64
|
+
*
|
|
65
|
+
* Transformations are possible in three ways:
|
|
66
|
+
*
|
|
67
|
+
* 1. Transforming the URL with search and replace
|
|
68
|
+
* 2. Adding URL search params
|
|
69
|
+
* 3. Adding HTTP headers
|
|
70
|
+
*
|
|
71
|
+
* Transforms are applied in this order. The `match` conditions are applied to
|
|
72
|
+
* possibly already transformed URLs.
|
|
73
|
+
*
|
|
74
|
+
* @remarks
|
|
75
|
+
* To gain insight into which transforms are applied set the log level to
|
|
76
|
+
* `"debug"` via {@link LogManager}:
|
|
77
|
+
*
|
|
78
|
+
* ```ts
|
|
79
|
+
* LogManager.setLogLevel("debug");
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
class TransformRequestManager {
|
|
83
|
+
/**
|
|
84
|
+
* Adds or updates a URL transform identified by `id`.
|
|
85
|
+
*
|
|
86
|
+
* Transforms execute in insertion order. Therefore `match` and `find` regexes
|
|
87
|
+
* are matched against possibly already modified URL by previous transforms.
|
|
88
|
+
*
|
|
89
|
+
* Re-adding an existing `id` updates the transform
|
|
90
|
+
* **in-place**, preserving its position in the pipeline. This makes it safe to
|
|
91
|
+
* refresh tokens or swap domains without disrupting the order of other transforms.
|
|
92
|
+
*
|
|
93
|
+
* URL transforms are applied before `addUrlSearchParam` and `addHeader`.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* // Upgrade all requests to HTTPS
|
|
97
|
+
* TransformRequestManager.addUrlTransform({
|
|
98
|
+
* id: "force-https",
|
|
99
|
+
* find: "^http://",
|
|
100
|
+
* replace: "https://",
|
|
101
|
+
* });
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* // Redirect a specific domain through a proxy
|
|
105
|
+
* TransformRequestManager.addUrlTransform({
|
|
106
|
+
* id: "proxy",
|
|
107
|
+
* match: "tiles\\.example\\.com",
|
|
108
|
+
* find: "tiles\\.example\\.com",
|
|
109
|
+
* replace: "proxy.example.com",
|
|
110
|
+
* });
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* // Inject an API key into the path using a capture group
|
|
114
|
+
* TransformRequestManager.addUrlTransform({
|
|
115
|
+
* id: "api-key",
|
|
116
|
+
* match: "api\\.example\\.com",
|
|
117
|
+
* find: "(https://api\\.example\\.com/)(.*)",
|
|
118
|
+
* replace: "$1mySecretKey/$2",
|
|
119
|
+
* });
|
|
120
|
+
*
|
|
121
|
+
* @param options The transform. Set {@link TransformOptions.id} to a stable string to
|
|
122
|
+
* enable in-place updates; if omitted an id is auto-generated and returned.
|
|
123
|
+
*
|
|
124
|
+
* @returns The id of the transform (the value of `transform.id` when provided, otherwise
|
|
125
|
+
* the auto-generated one). Pass it to {@link removeUrlTransform} to remove it later.
|
|
126
|
+
*/
|
|
127
|
+
addUrlTransform(options: UrlTransformOptions): string {
|
|
128
|
+
const id = options.id ?? this.getId();
|
|
129
|
+
|
|
130
|
+
NativeTransformRequestModule.addUrlTransform(
|
|
131
|
+
id,
|
|
132
|
+
TransformRequestManager.toRegexString(options.match),
|
|
133
|
+
TransformRequestManager.toRegexString(options.find) ?? "",
|
|
134
|
+
options.replace,
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
return id;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Removes the URL transform with the given `id`.
|
|
142
|
+
* No-op if the id is not registered.
|
|
143
|
+
*
|
|
144
|
+
* @param id The identifier passed to/returned from {@link addUrlTransform}.
|
|
145
|
+
*/
|
|
146
|
+
removeUrlTransform(id: string): void {
|
|
147
|
+
NativeTransformRequestModule.removeUrlTransform(id);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Removes all registered URL transforms
|
|
152
|
+
*/
|
|
153
|
+
clearUrlTransforms(): void {
|
|
154
|
+
NativeTransformRequestModule.clearUrlTransforms();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Adds or updates a URL query parameter identified by `id` that will be appended to all
|
|
159
|
+
* matching map resource requests. Re-adding an existing `id` updates the param in-place.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* // Add apiKey to for a specific domain
|
|
163
|
+
* TransformRequestManager.addUrlSearchParam({
|
|
164
|
+
* match: /tiles\.example\.com/,
|
|
165
|
+
* name: "apiKey",
|
|
166
|
+
* value: "your-api-key",
|
|
167
|
+
* });
|
|
168
|
+
*
|
|
169
|
+
* // Add apiKey to all requests (no match = applies to all)
|
|
170
|
+
* TransformRequestManager.addUrlSearchParam({ name: "apiKey", value: "your-api-key" });
|
|
171
|
+
*
|
|
172
|
+
* @param options The options. Set {@link TransformOptions.id} to a stable string to
|
|
173
|
+
* enable in-place updates; if omitted an id is auto-generated and returned.
|
|
174
|
+
*
|
|
175
|
+
* @returns The id of the options. Pass it to {@link removeUrlSearchParam} to remove it later.
|
|
176
|
+
*/
|
|
177
|
+
addUrlSearchParam(options: UrlSearchParamOptions): string {
|
|
178
|
+
const id = options.id ?? this.getId();
|
|
179
|
+
|
|
180
|
+
NativeTransformRequestModule.addUrlSearchParam(
|
|
181
|
+
id,
|
|
182
|
+
TransformRequestManager.toRegexString(options.match),
|
|
183
|
+
options.name,
|
|
184
|
+
options.value,
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
return id;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Removes a previously added URL query parameter by its `id`.
|
|
192
|
+
*
|
|
193
|
+
* @param id The identifier passed to/returned from {@link addUrlSearchParam}.
|
|
194
|
+
*/
|
|
195
|
+
removeUrlSearchParam(id: string): void {
|
|
196
|
+
NativeTransformRequestModule.removeUrlSearchParam(id);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Adds or updates an HTTP header identified by `id` that will be sent with all
|
|
201
|
+
* matching map resource requests. Re-adding an existing `id` updates the header in-place.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* // Add header to all requests
|
|
205
|
+
* TransformRequestManager.addHeader({ name: "Authorization", value: "Bearer token123" });
|
|
206
|
+
*
|
|
207
|
+
* // Add header only to requests matching a pattern
|
|
208
|
+
* TransformRequestManager.addHeader({
|
|
209
|
+
* name: "X-API-Key",
|
|
210
|
+
* value: "key123",
|
|
211
|
+
* match: /https:\/\/api\.example\.com\/tiles\//,
|
|
212
|
+
* });
|
|
213
|
+
*
|
|
214
|
+
* @param options The options. Set {@link TransformOptions.id} to a stable string to
|
|
215
|
+
* enable in-place updates; if omitted an id is auto-generated and returned.
|
|
216
|
+
*
|
|
217
|
+
* @returns The id of the options. Pass it to {@link removeHeader} to remove it later.
|
|
218
|
+
*/
|
|
219
|
+
addHeader(options: HeaderOptions): string {
|
|
220
|
+
const id = options.id ?? this.getId();
|
|
221
|
+
|
|
222
|
+
NativeTransformRequestModule.addHeader(
|
|
223
|
+
id,
|
|
224
|
+
TransformRequestManager.toRegexString(options.match),
|
|
225
|
+
options.name,
|
|
226
|
+
options.value,
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
return id;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Removes all registered URL search params.
|
|
234
|
+
*/
|
|
235
|
+
clearUrlSearchParams(): void {
|
|
236
|
+
NativeTransformRequestModule.clearUrlSearchParams();
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Removes a previously added HTTP header by its `id`.
|
|
241
|
+
*
|
|
242
|
+
* @param id The identifier passed to/returned from {@link addHeader}.
|
|
243
|
+
*/
|
|
244
|
+
removeHeader(id: string): void {
|
|
245
|
+
NativeTransformRequestModule.removeHeader(id);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Removes all registered HTTP headers.
|
|
250
|
+
*/
|
|
251
|
+
clearHeaders(): void {
|
|
252
|
+
NativeTransformRequestModule.clearHeaders();
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Removes all registered URL transforms, URL search params and HTTP headers.
|
|
257
|
+
*/
|
|
258
|
+
clear(): void {
|
|
259
|
+
this.clearUrlTransforms();
|
|
260
|
+
this.clearUrlSearchParams();
|
|
261
|
+
this.clearHeaders();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private lastId: number = -1;
|
|
265
|
+
|
|
266
|
+
private getId() {
|
|
267
|
+
this.lastId++;
|
|
268
|
+
|
|
269
|
+
return "transform-request-" + this.lastId;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
private static toRegexString(
|
|
273
|
+
value: string | RegExp | undefined,
|
|
274
|
+
): string | null {
|
|
275
|
+
let result: string | undefined = undefined;
|
|
276
|
+
if (value instanceof RegExp) {
|
|
277
|
+
if (value.flags) {
|
|
278
|
+
throw new Error("Regex flags are not supported");
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
result = value.source;
|
|
282
|
+
} else {
|
|
283
|
+
result = value;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return result || null;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const transformRequestManager = new TransformRequestManager();
|
|
291
|
+
|
|
292
|
+
export { transformRequestManager as TransformRequestManager };
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
package org.maplibre.reactnative.http
|
|
2
|
-
|
|
3
|
-
import android.util.Log
|
|
4
|
-
import okhttp3.Interceptor
|
|
5
|
-
import okhttp3.Response
|
|
6
|
-
import java.io.IOException
|
|
7
|
-
|
|
8
|
-
data class HeaderConfig(
|
|
9
|
-
val value: String,
|
|
10
|
-
val matchRegex: Regex?,
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
class RequestHeadersInterceptor : Interceptor {
|
|
14
|
-
private val requestHeaders: MutableMap<String, HeaderConfig> = HashMap()
|
|
15
|
-
|
|
16
|
-
fun addHeader(
|
|
17
|
-
name: String,
|
|
18
|
-
value: String,
|
|
19
|
-
match: String?,
|
|
20
|
-
) {
|
|
21
|
-
val regex =
|
|
22
|
-
if (match != null) {
|
|
23
|
-
try {
|
|
24
|
-
Regex(match)
|
|
25
|
-
} catch (e: Exception) {
|
|
26
|
-
Log.e(
|
|
27
|
-
"RequestHeadersInterceptor",
|
|
28
|
-
"Invalid regex pattern '$match': ${e.message}",
|
|
29
|
-
)
|
|
30
|
-
null
|
|
31
|
-
}
|
|
32
|
-
} else {
|
|
33
|
-
null
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
requestHeaders[name] = HeaderConfig(value, regex)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
fun removeHeader(name: String) {
|
|
40
|
-
requestHeaders.remove(name)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
@Throws(IOException::class)
|
|
44
|
-
override fun intercept(chain: Interceptor.Chain): Response {
|
|
45
|
-
val modifiedHeaderBuilder = chain.request().newBuilder()
|
|
46
|
-
val requestUrl = chain.request().url.toString()
|
|
47
|
-
|
|
48
|
-
for (entry in requestHeaders.entries) {
|
|
49
|
-
val config = entry.value
|
|
50
|
-
val shouldApply =
|
|
51
|
-
config.matchRegex == null || config.matchRegex.containsMatchIn(requestUrl)
|
|
52
|
-
|
|
53
|
-
if (shouldApply) {
|
|
54
|
-
modifiedHeaderBuilder.addHeader(entry.key, config.value)
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
val request = modifiedHeaderBuilder.build()
|
|
59
|
-
return chain.proceed(request)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
companion object {
|
|
63
|
-
val INSTANCE: RequestHeadersInterceptor = RequestHeadersInterceptor()
|
|
64
|
-
}
|
|
65
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
#import <Foundation/Foundation.h>
|
|
2
|
-
#import <MapLibre/MLNNetworkConfiguration.h>
|
|
3
|
-
|
|
4
|
-
@interface MLRNNetworkHTTPHeaders : NSObject <MLNNetworkConfigurationDelegate>
|
|
5
|
-
|
|
6
|
-
+ (id _Nonnull)sharedInstance;
|
|
7
|
-
|
|
8
|
-
- (void)addRequestHeader:(nonnull NSString *)name
|
|
9
|
-
value:(nonnull NSString *)value
|
|
10
|
-
match:(nullable NSString *)match;
|
|
11
|
-
|
|
12
|
-
- (void)removeRequestHeader:(nonnull NSString *)header;
|
|
13
|
-
|
|
14
|
-
@end
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
#import "MLRNNetworkHTTPHeaders.h"
|
|
2
|
-
#import <MapLibre/MLNNetworkConfiguration.h>
|
|
3
|
-
#import <MapLibre/MapLibre.h>
|
|
4
|
-
|
|
5
|
-
@interface HeaderConfig : NSObject
|
|
6
|
-
@property (nonatomic, strong) NSString *value;
|
|
7
|
-
@property (nonatomic, strong, nullable) NSRegularExpression *matchRegex;
|
|
8
|
-
|
|
9
|
-
- (instancetype)initWithValue:(NSString *)value match:(nullable NSString *)match;
|
|
10
|
-
@end
|
|
11
|
-
|
|
12
|
-
@implementation HeaderConfig
|
|
13
|
-
|
|
14
|
-
- (instancetype)initWithValue:(NSString *)value match:(nullable NSString *)match {
|
|
15
|
-
if (self = [super init]) {
|
|
16
|
-
_value = value;
|
|
17
|
-
|
|
18
|
-
if (match != nil) {
|
|
19
|
-
NSError *error = nil;
|
|
20
|
-
_matchRegex = [NSRegularExpression regularExpressionWithPattern:match options:0 error:&error];
|
|
21
|
-
if (error != nil) {
|
|
22
|
-
NSLog(@"[MLRNNetworkHTTPHeaders] Invalid regex pattern '%@': %@", match,
|
|
23
|
-
error.localizedDescription);
|
|
24
|
-
_matchRegex = nil;
|
|
25
|
-
}
|
|
26
|
-
} else {
|
|
27
|
-
_matchRegex = nil;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return self;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
@end
|
|
34
|
-
|
|
35
|
-
@implementation MLRNNetworkHTTPHeaders {
|
|
36
|
-
NSMutableDictionary<NSString *, HeaderConfig *> *requestHeaders;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
- (instancetype)init {
|
|
40
|
-
if (self = [super init]) {
|
|
41
|
-
requestHeaders = [[NSMutableDictionary alloc] init];
|
|
42
|
-
[[MLNNetworkConfiguration sharedManager] setDelegate:self];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return self;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
+ (id)sharedInstance {
|
|
49
|
-
static MLRNNetworkHTTPHeaders *networkHttpHeaders;
|
|
50
|
-
static dispatch_once_t onceToken;
|
|
51
|
-
|
|
52
|
-
dispatch_once(&onceToken, ^{
|
|
53
|
-
networkHttpHeaders = [[self alloc] init];
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
return networkHttpHeaders;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
- (void)addRequestHeader:(NSString *)name value:(NSString *)value match:(nullable NSString *)match {
|
|
60
|
-
HeaderConfig *config = [[HeaderConfig alloc] initWithValue:value match:match];
|
|
61
|
-
[requestHeaders setObject:config forKey:name];
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
- (void)removeRequestHeader:(NSString *)header {
|
|
65
|
-
[requestHeaders removeObjectForKey:header];
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
#pragma mark - MLNNetworkConfigurationDelegate
|
|
69
|
-
|
|
70
|
-
- (NSMutableURLRequest *)willSendRequest:(NSMutableURLRequest *)request {
|
|
71
|
-
NSDictionary<NSString *, HeaderConfig *> *currentHeaders = [requestHeaders copy];
|
|
72
|
-
|
|
73
|
-
if (currentHeaders != nil && [currentHeaders count] > 0) {
|
|
74
|
-
NSString *requestUrl = request.URL.absoluteString;
|
|
75
|
-
|
|
76
|
-
for (NSString *headerName in currentHeaders) {
|
|
77
|
-
HeaderConfig *config = currentHeaders[headerName];
|
|
78
|
-
BOOL shouldApply = YES;
|
|
79
|
-
|
|
80
|
-
if (config.matchRegex != nil) {
|
|
81
|
-
NSRange range =
|
|
82
|
-
[config.matchRegex rangeOfFirstMatchInString:requestUrl
|
|
83
|
-
options:0
|
|
84
|
-
range:NSMakeRange(0, requestUrl.length)];
|
|
85
|
-
shouldApply = (range.location != NSNotFound);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (shouldApply) {
|
|
89
|
-
[request setValue:config.value forHTTPHeaderField:headerName];
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return request;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
@end
|