Skip to content

Commit

Permalink
增加http控制接口,非HTTP2协议
Browse files Browse the repository at this point in the history
  • Loading branch information
j4nk90 committed Jul 10, 2024
1 parent 2d8efaa commit a0d8510
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 5 deletions.
2 changes: 1 addition & 1 deletion autojs/src/main/assets/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ runtime.init();
//初始化一般模块
(function (scope) {
var modules = ['app', 'automator', 'console', 'dialogs', 'files', 'io', 'selector', 'shell', 'web', 'ui',
"images", "threads", "events", "engines", "RootAutomator", "http", "storages", "floaty",
"images", "threads", "events", "engines", "RootAutomator", "http", "http2", "storages", "floaty",
"sensors", "media", "plugins", "continuation", "$zip", "$base64", "$crypto", "paddle", "geoip"];
var len = modules.length;
for (var i = 0; i < len; i++) {
Expand Down
222 changes: 222 additions & 0 deletions autojs/src/main/assets/modules/__http2__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
module.exports = function (runtime, scope) {
importPackage(Packages["okhttp3"]);
importClass(com.stardust.autojs.core.http.MutableOkHttp);
var http = {};

http.__okhttp__ = new MutableOkHttp();

http.getTimeout = function () {
return http.__okhttp__.getTimeout();
}

http.setTimeout = function (timeout) {
http.__okhttp__.setTimeout(timeout);
}

http.getConnTimeout = function () {
return http.__okhttp__.getConnTimeout();
}

http.setConnTimeout = function (timeout) {
http.__okhttp__.setConnTimeout(timeout);
}

http.get = function (url, options, callback) {
options = options || {};
options.method = "GET";
return http.request(url, options, callback);
}

http.client = function () {
return http.__okhttp__.client();
}

http.post = function (url, data, options, callback) {
options = options || {};
options.method = "POST";
options.contentType = options.contentType || "application/x-www-form-urlencoded";
if (data) {
fillPostData(options, data);
}
return http.request(url, options, callback);
}

http.postJson = function (url, data, options, callback) {
options = options || {};
options.contentType = "application/json";
return http.post(url, data, options, callback);
}

http.postMultipart = function (url, files, options, callback) {
options = options || {};
options.method = "POST";
options.contentType = "multipart/form-data";
options.files = files;
return http.request(url, options, callback);
}

http.request = function (url, options, callback) {
var cont = null;
if (!callback && ui.isUiThread() && continuation.enabled) {
cont = continuation.create();
}
var call = http.client().newCall(http.buildRequest(url, options));
if (!callback && !cont) {
return wrapResponse(call.execute());
}
call.enqueue(new Callback({
onResponse: function (call, res) {
res = wrapResponse(res);
cont && cont.resume(res);
callback && callback(res);
},
onFailure: function (call, ex) {
cont && cont.resumeError(ex);
callback && callback(null, ex);
}
}));
if (cont) {
return cont.await();
}
}

http.buildRequest = function (url, options) {
var r = new Request.Builder();
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://" + url;
}
r.url(url);
if (options.headers) {
setHeaders(r, options.headers);
}
if (options.body) {
r.method(options.method, parseBody(options, options.body));
} else if (options.files) {
r.method(options.method, parseMultipart(options.files));
} else {
r.method(options.method, null);
}
return r.build();
}

function parseMultipart(files) {
var builder = new MultipartBody.Builder()
.setType(MultipartBody.FORM);
for (var key in files) {
if (!files.hasOwnProperty(key)) {
continue;
}
var value = files[key];
if (typeof (value) == 'string') {
builder.addFormDataPart(key, value);
continue;
}
var path, mimeType, fileName;
if (typeof (value.getPath) == 'function') {
path = value.getPath();
} else if (value.length == 2) {
fileName = value[0];
path = value[1];
} else if (value.length >= 3) {
fileName = value[0];
mimeType = value[1]
path = value[2];
}
var file = new com.stardust.pio.PFile(path);
fileName = fileName || file.getName();
mimeType = mimeType || parseMimeType(file.getExtension());
builder.addFormDataPart(key, fileName, RequestBody.create(MediaType.parse(mimeType), file));
}
return builder.build();
}

function parseMimeType(ext) {
if (ext.length == 0) {
return "application/octet-stream";
}
return android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext)
|| "application/octet-stream";
}

function fillPostData(options, data) {
if (options.contentType == "application/x-www-form-urlencoded") {
var b = new FormBody.Builder();
for (var key in data) {
if (data.hasOwnProperty(key)) {
b.add(key, data[key]);
}
}
options.body = b.build();
} else if (options.contentType == "application/json") {
options.body = JSON.stringify(data);
} else {
options.body = data;
}
}

function setHeaders(r, headers) {
for (var key in headers) {
if (headers.hasOwnProperty(key)) {
let value = headers[key];
if (Array.isArray(value)) {
value.forEach(v => {
r.header(key, v);
});
} else {
r.header(key, value);
}
}
}
}

function parseBody(options, body) {
if (typeof (body) == "string") {
body = RequestBody.create(MediaType.parse(options.contentType), body);
} else if (body instanceof RequestBody) {
return body;
} else {
body = new RequestBody({
contentType: function () {
return MediaType.parse(options.contentType);
},
writeTo: body
});
}
return body;
}

function wrapResponse(res) {
var r = {};
r.statusCode = res.code();
r.statusMessage = res.message();
var headers = res.headers();
r.headers = {};
for (var i = 0; i < headers.size(); i++) {
let name = headers.name(i);
let value = headers.value(i);
if (r.headers.hasOwnProperty(name)) {
let origin = r.headers[name];
if (!Array.isArray(origin)) {
r.headers[name] = [origin];
}
r.headers[name].push(value);
} else {
r.headers[name] = value;
}
}
r.body = {};
var body = res.body();
r.body.string = body.string.bind(body);
r.body.bytes = body.bytes.bind(body);
r.body.json = function () {
return JSON.parse(r.body.string());
}
r.body.contentType = body.contentType();
r.request = res.request();
r.url = r.request.url();
r.method = r.request.method();
return r;
}

return http;
}
8 changes: 8 additions & 0 deletions autojs/src/main/assets/modules/__http__.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ module.exports = function (runtime, scope) {
http.__okhttp__.setTimeout(timeout);
}

http.getConnTimeout = function () {
return http.__okhttp__.getConnTimeout();
}

http.setConnTimeout = function (timeout) {
http.__okhttp__.setConnTimeout(timeout);
}

http.get = function (url, options, callback) {
options = options || {};
options.method = "GET";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
public class MutableOkHttp extends OkHttpClient {

private OkHttpClient mOkHttpClient;
private int mMaxRetries = 3;
private long mTimeout = 30 * 1000;
private int mMaxRetries = 0;
private long mConnTimeout = 2 * 1000;
private long mTimeout = 60 * 1000;
private Interceptor mRetryInterceptor = chain -> {
Request request = chain.request();
Response response = null;
Expand Down Expand Up @@ -54,7 +55,8 @@ public OkHttpClient client() {
protected OkHttpClient newClient(Builder builder) {
builder.readTimeout(getTimeout(), TimeUnit.MILLISECONDS)
.writeTimeout(getTimeout(), TimeUnit.MILLISECONDS)
.connectTimeout(getTimeout(), TimeUnit.MILLISECONDS);
.connectTimeout(getConnTimeout(), TimeUnit.MILLISECONDS);

for (Interceptor interceptor : getInterceptors()) {
builder.addInterceptor(interceptor);
}
Expand All @@ -77,12 +79,20 @@ public long getTimeout() {
return mTimeout;
}


public void setTimeout(long timeout) {
mTimeout = timeout;
muteClient();
}

public long getConnTimeout() {
return mConnTimeout;
}

public void setConnTimeout(long timeout) {
mConnTimeout = timeout;
muteClient();
}

public synchronized void muteClient(Builder builder) {
mOkHttpClient = newClient(builder);
}
Expand Down

0 comments on commit a0d8510

Please sign in to comment.