Skip to content

Commit

Permalink
Merge pull request FongMi#398 from jadehh/release
Browse files Browse the repository at this point in the history
* Add md5X Function and Support Post "raw" Methods
  • Loading branch information
FongMi committed Apr 16, 2024
2 parents 7cec976 + 45ce544 commit 01e034f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions quickjs/src/main/java/com/fongmi/quickjs/method/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ public String joinUrl(String parent, String child) {
return parser.joinUrl(parent, child);
}

@Keep
@JSMethod
public String md5X(String text) {
String result = Crypto.md5(text);
Logger.t("md5X").d("text:%s\nresult:\n%s" , text, result);
return result;
}

@Keep
@JSMethod
public String aesX(String mode, boolean encrypt, String input, boolean inBase64, String key, String iv, boolean outBase64) {
Expand Down
7 changes: 7 additions & 0 deletions quickjs/src/main/java/com/fongmi/quickjs/utils/Connect.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private static RequestBody getPostBody(Req req, String contentType) {
if (req.getData() != null && "json".equals(req.getPostType())) return getJsonBody(req);
if (req.getData() != null && "form".equals(req.getPostType())) return getFormBody(req);
if (req.getData() != null && "form-data".equals(req.getPostType())) return getFormDataBody(req);
if (req.getData() != null && "raw".equals(req.getPostType())) return getRawBody(req);
if (req.getBody() != null && contentType != null) return RequestBody.create(req.getBody(), MediaType.get(contentType));
return RequestBody.create("", null);
}
Expand All @@ -77,6 +78,10 @@ private static RequestBody getJsonBody(Req req) {
return RequestBody.create(req.getData().toString(), MediaType.get("application/json"));
}

private static RequestBody getRawBody(Req req) {
return RequestBody.create(req.getData().toString(), MediaType.get("application/json; charset=utf-8"));
}

private static RequestBody getFormBody(Req req) {
FormBody.Builder builder = new FormBody.Builder();
Map<String, String> params = Json.toMap(req.getData());
Expand All @@ -92,6 +97,8 @@ private static RequestBody getFormDataBody(Req req) {
return builder.build();
}



private static void setHeader(QuickJSContext ctx, Response res, JSObject object) {
for (Map.Entry<String, List<String>> entry : res.headers().toMultimap().entrySet()) {
if (entry.getValue().size() == 1) object.setProperty(entry.getKey(), entry.getValue().get(0));
Expand Down
11 changes: 11 additions & 0 deletions quickjs/src/main/java/com/fongmi/quickjs/utils/Crypto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.github.catvod.utils.Util;

public class Crypto {

public static String md5(String text) {
try {
return Util.md5(text);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}


public static String aes(String mode, boolean encrypt, String input, boolean inBase64, String key, String iv, boolean outBase64) {
try {
byte[] keyBuf = key.getBytes();
Expand Down

0 comments on commit 01e034f

Please sign in to comment.