From 02c55e1d53520b8d47b59c4c2063e5f1238bd071 Mon Sep 17 00:00:00 2001 From: mmyduckx <308303735@qq.com> Date: Tue, 8 Aug 2023 18:38:38 +0800 Subject: [PATCH] add http patch request process --- .../bindings/manual/jsb_xmlhttprequest.cpp | 5 ++++- native/cocos/network/HttpClient-apple.mm | 6 +++++- native/cocos/network/HttpClient-java.cpp | 8 +++++++- native/cocos/network/HttpClient.cpp | 18 ++++++++++++++++++ native/cocos/network/HttpRequest.h | 1 + .../com/cocos/lib/CocosHttpURLConnection.java | 3 ++- .../com/cocos/lib/CocosHttpURLConnection.java | 1 + platforms/native/builtin/jsb-adapter/fetch.js | 2 +- 8 files changed, 39 insertions(+), 5 deletions(-) diff --git a/native/cocos/bindings/manual/jsb_xmlhttprequest.cpp b/native/cocos/bindings/manual/jsb_xmlhttprequest.cpp index dd37ca74f4c..fdf44d48355 100644 --- a/native/cocos/bindings/manual/jsb_xmlhttprequest.cpp +++ b/native/cocos/bindings/manual/jsb_xmlhttprequest.cpp @@ -261,6 +261,9 @@ bool XMLHttpRequest::open(const ccstd::string &method, const ccstd::string &url) requestType = HttpRequest::Type::HEAD; } else if (_method == "delete" || _method == "DELETE") { requestType = HttpRequest::Type::DELETE; + } else if (_method == "patch" || _method == "PATCH") { + requestType = HttpRequest::Type::PATCH; + } CC_ASSERT(requestType != HttpRequest::Type::UNKNOWN); @@ -513,7 +516,7 @@ void XMLHttpRequest::sendRequest() { void XMLHttpRequest::setHttpRequestData(const char *data, size_t len) { if (len > 0 && - (_method == "post" || _method == "POST" || _method == "put" || _method == "PUT")) { + (_method == "post" || _method == "POST" || _method == "put" || _method == "PUT" || _method == "patch" || _method == "PATCH")) { _httpRequest->setRequestData(data, len); } } diff --git a/native/cocos/network/HttpClient-apple.mm b/native/cocos/network/HttpClient-apple.mm index b0335c69952..bcafaf913e6 100644 --- a/native/cocos/network/HttpClient-apple.mm +++ b/native/cocos/network/HttpClient-apple.mm @@ -165,7 +165,7 @@ static int processTask(HttpClient *client, HttpRequest *request, NSString *reque } //if request type is post or put,set header and data - if ([requestType isEqual:@"POST"] || [requestType isEqual:@"PUT"]) { + if ([requestType isEqual:@"POST"] || [requestType isEqual:@"PUT"] || [requestType isEqual:@"PATCH"]) { char *requestDataBuffer = request->getRequestData(); if (nullptr != requestDataBuffer && 0 != request->getRequestDataSize()) { NSData *postData = [NSData dataWithBytes:requestDataBuffer length:request->getRequestDataSize()]; @@ -466,6 +466,10 @@ static int processTask(HttpClient *client, HttpRequest *request, NSString *reque requestType = @"DELETE"; break; + case HttpRequest::Type::PATCH: + requestType = @"PATCH"; + break; + default: CC_ABORT(); break; diff --git a/native/cocos/network/HttpClient-java.cpp b/native/cocos/network/HttpClient-java.cpp index 0e1ec005a3d..86b1d94ddce 100644 --- a/native/cocos/network/HttpClient-java.cpp +++ b/native/cocos/network/HttpClient-java.cpp @@ -634,6 +634,11 @@ void HttpClient::processResponse(HttpResponse *response, char *responseMessage) case HttpRequest::Type::DELETE: urlConnection.setRequestMethod("DELETE"); break; + + case HttpRequest::Type::PATCH: + urlConnection.setRequestMethod("PATCH"); + break; + default: break; } @@ -647,7 +652,8 @@ void HttpClient::processResponse(HttpResponse *response, char *responseMessage) } if (HttpRequest::Type::POST == requestType || - HttpRequest::Type::PUT == requestType) { + HttpRequest::Type::PUT == requestType || + HttpRequest::Type::PATCH == requestType) { urlConnection.sendRequest(request); } diff --git a/native/cocos/network/HttpClient.cpp b/native/cocos/network/HttpClient.cpp index 5476f9c79ac..f4a728992a5 100644 --- a/native/cocos/network/HttpClient.cpp +++ b/native/cocos/network/HttpClient.cpp @@ -76,6 +76,7 @@ static int processGetTask(HttpClient *client, HttpRequest *request, write_callba static int processPostTask(HttpClient *client, HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char *errorBuffer); static int processPutTask(HttpClient *client, HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char *errorBuffer); static int processDeleteTask(HttpClient *client, HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char *errorBuffer); +static int processPatchTask(HttpClient *client, HttpRequest *request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char *errorBuffer); // int processDownloadTask(HttpRequest *task, write_callback callback, void *stream, int32_t *errorCode); // Worker thread @@ -307,6 +308,13 @@ static int processDeleteTask(HttpClient *client, HttpRequest *request, write_cal return ok ? 0 : 1; } +//Process PATCH Request +static int processPatchTask(HttpClient *client, HttpRequest *request, write_callback callback, void *stream, long *responseCode, write_callback headerCallback, void *headerStream, char *errorBuffer) { + CURLRaii curl; + bool ok = curl.init(client, request, callback, stream, headerCallback, headerStream, errorBuffer) && curl.setOption(CURLOPT_CUSTOMREQUEST, "PATCH") && curl.setOption(CURLOPT_POSTFIELDS, request->getRequestData()) && curl.setOption(CURLOPT_POSTFIELDSIZE, request->getRequestDataSize()) && curl.perform(responseCode); + return ok ? 0 : 1; +} + // HttpClient implementation HttpClient *HttpClient::getInstance() { if (_httpClient == nullptr) { @@ -511,6 +519,16 @@ void HttpClient::processResponse(HttpResponse *response, char *responseMessage) responseMessage); break; + case HttpRequest::Type::PATCH: + retValue = processPatchTask(this, request, + writeData, + response->getResponseData(), + &responseCode, + writeHeaderData, + response->getResponseHeader(), + responseMessage); + break; + default: CC_ABORT(); break; diff --git a/native/cocos/network/HttpRequest.h b/native/cocos/network/HttpRequest.h index 31d01f04b6e..ef0ffbf4a2c 100644 --- a/native/cocos/network/HttpRequest.h +++ b/native/cocos/network/HttpRequest.h @@ -72,6 +72,7 @@ class CC_DLL HttpRequest : public RefCounted { PUT, DELETE, HEAD, + PATCH, UNKNOWN, }; diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosHttpURLConnection.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosHttpURLConnection.java index c5bb2e72c44..221298649c7 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosHttpURLConnection.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosHttpURLConnection.java @@ -59,6 +59,7 @@ public class CocosHttpURLConnection private static String TAG = "CocosHttpURLConnection"; private static final String POST_METHOD = "POST" ; private static final String PUT_METHOD = "PUT" ; + private static final String PATCH_METHOD = "PATCH" ; static HttpURLConnection createHttpURLConnection(String linkURL) { URL url; @@ -86,7 +87,7 @@ static void setReadAndConnectTimeout(HttpURLConnection urlConnection, int readMi static void setRequestMethod(HttpURLConnection urlConnection, String method){ try { urlConnection.setRequestMethod(method); - if(method.equalsIgnoreCase(POST_METHOD) || method.equalsIgnoreCase(PUT_METHOD)) { + if(method.equalsIgnoreCase(POST_METHOD) || method.equalsIgnoreCase(PUT_METHOD) || method.equalsIgnoreCase(PATCH_METHOD)) { urlConnection.setDoOutput(true); } } catch (ProtocolException e) { diff --git a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosHttpURLConnection.java b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosHttpURLConnection.java index e2faea71ed6..fb3d202a455 100644 --- a/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosHttpURLConnection.java +++ b/native/cocos/platform/ohos/libcocos/src/main/java/com/cocos/lib/CocosHttpURLConnection.java @@ -61,6 +61,7 @@ public class CocosHttpURLConnection { private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0, "CocosHttpURLConnection"); private static final String POST_METHOD = "POST"; private static final String PUT_METHOD = "PUT"; + private static final String PATCH_METHOD = "PATCH"; @SuppressWarnings("unused") static HttpURLConnection createHttpURLConnection(String linkURL) { diff --git a/platforms/native/builtin/jsb-adapter/fetch.js b/platforms/native/builtin/jsb-adapter/fetch.js index 2ec5efb8686..e776a89f9ee 100644 --- a/platforms/native/builtin/jsb-adapter/fetch.js +++ b/platforms/native/builtin/jsb-adapter/fetch.js @@ -289,7 +289,7 @@ function Body () { } // HTTP methods whose capitalization should be normalized -const methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT']; +const methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT', 'PATCH']; function normalizeMethod (method) { const upcased = method.toUpperCase();