Merge pull request !1494 from maosiping/master
This commit is contained in:
openharmony_ci 2024-08-23 08:21:50 +00:00 committed by Gitee
commit bc4b3079fb
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 33 additions and 4 deletions

View File

@ -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;

View File

@ -1425,19 +1425,22 @@ struct curl_slist *HttpExec::MakeHeaders(const std::vector<std::string> &vec)
napi_value HttpExec::MakeResponseHeader(napi_env env, void *ctx)
{
auto context = reinterpret_cast<RequestContext *>(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;

View File

@ -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);

View File

@ -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<EventListener> listeners_;
void *data_;
std::queue<void *> dataQueue_;
static EventManagerMagic magic_;
static std::mutex mutexForManager_;
static std::unordered_set<EventManager *> validManager_;
napi_ref eventRef_;

View File

@ -162,9 +162,13 @@ void EventManager::DeleteListener(const std::string &type)
std::unordered_set<EventManager *> 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);
}