mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 22:30:23 +08:00
44 lines
1.8 KiB
Plaintext
44 lines
1.8 KiB
Plaintext
#import <React/RCTHTTPRequestHandler.h>
|
|
|
|
// A dedicated scheme keeps uploads from falling back to RN's redirect-following
|
|
// HTTP handler if a JS bundle is loaded in an older native build.
|
|
@interface DeepTutorUploadHandler : RCTHTTPRequestHandler
|
|
@end
|
|
|
|
@implementation DeepTutorUploadHandler
|
|
RCT_EXPORT_MODULE()
|
|
|
|
- (BOOL)canHandleRequest:(NSURLRequest *)request
|
|
{
|
|
NSString *scheme = request.URL.scheme;
|
|
return [request.HTTPMethod isEqualToString:@"POST"] &&
|
|
([scheme isEqualToString:@"deeptutor-upload-https"] || [scheme isEqualToString:@"deeptutor-upload-http"]);
|
|
}
|
|
|
|
- (id)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequestDelegate>)delegate
|
|
{
|
|
NSURLComponents *url = [NSURLComponents componentsWithURL:request.URL resolvingAgainstBaseURL:NO];
|
|
url.scheme = [url.scheme isEqualToString:@"deeptutor-upload-https"] ? @"https" : @"http";
|
|
NSMutableURLRequest *upload = [request mutableCopy];
|
|
upload.URL = url.URL;
|
|
if (upload.HTTPShouldHandleCookies) {
|
|
NSArray *cookies = [NSHTTPCookieStorage.sharedHTTPCookieStorage cookiesForURL:upload.URL];
|
|
NSString *cookieHeader = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies][@"Cookie"];
|
|
[upload setValue:cookieHeader forHTTPHeaderField:@"Cookie"];
|
|
}
|
|
// Expo's development network inspector runs its own URLSession. Give it
|
|
// the same policy as the release session, as expo/fetch does.
|
|
[NSURLProtocol setProperty:@NO forKey:@"shouldFollowRedirects" inRequest:upload];
|
|
return [super sendRequest:upload withDelegate:delegate];
|
|
}
|
|
|
|
- (void)URLSession:(NSURLSession *)session
|
|
task:(NSURLSessionTask *)task
|
|
willPerformHTTPRedirection:(NSHTTPURLResponse *)response
|
|
newRequest:(NSURLRequest *)request
|
|
completionHandler:(void (^)(NSURLRequest *))completionHandler
|
|
{
|
|
completionHandler(nil);
|
|
}
|
|
@end
|