diff --git a/frameworks/js/napi/http/include/http_napi.h b/frameworks/js/napi/http/include/http_napi.h index 8b9295aac462d364b2e29a06aadb74a44e40966f..5cbf4b03b794f27950658a6d66fd6b6e5ea5f6d8 100644 --- a/frameworks/js/napi/http/include/http_napi.h +++ b/frameworks/js/napi/http/include/http_napi.h @@ -51,7 +51,6 @@ napi_value On(napi_env env, napi_callback_info info); napi_value Off(napi_env env, napi_callback_info info); napi_value HttpRequestConstructor(napi_env env, napi_callback_info info); napi_value CreateHttp(napi_env env, napi_callback_info info); - } // namespace NetManagerStandard } // namespace OHOS #endif // HTTP_NAPI_H \ No newline at end of file diff --git a/frameworks/js/napi/http/include/http_request.h b/frameworks/js/napi/http/include/http_request.h index 5baf4446b8b6855b07e380ba003c8c37bb1bf2f9..cc34d6bf2b0874467960b4a2ae0ef665e434d477 100644 --- a/frameworks/js/napi/http/include/http_request.h +++ b/frameworks/js/napi/http/include/http_request.h @@ -25,15 +25,6 @@ #include -#define HTTP_CURL_EASY_SET_OPTION(handle, opt, data) \ - do { \ - CURLcode result = curl_easy_setopt(handle, opt, data); \ - if (result != CURLE_OK) { \ - NETMGR_LOGE("curl set option failed! error code %{public}d", result); \ - return false; \ - } \ - } while (0) - namespace OHOS { namespace NetManagerStandard { constexpr int32_t URL_PREFIX_LENGTH = 8; diff --git a/frameworks/js/napi/http/src/http_napi.cpp b/frameworks/js/napi/http/src/http_napi.cpp index 209909f1f39693f876533e63a73b6a0ff73fc592..03c456909f4f5fe54807412a679ba411c9b8a42e 100644 --- a/frameworks/js/napi/http/src/http_napi.cpp +++ b/frameworks/js/napi/http/src/http_napi.cpp @@ -191,6 +191,34 @@ napi_value CreateHttp(napi_env env, napi_callback_info info) return result; } +static napi_value GetRequestParams(napi_env env, size_t paraCount, napi_value *parameters, size_t paramNumber, + HttpRequestOptionsContext *asyncContext) +{ + char url[OHOS::NetManagerStandard::URL_ARRAY_LENGTH] = {0}; + size_t strLen = 0; + + NAPI_CALL(env, + napi_get_value_string_utf8( + env, parameters[0], url, OHOS::NetManagerStandard::URL_ARRAY_LENGTH - 1, &strLen)); + asyncContext->SetUrl(std::string(url, strLen)); + + if (paraCount == PARAMS_TWO_COUNT) { + if (NapiUtil::MatchValueType(env, parameters[ARRAY_FIRST_INDEX], napi_function)) { + NAPI_CALL(env, napi_create_reference(env, parameters[ARRAY_FIRST_INDEX], + ARRAY_COUNT, &(asyncContext->callbackRef_))); + } else if (NapiUtil::MatchValueType(env, parameters[ARRAY_FIRST_INDEX], napi_object)) { + GetRequestInfo(env, parameters[ARRAY_FIRST_INDEX], asyncContext); + } + } else if (paraCount == PARAMS_THREE_COUNT && + NapiUtil::MatchValueType(env, parameters[ARRAY_FIRST_INDEX], napi_object)) { + GetRequestInfo(env, parameters[ARRAY_SECOND_INDEX], asyncContext); + NAPI_CALL(env, napi_create_reference(env, parameters[ARRAY_SECOND_INDEX], ARRAY_COUNT, + &(asyncContext->callbackRef_))); + } + + return nullptr; +} + /* * http Request interface */ @@ -214,39 +242,16 @@ napi_value Request(napi_env env, napi_callback_info info) return nullptr; } - char url[OHOS::NetManagerStandard::URL_ARRAY_LENGTH] = {0}; - size_t strLen = 0; - - NAPI_CALL(env, - napi_get_value_string_utf8( - env, parameters[0], url, OHOS::NetManagerStandard::URL_ARRAY_LENGTH - 1, &strLen)); - HttpRequestOptionsContext *asyncContext = nullptr; - { - auto requestKey = httpRequestInstances.find(objectInfo); - if (requestKey != httpRequestInstances.end()) { - asyncContext = requestKey->second; - } else { - NETMGR_LOGE("httpRequestInstances add HttpRequest pointer"); - return nullptr; - } + auto requestKey = httpRequestInstances.find(objectInfo); + if (requestKey != httpRequestInstances.end()) { + asyncContext = requestKey->second; + } else { + NETMGR_LOGE("httpRequestInstances add HttpRequest pointer"); + return nullptr; } - asyncContext->SetUrl(std::string(url, strLen)); - - if (paraCount == PARAMS_TWO_COUNT) { - if (NapiUtil::MatchValueType(env, parameters[ARRAY_FIRST_INDEX], napi_function)) { - NAPI_CALL(env, napi_create_reference(env, parameters[ARRAY_FIRST_INDEX], - ARRAY_COUNT, &(asyncContext->callbackRef_))); - } else if (NapiUtil::MatchValueType(env, parameters[ARRAY_FIRST_INDEX], napi_object)) { - GetRequestInfo(env, parameters[ARRAY_FIRST_INDEX], asyncContext); - } - } else if (paraCount == PARAMS_THREE_COUNT && - NapiUtil::MatchValueType(env, parameters[ARRAY_FIRST_INDEX], napi_object)) { - GetRequestInfo(env, parameters[ARRAY_SECOND_INDEX], asyncContext); - NAPI_CALL(env, napi_create_reference(env, parameters[ARRAY_SECOND_INDEX], ARRAY_COUNT, - &(asyncContext->callbackRef_))); - } + GetRequestParams(env, paraCount, parameters, std::size(parameters), asyncContext); napi_value result = nullptr; diff --git a/frameworks/js/napi/http/src/http_request.cpp b/frameworks/js/napi/http/src/http_request.cpp index d32617090b8fe52e5d602bd3a217ba0d173b4df3..e0392cb803ed8d438e6783dcf69591e32c3f827b 100644 --- a/frameworks/js/napi/http/src/http_request.cpp +++ b/frameworks/js/napi/http/src/http_request.cpp @@ -165,8 +165,8 @@ bool HttpRequest::SetOptionForPost(CURL *curl, HttpRequestOptionsContext *asyncC NETMGR_LOGE("The parameter of curl or asyncContext is nullptr"); return false; } - HTTP_CURL_EASY_SET_OPTION(curl, CURLOPT_URL, asyncContext->GetUrl().c_str()); - HTTP_CURL_EASY_SET_OPTION(curl, CURLOPT_POST, 1L); + curl_easy_setopt(curl, CURLOPT_URL, asyncContext->GetUrl().c_str()); + curl_easy_setopt(curl, CURLOPT_POST, 1L); return true; } @@ -195,7 +195,7 @@ bool HttpRequest::SetOptionForGet(CURL *curl, HttpRequestOptionsContext *asyncCo curl_free(encodeOut); } } - HTTP_CURL_EASY_SET_OPTION(curl, CURLOPT_URL, url.c_str()); + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); } else { std::size_t index = url.find(URL_SEPARATOR); if (index != std::string::npos) { diff --git a/frameworks/js/napi/socket/include/tcp_extra_options.h b/frameworks/js/napi/socket/include/tcp_extra_options.h index a73f4527a005b4d1448470876dcd69dca658d924..c184f7448388837ca4ef3092c0ed4b8dee0536a7 100644 --- a/frameworks/js/napi/socket/include/tcp_extra_options.h +++ b/frameworks/js/napi/socket/include/tcp_extra_options.h @@ -28,7 +28,7 @@ public: ~TCPExtraOptions() {} class SocketLinger { - public: + public: bool on_ = true; int32_t linger_ = 0; } socketLinger_; diff --git a/frameworks/js/napi/socket/src/socket_napi.cpp b/frameworks/js/napi/socket/src/socket_napi.cpp index 08b1785c7da68051d7d13189a321f46e1720c9ad..a6bf0200237a7b24c7a68c6ba5575f36e572243b 100644 --- a/frameworks/js/napi/socket/src/socket_napi.cpp +++ b/frameworks/js/napi/socket/src/socket_napi.cpp @@ -474,8 +474,6 @@ napi_value UdpBind(napi_env env, napi_callback_info info) napi_value parameters[2] = {0}; napi_value thisVar = nullptr; void *data = nullptr; - uint32_t flag = 0; - bool isFdExist = false; NAPI_CALL(env, napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data)); @@ -497,30 +495,14 @@ napi_value UdpBind(napi_env env, napi_callback_info info) } objectInfo->GetJSParameter(env, parameters, asyncContext); - for (int i = 0; i < g_onInfoList.size(); i++) { - if (objectInfo->remInfo.socketfd == g_onInfoList.at(i).socketfd) { - flag = i; - isFdExist = true; - break; - } - } - if (!isFdExist) { - if (asyncContext->family == IPV6) { - asyncContext->socketfd = objectInfo->UdpSocket(AF_INET6, SOCK_DGRAM, 0); - } else { - asyncContext->socketfd = objectInfo->UdpSocket(AF_INET, SOCK_DGRAM, 0); - } - if (asyncContext->socketfd < 0) { - return nullptr; - } - } - if (!isFdExist) { - objectInfo->remInfo = *asyncContext; - g_onInfoList.push_back(objectInfo->remInfo); + if (asyncContext->family == IPV6) { + asyncContext->socketfd = objectInfo->UdpSocket(AF_INET6, SOCK_DGRAM, 0); } else { - g_onInfoList[flag] = *asyncContext; - objectInfo->remInfo = *asyncContext; + asyncContext->socketfd = objectInfo->UdpSocket(AF_INET, SOCK_DGRAM, 0); + } + if (asyncContext->socketfd < 0) { + return nullptr; } if (parameterCount == PARAMS_COUNT) { @@ -555,8 +537,6 @@ napi_value UdpConnect(napi_env env, napi_callback_info info) napi_value parameters[2] = {0}; napi_value thisVar = nullptr; void *data = nullptr; - uint32_t flag = 0; - bool isFdExist = false; NAPI_CALL(env, napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data)); @@ -577,19 +557,6 @@ napi_value UdpConnect(napi_env env, napi_callback_info info) } objectInfo->GetJSParameter(env, parameters, asyncContext); - for (int i = 0; i < g_onInfoList.size(); i++) { - if (objectInfo->remInfo.socketfd == g_onInfoList.at(i).socketfd) { - flag = i; - isFdExist = true; - break; - } - } - if (!isFdExist) { - return nullptr; - } - - g_onInfoList[flag] = *asyncContext; - objectInfo->remInfo = *asyncContext; if (parameterCount == PARAMS_COUNT) { if (NapiUtil::MatchValueType(env, parameters[1], napi_function)) { @@ -623,8 +590,6 @@ napi_value UdpSend(napi_env env, napi_callback_info info) napi_value parameters[2] = {0}; napi_value thisVar = nullptr; void *data = nullptr; - uint32_t flag = 0; - bool isFdExist = false; NAPI_CALL(env, napi_get_cb_info(env, info, ¶meterCount, parameters, &thisVar, &data)); @@ -644,23 +609,9 @@ napi_value UdpSend(napi_env env, napi_callback_info info) NETMGR_LOGE("UdpSend not find socket pointer"); return nullptr; } - - for (int i = 0; i < g_onInfoList.size(); i++) { - if (objectInfo->remInfo.socketfd == g_onInfoList.at(i).socketfd) { - flag = i; - isFdExist = true; - break; - } - } - if (!isFdExist) { - return nullptr; - } objectInfo->GetJSParameter(env, parameters, asyncContext); - g_onInfoList.at(flag) = *asyncContext; - objectInfo->remInfo = *asyncContext; - if (parameterCount == PARAMS_COUNT) { if (NapiUtil::MatchValueType(env, parameters[1], napi_function)) { NAPI_CALL(env, napi_create_reference(env, parameters[1], 1, &(asyncContext->callbackRef_)));