Skip to content
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

@ionic-native/httpd plugin wrapper compatibility for use with Capacitor #79

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.settings
.idea
*.iml
*~
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## CorHttpd: embeded httpd for cordova ##
## CorHttpd: embeded httpd for cordova & capacitor ##

Supported platform:
* iOS
Expand All @@ -9,6 +9,7 @@ You can browse (locally or remotely) to access files in android/ios phone/pad:
* browse the files in mobile device with a browser in PC.
* copy files from mobile device to PC quickly, just with Wifi.
* use cordova webview to access the assets/www/ content with http protocol.
* if using capacitor, set `www_dir_name` to `public`.

Why http access is good?

Expand All @@ -18,13 +19,19 @@ Why http access is good?

## How to use CorHttpd? ##

**Cordova**

Add the plugin to your cordova project:

cordova plugin add https://github.com/floatinghotpot/cordova-httpd.git

Quick start, copy the demo files, and just build to play.

cp -r plugins/com.rjfun.cordova.httpd/test/* www/

**Capacitor**

Follow these [instructions](https://ionicframework.com/docs/native/httpd/).

## Javascript APIs ##

Expand Down Expand Up @@ -75,13 +82,15 @@ Example code: (read the comments)
if(url.length > 0) {
document.getElementById('url').innerHTML = "server is up: <a href='" + url + "' target='_blank'>" + url + "</a>";
} else {
/* wwwroot is the root dir of web server, it can be absolute or relative path
* if a relative path is given, it will be relative to cordova assets/www/ in APK.
* "", by default, it will point to cordova assets/www/, it's good to use 'htdocs' for 'www/htdocs'
* if a absolute path is given, it will access file system.
* "/", set the root dir as the www root, it maybe a security issue, but very powerful to browse all dir
/* For capacitor users, set www_dir_name to "public". For Cordova users the default is "www".
* wwwroot is the root dir of web server and child dir of www_dir_name, it can be an absolute or relative path
* if a relative path is given, it will be relative to assets/www/ for cordova users or public for capacitor users, depending on the value of www_dir_name.
* if it is "", by default, it will point to cordova assets/www/ or capacitor public, depending on the value of www_dir_name. For cordova, it's good to use 'htdocs' for 'www/htdocs'
* if an absolute path is given, it will access the file system, even if www_dir_name is set.
* "/", set the root dir as the www root even if www_dir_name is set, it maybe a security issue, but very powerful to browse all dir
*/
httpd.startServer({
'www_dir_name': "www",
'www_root' : wwwroot,
'port' : 8080,
'localhost_only' : false
Expand Down
14 changes: 10 additions & 4 deletions src/ios/CorHttpd.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

@interface CorHttpd : CDVPlugin {
// Member variables go here.

}

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

@property (nonatomic, retain) NSString* www_dir_name;
@property (nonatomic, retain) NSString* www_root;
@property (assign) int port;
@property (assign) BOOL localhost_only;
Expand All @@ -40,6 +40,7 @@ @implementation CorHttpd
#define IP_ADDR_IPv4 @"ipv4"
#define IP_ADDR_IPv6 @"ipv6"

#define OPT_WWW_DIR_NAME @"www_dir_name"
#define OPT_WWW_ROOT @"www_root"
#define OPT_PORT @"port"
#define OPT_LOCALHOST_ONLY @"localhost_only"
Expand Down Expand Up @@ -111,6 +112,7 @@ - (void)pluginInitialize
self.localPath = @"";
self.url = @"";

self.www_dir_name = @"www";
self.www_root = @"";
self.port = 8888;
self.localhost_only = false;
Expand All @@ -122,7 +124,10 @@ - (void)startServer:(CDVInvokedUrlCommand*)command

NSDictionary* options = [command.arguments objectAtIndex:0];

NSString* str = [options valueForKey:OPT_WWW_ROOT];
NSString* str = [options valueForKey:OPT_WWW_DIR_NAME];
if(str) self.www_dir_name = str;

str = [options valueForKey:OPT_WWW_ROOT];
if(str) self.www_root = str;

str = [options valueForKey:OPT_PORT];
Expand Down Expand Up @@ -156,11 +161,12 @@ - (void)startServer:(CDVInvokedUrlCommand*)command
if(self.localhost_only) [self.httpServer setInterface:IP_LOCALHOST];

// Serve files from our embedded Web folder
const char * root = [self.www_dir_name UTF8String];
const char * docroot = [self.www_root UTF8String];
if(*docroot == '/') {
if(*docroot == '/' || *root == '/') {
self.localPath = self.www_root;
} else {
NSString* basePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"www"];
NSString* basePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:self.www_dir_name];
self.localPath = [NSString stringWithFormat:@"%@/%@", basePath, self.www_root];
}
NSLog(@"Setting document root: %@", self.localPath);
Expand Down
40 changes: 23 additions & 17 deletions www/CorHttpd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,38 @@ var argscheck = require('cordova/argscheck'),
var corhttpd_exports = {};

corhttpd_exports.startServer = function(options, success, error) {
var defaults = {
'www_root': '',
'port': 8888,
'localhost_only': false
};

// Merge optional settings into defaults.
for (var key in defaults) {
if (typeof options[key] !== 'undefined') {
defaults[key] = options[key];
}
}

exec(success, error, "CorHttpd", "startServer", [ defaults ]);
var defaults = {
// For Cordova this is `www`. For Capacitor it's `public`.
'www_dir_name': 'www',
'www_root': '',
'port': 8888,
'localhost_only': false
};

// Merge optional settings into defaults.
for (var key in defaults) {
if (typeof options[key] !== 'undefined') {
defaults[key] = options[key];
}
}

exec(success, error, "CorHttpd", "startServer", [defaults]);
};

corhttpd_exports.stopServer = function(success, error) {
exec(success, error, "CorHttpd", "stopServer", []);
exec(success, error, "CorHttpd", "stopServer", []);
};

corhttpd_exports.getURL = function(success, error) {
exec(success, error, "CorHttpd", "getURL", []);
exec(success, error, "CorHttpd", "getURL", []);
};

// Compatibility for @ionic-native/httpd plugin. Keep this until the following issue is resolved:
// https://github.com/danielsogl/awesome-cordova-plugins/issues/3805
corhttpd_exports.getUrl = corhttpd_exports.getURL

corhttpd_exports.getLocalPath = function(success, error) {
exec(success, error, "CorHttpd", "getLocalPath", []);
exec(success, error, "CorHttpd", "getLocalPath", []);
};

module.exports = corhttpd_exports;
Expand Down