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 eed9267f..8eb963b9 100644 --- a/frameworks/js/napi/http/async_context/include/request_context.h +++ b/frameworks/js/napi/http/async_context/include/request_context.h @@ -30,6 +30,7 @@ #include "request_tracer.h" namespace OHOS::NetStack::Http { +static constexpr const uint32_t MAGIC_NUMBER = 0x86161616; struct LoadBytes { LoadBytes() : nLen(0), tLen(0){}; LoadBytes(curl_off_t nowLen, curl_off_t totalLen) @@ -51,6 +52,8 @@ struct CertsPath { class RequestContext final : public BaseContext { public: + friend class HttpExec; + RequestContext() = delete; RequestContext(napi_env env, EventManager *manager); @@ -137,6 +140,7 @@ public: RequestTracer::Trace &GetTrace(); private: + uint32_t magicNumber_ = MAGIC_NUMBER; int32_t taskId_ = -1; bool usingCache_ = true; bool requestInStream_ = false; 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 211bafd8..be8354ff 100755 --- a/frameworks/js/napi/http/http_exec/src/http_exec.cpp +++ b/frameworks/js/napi/http/http_exec/src/http_exec.cpp @@ -1425,19 +1425,22 @@ struct curl_slist *HttpExec::MakeHeaders(const std::vector &vec) napi_value HttpExec::MakeResponseHeader(napi_env env, void *ctx) { auto context = reinterpret_cast(ctx); + if (context->magicNumber_ != MAGIC_NUMBER) { + return NapiUtils::CreateObject(env); + } (void)env; napi_value header = NapiUtils::CreateObject(context->GetEnv()); if (NapiUtils::GetValueType(context->GetEnv(), header) == napi_object) { - for (const auto &it : context->response.GetHeader()) { + for (const auto it : context->response.header_) { if (!it.first.empty() && !it.second.empty()) { NapiUtils::SetStringPropertyUtf8(context->GetEnv(), header, it.first, it.second); } } - if (!context->response.GetsetCookie().empty()) { + if (!context->response.setCookie_.empty()) { uint32_t index = 0; - auto len = context->response.GetsetCookie().size(); + auto len = context->response.setCookie_.size(); auto array = NapiUtils::CreateArray(context->GetEnv(), len); - for (const auto &setCookie : context->response.GetsetCookie()) { + for (const auto setCookie : context->response.setCookie_) { auto str = NapiUtils::CreateStringUtf8(context->GetEnv(), setCookie); NapiUtils::SetArrayElement(context->GetEnv(), array, index, str); ++index; diff --git a/frameworks/js/napi/http/options/include/http_response.h b/frameworks/js/napi/http/options/include/http_response.h index 93a30b14..fc6a0a3f 100644 --- a/frameworks/js/napi/http/options/include/http_response.h +++ b/frameworks/js/napi/http/options/include/http_response.h @@ -25,6 +25,8 @@ static constexpr const char *WARNING = "Warning"; class HttpResponse final { public: + friend class HttpExec; + HttpResponse(); void AppendResult(const void *data, size_t length); diff --git a/utils/napi_utils/include/event_manager.h b/utils/napi_utils/include/event_manager.h index 6060c858..d34785f6 100644 --- a/utils/napi_utils/include/event_manager.h +++ b/utils/napi_utils/include/event_manager.h @@ -31,6 +31,15 @@ #include "uv.h" namespace OHOS::NetStack { +static constexpr const uint32_t EVENT_MANAGER_MAGIC_NUMBER = 0x86161616; +struct EventManagerMagic { + uint32_t magicNumber_ = EVENT_MANAGER_MAGIC_NUMBER; + ~EventManagerMagic() + { + magicNumber_ = ~magicNumber_; + } +}; + class EventManager { public: EventManager(); @@ -102,6 +111,7 @@ private: std::list listeners_; void *data_; std::queue dataQueue_; + static EventManagerMagic magic_; static std::mutex mutexForManager_; static std::unordered_set validManager_; napi_ref eventRef_; diff --git a/utils/napi_utils/src/event_manager.cpp b/utils/napi_utils/src/event_manager.cpp index ea34e370..a13f5f44 100644 --- a/utils/napi_utils/src/event_manager.cpp +++ b/utils/napi_utils/src/event_manager.cpp @@ -162,9 +162,13 @@ void EventManager::DeleteListener(const std::string &type) std::unordered_set EventManager::validManager_; std::mutex EventManager::mutexForManager_; +EventManagerMagic EventManager::magic_; void EventManager::SetInvalid(EventManager *manager) { + if (magic_.magicNumber_ != EVENT_MANAGER_MAGIC_NUMBER) { + return; + } std::lock_guard lock(mutexForManager_); auto pos = validManager_.find(manager); if (pos == validManager_.end()) { @@ -178,12 +182,18 @@ void EventManager::SetInvalid(EventManager *manager) bool EventManager::IsManagerValid(EventManager *manager) { + if (magic_.magicNumber_ != EVENT_MANAGER_MAGIC_NUMBER) { + return false; + } std::lock_guard lock(mutexForManager_); return validManager_.find(manager) != validManager_.end(); } void EventManager::SetValid(EventManager *manager) { + if (magic_.magicNumber_ != EVENT_MANAGER_MAGIC_NUMBER) { + return; + } std::lock_guard lock(mutexForManager_); validManager_.emplace(manager); }