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 143 bool IsRootCaVerified() const; 144 145 void SetRootCaVerified(); 146 147 bool IsRootCaVerifiedOk() const; 148 149 void SetRootCaVerifiedOk(bool ok); 150 151 void SetPinnedPubkey(std::string &pubkey); 152 153 std::string GetPinnedPubkey() const; 154 private: 155 int32_t taskId_ = -1; 156 bool usingCache_ = true; 157 bool requestInStream_ = false; 158 std::mutex dlLenLock_; 159 std::mutex ulLenLock_; 160 std::mutex tempDataLock_; 161 std::queue<std::string> tempData_; 162 HttpResponse cacheResponse_; 163 std::queue<LoadBytes> dlBytes_; 164 std::queue<LoadBytes> ulBytes_; 165 curl_slist *curlHeaderList_ = nullptr; 166 Timing::TimerMap timerMap_; 167 std::map<std::string, double> performanceTimingMap_; 168 curl_mime *multipart_ = nullptr; 169 CertsPath certsPath_; 170 uint64_t moduleId_ = 0; 171 curl_slist *curlHostList_ = nullptr; 172 bool isAtomicService_ = false; 173 std::string bundleName_; 174 std::string traceName_; 175 bool isRootCaVerified_ = false; 176 bool isRootCaVerifiedOk_ = false; 177 std::string pinnedPubkey_; 178 #if HAS_NETMANAGER_BASE 179 std::unique_ptr<NetworkProfilerUtils> networkProfilerUtils_; 180 #endif 181 CURL *curlHandle_; 182 183 bool CheckParamsType(napi_value *params, size_t paramsCount); 184 185 void ParseNumberOptions(napi_value optionsValue); 186 187 void ParseHeader(napi_value optionsValue); 188 189 bool ParseExtraData(napi_value optionsValue); 190 191 void ParseUsingHttpProxy(napi_value optionsValue); 192 193 void ParseCaPath(napi_value optionsValue); 194 195 void ParseDnsServers(napi_value optionsValue); 196 197 void ParseMultiFormData(napi_value optionsValue); 198 199 void ParseDohUrl(napi_value optionsValue); 200 201 void ParseResumeFromToNumber(napi_value optionsValue); 202 203 void ParseCertificatePinning(napi_value optionsValue); 204 205 bool GetRequestBody(napi_value extraData); 206 207 void UrlAndOptions(napi_value urlValue, napi_value optionsValue); 208 209 bool HandleMethodForGet(napi_value extraData); 210 211 MultiFormData NapiValue2FormData(napi_value formDataValue); 212 213 CertificatePinning NapiValue2CertPinning(napi_value certPIN); 214 215 void SaveFormData(napi_env env, napi_value dataValue, MultiFormData &multiFormData); 216 217 void ParseAddressFamily(napi_value optionsValue); 218 }; 219 } // namespace OHOS::NetStack::Http 220 221 #endif /* COMMUNICATIONNETSTACK_REQUEST_CONTEXT_H */ 222