1 /* 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef COMMUNICATIONNETSTACK_REQUEST_CONTEXT_H 17 #define COMMUNICATIONNETSTACK_REQUEST_CONTEXT_H 18 19 #include <queue> 20 #include <mutex> 21 #include <map> 22 #include "curl/curl.h" 23 #include "base_context.h" 24 #include "http_request_options.h" 25 #include "http_response.h" 26 #include "timing.h" 27 #if HAS_NETMANAGER_BASE 28 #include "netstack_network_profiler.h" 29 #endif 30 31 namespace OHOS::NetStack::Http { 32 struct LoadBytes { LoadBytesLoadBytes33 LoadBytes() : nLen(0), tLen(0){}; LoadBytesLoadBytes34 LoadBytes(curl_off_t nowLen, curl_off_t totalLen) 35 { 36 nLen = nowLen; 37 tLen = totalLen; 38 }; 39 ~LoadBytes() = default; 40 curl_off_t nLen; 41 curl_off_t tLen; 42 }; 43 44 struct CertsPath { 45 CertsPath() = default; 46 ~CertsPath() = default; 47 std::vector<std::string> certPathList; 48 std::string certFile; 49 }; 50 51 class RequestContext final : public BaseContext { 52 public: 53 friend class HttpExec; 54 55 RequestContext() = delete; 56 57 RequestContext(napi_env env, EventManager *manager); 58 59 ~RequestContext() override; 60 61 void StartTiming(); 62 63 void ParseParams(napi_value *params, size_t paramsCount) override; 64 65 HttpRequestOptions options; 66 67 HttpResponse response; 68 69 [[nodiscard]] bool IsUsingCache() const; 70 71 void SetCurlHeaderList(curl_slist *curlHeaderList); 72 73 curl_slist *GetCurlHeaderList(); 74 75 void SetCacheResponse(const HttpResponse &cacheResponse); 76 77 void SetResponseByCache(); 78 79 [[nodiscard]] int32_t GetErrorCode() const override; 80 81 [[nodiscard]] std::string GetErrorMessage() const override; 82 83 void EnableRequestInStream(); 84 85 [[nodiscard]] bool IsRequestInStream() const; 86 87 void SetDlLen(curl_off_t nowLen, curl_off_t totalLen); 88 89 LoadBytes GetDlLen(); 90 91 void SetUlLen(curl_off_t nowLen, curl_off_t totalLen); 92 93 LoadBytes GetUlLen(); 94 95 bool CompareWithLastElement(curl_off_t nowLen, curl_off_t totalLen); 96 97 void SetTempData(const void *data, size_t size); 98 99 std::string GetTempData(); 100 101 void PopTempData(); 102 103 void ParseClientCert(napi_value optionsValue); 104 105 void CachePerformanceTimingItem(const std::string &key, double value); 106 107 void StopAndCacheNapiPerformanceTiming(const char *key); 108 109 void SetPerformanceTimingToResult(napi_value result); 110 111 void SetMultipart(curl_mime *multipart); 112 113 void SetCertsPath(std::vector<std::string> &&certPathList, const std::string &certFile); 114 115 const CertsPath &GetCertsPath(); 116 117 [[nodiscard]] int32_t GetTaskId() const; 118 119 void SetModuleId(uint64_t moduleId); 120 121 uint64_t GetModuleId() const; 122 123 void SetCurlHostList(curl_slist *curlHostList); 124 125 [[nodiscard]] curl_slist *GetCurlHostList(); 126 127 void SetAtomicService(bool isAtomicService); 128 129 [[nodiscard]] bool IsAtomicService() const; 130 131 void SetBundleName(const std::string &bundleName); 132 133 [[nodiscard]] std::string GetBundleName() const; 134 135 void SetTraceName(const std::string &traceName); 136 137 [[nodiscard]] std::string GetTraceName() const; 138 139 void SetCurlHandle(CURL *handle); 140 141 void SendNetworkProfiler(); 142 private: 143 int32_t taskId_ = -1; 144 bool usingCache_ = true; 145 bool requestInStream_ = false; 146 std::mutex dlLenLock_; 147 std::mutex ulLenLock_; 148 std::mutex tempDataLock_; 149 std::queue<std::string> tempData_; 150 HttpResponse cacheResponse_; 151 std::queue<LoadBytes> dlBytes_; 152 std::queue<LoadBytes> ulBytes_; 153 curl_slist *curlHeaderList_ = nullptr; 154 Timing::TimerMap timerMap_; 155 std::map<std::string, double> performanceTimingMap_; 156 curl_mime *multipart_ = nullptr; 157 CertsPath certsPath_; 158 uint64_t moduleId_ = 0; 159 curl_slist *curlHostList_ = nullptr; 160 bool isAtomicService_ = false; 161 std::string bundleName_; 162 std::string traceName_; 163 #if HAS_NETMANAGER_BASE 164 std::unique_ptr<NetworkProfilerUtils> networkProfilerUtils_; 165 #endif 166 CURL *curlHandle_; 167 168 bool CheckParamsType(napi_value *params, size_t paramsCount); 169 170 void ParseNumberOptions(napi_value optionsValue); 171 172 void ParseHeader(napi_value optionsValue); 173 174 bool ParseExtraData(napi_value optionsValue); 175 176 void ParseUsingHttpProxy(napi_value optionsValue); 177 178 void ParseCaPath(napi_value optionsValue); 179 180 void ParseDnsServers(napi_value optionsValue); 181 182 void ParseMultiFormData(napi_value optionsValue); 183 184 void ParseDohUrl(napi_value optionsValue); 185 186 void ParseResumeFromToNumber(napi_value optionsValue); 187 188 void ParseCertificatePinning(napi_value optionsValue); 189 190 bool GetRequestBody(napi_value extraData); 191 192 void UrlAndOptions(napi_value urlValue, napi_value optionsValue); 193 194 bool HandleMethodForGet(napi_value extraData); 195 196 MultiFormData NapiValue2FormData(napi_value formDataValue); 197 198 CertificatePinning NapiValue2CertPinning(napi_value certPIN); 199 200 void SaveFormData(napi_env env, napi_value dataValue, MultiFormData &multiFormData); 201 }; 202 } // namespace OHOS::NetStack::Http 203 204 #endif /* COMMUNICATIONNETSTACK_REQUEST_CONTEXT_H */ 205