diff --git a/native/cocos/bindings/manual/jsb_xmlhttprequest.cpp b/native/cocos/bindings/manual/jsb_xmlhttprequest.cpp index 5b41fb21f2..93a43d74e3 100644 --- a/native/cocos/bindings/manual/jsb_xmlhttprequest.cpp +++ b/native/cocos/bindings/manual/jsb_xmlhttprequest.cpp @@ -1,5 +1,5 @@ /**************************************************************************** - Copyright (c) 2017-2021 Xiamen Yaji Software Co., Ltd. + Copyright (c) 2017-2022 Xiamen Yaji Software Co., Ltd. http://www.cocos.com @@ -23,13 +23,6 @@ THE SOFTWARE. ****************************************************************************/ -// -// jsb_XMLHttpRequest.cpp -// cocos2d_js_bindings -// -// Created by James Chen on 5/15/17. -// -// #include "jsb_xmlhttprequest.h" #include #include @@ -357,12 +350,12 @@ void XMLHttpRequest::getHeader(const ccstd::string &header) { http_field = header.substr(0, found_header_field); http_value = header.substr(found_header_field + 1, header.length()); - // Get rid of all \n + // trim \n at the end of the string if (!http_value.empty() && http_value[http_value.size() - 1] == '\n') { http_value.erase(http_value.size() - 1); } - // Get rid of leading space (header is field: value format) + // trim leading space (header is field: value format) if (!http_value.empty() && http_value[0] == ' ') { http_value.erase(0, 1); } @@ -544,15 +537,15 @@ void XMLHttpRequest::setRequestHeader(const ccstd::string &key, const ccstd::str } ccstd::string XMLHttpRequest::getAllResponseHeaders() const { - std::stringstream responseheaders; - ccstd::string responseheader; + std::stringstream responseHeaders; + ccstd::string responseHeader; for (const auto &it : _httpHeader) { - responseheaders << it.first << ": " << it.second << "\n"; + responseHeaders << it.first << ": " << it.second << "\n"; } - responseheader = responseheaders.str(); - return responseheader; + responseHeader = responseHeaders.str(); + return responseHeader; } ccstd::string XMLHttpRequest::getResponseHeader(const ccstd::string &key) const { @@ -568,26 +561,14 @@ ccstd::string XMLHttpRequest::getResponseHeader(const ccstd::string &key) const } void XMLHttpRequest::setHttpRequestHeader() { - ccstd::vector header; + ccstd::vector headers; for (auto &it : _requestHeader) { - const char *first = it.first.c_str(); - const char *second = it.second.c_str(); - size_t len = sizeof(char) * (strlen(first) + 3 + strlen(second)); - char *test = static_cast(malloc(len)); - memset(test, 0, len); - - strcpy(test, first); - strcpy(test + strlen(first), ": "); - strcpy(test + strlen(first) + 2, second); - - header.emplace_back(test); - - free(test); + headers.emplace_back(it.first + ": " + it.second); } - if (!header.empty()) { - _httpRequest->setHeaders(header); + if (!headers.empty()) { + _httpRequest->setHeaders(headers); } } diff --git a/native/cocos/network/HttpClient-apple.mm b/native/cocos/network/HttpClient-apple.mm index f1af05e248..406cccef8c 100644 --- a/native/cocos/network/HttpClient-apple.mm +++ b/native/cocos/network/HttpClient-apple.mm @@ -149,7 +149,15 @@ static int processTask(HttpClient *client, HttpRequest *request, NSString *reque unsigned long i = header.find(':', 0); unsigned long length = header.size(); ccstd::string field = header.substr(0, i); - ccstd::string value = header.substr(i + 1, length - i); + ccstd::string value = header.substr(i + 1); + // trim \n at the end of the string + if (!value.empty() && value[value.size() - 1] == '\n') { + value.erase(value.size() - 1); + } + // trim leading space (header is field: value format) + if (!value.empty() && value[0] == ' ') { + value.erase(0, 1); + } NSString *headerField = [NSString stringWithUTF8String:field.c_str()]; NSString *headerValue = [NSString stringWithUTF8String:value.c_str()]; [nsrequest setValue:headerValue forHTTPHeaderField:headerField]; diff --git a/native/cocos/network/HttpClient-java.cpp b/native/cocos/network/HttpClient-java.cpp index 3ac5c1644f..e93ed9972c 100644 --- a/native/cocos/network/HttpClient-java.cpp +++ b/native/cocos/network/HttpClient-java.cpp @@ -124,12 +124,20 @@ public: /* append custom headers one by one */ for (auto &header : headers) { uint32_t len = header.length(); - uint32_t pos = header.find(':'); - if (-1 == pos || pos >= len) { + ccstd::string::size_type pos = header.find(':'); + if (ccstd::string::npos == pos || pos >= len) { continue; } ccstd::string str1 = header.substr(0, pos); - ccstd::string str2 = header.substr(pos + 1, len - pos - 1); + ccstd::string str2 = header.substr(pos + 1); + // trim \n at the end of the string + if (!str2.empty() && str2[str2.size() - 1] == '\n') { + str2.erase(str2.size() - 1); + } + // trim leading space (header is field: value format) + if (!str2.empty() && str2[0] == ' ') { + str2.erase(0, 1); + } addRequestHeader(str1.c_str(), str2.c_str()); } }