Skip to content

Ability to override Content-Type header for responses #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ declare module 'react-native-static-server' {
type Options = {
localOnly?: boolean
keepAlive?: boolean
}
mimeTypeOverrides?: Record<string, string>
};

export default class StaticServer {
constructor(port: number, root?: string, opts?: Options)
Expand All @@ -13,10 +14,11 @@ declare module 'react-native-static-server' {
keepAlive: boolean
started: boolean
_origin?: string
mimeTypeOverrides: Record<string, string>

start: () => Promise<string>
stop: () => Promise<any>
isRunning: () => Promise<boolean>
kill: () => void
}
}
}
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
NativeModules,
AppState,
Platform
} from 'react-native';
} from 'react-native';

const { FPStaticServer } = NativeModules;

Expand All @@ -18,17 +18,20 @@ class StaticServer {
this.root = root || ROOT;
this.localOnly = (opts && opts.localOnly) || false;
this.keepAlive = (opts && opts.keepAlive) || false;
this.mimeTypeOverrides = opts.mimeTypeOverrides || null;
break;
case 2:
this.port = `${port}`;
if (typeof(arguments[1]) === 'string') {
this.root = root;
this.localOnly = false;
this.keepAlive = false;
this.mimeTypeOverrides = null;
} else {
this.root = ROOT;
this.localOnly = (arguments[1] && arguments[1].localOnly) || false;
this.keepAlive = (arguments[1] && arguments[1].keepAlive) || false;
this.mimeTypeOverrides = arguments[1].mimeTypeOverrides || null;
}
break;
case 1:
Expand All @@ -37,18 +40,21 @@ class StaticServer {
this.root = ROOT;
this.localOnly = false;
this.keepAlive = false;
this.mimeTypeOverrides = null;
} else {
this.port = PORT;
this.root = ROOT;
this.localOnly = (arguments[0] && arguments[0].localOnly) || false;
this.keepAlive = (arguments[0] && arguments[0].keepAlive) || false;
this.mimeTypeOverrides = arguments[0].mimeTypeOverrides || null;
}
break;
default:
this.port = PORT;
this.root = ROOT;
this.localOnly = false;
this.keepAlive = false;
this.mimeTypeOverrides = null;
}


Expand All @@ -58,7 +64,7 @@ class StaticServer {
}

start() {
if( this.running ){
if( this.running ) {
return Promise.resolve(this.origin);
}

Expand All @@ -69,7 +75,7 @@ class StaticServer {
AppState.addEventListener('change', this._handleAppStateChangeFn);
}

return FPStaticServer.start(this.port, this.root, this.localOnly, this.keepAlive)
return FPStaticServer.start(this.port, this.root, this.localOnly, this.keepAlive, this.mimeTypeOverrides)
.then((origin) => {
this._origin = origin;
return origin;
Expand Down
12 changes: 6 additions & 6 deletions ios/FPStaticServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
#import "GCDWebServerHTTPStatusCodes.h"

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

@property(nonatomic, retain) NSString *localPath;
@property(nonatomic, retain) NSString *url;

@property (nonatomic, retain) NSString* www_root;
@property (nonatomic, retain) NSNumber* port;
@property (assign) BOOL localhost_only;
@property (assign) BOOL keep_alive;
@property(nonatomic, retain) NSString *www_root;
@property(nonatomic, retain) NSNumber *port;
@property(nonatomic, retain) NSDictionary<NSString *, NSString *> *mime_type_overrides;
@property(assign) BOOL localhost_only;
@property(assign) BOOL keep_alive;

@end

9 changes: 6 additions & 3 deletions ios/FPStaticServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ - (dispatch_queue_t)methodQueue
root:(NSString *)optroot
localOnly:(BOOL *)localhost_only
keepAlive:(BOOL *)keep_alive
mimeTypeOverrides:(NSDictionary *)mime_type_overrides
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {

Expand Down Expand Up @@ -67,12 +68,14 @@ - (dispatch_queue_t)methodQueue

self.localhost_only = localhost_only;

self.mime_type_overrides = mime_type_overrides;

if(_webServer.isRunning != NO) {
NSLog(@"StaticServer already running at %@", self.url);
resolve(self.url);
return;
}

//[_webServer addGETHandlerForBasePath:@"/" directoryPath:self.www_root indexFilename:@"index.html" cacheAge:3600 allowRangeRequests:YES];
NSString *basePath = @"/";
NSString *directoryPath = self.www_root;
Expand Down Expand Up @@ -105,10 +108,10 @@ - (dispatch_queue_t)methodQueue
}
} else if ([fileType isEqualToString:NSFileTypeRegular]) {
if (allowRangeRequests) {
response = [GCDWebServerFileResponse responseWithFile:filePath byteRange:request.byteRange];
response = [[GCDWebServerFileResponse alloc] initWithFile:filePath byteRange:request.byteRange isAttachment:NO mimeTypeOverrides:mime_type_overrides] ;
[response setValue:@"bytes" forAdditionalHeader:@"Accept-Ranges"];
} else {
response = [GCDWebServerFileResponse responseWithFile:filePath];
response = [[GCDWebServerFileResponse alloc] initWithFile:filePath byteRange:NSMakeRange(NSUIntegerMax, 0) isAttachment:NO mimeTypeOverrides:mime_type_overrides];
}
}
}
Expand Down