diff --git a/frameworks/js/napi/http/include/http_enum_define.h b/frameworks/js/napi/http/include/http_enum_define.h index e61b2dc19de0d77f37cc8e0d30ed96483d785f88..1dbd4bc875edfa17e620b62282b25df1410e4b57 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 db177107dcf97d7b6590fcce832689cac8df8b36..ef9c5f1e162e85a6631a06827a6033c65ee829e6 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 56e378e6b58ed89cdef1a2b2d18a8d89210ba159..209909f1f39693f876533e63a73b6a0ff73fc592 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 = { @@ -36,9 +42,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 +110,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); } /* @@ -223,15 +234,18 @@ 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; @@ -351,8 +365,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); @@ -404,8 +418,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); diff --git a/frameworks/js/napi/http/src/http_request.cpp b/frameworks/js/napi/http/src/http_request.cpp index f5fe44c5a65999d643e9d71e4fb2710bb7609844..d32617090b8fe52e5d602bd3a217ba0d173b4df3 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");