Skip to content

Commit ee160c7

Browse files
committed
supports CORS in iOS
1 parent fd81ea4 commit ee160c7

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ my-release-key.keystore
5151
StaticServerExample/my-release-key.keystore
5252

5353
# Android
54+
.classpath
5455
.project
5556
.settings/
5657
android/.project

Diff for: StaticServerExample/App.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface ITestViewProps {
2424
port?: number
2525
root?: string
2626
file?: string
27+
target?: any
2728
}
2829

2930
export default function App (props: ITestViewProps): JSX.Element {
@@ -32,11 +33,12 @@ export default function App (props: ITestViewProps): JSX.Element {
3233
const port = typeof props.port !== 'undefined' ? props.port : 3030
3334
const root = typeof props.root !== 'undefined' ? props.root : 'www/'
3435
const file = typeof props.file !== 'undefined' ? props.file : 'index.html'
36+
const target = typeof props.target !== 'undefined' ? props.target : require('./index.html')
3537

3638
useEffect(() => {
3739
if (origin === '') {
3840
// eslint-disable-next-line @typescript-eslint/no-var-requires
39-
const index = require('./index.html')
41+
const index = target
4042
const { uri } = Image.resolveAssetSource(index)
4143
const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + root
4244
const dest = path + file

Diff for: ios/FPStaticServer.h

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
// GCDWebServer: https://github.com/swisspol/GCDWebServer
44
#import "GCDWebServer.h"
5+
#import "GCDWebServerFunctions.h"
6+
#import "GCDWebServerFileResponse.h"
7+
#import "GCDWebServerHTTPStatusCodes.h"
58

69
@interface FPStaticServer : NSObject <RCTBridgeModule> {
710
GCDWebServer* _webServer;

Diff for: ios/FPStaticServer.m

+49-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,55 @@ - (dispatch_queue_t)methodQueue
7373
return;
7474
}
7575

76-
[_webServer addGETHandlerForBasePath:@"/" directoryPath:self.www_root indexFilename:@"index.html" cacheAge:3600 allowRangeRequests:YES];
76+
//[_webServer addGETHandlerForBasePath:@"/" directoryPath:self.www_root indexFilename:@"index.html" cacheAge:3600 allowRangeRequests:YES];
77+
NSString *basePath = @"/";
78+
NSString *directoryPath = self.www_root;
79+
NSString *indexFilename = @"index.html";
80+
NSUInteger cacheAge = 3600;
81+
BOOL allowRangeRequests = YES;
82+
[_webServer addHandlerWithMatchBlock:^GCDWebServerRequest*(NSString* requestMethod, NSURL* requestURL, NSDictionary<NSString*, NSString*>* requestHeaders, NSString* urlPath, NSDictionary<NSString*, NSString*>* urlQuery) {
83+
if (![requestMethod isEqualToString:@"GET"]) {
84+
return nil;
85+
}
86+
if (![urlPath hasPrefix:basePath]) {
87+
return nil;
88+
}
89+
return [[GCDWebServerRequest alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];
90+
}
91+
processBlock:^GCDWebServerResponse*(GCDWebServerRequest* request) {
92+
GCDWebServerResponse* response = nil;
93+
NSString* filePath = [directoryPath stringByAppendingPathComponent:GCDWebServerNormalizePath([request.path substringFromIndex:basePath.length])];
94+
NSString* fileType = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:NULL] fileType];
95+
if (fileType) {
96+
if ([fileType isEqualToString:NSFileTypeDirectory]) {
97+
if (indexFilename) {
98+
NSString* indexPath = [filePath stringByAppendingPathComponent:indexFilename];
99+
NSString* indexType = [[[NSFileManager defaultManager] attributesOfItemAtPath:indexPath error:NULL] fileType];
100+
if ([indexType isEqualToString:NSFileTypeRegular]) {
101+
response = [GCDWebServerFileResponse responseWithFile:indexPath];
102+
}
103+
} else {
104+
response = [GCDWebServerResponse responseWithStatusCode:kGCDWebServerHTTPStatusCode_NotFound];
105+
}
106+
} else if ([fileType isEqualToString:NSFileTypeRegular]) {
107+
if (allowRangeRequests) {
108+
response = [GCDWebServerFileResponse responseWithFile:filePath byteRange:request.byteRange];
109+
[response setValue:@"bytes" forAdditionalHeader:@"Accept-Ranges"];
110+
} else {
111+
response = [GCDWebServerFileResponse responseWithFile:filePath];
112+
}
113+
}
114+
}
115+
if (response) {
116+
response.cacheControlMaxAge = cacheAge;
117+
[response setValue:@"GET" forAdditionalHeader:@"Access-Control-Request-Method"];
118+
[response setValue:@"OriginX-Requested-With, Content-Type, Accept, Cache-Control, Range,Access-Control-Allow-Origin" forAdditionalHeader:@"Access-Control-Request-Headers"];
119+
[response setValue: @"*" forAdditionalHeader:@"Access-Control-Allow-Origin"];
120+
} else {
121+
response = [GCDWebServerResponse responseWithStatusCode:kGCDWebServerHTTPStatusCode_NotFound];
122+
}
123+
return response;
124+
}];
77125

78126
NSError *error;
79127
NSMutableDictionary* options = [NSMutableDictionary dictionary];

0 commit comments

Comments
 (0)