Compare commits

..

8 Commits

Author SHA1 Message Date
13586d2514 处理ios获取服务状态错误代码 2023-12-15 02:10:49 +08:00
d88b4a509d 调整脚本 同步处理文件 2023-12-15 01:48:52 +08:00
e17c4d002c 处理异常状态 2023-12-15 01:18:52 +08:00
ce067a1e89 添加是否运行状态查询 2023-12-14 22:29:17 +08:00
00adabeb3f 修正脚本 2023-12-14 17:02:16 +08:00
5bb01739d7 添加重新添加webServer的脚本 2023-12-14 16:56:34 +08:00
2d41148067 更新依赖地址 2023-12-14 15:05:38 +08:00
185a34d196 改下pod的spec地址 2023-12-14 14:24:50 +08:00
8 changed files with 118 additions and 26 deletions

12
package-lock.json generated
View File

@ -7019,9 +7019,9 @@
}
},
"node_modules/systeminformation": {
"version": "5.21.8",
"resolved": "https://registry.npmjs.org/systeminformation/-/systeminformation-5.21.8.tgz",
"integrity": "sha512-Xf1KDMUTQHLOT9Z7MjpSpsbaICOHcm4OZ9c9qqpkCoXuxq5MoyDrgu5GIQYpoiralXNPrqxDz3ND8MdllpXeQA==",
"version": "5.12.1",
"resolved": "https://registry.npmjs.org/systeminformation/-/systeminformation-5.12.1.tgz",
"integrity": "sha512-qAV0xSeSJlg0ZHmQ1T2rLrL54SATalBx6v4T8Sd5s17pEm6saX3LKzlPhfPx+EfT91y9yhRYnKhnMoLTFkxbqw==",
"dev": true,
"os": [
"darwin",
@ -13419,9 +13419,9 @@
"dev": true
},
"systeminformation": {
"version": "5.21.8",
"resolved": "https://registry.npmjs.org/systeminformation/-/systeminformation-5.21.8.tgz",
"integrity": "sha512-Xf1KDMUTQHLOT9Z7MjpSpsbaICOHcm4OZ9c9qqpkCoXuxq5MoyDrgu5GIQYpoiralXNPrqxDz3ND8MdllpXeQA==",
"version": "5.12.1",
"resolved": "https://registry.npmjs.org/systeminformation/-/systeminformation-5.12.1.tgz",
"integrity": "sha512-qAV0xSeSJlg0ZHmQ1T2rLrL54SATalBx6v4T8Sd5s17pEm6saX3LKzlPhfPx+EfT91y9yhRYnKhnMoLTFkxbqw==",
"dev": true
},
"tar": {

View File

@ -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
View 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 });
}
};

View File

@ -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")
);

View File

@ -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.
}

View File

@ -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)
}
}

View File

@ -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,
[]
);
}

View File

@ -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, []);
}