From 407eb0d190a3614a50f796e89c74d765152666ab Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Fri, 3 Dec 2021 11:29:31 +0800 Subject: [PATCH 1/3] dailycheck code clean Signed-off-by: hhchinasoft --- .../js/napi/http/include/http_enum_define.h | 2 +- .../include/http_request_options_context.h | 2 +- frameworks/js/napi/http/src/http_napi.cpp | 19 ++++++++++++------- frameworks/js/napi/http/src/http_request.cpp | 6 +++--- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/frameworks/js/napi/http/include/http_enum_define.h b/frameworks/js/napi/http/include/http_enum_define.h index e61b2dc19..1dbd4bc87 100644 --- a/frameworks/js/napi/http/include/http_enum_define.h +++ b/frameworks/js/napi/http/include/http_enum_define.h @@ -18,7 +18,7 @@ namespace OHOS { namespace NetManagerStandard { -enum RequestMethod { OPTIONS = 0, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT }; +enum class RequestMethod { OPTIONS = 0, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT }; enum ResponseCode { OK = 200, diff --git a/frameworks/js/napi/http/include/http_request_options_context.h b/frameworks/js/napi/http/include/http_request_options_context.h index db177107d..ef9c5f1e1 100644 --- a/frameworks/js/napi/http/include/http_request_options_context.h +++ b/frameworks/js/napi/http/include/http_request_options_context.h @@ -141,7 +141,7 @@ public: return this->cafile_; } private: - enum RequestMethod method_ = GET; + RequestMethod method_ = RequestMethod::GET; std::string extraData_ = ""; std::string header_ = ""; int32_t readTimeout_ = 60; diff --git a/frameworks/js/napi/http/src/http_napi.cpp b/frameworks/js/napi/http/src/http_napi.cpp index 56e378e6b..66c8540f4 100644 --- a/frameworks/js/napi/http/src/http_napi.cpp +++ b/frameworks/js/napi/http/src/http_napi.cpp @@ -36,9 +36,19 @@ static int32_t FindMethodIndex(const std::string &key) return result; } +static void GetCaFilePathInfo(napi_env env, napi_value objValue, HttpRequestOptionsContext *asyncContext) +{ + std::string caFile = std::string("/etc/ssl/cacert.pem"); + bool result = NapiUtil::HasNamedTypeProperty(env, objValue, napi_string, "caFile"); + if (result) { + caFile = NapiUtil::GetStringProperty(env, objValue, "caFile"); + } + asyncContext->SetCaFile(caFile); +} + static void GetRequestInfo(napi_env env, napi_value objValue, HttpRequestOptionsContext *asyncContext) { - enum RequestMethod method = GET; + RequestMethod method = RequestMethod::GET; bool result = NapiUtil::HasNamedTypeProperty(env, objValue, napi_string, "method"); if (result) { method = static_cast(FindMethodIndex(NapiUtil::GetStringProperty(env, objValue, "method"))); @@ -94,12 +104,7 @@ static void GetRequestInfo(napi_env env, napi_value objValue, HttpRequestOptions } asyncContext->SetFixedLengthStreamingMode(fixedLengthStreamingMode); - std::string caFile = std::string("/etc/ssl/cacert.pem"); - result = NapiUtil::HasNamedTypeProperty(env, objValue, napi_string, "caFile"); - if (result) { - caFile = NapiUtil::GetStringProperty(env, objValue, "caFile"); - } - asyncContext->SetCaFile(caFile); + GetCaFilePathInfo(env, objValue, asyncContext); } /* diff --git a/frameworks/js/napi/http/src/http_request.cpp b/frameworks/js/napi/http/src/http_request.cpp index f5fe44c5a..d32617090 100644 --- a/frameworks/js/napi/http/src/http_request.cpp +++ b/frameworks/js/napi/http/src/http_request.cpp @@ -149,10 +149,10 @@ void HttpRequest::SetMethod(CURL *curl, HttpRequestOptionsContext *asyncContext) return; } RequestMethod method = asyncContext->GetRequestMethod(); - if (method == OPTIONS || method == GET || method == HEAD || method == DELETE || method == TRACE || - method == CONNECT) { + if (method == RequestMethod::OPTIONS || method == RequestMethod::GET || method == RequestMethod::HEAD + || method == RequestMethod::DELETE || method == RequestMethod::TRACE || method == RequestMethod::CONNECT) { SetOptionForGet(curl, asyncContext); - } else if (method == POST || method == PUT) { + } else if (method == RequestMethod::POST || method == RequestMethod::PUT) { SetOptionForPost(curl, asyncContext); } else { NETMGR_LOGE("SetMethod ErrorCode : COMMON_ERROR_CODE"); -- Gitee From 08fa62cde5d5a3792d9b426e64346fdd74036a9e Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Fri, 3 Dec 2021 11:52:03 +0800 Subject: [PATCH 2/3] dailycheck code http clean Signed-off-by: hhchinasoft --- frameworks/js/napi/http/src/http_napi.cpp | 30 ++++++++++++++--------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/frameworks/js/napi/http/src/http_napi.cpp b/frameworks/js/napi/http/src/http_napi.cpp index 66c8540f4..be88bd3d4 100644 --- a/frameworks/js/napi/http/src/http_napi.cpp +++ b/frameworks/js/napi/http/src/http_napi.cpp @@ -21,6 +21,12 @@ namespace OHOS { namespace NetManagerStandard { +constexpr int32_t PARAMS_TWO_COUNT = 2; +constexpr int32_t PARAMS_THREE_COUNT = 3; +constexpr int32_t ARRAY_FIRST_INDEX = 1; +constexpr int32_t ARRAY_SECOND_INDEX = 2; +constexpr int32_t ARRAY_COUNT = 1; + static int32_t FindMethodIndex(const std::string &key) { std::vector methodVector = { @@ -228,15 +234,15 @@ napi_value Request(napi_env env, napi_callback_info info) asyncContext->SetUrl(std::string(url, strLen)); - if (paraCount == 2) { - if (NapiUtil::MatchValueType(env, parameters[1], napi_function)) { - NAPI_CALL(env, napi_create_reference(env, parameters[1], 1, &(asyncContext->callbackRef_))); - } else if (NapiUtil::MatchValueType(env, parameters[1], napi_object)) { - GetRequestInfo(env, parameters[1], asyncContext); + 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 == 3 && NapiUtil::MatchValueType(env, parameters[1], napi_object)) { - GetRequestInfo(env, parameters[2], asyncContext); - NAPI_CALL(env, napi_create_reference(env, parameters[2], 1, &(asyncContext->callbackRef_))); + } 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_))); } napi_value result = nullptr; @@ -356,8 +362,8 @@ napi_value On(napi_env env, napi_callback_info info) napi_ref callbackRef = nullptr; - if (paraCount == 2) { - napi_create_reference(env, parameters[1], 1, &callbackRef); + if (paraCount == PARAMS_TWO_COUNT) { + napi_create_reference(env, parameters[ARRAY_FIRST_INDEX], ARRAY_COUNT, &callbackRef); } napi_value result = nullptr; uint32_t eventType = GetEventType(eventTypeChars); @@ -409,8 +415,8 @@ napi_value Off(napi_env env, napi_callback_info info) env, parameters[0], eventTypeChars, OHOS::NetManagerStandard::URL_ARRAY_LENGTH - 1, &strLen)); napi_ref callbackRef = nullptr; - if (paraCount == 2) { - napi_create_reference(env, parameters[1], 1, &callbackRef); + if (paraCount == PARAMS_TWO_COUNT) { + napi_create_reference(env, parameters[ARRAY_FIRST_INDEX], ARRAY_COUNT, &callbackRef); } napi_value result = nullptr; uint32_t eventType = GetEventType(eventTypeChars); -- Gitee From 836376331b398baa1ada11f08b45f5e75b259951 Mon Sep 17 00:00:00 2001 From: hhchinasoft Date: Fri, 3 Dec 2021 12:01:49 +0800 Subject: [PATCH 3/3] dailycheck http code clean Signed-off-by: hhchinasoft --- frameworks/js/napi/http/src/http_napi.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/frameworks/js/napi/http/src/http_napi.cpp b/frameworks/js/napi/http/src/http_napi.cpp index be88bd3d4..209909f1f 100644 --- a/frameworks/js/napi/http/src/http_napi.cpp +++ b/frameworks/js/napi/http/src/http_napi.cpp @@ -236,13 +236,16 @@ napi_value Request(napi_env env, napi_callback_info info) 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_))); + 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)) { + } 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_))); + NAPI_CALL(env, napi_create_reference(env, parameters[ARRAY_SECOND_INDEX], ARRAY_COUNT, + &(asyncContext->callbackRef_))); } napi_value result = nullptr; -- Gitee