From f4a77d459cf95adc965326851cda41c5fa0c5a72 Mon Sep 17 00:00:00 2001 From: YangWeimin Date: Wed, 10 Sep 2025 15:35:50 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A4=9A=E7=BD=91=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BC=A0=E5=8F=82=EF=BC=8C=E8=A6=86=E7=9B=96?= =?UTF-8?q?=E7=8E=87=E6=8F=90=E5=8D=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: YangWeimin --- .../async_context/include/request_context.h | 18 ++++--------- .../async_context/src/request_context.cpp | 27 +++++++++---------- .../js/napi/http/http_exec/src/http_exec.cpp | 5 +--- .../native/http/http_client/http_client.cpp | 5 +--- .../http/http_client/http_client_task.cpp | 25 ++++++++--------- .../http_client/include/http_client_task.h | 12 ++++----- test/unittest/http_client/BUILD.gn | 1 + .../http_client/HttpClientTaskTest.cpp | 23 +++++++--------- .../http_handover_handler_test.cpp | 1 + .../include/http_handover_handler.h | 2 +- .../include/http_handover_info.h | 8 ++++-- .../src/http_handover_handler.cpp | 2 +- 12 files changed, 55 insertions(+), 74 deletions(-) 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 2e0453259..16e317264 100644 --- a/frameworks/js/napi/http/async_context/include/request_context.h +++ b/frameworks/js/napi/http/async_context/include/request_context.h @@ -28,6 +28,9 @@ #include "netstack_network_profiler.h" #endif #include "request_tracer.h" +#ifdef HTTP_HANDOVER_FEATURE +#include "http_andover_info.h" +#endif namespace OHOS::NetStack::Http { static constexpr const uint32_t MAGIC_NUMBER = 0x86161616; @@ -50,17 +53,6 @@ struct CertsPath { std::string certFile; }; -#ifdef HTTP_HANDOVER_FEATURE -struct RequestHandoverInfo { - RequestHandoverInfo() = default; - ~RequestHandoverInfo() = default; - int32_t handoverNum = 0; - int32_t handoverReason = 0; - double flowControlTime = 0; - int32_t readFlag = 0; -}; -#endif - class RequestContext final : public BaseContext { public: friend class HttpExec; @@ -170,7 +162,7 @@ public: std::string GetPinnedPubkey() const; #ifdef HTTP_HANDOVER_FEATURE - void SetRequestHandoverInfo(int32_t handoverNum, int32_t handoverReason, double flowControlTime, int32_t readFlag); + void SetRequestHandoverInfo(const HttpHandoverInfo &httpHandoverInfo); std::string GetRequestHandoverInfo(); #endif @@ -203,7 +195,7 @@ private: #endif CURL *curlHandle_; #ifdef HTTP_HANDOVER_FEATURE - RequestHandoverInfo requestHandoverInfo_; + HttpHandoverInfo httpHandoverInfo_; #endif RequestTracer::Trace trace_; 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 eaf3013a3..09d386c14 100755 --- a/frameworks/js/napi/http/async_context/src/request_context.cpp +++ b/frameworks/js/napi/http/async_context/src/request_context.cpp @@ -34,9 +34,6 @@ #if HAS_NETMANAGER_BASE #include "http_network_message.h" #endif -#ifdef HTTP_HANDOVER_FEATURE -#include "http_handover_handler.h" -#endif static constexpr const int PARAM_JUST_URL = 1; @@ -1046,27 +1043,23 @@ std::string RequestContext::GetPinnedPubkey() const } #ifdef HTTP_HANDOVER_FEATURE -void RequestContext::SetRequestHandoverInfo(int32_t handoverNum, int32_t handoverReason, double flowControlTime, - int32_t readFlag) +void RequestContext::SetRequestHandoverInfo(const HttpHandoverInfo &httpHandoverInfo) { - requestHandoverInfo_.handoverNum = handoverNum; - requestHandoverInfo_.handoverReason = handoverReason; - requestHandoverInfo_.flowControlTime = flowControlTime; - requestHandoverInfo_.readFlag = readFlag; + httpHandoverInfo_ = httpHandoverInfo; } std::string RequestContext::GetRequestHandoverInfo() { std::string requestHandoverInfo; - if (requestHandoverInfo_.handoverNum <= 0) { + if (httpHandoverInfo_.handoverNum <= 0) { requestHandoverInfo = "no handover"; return requestHandoverInfo; } - int32_t readFlag = requestHandoverInfo_.readFlag; + int32_t readFlag = httpHandoverInfo_.readFlag; requestHandoverInfo += "HandoverNum:"; - requestHandoverInfo += std::to_string(requestHandoverInfo_.handoverNum); + requestHandoverInfo += std::to_string(httpHandoverInfo_.handoverNum); requestHandoverInfo += ", handoverReason:"; - switch (requestHandoverInfo_.handoverReason) { + switch (httpHandoverInfo_.handoverReason) { case HandoverRequestType::INCOMING: requestHandoverInfo += "flowControl, flowControlTime:"; break; @@ -1080,9 +1073,13 @@ std::string RequestContext::GetRequestHandoverInfo() requestHandoverInfo += "unknown type"; break; } - requestHandoverInfo += std::to_string(requestHandoverInfo_.flowControlTime); + requestHandoverInfo += std::to_string(httpHandoverInfo_.flowControlTime); requestHandoverInfo += ", isRead:"; - requestHandoverInfo += readFlag == 1 ? "true" : (readFlag == 0 ? "false" : "error"); + requestHandoverInfo += + httpHandoverInfo_.readFlag == 1 ? "true" : (httpHandoverInfo_.readFlag == 0 ? "false" : "error"); + requestHandoverInfo += ", isInQueue:"; + requestHandoverInfo += + httpHandoverInfo_.inQueueFlag == 1 ? "true" : (httpHandoverInfo_.inQueueFlag == 0 ? "false" : "error"); requestHandoverInfo += ", isStream:"; requestHandoverInfo += this->IsRequestInStream() ? "true" : "false"; return requestHandoverInfo; 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 8037af626..05c105c6d 100755 --- a/frameworks/js/napi/http/http_exec/src/http_exec.cpp +++ b/frameworks/js/napi/http/http_exec/src/http_exec.cpp @@ -215,10 +215,7 @@ void HttpExec::SetRequestInfoCallbacks(HttpOverCurl::TransferCallbacks &callback NETSTACK_LOGE("setHandoverInfoCallback context is nullptr, error!"); return; } - context->SetRequestHandoverInfo(httpHandoverInfo.handoverNum, - httpHandoverInfo.handoverReason, - httpHandoverInfo.flowControlTime, - httpHandoverInfo.readFlag); + context->SetRequestHandoverInfo(httpHandoverInfo); }; callbacks.handoverInfoCallback = handoverInfoCallback; callbacks.setHandoverInfoCallback = setHandoverInfoCallback; diff --git a/frameworks/native/http/http_client/http_client.cpp b/frameworks/native/http/http_client/http_client.cpp index 58954c896..62fc184d5 100644 --- a/frameworks/native/http/http_client/http_client.cpp +++ b/frameworks/native/http/http_client/http_client.cpp @@ -113,10 +113,7 @@ void HttpSession::SetRequestInfoCallbacks( return httpHandoverStackInfo; }; static auto setHandoverInfoCallback = [ptr](HttpHandoverInfo httpHandoverInfo, void *) { - ptr->SetRequestHandoverInfo(httpHandoverInfo.handoverNum, - httpHandoverInfo.handoverReason, - httpHandoverInfo.flowControlTime, - httpHandoverInfo.readFlag); + ptr->SetRequestHandoverInfo(httpHandoverInfo); }; callbacks.handoverInfoCallback = handoverInfoCallback; callbacks.setHandoverInfoCallback = setHandoverInfoCallback; diff --git a/frameworks/native/http/http_client/http_client_task.cpp b/frameworks/native/http/http_client/http_client_task.cpp index c01e66181..bf8d7dec7 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 { \ @@ -925,26 +922,22 @@ void HttpClientTask::SetSuccess(bool isSuccess) isSuccess_ = isSuccess; } -void HttpClientTask::SetRequestHandoverInfo( - int32_t handoverNum, int32_t handoverReason, double flowControlTime, int32_t readFlag) +void HttpClientTask::SetRequestHandoverInfo(const HttpHandoverInfo &httpHandoverInfo) { - handoverNum_ = handoverNum; - handoverReason_ = handoverReason; - flowControlTime_ = flowControlTime; - readFlag_ = readFlag; + httpHandoverInfo_ = httpHandoverInfo; } std::string HttpClientTask::GetRequestHandoverInfo() { std::string requestHandoverInfo; - if (handoverNum_ <= 0) { + if (httpHandoverInfo_.handOverNum <= 0) { requestHandoverInfo = "no handover"; return requestHandoverInfo; } requestHandoverInfo += "HandoverNum:"; - requestHandoverInfo += std::to_string(handoverNum_); + requestHandoverInfo += std::to_string(httpHandoverInfo_.handOverNum); requestHandoverInfo += ", handoverReason:"; - switch (handoverReason_) { + switch (httpHandoverInfo_.handOverReason) { case HandoverRequestType::INCOMING: requestHandoverInfo += "flowControl, flowControlTime:"; break; @@ -958,9 +951,13 @@ std::string HttpClientTask::GetRequestHandoverInfo() requestHandoverInfo += "unkown type"; break; } - requestHandoverInfo += std::to_string(flowControlTime_); + requestHandoverInfo += std::to_string(httpHandoverInfo_.flowControlTime); requestHandoverInfo += ", isRead:"; - requestHandoverInfo += readFlag_ == 1 ? "true" : (readFlag_ == 0 ? "false" : "error"); + requestHandoverInfo += + httpHandoverInfo_.readFlag == 1 ? "true" : (httpHandoverInfo_.readFlag == 0 ? "false" : "error"); + requestHandoverInfo += ", isIInQueue:"; + requestHandoverInfo += + httpHandoverInfo_.inQueueFlag == 1 ? "true" : (httpHandoverInfo_.inQueueFlag == 0 ? "false" : "error"); requestHandoverInfo += ", isStream:"; requestHandoverInfo += onDataReceive_ ? "true" : "false"; return requestHandoverInfo; diff --git a/interfaces/innerkits/http_client/include/http_client_task.h b/interfaces/innerkits/http_client/include/http_client_task.h index 7310f07c1..b3c2eff9d 100644 --- a/interfaces/innerkits/http_client/include/http_client_task.h +++ b/interfaces/innerkits/http_client/include/http_client_task.h @@ -30,6 +30,9 @@ #if HAS_NETMANAGER_BASE #include "netstack_network_profiler.h" #endif +#ifdef HTTP_HANDOVER_FEATURE +#include "http_handover_info.h" +#endif namespace OHOS { namespace NetStack { @@ -381,8 +384,7 @@ private: /** * Set the request information and print it to the log. */ - void SetRequestHandoverInfo(int32_t handoverNum, int32_t handoverReason, double flowControlTime, - int32_t readFlag); + void SetRequestHandoverInfo(const HttpHandoverInfo &httpHandoverInfo); #endif /** @@ -406,11 +408,7 @@ private: 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; + HttpHandoverInfo httpHandoverInfo_; #endif TaskType type_; diff --git a/test/unittest/http_client/BUILD.gn b/test/unittest/http_client/BUILD.gn index 02847e32e..c56a7add7 100644 --- a/test/unittest/http_client/BUILD.gn +++ b/test/unittest/http_client/BUILD.gn @@ -71,6 +71,7 @@ ohos_unittest("http_client_unittest") { "HAS_NETMANAGER_BASE=1", "HTTP_HANDOVER_FEATURE", ] + include_dirs += [ "$SUBSYSTEM_DIR/netstack/utils/http_over_curl/include" ] } part_name = "netstack" diff --git a/test/unittest/http_client/HttpClientTaskTest.cpp b/test/unittest/http_client/HttpClientTaskTest.cpp index d29a0ff40..e18c3d4be 100644 --- a/test/unittest/http_client/HttpClientTaskTest.cpp +++ b/test/unittest/http_client/HttpClientTaskTest.cpp @@ -1351,25 +1351,22 @@ HWTEST_F(HttpClientTaskTest, HandoverInfoTest, TestSize.Level1) HttpSession &session = HttpSession::GetInstance(); auto task = session.CreateTask(httpReq); - int32_t handoverNum = 1; - int32_t handoverReason = 0; - double flowControlTime = 1.0; - int32_t readFlag = 0; - task->SetRequestHandoverInfo(handoverNum, handoverReason, flowControlTime, readFlag); + HttpHandoverInfo httpHandoverInfo; + task->SetRequestHandoverInfo(httpHandoverInfo); std::string handoverInfo = task->GetRequestHandoverInfo(); - handoverReason = 1; - readFlag = 1; - task->SetRequestHandoverInfo(handoverNum, handoverReason, flowControlTime, readFlag); + httpHandoverInfo.handOverReason = 1; + httpHandoverInfo.readFlag = 1; + task->SetRequestHandoverInfo(httpHandoverInfo); handoverInfo = task->GetRequestHandoverInfo(); - handoverReason = 2; - readFlag = 2; - task->SetRequestHandoverInfo(handoverNum, handoverReason, flowControlTime, readFlag); + httpHandoverInfo.handOverReason = 2; + httpHandoverInfo.readFlag = 2; + task->SetRequestHandoverInfo(httpHandoverInfo); handoverInfo = task->GetRequestHandoverInfo(); - handoverReason = 3; - task->SetRequestHandoverInfo(handoverNum, handoverReason, flowControlTime, readFlag); + httpHandoverInfo.handOverReason = 3; + task->SetRequestHandoverInfo(httpHandoverInfo); handoverInfo = task->GetRequestHandoverInfo(); EXPECT_TRUE(task->Start()); } 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 4a074002e..e1eb7ac71 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 @@ -158,6 +158,7 @@ HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestCallbackEvent, TestSize CURL *handle = GetCurlHandle(); EXPECT_EQ(SingletonHttpHandoverHandler::GetInstance()->IsRequestRead(handle), 0); + EXPECT_EQ(SingletonHttpHandoverHandler::GetInstance()->IsRequestInQueue(handle), 1); } HWTEST_F(HttpHandoverHandlerTest, HttpHandoverHandlerTestHandoverQuery, TestSize.Level2) diff --git a/utils/http_over_curl/include/http_handover_handler.h b/utils/http_over_curl/include/http_handover_handler.h index 7e9866e53..08bb35f57 100644 --- a/utils/http_over_curl/include/http_handover_handler.h +++ b/utils/http_over_curl/include/http_handover_handler.h @@ -33,7 +33,7 @@ namespace OHOS::NetStack::HttpOverCurl { struct RequestInfo; -typedef void *(*HTTP_HAND_OVER_INIT)(void *user, void (*HMS_NetworkBoost_HandoverEventCallback)(void *), +typedef void *(*HTTP_HAND_OVER_INIT)(const void *user, void (*HMS_NetworkBoost_HandoverEventCallback)(void *), 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); diff --git a/utils/http_over_curl/include/http_handover_info.h b/utils/http_over_curl/include/http_handover_info.h index 09f1b6615..5eb96770d 100644 --- a/utils/http_over_curl/include/http_handover_info.h +++ b/utils/http_over_curl/include/http_handover_info.h @@ -26,10 +26,14 @@ struct HttpHandoverStackInfo { }; struct HttpHandoverInfo { - int32_t handoverNum = 0; - int32_t handoverReason = 0; + uint32_t handOverId = 0; + uint32_t handOverNum = 0; // 0 means no handover or query failed. + int32_t handOverReason = 0; + uint64_t startTime = 0; double flowControlTime = 0.0; int32_t readFlag = 0; + int32_t inQueueFlag = 0; + bool isHistory = false; }; enum HandoverRequestType { diff --git a/utils/http_over_curl/src/http_handover_handler.cpp b/utils/http_over_curl/src/http_handover_handler.cpp index 9cdd69121..9b4079269 100644 --- a/utils/http_over_curl/src/http_handover_handler.cpp +++ b/utils/http_over_curl/src/http_handover_handler.cpp @@ -34,7 +34,7 @@ HttpHandoverHandler::HttpHandoverHandler() initsuccess_ = Initialize(); } -void HandoverCallback(void *user) +void HandoverCallback(const void *user) { if (user == nullptr) { NETSTACK_LOGE("handover callback user is nullptr"); -- Gitee From 5c8978824b310053d4bbd21f5ac5dbec22d61fa8 Mon Sep 17 00:00:00 2001 From: YangWeimin Date: Wed, 10 Sep 2025 07:48:24 +0000 Subject: [PATCH 2/3] update utils/http_over_curl/include/http_handover_handler.h. Signed-off-by: YangWeimin --- .../include/http_handover_handler.h | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/utils/http_over_curl/include/http_handover_handler.h b/utils/http_over_curl/include/http_handover_handler.h index 08bb35f57..f60779b9d 100644 --- a/utils/http_over_curl/include/http_handover_handler.h +++ b/utils/http_over_curl/include/http_handover_handler.h @@ -33,18 +33,18 @@ namespace OHOS::NetStack::HttpOverCurl { struct RequestInfo; -typedef void *(*HTTP_HAND_OVER_INIT)(const void *user, void (*HMS_NetworkBoost_HandoverEventCallback)(void *), - 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_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); -typedef void (*HTTP_HAND_OVER_REPORT_TIMEOUT)(void *handle); +typedef void *(*HTTP_HAND_OVER_INIT)(const void *user, + void (*HMS_NetworkBoost_HandoverEventCallback)(const void *), + void (*HMS_NetworkBoost_HandoverTimerCallback)(const void *, long), const char *stackName); +typedef int32_t (*HTTP_HAND_OVER_UNINIT)(const void *handle); +typedef void (*HTTP_HAND_OVER_QUERY)(const void *handle, int32_t *status, int32_t *netId); +typedef void (*HTTP_HAND_OVER_ADD)(const void *handle, const void *userp, HttpHandoverInfo httpHandoverInfo); +typedef void (*HTTP_HAND_OVER_DEL)(const void *handle, const void *userp, bool isSuccess); +typedef HttpHandoverInfo (*HTTP_HAND_OVER_QUERY_REQUEST)(const void *handle, const void *userp); +typedef void (*HTTP_HAND_OVER_REPORT_TIMEOUT)(const void *handle); -void HandoverCallback(void *user); -void HandoverTimerCallback(void *user, long timeoutMs); +void HandoverCallback(const void *user); +void HandoverTimerCallback(const void *user, long timeoutMs); bool CheckSocketTime(void *user, curl_socket_t fd); curl_socket_t OpenSocket(void *user, curlsocktype purpose, struct curl_sockaddr *addr); int CloseSocketCallback(void *user, curl_socket_t fd); @@ -76,18 +76,19 @@ public: bool RetransRequest(std::map &ongoingRequests, CURLM *multi, RequestInfo *request); bool CheckRequestCanRetrans(RequestInfo *request, int32_t requestType, CURLcode result); void UndoneRequestHandle(std::map &ongoingRequests, CURLM *multi); + int32_t IsRequestInQueue(CURL *easyHandle); 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 AddRequest(RequestInfo *requestInfo, int32_t handoverReason); 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); + private: void *netHandoverHandler_ = nullptr; void *httpHandoverManager_ = nullptr; -- Gitee From a7124a8848a24c87d92f107eeec37460e4281ab0 Mon Sep 17 00:00:00 2001 From: YangWeimin Date: Wed, 10 Sep 2025 08:01:39 +0000 Subject: [PATCH 3/3] update utils/http_over_curl/src/http_handover_handler.cpp. Signed-off-by: YangWeimin --- .../async_context/src/request_context.cpp | 8 +- .../http_client/HttpClientTaskTest.cpp | 5 + .../src/http_handover_handler.cpp | 100 ++++++++++-------- 3 files changed, 62 insertions(+), 51 deletions(-) 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 09d386c14..9e978c425 100755 --- a/frameworks/js/napi/http/async_context/src/request_context.cpp +++ b/frameworks/js/napi/http/async_context/src/request_context.cpp @@ -1051,15 +1051,15 @@ void RequestContext::SetRequestHandoverInfo(const HttpHandoverInfo &httpHandover std::string RequestContext::GetRequestHandoverInfo() { std::string requestHandoverInfo; - if (httpHandoverInfo_.handoverNum <= 0) { + if (httpHandoverInfo_.handOverNum <= 0) { requestHandoverInfo = "no handover"; return requestHandoverInfo; } int32_t readFlag = httpHandoverInfo_.readFlag; requestHandoverInfo += "HandoverNum:"; - requestHandoverInfo += std::to_string(httpHandoverInfo_.handoverNum); - requestHandoverInfo += ", handoverReason:"; - switch (httpHandoverInfo_.handoverReason) { + requestHandoverInfo += std::to_string(httpHandoverInfo_.handOverNum); + requestHandoverInfo += ", handverReason:"; + switch (httpHandoverInfo_.handOverReason) { case HandoverRequestType::INCOMING: requestHandoverInfo += "flowControl, flowControlTime:"; break; diff --git a/test/unittest/http_client/HttpClientTaskTest.cpp b/test/unittest/http_client/HttpClientTaskTest.cpp index e18c3d4be..739aec63a 100644 --- a/test/unittest/http_client/HttpClientTaskTest.cpp +++ b/test/unittest/http_client/HttpClientTaskTest.cpp @@ -33,6 +33,9 @@ #include "net_conn_client.h" #include "network_security_config.h" #endif +#ifdef HTTP_HANDOVER_FEATURE +#include "http_handover_info.h" +#endif using namespace OHOS::NetStack::HttpClient; using namespace testing; @@ -1342,6 +1345,7 @@ HWTEST_F(HttpClientTaskTest, ProcessErrorTest001, TestSize.Level1) EXPECT_FALSE(error.GetErrorMessage().empty()); } +#ifdef HTTP_HANDOVER_FEATURE HWTEST_F(HttpClientTaskTest, HandoverInfoTest, TestSize.Level1) { HttpClientRequest httpReq; @@ -1370,4 +1374,5 @@ HWTEST_F(HttpClientTaskTest, HandoverInfoTest, TestSize.Level1) handoverInfo = task->GetRequestHandoverInfo(); EXPECT_TRUE(task->Start()); } +#endif } // namespace diff --git a/utils/http_over_curl/src/http_handover_handler.cpp b/utils/http_over_curl/src/http_handover_handler.cpp index 9b4079269..918f2c973 100644 --- a/utils/http_over_curl/src/http_handover_handler.cpp +++ b/utils/http_over_curl/src/http_handover_handler.cpp @@ -41,11 +41,11 @@ void HandoverCallback(const void *user) return; } - HttpHandoverHandler* const handoverhandler = reinterpret_cast(user); + HttpHandoverHandler* const handoverhandler = reinterpret_cast(const_cast(user)); handoverhandler->SetHandoverEvent(); } -void HandoverTimerCallback(void *user, long timeoutMs) +void HandoverTimerCallback(const void *user, long timeoutMs) { NETSTACK_LOGD("HandoverTimerCallback enter, set timeout %{public}ld ms.", timeoutMs); if (user == nullptr) { @@ -53,14 +53,14 @@ void HandoverTimerCallback(void *user, long timeoutMs) return; } - HttpHandoverHandler* const handoverHandler = reinterpret_cast(user); + HttpHandoverHandler* const handoverHandler = reinterpret_cast(const_cast(user)); handoverHandler->SetHandoverTimeoutEvent(timeoutMs); } bool CheckSocketTime(void *user, curl_socket_t fd) { auto handover = static_cast(user); - if (handover && handover->CheckSocketOpentimeLessThanEndTime(fd)) { + if (handover && handover->CheckSocketOpentimeLessThanEndTime(fd)) { // LCOV_EXCL_LINE return false; } return true; @@ -69,7 +69,7 @@ bool CheckSocketTime(void *user, curl_socket_t fd) curl_socket_t OpenSocket(void *user, curlsocktype purpose, struct curl_sockaddr *addr) { curl_socket_t sockfd = socket(addr->family, addr->socktype, addr->protocol); - if (sockfd < 0) { + if (sockfd < 0) { // LCOV_EXCL_LINE NETSTACK_LOGE("Failed to open socket: %{public}d, errno: %{public}d", sockfd, errno); return -1; } @@ -87,7 +87,7 @@ int CloseSocketCallback(void *user, curl_socket_t fd) handover->EraseFd(fd); } int ret = close(fd); - if (ret < 0) { + if (ret < 0) { // LCOV_EXCL_LINE NETSTACK_LOGE("Failed to close socket: %{public}d, errno: %{public}d", fd, errno); return ret; } @@ -128,7 +128,7 @@ bool HttpHandoverHandler::Initialize() { const std::string HTTP_HANDOVER_WRAPPER_PATH = "/system/lib64/libhttp_handover.z.so"; netHandoverHandler_ = dlopen(HTTP_HANDOVER_WRAPPER_PATH.c_str(), RTLD_NOW); - if (netHandoverHandler_ == nullptr) { + if (netHandoverHandler_ == nullptr) { // LCOV_EXCL_LINE NETSTACK_LOGE("libhttp_handover.z.so was not loaded, error: %{public}s", dlerror()); return false; } @@ -145,9 +145,11 @@ bool HttpHandoverHandler::Initialize() (HTTP_HAND_OVER_QUERY_REQUEST)dlsym(netHandoverHandler_, "HMS_NetworkBoost_HttpHandoverManagerQueryRequest"); httpHandoverReportTimeout_ = (HTTP_HAND_OVER_REPORT_TIMEOUT)dlsym(netHandoverHandler_, "HMS_NetworkBoost_HttpHandoverManagerReportTimeout"); + // LCOV_EXCL_START bool hasFuncNull = (httpHandoverInit_ == nullptr || httpHandoverUninit_ == nullptr || httpHandoverQuery_ == nullptr || httpHandoverAddRequest_ == nullptr || httpHandoverDelRequest_ == nullptr || httpHandoverQueryRequest_ == nullptr || httpHandoverReportTimeout_ == nullptr); + // LCOV_EXCL_STOP if (hasFuncNull) { NETSTACK_LOGE("http handover wrapper symbol failed, error: %{public}s", dlerror()); return false; @@ -164,10 +166,10 @@ bool HttpHandoverHandler::Initialize() HttpHandoverHandler::~HttpHandoverHandler() { NETSTACK_LOGD("start httpHandoverUninit_"); - if (httpHandoverManager_ != nullptr) { + if (httpHandoverManager_ != nullptr) { // LCOV_EXCL_LINE httpHandoverUninit_(httpHandoverManager_); } - if (netHandoverHandler_ != nullptr) { + if (netHandoverHandler_ != nullptr) { // LCOV_EXCL_LINE dlclose(netHandoverHandler_); } httpHandoverManager_ = nullptr; @@ -215,11 +217,11 @@ void HttpHandoverHandler::SetHandoverTimeoutEvent(long timeoutMs) void HttpHandoverHandler::HandoverQuery() { - if (httpHandoverQuery_ == nullptr || httpHandoverManager_ == nullptr) { + if (httpHandoverQuery_ == nullptr || httpHandoverManager_ == nullptr) { // LCOV_EXCL_LINE NETSTACK_LOGE("nullptr param error"); return; } - httpHandoverQuery_(httpHandoverManager_, &status_, &netId_); + httpHandoverQuery_(httpHandoverManager_, &status_, &netId_); // LCOV_EXCL_LINE } bool HttpHandoverHandler::CheckSocketOpentimeLessThanEndTime(curl_socket_t fd) @@ -228,7 +230,7 @@ bool HttpHandoverHandler::CheckSocketOpentimeLessThanEndTime(curl_socket_t fd) return false; } bool ret = socketopentime_[fd] < endTime_; - if (ret) { + if (ret) { // LCOV_EXCL_LINE NETSTACK_LOGD("Old fd:%{public}d fdtime:%{public}d endTime:%{public}d", (int)fd, socketopentime_[fd], endTime_); } return ret; @@ -300,22 +302,25 @@ bool HttpHandoverHandler::RetransRequest(std::map &ongoin bool HttpHandoverHandler::CheckRequestCanRetrans(RequestInfo *request, int32_t requestType, CURLcode result) { - if (request == nullptr) { + if (request == nullptr) { // LCOV_EXCL_LINE return false; } time_t recvtime = 0; time_t sendtime = 0; int32_t readFlag = IsRequestRead(request->easyHandle, recvtime, sendtime); - if (readFlag == -1) { + if (readFlag == -1) { // LCOV_EXCL_LINE return false; } + HttpHandoverStackInfo httpHandoverStackInfo = request->callbacks.handoverInfoCallback(request->opaqueData); + // LCOV_EXCL_START bool isSafe = (httpHandoverStackInfo.method == METHOD_GET || httpHandoverStackInfo.method == METHOD_HEAD || httpHandoverStackInfo.method == METHOD_OPTIONS || httpHandoverStackInfo.method == METHOD_TRACE); bool ret = false; if (IsConnectError(result) || sendtime == 0 || (isSafe && (!httpHandoverStackInfo.isInStream || readFlag == 0))) { ret = true; } + // LCOV_EXCL_STOP if (requestType == HandoverRequestType::INCOMING || requestType == HandoverRequestType::NETWORKERROR) { return ret; } @@ -348,6 +353,7 @@ void HttpHandoverHandler::UndoneRequestHandle(std::map &o ++it; continue; } + // LCOV_EXCL_START if (requestInfo != nullptr && requestInfo->callbacks.doneCallback) { CURLMsg message; message.msg = CURLMSG_DONE; @@ -355,6 +361,7 @@ void HttpHandoverHandler::UndoneRequestHandle(std::map &o requestInfo->callbacks.doneCallback(&message, requestInfo->opaqueData); } it = ongoingRequests.erase(it); + // LCOV_EXCL_STOP } else { ++it; } @@ -365,7 +372,7 @@ void HttpHandoverHandler::HandoverRequestCallback(std::mapReset(); HandoverQuery(); - NETSTACK_LOGD("Enter HandoverRequestCallback status %{public}d", GetStatus()); + NETSTACK_LOGD("Enter HandoverRequestCallback status %{public}d", GetStatus()); // LCOV_EXCL_START if (GetStatus() == HttpHandoverHandler::START) { NETSTACK_LOGD("start ongoingRequests:%{public}d", (int)ongoingRequests.size()); for (auto &request : ongoingRequests) { @@ -389,7 +396,7 @@ void HttpHandoverHandler::HandoverRequestCallback(std::map &ong void HttpHandoverHandler::SetHandoverInfo(RequestInfo *requestInfo) { - if (requestInfo != nullptr) { - 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); + if (requestInfo == nullptr || requestInfo->opaqueData == nullptr) { + NETSTACK_LOGE("handover requestInfo nullptr error"); + return; } + HttpHandoverInfo httpHandoverInfo = httpHandoverQueryRequest_(httpHandoverManager_, requestInfo->opaqueData); + requestInfo->callbacks.setHandoverInfoCallback(httpHandoverInfo, requestInfo->opaqueData); + DelRequest(requestInfo); } bool HttpHandoverHandler::ProcessRequestNetError(std::map &ongoingRequests, CURLM *multi, @@ -463,7 +474,7 @@ bool HttpHandoverHandler::ProcessRequestNetError(std::map { if (!requestInfo || requestEndtime_.count(requestInfo) == 0) { return false; - } + } // LCOV_EXCL_START int endTime = requestEndtime_[requestInfo]; requestEndtime_.erase(requestInfo); if (!msg || !IsNetworkErrorTypeCorrect(msg->data.result)) { @@ -480,18 +491,21 @@ bool HttpHandoverHandler::ProcessRequestNetError(std::map NETSTACK_LOGD("networkerror after end status"); AddRequest(requestInfo, HandoverRequestType::NETWORKERROR); return RetransRequest(ongoingRequests, multi, requestInfo); - } + } // LCOV_EXCL_STOP return false; } void HttpHandoverHandler::AddRequest(RequestInfo *requestInfo, int32_t type) { - if (httpHandoverManager_ == nullptr) { + if (httpHandoverManager_ == nullptr) { // LCOV_EXCL_LINE NETSTACK_LOGE("httpHandoverManager_ nullptr error"); return; } - httpHandoverAddRequest_(httpHandoverManager_, requestInfo->opaqueData, type, - IsRequestRead(requestInfo->easyHandle)); + HttpHandoverInfo httpHandoverInfo; + httpHandoverInfo.handOverReason = type; + httpHandoverInfo.readFlag = IsRequestRead(requestInfo->easyHandle); + httpHandoverInfo.inQueueFlag = IsRequestInQueue(requestInfo->easyHandle); + httpHandoverAddRequest_(httpHandoverManager_, requestInfo->opaqueData, httpHandoverInfo); // LCOV_EXCL_LINE } void HttpHandoverHandler::DelRequest(RequestInfo *requestInfo) @@ -499,19 +513,11 @@ void HttpHandoverHandler::DelRequest(RequestInfo *requestInfo) if (httpHandoverManager_ == nullptr) { NETSTACK_LOGE("httpHandoverManager_ nullptr error"); return; + // LCOV_EXCL_START } HttpHandoverStackInfo httpHandoverStackInfo = requestInfo->callbacks.handoverInfoCallback(requestInfo->opaqueData); httpHandoverDelRequest_(httpHandoverManager_, requestInfo->opaqueData, httpHandoverStackInfo.isSuccess); -} - -int32_t HttpHandoverHandler::QueryRequest(void *userp, int32_t &handOverReason, double &flowControlTime, - int32_t &readFlag) -{ - if (httpHandoverManager_ == nullptr) { - NETSTACK_LOGE("httpHandoverManager_ nullptr error"); - return -1; - } - return httpHandoverQueryRequest_(httpHandoverManager_, userp, &handOverReason, &flowControlTime, &readFlag); + // LCOV_EXCL_STOP } int32_t HttpHandoverHandler::GetStatus() -- Gitee