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