@@ -19,35 +19,44 @@ - (instancetype)initWithBasePath:(NSString *)basePath andScheme:(NSString *)sche
1919
2020- (void )webView : (WKWebView *)webView startURLSchemeTask : (id <WKURLSchemeTask >)urlSchemeTask
2121{
22+ Boolean loadFile = true ;
2223 NSString * startPath = @" " ;
2324 NSURL * url = urlSchemeTask.request .URL ;
25+ NSDictionary * header = urlSchemeTask.request .allHTTPHeaderFields ;
2426 NSString * stringToLoad = url.path ;
2527 NSString * scheme = url.scheme ;
2628 NSString * method = urlSchemeTask.request .HTTPMethod ;
29+ NSData * body = urlSchemeTask.request .HTTPBody ;
2730 NSData * data;
2831 NSInteger statusCode;
2932
3033 if ([scheme isEqualToString: self .scheme]) {
3134 if ([stringToLoad hasPrefix: @" /_app_file_" ]) {
3235 startPath = [stringToLoad stringByReplacingOccurrencesOfString: @" /_app_file_" withString: @" " ];
3336 } else if ([stringToLoad hasPrefix: @" /_http_proxy_" ]||[stringToLoad hasPrefix: @" /_https_proxy_" ]) {
37+ loadFile = false ;
3438 startPath = [stringToLoad stringByReplacingOccurrencesOfString: @" /_http_proxy_" withString: @" http://" ];
35- startPath = [stringToLoad stringByReplacingOccurrencesOfString: @" /_https_proxy_" withString: @" https://" ];
39+ // startPath = [stringToLoad stringByReplacingOccurrencesOfString:@"/_https_proxy_" withString:@"https://"];
3640 NSLog (@" Proxy %@ " , startPath);
3741 NSMutableURLRequest *request = [[NSMutableURLRequest alloc ] init ];
3842 [request setHTTPMethod: method];
3943 [request setURL: [NSURL URLWithString: startPath]];
40- [request setAllHTTPHeaderFields: [NSHTTPCookie requestHeaderFieldsWithCookies: [NSHTTPCookieStorage sharedHTTPCookieStorage ].cookies]];
41- [request HTTPShouldHandleCookies ];
42-
43- NSError *error = nil ;
44- NSHTTPURLResponse *responseCode = nil ;
45-
46- data = [NSURLConnection sendSynchronousRequest: request returningResponse: &responseCode error: &error];
47- statusCode = [responseCode statusCode ];
48- if (!error) {
49- NSLog (@" %@ " , error);
44+ if (body) {
45+ [request setHTTPBody: body];
5046 }
47+ [request setAllHTTPHeaderFields: header];
48+ [request setAllHTTPHeaderFields: [NSHTTPCookie requestHeaderFieldsWithCookies: [NSHTTPCookieStorage sharedHTTPCookieStorage ].cookies]];
49+ // [request setHTTPShouldHandleCookies:YES];
50+
51+ [[[NSURLSession sharedSession ] dataTaskWithRequest: request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
52+ if (error) {
53+ NSLog (@" Proxy error: %@ " , error);
54+ }
55+
56+ [urlSchemeTask didReceiveResponse: response];
57+ [urlSchemeTask didReceiveData: data];
58+ [urlSchemeTask didFinish ];
59+ }] resume ];
5160 } else {
5261 startPath = self.basePath ;
5362 if ([stringToLoad isEqualToString: @" " ] || [url.pathExtension isEqualToString: @" " ]) {
@@ -58,27 +67,26 @@ - (void)webView:(WKWebView *)webView startURLSchemeTask:(id <WKURLSchemeTask>)ur
5867 }
5968 }
6069
61- if (!data ) {
70+ if (loadFile ) {
6271 data = [[NSData alloc ] initWithContentsOfFile: startPath];
72+ statusCode = 200 ;
73+ if (!data) {
74+ statusCode = 404 ;
75+ }
76+ NSURL * localUrl = [NSURL URLWithString: url.absoluteString];
77+ NSString * mimeType = [self getMimeType: url.pathExtension];
78+ id response = nil ;
79+ if (data && [self isMediaExtension: url.pathExtension]) {
80+ response = [[NSURLResponse alloc ] initWithURL: localUrl MIMEType: mimeType expectedContentLength: data.length textEncodingName: nil ];
81+ } else {
82+ NSDictionary * headers = @{ @" Content-Type" : mimeType, @" Cache-Control" : @" no-cache" };
83+ response = [[NSHTTPURLResponse alloc ] initWithURL: localUrl statusCode: statusCode HTTPVersion: nil headerFields: headers];
84+ }
85+
86+ [urlSchemeTask didReceiveResponse: response];
87+ [urlSchemeTask didReceiveData: data];
88+ [urlSchemeTask didFinish ];
6389 }
64- statusCode = 200 ;
65- if (!data) {
66- statusCode = 404 ;
67- }
68- NSURL * localUrl = [NSURL URLWithString: url.absoluteString];
69- NSString * mimeType = [self getMimeType: url.pathExtension];
70- id response = nil ;
71- if (data && [self isMediaExtension: url.pathExtension]) {
72- response = [[NSURLResponse alloc ] initWithURL: localUrl MIMEType: mimeType expectedContentLength: data.length textEncodingName: nil ];
73- } else {
74- NSDictionary * headers = @{ @" Content-Type" : mimeType, @" Cache-Control" : @" no-cache" };
75- response = [[NSHTTPURLResponse alloc ] initWithURL: localUrl statusCode: statusCode HTTPVersion: nil headerFields: headers];
76- }
77-
78- [urlSchemeTask didReceiveResponse: response];
79- [urlSchemeTask didReceiveData: data];
80- [urlSchemeTask didFinish ];
81-
8290}
8391
8492- (void )webView : (nonnull WKWebView *)webView stopURLSchemeTask : (nonnull id <WKURLSchemeTask >)urlSchemeTask
0 commit comments