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 #include "request_tracer.h" 31 32 namespace OHOS::NetStack::Http { 33 static constexpr const uint32_t MAGIC_NUMBER = 0x86161616; 34 struct LoadBytes { LoadBytesLoadBytes35 LoadBytes() : nLen(0), tLen(0){}; LoadBytesLoadBytes36 LoadBytes(curl_off_t nowLen, curl_off_t totalLen) 37 { 38 nLen = nowLen; 39 tLen = totalLen; 40 }; 41 ~LoadBytes() = default; 42 curl_off_t nLen; 43 curl_off_t tLen; 44 }; 45 46 struct CertsPath { 47 CertsPath() = default; 48 ~CertsPath() = default; 49 std::vector<std::string> certPathList; 50 std::string certFile; 51 }; 52 53 #ifdef HTTP_HANDOVER_FEATURE 54 struct RequestHandoverInfo { 55 RequestHandoverInfo() = default; 56 ~RequestHandoverInfo() = default; 57 int32_t handoverNum = 0; 58 int32_t handoverReason = 0; 59 double flowControlTime = 0; 60 int32_t readFlag = 0; 61 }; 62 #endif 63 64 class RequestContext final : public BaseContext { 65 public: 66 friend class HttpExec; 67 68 RequestContext() = delete; 69 70 RequestContext(napi_env env, const std::shared_ptr<EventManager> &manager); 71 72 ~RequestContext() override; 73 74 void StartTiming(); 75 76 void ParseParams(napi_value *params, size_t paramsCount) override; 77 78 HttpRequestOptions options; 79 80 HttpResponse response; 81 82 [[nodiscard]] bool IsUsingCache() const; 83 84 void SetCurlHeaderList(curl_slist *curlHeaderList); 85 86 curl_slist *GetCurlHeaderList(); 87 88 void SetCacheResponse(const HttpResponse &cacheResponse); 89 90 void SetResponseByCache(); 91 92 [[nodiscard]] int32_t GetErrorCode() const override; 93 94 [[nodiscard]] std::string GetErrorMessage() const override; 95 96 void EnableRequestInStream(); 97 98 [[nodiscard]] bool IsRequestInStream() const; 99 100 void SetDlLen(curl_off_t nowLen, curl_off_t totalLen); 101 102 LoadBytes GetDlLen(); 103 104 void SetUlLen(curl_off_t nowLen, curl_off_t totalLen); 105 106 LoadBytes GetUlLen(); 107 108 bool CompareWithLastElement(curl_off_t nowLen, curl_off_t totalLen); 109 110 void SetTempData(const void *data, size_t size); 111 112 std::string GetTempData(); 113 114 void PopTempData(); 115 116 void ParseClientCert(napi_value optionsValue); 117 118 void ParseRemoteValidationMode(napi_value optionsValue); 119 120 void ParseTlsOption(napi_value optionsValue); 121 122 void ParseServerAuthentication(napi_value optionsValue); 123 124 void CachePerformanceTimingItem(const std::string &key, double value); 125 126 void StopAndCacheNapiPerformanceTiming(const char *key); 127 128 void SetPerformanceTimingToResult(napi_value result); 129 130 void SetMultipart(curl_mime *multipart); 131 132 void SetCertsPath(std::vector<std::string> &&certPathList, const std::string &certFile); 133 134 const CertsPath &GetCertsPath(); 135 136 [[nodiscard]] int32_t GetTaskId() const; 137 138 void SetModuleId(uint64_t moduleId); 139 140 uint64_t GetModuleId() const; 141 142 void SetCurlHostList(curl_slist *curlHostList); 143 144 [[nodiscard]] curl_slist *GetCurlHostList(); 145 146 void SetAtomicService(bool isAtomicService); 147 148 [[nodiscard]] bool IsAtomicService() const; 149 150 void SetBundleName(const std::string &bundleName); 151 152 [[nodiscard]] std::string GetBundleName() const; 153 154 void SetCurlHandle(CURL *handle); 155 156 void SendNetworkProfiler(); 157 158 RequestTracer::Trace &GetTrace(); 159 160 bool IsRootCaVerified() const; 161 162 void SetRootCaVerified(); 163 164 bool IsRootCaVerifiedOk() const; 165 166 void SetRootCaVerifiedOk(bool ok); 167 168 void SetPinnedPubkey(std::string &pubkey); 169 170 std::string GetPinnedPubkey() const; 171 172 #ifdef HTTP_HANDOVER_FEATURE 173 void SetRequestHandoverInfo(int32_t handoverNum, int32_t handoverReason, double flowControlTime, int32_t readFlag); 174 175 std::string GetRequestHandoverInfo(); 176 #endif 177 private: 178 uint32_t magicNumber_ = MAGIC_NUMBER; 179 int32_t taskId_ = -1; 180 bool usingCache_ = true; 181 bool requestInStream_ = false; 182 std::mutex dlLenLock_; 183 std::mutex ulLenLock_; 184 std::mutex tempDataLock_; 185 std::queue<std::string> tempData_; 186 HttpResponse cacheResponse_; 187 std::queue<LoadBytes> dlBytes_; 188 std::queue<LoadBytes> ulBytes_; 189 curl_slist *curlHeaderList_ = nullptr; 190 Timing::TimerMap timerMap_; 191 std::map<std::string, double> performanceTimingMap_; 192 curl_mime *multipart_ = nullptr; 193 CertsPath certsPath_; 194 uint64_t moduleId_ = 0; 195 curl_slist *curlHostList_ = nullptr; 196 bool isAtomicService_ = false; 197 std::string bundleName_; 198 bool isRootCaVerified_ = false; 199 bool isRootCaVerifiedOk_ = false; 200 std::string pinnedPubkey_; 201 #if HAS_NETMANAGER_BASE 202 std::unique_ptr<NetworkProfilerUtils> networkProfilerUtils_; 203 #endif 204 CURL *curlHandle_; 205 #ifdef HTTP_HANDOVER_FEATURE 206 RequestHandoverInfo requestHandoverInfo_; 207 #endif 208 RequestTracer::Trace trace_; 209 210 bool CheckParamsType(napi_value *params, size_t paramsCount); 211 212 void ParseNumberOptions(napi_value optionsValue); 213 214 void ParseHeader(napi_value optionsValue); 215 216 bool ParseExtraData(napi_value optionsValue); 217 218 void ParseUsingHttpProxy(napi_value optionsValue); 219 220 void ParseCaPath(napi_value optionsValue); 221 222 void ParseCaData(napi_value optionsValue); 223 224 void ParseDnsServers(napi_value optionsValue); 225 226 void ParseMultiFormData(napi_value optionsValue); 227 228 void ParseDohUrl(napi_value optionsValue); 229 230 void ParseResumeFromToNumber(napi_value optionsValue); 231 232 void ParseCertificatePinning(napi_value optionsValue); 233 234 bool GetRequestBody(napi_value extraData); 235 236 void UrlAndOptions(napi_value urlValue, napi_value optionsValue); 237 238 bool HandleMethodForGet(napi_value extraData); 239 240 MultiFormData NapiValue2FormData(napi_value formDataValue); 241 242 CertificatePinning NapiValue2CertPinning(napi_value certPIN); 243 244 void SaveFormData(napi_env env, napi_value dataValue, MultiFormData &multiFormData); 245 246 void ParseAddressFamily(napi_value optionsValue); 247 }; 248 } // namespace OHOS::NetStack::Http 249 250 #endif /* COMMUNICATIONNETSTACK_REQUEST_CONTEXT_H */ 251