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