From 04bed33531d8b44711778c6ac5a24357cea325e8 Mon Sep 17 00:00:00 2001 From: maosiping Date: Thu, 19 May 2022 17:37:13 +0800 Subject: [PATCH 01/20] =?UTF-8?q?http=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: maosiping --- CMakeLists.txt | 2 + .../include/none_param_context.h | 39 ++++ .../async_context/src/none_param_context.cpp | 48 +++++ .../cache/base64/include/base64_utils.h | 26 +++ .../options/cache/base64/src/base64_utils.cpp | 52 +++++ .../cache/cache_proxy/include/cache_proxy.h | 26 +++ .../cache/lru_cache/include/disk_handler.h | 40 ++++ .../cache/lru_cache/include/lru_cache.h | 67 ++++++ .../include/lru_cache_disk_handler.h | 42 ++++ .../cache/lru_cache/src/disk_handler.cpp | 84 ++++++++ .../options/cache/lru_cache/src/lru_cache.cpp | 200 ++++++++++++++++++ .../lru_cache/src/lru_cache_disk_handler.cpp | 85 ++++++++ .../options/cache/md5/include/calculate_md5.h | 24 +++ .../options/cache/md5/src/calculate_md5.cpp | 37 ++++ test/napi/http/CMakeLists.txt | 11 + utils/CMakeLists.txt | 2 +- 16 files changed, 784 insertions(+), 1 deletion(-) create mode 100644 frameworks/js/napi/http/async_context/include/none_param_context.h create mode 100644 frameworks/js/napi/http/async_context/src/none_param_context.cpp create mode 100644 frameworks/js/napi/http/options/cache/base64/include/base64_utils.h create mode 100644 frameworks/js/napi/http/options/cache/base64/src/base64_utils.cpp create mode 100644 frameworks/js/napi/http/options/cache/cache_proxy/include/cache_proxy.h create mode 100644 frameworks/js/napi/http/options/cache/lru_cache/include/disk_handler.h create mode 100644 frameworks/js/napi/http/options/cache/lru_cache/include/lru_cache.h create mode 100644 frameworks/js/napi/http/options/cache/lru_cache/include/lru_cache_disk_handler.h create mode 100644 frameworks/js/napi/http/options/cache/lru_cache/src/disk_handler.cpp create mode 100644 frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache.cpp create mode 100644 frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache_disk_handler.cpp create mode 100644 frameworks/js/napi/http/options/cache/md5/include/calculate_md5.h create mode 100644 frameworks/js/napi/http/options/cache/md5/src/calculate_md5.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 89f3e090b..32578ef07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,9 +44,11 @@ include_directories(../../../foundation/arkui/napi/interfaces/inner_api) include_directories(../../../for_linux) include_directories(../../../third_party/quickjs) +include_directories(../../../third_party/openssl/include) include_directories(../../../third_party/googletest/googletest/include) include_directories(../../../third_party/libuv/include) +include_directories(../../../third_party/jsoncpp/include) link_directories(../../../utils/native/base/cmake-build-debug/src/securec) diff --git a/frameworks/js/napi/http/async_context/include/none_param_context.h b/frameworks/js/napi/http/async_context/include/none_param_context.h new file mode 100644 index 000000000..0a52fd36d --- /dev/null +++ b/frameworks/js/napi/http/async_context/include/none_param_context.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2022 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_NONE_PARAM_CONTEXT_H +#define COMMUNICATIONNETSTACK_NONE_PARAM_CONTEXT_H + +#include "netstack_base_context.h" +#include "nocopyable.h" + +namespace OHOS::NetStack { +class NoneParamContext final : public BaseContext { + DISALLOW_COPY_AND_MOVE(NoneParamContext); + + NoneParamContext() = delete; + + explicit NoneParamContext(napi_env env, EventManager *manager); + + void ParseParams(napi_value *params, size_t paramsCount); + +private: + bool CheckParamsType(napi_value *params, size_t paramsCount); +}; + +using HttpResponseCacheFlushContext = NoneParamContext; +using HttpResponseCacheCloseContext = NoneParamContext; +using HttpResponseCacheDeleteContext = NoneParamContext; +}; // namespace OHOS::NetStack +#endif // COMMUNICATIONNETSTACK_NONE_PARAM_CONTEXT_H diff --git a/frameworks/js/napi/http/async_context/src/none_param_context.cpp b/frameworks/js/napi/http/async_context/src/none_param_context.cpp new file mode 100644 index 000000000..1ddd52628 --- /dev/null +++ b/frameworks/js/napi/http/async_context/src/none_param_context.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2022 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. + */ +#include "none_param_context.h" +#include "netstack_napi_utils.h" + +namespace OHOS::NetStack { +static constexpr const int PARAM_NONE = 0; +static constexpr const int PARAM_JUST_CALLBACK = 1; + +NoneParamContext::NoneParamContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {} + +void NoneParamContext::ParseParams(napi_value *params, size_t paramsCount) +{ + if (!CheckParamsType(params, paramsCount)) { + return; + } + + if (paramsCount == PARAM_JUST_CALLBACK) { + SetParseOK(SetCallback(params[0]) == napi_ok); + return; + } + SetParseOK(true); +} + +bool NoneParamContext::CheckParamsType(napi_value *params, size_t paramsCount) +{ + if (paramsCount == PARAM_NONE) { + return true; + } + + if (paramsCount == PARAM_JUST_CALLBACK) { + return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function; + } + return false; +} +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/http/options/cache/base64/include/base64_utils.h b/frameworks/js/napi/http/options/cache/base64/include/base64_utils.h new file mode 100644 index 000000000..b8e3a27f6 --- /dev/null +++ b/frameworks/js/napi/http/options/cache/base64/include/base64_utils.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022 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_BASE64_H +#define COMMUNICATIONNETSTACK_BASE64_H + +#include + +namespace OHOS::NetStack::Base64 { +std::string Encode(const std::string &source); +std::string Decode(const std::string &encoded); +} // namespace OHOS::NetStack::Base64 + +#endif /* COMMUNICATIONNETSTACK_BASE64_H */ diff --git a/frameworks/js/napi/http/options/cache/base64/src/base64_utils.cpp b/frameworks/js/napi/http/options/cache/base64/src/base64_utils.cpp new file mode 100644 index 000000000..a3940462b --- /dev/null +++ b/frameworks/js/napi/http/options/cache/base64/src/base64_utils.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2022 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. + */ + +#include + +#include "base64_utils.h" +#include "glib.h" +#include "netstack_log.h" + +static constexpr const size_t MAX_SOURCE_SIZE = 1024 * 1024; +static constexpr const size_t MAX_ENCODED_SIZE = 1024 * 1024 * 2; +static constexpr const size_t DOUBLE_LENGTH = 2; + +namespace OHOS::NetStack::Base64 { +std::string Encode(const std::string &source) +{ + gchar *encodeData = g_base64_encode(reinterpret_cast(source.c_str()), source.size()); + if (encodeData == nullptr) { + NETSTACK_LOGE("base64 encode failed"); + return {}; + } + gsize out_len = strlen(encodeData); + std::string dest(encodeData, out_len); + g_free(encodeData); + return dest; +} + +std::string Decode(const std::string &encoded) +{ + gsize out_len = 0; + guchar *decodeData = g_base64_decode(encoded.c_str(), &out_len); + if (decodeData == nullptr) { + NETSTACK_LOGE("base64 decode failed"); + return {}; + } + std::string dest(reinterpret_cast(decodeData), out_len); + g_free(decodeData); + return dest; +} +} // namespace OHOS::NetStack::Base64 \ No newline at end of file diff --git a/frameworks/js/napi/http/options/cache/cache_proxy/include/cache_proxy.h b/frameworks/js/napi/http/options/cache/cache_proxy/include/cache_proxy.h new file mode 100644 index 000000000..de3defcca --- /dev/null +++ b/frameworks/js/napi/http/options/cache/cache_proxy/include/cache_proxy.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2022 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_CACHE_PROXY_H +#define COMMUNICATIONNETSTACK_CACHE_PROXY_H + +namespace OHOS::NetStack { +class CacheProxy final { + +}; + +} // namespace OHOS::NetStack + +#endif // COMMUNICATIONNETSTACK_CACHE_PROXY_H diff --git a/frameworks/js/napi/http/options/cache/lru_cache/include/disk_handler.h b/frameworks/js/napi/http/options/cache/lru_cache/include/disk_handler.h new file mode 100644 index 000000000..ea11eacca --- /dev/null +++ b/frameworks/js/napi/http/options/cache/lru_cache/include/disk_handler.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 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_DISK_HANDLER_H +#define COMMUNICATIONNETSTACK_DISK_HANDLER_H + +#include + +#include "nocopyable.h" + +namespace OHOS::NetStack { +class DiskHandler final { +private: + DISALLOW_COPY_AND_MOVE(DiskHandler); + + std::string fileName_; + +public: + DiskHandler() = delete; + + DiskHandler(std::string fileName); + + void Write(const std::string &str); + + [[nodiscard]] std::string Read(); +}; +} // namespace OHOS::NetStack +#endif /* COMMUNICATIONNETSTACK_DISK_HANDLER_H */ diff --git a/frameworks/js/napi/http/options/cache/lru_cache/include/lru_cache.h b/frameworks/js/napi/http/options/cache/lru_cache/include/lru_cache.h new file mode 100644 index 000000000..ed76343f4 --- /dev/null +++ b/frameworks/js/napi/http/options/cache/lru_cache/include/lru_cache.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2022 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_LRU_CACHE_H +#define COMMUNICATIONNETSTACK_LRU_CACHE_H + +#include +#include +#include + +#include "nocopyable.h" +#include "json/json.h" + +namespace OHOS::NetStack { +class LRUCache { + DISALLOW_COPY_AND_MOVE(LRUCache); + +private: + struct Node { + std::string key; + std::unordered_map value; + + Node() = delete; + + Node(std::string key, std::unordered_map value); + }; + + void AddNode(const Node &node); + + void MoveNodeToHead(const std::list::iterator &it); + + void EraseTailNode(); + + std::unordered_map::iterator> cache_; + std::list nodeList_; + size_t capacity_; + size_t size_; + +public: + LRUCache(); + + explicit LRUCache(size_t capacity); + + std::unordered_map Get(const std::string &key); + + void Put(const std::string &key, const std::unordered_map &value); + + void MergeOtherCache(const LRUCache &other); + + Json::Value WriteCacheToJsonValue(); + + void ReadCacheFromJsonValue(const Json::Value &root); +}; +} // namespace OHOS::NetStack +#endif /* COMMUNICATIONNETSTACK_LRU_CACHE_H */ diff --git a/frameworks/js/napi/http/options/cache/lru_cache/include/lru_cache_disk_handler.h b/frameworks/js/napi/http/options/cache/lru_cache/include/lru_cache_disk_handler.h new file mode 100644 index 000000000..3636a1e09 --- /dev/null +++ b/frameworks/js/napi/http/options/cache/lru_cache/include/lru_cache_disk_handler.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 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_LRU_CACHE_DISK_HANDLER_H +#define COMMUNICATIONNETSTACK_LRU_CACHE_DISK_HANDLER_H + +#include "disk_handler.h" +#include "lru_cache.h" +#include "nocopyable.h" + +namespace OHOS::NetStack { +class LRUCacheDiskHandler { +private: + LRUCache &cache_; + DiskHandler diskHandler_; + size_t capacity_; + + Json::Value GetJsonValueFromFile(); + +public: + LRUCacheDiskHandler() = delete; + + LRUCacheDiskHandler(std::string fileName, LRUCache &cache, size_t capacity); + + void WriteCacheToJsonFile(); + + void ReadCacheFromJsonFile(); +}; +} // namespace OHOS::NetStack +#endif /* COMMUNICATIONNETSTACK_LRU_CACHE_DISK_HANDLER_H */ diff --git a/frameworks/js/napi/http/options/cache/lru_cache/src/disk_handler.cpp b/frameworks/js/napi/http/options/cache/lru_cache/src/disk_handler.cpp new file mode 100644 index 000000000..8332aa0a6 --- /dev/null +++ b/frameworks/js/napi/http/options/cache/lru_cache/src/disk_handler.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2022 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. + */ + +#include "disk_handler.h" +#include "netstack_log.h" + +#include +#include +#include +#include +#include + +namespace OHOS::NetStack { +DiskHandler::DiskHandler(std::string fileName) : fileName_(std::move(fileName)) {} + +void DiskHandler::Write(const std::string &str) +{ + int fd = open(fileName_.c_str(), O_CREAT | O_WRONLY); + if (fd < 0) { + NETSTACK_LOGE("errmsg: [%{public}d] %{public}s|\n", errno, strerror(errno)); + return; + } + + int ret = flock(fd, LOCK_NB | LOCK_EX); + if (ret < 0) { + NETSTACK_LOGE("errmsg: [%{public}d] %{public}s|\n", errno, strerror(errno)); + close(fd); + return; + } + + if (write(fd, str.c_str(), str.size()) < 0) { + NETSTACK_LOGE("errmsg: [%{public}d] %{public}s|\n", errno, strerror(errno)); + } + + flock(fd, LOCK_UN); + close(fd); +} + +std::string DiskHandler::Read() +{ + int fd = open(fileName_.c_str(), O_RDONLY); + if (fd < 0) { + NETSTACK_LOGE("errmsg: [%{public}d] %{public}s|\n", errno, strerror(errno)); + return {}; + } + + int ret = flock(fd, LOCK_NB | LOCK_EX); + if (ret < 0) { + NETSTACK_LOGE("errmsg: [%{public}d] %{public}s|\n", errno, strerror(errno)); + close(fd); + return {}; + } + + struct stat buf = {0}; + if (fstat(fd, &buf) < 0 || buf.st_size <= 0) { + flock(fd, LOCK_UN); + close(fd); + return {}; + } + + std::unique_ptr mem = std::unique_ptr(new char[buf.st_size]); + if (read(fd, mem.get(), buf.st_size) < 0) { + NETSTACK_LOGE("errmsg: [%{public}d] %{public}s|\n", errno, strerror(errno)); + } + + flock(fd, LOCK_UN); + close(fd); + std::string str; + str.append(mem.get(), buf.st_size); + return str; +} +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache.cpp b/frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache.cpp new file mode 100644 index 000000000..bdd6661cf --- /dev/null +++ b/frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache.cpp @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2022 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. + */ + +#include +#include + +#include "lru_cache.h" +#include "netstack_log.h" + +static constexpr const char *LRU_INDEX = "LRUIndex"; +static constexpr const int DECIMAL_BASE = 10; +static constexpr const int MAX_SIZE = 1024 * 1024; +static constexpr const size_t INVALID_SIZE = SIZE_MAX; + +namespace OHOS::NetStack { +static size_t GetMapValueSize(const std::unordered_map &m) +{ + size_t size = 0; + for (const auto &p : m) { + if (p.second.size() > MAX_SIZE) { + return INVALID_SIZE; + } + if (size + p.second.size() > MAX_SIZE) { + return INVALID_SIZE; + } + size += p.second.size(); + } + if (size > MAX_SIZE || size == 0) { + return INVALID_SIZE; + } + return size; +} + +LRUCache::Node::Node(std::string key, std::unordered_map value) + : key(std::move(key)), value(std::move(value)) +{ +} + +LRUCache::LRUCache() : capacity_(MAX_SIZE), size_(0) {} + +LRUCache::LRUCache(size_t capacity) : capacity_(std::min(MAX_SIZE, capacity)), size_(0) {} + +void LRUCache::AddNode(const Node &node) +{ + // 添加至双向链表的头部 + // 添加进哈希表 + // size_递增 + nodeList_.emplace_front(node); + cache_[node.key] = nodeList_.begin(); + size_ += GetMapValueSize(node.value); +} + +void LRUCache::MoveNodeToHead(const std::list::iterator &it) +{ + std::string key = it->key; + std::unordered_map value = it->value; + nodeList_.erase(it); + nodeList_.emplace_front(key, value); + cache_[key] = nodeList_.begin(); +} + +void LRUCache::EraseTailNode() +{ + if (nodeList_.empty()) { + return; + } + Node node = nodeList_.back(); + nodeList_.pop_back(); + cache_.erase(node.key); + size_ -= GetMapValueSize(node.value); +} + +std::unordered_map LRUCache::Get(const std::string &key) +{ + if (cache_.find(key) == cache_.end()) { + return {}; + } + // 如果 key 存在,先通过哈希表定位,再移到头部 + auto it = cache_[key]; + auto value = it->value; + MoveNodeToHead(it); + return value; +} + +void LRUCache::Put(const std::string &key, const std::unordered_map &value) +{ + if (GetMapValueSize(value) == INVALID_SIZE) { + NETSTACK_LOGE("value is too long can not insert to cache"); + return; + } + + if (cache_.find(key) == cache_.end()) { + AddNode(Node(key, value)); + while (size_ > capacity_) { + // 如果超出容量,删除双向链表的尾部节点 + // 删除哈希表中对应的项 + EraseTailNode(); + } + return; + } + + // 如果 key 存在,先通过哈希表定位,再修改 value,并移到头部 + // 如果 key 存在,先通过哈希表定位,再移到头部 + auto it = cache_[key]; + + size_ -= GetMapValueSize(it->value); + it->value = value; + size_ += GetMapValueSize(it->value); + + MoveNodeToHead(it); + while (size_ > capacity_) { + // 如果超出容量,删除双向链表的尾部节点 + // 删除哈希表中对应的项 + EraseTailNode(); + } +} + +void LRUCache::MergeOtherCache(const LRUCache &other) +{ + if (other.nodeList_.empty()) { + return; + } + // 倒序插入,后插入的新鲜度最高 + auto reverseList = other.nodeList_; + reverseList.reverse(); + for (const auto &node : reverseList) { + Put(node.key, node.value); + } +} + +Json::Value LRUCache::WriteCacheToJsonValue() +{ + Json::Value root; + + int index = 0; + for (const auto &node : nodeList_) { + root[node.key] = Json::Value(); + for (const auto &p : node.value) { + root[node.key][p.first] = p.second; + } + root[node.key][LRU_INDEX] = std::to_string(index); + ++index; + } + return root; +} + +void LRUCache::ReadCacheFromJsonValue(const Json::Value &root) +{ + std::vector nodeVec; + for (auto it = root.begin(); it != root.end(); ++it) { + if (!it.key().isString()) { + continue; + } + Json::Value value = root[it.key().asString()]; + if (!value.isObject()) { + continue; + } + + std::unordered_map m; + for (auto innerIt = value.begin(); innerIt != value.end(); ++innerIt) { + if (!innerIt.key().isString()) { + continue; + } + Json::Value innerValue = root[it.key().asString()][innerIt.key().asString()]; + if (!innerValue.isString()) { + continue; + } + + m[innerIt.key().asString()] = innerValue.asString(); + } + + if (m.find(LRU_INDEX) != m.end()) { + nodeVec.emplace_back(it.key().asString(), m); + } + } + std::sort(nodeVec.begin(), nodeVec.end(), [](Node &a, Node &b) { + // 倒序排列,调用Put变回正序 + return std::strtol(a.value[LRU_INDEX].c_str(), nullptr, DECIMAL_BASE) > + std::strtol(b.value[LRU_INDEX].c_str(), nullptr, DECIMAL_BASE); + }); + for (auto &node : nodeVec) { + node.value.erase(LRU_INDEX); + if (!node.value.empty()) { + Put(node.key, node.value); + } + } +} +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache_disk_handler.cpp b/frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache_disk_handler.cpp new file mode 100644 index 000000000..92b86047f --- /dev/null +++ b/frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache_disk_handler.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2022 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. + */ + +#include +#include + +#include "lru_cache_disk_handler.h" +#include "netstack_log.h" + +static constexpr const int MAX_DISK_CACHE_SIZE = 1024 * 1024 * 10; +static constexpr const uint64_t MIN_WRITE_INTERVAL_MILLISECOND = 60 * 1000; +static uint64_t LAST_WRITE_TIME_MILLISECOND = 0; + +namespace OHOS::NetStack { +uint64_t GetNowTime() +{ + auto now = std::chrono::system_clock::now(); + return std::chrono::duration_cast(now.time_since_epoch()).count(); +} + +LRUCacheDiskHandler::LRUCacheDiskHandler(std::string fileName, LRUCache &cache, size_t capacity) + : cache_(cache), diskHandler_(std::move(fileName)), capacity_(std::min(MAX_DISK_CACHE_SIZE, capacity)) +{ +} + +Json::Value LRUCacheDiskHandler::GetJsonValueFromFile() +{ + std::string jsonStr = diskHandler_.Read(); + Json::Value root; + JSONCPP_STRING err; + Json::CharReaderBuilder builder; + std::unique_ptr reader(builder.newCharReader()); + if (!reader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.size(), &root, &err)) { + NETSTACK_LOGE("parse json failed: %{public}s", err.c_str()); + return {}; + } + return root; +} + +void LRUCacheDiskHandler::WriteCacheToJsonFile() +{ + uint64_t now = GetNowTime(); + if (now < LAST_WRITE_TIME_MILLISECOND) { + // 获取的系统时间比上一次记录的时间小,说明系统时间有问题,重新计算。 + LAST_WRITE_TIME_MILLISECOND = 0; + } + if (LAST_WRITE_TIME_MILLISECOND != 0 && now - LAST_WRITE_TIME_MILLISECOND < MIN_WRITE_INTERVAL_MILLISECOND) { + NETSTACK_LOGI("write too often"); + return; + } + LAST_WRITE_TIME_MILLISECOND = now; + + LRUCache oldCache(capacity_); + oldCache.ReadCacheFromJsonValue(GetJsonValueFromFile()); + oldCache.MergeOtherCache(cache_); + + Json::Value root = oldCache.WriteCacheToJsonValue(); + Json::StreamWriterBuilder builder; + std::unique_ptr writer(builder.newStreamWriter()); + std::stringstream s; + int res = writer->write(root, &s); + if (res != 0) { + NETSTACK_LOGE("write json failed %{public}d", res); + return; + } + diskHandler_.Write(s.str()); +} + +void LRUCacheDiskHandler::ReadCacheFromJsonFile() +{ + cache_.ReadCacheFromJsonValue(GetJsonValueFromFile()); +} +} // namespace OHOS::NetStack diff --git a/frameworks/js/napi/http/options/cache/md5/include/calculate_md5.h b/frameworks/js/napi/http/options/cache/md5/include/calculate_md5.h new file mode 100644 index 000000000..58a2e24a5 --- /dev/null +++ b/frameworks/js/napi/http/options/cache/md5/include/calculate_md5.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2022 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_CALCULATE_MD5_H +#define COMMUNICATIONNETSTACK_CALCULATE_MD5_H + +#include + +namespace OHOS::NetStack { +std::string Calculate(const std::string &source); +} // namespace OHOS::NetStack +#endif /* COMMUNICATIONNETSTACK_CALCULATE_MD5_H */ diff --git a/frameworks/js/napi/http/options/cache/md5/src/calculate_md5.cpp b/frameworks/js/napi/http/options/cache/md5/src/calculate_md5.cpp new file mode 100644 index 000000000..7d341a3bf --- /dev/null +++ b/frameworks/js/napi/http/options/cache/md5/src/calculate_md5.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 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. + */ + +#include "calculate_md5.h" +#include "openssl/md5.h" +#include "securec.h" + +static constexpr const int HEX_LENGTH = 2; + +namespace OHOS::NetStack { +std::string Calculate(const std::string &source) +{ + unsigned char md5[MD5_DIGEST_LENGTH] = {0}; + (void)MD5(reinterpret_cast(source.c_str()), source.size(), md5); + std::string str; + for (unsigned char i : md5) { + char s[HEX_LENGTH + 1] = {0}; + if (sprintf_s(s, HEX_LENGTH + 1, "%02x", i) < 0) { + return {}; + } + str += s; + } + return str; +} +} // namespace OHOS::NetStack \ No newline at end of file diff --git a/test/napi/http/CMakeLists.txt b/test/napi/http/CMakeLists.txt index ba0821ef0..4654076c2 100644 --- a/test/napi/http/CMakeLists.txt +++ b/test/napi/http/CMakeLists.txt @@ -17,8 +17,14 @@ include_directories(../../../frameworks/js/napi/http/http_exec/include) include_directories(../../../frameworks/js/napi/http/constant/include) include_directories(../../../frameworks/js/napi/http/async_context/include) include_directories(../../../frameworks/js/napi/http/options/include) +include_directories(../../../frameworks/js/napi/http/options/cache/lru_cache/include) +include_directories(../../../frameworks/js/napi/http/options/cache/md5/include) +include_directories(../../../frameworks/js/napi/http/options/cache/base64/include) +include_directories(../../../frameworks/js/napi/http/options/cache/cache_proxy/include) include_directories(../../../../../../third_party/curl/include) +include_directories(../../../../../../third_party/glib/glib/) +include_directories(../../../../../../third_party/glib/) link_directories(../../../../../../third_party/curl/cmake-build-debug/lib) @@ -33,7 +39,12 @@ add_executable( ../../../frameworks/js/napi/http/async_work/src/http_async_work.cpp ../../../frameworks/js/napi/http/options/src/http_response.cpp ../../../frameworks/js/napi/http/options/src/http_request_options.cpp + ../../../frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache.cpp ../../../frameworks/js/napi/http/http_exec/src/http_exec.cpp + ../../../frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache_disk_handler.cpp + ../../../frameworks/js/napi/http/options/cache/lru_cache/src/disk_handler.cpp + ../../../frameworks/js/napi/http/options/cache/md5/src/calculate_md5.cpp + ../../../frameworks/js/napi/http/options/cache/base64/src/base64_utils.cpp ) target_link_libraries(test_napi_http_exec curl-d) diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index d8c48749b..f49f288ea 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -20,4 +20,4 @@ add_library( event_manager/src/netstack_event_manager.cpp base_context/src/netstack_base_context.cpp module_template/src/netstack_module_template.cpp -) \ No newline at end of file + ) \ No newline at end of file -- Gitee From 9ba9a346155003cd8361242ffcd9ad98dc3f584d Mon Sep 17 00:00:00 2001 From: maosiping Date: Fri, 20 May 2022 11:52:43 +0800 Subject: [PATCH 02/20] Http Response Cache Signed-off-by: maosiping --- .../cache/base64/include/base64_utils.h | 0 .../cache/base64/src/base64_utils.cpp | 0 .../cache/cache_proxy/include/cache_proxy.h | 19 ++++ .../cache/cache_proxy/src/cache_proxy.cpp | 100 ++++++++++++++++++ .../cache_strategy/include/cache_strategy.h | 37 +++++++ .../cache_strategy/src/cache_strategy.cpp | 25 +++++ .../cache/lru_cache/include/disk_handler.h | 0 .../cache/lru_cache/include/lru_cache.h | 3 + .../include/lru_cache_disk_handler.h | 14 ++- .../cache/lru_cache/src/disk_handler.cpp | 0 .../cache/lru_cache/src/lru_cache.cpp | 42 ++++++-- .../lru_cache/src/lru_cache_disk_handler.cpp | 80 ++++++++------ .../cache/md5/include/calculate_md5.h | 2 +- .../cache/md5/src/calculate_md5.cpp | 2 +- .../js/napi/http/constant/include/constant.h | 3 + .../js/napi/http/constant/src/constant.cpp | 3 + .../js/napi/http/http_exec/src/http_exec.cpp | 7 ++ .../napi/http/options/include/http_response.h | 20 ++++ .../napi/http/options/src/http_response.cpp | 40 +++++++ test/napi/http/CMakeLists.txt | 22 ++-- 20 files changed, 362 insertions(+), 57 deletions(-) rename frameworks/js/napi/http/{options => }/cache/base64/include/base64_utils.h (100%) rename frameworks/js/napi/http/{options => }/cache/base64/src/base64_utils.cpp (100%) rename frameworks/js/napi/http/{options => }/cache/cache_proxy/include/cache_proxy.h (65%) create mode 100644 frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp create mode 100644 frameworks/js/napi/http/cache/cache_strategy/include/cache_strategy.h create mode 100644 frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp rename frameworks/js/napi/http/{options => }/cache/lru_cache/include/disk_handler.h (100%) rename frameworks/js/napi/http/{options => }/cache/lru_cache/include/lru_cache.h (97%) rename frameworks/js/napi/http/{options => }/cache/lru_cache/include/lru_cache_disk_handler.h (72%) rename frameworks/js/napi/http/{options => }/cache/lru_cache/src/disk_handler.cpp (100%) rename frameworks/js/napi/http/{options => }/cache/lru_cache/src/lru_cache.cpp (84%) rename frameworks/js/napi/http/{options => }/cache/lru_cache/src/lru_cache_disk_handler.cpp (50%) rename frameworks/js/napi/http/{options => }/cache/md5/include/calculate_md5.h (93%) rename frameworks/js/napi/http/{options => }/cache/md5/src/calculate_md5.cpp (95%) diff --git a/frameworks/js/napi/http/options/cache/base64/include/base64_utils.h b/frameworks/js/napi/http/cache/base64/include/base64_utils.h similarity index 100% rename from frameworks/js/napi/http/options/cache/base64/include/base64_utils.h rename to frameworks/js/napi/http/cache/base64/include/base64_utils.h diff --git a/frameworks/js/napi/http/options/cache/base64/src/base64_utils.cpp b/frameworks/js/napi/http/cache/base64/src/base64_utils.cpp similarity index 100% rename from frameworks/js/napi/http/options/cache/base64/src/base64_utils.cpp rename to frameworks/js/napi/http/cache/base64/src/base64_utils.cpp diff --git a/frameworks/js/napi/http/options/cache/cache_proxy/include/cache_proxy.h b/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h similarity index 65% rename from frameworks/js/napi/http/options/cache/cache_proxy/include/cache_proxy.h rename to frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h index de3defcca..878cafb0f 100644 --- a/frameworks/js/napi/http/options/cache/cache_proxy/include/cache_proxy.h +++ b/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h @@ -16,9 +16,28 @@ #ifndef COMMUNICATIONNETSTACK_CACHE_PROXY_H #define COMMUNICATIONNETSTACK_CACHE_PROXY_H +#include "http_request_options.h" +#include "http_response.h" +#include "nocopyable.h" + namespace OHOS::NetStack { class CacheProxy final { + DISALLOW_COPY_AND_MOVE(CacheProxy); + + HttpRequestOptions &requestOptions_; + + bool CouldUseCache(); + + std::string MakeKey(); + +public: + CacheProxy() = delete; + + CacheProxy(HttpRequestOptions &requestOptions); + + bool ReadResponseFromCache(HttpResponse &response); + void WriteResponseToCache(const HttpResponse &response); }; } // namespace OHOS::NetStack diff --git a/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp b/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp new file mode 100644 index 000000000..c8468eba6 --- /dev/null +++ b/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2022 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. + */ + +#include "cache_proxy.h" +#include "base64_utils.h" +#include "cache_strategy.h" +#include "calculate_md5.h" +#include "constant.h" +#include "curl/curl.h" +#include "http_exec.h" +#include "http_module.h" +#include "lru_cache_disk_handler.h" +#include "netstack_common_utils.h" +#include "request_context.h" + +static constexpr const char *KEY_RANGE = "range"; +static constexpr const char *CACHE_FILE = "./cache.json"; + +namespace OHOS::NetStack { +static LRUCacheDiskHandler DISK_LRU_CACHE(CACHE_FILE, MAX_DISK_CACHE_SIZE); // NOLINT(cert-err58-cpp) + +CacheProxy::CacheProxy(HttpRequestOptions &requestOptions) : requestOptions_(requestOptions) {} + +bool CacheProxy::CouldUseCache() +{ + return requestOptions_.GetMethod() == HttpConstant::HTTP_METHOD_GET || + requestOptions_.GetMethod() == HttpConstant::HTTP_METHOD_HEAD || + requestOptions_.GetHeader().find(KEY_RANGE) != requestOptions_.GetHeader().end(); +} + +std::string CacheProxy::MakeKey() +{ + std::string str = requestOptions_.GetUrl() + HttpConstant::HTTP_LINE_SEPARATOR + + CommonUtils::ToLower(requestOptions_.GetMethod()) + HttpConstant::HTTP_LINE_SEPARATOR; + for (const auto &p : requestOptions_.GetHeader()) { + str += p.first + HttpConstant::HTTP_HEADER_SEPARATOR + p.second + HttpConstant::HTTP_LINE_SEPARATOR; + } + return CalculateMD5(str); +} + +bool CacheProxy::ReadResponseFromCache(HttpResponse &response) +{ + if (!CouldUseCache()) { + return false; + } + + auto responseFromCache = DISK_LRU_CACHE.Get(MakeKey()); + HttpResponse cachedResponse; + cachedResponse.SetRawHeader(Base64::Decode(responseFromCache[HttpConstant::RESPONSE_KEY_HEADER])); + cachedResponse.SetResult(Base64::Decode(responseFromCache[HttpConstant::RESPONSE_KEY_RESULT])); + cachedResponse.SetCookies(Base64::Decode(responseFromCache[HttpConstant::RESPONSE_KEY_COOKIES])); + cachedResponse.SetRequestTime(Base64::Decode(responseFromCache[HttpConstant::REQUEST_TIME])); + cachedResponse.SetResponseTime(Base64::Decode(responseFromCache[HttpConstant::RESPONSE_TIME])); + cachedResponse.ParseHeaders(); + + CacheStatus status = CacheStrategy::GetCacheStatus(requestOptions_, cachedResponse); + if (status == CacheStatus::FRESH) { + response = cachedResponse; + return true; + } + if (status == CacheStatus::STATE) { + CacheStrategy::SetHeaderForValidation(requestOptions_, cachedResponse); + RequestContext context(nullptr, nullptr); + HttpExec::ExecRequest(&context); + if (context.response.GetResponseCode() == + static_cast(HttpModuleExports::ResponseCode::NOT_MODIFIED)) { + response = cachedResponse; + } else { + response = context.response; + } + WriteResponseToCache(response); + return true; + } + return false; +} + +void CacheProxy::WriteResponseToCache(const HttpResponse &response) +{ + std::unordered_map cacheResponse; + cacheResponse[HttpConstant::RESPONSE_KEY_HEADER] = Base64::Encode(response.GetRawHeader()); + cacheResponse[HttpConstant::RESPONSE_KEY_RESULT] = Base64::Encode(response.GetResult()); + cacheResponse[HttpConstant::RESPONSE_KEY_COOKIES] = Base64::Encode(response.GetCookies()); + cacheResponse[HttpConstant::REQUEST_TIME] = Base64::Encode(response.GetRequestTime()); + cacheResponse[HttpConstant::RESPONSE_TIME] = Base64::Encode(response.GetResponseTime()); + + DISK_LRU_CACHE.Put(CalculateMD5(MakeKey()), cacheResponse); +} +} // namespace OHOS::NetStack \ No newline at end of file diff --git a/frameworks/js/napi/http/cache/cache_strategy/include/cache_strategy.h b/frameworks/js/napi/http/cache/cache_strategy/include/cache_strategy.h new file mode 100644 index 000000000..173afe85f --- /dev/null +++ b/frameworks/js/napi/http/cache/cache_strategy/include/cache_strategy.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 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_CACHE_STRATEGY_H +#define COMMUNICATIONNETSTACK_CACHE_STRATEGY_H + +#include "http_request_options.h" +#include "http_response.h" + +namespace OHOS::NetStack { +enum class CacheStatus { + FRESH, + STATE, + TRANSPARENT [[maybe_unused]], +}; + +class CacheStrategy final { +public: + static CacheStatus GetCacheStatus(const HttpRequestOptions &request, const HttpResponse &response); + + static void SetHeaderForValidation(HttpRequestOptions &request, HttpResponse &response); +}; +} // namespace OHOS::NetStack + +#endif /* COMMUNICATIONNETSTACK_CACHE_STRATEGY_H */ diff --git a/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp b/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp new file mode 100644 index 000000000..f9fb2c5d0 --- /dev/null +++ b/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022 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. + */ + +#include "cache_strategy.h" + +namespace OHOS::NetStack { +CacheStatus CacheStrategy::GetCacheStatus(const HttpRequestOptions &request, const HttpResponse &response) +{ + return CacheStatus::FRESH; +} + +void CacheStrategy::SetHeaderForValidation(HttpRequestOptions &request, HttpResponse &response) {} +} // namespace OHOS::NetStack \ No newline at end of file diff --git a/frameworks/js/napi/http/options/cache/lru_cache/include/disk_handler.h b/frameworks/js/napi/http/cache/lru_cache/include/disk_handler.h similarity index 100% rename from frameworks/js/napi/http/options/cache/lru_cache/include/disk_handler.h rename to frameworks/js/napi/http/cache/lru_cache/include/disk_handler.h diff --git a/frameworks/js/napi/http/options/cache/lru_cache/include/lru_cache.h b/frameworks/js/napi/http/cache/lru_cache/include/lru_cache.h similarity index 97% rename from frameworks/js/napi/http/options/cache/lru_cache/include/lru_cache.h rename to frameworks/js/napi/http/cache/lru_cache/include/lru_cache.h index ed76343f4..6a3cd627f 100644 --- a/frameworks/js/napi/http/options/cache/lru_cache/include/lru_cache.h +++ b/frameworks/js/napi/http/cache/lru_cache/include/lru_cache.h @@ -43,6 +43,7 @@ private: void EraseTailNode(); + std::mutex mutex_; std::unordered_map::iterator> cache_; std::list nodeList_; size_t capacity_; @@ -62,6 +63,8 @@ public: Json::Value WriteCacheToJsonValue(); void ReadCacheFromJsonValue(const Json::Value &root); + + void Clear(); }; } // namespace OHOS::NetStack #endif /* COMMUNICATIONNETSTACK_LRU_CACHE_H */ diff --git a/frameworks/js/napi/http/options/cache/lru_cache/include/lru_cache_disk_handler.h b/frameworks/js/napi/http/cache/lru_cache/include/lru_cache_disk_handler.h similarity index 72% rename from frameworks/js/napi/http/options/cache/lru_cache/include/lru_cache_disk_handler.h rename to frameworks/js/napi/http/cache/lru_cache/include/lru_cache_disk_handler.h index 3636a1e09..8e052900b 100644 --- a/frameworks/js/napi/http/options/cache/lru_cache/include/lru_cache_disk_handler.h +++ b/frameworks/js/napi/http/cache/lru_cache/include/lru_cache_disk_handler.h @@ -20,23 +20,31 @@ #include "lru_cache.h" #include "nocopyable.h" +static constexpr const int MAX_DISK_CACHE_SIZE = 1024 * 1024 * 5; + namespace OHOS::NetStack { class LRUCacheDiskHandler { private: - LRUCache &cache_; + LRUCache cache_; DiskHandler diskHandler_; size_t capacity_; - Json::Value GetJsonValueFromFile(); + Json::Value ReadJsonValueFromFile(); + + void WriteJsonValueToFile(const Json::Value &root); public: LRUCacheDiskHandler() = delete; - LRUCacheDiskHandler(std::string fileName, LRUCache &cache, size_t capacity); + LRUCacheDiskHandler(std::string fileName, size_t capacity); void WriteCacheToJsonFile(); void ReadCacheFromJsonFile(); + + std::unordered_map Get(const std::string &key); + + void Put(const std::string &key, const std::unordered_map &value); }; } // namespace OHOS::NetStack #endif /* COMMUNICATIONNETSTACK_LRU_CACHE_DISK_HANDLER_H */ diff --git a/frameworks/js/napi/http/options/cache/lru_cache/src/disk_handler.cpp b/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp similarity index 100% rename from frameworks/js/napi/http/options/cache/lru_cache/src/disk_handler.cpp rename to frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp diff --git a/frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache.cpp b/frameworks/js/napi/http/cache/lru_cache/src/lru_cache.cpp similarity index 84% rename from frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache.cpp rename to frameworks/js/napi/http/cache/lru_cache/src/lru_cache.cpp index bdd6661cf..72d6ed4d4 100644 --- a/frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache.cpp +++ b/frameworks/js/napi/http/cache/lru_cache/src/lru_cache.cpp @@ -84,6 +84,8 @@ void LRUCache::EraseTailNode() std::unordered_map LRUCache::Get(const std::string &key) { + std::lock_guard guard(mutex_); + if (cache_.find(key) == cache_.end()) { return {}; } @@ -96,8 +98,10 @@ std::unordered_map LRUCache::Get(const std::string &ke void LRUCache::Put(const std::string &key, const std::unordered_map &value) { + std::lock_guard guard(mutex_); + if (GetMapValueSize(value) == INVALID_SIZE) { - NETSTACK_LOGE("value is too long can not insert to cache"); + NETSTACK_LOGE("value is invalid(0 or too long) can not insert to cache"); return; } @@ -129,11 +133,16 @@ void LRUCache::Put(const std::string &key, const std::unordered_map reverseList; + { + // set mutex in min scope + std::lock_guard guard(mutex_); + if (other.nodeList_.empty()) { + return; + } + // 倒序插入,后插入的新鲜度最高 + reverseList = other.nodeList_; } - // 倒序插入,后插入的新鲜度最高 - auto reverseList = other.nodeList_; reverseList.reverse(); for (const auto &node : reverseList) { Put(node.key, node.value); @@ -145,13 +154,17 @@ Json::Value LRUCache::WriteCacheToJsonValue() Json::Value root; int index = 0; - for (const auto &node : nodeList_) { - root[node.key] = Json::Value(); - for (const auto &p : node.value) { - root[node.key][p.first] = p.second; + { + // set mutex in min scope + std::lock_guard guard(mutex_); + for (const auto &node : nodeList_) { + root[node.key] = Json::Value(); + for (const auto &p : node.value) { + root[node.key][p.first] = p.second; + } + root[node.key][LRU_INDEX] = std::to_string(index); + ++index; } - root[node.key][LRU_INDEX] = std::to_string(index); - ++index; } return root; } @@ -197,4 +210,11 @@ void LRUCache::ReadCacheFromJsonValue(const Json::Value &root) } } } + +void LRUCache::Clear() +{ + std::lock_guard guard(mutex_); + cache_.clear(); + nodeList_.clear(); +} } // namespace OHOS::NetStack diff --git a/frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache_disk_handler.cpp b/frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp similarity index 50% rename from frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache_disk_handler.cpp rename to frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp index 92b86047f..cea93e96c 100644 --- a/frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache_disk_handler.cpp +++ b/frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp @@ -15,27 +15,32 @@ #include #include +#include #include "lru_cache_disk_handler.h" #include "netstack_log.h" -static constexpr const int MAX_DISK_CACHE_SIZE = 1024 * 1024 * 10; -static constexpr const uint64_t MIN_WRITE_INTERVAL_MILLISECOND = 60 * 1000; -static uint64_t LAST_WRITE_TIME_MILLISECOND = 0; +static constexpr const int WRITE_INTERVAL = 60 * 1000; namespace OHOS::NetStack { -uint64_t GetNowTime() +LRUCacheDiskHandler::LRUCacheDiskHandler(std::string fileName, size_t capacity) + : diskHandler_(std::move(fileName)), capacity_(std::min(MAX_DISK_CACHE_SIZE, capacity)) { - auto now = std::chrono::system_clock::now(); - return std::chrono::duration_cast(now.time_since_epoch()).count(); + // 从磁盘中读取缓存到内存里 + ReadCacheFromJsonFile(); + // 起线程每一分钟讲内存缓存持久化 + std::thread([this]() { +#pragma clang diagnostic push +#pragma ide diagnostic ignored "EndlessLoop" + while (true) { + std::this_thread::sleep_for(std::chrono::milliseconds(WRITE_INTERVAL)); + WriteCacheToJsonFile(); + } +#pragma clang diagnostic pop + }).detach(); } -LRUCacheDiskHandler::LRUCacheDiskHandler(std::string fileName, LRUCache &cache, size_t capacity) - : cache_(cache), diskHandler_(std::move(fileName)), capacity_(std::min(MAX_DISK_CACHE_SIZE, capacity)) -{ -} - -Json::Value LRUCacheDiskHandler::GetJsonValueFromFile() +Json::Value LRUCacheDiskHandler::ReadJsonValueFromFile() { std::string jsonStr = diskHandler_.Read(); Json::Value root; @@ -43,30 +48,14 @@ Json::Value LRUCacheDiskHandler::GetJsonValueFromFile() Json::CharReaderBuilder builder; std::unique_ptr reader(builder.newCharReader()); if (!reader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.size(), &root, &err)) { - NETSTACK_LOGE("parse json failed: %{public}s", err.c_str()); + NETSTACK_LOGI("parse json not success, maybe file is broken: %{public}s", err.c_str()); return {}; } return root; } -void LRUCacheDiskHandler::WriteCacheToJsonFile() +void LRUCacheDiskHandler::WriteJsonValueToFile(const Json::Value &root) { - uint64_t now = GetNowTime(); - if (now < LAST_WRITE_TIME_MILLISECOND) { - // 获取的系统时间比上一次记录的时间小,说明系统时间有问题,重新计算。 - LAST_WRITE_TIME_MILLISECOND = 0; - } - if (LAST_WRITE_TIME_MILLISECOND != 0 && now - LAST_WRITE_TIME_MILLISECOND < MIN_WRITE_INTERVAL_MILLISECOND) { - NETSTACK_LOGI("write too often"); - return; - } - LAST_WRITE_TIME_MILLISECOND = now; - - LRUCache oldCache(capacity_); - oldCache.ReadCacheFromJsonValue(GetJsonValueFromFile()); - oldCache.MergeOtherCache(cache_); - - Json::Value root = oldCache.WriteCacheToJsonValue(); Json::StreamWriterBuilder builder; std::unique_ptr writer(builder.newStreamWriter()); std::stringstream s; @@ -78,8 +67,37 @@ void LRUCacheDiskHandler::WriteCacheToJsonFile() diskHandler_.Write(s.str()); } +void LRUCacheDiskHandler::WriteCacheToJsonFile() +{ + LRUCache oldCache(capacity_); + oldCache.ReadCacheFromJsonValue(ReadJsonValueFromFile()); + oldCache.MergeOtherCache(cache_); + Json::Value root = oldCache.WriteCacheToJsonValue(); + WriteJsonValueToFile(root); + cache_.Clear(); +} + void LRUCacheDiskHandler::ReadCacheFromJsonFile() { - cache_.ReadCacheFromJsonValue(GetJsonValueFromFile()); + cache_.ReadCacheFromJsonValue(ReadJsonValueFromFile()); +} + +std::unordered_map LRUCacheDiskHandler::Get(const std::string &key) +{ + auto valueFromMemory = cache_.Get(key); + if (!valueFromMemory.empty()) { + return valueFromMemory; + } + + LRUCache diskCache(capacity_); + diskCache.ReadCacheFromJsonValue(ReadJsonValueFromFile()); + auto valueFromDisk = diskCache.Get(key); + cache_.Put(key, valueFromDisk); + return valueFromDisk; +} + +void LRUCacheDiskHandler::Put(const std::string &key, const std::unordered_map &value) +{ + cache_.Put(key, value); } } // namespace OHOS::NetStack diff --git a/frameworks/js/napi/http/options/cache/md5/include/calculate_md5.h b/frameworks/js/napi/http/cache/md5/include/calculate_md5.h similarity index 93% rename from frameworks/js/napi/http/options/cache/md5/include/calculate_md5.h rename to frameworks/js/napi/http/cache/md5/include/calculate_md5.h index 58a2e24a5..632ddb3ad 100644 --- a/frameworks/js/napi/http/options/cache/md5/include/calculate_md5.h +++ b/frameworks/js/napi/http/cache/md5/include/calculate_md5.h @@ -19,6 +19,6 @@ #include namespace OHOS::NetStack { -std::string Calculate(const std::string &source); +std::string CalculateMD5(const std::string &source); } // namespace OHOS::NetStack #endif /* COMMUNICATIONNETSTACK_CALCULATE_MD5_H */ diff --git a/frameworks/js/napi/http/options/cache/md5/src/calculate_md5.cpp b/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp similarity index 95% rename from frameworks/js/napi/http/options/cache/md5/src/calculate_md5.cpp rename to frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp index 7d341a3bf..1f3066f80 100644 --- a/frameworks/js/napi/http/options/cache/md5/src/calculate_md5.cpp +++ b/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp @@ -20,7 +20,7 @@ static constexpr const int HEX_LENGTH = 2; namespace OHOS::NetStack { -std::string Calculate(const std::string &source) +std::string CalculateMD5(const std::string &source) { unsigned char md5[MD5_DIGEST_LENGTH] = {0}; (void)MD5(reinterpret_cast(source.c_str()), source.size(), md5); diff --git a/frameworks/js/napi/http/constant/include/constant.h b/frameworks/js/napi/http/constant/include/constant.h index 87f1c6230..047ed1f7e 100644 --- a/frameworks/js/napi/http/constant/include/constant.h +++ b/frameworks/js/napi/http/constant/include/constant.h @@ -71,6 +71,9 @@ public: static const char *const HTTP_CONTENT_TYPE_JPEG_STREAM; static const char *const HTTP_CONTENT_ENCODING_GZIP; + + static const char *const REQUEST_TIME; + static const char *const RESPONSE_TIME; }; } // namespace OHOS::NetStack diff --git a/frameworks/js/napi/http/constant/src/constant.cpp b/frameworks/js/napi/http/constant/src/constant.cpp index e51aacc9c..a34b401ac 100644 --- a/frameworks/js/napi/http/constant/src/constant.cpp +++ b/frameworks/js/napi/http/constant/src/constant.cpp @@ -59,4 +59,7 @@ const char *const HttpConstant::HTTP_CONTENT_TYPE_OCTET_STREAM = "application/oc const char *const HttpConstant::HTTP_CONTENT_TYPE_JPEG_STREAM = "image/jpeg"; const char *const HttpConstant::HTTP_CONTENT_ENCODING_GZIP = "gzip"; + +const char *const HttpConstant::REQUEST_TIME = "requestTime"; +const char *const HttpConstant::RESPONSE_TIME = "responseTime"; } // namespace OHOS::NetStack \ No newline at end of file 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 99cecfeb7..d2d3933fb 100644 --- a/frameworks/js/napi/http/http_exec/src/http_exec.cpp +++ b/frameworks/js/napi/http/http_exec/src/http_exec.cpp @@ -19,6 +19,7 @@ #include #include +#include "cache_proxy.h" #include "constant.h" #include "event_list.h" #include "netstack_common_utils.h" @@ -66,6 +67,11 @@ bool HttpExec::initialized_ = false; bool HttpExec::ExecRequest(RequestContext *context) { + CacheProxy proxy(context->options); + if (proxy.ReadResponseFromCache(context->response)) { + return true; + } + if (!initialized_) { NETSTACK_LOGE("curl not init"); return false; @@ -111,6 +117,7 @@ bool HttpExec::ExecRequest(RequestContext *context) context->response.SetResponseCode(responseCode); context->response.ParseHeaders(); + proxy.WriteResponseToCache(context->response); return true; } diff --git a/frameworks/js/napi/http/options/include/http_response.h b/frameworks/js/napi/http/options/include/http_response.h index 359467464..93c1b0b23 100644 --- a/frameworks/js/napi/http/options/include/http_response.h +++ b/frameworks/js/napi/http/options/include/http_response.h @@ -30,10 +30,16 @@ public: void AppendRawHeader(const void *data, size_t length); + void SetRawHeader(const std::string &header); + void SetResponseCode(uint32_t responseCode); void ParseHeaders(); + void SetResult(const std::string &res); + + void SetCookies(const std::string &cookies); + void AppendCookies(const void *data, size_t length); [[nodiscard]] const std::string &GetResult() const; @@ -44,6 +50,16 @@ public: [[nodiscard]] const std::string &GetCookies() const; + [[nodiscard]] const std::string &GetRawHeader() const; + + void SetRequestTime(const std::string &time); + + [[nodiscard]] const std::string &GetRequestTime() const; + + void SetResponseTime(const std::string &time); + + [[nodiscard]] const std::string &GetResponseTime() const; + private: std::string result_; @@ -54,6 +70,10 @@ private: std::map header_; std::string cookies_; + + std::string requestTime_; + + std::string responseTime_; }; } // namespace OHOS::NetStack diff --git a/frameworks/js/napi/http/options/src/http_response.cpp b/frameworks/js/napi/http/options/src/http_response.cpp index 4a2fb1728..eb3c53818 100644 --- a/frameworks/js/napi/http/options/src/http_response.cpp +++ b/frameworks/js/napi/http/options/src/http_response.cpp @@ -79,4 +79,44 @@ const std::string &HttpResponse::GetCookies() const { return cookies_; } + +void HttpResponse::SetRawHeader(const std::string &header) +{ + rawHeader_ = header; +} + +const std::string &HttpResponse::GetRawHeader() const +{ + return rawHeader_; +} + +void HttpResponse::SetResult(const std::string &res) +{ + result_ = res; +} + +void HttpResponse::SetCookies(const std::string &cookies) +{ + cookies_ = cookies; +} + +void HttpResponse::SetRequestTime(const std::string &time) +{ + requestTime_ = time; +} + +const std::string &HttpResponse::GetRequestTime() const +{ + return requestTime_; +} + +void HttpResponse::SetResponseTime(const std::string &time) +{ + responseTime_ = time; +} + +const std::string &HttpResponse::GetResponseTime() const +{ + return responseTime_; +} } // namespace OHOS::NetStack \ No newline at end of file diff --git a/test/napi/http/CMakeLists.txt b/test/napi/http/CMakeLists.txt index 4654076c2..054461f01 100644 --- a/test/napi/http/CMakeLists.txt +++ b/test/napi/http/CMakeLists.txt @@ -17,10 +17,11 @@ include_directories(../../../frameworks/js/napi/http/http_exec/include) include_directories(../../../frameworks/js/napi/http/constant/include) include_directories(../../../frameworks/js/napi/http/async_context/include) include_directories(../../../frameworks/js/napi/http/options/include) -include_directories(../../../frameworks/js/napi/http/options/cache/lru_cache/include) -include_directories(../../../frameworks/js/napi/http/options/cache/md5/include) -include_directories(../../../frameworks/js/napi/http/options/cache/base64/include) -include_directories(../../../frameworks/js/napi/http/options/cache/cache_proxy/include) +include_directories(../../../frameworks/js/napi/http/cache/lru_cache/include) +include_directories(../../../frameworks/js/napi/http/cache/md5/include) +include_directories(../../../frameworks/js/napi/http/cache/base64/include) +include_directories(../../../frameworks/js/napi/http/cache/cache_proxy/include) +include_directories(../../../frameworks/js/napi/http/cache/cache_strategy/include) include_directories(../../../../../../third_party/curl/include) include_directories(../../../../../../third_party/glib/glib/) @@ -39,13 +40,14 @@ add_executable( ../../../frameworks/js/napi/http/async_work/src/http_async_work.cpp ../../../frameworks/js/napi/http/options/src/http_response.cpp ../../../frameworks/js/napi/http/options/src/http_request_options.cpp - ../../../frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache.cpp + ../../../frameworks/js/napi/http/cache/lru_cache/src/lru_cache.cpp ../../../frameworks/js/napi/http/http_exec/src/http_exec.cpp - ../../../frameworks/js/napi/http/options/cache/lru_cache/src/lru_cache_disk_handler.cpp - ../../../frameworks/js/napi/http/options/cache/lru_cache/src/disk_handler.cpp - ../../../frameworks/js/napi/http/options/cache/md5/src/calculate_md5.cpp - ../../../frameworks/js/napi/http/options/cache/base64/src/base64_utils.cpp -) + ../../../frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp + ../../../frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp + ../../../frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp + ../../../frameworks/js/napi/http/cache/base64/src/base64_utils.cpp + ../../../frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp + ../../../frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp) target_link_libraries(test_napi_http_exec curl-d) target_link_libraries(test_napi_http_exec ace_napi_quickjs) -- Gitee From 8c8439819fa14119ba659e1b238bbbce16d4cc9e Mon Sep 17 00:00:00 2001 From: maosiping Date: Fri, 20 May 2022 14:50:20 +0800 Subject: [PATCH 03/20] Http Response Cache Signed-off-by: maosiping --- frameworks/js/napi/BUILD.gn | 27 +++++++++++++++++-- .../cache/cache_proxy/src/cache_proxy.cpp | 2 +- .../http/cache/lru_cache/src/disk_handler.cpp | 12 ++++----- .../lru_cache/src/lru_cache_disk_handler.cpp | 2 +- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index 40e43fd92..60e644600 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -57,6 +57,11 @@ ohos_shared_library("http") { include_dirs = [ "$NETSTACK_NAPI_ROOT/http/async_context/include", "$NETSTACK_NAPI_ROOT/http/async_work/include", + "$NETSTACK_NAPI_ROOT/http/cache/base64/include", + "$NETSTACK_NAPI_ROOT/http/cache/cache_proxy/include", + "$NETSTACK_NAPI_ROOT/http/cache/cache_strategy/include", + "$NETSTACK_NAPI_ROOT/http/cache/lru_cache/include", + "$NETSTACK_NAPI_ROOT/http/cache/md5/include", "$NETSTACK_NAPI_ROOT/http/constant/include", "$NETSTACK_NAPI_ROOT/http/http_exec/include", "$NETSTACK_NAPI_ROOT/http/http_module/include", @@ -64,11 +69,24 @@ ohos_shared_library("http") { ] include_dirs += utils_include include_dirs += common_include - include_dirs += [ "//third_party/curl/include" ] + include_dirs += [ + "//third_party/curl/include", + "//third_party/glib/glib", + "//third_party/glib", + "//third_party/jsoncpp/include", + "//third_party/openssl/include", + ] sources = [ "http/async_context/src/request_context.cpp", "http/async_work/src/http_async_work.cpp", + "http/cache/base64/src/base64_utils.cpp", + "http/cache/cache_proxy/src/cache_proxy.cpp", + "http/cache/cache_strategy/src/cache_strategy.cpp", + "http/cache/lru_cache/src/disk_handler.cpp", + "http/cache/lru_cache/src/lru_cache.cpp", + "http/cache/lru_cache/src/lru_cache_disk_handler.cpp", + "http/cache/md5/src/calculate_md5.cpp", "http/constant/src/constant.cpp", "http/http_exec/src/http_exec.cpp", "http/http_module/src/http_module.cpp", @@ -78,7 +96,12 @@ ohos_shared_library("http") { sources += utils_source deps = common_deps - deps += [ "//third_party/curl:curl" ] + deps += [ + "//third_party/curl:curl", + "//third_party/glib:glib_packages", + "//third_party/jsoncpp:jsoncpp", + "//third_party/openssl:libcrypto_shared", + ] external_deps = common_external_deps diff --git a/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp b/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp index c8468eba6..2184ddc7c 100644 --- a/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp +++ b/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp @@ -95,6 +95,6 @@ void CacheProxy::WriteResponseToCache(const HttpResponse &response) cacheResponse[HttpConstant::REQUEST_TIME] = Base64::Encode(response.GetRequestTime()); cacheResponse[HttpConstant::RESPONSE_TIME] = Base64::Encode(response.GetResponseTime()); - DISK_LRU_CACHE.Put(CalculateMD5(MakeKey()), cacheResponse); + DISK_LRU_CACHE.Put(MakeKey(), cacheResponse); } } // namespace OHOS::NetStack \ No newline at end of file diff --git a/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp b/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp index 8332aa0a6..ee5da5ad4 100644 --- a/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp +++ b/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp @@ -29,19 +29,19 @@ void DiskHandler::Write(const std::string &str) { int fd = open(fileName_.c_str(), O_CREAT | O_WRONLY); if (fd < 0) { - NETSTACK_LOGE("errmsg: [%{public}d] %{public}s|\n", errno, strerror(errno)); + NETSTACK_LOGE("errmsg: Write open [%{public}d] %{public}s|\n", errno, strerror(errno)); return; } int ret = flock(fd, LOCK_NB | LOCK_EX); if (ret < 0) { - NETSTACK_LOGE("errmsg: [%{public}d] %{public}s|\n", errno, strerror(errno)); + NETSTACK_LOGE("errmsg: Write open [%{public}d] %{public}s|\n", errno, strerror(errno)); close(fd); return; } if (write(fd, str.c_str(), str.size()) < 0) { - NETSTACK_LOGE("errmsg: [%{public}d] %{public}s|\n", errno, strerror(errno)); + NETSTACK_LOGE("errmsg: Write open [%{public}d] %{public}s|\n", errno, strerror(errno)); } flock(fd, LOCK_UN); @@ -52,13 +52,13 @@ std::string DiskHandler::Read() { int fd = open(fileName_.c_str(), O_RDONLY); if (fd < 0) { - NETSTACK_LOGE("errmsg: [%{public}d] %{public}s|\n", errno, strerror(errno)); + NETSTACK_LOGE("errmsg: Read open [%{public}d] %{public}s|\n", errno, strerror(errno)); return {}; } int ret = flock(fd, LOCK_NB | LOCK_EX); if (ret < 0) { - NETSTACK_LOGE("errmsg: [%{public}d] %{public}s|\n", errno, strerror(errno)); + NETSTACK_LOGE("errmsg: Read flock [%{public}d] %{public}s|\n", errno, strerror(errno)); close(fd); return {}; } @@ -72,7 +72,7 @@ std::string DiskHandler::Read() std::unique_ptr mem = std::unique_ptr(new char[buf.st_size]); if (read(fd, mem.get(), buf.st_size) < 0) { - NETSTACK_LOGE("errmsg: [%{public}d] %{public}s|\n", errno, strerror(errno)); + NETSTACK_LOGE("errmsg: Read read [%{public}d] %{public}s|\n", errno, strerror(errno)); } flock(fd, LOCK_UN); diff --git a/frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp b/frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp index cea93e96c..73b49f794 100644 --- a/frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp +++ b/frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp @@ -20,7 +20,7 @@ #include "lru_cache_disk_handler.h" #include "netstack_log.h" -static constexpr const int WRITE_INTERVAL = 60 * 1000; +static constexpr const int WRITE_INTERVAL = 5 * 1000; namespace OHOS::NetStack { LRUCacheDiskHandler::LRUCacheDiskHandler(std::string fileName, size_t capacity) -- Gitee From b571cace5203e0d750693e980fbccfddef51bc71 Mon Sep 17 00:00:00 2001 From: maosiping Date: Fri, 20 May 2022 15:00:16 +0800 Subject: [PATCH 04/20] Http Response Cache Signed-off-by: maosiping --- .../napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp b/frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp index 73b49f794..2c3ccd8e7 100644 --- a/frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp +++ b/frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp @@ -30,13 +30,10 @@ LRUCacheDiskHandler::LRUCacheDiskHandler(std::string fileName, size_t capacity) ReadCacheFromJsonFile(); // 起线程每一分钟讲内存缓存持久化 std::thread([this]() { -#pragma clang diagnostic push -#pragma ide diagnostic ignored "EndlessLoop" while (true) { std::this_thread::sleep_for(std::chrono::milliseconds(WRITE_INTERVAL)); WriteCacheToJsonFile(); } -#pragma clang diagnostic pop }).detach(); } -- Gitee From 05c90e6bb84449c756cc7d6e0f0eeb0dc8b2a5d0 Mon Sep 17 00:00:00 2001 From: maosiping Date: Fri, 20 May 2022 15:03:11 +0800 Subject: [PATCH 05/20] Http Response Cache Signed-off-by: maosiping --- frameworks/js/napi/http/cache/base64/src/base64_utils.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/frameworks/js/napi/http/cache/base64/src/base64_utils.cpp b/frameworks/js/napi/http/cache/base64/src/base64_utils.cpp index a3940462b..3df47915a 100644 --- a/frameworks/js/napi/http/cache/base64/src/base64_utils.cpp +++ b/frameworks/js/napi/http/cache/base64/src/base64_utils.cpp @@ -19,10 +19,6 @@ #include "glib.h" #include "netstack_log.h" -static constexpr const size_t MAX_SOURCE_SIZE = 1024 * 1024; -static constexpr const size_t MAX_ENCODED_SIZE = 1024 * 1024 * 2; -static constexpr const size_t DOUBLE_LENGTH = 2; - namespace OHOS::NetStack::Base64 { std::string Encode(const std::string &source) { -- Gitee From 21c1dbb9f8c11d943f91024cbe46518774e6d871 Mon Sep 17 00:00:00 2001 From: maosiping Date: Fri, 20 May 2022 15:06:55 +0800 Subject: [PATCH 06/20] Http Response Cache Signed-off-by: maosiping --- frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp b/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp index ee5da5ad4..21705af77 100644 --- a/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp +++ b/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp @@ -16,6 +16,7 @@ #include "disk_handler.h" #include "netstack_log.h" +#include #include #include #include -- Gitee From 03ebccba5986ab7f942b606a8bf132dac1459191 Mon Sep 17 00:00:00 2001 From: maosiping Date: Fri, 20 May 2022 15:15:19 +0800 Subject: [PATCH 07/20] Http Response Cache Signed-off-by: maosiping --- frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp b/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp index 21705af77..eb4cf0395 100644 --- a/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp +++ b/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp @@ -16,7 +16,7 @@ #include "disk_handler.h" #include "netstack_log.h" -#include +#include #include #include #include @@ -28,7 +28,7 @@ DiskHandler::DiskHandler(std::string fileName) : fileName_(std::move(fileName)) void DiskHandler::Write(const std::string &str) { - int fd = open(fileName_.c_str(), O_CREAT | O_WRONLY); + int fd = open(fileName_.c_str(), O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR); if (fd < 0) { NETSTACK_LOGE("errmsg: Write open [%{public}d] %{public}s|\n", errno, strerror(errno)); return; -- Gitee From 533fec043d5d35359e5eaaf21a059ff4c6311486 Mon Sep 17 00:00:00 2001 From: maosiping Date: Fri, 20 May 2022 15:39:31 +0800 Subject: [PATCH 08/20] Http Response Cache Signed-off-by: maosiping --- frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp b/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp index 2184ddc7c..303149675 100644 --- a/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp +++ b/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp @@ -57,6 +57,9 @@ bool CacheProxy::ReadResponseFromCache(HttpResponse &response) } auto responseFromCache = DISK_LRU_CACHE.Get(MakeKey()); + if (responseFromCache.empty()) { + return false; + } HttpResponse cachedResponse; cachedResponse.SetRawHeader(Base64::Decode(responseFromCache[HttpConstant::RESPONSE_KEY_HEADER])); cachedResponse.SetResult(Base64::Decode(responseFromCache[HttpConstant::RESPONSE_KEY_RESULT])); -- Gitee From e40c16d9fb7eb0cbf01fab2a6d68016be5da7a27 Mon Sep 17 00:00:00 2001 From: maosiping Date: Fri, 20 May 2022 15:56:12 +0800 Subject: [PATCH 09/20] Http Response Cache Signed-off-by: maosiping --- .../js/napi/http/cache/lru_cache/src/disk_handler.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp b/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp index eb4cf0395..420c0d879 100644 --- a/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp +++ b/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp @@ -28,6 +28,15 @@ DiskHandler::DiskHandler(std::string fileName) : fileName_(std::move(fileName)) void DiskHandler::Write(const std::string &str) { + char buffer[500] = {0}; + getcwd(buffer, sizeof(buffer)); + if (strlen(buffer)) { + NETSTACK_LOGI("pwd: %{public}s", buffer); + } else { + NETSTACK_LOGI("pwd: can not get pwd"); + } + + int fd = open(fileName_.c_str(), O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR); if (fd < 0) { NETSTACK_LOGE("errmsg: Write open [%{public}d] %{public}s|\n", errno, strerror(errno)); -- Gitee From 506c51f628c6dc63bccdc4b8ae604ff397303509 Mon Sep 17 00:00:00 2001 From: maosiping Date: Fri, 20 May 2022 16:04:18 +0800 Subject: [PATCH 10/20] Http Response Cache Signed-off-by: maosiping --- frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp b/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp index 303149675..51620f958 100644 --- a/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp +++ b/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp @@ -26,7 +26,7 @@ #include "request_context.h" static constexpr const char *KEY_RANGE = "range"; -static constexpr const char *CACHE_FILE = "./cache.json"; +static constexpr const char *CACHE_FILE = "/data/storage/el2/base/cache.json"; namespace OHOS::NetStack { static LRUCacheDiskHandler DISK_LRU_CACHE(CACHE_FILE, MAX_DISK_CACHE_SIZE); // NOLINT(cert-err58-cpp) -- Gitee From cb00d259b6a1927ce1345b40bdc246926d899e0a Mon Sep 17 00:00:00 2001 From: maosiping Date: Fri, 20 May 2022 18:02:01 +0800 Subject: [PATCH 11/20] Http Response Cache Signed-off-by: maosiping --- .../http/cache/base64/src/base64_utils.cpp | 3 +- .../cache/cache_proxy/include/cache_proxy.h | 2 -- .../cache/lru_cache/include/disk_handler.h | 10 +++--- .../http/cache/lru_cache/include/lru_cache.h | 36 +++++++++---------- .../include/lru_cache_disk_handler.h | 18 +++++----- .../http/cache/lru_cache/src/disk_handler.cpp | 11 +----- .../http/cache/lru_cache/src/lru_cache.cpp | 3 +- .../lru_cache/src/lru_cache_disk_handler.cpp | 5 ++- .../napi/http/cache/md5/src/calculate_md5.cpp | 2 +- 9 files changed, 40 insertions(+), 50 deletions(-) diff --git a/frameworks/js/napi/http/cache/base64/src/base64_utils.cpp b/frameworks/js/napi/http/cache/base64/src/base64_utils.cpp index 3df47915a..c2149272c 100644 --- a/frameworks/js/napi/http/cache/base64/src/base64_utils.cpp +++ b/frameworks/js/napi/http/cache/base64/src/base64_utils.cpp @@ -15,10 +15,11 @@ #include -#include "base64_utils.h" #include "glib.h" #include "netstack_log.h" +#include "base64_utils.h" + namespace OHOS::NetStack::Base64 { std::string Encode(const std::string &source) { diff --git a/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h b/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h index 878cafb0f..16ef5fef3 100644 --- a/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h +++ b/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h @@ -39,7 +39,5 @@ public: void WriteResponseToCache(const HttpResponse &response); }; - } // namespace OHOS::NetStack - #endif // COMMUNICATIONNETSTACK_CACHE_PROXY_H diff --git a/frameworks/js/napi/http/cache/lru_cache/include/disk_handler.h b/frameworks/js/napi/http/cache/lru_cache/include/disk_handler.h index ea11eacca..400813b44 100644 --- a/frameworks/js/napi/http/cache/lru_cache/include/disk_handler.h +++ b/frameworks/js/napi/http/cache/lru_cache/include/disk_handler.h @@ -22,11 +22,6 @@ namespace OHOS::NetStack { class DiskHandler final { -private: - DISALLOW_COPY_AND_MOVE(DiskHandler); - - std::string fileName_; - public: DiskHandler() = delete; @@ -35,6 +30,11 @@ public: void Write(const std::string &str); [[nodiscard]] std::string Read(); + +private: + DISALLOW_COPY_AND_MOVE(DiskHandler); + + std::string fileName_; }; } // namespace OHOS::NetStack #endif /* COMMUNICATIONNETSTACK_DISK_HANDLER_H */ diff --git a/frameworks/js/napi/http/cache/lru_cache/include/lru_cache.h b/frameworks/js/napi/http/cache/lru_cache/include/lru_cache.h index 6a3cd627f..db5f78b8a 100644 --- a/frameworks/js/napi/http/cache/lru_cache/include/lru_cache.h +++ b/frameworks/js/napi/http/cache/lru_cache/include/lru_cache.h @@ -25,9 +25,26 @@ namespace OHOS::NetStack { class LRUCache { - DISALLOW_COPY_AND_MOVE(LRUCache); +public: + LRUCache(); + + explicit LRUCache(size_t capacity); + + std::unordered_map Get(const std::string &key); + + void Put(const std::string &key, const std::unordered_map &value); + + void MergeOtherCache(const LRUCache &other); + + Json::Value WriteCacheToJsonValue(); + + void ReadCacheFromJsonValue(const Json::Value &root); + + void Clear(); private: + DISALLOW_COPY_AND_MOVE(LRUCache); + struct Node { std::string key; std::unordered_map value; @@ -48,23 +65,6 @@ private: std::list nodeList_; size_t capacity_; size_t size_; - -public: - LRUCache(); - - explicit LRUCache(size_t capacity); - - std::unordered_map Get(const std::string &key); - - void Put(const std::string &key, const std::unordered_map &value); - - void MergeOtherCache(const LRUCache &other); - - Json::Value WriteCacheToJsonValue(); - - void ReadCacheFromJsonValue(const Json::Value &root); - - void Clear(); }; } // namespace OHOS::NetStack #endif /* COMMUNICATIONNETSTACK_LRU_CACHE_H */ diff --git a/frameworks/js/napi/http/cache/lru_cache/include/lru_cache_disk_handler.h b/frameworks/js/napi/http/cache/lru_cache/include/lru_cache_disk_handler.h index 8e052900b..f188cc645 100644 --- a/frameworks/js/napi/http/cache/lru_cache/include/lru_cache_disk_handler.h +++ b/frameworks/js/napi/http/cache/lru_cache/include/lru_cache_disk_handler.h @@ -24,15 +24,6 @@ static constexpr const int MAX_DISK_CACHE_SIZE = 1024 * 1024 * 5; namespace OHOS::NetStack { class LRUCacheDiskHandler { -private: - LRUCache cache_; - DiskHandler diskHandler_; - size_t capacity_; - - Json::Value ReadJsonValueFromFile(); - - void WriteJsonValueToFile(const Json::Value &root); - public: LRUCacheDiskHandler() = delete; @@ -45,6 +36,15 @@ public: std::unordered_map Get(const std::string &key); void Put(const std::string &key, const std::unordered_map &value); + +private: + LRUCache cache_; + DiskHandler diskHandler_; + size_t capacity_; + + Json::Value ReadJsonValueFromFile(); + + void WriteJsonValueToFile(const Json::Value &root); }; } // namespace OHOS::NetStack #endif /* COMMUNICATIONNETSTACK_LRU_CACHE_DISK_HANDLER_H */ diff --git a/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp b/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp index 420c0d879..26c886aa5 100644 --- a/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp +++ b/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp @@ -28,15 +28,6 @@ DiskHandler::DiskHandler(std::string fileName) : fileName_(std::move(fileName)) void DiskHandler::Write(const std::string &str) { - char buffer[500] = {0}; - getcwd(buffer, sizeof(buffer)); - if (strlen(buffer)) { - NETSTACK_LOGI("pwd: %{public}s", buffer); - } else { - NETSTACK_LOGI("pwd: can not get pwd"); - } - - int fd = open(fileName_.c_str(), O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR); if (fd < 0) { NETSTACK_LOGE("errmsg: Write open [%{public}d] %{public}s|\n", errno, strerror(errno)); @@ -80,7 +71,7 @@ std::string DiskHandler::Read() return {}; } - std::unique_ptr mem = std::unique_ptr(new char[buf.st_size]); + auto mem = std::make_unique(buf.st_size); if (read(fd, mem.get(), buf.st_size) < 0) { NETSTACK_LOGE("errmsg: Read read [%{public}d] %{public}s|\n", errno, strerror(errno)); } diff --git a/frameworks/js/napi/http/cache/lru_cache/src/lru_cache.cpp b/frameworks/js/napi/http/cache/lru_cache/src/lru_cache.cpp index 72d6ed4d4..145d0cf8f 100644 --- a/frameworks/js/napi/http/cache/lru_cache/src/lru_cache.cpp +++ b/frameworks/js/napi/http/cache/lru_cache/src/lru_cache.cpp @@ -16,9 +16,10 @@ #include #include -#include "lru_cache.h" #include "netstack_log.h" +#include "lru_cache.h" + static constexpr const char *LRU_INDEX = "LRUIndex"; static constexpr const int DECIMAL_BASE = 10; static constexpr const int MAX_SIZE = 1024 * 1024; diff --git a/frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp b/frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp index 2c3ccd8e7..c096d0d67 100644 --- a/frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp +++ b/frameworks/js/napi/http/cache/lru_cache/src/lru_cache_disk_handler.cpp @@ -13,13 +13,12 @@ * limitations under the License. */ -#include -#include #include -#include "lru_cache_disk_handler.h" #include "netstack_log.h" +#include "lru_cache_disk_handler.h" + static constexpr const int WRITE_INTERVAL = 5 * 1000; namespace OHOS::NetStack { diff --git a/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp b/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp index 1f3066f80..0f5df4d3d 100644 --- a/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp +++ b/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp @@ -27,7 +27,7 @@ std::string CalculateMD5(const std::string &source) std::string str; for (unsigned char i : md5) { char s[HEX_LENGTH + 1] = {0}; - if (sprintf_s(s, HEX_LENGTH + 1, "%02x", i) < 0) { + if (sprintf_s(s, HEX_LENGTH + 1, "%02x", static_cast(i)) < 0) { return {}; } str += s; -- Gitee From 05eefd39f5dd024cf5941b7f96a55a5be0d5d215 Mon Sep 17 00:00:00 2001 From: maosiping Date: Sat, 21 May 2022 15:27:59 +0800 Subject: [PATCH 12/20] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=80=89=E6=8B=A9http?= =?UTF-8?q?=E5=8D=8F=E8=AE=AE=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: maosiping --- .../include/none_param_context.h | 39 ------------- .../async_context/src/none_param_context.cpp | 48 ---------------- .../async_context/src/request_context.cpp | 19 +++---- .../cache/cache_proxy/include/cache_proxy.h | 4 ++ .../cache/cache_proxy/src/cache_proxy.cpp | 14 ++++- .../cache_strategy/include/cache_strategy.h | 4 +- .../cache_strategy/src/cache_strategy.cpp | 18 +++++- .../napi/http/cache/md5/src/calculate_md5.cpp | 2 +- .../js/napi/http/constant/include/constant.h | 7 +-- .../js/napi/http/constant/src/constant.cpp | 5 +- .../js/napi/http/http_exec/src/http_exec.cpp | 4 ++ .../http/http_module/include/http_module.h | 38 ------------- .../options/include/http_request_options.h | 56 ++++++++++++++++--- .../http/options/src/http_request_options.cpp | 32 +++++------ 14 files changed, 113 insertions(+), 177 deletions(-) delete mode 100644 frameworks/js/napi/http/async_context/include/none_param_context.h delete mode 100644 frameworks/js/napi/http/async_context/src/none_param_context.cpp diff --git a/frameworks/js/napi/http/async_context/include/none_param_context.h b/frameworks/js/napi/http/async_context/include/none_param_context.h deleted file mode 100644 index 0a52fd36d..000000000 --- a/frameworks/js/napi/http/async_context/include/none_param_context.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2022 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_NONE_PARAM_CONTEXT_H -#define COMMUNICATIONNETSTACK_NONE_PARAM_CONTEXT_H - -#include "netstack_base_context.h" -#include "nocopyable.h" - -namespace OHOS::NetStack { -class NoneParamContext final : public BaseContext { - DISALLOW_COPY_AND_MOVE(NoneParamContext); - - NoneParamContext() = delete; - - explicit NoneParamContext(napi_env env, EventManager *manager); - - void ParseParams(napi_value *params, size_t paramsCount); - -private: - bool CheckParamsType(napi_value *params, size_t paramsCount); -}; - -using HttpResponseCacheFlushContext = NoneParamContext; -using HttpResponseCacheCloseContext = NoneParamContext; -using HttpResponseCacheDeleteContext = NoneParamContext; -}; // namespace OHOS::NetStack -#endif // COMMUNICATIONNETSTACK_NONE_PARAM_CONTEXT_H diff --git a/frameworks/js/napi/http/async_context/src/none_param_context.cpp b/frameworks/js/napi/http/async_context/src/none_param_context.cpp deleted file mode 100644 index 1ddd52628..000000000 --- a/frameworks/js/napi/http/async_context/src/none_param_context.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2022 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. - */ -#include "none_param_context.h" -#include "netstack_napi_utils.h" - -namespace OHOS::NetStack { -static constexpr const int PARAM_NONE = 0; -static constexpr const int PARAM_JUST_CALLBACK = 1; - -NoneParamContext::NoneParamContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {} - -void NoneParamContext::ParseParams(napi_value *params, size_t paramsCount) -{ - if (!CheckParamsType(params, paramsCount)) { - return; - } - - if (paramsCount == PARAM_JUST_CALLBACK) { - SetParseOK(SetCallback(params[0]) == napi_ok); - return; - } - SetParseOK(true); -} - -bool NoneParamContext::CheckParamsType(napi_value *params, size_t paramsCount) -{ - if (paramsCount == PARAM_NONE) { - return true; - } - - if (paramsCount == PARAM_JUST_CALLBACK) { - return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function; - } - return false; -} -} // namespace OHOS::NetStack 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 e9b9d246b..ea05cbbed 100644 --- a/frameworks/js/napi/http/async_context/src/request_context.cpp +++ b/frameworks/js/napi/http/async_context/src/request_context.cpp @@ -101,16 +101,15 @@ void RequestContext::ParseNumberOptions(napi_value optionsValue) options.SetConnectTimeout(HttpConstant::DEFAULT_CONNECT_TIMEOUT); } - options.SetIfModifiedSince( - NapiUtils::GetUint32Property(GetEnv(), optionsValue, HttpConstant::PARAM_KEY_IF_MODIFIED_SINCE)); - if (options.GetIfModifiedSince() == 0) { - options.SetIfModifiedSince(HttpConstant::DEFAULT_IF_MODIFIED_SINCE); - } - - options.SetFixedLengthStreamingMode( - NapiUtils::GetInt32Property(GetEnv(), optionsValue, HttpConstant::PARAM_KEY_FIXED_LENGTH_STREAMING_MODE)); - if (options.GetFixedLengthStreamingMode() == 0) { - options.SetFixedLengthStreamingMode(HttpConstant::DEFAULT_FIXED_LENGTH_STREAMING_MODE); + if (NapiUtils::HasNamedProperty(GetEnv(), optionsValue, HttpConstant::PARAM_KEY_USING_PROTOCOL)) { + napi_value value = NapiUtils::GetNamedProperty(GetEnv(), optionsValue, HttpConstant::PARAM_KEY_USING_PROTOCOL); + if (NapiUtils::GetValueType(GetEnv(), value) == napi_number) { + uint32_t number = NapiUtils::GetUint32FromValue(GetEnv(), value); + if (number == static_cast(HttpProtocol::HTTP1_1) || + number == static_cast(HttpProtocol::HTTP2)) { + options.SetUsingProtocol(static_cast(number)); + } + } } } diff --git a/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h b/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h index 16ef5fef3..a7bd4dae8 100644 --- a/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h +++ b/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h @@ -38,6 +38,10 @@ public: bool ReadResponseFromCache(HttpResponse &response); void WriteResponseToCache(const HttpResponse &response); + + static void SetRequestTimeForResponse(HttpResponse &response); + + static void SetResponseTimeForResponse(HttpResponse &response); }; } // namespace OHOS::NetStack #endif // COMMUNICATIONNETSTACK_CACHE_PROXY_H diff --git a/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp b/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp index 51620f958..d9d269578 100644 --- a/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp +++ b/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp @@ -20,7 +20,6 @@ #include "constant.h" #include "curl/curl.h" #include "http_exec.h" -#include "http_module.h" #include "lru_cache_disk_handler.h" #include "netstack_common_utils.h" #include "request_context.h" @@ -77,8 +76,7 @@ bool CacheProxy::ReadResponseFromCache(HttpResponse &response) CacheStrategy::SetHeaderForValidation(requestOptions_, cachedResponse); RequestContext context(nullptr, nullptr); HttpExec::ExecRequest(&context); - if (context.response.GetResponseCode() == - static_cast(HttpModuleExports::ResponseCode::NOT_MODIFIED)) { + if (context.response.GetResponseCode() == static_cast(ResponseCode::NOT_MODIFIED)) { response = cachedResponse; } else { response = context.response; @@ -100,4 +98,14 @@ void CacheProxy::WriteResponseToCache(const HttpResponse &response) DISK_LRU_CACHE.Put(MakeKey(), cacheResponse); } + +void CacheProxy::SetRequestTimeForResponse(HttpResponse &response) +{ + response.SetRequestTime(CacheStrategy::GetNowTimeGMT()); +} + +void CacheProxy::SetResponseTimeForResponse(HttpResponse &response) +{ + response.SetResponseTime(CacheStrategy::GetNowTimeGMT()); +} } // namespace OHOS::NetStack \ No newline at end of file diff --git a/frameworks/js/napi/http/cache/cache_strategy/include/cache_strategy.h b/frameworks/js/napi/http/cache/cache_strategy/include/cache_strategy.h index 173afe85f..1f6425961 100644 --- a/frameworks/js/napi/http/cache/cache_strategy/include/cache_strategy.h +++ b/frameworks/js/napi/http/cache/cache_strategy/include/cache_strategy.h @@ -30,7 +30,9 @@ class CacheStrategy final { public: static CacheStatus GetCacheStatus(const HttpRequestOptions &request, const HttpResponse &response); - static void SetHeaderForValidation(HttpRequestOptions &request, HttpResponse &response); + static void SetHeaderForValidation(HttpRequestOptions &request, const HttpResponse &response); + + static std::string GetNowTimeGMT(); }; } // namespace OHOS::NetStack diff --git a/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp b/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp index f9fb2c5d0..a9e7fd1cd 100644 --- a/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp +++ b/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp @@ -13,13 +13,29 @@ * limitations under the License. */ +#include + #include "cache_strategy.h" +static constexpr const int MAX_TIME_LEN = 128; + namespace OHOS::NetStack { CacheStatus CacheStrategy::GetCacheStatus(const HttpRequestOptions &request, const HttpResponse &response) { return CacheStatus::FRESH; } -void CacheStrategy::SetHeaderForValidation(HttpRequestOptions &request, HttpResponse &response) {} +void CacheStrategy::SetHeaderForValidation(HttpRequestOptions &request, const HttpResponse &response) {} + +std::string CacheStrategy::GetNowTimeGMT() +{ + auto now = std::chrono::system_clock::now(); + auto timeSeconds = std::chrono::duration_cast(now.time_since_epoch()).count(); + tm timeInfo = *gmtime(&timeSeconds); + char s[MAX_TIME_LEN] = {0}; + if (strftime(s, sizeof(s), "%a, %d %b %Y %H:%M:%S GMT", &timeInfo) == 0) { + return {}; + } + return s; +} } // namespace OHOS::NetStack \ No newline at end of file diff --git a/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp b/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp index 0f5df4d3d..30a614ab1 100644 --- a/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp +++ b/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp @@ -27,7 +27,7 @@ std::string CalculateMD5(const std::string &source) std::string str; for (unsigned char i : md5) { char s[HEX_LENGTH + 1] = {0}; - if (sprintf_s(s, HEX_LENGTH + 1, "%02x", static_cast(i)) < 0) { + if (sprintf_s(s, HEX_LENGTH + 1, "%02hhx", i) < 0) { return {}; } str += s; diff --git a/frameworks/js/napi/http/constant/include/constant.h b/frameworks/js/napi/http/constant/include/constant.h index 047ed1f7e..8e67a9d4f 100644 --- a/frameworks/js/napi/http/constant/include/constant.h +++ b/frameworks/js/napi/http/constant/include/constant.h @@ -22,9 +22,9 @@ namespace OHOS::NetStack { class HttpConstant final { -public: DISALLOW_COPY_AND_MOVE(HttpConstant); +public: /* Http Method */ static const char *const HTTP_METHOD_GET; static const char *const HTTP_METHOD_HEAD; @@ -38,8 +38,6 @@ public: /* default options */ static const uint32_t DEFAULT_READ_TIMEOUT; static const uint32_t DEFAULT_CONNECT_TIMEOUT; - static const uint32_t DEFAULT_IF_MODIFIED_SINCE; - static const int32_t DEFAULT_FIXED_LENGTH_STREAMING_MODE; /* options key */ static const char *const PARAM_KEY_METHOD; @@ -47,8 +45,7 @@ public: static const char *const PARAM_KEY_HEADER; static const char *const PARAM_KEY_READ_TIMEOUT; static const char *const PARAM_KEY_CONNECT_TIMEOUT; - static const char *const PARAM_KEY_IF_MODIFIED_SINCE; - static const char *const PARAM_KEY_FIXED_LENGTH_STREAMING_MODE; + static const char *const PARAM_KEY_USING_PROTOCOL; static const char *const RESPONSE_KEY_RESULT; static const char *const RESPONSE_KEY_RESPONSE_CODE; diff --git a/frameworks/js/napi/http/constant/src/constant.cpp b/frameworks/js/napi/http/constant/src/constant.cpp index a34b401ac..4af07af90 100644 --- a/frameworks/js/napi/http/constant/src/constant.cpp +++ b/frameworks/js/napi/http/constant/src/constant.cpp @@ -27,16 +27,13 @@ const char *const HttpConstant::HTTP_METHOD_CONNECT = "CONNECT"; const uint32_t HttpConstant::DEFAULT_READ_TIMEOUT = 60000; const uint32_t HttpConstant::DEFAULT_CONNECT_TIMEOUT = 60000; -const uint32_t HttpConstant::DEFAULT_IF_MODIFIED_SINCE = 0; -const int32_t HttpConstant::DEFAULT_FIXED_LENGTH_STREAMING_MODE = -1; const char *const HttpConstant::PARAM_KEY_METHOD = "method"; const char *const HttpConstant::PARAM_KEY_EXTRA_DATA = "extraData"; const char *const HttpConstant::PARAM_KEY_HEADER = "header"; const char *const HttpConstant::PARAM_KEY_READ_TIMEOUT = "readTimeout"; const char *const HttpConstant::PARAM_KEY_CONNECT_TIMEOUT = "connectTimeout"; -const char *const HttpConstant::PARAM_KEY_IF_MODIFIED_SINCE = "ifModifiedSince"; -const char *const HttpConstant::PARAM_KEY_FIXED_LENGTH_STREAMING_MODE = "fixedLengthStreamingMode"; +const char *const HttpConstant::PARAM_KEY_USING_PROTOCOL = "usingProtocol"; const char *const HttpConstant::RESPONSE_KEY_RESULT = "result"; const char *const HttpConstant::RESPONSE_KEY_RESPONSE_CODE = "responseCode"; 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 d2d3933fb..2fae65c13 100644 --- a/frameworks/js/napi/http/http_exec/src/http_exec.cpp +++ b/frameworks/js/napi/http/http_exec/src/http_exec.cpp @@ -98,7 +98,9 @@ bool HttpExec::ExecRequest(RequestContext *context) return false; } + CacheProxy::SetRequestTimeForResponse(context->response); NETSTACK_CURL_EASY_PERFORM(handle.get(), context); + CacheProxy::SetResponseTimeForResponse(context->response); int32_t responseCode; NETSTACK_CURL_EASY_GET_INFO(handle.get(), CURLINFO_RESPONSE_CODE, &responseCode, context); @@ -286,6 +288,8 @@ bool HttpExec::SetOption(CURL *curl, RequestContext *context, struct curl_slist NETSTACK_CURL_EASY_SET_OPTION(curl, CURLOPT_POSTFIELDSIZE, context->options.GetBody().size(), context); } + NETSTACK_CURL_EASY_SET_OPTION(curl, CURLOPT_HTTP_VERSION, context->options.GetHttpVersion(), context); + return true; } diff --git a/frameworks/js/napi/http/http_module/include/http_module.h b/frameworks/js/napi/http/http_module/include/http_module.h index 354c2ad97..ce3e69814 100644 --- a/frameworks/js/napi/http/http_module/include/http_module.h +++ b/frameworks/js/napi/http/http_module/include/http_module.h @@ -36,44 +36,6 @@ public: static napi_value Off(napi_env env, napi_callback_info info); }; - enum class ResponseCode { - OK = 200, - CREATED, - ACCEPTED, - NOT_AUTHORITATIVE, - NO_CONTENT, - RESET, - PARTIAL, - MULT_CHOICE = 300, - MOVED_PERM, - MOVED_TEMP, - SEE_OTHER, - NOT_MODIFIED, - USE_PROXY, - BAD_REQUEST = 400, - UNAUTHORIZED, - PAYMENT_REQUIRED, - FORBIDDEN, - NOT_FOUND, - BAD_METHOD, - NOT_ACCEPTABLE, - PROXY_AUTH, - CLIENT_TIMEOUT, - CONFLICT, - GONE, - LENGTH_REQUIRED, - PRECON_FAILED, - ENTITY_TOO_LARGE, - REQ_TOO_LONG, - UNSUPPORTED_TYPE, - INTERNAL_ERROR = 500, - NOT_IMPLEMENTED, - BAD_GATEWAY, - UNAVAILABLE, - GATEWAY_TIMEOUT, - VERSION, - }; - static constexpr const char *FUNCTION_CREATE_HTTP = "createHttp"; static constexpr const char *INTERFACE_REQUEST_METHOD = "RequestMethod"; static constexpr const char *INTERFACE_RESPONSE_CODE = "ResponseCode"; diff --git a/frameworks/js/napi/http/options/include/http_request_options.h b/frameworks/js/napi/http/options/include/http_request_options.h index f9cfa3a0e..6f6f230e7 100644 --- a/frameworks/js/napi/http/options/include/http_request_options.h +++ b/frameworks/js/napi/http/options/include/http_request_options.h @@ -20,6 +20,50 @@ #include namespace OHOS::NetStack { +enum class HttpProtocol { + HTTP1_1, + HTTP2, + HTTP_NONE, // default choose by curl +}; + +enum class ResponseCode { + OK = 200, + CREATED, + ACCEPTED, + NOT_AUTHORITATIVE, + NO_CONTENT, + RESET, + PARTIAL, + MULT_CHOICE = 300, + MOVED_PERM, + MOVED_TEMP, + SEE_OTHER, + NOT_MODIFIED, + USE_PROXY, + BAD_REQUEST = 400, + UNAUTHORIZED, + PAYMENT_REQUIRED, + FORBIDDEN, + NOT_FOUND, + BAD_METHOD, + NOT_ACCEPTABLE, + PROXY_AUTH, + CLIENT_TIMEOUT, + CONFLICT, + GONE, + LENGTH_REQUIRED, + PRECON_FAILED, + ENTITY_TOO_LARGE, + REQ_TOO_LONG, + UNSUPPORTED_TYPE, + INTERNAL_ERROR = 500, + NOT_IMPLEMENTED, + BAD_GATEWAY, + UNAVAILABLE, + GATEWAY_TIMEOUT, + VERSION, +}; + class HttpRequestOptions final { public: HttpRequestOptions(); @@ -36,9 +80,7 @@ public: void SetConnectTimeout(uint32_t connectTimeout); - void SetIfModifiedSince(uint32_t ifModifiedSince); - - void SetFixedLengthStreamingMode(int32_t fixedLengthStreamingMode); + void SetUsingProtocol(HttpProtocol httpProtocol); [[nodiscard]] const std::string &GetUrl() const; @@ -52,9 +94,7 @@ public: [[nodiscard]] uint32_t GetConnectTimeout() const; - [[nodiscard]] uint32_t GetIfModifiedSince() const; - - [[nodiscard]] int32_t GetFixedLengthStreamingMode() const; + [[nodiscard]] uint32_t GetHttpVersion() const; private: std::string url_; @@ -69,9 +109,7 @@ private: uint32_t connectTimeout_; - uint32_t ifModifiedSince_; - - int32_t fixedLengthStreamingMode_; + HttpProtocol usingProtocol_; }; } // namespace OHOS::NetStack diff --git a/frameworks/js/napi/http/options/src/http_request_options.cpp b/frameworks/js/napi/http/options/src/http_request_options.cpp index 1a747543b..e6e54fc20 100644 --- a/frameworks/js/napi/http/options/src/http_request_options.cpp +++ b/frameworks/js/napi/http/options/src/http_request_options.cpp @@ -13,18 +13,18 @@ * limitations under the License. */ -#include "http_request_options.h" - #include "constant.h" +#include "curl/curl.h" #include "netstack_common_utils.h" +#include "http_request_options.h" + namespace OHOS::NetStack { HttpRequestOptions::HttpRequestOptions() : method_(HttpConstant::HTTP_METHOD_GET), readTimeout_(HttpConstant::DEFAULT_READ_TIMEOUT), connectTimeout_(HttpConstant::DEFAULT_CONNECT_TIMEOUT), - ifModifiedSince_(HttpConstant::DEFAULT_IF_MODIFIED_SINCE), - fixedLengthStreamingMode_(HttpConstant::DEFAULT_FIXED_LENGTH_STREAMING_MODE) + usingProtocol_(HttpProtocol::HTTP_NONE) { header_[CommonUtils::ToLower(HttpConstant::HTTP_CONTENT_TYPE)] = HttpConstant::HTTP_CONTENT_TYPE_JSON; // default } @@ -59,16 +59,6 @@ void HttpRequestOptions::SetConnectTimeout(uint32_t connectTimeout) connectTimeout_ = connectTimeout; } -void HttpRequestOptions::SetIfModifiedSince(uint32_t ifModifiedSince) -{ - ifModifiedSince_ = ifModifiedSince; -} - -void HttpRequestOptions::SetFixedLengthStreamingMode(int32_t fixedLengthStreamingMode) -{ - fixedLengthStreamingMode_ = fixedLengthStreamingMode; -} - const std::string &HttpRequestOptions::GetUrl() const { return url_; @@ -99,13 +89,19 @@ uint32_t HttpRequestOptions::GetConnectTimeout() const return connectTimeout_; } -uint32_t HttpRequestOptions::GetIfModifiedSince() const +void HttpRequestOptions::SetUsingProtocol(HttpProtocol httpProtocol) { - return ifModifiedSince_; + usingProtocol_ = httpProtocol; } -int32_t HttpRequestOptions::GetFixedLengthStreamingMode() const +uint32_t HttpRequestOptions::GetHttpVersion() const { - return fixedLengthStreamingMode_; + if (usingProtocol_ == HttpProtocol::HTTP2) { + return CURL_HTTP_VERSION_2_0; + } + if (usingProtocol_ == HttpProtocol::HTTP1_1) { + return CURL_HTTP_VERSION_1_1; + } + return CURL_HTTP_VERSION_NONE; } } // namespace OHOS::NetStack \ No newline at end of file -- Gitee From 30cedc5326584fc7d382aea4acc476ae984fdd33 Mon Sep 17 00:00:00 2001 From: maosiping Date: Sat, 21 May 2022 15:38:32 +0800 Subject: [PATCH 13/20] =?UTF-8?q?=E5=AE=9A=E4=B9=89http=20protocol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: maosiping --- .../napi/http/http_module/include/http_module.h | 3 +++ .../js/napi/http/http_module/src/http_module.cpp | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/frameworks/js/napi/http/http_module/include/http_module.h b/frameworks/js/napi/http/http_module/include/http_module.h index ce3e69814..26e452768 100644 --- a/frameworks/js/napi/http/http_module/include/http_module.h +++ b/frameworks/js/napi/http/http_module/include/http_module.h @@ -40,6 +40,7 @@ public: static constexpr const char *INTERFACE_REQUEST_METHOD = "RequestMethod"; static constexpr const char *INTERFACE_RESPONSE_CODE = "ResponseCode"; static constexpr const char *INTERFACE_HTTP_REQUEST = "HttpRequest"; + static constexpr const char *INTERFACE_HTTP_PROTOCOL = "HttpProtocol"; static napi_value InitHttpModule(napi_env env, napi_value exports); @@ -53,6 +54,8 @@ private: static void InitRequestMethod(napi_env env, napi_value exports); static void InitResponseCode(napi_env env, napi_value exports); + + static void InitHttpProtocol(napi_env env, napi_value exports); }; } // namespace OHOS::NetStack #endif // COMMUNICATIONNETSTACK_HTTP_MODULE_H \ No newline at end of file diff --git a/frameworks/js/napi/http/http_module/src/http_module.cpp b/frameworks/js/napi/http/http_module/src/http_module.cpp index 5c5039058..34bc4ec52 100644 --- a/frameworks/js/napi/http/http_module/src/http_module.cpp +++ b/frameworks/js/napi/http/http_module/src/http_module.cpp @@ -28,6 +28,9 @@ #define DECLARE_REQUEST_METHOD(method) \ DECLARE_NAPI_STATIC_PROPERTY(HttpConstant::method, NapiUtils::CreateStringUtf8(env, HttpConstant::method)) +#define DECLARE_HTTP_PROTOCOL(protocol) \ + DECLARE_NAPI_STATIC_PROPERTY(#protocol, NapiUtils::CreateUint32(env, static_cast(HttpProtocol::protocol))) + static constexpr const char *REQUEST_ASYNC_WORK_NAME = "ExecRequest"; static constexpr const char *HTTP_MODULE_NAME = "net.http"; @@ -133,6 +136,19 @@ void HttpModuleExports::InitResponseCode(napi_env env, napi_value exports) NapiUtils::SetNamedProperty(env, exports, INTERFACE_RESPONSE_CODE, responseCode); } +void HttpModuleExports::InitHttpProtocol(napi_env env, napi_value exports) +{ + std::initializer_list properties = { + DECLARE_HTTP_PROTOCOL(HTTP1_1), + DECLARE_HTTP_PROTOCOL(HTTP2), + }; + + napi_value httpProtocol = NapiUtils::CreateObject(env); + NapiUtils::DefineProperties(env, httpProtocol, properties); + + NapiUtils::SetNamedProperty(env, exports, INTERFACE_HTTP_PROTOCOL, httpProtocol); +} + napi_value HttpModuleExports::HttpRequest::Request(napi_env env, napi_callback_info info) { return ModuleTemplate::Interface( -- Gitee From f9978d458981736ea15461e0a789fbf29f905d52 Mon Sep 17 00:00:00 2001 From: maosiping Date: Sat, 21 May 2022 15:41:50 +0800 Subject: [PATCH 14/20] =?UTF-8?q?gmtime=E5=87=BD=E6=95=B0=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E4=B8=BAtime=5Ft=20*=EF=BC=8C=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=85=A5=E5=8F=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: maosiping --- .../js/napi/http/cache/cache_strategy/src/cache_strategy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp b/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp index a9e7fd1cd..8f024ece5 100644 --- a/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp +++ b/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp @@ -30,7 +30,7 @@ void CacheStrategy::SetHeaderForValidation(HttpRequestOptions &request, const Ht std::string CacheStrategy::GetNowTimeGMT() { auto now = std::chrono::system_clock::now(); - auto timeSeconds = std::chrono::duration_cast(now.time_since_epoch()).count(); + time_t timeSeconds = std::chrono::duration_cast(now.time_since_epoch()).count(); tm timeInfo = *gmtime(&timeSeconds); char s[MAX_TIME_LEN] = {0}; if (strftime(s, sizeof(s), "%a, %d %b %Y %H:%M:%S GMT", &timeInfo) == 0) { -- Gitee From 831701dc90ee603cc5fd7fd747ac2711a02bf2ad Mon Sep 17 00:00:00 2001 From: maosiping Date: Sat, 21 May 2022 15:50:19 +0800 Subject: [PATCH 15/20] =?UTF-8?q?=E4=BD=BF=E7=94=A8gmtime=5Fr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: maosiping --- .../js/napi/http/cache/cache_strategy/src/cache_strategy.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp b/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp index 8f024ece5..a14e42644 100644 --- a/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp +++ b/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp @@ -31,7 +31,10 @@ std::string CacheStrategy::GetNowTimeGMT() { auto now = std::chrono::system_clock::now(); time_t timeSeconds = std::chrono::duration_cast(now.time_since_epoch()).count(); - tm timeInfo = *gmtime(&timeSeconds); + tm timeInfo = {0}; + if (gmtime_r(&timeSeconds, &timeInfo) == nullptr) { + return {}; + } char s[MAX_TIME_LEN] = {0}; if (strftime(s, sizeof(s), "%a, %d %b %Y %H:%M:%S GMT", &timeInfo) == 0) { return {}; -- Gitee From 402c2b116cc5cdc2e1e30fbc2cc9102bd2fd44cf Mon Sep 17 00:00:00 2001 From: maosiping Date: Mon, 23 May 2022 11:53:21 +0800 Subject: [PATCH 16/20] =?UTF-8?q?=E4=BD=BF=E7=94=A8hhx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: maosiping --- frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp b/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp index 30a614ab1..4cd418a86 100644 --- a/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp +++ b/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp @@ -27,7 +27,7 @@ std::string CalculateMD5(const std::string &source) std::string str; for (unsigned char i : md5) { char s[HEX_LENGTH + 1] = {0}; - if (sprintf_s(s, HEX_LENGTH + 1, "%02hhx", i) < 0) { + if (sprintf_s(s, HEX_LENGTH + 1, "%hhx", i) < 0) { return {}; } str += s; -- Gitee From abe66af4396d35e229d20caa367a3868ce71d60c Mon Sep 17 00:00:00 2001 From: maosiping Date: Mon, 23 May 2022 14:51:18 +0800 Subject: [PATCH 17/20] for codecheck Signed-off-by: maosiping --- frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp b/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp index 4cd418a86..119e1caff 100644 --- a/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp +++ b/frameworks/js/napi/http/cache/md5/src/calculate_md5.cpp @@ -27,7 +27,8 @@ std::string CalculateMD5(const std::string &source) std::string str; for (unsigned char i : md5) { char s[HEX_LENGTH + 1] = {0}; - if (sprintf_s(s, HEX_LENGTH + 1, "%hhx", i) < 0) { + int d = i; + if (sprintf_s(s, HEX_LENGTH + 1, "%02x", d) < 0) { return {}; } str += s; -- Gitee From d1cab26f84cefc43695d2792033318e37a5faec4 Mon Sep 17 00:00:00 2001 From: maosiping Date: Mon, 23 May 2022 19:38:52 +0800 Subject: [PATCH 18/20] for codecheck Signed-off-by: maosiping --- frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp b/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp index 26c886aa5..8bbc76d58 100644 --- a/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp +++ b/frameworks/js/napi/http/cache/lru_cache/src/disk_handler.cpp @@ -71,7 +71,7 @@ std::string DiskHandler::Read() return {}; } - auto mem = std::make_unique(buf.st_size); + std::unique_ptr mem(new char[buf.st_size]); if (read(fd, mem.get(), buf.st_size) < 0) { NETSTACK_LOGE("errmsg: Read read [%{public}d] %{public}s|\n", errno, strerror(errno)); } -- Gitee From da319bce3cff6fabc6598d88522043a42301cf19 Mon Sep 17 00:00:00 2001 From: maosiping Date: Mon, 23 May 2022 21:59:17 +0800 Subject: [PATCH 19/20] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: maosiping --- .../cache/cache_proxy/include/cache_proxy.h | 4 +-- .../cache/cache_proxy/src/cache_proxy.cpp | 32 ++++++++++++------- .../cache_strategy/include/cache_strategy.h | 14 ++++++-- .../cache_strategy/src/cache_strategy.cpp | 21 ++++++++++-- .../js/napi/http/http_exec/src/http_exec.cpp | 6 +++- .../napi/http/http_module/src/http_module.cpp | 1 + .../http/options/src/http_request_options.cpp | 4 +++ 7 files changed, 63 insertions(+), 19 deletions(-) diff --git a/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h b/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h index a7bd4dae8..ab1bad4b5 100644 --- a/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h +++ b/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h @@ -26,14 +26,14 @@ class CacheProxy final { HttpRequestOptions &requestOptions_; - bool CouldUseCache(); + CacheStrategy strategy_; std::string MakeKey(); public: CacheProxy() = delete; - CacheProxy(HttpRequestOptions &requestOptions); + explicit CacheProxy(HttpRequestOptions &requestOptions); bool ReadResponseFromCache(HttpResponse &response); diff --git a/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp b/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp index d9d269578..db87e6b6c 100644 --- a/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp +++ b/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp @@ -13,30 +13,25 @@ * limitations under the License. */ -#include "cache_proxy.h" #include "base64_utils.h" #include "cache_strategy.h" #include "calculate_md5.h" #include "constant.h" -#include "curl/curl.h" #include "http_exec.h" #include "lru_cache_disk_handler.h" #include "netstack_common_utils.h" +#include "netstack_log.h" #include "request_context.h" -static constexpr const char *KEY_RANGE = "range"; +#include "cache_proxy.h" + static constexpr const char *CACHE_FILE = "/data/storage/el2/base/cache.json"; namespace OHOS::NetStack { static LRUCacheDiskHandler DISK_LRU_CACHE(CACHE_FILE, MAX_DISK_CACHE_SIZE); // NOLINT(cert-err58-cpp) -CacheProxy::CacheProxy(HttpRequestOptions &requestOptions) : requestOptions_(requestOptions) {} - -bool CacheProxy::CouldUseCache() +CacheProxy::CacheProxy(HttpRequestOptions &requestOptions) : requestOptions_(requestOptions), strategy_(requestOptions) { - return requestOptions_.GetMethod() == HttpConstant::HTTP_METHOD_GET || - requestOptions_.GetMethod() == HttpConstant::HTTP_METHOD_HEAD || - requestOptions_.GetHeader().find(KEY_RANGE) != requestOptions_.GetHeader().end(); } std::string CacheProxy::MakeKey() @@ -46,17 +41,20 @@ std::string CacheProxy::MakeKey() for (const auto &p : requestOptions_.GetHeader()) { str += p.first + HttpConstant::HTTP_HEADER_SEPARATOR + p.second + HttpConstant::HTTP_LINE_SEPARATOR; } + str += std::to_string(requestOptions_.GetHttpVersion()); return CalculateMD5(str); } bool CacheProxy::ReadResponseFromCache(HttpResponse &response) { - if (!CouldUseCache()) { + if (!strategy_.CouldUseCache()) { + NETSTACK_LOGI("only GET/HEAD method or header has [Range] can use cache"); return false; } auto responseFromCache = DISK_LRU_CACHE.Get(MakeKey()); if (responseFromCache.empty()) { + NETSTACK_LOGI("no cache with this request"); return false; } HttpResponse cachedResponse; @@ -65,30 +63,40 @@ bool CacheProxy::ReadResponseFromCache(HttpResponse &response) cachedResponse.SetCookies(Base64::Decode(responseFromCache[HttpConstant::RESPONSE_KEY_COOKIES])); cachedResponse.SetRequestTime(Base64::Decode(responseFromCache[HttpConstant::REQUEST_TIME])); cachedResponse.SetResponseTime(Base64::Decode(responseFromCache[HttpConstant::RESPONSE_TIME])); + cachedResponse.SetResponseCode(static_cast(ResponseCode::OK)); cachedResponse.ParseHeaders(); - CacheStatus status = CacheStrategy::GetCacheStatus(requestOptions_, cachedResponse); + CacheStatus status = strategy_.GetCacheStatus(cachedResponse); if (status == CacheStatus::FRESH) { response = cachedResponse; + NETSTACK_LOGI("cache is FRESH"); return true; } if (status == CacheStatus::STATE) { - CacheStrategy::SetHeaderForValidation(requestOptions_, cachedResponse); + NETSTACK_LOGI("cache is STATE, we try to talk to the server"); + strategy_.SetHeaderForValidation(cachedResponse); RequestContext context(nullptr, nullptr); HttpExec::ExecRequest(&context); if (context.response.GetResponseCode() == static_cast(ResponseCode::NOT_MODIFIED)) { + NETSTACK_LOGI("cache is NOT_MODIFIED, we use the cache"); response = cachedResponse; } else { + NETSTACK_LOGI("cache is MODIFIED, server return the newer one"); response = context.response; } WriteResponseToCache(response); return true; } + NETSTACK_LOGI("cache should not be used"); return false; } void CacheProxy::WriteResponseToCache(const HttpResponse &response) { + if (!strategy_.CouldCache(response)) { + NETSTACK_LOGI("do not cache this response"); + return; + } std::unordered_map cacheResponse; cacheResponse[HttpConstant::RESPONSE_KEY_HEADER] = Base64::Encode(response.GetRawHeader()); cacheResponse[HttpConstant::RESPONSE_KEY_RESULT] = Base64::Encode(response.GetResult()); diff --git a/frameworks/js/napi/http/cache/cache_strategy/include/cache_strategy.h b/frameworks/js/napi/http/cache/cache_strategy/include/cache_strategy.h index 1f6425961..082098f34 100644 --- a/frameworks/js/napi/http/cache/cache_strategy/include/cache_strategy.h +++ b/frameworks/js/napi/http/cache/cache_strategy/include/cache_strategy.h @@ -27,10 +27,20 @@ enum class CacheStatus { }; class CacheStrategy final { + HttpRequestOptions &requestOptions_; + public: - static CacheStatus GetCacheStatus(const HttpRequestOptions &request, const HttpResponse &response); + CacheStrategy() = delete; + + explicit CacheStrategy(HttpRequestOptions &requestOptions); + + CacheStatus GetCacheStatus(const HttpResponse &response); + + void SetHeaderForValidation(const HttpResponse &response); + + bool CouldCache(const HttpResponse &response); - static void SetHeaderForValidation(HttpRequestOptions &request, const HttpResponse &response); + bool CouldUseCache(); static std::string GetNowTimeGMT(); }; diff --git a/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp b/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp index a14e42644..ba1c67242 100644 --- a/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp +++ b/frameworks/js/napi/http/cache/cache_strategy/src/cache_strategy.cpp @@ -15,17 +15,34 @@ #include +#include "constant.h" + #include "cache_strategy.h" static constexpr const int MAX_TIME_LEN = 128; +static constexpr const char *KEY_RANGE = "range"; namespace OHOS::NetStack { -CacheStatus CacheStrategy::GetCacheStatus(const HttpRequestOptions &request, const HttpResponse &response) +CacheStrategy::CacheStrategy(HttpRequestOptions &requestOptions) : requestOptions_(requestOptions) {} + +CacheStatus CacheStrategy::GetCacheStatus(const HttpResponse &response) { return CacheStatus::FRESH; } -void CacheStrategy::SetHeaderForValidation(HttpRequestOptions &request, const HttpResponse &response) {} +void CacheStrategy::SetHeaderForValidation(const HttpResponse &response) {} + +bool CacheStrategy::CouldUseCache() +{ + return requestOptions_.GetMethod() == HttpConstant::HTTP_METHOD_GET || + requestOptions_.GetMethod() == HttpConstant::HTTP_METHOD_HEAD || + requestOptions_.GetHeader().find(KEY_RANGE) != requestOptions_.GetHeader().end(); +} + +bool CacheStrategy::CouldCache(const HttpResponse &response) +{ + return true; +} std::string CacheStrategy::GetNowTimeGMT() { 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 2fae65c13..e437723bf 100644 --- a/frameworks/js/napi/http/http_exec/src/http_exec.cpp +++ b/frameworks/js/napi/http/http_exec/src/http_exec.cpp @@ -104,6 +104,7 @@ bool HttpExec::ExecRequest(RequestContext *context) int32_t responseCode; NETSTACK_CURL_EASY_GET_INFO(handle.get(), CURLINFO_RESPONSE_CODE, &responseCode, context); + NETSTACK_LOGI("responseCode is %{public}d", responseCode); struct curl_slist *cookies = nullptr; NETSTACK_CURL_EASY_GET_INFO(handle.get(), CURLINFO_COOKIELIST, &cookies, context); @@ -119,7 +120,10 @@ bool HttpExec::ExecRequest(RequestContext *context) context->response.SetResponseCode(responseCode); context->response.ParseHeaders(); - proxy.WriteResponseToCache(context->response); + if (context->response.GetResponseCode() == static_cast(ResponseCode::OK)) { + NETSTACK_LOGI("response code is 200, we write it to cache"); + proxy.WriteResponseToCache(context->response); + } return true; } diff --git a/frameworks/js/napi/http/http_module/src/http_module.cpp b/frameworks/js/napi/http/http_module/src/http_module.cpp index 34bc4ec52..c963de43a 100644 --- a/frameworks/js/napi/http/http_module/src/http_module.cpp +++ b/frameworks/js/napi/http/http_module/src/http_module.cpp @@ -73,6 +73,7 @@ void HttpModuleExports::InitHttpProperties(napi_env env, napi_value exports) InitRequestMethod(env, exports); InitResponseCode(env, exports); + InitHttpProtocol(env, exports); } void HttpModuleExports::InitRequestMethod(napi_env env, napi_value exports) diff --git a/frameworks/js/napi/http/options/src/http_request_options.cpp b/frameworks/js/napi/http/options/src/http_request_options.cpp index e6e54fc20..54b2bdf81 100644 --- a/frameworks/js/napi/http/options/src/http_request_options.cpp +++ b/frameworks/js/napi/http/options/src/http_request_options.cpp @@ -16,6 +16,7 @@ #include "constant.h" #include "curl/curl.h" #include "netstack_common_utils.h" +#include "netstack_log.h" #include "http_request_options.h" @@ -97,11 +98,14 @@ void HttpRequestOptions::SetUsingProtocol(HttpProtocol httpProtocol) uint32_t HttpRequestOptions::GetHttpVersion() const { if (usingProtocol_ == HttpProtocol::HTTP2) { + NETSTACK_LOGI("CURL_HTTP_VERSION_2_0"); return CURL_HTTP_VERSION_2_0; } if (usingProtocol_ == HttpProtocol::HTTP1_1) { + NETSTACK_LOGI("CURL_HTTP_VERSION_1_1"); return CURL_HTTP_VERSION_1_1; } + NETSTACK_LOGI("CURL_HTTP_VERSION_NONE"); return CURL_HTTP_VERSION_NONE; } } // namespace OHOS::NetStack \ No newline at end of file -- Gitee From a2055e8bfc10e481e06c427d8cc4cf409ab94062 Mon Sep 17 00:00:00 2001 From: maosiping Date: Tue, 24 May 2022 10:03:56 +0800 Subject: [PATCH 20/20] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: maosiping --- frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h | 1 + frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h b/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h index ab1bad4b5..3b1a02ede 100644 --- a/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h +++ b/frameworks/js/napi/http/cache/cache_proxy/include/cache_proxy.h @@ -16,6 +16,7 @@ #ifndef COMMUNICATIONNETSTACK_CACHE_PROXY_H #define COMMUNICATIONNETSTACK_CACHE_PROXY_H +#include "cache_strategy.h" #include "http_request_options.h" #include "http_response.h" #include "nocopyable.h" diff --git a/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp b/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp index db87e6b6c..2a2840132 100644 --- a/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp +++ b/frameworks/js/napi/http/cache/cache_proxy/src/cache_proxy.cpp @@ -14,7 +14,6 @@ */ #include "base64_utils.h" -#include "cache_strategy.h" #include "calculate_md5.h" #include "constant.h" #include "http_exec.h" -- Gitee