Skip to content

Commit

Permalink
add http patch request process (#15898)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmyduckx authored Aug 9, 2023
1 parent 904cdd8 commit 8c1a005
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 5 deletions.
5 changes: 4 additions & 1 deletion native/cocos/bindings/manual/jsb_xmlhttprequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Expand Down
6 changes: 5 additions & 1 deletion native/cocos/network/HttpClient-apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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()];
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion native/cocos/network/HttpClient-java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}

Expand Down
18 changes: 18 additions & 0 deletions native/cocos/network/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions native/cocos/network/HttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class CC_DLL HttpRequest : public RefCounted {
PUT,
DELETE,
HEAD,
PATCH,
UNKNOWN,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion platforms/native/builtin/jsb-adapter/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 8c1a005

Please sign in to comment.