-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxyURLProtocol.swift
More file actions
59 lines (44 loc) · 2.17 KB
/
ProxyURLProtocol.swift
File metadata and controls
59 lines (44 loc) · 2.17 KB
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
//
// ProxyURLProtocol.swift
// HQTVInjection
//
// Created by Rus Price on 10/2/24.
//
import Foundation
@objc
public class ProxyURLProtocol: URLProtocol {
public static override func canInit(with request: URLRequest) -> Bool {
NSLog("[HQTVInjection] canInit with request URl: \(request.url!.absoluteString), host is: \(request.url?.absoluteURL.host ?? "nil")");
return request.url?.absoluteURL.host == "api-quiz.hype.space"
}
static override public func canonicalRequest(for request: URLRequest) -> URLRequest {
return request
}
public override func startLoading() {
guard let originalURL = request.url else { return }
let transformedURL = URL(string: "https://play.question.house\(originalURL.relativePath)")
NSLog("[HQTVInjection] transformedURL: \(transformedURL!)");
var newRequest = URLRequest(url: transformedURL!);
newRequest.httpMethod = request.httpMethod;
NSLog("[HQTVInjection] Original Body: \(String(describing: request.httpBody))");
newRequest.httpBodyStream = request.httpBodyStream;
NSLog("[HQTVInjection] New Body: \(String(describing: newRequest.httpBody))");
let originalHeaders = request.allHTTPHeaderFields;
for (header, value) in originalHeaders! {
newRequest.setValue(value, forHTTPHeaderField: header)
}
let newTask = URLSession.shared.dataTask(with: newRequest) { data, response, error in
if let error {
NSLog("[HQTVInjection] Error: \(error.localizedDescription)")
self.client?.urlProtocol(self, didFailWithError: error);
}
NSLog("[HQTVInjection] Body response for URL \(originalURL.absoluteString): \(String(decoding: data!, as: Unicode.UTF8.self))");
self.client?.urlProtocol(self, didReceive: response!, cacheStoragePolicy: .notAllowed);
self.client?.urlProtocol(self, didLoad: data!);
self.client?.urlProtocolDidFinishLoading(self);
}
newTask.resume()
}
override public func stopLoading() {
}
}