@@ -19,14 +19,68 @@ - (instancetype)initWithBasePath:(NSString *)basePath andScheme:(NSString *)sche
19
19
20
20
- (void )webView : (WKWebView *)webView startURLSchemeTask : (id <WKURLSchemeTask >)urlSchemeTask
21
21
{
22
+ Boolean loadFile = true ;
22
23
NSString * startPath = @" " ;
23
24
NSURL * url = urlSchemeTask.request .URL ;
24
- NSString * stringToLoad = url.path ;
25
+ NSDictionary * header = urlSchemeTask.request .allHTTPHeaderFields ;
26
+ NSMutableString * stringToLoad = [NSMutableString string ];
27
+ [stringToLoad appendString: url.path];
25
28
NSString * scheme = url.scheme ;
29
+ NSString * method = urlSchemeTask.request .HTTPMethod ;
30
+ NSData * body = urlSchemeTask.request .HTTPBody ;
31
+ NSData * data;
32
+ NSInteger statusCode;
26
33
27
34
if ([scheme isEqualToString: self .scheme]) {
28
35
if ([stringToLoad hasPrefix: @" /_app_file_" ]) {
29
36
startPath = [stringToLoad stringByReplacingOccurrencesOfString: @" /_app_file_" withString: @" " ];
37
+ } else if ([stringToLoad hasPrefix: @" /_http_proxy_" ]||[stringToLoad hasPrefix: @" /_https_proxy_" ]) {
38
+ if (url.query ) {
39
+ [stringToLoad appendString: @" ?" ];
40
+ [stringToLoad appendString: url.query];
41
+ }
42
+ loadFile = false ;
43
+ startPath = [stringToLoad stringByReplacingOccurrencesOfString: @" /_http_proxy_" withString: @" http://" ];
44
+ startPath = [startPath stringByReplacingOccurrencesOfString: @" /_https_proxy_" withString: @" https://" ];
45
+ NSURL * requestUrl = [NSURL URLWithString: startPath];
46
+ WKWebsiteDataStore * dataStore = [WKWebsiteDataStore defaultDataStore ];
47
+ WKHTTPCookieStore * cookieStore = dataStore.httpCookieStore ;
48
+ NSMutableURLRequest *request = [[NSMutableURLRequest alloc ] init ];
49
+ [request setHTTPMethod: method];
50
+ [request setURL: requestUrl];
51
+ if (body) {
52
+ [request setHTTPBody: body];
53
+ }
54
+ [request setAllHTTPHeaderFields: header];
55
+ [request setHTTPShouldHandleCookies: YES ];
56
+
57
+ [[[NSURLSession sharedSession ] dataTaskWithRequest: request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
58
+ if (error) {
59
+ NSLog (@" Proxy error: %@ " , error);
60
+ }
61
+
62
+ // set cookies to WKWebView
63
+ NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
64
+ if (httpResponse) {
65
+ NSArray * cookies = [NSHTTPCookie cookiesWithResponseHeaderFields: [httpResponse allHeaderFields ] forURL: response.URL];
66
+ [[NSHTTPCookieStorage sharedHTTPCookieStorage ] setCookies: cookies forURL: httpResponse.URL mainDocumentURL: nil ];
67
+ cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage ] cookies ];
68
+
69
+ for (NSHTTPCookie * c in cookies)
70
+ {
71
+ dispatch_async (dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^(void ){
72
+ // running in background thread is necessary because setCookie otherwise fails
73
+ dispatch_async (dispatch_get_main_queue (), ^(void ){
74
+ [cookieStore setCookie: c completionHandler: nil ];
75
+ });
76
+ });
77
+ };
78
+ }
79
+
80
+ [urlSchemeTask didReceiveResponse: response];
81
+ [urlSchemeTask didReceiveData: data];
82
+ [urlSchemeTask didFinish ];
83
+ }] resume ];
30
84
} else {
31
85
startPath = self.basePath ;
32
86
if ([stringToLoad isEqualToString: @" " ] || [url.pathExtension isEqualToString: @" " ]) {
@@ -37,25 +91,26 @@ - (void)webView:(WKWebView *)webView startURLSchemeTask:(id <WKURLSchemeTask>)ur
37
91
}
38
92
}
39
93
40
- NSData * data = [[NSData alloc ] initWithContentsOfFile: startPath];
41
- NSInteger statusCode = 200 ;
42
- if (!data) {
43
- statusCode = 404 ;
44
- }
45
- NSURL * localUrl = [NSURL URLWithString: url.absoluteString];
46
- NSString * mimeType = [self getMimeType: url.pathExtension];
47
- id response = nil ;
48
- if (data && [self isMediaExtension: url.pathExtension]) {
49
- response = [[NSURLResponse alloc ] initWithURL: localUrl MIMEType: mimeType expectedContentLength: data.length textEncodingName: nil ];
50
- } else {
51
- NSDictionary * headers = @{ @" Content-Type" : mimeType, @" Cache-Control" : @" no-cache" };
52
- response = [[NSHTTPURLResponse alloc ] initWithURL: localUrl statusCode: statusCode HTTPVersion: nil headerFields: headers];
94
+ if (loadFile) {
95
+ data = [[NSData alloc ] initWithContentsOfFile: startPath];
96
+ statusCode = 200 ;
97
+ if (!data) {
98
+ statusCode = 404 ;
99
+ }
100
+ NSURL * localUrl = [NSURL URLWithString: url.absoluteString];
101
+ NSString * mimeType = [self getMimeType: url.pathExtension];
102
+ id response = nil ;
103
+ if (data && [self isMediaExtension: url.pathExtension]) {
104
+ response = [[NSURLResponse alloc ] initWithURL: localUrl MIMEType: mimeType expectedContentLength: data.length textEncodingName: nil ];
105
+ } else {
106
+ NSDictionary * headers = @{ @" Content-Type" : mimeType, @" Cache-Control" : @" no-cache" };
107
+ response = [[NSHTTPURLResponse alloc ] initWithURL: localUrl statusCode: statusCode HTTPVersion: nil headerFields: headers];
108
+ }
109
+
110
+ [urlSchemeTask didReceiveResponse: response];
111
+ [urlSchemeTask didReceiveData: data];
112
+ [urlSchemeTask didFinish ];
53
113
}
54
-
55
- [urlSchemeTask didReceiveResponse: response];
56
- [urlSchemeTask didReceiveData: data];
57
- [urlSchemeTask didFinish ];
58
-
59
114
}
60
115
61
116
- (void )webView : (nonnull WKWebView *)webView stopURLSchemeTask : (nonnull id <WKURLSchemeTask >)urlSchemeTask
0 commit comments