diff --git a/frameworks/js/napi/http/BUILD.gn b/frameworks/js/napi/http/BUILD.gn index ecca6f01a171360dead51d2d7542ef347bf89279..2512dcee79b930121c5d940f621ee4166f9f344b 100644 --- a/frameworks/js/napi/http/BUILD.gn +++ b/frameworks/js/napi/http/BUILD.gn @@ -176,13 +176,7 @@ ohos_shared_library("http") { "$NETSTACK_DIR/utils/http_over_curl/src/epoll_request_handler.cpp", "$NETSTACK_DIR/utils/http_over_curl/src/http_handover_handler.cpp", ] - defines += [ - "HTTP_HANDOVER_FEATURE", - "HTTP_STACK_NAME=\"http\"", - ] - include_dirs += [ - "$NETSTACK_DIR/interfaces/innerkits/http_client/include", - ] + defines += [ "HTTP_HANDOVER_FEATURE" ] } else { defines = [ "HAS_NETMANAGER_BASE=0", diff --git a/frameworks/js/napi/http/async_context/include/request_context.h b/frameworks/js/napi/http/async_context/include/request_context.h index 2e0453259227388c63570ee26cd38dd392105825..1622325c1354a50365edcf5a6cba5ec63980b524 100644 --- a/frameworks/js/napi/http/async_context/include/request_context.h +++ b/frameworks/js/napi/http/async_context/include/request_context.h @@ -57,7 +57,7 @@ struct RequestHandoverInfo { int32_t handoverNum = 0; int32_t handoverReason = 0; double flowControlTime = 0; - int32_t readFlag = 0; + int32_t isRead = 0; }; #endif @@ -170,8 +170,8 @@ public: std::string GetPinnedPubkey() const; #ifdef HTTP_HANDOVER_FEATURE - void SetRequestHandoverInfo(int32_t handoverNum, int32_t handoverReason, double flowControlTime, int32_t readFlag); - + void SetRequestHandoverInfo(int32_t handoverNum, int32_t handoverReason, double flowControlTime, int32_t isRead); + std::string GetRequestHandoverInfo(); #endif private: diff --git a/frameworks/js/napi/http/async_context/src/request_context.cpp b/frameworks/js/napi/http/async_context/src/request_context.cpp index fc6e37871aea8f984446ef90d246036ad1fa3d3c..bd6295cf78dc69d9cc9f3708505eb1dc5ed115e1 100755 --- a/frameworks/js/napi/http/async_context/src/request_context.cpp +++ b/frameworks/js/napi/http/async_context/src/request_context.cpp @@ -38,6 +38,7 @@ #include "http_handover_handler.h" #endif + static constexpr const int PARAM_JUST_URL = 1; static constexpr const int PARAM_JUST_URL_OR_CALLBACK = 1; @@ -1047,14 +1048,14 @@ std::string RequestContext::GetPinnedPubkey() const #ifdef HTTP_HANDOVER_FEATURE void RequestContext::SetRequestHandoverInfo(int32_t handoverNum, int32_t handoverReason, double flowControlTime, - int32_t readFlag) + int32_t isRead) { requestHandoverInfo_.handoverNum = handoverNum; requestHandoverInfo_.handoverReason = handoverReason; requestHandoverInfo_.flowControlTime = flowControlTime; - requestHandoverInfo_.readFlag = readFlag; + requestHandoverInfo_.isRead = isRead; } - + std::string RequestContext::GetRequestHandoverInfo() { std::string requestHandoverInfo; @@ -1062,27 +1063,26 @@ std::string RequestContext::GetRequestHandoverInfo() requestHandoverInfo = "no handover"; return requestHandoverInfo; } - int32_t readFlag = requestHandoverInfo_.readFlag; + int32_t isRead = requestHandoverInfo_.isRead; requestHandoverInfo += "HandoverNum:"; requestHandoverInfo += std::to_string(requestHandoverInfo_.handoverNum); requestHandoverInfo += ", handoverReason:"; switch (requestHandoverInfo_.handoverReason) { - case HandoverRequestType::INCOMING: + case HttpOverCurl::HttpHandoverHandler::RequestType::INCOMING: requestHandoverInfo += "flowControl, flowControlTime:"; break; - case HandoverRequestType::NETWORKERROR: + case HttpOverCurl::HttpHandoverHandler::RequestType::NETWORKERROR: requestHandoverInfo += "netErr, retransTime:"; break; - case HandoverRequestType::UNDONE: + case HttpOverCurl::HttpHandoverHandler::RequestType::UNDONE: requestHandoverInfo += "undone, retransTime:"; break; default: - requestHandoverInfo += "unkown type"; break; } requestHandoverInfo += std::to_string(requestHandoverInfo_.flowControlTime); requestHandoverInfo += ", isRead:"; - requestHandoverInfo += readFlag == 1 ? "true" : (readFlag == 0 ? "false" : "error"); + requestHandoverInfo += isRead == 1 ? "true" : (isRead == 0 ? "false" : "error"); requestHandoverInfo += ", isStream:"; requestHandoverInfo += this->IsRequestInStream() ? "true" : "false"; return requestHandoverInfo; diff --git a/frameworks/js/napi/http/http_exec/include/http_exec.h b/frameworks/js/napi/http/http_exec/include/http_exec.h index 6b0ca2af6731ee52cdbe09854fdb8b349596645d..62bf7eacebcd64050eaedafa2ce972fa6d3e9fb7 100644 --- a/frameworks/js/napi/http/http_exec/include/http_exec.h +++ b/frameworks/js/napi/http/http_exec/include/http_exec.h @@ -33,10 +33,6 @@ #include "napi/native_api.h" #include "request_context.h" -namespace OHOS::NetStack::HttpOverCurl { - struct TransferCallbacks; -} - namespace OHOS::NetStack::Http { class HttpResponseCacheExec final { public: @@ -112,10 +108,6 @@ private: static bool SetCertPinnerOption(CURL *curl, RequestContext *context); -#if HAS_NETMANAGER_BASE - static void SetRequestInfoCallbacks(HttpOverCurl::TransferCallbacks &callbacks); -#endif - static size_t OnWritingMemoryBody(const void *data, size_t size, size_t memBytes, void *userData); static size_t OnWritingMemoryHeader(const void *data, size_t size, size_t memBytes, void *userData); diff --git a/frameworks/js/napi/http/http_exec/src/http_exec.cpp b/frameworks/js/napi/http/http_exec/src/http_exec.cpp index 5c35b5a8187baa5b63bb093ccdde80f78fe86acd..f2592f5e5b39dea3d723493ddbe50b1832ab325f 100755 --- a/frameworks/js/napi/http/http_exec/src/http_exec.cpp +++ b/frameworks/js/napi/http/http_exec/src/http_exec.cpp @@ -68,9 +68,6 @@ #include "secure_char.h" #include "trace_events.h" #include "hi_app_event_report.h" -#ifdef HTTP_HANDOVER_FEATURE -#include "http_handover_info.h" -#endif #include "http_utils.h" @@ -174,58 +171,6 @@ static void AsyncWorkRequestInStreamCallback(napi_env env, napi_status status, v } } -#if HAS_NETMANAGER_BASE -void HttpExec::SetRequestInfoCallbacks(HttpOverCurl::TransferCallbacks &callbacks) -{ - static auto startedCallback = +[](CURL *easyHandle, void *opaqueData) { - char *url = nullptr; - curl_easy_getinfo(easyHandle, CURLINFO_EFFECTIVE_URL, &url); - auto context = static_cast(opaqueData); // - context->GetTrace().Tracepoint(TraceEvents::QUEUE); - }; - - static auto responseCallback = +[](CURLMsg *curlMessage, void *opaqueData) { - auto context = static_cast(opaqueData); - context->GetTrace().Tracepoint(TraceEvents::NAPI_QUEUE); - HttpExec::HandleCurlData(curlMessage, context); - }; - callbacks.startedCallback = startedCallback; - callbacks.doneCallback = responseCallback; - -#ifdef HTTP_HANDOVER_FEATURE - static auto handoverInfoCallback = +[](void *opaqueData) { - HttpHandoverStackInfo httpHandoverStackInfo; - auto context = static_cast(opaqueData); - if (context == nullptr) { - NETSTACK_LOGE("handoverInfoCallback context is nullptr, error!"); - return httpHandoverStackInfo; - } - httpHandoverStackInfo.taskId = context->GetTaskId(); - httpHandoverStackInfo.readTimeout = context->options.GetReadTimeout(); - httpHandoverStackInfo.connectTimeout = context->options.GetConnectTimeout(); - httpHandoverStackInfo.method = context->options.GetMethod(); - httpHandoverStackInfo.requestUrl = context->options.GetUrl(); - httpHandoverStackInfo.isInStream = context->IsRequestInStream(); - httpHandoverStackInfo.isSuccess = (context->IsParseOK() && context->IsExecOK()); - return httpHandoverStackInfo; - }; - static auto setHandoverInfoCallback = +[](HttpHandoverInfo httpHandoverInfo, void *opaqueData) { - auto context = static_cast(opaqueData); - if (context == nullptr) { - NETSTACK_LOGE("setHandoverInfoCallback context is nullptr, error!"); - return; - } - context->SetRequestHandoverInfo(httpHandoverInfo.handoverNum, - httpHandoverInfo.handoverReason, - httpHandoverInfo.flowControlTime, - httpHandoverInfo.readFlag); - }; - callbacks.handoverInfoCallback = handoverInfoCallback; - callbacks.setHandoverInfoCallback = setHandoverInfoCallback; -#endif -} -#endif - void HttpExec::AsyncWorkRequestCallback(napi_env env, napi_status status, void *data) { if (status != napi_ok) { @@ -349,9 +294,21 @@ bool HttpExec::AddCurlHandle(CURL *handle, RequestContext *context) SetServerSSLCertOption(handle, context); static HttpOverCurl::EpollRequestHandler requestHandler; - HttpOverCurl::TransferCallbacks callbacks; - SetRequestInfoCallbacks(callbacks); - requestHandler.Process(handle, callbacks, context); + + static auto startedCallback = +[](CURL *easyHandle, void *opaqueData) { + char *url = nullptr; + curl_easy_getinfo(easyHandle, CURLINFO_EFFECTIVE_URL, &url); + auto context = static_cast(opaqueData); + context->GetTrace().Tracepoint(TraceEvents::QUEUE); + }; + + static auto responseCallback = +[](CURLMsg *curlMessage, void *opaqueData) { + auto context = static_cast(opaqueData); + context->GetTrace().Tracepoint(TraceEvents::NAPI_QUEUE); + HttpExec::HandleCurlData(curlMessage, context); + }; + + requestHandler.Process(handle, startedCallback, responseCallback, context); return true; #else std::thread([context, handle] { diff --git a/frameworks/native/http/http_client/http_client.cpp b/frameworks/native/http/http_client/http_client.cpp index c372a4635ea785d4da39c9279267ba3f865b0864..ae5b18a6ca4cf4c9ba80abf65895fb140fa99b30 100644 --- a/frameworks/native/http/http_client/http_client.cpp +++ b/frameworks/native/http/http_client/http_client.cpp @@ -24,9 +24,6 @@ #include "trace_events.h" #include "http_client.h" #include "netstack_log.h" -#ifdef HTTP_HANDOVER_FEATURE -#include "http_handover_info.h" -#endif namespace OHOS { namespace NetStack { @@ -82,12 +79,17 @@ std::shared_ptr HttpSession::CreateTask(const HttpClientRequest return ptr; } -void HttpSession::SetRequestInfoCallbacks( - HttpOverCurl::TransferCallbacks &callbacks, const std::shared_ptr &ptr) +void HttpSession::StartTask(const std::shared_ptr &ptr) { if (nullptr == ptr) { + NETSTACK_LOGE("HttpSession::StartTask shared_ptr = nullptr! Error!"); return; } + + static HttpOverCurl::EpollRequestHandler requestHandler; + + ptr->SetStatus(TaskStatus::RUNNING); + auto startedCallback = [ptr](CURL *, void *) { ptr->GetTrace().Tracepoint(TraceEvents::QUEUE); }; @@ -96,50 +98,7 @@ void HttpSession::SetRequestInfoCallbacks( ptr->ProcessResponse(curlMessage); ptr->SetStatus(TaskStatus::IDLE); }; - callbacks.startedCallback = startedCallback; - callbacks.doneCallback = responseCallback; - -#ifdef HTTP_HANDOVER_FEATURE - auto handoverInfoCallback = [ptr](void *) { - HttpHandoverStackInfo httpHandoverStackInfo; - httpHandoverStackInfo.taskId = ptr->GetTaskId(); - httpHandoverStackInfo.readTimeout = ptr->GetRequest().GetTimeout(); - httpHandoverStackInfo.connectTimeout = ptr->GetRequest().GetConnectTimeout(); - httpHandoverStackInfo.method = ptr->GetRequest().GetMethod(); - httpHandoverStackInfo.requestUrl = ptr->GetRequest().GetURL(); - httpHandoverStackInfo.isInStream = ptr->onDataReceive_ ? true : false; - httpHandoverStackInfo.isSuccess = ptr->IsSuccess(); - return httpHandoverStackInfo; - }; - static auto setHandoverInfoCallback = [ptr](HttpHandoverInfo httpHandoverInfo, void *) { - ptr->SetRequestHandoverInfo(httpHandoverInfo.handoverNum, - httpHandoverInfo.handoverReason, - httpHandoverInfo.flowControlTime, - httpHandoverInfo.readFlag); - }; - callbacks.handoverInfoCallback = handoverInfoCallback; - callbacks.setHandoverInfoCallback = setHandoverInfoCallback; -#endif -} - -void HttpSession::StartTask(const std::shared_ptr &ptr) -{ - if (nullptr == ptr) { - NETSTACK_LOGE("HttpSession::StartTask shared_ptr = nullptr! Error!"); - return; - } - - static HttpOverCurl::EpollRequestHandler requestHandler; - - ptr->SetStatus(TaskStatus::RUNNING); - - HttpOverCurl::TransferCallbacks callbacks; - SetRequestInfoCallbacks(callbacks, ptr); -#ifdef HTTP_HANDOVER_FEATURE - requestHandler.Process(ptr->GetCurlHandle(), callbacks, &ptr->GetRequest()); -#else - requestHandler.Process(ptr->GetCurlHandle(), callbacks); -#endif + requestHandler.Process(ptr->GetCurlHandle(), startedCallback, responseCallback); } } // namespace HttpClient diff --git a/frameworks/native/http/http_client/http_client_task.cpp b/frameworks/native/http/http_client/http_client_task.cpp index c01e66181e29cb29736621289759eb11a9157629..faf1a86839504a09c809de1750485ed175c12b1b 100644 --- a/frameworks/native/http/http_client/http_client_task.cpp +++ b/frameworks/native/http/http_client/http_client_task.cpp @@ -35,9 +35,6 @@ #include "netsys_client.h" #endif #include "netstack_hisysevent.h" -#ifdef HTTP_HANDOVER_FEATURE -#include "http_handover_info.h" -#endif #define NETSTACK_CURL_EASY_SET_OPTION(handle, opt, data) \ do { \ @@ -800,9 +797,6 @@ void HttpClientTask::DumpHttpPerformance() curl_off_t size = GetSizeFromCurl(curlHandle_); char *ip = nullptr; curl_easy_getinfo(curlHandle_, CURLINFO_PRIMARY_IP, &ip); -#ifdef HTTP_HANDOVER_FEATURE - std::string handoverInfo = GetRequestHandoverInfo(); -#endif NETSTACK_LOGI( "taskid=%{public}d" ", size:%{public}" CURL_FORMAT_CURL_OFF_T @@ -813,9 +807,6 @@ void HttpClientTask::DumpHttpPerformance() ", firstRecv:%{public}.3f" ", total:%{public}.3f" ", redirect:%{public}.3f" -#ifdef HTTP_HANDOVER_FEATURE - ", %{public}s" -#endif ", errCode:%{public}d" ", RespCode:%{public}s" ", httpVer:%{public}s" @@ -825,9 +816,6 @@ void HttpClientTask::DumpHttpPerformance() tlsTime == 0 ? 0 : tlsTime - connectTime, firstSendTime == 0 ? 0 : firstSendTime - std::max({dnsTime, connectTime, tlsTime}), firstRecvTime == 0 ? 0 : firstRecvTime - firstSendTime, totalTime, redirectTime, -#ifdef HTTP_HANDOVER_FEATURE - handoverInfo.c_str(), -#endif error_.GetErrorCode(), std::to_string(responseCode).c_str(), std::to_string(httpVer).c_str(), request_.GetMethod().c_str(), osErr); @@ -862,9 +850,6 @@ void HttpClientTask::ProcessResponse(CURLMsg *msg) if (onCanceled_) { onCanceled_(request_, response_); } -#ifdef HTTP_HANDOVER_FEATURE - SetSuccess(false); -#endif return; } @@ -872,9 +857,6 @@ void HttpClientTask::ProcessResponse(CURLMsg *msg) if (onFailed_) { onFailed_(request_, response_, error_); } -#ifdef HTTP_HANDOVER_FEATURE - SetSuccess(false); -#endif return; } @@ -885,14 +867,8 @@ void HttpClientTask::ProcessResponse(CURLMsg *msg) if (onSucceeded_) { onSucceeded_(request_, response_); } -#ifdef HTTP_HANDOVER_FEATURE - SetSuccess(true); -#endif } else if (onFailed_) { onFailed_(request_, response_, error_); -#ifdef HTTP_HANDOVER_FEATURE - SetSuccess(false); -#endif } #if HAS_NETMANAGER_BASE HttpClientNetworkMessage httpClientNetworkMessage(std::to_string(GetTaskId()), request_, response_, curlHandle_); @@ -918,54 +894,6 @@ bool HttpClientTask::SetDnsCacheOption(CURL *handle) #endif return true; } - -#ifdef HTTP_HANDOVER_FEATURE -void HttpClientTask::SetSuccess(bool isSuccess) -{ - isSuccess_ = isSuccess; -} - -void HttpClientTask::SetRequestHandoverInfo( - int32_t handoverNum, int32_t handoverReason, double flowControlTime, int32_t readFlag) -{ - handoverNum_ = handoverNum; - handoverReason_ = handoverReason; - flowControlTime_ = flowControlTime; - readFlag_ = readFlag; -} - -std::string HttpClientTask::GetRequestHandoverInfo() -{ - std::string requestHandoverInfo; - if (handoverNum_ <= 0) { - requestHandoverInfo = "no handover"; - return requestHandoverInfo; - } - requestHandoverInfo += "HandoverNum:"; - requestHandoverInfo += std::to_string(handoverNum_); - requestHandoverInfo += ", handoverReason:"; - switch (handoverReason_) { - case HandoverRequestType::INCOMING: - requestHandoverInfo += "flowControl, flowControlTime:"; - break; - case HandoverRequestType::NETWORKERROR: - requestHandoverInfo += "netErr, retransTime:"; - break; - case HandoverRequestType::UNDONE: - requestHandoverInfo += "undone, retransTime:"; - break; - default: - requestHandoverInfo += "unkown type"; - break; - } - requestHandoverInfo += std::to_string(flowControlTime_); - requestHandoverInfo += ", isRead:"; - requestHandoverInfo += readFlag_ == 1 ? "true" : (readFlag_ == 0 ? "false" : "error"); - requestHandoverInfo += ", isStream:"; - requestHandoverInfo += onDataReceive_ ? "true" : "false"; - return requestHandoverInfo; -} -#endif } // namespace HttpClient } // namespace NetStack } // namespace OHOS diff --git a/interfaces/innerkits/http_client/BUILD.gn b/interfaces/innerkits/http_client/BUILD.gn index ddd67d95e923e05b92a8dcbc971cbdfaddb8b9e7..cc2d3ffc455758bfb17ef8296cadc47ef4b0669b 100644 --- a/interfaces/innerkits/http_client/BUILD.gn +++ b/interfaces/innerkits/http_client/BUILD.gn @@ -50,21 +50,11 @@ config("http_client_config") { "HTTP_MULTIPATH_CERT_ENABLE", "HAS_NETSTACK_CHR", ] - include_dirs += [ - "$NETSTACK_DIR/frameworks/js/napi/http/async_context/include", - "$NETSTACK_DIR/frameworks/js/napi/http/constant/include", - "$NETSTACK_DIR/frameworks/js/napi/http/options/include", - "$NETSTACK_DIR/frameworks/js/napi/http/http_exec/include", - ] } if (defined(global_parts_info) && defined(global_parts_info.communication_netmanager_base) && global_parts_info.communication_netmanager_base) { - defines += [ - "HAS_NETMANAGER_BASE=1", - "HTTP_HANDOVER_FEATURE", - "HTTP_STACK_NAME=\"HTTP_CLIEND\"", - ] + defines += [ "HAS_NETMANAGER_BASE=1" ] } } @@ -93,9 +83,6 @@ ohos_shared_library("http_client") { "$NETSTACK_NATIVE_ROOT/http/http_client/http_client_response.cpp", "$NETSTACK_NATIVE_ROOT/http/http_client/http_client_task.cpp", "$NETSTACK_NATIVE_ROOT/http/http_client/http_client_time.cpp", - "$NETSTACK_DIR/frameworks/js/napi/http/async_context/src/request_context.cpp", - "$NETSTACK_DIR/frameworks/js/napi/http/options/src/http_request_options.cpp", - "$NETSTACK_DIR/utils/http_over_curl/src/http_handover_handler.cpp", ] include_dirs = [ @@ -147,7 +134,6 @@ ohos_shared_library("http_client") { "netmanager_base:net_conn_manager_if", "netmanager_base:net_security_config_if", "time_service:time_client", - "napi:ace_napi", ] if (product_name != "ohos-sdk") { external_deps += [ "init:libbegetutil" ] diff --git a/interfaces/innerkits/http_client/include/http_client.h b/interfaces/innerkits/http_client/include/http_client.h index d72237a4b93f17dcef9b55d5986b026aecfc26b3..3b156b5cacad0ad46027020ecba6a6cd00f6b4e6 100644 --- a/interfaces/innerkits/http_client/include/http_client.h +++ b/interfaces/innerkits/http_client/include/http_client.h @@ -31,10 +31,6 @@ #include "http_client_request.h" #include "http_client_task.h" -namespace OHOS::NetStack::HttpOverCurl { - struct TransferCallbacks; -} - namespace OHOS { namespace NetStack { namespace HttpClient { @@ -77,14 +73,6 @@ private: * @param ptr A shared pointer to the HttpClientTask object. */ void StartTask(const std::shared_ptr &ptr); - - /** - * Set RequestInfo callbacks. - * @param callbacks A structure object of callback functions for RequestInfo. - * @param ptr A shared pointer to the HttpClientTask object. - */ - void SetRequestInfoCallbacks( - HttpOverCurl::TransferCallbacks &callbacks, const std::shared_ptr &ptr); }; } // namespace HttpClient } // namespace NetStack diff --git a/interfaces/innerkits/http_client/include/http_client_task.h b/interfaces/innerkits/http_client/include/http_client_task.h index 7310f07c118170d28100318036ada8ddb519c1d3..cc5725dc8ecdec619f1949d1b3157c442ba6df9d 100644 --- a/interfaces/innerkits/http_client/include/http_client_task.h +++ b/interfaces/innerkits/http_client/include/http_client_task.h @@ -357,34 +357,6 @@ private: */ bool SetIpResolve(CURL *handle); -#ifdef HTTP_HANDOVER_FEATURE - /** - * Get the flag which indicating whether the request is success. - * @return Return flag indicating the request whether is success. - */ - bool IsSuccess() - { - return isSuccess_; - } - - /** - * Set the request whether is success. - */ - void SetSuccess(bool isSuccess); - - /** - * Get network switch information. - * @return Return String format for obtaining network sswitching information. - */ - std::string GetRequestHandoverInfo(); - - /** - * Set the request information and print it to the log. - */ - void SetRequestHandoverInfo(int32_t handoverNum, int32_t handoverReason, double flowControlTime, - int32_t readFlag); -#endif - /** * Gets the start and end download position for the HTTP request. * @return Returns the string of range. If the position is invallid, the string is empty. @@ -405,13 +377,6 @@ private: HttpClientRequest request_; HttpClientResponse response_; HttpClientError error_; -#ifdef HTTP_HANDOVER_FEATURE - bool isSuccess_ = false; - int32_t handoverNum_ = -1; - int32_t handoverReason_ = -1; - double flowControlTime_ = 0.0; - int32_t readFlag_ = -1; -#endif TaskType type_; TaskStatus status_; diff --git a/test/unittest/utils/http_handover_handler/BUILD.gn b/test/unittest/utils/http_handover_handler/BUILD.gn index 6bc01e16056bd4849feeb72ce095a2a4c95c10ef..0a461aa22e4cebb215f13f00d8dd8e6ec5d9051a 100644 --- a/test/unittest/utils/http_handover_handler/BUILD.gn +++ b/test/unittest/utils/http_handover_handler/BUILD.gn @@ -33,7 +33,7 @@ ohos_unittest("http_handover_handler_test") { branch_protector_ret = "pac_ret" - module_out_path = "netstack/netstack/http_handover_handler_test" + module_out_path = "netstack/http_handover_handler_test" include_dirs = [ "$NETSTACK_DIR/frameworks/js/napi/http/async_context/include", @@ -51,7 +51,7 @@ ohos_unittest("http_handover_handler_test") { ] include_dirs += utils_include - defines = [] + defines = [ "HTTP_HANDOVER_FEATURE" ] deps = [ "$NETSTACK_DIR/frameworks/js/napi/http:http", @@ -70,35 +70,6 @@ ohos_unittest("http_handover_handler_test") { "hiappevent:hiappevent_innerapi", ] - if (is_mingw || is_mac) { - sources += - [ "$NETSTACK_DIR/utils/common_utils/src/netstack_common_utils.cpp" ] - deps += [ - "//vendor/open_source/boringssl:crypto_boringssl", - "//vendor/open_source/boringssl:ssl_boringssl", - ] - } else { - if (defined(global_parts_info) && - defined(global_parts_info.communication_netmanager_base) && - global_parts_info.communication_netmanager_base) { - external_deps += [ - "netmanager_base:net_conn_manager_if", - "netmanager_base:net_security_config_if", - "netmanager_base:netsys_client", - "hitrace:hitrace_meter", - "samgr:samgr_proxy", - "safwk:system_ability_fwk", - ] - defines += [ - "HAS_NETMANAGER_BASE=1", - "HTTP_HANDOVER_FEATURE", - "HTTP_STACK_NAME=\"test\"", - ] - } else { - defines += [ "HAS_NETMANAGER_BASE=0" ] - } - } - sources = [ "$NETSTACK_DIR/frameworks/js/napi/http/async_context/src/request_context.cpp", "$NETSTACK_DIR/frameworks/js/napi/http/constant/src/constant.cpp", @@ -106,7 +77,7 @@ ohos_unittest("http_handover_handler_test") { "$NETSTACK_DIR/frameworks/js/napi/http/http_exec/src/http_tls_config.cpp", "$NETSTACK_DIR/frameworks/js/napi/http/options/src/http_request_options.cpp", "$NETSTACK_DIR/utils/profiler_utils/src/netstack_network_profiler.cpp", - "$NETSTACK_DIR/utils/http_over_curl/src/http_handover_handler.cpp", + "$NETSTACK_UTILS_ROOT/http_over_curl/src/http_handover_handler.cpp", "http_handover_handler_test.cpp", ] diff --git a/test/unittest/utils/http_handover_handler/http_handover_handler_test.cpp b/test/unittest/utils/http_handover_handler/http_handover_handler_test.cpp index d6c7115d0980da150397dab697b15e2d70482c20..d4a7831a25d34fd3be5a9f76a4de0e28447e50f7 100644 --- a/test/unittest/utils/http_handover_handler/http_handover_handler_test.cpp +++ b/test/unittest/utils/http_handover_handler/http_handover_handler_test.cpp @@ -34,35 +34,6 @@ CURL *GetCurlHandle() curl_easy_setopt(handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); return handle; } - -RequestInfo *GetRequestInfo() -{ - CURL *handle = GetCurlHandle(); - RequestInfo *requestInfo = new RequestInfo(); - requestInfo->easyHandle = handle; - static auto startCallback = +[](CURL *easyHandle, void *opaqueData) {}; - static auto responseCallback = +[](CURLMsg *curlMessage, void *opaqueData) {}; - static auto handoverInfoCallback = +[](void *opaqueData) { - HttpHandoverStackInfo httpHandoverStackInfo; - return httpHandoverStackInfo; - }; - static auto setHandoverInfoCallback = +[](HttpHandoverInfo httpHandoverInfo, void *opaqueData) {}; - HttpOverCurl::TransferCallbacks callbacks = { - .startedCallback = startCallback, - .doneCallback = responseCallback, - .handoverInfoCallback = handoverInfoCallback, - .setHandoverInfoCallback = setHandoverInfoCallback, - }; - requestInfo->callbacks = callbacks; - requestInfo->opaqueData = static_cast(malloc(sizeof(Http::RequestContext))); - return requestInfo; -} - -void DeleteRequestInfo(RequestInfo *requestInfo) -{ - free(requestInfo->opaqueData); - delete requestInfo; -} } class HttpHandoverHandlerTest : public testing::Test { @@ -76,34 +47,34 @@ public: virtual void TearDown() {} }; -HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestSocketTime, TestSize.Level2) +HWTEST_F(HttpHandoverHandlerTest, TestSocketTime, TestSize.Level2) { - std::shared_ptr netHandoverHandler = std::make_shared(); + auto mockHandler = std::make_shared(); curl_socket_t fd = 0; - EXPECT_TRUE(CheckSocketTime(netHandoverHandler.get(), fd)); - EXPECT_TRUE(CheckSocketTime(nullptr, fd)); + EXPECT_EQ(CheckSocketTime(mockHandler.get(), fd), true); } -HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestOpenSocket, TestSize.Level2) +HWTEST_F(HttpHandoverHandlerTest, TestOpenSocket, TestSize.Level2) { - std::shared_ptr netHandoverHandler = std::make_shared(); + auto mockHandler = std::make_shared(); curl_sockaddr addr = {AF_INET, SOCK_STREAM, 0}; curlsocktype purpose = CURLSOCKTYPE_IPCXN; - curl_socket_t sockfd = OpenSocket(netHandoverHandler.get(), purpose, &addr); + curl_socket_t sockfd = OpenSocket(mockHandler.get(), purpose, &addr); EXPECT_GE(sockfd, 0); } -HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestCloseSocketCallback, TestSize.Level2) +HWTEST_F(HttpHandoverHandlerTest, TestCloseSocketCallback, TestSize.Level2) { - auto netHandoverHandler = std::make_shared(); + auto mockHandler = std::make_shared(); curl_socket_t fd = 0; - int ret = CloseSocketCallback(netHandoverHandler.get(), fd); + int ret = CloseSocketCallback(mockHandler.get(), fd); EXPECT_EQ(ret, 0); } HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestEvent, TestSize.Level2) { - std::shared_ptr netHandoverHandler = std::make_shared(); + std::shared_ptr netHandoverHandler; + netHandoverHandler = std::make_shared(); netHandoverHandler->Initialize(); netHandoverHandler->IsInitSuccess(); @@ -119,41 +90,49 @@ HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestEvent, TestSize.Level2) EXPECT_TRUE(!netHandoverHandler->IsItHandoverTimeoutEvent(descriptor)); } -HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestCallbackEvent, TestSize.Level2) +HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestTimeoutTimerEvent, TestSize.Level2) { - std::shared_ptr netHandoverHandler = std::make_shared(); + std::shared_ptr netHandoverHandler; + netHandoverHandler = std::make_shared(); + HandoverCallback(static_cast(netHandoverHandler.get())); HandoverTimerCallback(static_cast(netHandoverHandler.get()), TIMEOUT_MS); HandoverCallback(nullptr); HandoverTimerCallback(nullptr, TIMEOUT_MS); netHandoverHandler->HandoverTimeoutCallback(); +} - RequestInfo *requestInfo = GetRequestInfo(); +HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestHandoverEvent, TestSize.Level2) +{ + std::shared_ptr netHandoverHandler; + netHandoverHandler = std::make_shared(); + CURL *handle = GetCurlHandle(); + RequestInfo *requestInfo = new RequestInfo(); + requestInfo->easyHandle = handle; netHandoverHandler->SetCallback(requestInfo); std::map ongoingRequests; CURLM *multi = curl_multi_init(); + netHandoverHandler->SetHandoverEvent(); netHandoverHandler->HandoverRequestCallback(ongoingRequests, multi); - DeleteRequestInfo(requestInfo); - - CURL *handle = GetCurlHandle(); - EXPECT_EQ(netHandoverHandler->IsRequestRead(handle), 0); + delete requestInfo; } HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestHandoverQuery, TestSize.Level2) { - std::shared_ptr netHandoverHandler = std::make_shared(); - netHandoverHandler->HandoverQuery(); - EXPECT_EQ(netHandoverHandler->GetStatus(), HttpHandoverHandler::INIT); - EXPECT_EQ(netHandoverHandler->GetNetId(), 0); - int32_t netId = 100; - netHandoverHandler->SetNetId(netId); - EXPECT_EQ(netHandoverHandler->GetNetId(), netId); + std::shared_ptr netHandoverHandler; + netHandoverHandler = std::make_shared(); + int32_t status; + int32_t netId; + netHandoverHandler->HandoverQuery(status, netId); + EXPECT_EQ(status, HttpHandoverHandler::INIT); } HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestCheckSocket, TestSize.Level2) { - std::shared_ptr netHandoverHandler = std::make_shared(); + std::shared_ptr netHandoverHandler; + netHandoverHandler = std::make_shared(); + curl_socket_t fd = 0; netHandoverHandler->SetSocketOpenTime(fd); netHandoverHandler->EraseFd(fd); @@ -162,120 +141,108 @@ HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestCheckSocket, TestSize.L HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestTryFlowControl, TestSize.Level2) { - std::shared_ptr netHandoverHandler = std::make_shared(); - RequestInfo *requestInfo = GetRequestInfo(); - netHandoverHandler->SetStatus(HttpHandoverHandler::INIT); - EXPECT_FALSE(netHandoverHandler->TryFlowControl(requestInfo, HandoverRequestType::INCOMING)); - netHandoverHandler->SetStatus(HttpHandoverHandler::START); - EXPECT_TRUE(netHandoverHandler->TryFlowControl(requestInfo, HandoverRequestType::INCOMING)); - EXPECT_TRUE(netHandoverHandler->TryFlowControl(requestInfo, HandoverRequestType::NETWORKERROR)); - EXPECT_TRUE(netHandoverHandler->TryFlowControl(requestInfo, HandoverRequestType::UNDONE)); - netHandoverHandler->SetStatus(HttpHandoverHandler::FATAL); - EXPECT_FALSE(netHandoverHandler->TryFlowControl(requestInfo, HandoverRequestType::INCOMING)); - DeleteRequestInfo(requestInfo); -} - -HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestHandoverRequestCallback, TestSize.Level2) -{ - std::shared_ptr netHandoverHandler = std::make_shared(); - RequestInfo *requestInfo = GetRequestInfo(); - std::map ongoingRequests; - ongoingRequests[requestInfo->easyHandle] = requestInfo; - CURLM *multi = curl_multi_init(); + std::shared_ptr netHandoverHandler; + netHandoverHandler = std::make_shared(); + CURL *handle = GetCurlHandle(); + RequestInfo *requestInfo = new RequestInfo(); + requestInfo->easyHandle = handle; + requestInfo->opaqueData = static_cast(malloc(sizeof(Http::RequestContext))); - netHandoverHandler->SetHandoverEvent(); - netHandoverHandler->SetStatus(HttpHandoverHandler::START); - netHandoverHandler->HandoverRequestCallback(ongoingRequests, multi); - netHandoverHandler->SetHandoverEvent(); - netHandoverHandler->SetStatus(HttpHandoverHandler::END); - netHandoverHandler->HandoverRequestCallback(ongoingRequests, multi); - netHandoverHandler->SetHandoverEvent(); - netHandoverHandler->SetStatus(HttpHandoverHandler::TIMEOUT); - netHandoverHandler->HandoverRequestCallback(ongoingRequests, multi); - netHandoverHandler->SetHandoverEvent(); - netHandoverHandler->SetStatus(HttpHandoverHandler::FATAL); - netHandoverHandler->HandoverRequestCallback(ongoingRequests, multi); - EXPECT_EQ(netHandoverHandler->GetStatus(), HttpHandoverHandler::FATAL); - DeleteRequestInfo(requestInfo); + EXPECT_TRUE(!netHandoverHandler->TryFlowControl(requestInfo, HttpHandoverHandler::RequestType::OLD)); + free(requestInfo->opaqueData); + delete requestInfo; } HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestRetransRequest, TestSize.Level2) { - std::shared_ptr netHandoverHandler = std::make_shared(); + std::shared_ptr netHandoverHandler; + netHandoverHandler = std::make_shared(); std::map ongoingRequests; CURLM *multi = curl_multi_init(); - RequestInfo *requestInfo = GetRequestInfo(); - EXPECT_TRUE(netHandoverHandler->RetransRequest(ongoingRequests, multi, requestInfo)); - DeleteRequestInfo(requestInfo); -} - -HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestIsNetworkErrorTypeCorrect, TestSize.Level2) -{ - std::shared_ptr netHandoverHandler = std::make_shared(); - EXPECT_TRUE(netHandoverHandler->IsNetworkErrorTypeCorrect(CURLE_SEND_ERROR)); - EXPECT_TRUE(netHandoverHandler->IsNetworkErrorTypeCorrect(CURLE_RECV_ERROR)); - - EXPECT_TRUE(netHandoverHandler->IsNetworkErrorTypeCorrect(CURLE_COULDNT_RESOLVE_HOST)); - EXPECT_TRUE(netHandoverHandler->IsNetworkErrorTypeCorrect(CURLE_COULDNT_CONNECT)); - EXPECT_TRUE(netHandoverHandler->IsNetworkErrorTypeCorrect(CURLE_SSL_CONNECT_ERROR)); - EXPECT_TRUE(netHandoverHandler->IsNetworkErrorTypeCorrect(CURLE_QUIC_CONNECT_ERROR)); + CURL *handle = GetCurlHandle(); + RequestInfo *requestInfo = new RequestInfo(); + requestInfo->easyHandle = handle; - EXPECT_FALSE(netHandoverHandler->IsNetworkErrorTypeCorrect(CURLE_OK)); + EXPECT_TRUE(netHandoverHandler->RetransRequest(ongoingRequests, multi, requestInfo)); + delete requestInfo; } HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestCheckRequestCanRetrans, TestSize.Level2) { - std::shared_ptr netHandoverHandler = std::make_shared(); - RequestInfo *requestInfo = GetRequestInfo(); + std::shared_ptr netHandoverHandler; + netHandoverHandler = std::make_shared(); + CURL *handle = GetCurlHandle(); + RequestInfo *requestInfo = new RequestInfo(); + requestInfo->easyHandle = handle; + requestInfo->opaqueData = static_cast(malloc(sizeof(Http::RequestContext))); EXPECT_TRUE(netHandoverHandler->CheckRequestCanRetrans( - requestInfo, HandoverRequestType::INCOMING, CURLE_SEND_ERROR)); + requestInfo, HttpHandoverHandler::RequestType::INCOMING, CURLE_SEND_ERROR)); EXPECT_TRUE(netHandoverHandler->CheckRequestCanRetrans( - requestInfo, HandoverRequestType::NETWORKERROR, CURLE_RECV_ERROR)); + requestInfo, HttpHandoverHandler::RequestType::NETWORKERROR, CURLE_RECV_ERROR)); EXPECT_TRUE(netHandoverHandler->CheckRequestCanRetrans( - requestInfo, HandoverRequestType::OLD, CURLE_COULDNT_RESOLVE_HOST)); + requestInfo, HttpHandoverHandler::RequestType::OLD, CURLE_COULDNT_RESOLVE_HOST)); EXPECT_TRUE(netHandoverHandler->CheckRequestCanRetrans( - requestInfo, HandoverRequestType::UNDONE, CURLE_COULDNT_CONNECT)); - DeleteRequestInfo(requestInfo); + requestInfo, HttpHandoverHandler::RequestType::UNDONE, CURLE_COULDNT_CONNECT)); + free(requestInfo->opaqueData); + delete requestInfo; } HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestUndoneRequestHandle, TestSize.Level2) { - std::shared_ptr netHandoverHandler = std::make_shared(); - RequestInfo *requestInfo = GetRequestInfo(); + std::shared_ptr netHandoverHandler; + netHandoverHandler = std::make_shared(); + CURL *handle = GetCurlHandle(); + RequestInfo *requestInfo = new RequestInfo(); + requestInfo->easyHandle = handle; + requestInfo->opaqueData = static_cast(malloc(sizeof(Http::RequestContext))); std::map ongoingRequests; ongoingRequests[requestInfo->easyHandle] = requestInfo; CURLM *multi = curl_multi_init(); + netHandoverHandler->UndoneRequestHandle(ongoingRequests, multi); - DeleteRequestInfo(requestInfo); + free(requestInfo->opaqueData); + delete requestInfo; } HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestProcessRequestErr, TestSize.Level2) { - std::shared_ptr netHandoverHandler = std::make_shared(); + std::shared_ptr netHandoverHandler; + netHandoverHandler = std::make_shared(); std::map ongoingRequests; CURLM *multi = curl_multi_init(); - RequestInfo *requestInfo = GetRequestInfo(); + CURL *handle = GetCurlHandle(); + RequestInfo *requestInfo = new RequestInfo(); + requestInfo->easyHandle = handle; + requestInfo->opaqueData = static_cast(malloc(sizeof(Http::RequestContext))); CURLMsg message; message.msg = CURLMSG_DONE; message.data.result = CURLE_SEND_ERROR; + EXPECT_EQ(netHandoverHandler->ProcessRequestErr(ongoingRequests, multi, requestInfo, &message), 0); - DeleteRequestInfo(requestInfo); + free(requestInfo->opaqueData); + delete requestInfo; } -HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestProcessRequestNetErrorErrorType, TestSize.Level2) +HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestProcessRequestNetError, TestSize.Level2) { - std::shared_ptr netHandoverHandler = std::make_shared(); + std::shared_ptr netHandoverHandler; + netHandoverHandler = std::make_shared(); std::map ongoingRequests; CURLM *multi = curl_multi_init(); - RequestInfo *requestInfo = GetRequestInfo(); + CURL *handle = GetCurlHandle(); + RequestInfo *requestInfo = new RequestInfo(); + requestInfo->easyHandle = handle; + requestInfo->opaqueData = static_cast(malloc(sizeof(Http::RequestContext))); + ongoingRequests[requestInfo->easyHandle] = requestInfo; CURLMsg message; + message.msg = CURLMSG_DONE; + message.data.result = CURLE_SEND_ERROR; netHandoverHandler->SetHandoverEvent(); netHandoverHandler->HandoverRequestCallback(ongoingRequests, multi); - message.msg = CURLMSG_DONE; - message.data.result = CURLE_SEND_ERROR; - EXPECT_FALSE(netHandoverHandler->ProcessRequestNetError(ongoingRequests, multi, requestInfo, &message)); - DeleteRequestInfo(requestInfo); + netHandoverHandler->ProcessRequestNetError(ongoingRequests, multi, requestInfo, &message); + free(requestInfo->opaqueData); + delete requestInfo; } } \ No newline at end of file diff --git a/utils/http_over_curl/include/epoll_request_handler.h b/utils/http_over_curl/include/epoll_request_handler.h index 68d1054034ca8e0cb5ec1fa169a5a9471b473f5e..94d40f342dad03a872756c5d8aad5047027d3179 100644 --- a/utils/http_over_curl/include/epoll_request_handler.h +++ b/utils/http_over_curl/include/epoll_request_handler.h @@ -25,7 +25,6 @@ #include "thread_safe_storage.h" #include "transfer_callbacks.h" -#include "request_info.h" namespace OHOS::NetStack::HttpOverCurl { @@ -36,7 +35,8 @@ public: explicit EpollRequestHandler(int sleepTimeoutMs = 5000); ~EpollRequestHandler(); - void Process(CURL *easyHandle, TransferCallbacks callbacks, void *opaqueData = nullptr); + void Process(CURL *easyHandle, const TransferStartedCallback &startedCallback, + const TransferDoneCallback &responseCallback, void *opaqueData = nullptr); private: void WorkingThread(); diff --git a/utils/http_over_curl/include/http_handover_handler.h b/utils/http_over_curl/include/http_handover_handler.h index 7e9866e531d191f433f23c90eeb37dc6e4cfdd5a..0919c773166c055a89ce57301dbcf5427d50aeee 100644 --- a/utils/http_over_curl/include/http_handover_handler.h +++ b/utils/http_over_curl/include/http_handover_handler.h @@ -37,10 +37,10 @@ typedef void *(*HTTP_HAND_OVER_INIT)(void *user, void (*HMS_NetworkBoost_Handove void (*HMS_NetworkBoost_HandoverTimerCallback)(void *, long), const char* stackName); typedef int32_t (*HTTP_HAND_OVER_UNINIT)(void *handle); typedef void (*HTTP_HAND_OVER_QUERY)(void *handle, int32_t *status, int32_t *netId); -typedef void (*HTTP_HAND_OVER_ADD)(void *handle, void *userp, int32_t type, int32_t readFlag); +typedef void (*HTTP_HAND_OVER_ADD)(void *handle, void *userp, int32_t type, int32_t isRead); typedef void (*HTTP_HAND_OVER_DEL)(void *handle, void *userp, bool isSuccess); typedef int32_t (*HTTP_HAND_OVER_QUERY_REQUEST)(void *handle, void *userp, int32_t *handOverReason, - double *flowControlTime, int32_t *readFlag); + double *flowControlTime, int32_t *isRead); typedef void (*HTTP_HAND_OVER_REPORT_TIMEOUT)(void *handle); void HandoverCallback(void *user); @@ -52,6 +52,12 @@ int CloseSocketCallback(void *user, curl_socket_t fd); class HttpHandoverHandler { public: enum { INIT, START, CONTINUE, END, FATAL, TIMEOUT }; + enum RequestType { + OLD, // old request before network change + INCOMING, // new request during network change + NETWORKERROR, // old request of network error + UNDONE // undone old request after network change + }; explicit HttpHandoverHandler(); ~HttpHandoverHandler(); @@ -69,7 +75,7 @@ public: bool Initialize(); void SetCallback(RequestInfo *request); void SetHandoverInfo(RequestInfo *requestInfo); - void HandoverQuery(); + void HandoverQuery(int32_t &status, int32_t &netId); bool CheckSocketOpentimeLessThanEndTime(curl_socket_t fd); void SetSocketOpenTime(curl_socket_t fd); void EraseFd(curl_socket_t fd); @@ -78,16 +84,11 @@ public: void UndoneRequestHandle(std::map &ongoingRequests, CURLM *multi); int32_t IsRequestRead(CURL *easyHandle); int32_t IsRequestRead(CURL *easyHandle, time_t &recvtime, time_t &sendtime); - bool IsNetworkErrorTypeCorrect(CURLcode result); bool ProcessRequestNetError(std::map &ongoingRequests, CURLM *multi, RequestInfo *requestInfo, CURLMsg *msg); void AddRequest(RequestInfo *requestInfo, int32_t type); - void DelRequest(RequestInfo *requestInfo); - int32_t QueryRequest(void *userp, int32_t &handOverReason, double &flowControlTime, int32_t &readFlag); - int32_t GetStatus(); - void SetStatus(int32_t status); - int32_t GetNetId(); - void SetNetId(int32_t netId); + void DelRequest(void *userp); + int32_t QueryRequest(void *userp, int32_t &handOverReason, double &flowControlTime, int32_t &isRead); private: void *netHandoverHandler_ = nullptr; void *httpHandoverManager_ = nullptr; @@ -108,8 +109,6 @@ private: int endTime_ = 0; int timeoutTime_ = 0; int retrans_ = 0; - int32_t status_ = HttpHandoverHandler::INIT; - int32_t netId_ = 0; }; } // namespace OHOS::NetStack::HttpOverCurl diff --git a/utils/http_over_curl/include/http_handover_info.h b/utils/http_over_curl/include/http_handover_info.h deleted file mode 100644 index 09f1b6615f45ce4a13a7df0153b4fb6c734bd00d..0000000000000000000000000000000000000000 --- a/utils/http_over_curl/include/http_handover_info.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef COMMUNICATIONNETSTACK_HTTP_HANDOVER_INFO_H -#define COMMUNICATIONNETSTACK_HTTP_HANDOVER_INFO_H -struct HttpHandoverStackInfo { - unsigned int taskId = 0; - uint32_t readTimeout = 0; - uint32_t connectTimeout = 0; - std::string method = "init"; - std::string requestUrl = "init"; - bool isInStream = false; - bool isSuccess = false; -}; - -struct HttpHandoverInfo { - int32_t handoverNum = 0; - int32_t handoverReason = 0; - double flowControlTime = 0.0; - int32_t readFlag = 0; -}; - -enum HandoverRequestType { - OLD, // old request before network change - INCOMING, // new request during network change - NETWORKERROR, // old request of network error - UNDONE // undone old request after network change -}; -#endif // COMMUNICATIONNETSTACK_HTTP_HANDOVER_INFO_H \ No newline at end of file diff --git a/utils/http_over_curl/include/request_info.h b/utils/http_over_curl/include/request_info.h index 1ee9eb164094ad6bffb908af59f614b2d53f60a9..8025146cc7bfe4d702a30cc286cab5995ca09517 100644 --- a/utils/http_over_curl/include/request_info.h +++ b/utils/http_over_curl/include/request_info.h @@ -21,18 +21,11 @@ #include "transfer_callbacks.h" namespace OHOS::NetStack::HttpOverCurl { -struct TransferCallbacks { - TransferStartedCallback startedCallback; - TransferDoneCallback doneCallback; -#ifdef HTTP_HANDOVER_FEATURE - TransferHandoverInfoCallback handoverInfoCallback; - SetHandoverInfoCallback setHandoverInfoCallback; -#endif -}; struct RequestInfo { CURL *easyHandle; - TransferCallbacks callbacks; + TransferStartedCallback startedCallback; + TransferDoneCallback doneCallback; void *opaqueData; }; diff --git a/utils/http_over_curl/include/transfer_callbacks.h b/utils/http_over_curl/include/transfer_callbacks.h index 9fdc3154dd18cf2f9a55d67a9be77dbb6e6770ed..f484b1c62e8d7326af7dbc005690d40c21766ede 100644 --- a/utils/http_over_curl/include/transfer_callbacks.h +++ b/utils/http_over_curl/include/transfer_callbacks.h @@ -19,18 +19,11 @@ #include #include "curl/curl.h" -#ifdef HTTP_HANDOVER_FEATURE -#include "http_handover_info.h" -#endif namespace OHOS::NetStack::HttpOverCurl { using TransferDoneCallback = std::function; using TransferStartedCallback = std::function; -#ifdef HTTP_HANDOVER_FEATURE -using TransferHandoverInfoCallback = std::function; -using SetHandoverInfoCallback = std::function; -#endif } // namespace OHOS::NetStack::HttpOverCurl diff --git a/utils/http_over_curl/src/epoll_multi_driver.cpp b/utils/http_over_curl/src/epoll_multi_driver.cpp index 628f2814c63ba697778d4add86236066a82abbdc..98793bab06137febec8981605cb0700e81f464e0 100644 --- a/utils/http_over_curl/src/epoll_multi_driver.cpp +++ b/utils/http_over_curl/src/epoll_multi_driver.cpp @@ -117,7 +117,7 @@ void EpollMultiDriver::IncomingRequestCallback() for (auto &request : requestsToAdd) { #ifdef HTTP_HANDOVER_FEATURE if (netHandoverHandler_ && - netHandoverHandler_->TryFlowControl(request, HandoverRequestType::INCOMING)) { + netHandoverHandler_->TryFlowControl(request, HttpHandoverHandler::RequestType::INCOMING)) { continue; } #endif @@ -128,8 +128,8 @@ void EpollMultiDriver::IncomingRequestCallback() continue; } - if (request->callbacks.startedCallback) { - request->callbacks.startedCallback(request->easyHandle, request->opaqueData); + if (request->startedCallback) { + request->startedCallback(request->easyHandle, request->opaqueData); } } } @@ -187,8 +187,8 @@ __attribute__((no_sanitize("cfi"))) void EpollMultiDriver::CheckMultiInfo() break; } #endif - if (requestInfo != nullptr && requestInfo->callbacks.doneCallback) { - requestInfo->callbacks.doneCallback(message, requestInfo->opaqueData); + if (requestInfo != nullptr && requestInfo->doneCallback) { + requestInfo->doneCallback(message, requestInfo->opaqueData); } delete requestInfo; break; diff --git a/utils/http_over_curl/src/epoll_request_handler.cpp b/utils/http_over_curl/src/epoll_request_handler.cpp index 6f6f3d543ba5088c03b31419d778e50bc428a750..71108813ca09df2536ec05a6e87e4032942d5476 100644 --- a/utils/http_over_curl/src/epoll_request_handler.cpp +++ b/utils/http_over_curl/src/epoll_request_handler.cpp @@ -38,9 +38,10 @@ EpollRequestHandler::~EpollRequestHandler() } } -void EpollRequestHandler::Process(CURL *easyHandle, const TransferCallbacks callbacks, void *opaqueData) +void EpollRequestHandler::Process(CURL *easyHandle, const TransferStartedCallback &startedCallback, + const TransferDoneCallback &responseCallback, void *opaqueData) { - auto requestInfo = new RequestInfo{easyHandle, callbacks, opaqueData}; + auto requestInfo = new RequestInfo{easyHandle, startedCallback, responseCallback, opaqueData}; incomingQueue_->Push(requestInfo); auto start = [this]() { diff --git a/utils/http_over_curl/src/http_handover_handler.cpp b/utils/http_over_curl/src/http_handover_handler.cpp index 1fb0147fa83816941ecaea2bb1dbfd0c1ee34f17..ece6b04db379a5c721503b267425c03d1dad4d59 100644 --- a/utils/http_over_curl/src/http_handover_handler.cpp +++ b/utils/http_over_curl/src/http_handover_handler.cpp @@ -111,7 +111,7 @@ static bool IsConnectError(CURLcode result) return false; } -bool HttpHandoverHandler::IsNetworkErrorTypeCorrect(CURLcode result) +static bool IsNetworkErrorTypeCorrect(CURLcode result) { if (IsIoError(result) || IsConnectError(result)) { return true; @@ -150,16 +150,13 @@ bool HttpHandoverHandler::Initialize() httpHandoverQueryRequest_ == nullptr || httpHandoverReportTimeout_ == nullptr); if (hasFuncNull) { NETSTACK_LOGE("http handover wrapper symbol failed, error: %{public}s", dlerror()); - dlclose(netHandoverHandler_); - netHandoverHandler_ = nullptr; return false; } NETSTACK_LOGD("NetHandover enabled"); - httpHandoverManager_ = httpHandoverInit_(this, HandoverCallback, HandoverTimerCallback, HTTP_STACK_NAME); + const char* stackName = "http"; + httpHandoverManager_ = httpHandoverInit_(this, HandoverCallback, HandoverTimerCallback, stackName); if (httpHandoverManager_ == nullptr) { NETSTACK_LOGE("http handover manager init fail"); - dlclose(netHandoverHandler_); - netHandoverHandler_ = nullptr; return false; } return true; @@ -217,13 +214,13 @@ void HttpHandoverHandler::SetHandoverTimeoutEvent(long timeoutMs) } } -void HttpHandoverHandler::HandoverQuery() +void HttpHandoverHandler::HandoverQuery(int32_t &status, int32_t &netId) { if (httpHandoverQuery_ == nullptr || httpHandoverManager_ == nullptr) { NETSTACK_LOGE("nullptr param error"); return; } - httpHandoverQuery_(httpHandoverManager_, &status_, &netId_); + httpHandoverQuery_(httpHandoverManager_, &status, &netId); } bool HttpHandoverHandler::CheckSocketOpentimeLessThanEndTime(curl_socket_t fd) @@ -265,28 +262,29 @@ void HttpHandoverHandler::SetCallback(RequestInfo *request) bool HttpHandoverHandler::TryFlowControl(RequestInfo *requestInfo, int32_t requestType) { - HandoverQuery(); - if (GetStatus() == HttpHandoverHandler::FATAL) { + int32_t status = -1; + int32_t netId = -1; + HandoverQuery(status, netId); + if (status == HttpHandoverHandler::FATAL) { NETSTACK_LOGE("Handover status fatal, feature disable."); return false; } SetCallback(requestInfo); - if (GetStatus() == HttpHandoverHandler::START) { + if (status == HttpHandoverHandler::START) { handoverQueue_.insert(requestInfo); + auto context = static_cast(requestInfo->opaqueData); std::string reason; - if (requestType == HandoverRequestType::INCOMING) { + if (requestType == RequestType::INCOMING) { reason = "incoming request"; - } else if (requestType == HandoverRequestType::NETWORKERROR) { + } else if (requestType == RequestType::NETWORKERROR) { reason = "network error"; } - HttpHandoverStackInfo httpHandoverStackInfo = - requestInfo->callbacks.handoverInfoCallback(requestInfo->opaqueData); - NETSTACK_LOGD("taskid=%{public}d, FlowControl reason:%{public}s", httpHandoverStackInfo.taskId, reason.c_str()); + NETSTACK_LOGD("taskid=%{public}d, FlowControl reason:%{public}s", context->GetTaskId(), reason.c_str()); AddRequest(requestInfo, requestType); return true; } - AddRequest(requestInfo, HandoverRequestType::OLD); + AddRequest(requestInfo, RequestType::OLD); return false; } @@ -304,38 +302,37 @@ bool HttpHandoverHandler::RetransRequest(std::map &ongoin bool HttpHandoverHandler::CheckRequestCanRetrans(RequestInfo *request, int32_t requestType, CURLcode result) { - if (request == nullptr) { - return false; - } time_t recvtime = 0; time_t sendtime = 0; - int32_t readFlag = IsRequestRead(request->easyHandle, recvtime, sendtime); - if (readFlag == -1) { + int32_t isRead = IsRequestRead(request->easyHandle, recvtime, sendtime); + if (isRead == -1) { return false; } - HttpHandoverStackInfo httpHandoverStackInfo = request->callbacks.handoverInfoCallback(request->opaqueData); - bool isSafe = (httpHandoverStackInfo.method == METHOD_GET || httpHandoverStackInfo.method == METHOD_HEAD || - httpHandoverStackInfo.method == METHOD_OPTIONS || httpHandoverStackInfo.method == METHOD_TRACE); + auto context = static_cast(request->opaqueData); + auto method = context->options.GetMethod(); + int isInstream = context->IsRequestInStream(); + uint32_t readTimeout = context->options.GetReadTimeout(); + uint32_t connecttimeout = context->options.GetConnectTimeout(); + bool isSafe = (method == METHOD_GET || method == METHOD_HEAD || method == METHOD_OPTIONS || method == METHOD_TRACE); bool ret = false; - if (IsConnectError(result) || sendtime == 0 || (isSafe && (!httpHandoverStackInfo.isInStream || readFlag == 0))) { + if (IsConnectError(result) || sendtime == 0 || (isSafe && (!isInstream || isRead == 0))) { ret = true; } - if (requestType == HandoverRequestType::INCOMING || requestType == HandoverRequestType::NETWORKERROR) { + if (requestType == RequestType::INCOMING || requestType == RequestType::NETWORKERROR) { return ret; } std::string type; - if (requestType == HandoverRequestType::OLD) { + if (requestType == RequestType::OLD) { type = "old request"; } else { type = "undone request"; } NETSTACK_LOGI( "taskid=%{public}d,requestType:%{public}s,canRetrans:%{public}d," - "method:%{public}s,isInStream:%{public}d,recvtime:%{public}d,sendtime:%{public}d,readTimeout:%{public}u," - "connecttimeout:%{public}u,url:%{public}s ", - httpHandoverStackInfo.taskId, type.c_str(), (int)ret, httpHandoverStackInfo.method.c_str(), - httpHandoverStackInfo.isInStream, (int)recvtime, (int)sendtime, httpHandoverStackInfo.readTimeout, - httpHandoverStackInfo.connectTimeout, httpHandoverStackInfo.requestUrl.c_str()); + "method:%{public}s,isInstream:%{public}d,recvtime:%{public}d,sendtime:%{public}d,readTimeout:%{public}u," + "connecttimeout:%{public}u,url:%{public}s ", context->GetTaskId(), type.c_str(), (int)ret, method.c_str(), + isInstream, (int)recvtime, (int)sendtime, readTimeout, connecttimeout, context->options.GetUrl().c_str()); + return ret; } @@ -344,19 +341,19 @@ void HttpHandoverHandler::UndoneRequestHandle(std::map &o for (auto it = ongoingRequests.begin(); it != ongoingRequests.end();) { auto handle = it->first; auto requestInfo = it->second; - if (CheckRequestCanRetrans(requestInfo, HandoverRequestType::UNDONE, CURLE_OK)) { + if (CheckRequestCanRetrans(requestInfo, RequestType::UNDONE, CURLE_OK)) { curl_multi_remove_handle(multi, handle); if (RetransRequest(ongoingRequests, multi, requestInfo)) { ++retrans_; - AddRequest(requestInfo, HandoverRequestType::UNDONE); + AddRequest(requestInfo, RequestType::UNDONE); ++it; continue; } - if (requestInfo != nullptr && requestInfo->callbacks.doneCallback) { + if (requestInfo != nullptr && requestInfo->doneCallback) { CURLMsg message; message.msg = CURLMSG_DONE; message.data.result = CURLE_SEND_ERROR; - requestInfo->callbacks.doneCallback(&message, requestInfo->opaqueData); + requestInfo->doneCallback(&message, requestInfo->opaqueData); } it = ongoingRequests.erase(it); } else { @@ -368,18 +365,21 @@ void HttpHandoverHandler::UndoneRequestHandle(std::map &o void HttpHandoverHandler::HandoverRequestCallback(std::map &ongoingRequests, CURLM *multi) { handOverEvent_->Reset(); - HandoverQuery(); - NETSTACK_LOGD("Enter HandoverRequestCallback status %{public}d", GetStatus()); - if (GetStatus() == HttpHandoverHandler::START) { + int32_t status = -1; + int32_t netId = -1; + HandoverQuery(status, netId); + + NETSTACK_LOGD("Enter HandoverRequestCallback status %{public}d", status); + if (status == HttpHandoverHandler::START) { NETSTACK_LOGD("start ongoingRequests:%{public}d", (int)ongoingRequests.size()); for (auto &request : ongoingRequests) { if (requestEndtime_.count(request.second) == 0) { requestEndtime_[request.second] = endTime_; } - (void)CheckRequestCanRetrans(request.second, HandoverRequestType::OLD, CURLE_OK); + (void)CheckRequestCanRetrans(request.second, RequestType::OLD, CURLE_OK); } - } else if (GetStatus() == HttpHandoverHandler::END || GetStatus() == HttpHandoverHandler::TIMEOUT) { - (GetStatus() == HttpHandoverHandler::END) ? ++endTime_ : ++timeoutTime_; + } else if (status == HttpHandoverHandler::END || status == HttpHandoverHandler::TIMEOUT) { + (status == HttpHandoverHandler::END) ? ++endTime_ : ++timeoutTime_; NETSTACK_LOGD("endTime:%{public}d, timeoutTime: %{public}d, ongoingRequests:%{public}d, retrans count before " "end:%{public}d", endTime_, timeoutTime_, (int)ongoingRequests.size(), retrans_); UndoneRequestHandle(ongoingRequests, multi); @@ -391,7 +391,7 @@ void HttpHandoverHandler::HandoverRequestCallback(std::mapResetEvent(); handOverTimerEvent_->Stop(); - HandoverQuery(); - if (GetStatus() == HttpHandoverHandler::END) { + int32_t status = -1; + int32_t netId = -1; + HandoverQuery(status, netId); + if (status == HttpHandoverHandler::END) { return; } if (httpHandoverManager_ == nullptr) { @@ -447,19 +449,17 @@ bool HttpHandoverHandler::ProcessRequestErr(std::map &ong void HttpHandoverHandler::SetHandoverInfo(RequestInfo *requestInfo) { if (requestInfo != nullptr) { - int32_t handoverReason = 0; + int32_t handOverReason = 0; double flowControlTime = 0; - int32_t readFlag = 0; - int32_t handOverNum = - QueryRequest(requestInfo->opaqueData, handoverReason, flowControlTime, readFlag); - HttpHandoverInfo httpHandoverInfo; - httpHandoverInfo.handoverNum = handOverNum; - httpHandoverInfo.handoverReason = handoverReason; - httpHandoverInfo.flowControlTime = flowControlTime; - httpHandoverInfo.readFlag = readFlag; - requestInfo->callbacks.setHandoverInfoCallback(httpHandoverInfo, requestInfo->opaqueData); - DelRequest(requestInfo); + int32_t isRead = 0; + int32_t handOverNum = QueryRequest(requestInfo->opaqueData, handOverReason, flowControlTime, isRead); + auto context = static_cast(requestInfo->opaqueData); + if (context) { + context->SetRequestHandoverInfo(handOverNum, handOverReason, flowControlTime, isRead); + } + DelRequest(requestInfo->opaqueData); } + return; } bool HttpHandoverHandler::ProcessRequestNetError(std::map &ongoingRequests, CURLM *multi, @@ -473,16 +473,16 @@ bool HttpHandoverHandler::ProcessRequestNetError(std::map if (!msg || !IsNetworkErrorTypeCorrect(msg->data.result)) { return false; } - if (!CheckRequestCanRetrans(requestInfo, HandoverRequestType::NETWORKERROR, msg->data.result)) { + if (!CheckRequestCanRetrans(requestInfo, RequestType::NETWORKERROR, msg->data.result)) { return false; } - if (TryFlowControl(requestInfo, HandoverRequestType::NETWORKERROR)) { + if (TryFlowControl(requestInfo, RequestType::NETWORKERROR)) { ++retrans_; return true; } if (endTime == endTime_ - 1) { NETSTACK_LOGD("networkerror after end status"); - AddRequest(requestInfo, HandoverRequestType::NETWORKERROR); + AddRequest(requestInfo, RequestType::NETWORKERROR); return RetransRequest(ongoingRequests, multi, requestInfo); } return false; @@ -498,43 +498,27 @@ void HttpHandoverHandler::AddRequest(RequestInfo *requestInfo, int32_t type) IsRequestRead(requestInfo->easyHandle)); } -void HttpHandoverHandler::DelRequest(RequestInfo *requestInfo) +void HttpHandoverHandler::DelRequest(void *userp) { if (httpHandoverManager_ == nullptr) { NETSTACK_LOGE("httpHandoverManager_ nullptr error"); return; } - HttpHandoverStackInfo httpHandoverStackInfo = requestInfo->callbacks.handoverInfoCallback(requestInfo->opaqueData); - httpHandoverDelRequest_(httpHandoverManager_, requestInfo->opaqueData, httpHandoverStackInfo.isSuccess); + auto context = static_cast(userp); + if (context->IsParseOK() && context->IsExecOK()) { + httpHandoverDelRequest_(httpHandoverManager_, userp, true); + return; + } + httpHandoverDelRequest_(httpHandoverManager_, userp, false); } int32_t HttpHandoverHandler::QueryRequest(void *userp, int32_t &handOverReason, double &flowControlTime, - int32_t &readFlag) + int32_t &isRead) { if (httpHandoverManager_ == nullptr) { NETSTACK_LOGE("httpHandoverManager_ nullptr error"); return -1; } - return httpHandoverQueryRequest_(httpHandoverManager_, userp, &handOverReason, &flowControlTime, &readFlag); -} - -int32_t HttpHandoverHandler::GetStatus() -{ - return status_; -} - -void HttpHandoverHandler::SetStatus(int32_t status) -{ - status_ = status; -} - -int32_t HttpHandoverHandler::GetNetId() -{ - return netId_; -} - -void HttpHandoverHandler::SetNetId(int32_t netId) -{ - netId_ = netId; + return httpHandoverQueryRequest_(httpHandoverManager_, userp, &handOverReason, &flowControlTime, &isRead); } } \ No newline at end of file