Compare commits
8 Commits
dependabot
...
master
Author | SHA1 | Date | |
---|---|---|---|
13586d2514 | |||
d88b4a509d | |||
e17c4d002c | |||
ce067a1e89 | |||
00adabeb3f | |||
5bb01739d7 | |||
2d41148067 | |||
185a34d196 |
@ -27,6 +27,8 @@
|
||||
</platform>
|
||||
|
||||
<platform name="ios">
|
||||
<hook type="after_platform_add" src="scripts/re-add.js" />
|
||||
<hook type="after_plugin_add" src="scripts/re-add.js" />
|
||||
<config-file target="config.xml" parent="/*">
|
||||
<feature name="Webserver">
|
||||
<param name="ios-package" value="Webserver" />
|
||||
@ -42,10 +44,10 @@
|
||||
|
||||
<podspec>
|
||||
<config>
|
||||
<source url="https://github.com/CocoaPods/Specs.git"/>
|
||||
<source url="https://m.shuto.cn:8681/github/CocoaPods-Specs.git"/>
|
||||
</config>
|
||||
<pods use-frameworks="true">
|
||||
<pod name="GCDWebServer" spec="~> 3.5.2"/>
|
||||
<pod name="GCDWebServer" git="https://m.shuto.cn:8681/github/GCDWebServer.git"/>
|
||||
</pods>
|
||||
</podspec>
|
||||
|
||||
|
37
scripts/re-add.js
Normal file
37
scripts/re-add.js
Normal file
@ -0,0 +1,37 @@
|
||||
/**
|
||||
* ~ Copyright (c) 2023 Beijing Shuto Technology Co,. Ltd. All rights reserved.
|
||||
*/
|
||||
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
var child_process = require("child_process");
|
||||
|
||||
/**
|
||||
* 读取文件内容,然后替换部分内容后写回
|
||||
* @param file
|
||||
* @param callback
|
||||
*/
|
||||
function replaceContents(file, callback) {
|
||||
var data = fs.readFileSync(file, 'utf8');
|
||||
data = callback(data);
|
||||
fs.writeFileSync(file,data,'utf8');
|
||||
}
|
||||
|
||||
/**
|
||||
* 一些自定义操作
|
||||
* @param ctx
|
||||
*/
|
||||
module.exports = function (ctx) {
|
||||
console.log('======================webServer pod re-add hook==========================');
|
||||
var rootDir = ctx.opts.projectRoot;
|
||||
var project = path.join(rootDir, 'platforms/ios');
|
||||
if (fs.existsSync(project)) {
|
||||
replaceContents(path.join(project, 'Podfile'), (content) => {
|
||||
if (content.indexOf('GCDWebServer') < 0) {
|
||||
content = content.replace("\nend", "\n pod 'GCDWebServer', :git => 'https://m.shuto.cn:8681/github/GCDWebServer.git'\nend")
|
||||
}
|
||||
return content;
|
||||
});
|
||||
child_process.execSync("pod install", { cwd: project });
|
||||
}
|
||||
};
|
@ -20,6 +20,7 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import fi.iki.elonen.NanoHTTPD;
|
||||
import fi.iki.elonen.NanoHTTPD.Response.Status;
|
||||
|
||||
public class NanoHTTPDWebserver extends NanoHTTPD {
|
||||
|
||||
@ -83,7 +84,7 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
|
||||
|
||||
private Response newFixedFileResponse(File file, String mime) throws FileNotFoundException {
|
||||
Response res;
|
||||
res = newFixedLengthResponse(Response.Status.OK, mime, new FileInputStream(file), (int) file.length());
|
||||
res = newFixedLengthResponse(Status.OK, mime, new FileInputStream(file), (int) file.length());
|
||||
res.addHeader("Accept-Ranges", "bytes");
|
||||
return res;
|
||||
}
|
||||
@ -132,7 +133,7 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
|
||||
// and the startFrom of the range is satisfiable
|
||||
// would return range from file
|
||||
// respond with not-modified
|
||||
res = newFixedLengthResponse(Response.Status.NOT_MODIFIED, mime, "");
|
||||
res = newFixedLengthResponse(Status.NOT_MODIFIED, mime, "");
|
||||
res.addHeader("ETag", etag);
|
||||
} else {
|
||||
if (endAt < 0) {
|
||||
@ -146,7 +147,7 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
fis.skip(startFrom);
|
||||
|
||||
res = newFixedLengthResponse(Response.Status.PARTIAL_CONTENT, mime, fis, newLen);
|
||||
res = newFixedLengthResponse(Status.PARTIAL_CONTENT, mime, fis, newLen);
|
||||
res.addHeader("Accept-Ranges", "bytes");
|
||||
res.addHeader("Content-Length", "" + newLen);
|
||||
res.addHeader("Content-Range", "bytes " + startFrom + "-" + endAt + "/" + fileLen);
|
||||
@ -157,21 +158,21 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
|
||||
if (headerIfRangeMissingOrMatching && range != null && startFrom >= fileLen) {
|
||||
// return the size of the file
|
||||
// 4xx responses are not trumped by if-none-match
|
||||
res = newFixedLengthResponse(Response.Status.RANGE_NOT_SATISFIABLE, NanoHTTPD.MIME_PLAINTEXT, "");
|
||||
res = newFixedLengthResponse(Status.RANGE_NOT_SATISFIABLE, NanoHTTPD.MIME_PLAINTEXT, "");
|
||||
res.addHeader("Content-Range", "bytes */" + fileLen);
|
||||
res.addHeader("ETag", etag);
|
||||
} else if (range == null && headerIfNoneMatchPresentAndMatching) {
|
||||
// full-file-fetch request
|
||||
// would return entire file
|
||||
// respond with not-modified
|
||||
res = newFixedLengthResponse(Response.Status.NOT_MODIFIED, mime, "");
|
||||
res = newFixedLengthResponse(Status.NOT_MODIFIED, mime, "");
|
||||
res.addHeader("ETag", etag);
|
||||
} else if (!headerIfRangeMissingOrMatching && headerIfNoneMatchPresentAndMatching) {
|
||||
// range request that doesn't match current etag
|
||||
// would return entire (different) file
|
||||
// respond with not-modified
|
||||
|
||||
res = newFixedLengthResponse(Response.Status.NOT_MODIFIED, mime, "");
|
||||
res = newFixedLengthResponse(Status.NOT_MODIFIED, mime, "");
|
||||
res.addHeader("ETag", etag);
|
||||
} else {
|
||||
// supply the file
|
||||
@ -181,7 +182,7 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
res = newFixedLengthResponse(Response.Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, ioe.getMessage());
|
||||
res = newFixedLengthResponse(Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, ioe.getMessage());
|
||||
}
|
||||
|
||||
return res;
|
||||
@ -250,8 +251,30 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
|
||||
return response;
|
||||
} else {
|
||||
try {
|
||||
response = newFixedLengthResponse(
|
||||
Response.Status.lookup(responseObject.getInt("status")),
|
||||
Response.IStatus status = Status.lookup(responseObject.getInt("status"));
|
||||
if(status == null){
|
||||
status = new Response.IStatus(){
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
try {
|
||||
return ""+this.getRequestStatus()+" "+responseObject.getString("body");
|
||||
} catch (JSONException e) {
|
||||
return ""+this.getRequestStatus()+" "+"Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRequestStatus() {
|
||||
try {
|
||||
return responseObject.getInt("status");
|
||||
} catch (JSONException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
response = newFixedLengthResponse(status,
|
||||
getContentType(responseObject),
|
||||
responseObject.getString("body")
|
||||
);
|
||||
|
@ -26,24 +26,32 @@ public class Webserver extends CordovaPlugin {
|
||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||
if ("start".equals(action)) {
|
||||
try {
|
||||
this.start(args, callbackContext);
|
||||
this.start(args, callbackContext);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if ("stop".equals(action)) {
|
||||
}
|
||||
if ("stop".equals(action)) {
|
||||
this.stop(args, callbackContext);
|
||||
return true;
|
||||
}
|
||||
if ("onRequest".equals(action)) {
|
||||
}
|
||||
if ("onRequest".equals(action)) {
|
||||
this.onRequest(args, callbackContext);
|
||||
return true;
|
||||
}
|
||||
if ("sendResponse".equals(action)) {
|
||||
}
|
||||
if ("sendResponse".equals(action)) {
|
||||
this.sendResponse(args, callbackContext);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if ("isRunning".equals(action)) {
|
||||
if(this.nanoHTTPDWebserver!=null){
|
||||
callbackContext.success(this.nanoHTTPDWebserver.isAlive()?1:0);
|
||||
}else{
|
||||
callbackContext.success(0);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false; // Returning false results in a "MethodNotFound" error.
|
||||
}
|
||||
|
||||
|
@ -167,4 +167,11 @@
|
||||
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK)
|
||||
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
|
||||
}
|
||||
|
||||
@objc(isRunning:)
|
||||
func isRunning(_ command: CDVInvokedUrlCommand) {
|
||||
let messageAsInt = self.webServer.isRunning ? 1 : 0;
|
||||
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: messageAsInt)
|
||||
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ const START_FUNCTION = 'start';
|
||||
const ONREQUEST_FUNCTION = 'onRequest';
|
||||
const SENDRESPONSE_FUNCION = 'sendResponse';
|
||||
const STOP_FUNCTION = 'stop';
|
||||
const ISRUNNING_FUNCTION = 'isRunning';
|
||||
|
||||
export function start(success_callback, error_callback, port) {
|
||||
let params = [];
|
||||
@ -54,3 +55,12 @@ export function stop(success_callback, error_callback) {
|
||||
[]
|
||||
);
|
||||
}
|
||||
export function isRunning(success_callback, error_callback) {
|
||||
exec(
|
||||
success_callback,
|
||||
error_callback,
|
||||
WEBSERVER_CLASS,
|
||||
ISRUNNING_FUNCTION,
|
||||
[]
|
||||
);
|
||||
}
|
@ -7,6 +7,7 @@ exports.start = start;
|
||||
exports.onRequest = onRequest;
|
||||
exports.sendResponse = sendResponse;
|
||||
exports.stop = stop;
|
||||
exports.isRunning = isRunning;
|
||||
|
||||
var _exec = require('cordova/exec');
|
||||
|
||||
@ -19,6 +20,7 @@ var START_FUNCTION = 'start';
|
||||
var ONREQUEST_FUNCTION = 'onRequest';
|
||||
var SENDRESPONSE_FUNCION = 'sendResponse';
|
||||
var STOP_FUNCTION = 'stop';
|
||||
var ISRUNNING_FUNCTION = 'isRunning';
|
||||
|
||||
function start(success_callback, error_callback, port) {
|
||||
var params = [];
|
||||
@ -41,3 +43,6 @@ function sendResponse(requestId, params, success_callback, error_callback) {
|
||||
function stop(success_callback, error_callback) {
|
||||
(0, _exec2.default)(success_callback, error_callback, WEBSERVER_CLASS, STOP_FUNCTION, []);
|
||||
}
|
||||
function isRunning(success_callback, error_callback) {
|
||||
(0, _exec2.default)(success_callback, error_callback, WEBSERVER_CLASS, ISRUNNING_FUNCTION, []);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user