• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 
28 namespace OHOS::NetStack::Http {
29 struct LoadBytes {
LoadBytesLoadBytes30     LoadBytes() : nLen(0), tLen(0) {};
LoadBytesLoadBytes31     LoadBytes(curl_off_t nowLen, curl_off_t totalLen)
32     {
33         nLen = nowLen;
34         tLen = totalLen;
35     };
36     ~LoadBytes() = default;
37     curl_off_t nLen;
38     curl_off_t tLen;
39 };
40 
41 class RequestContext final : public BaseContext {
42 public:
43     RequestContext() = delete;
44 
45     RequestContext(napi_env env, EventManager *manager);
46 
47     ~RequestContext() override;
48 
49     void StartTiming();
50 
51     void ParseParams(napi_value *params, size_t paramsCount) override;
52 
53     HttpRequestOptions options;
54 
55     HttpResponse response;
56 
57     [[nodiscard]] bool IsUsingCache() const;
58 
59     void SetCurlHeaderList(struct curl_slist *curlHeaderList);
60 
61     struct curl_slist *GetCurlHeaderList();
62 
63     void SetCacheResponse(const HttpResponse &cacheResponse);
64 
65     void SetResponseByCache();
66 
67     [[nodiscard]] int32_t GetErrorCode() const override;
68 
69     [[nodiscard]] std::string GetErrorMessage() const override;
70 
71     void EnableRequestInStream();
72 
73     [[nodiscard]] bool IsRequestInStream() const;
74 
75     void SetDlLen(curl_off_t nowLen, curl_off_t totalLen);
76 
77     LoadBytes GetDlLen();
78 
79     void SetUlLen(curl_off_t nowLen, curl_off_t totalLen);
80 
81     LoadBytes GetUlLen();
82 
83     bool CompareWithLastElement(curl_off_t nowLen, curl_off_t totalLen);
84 
85     void SetTempData(const void *data, size_t size);
86 
87     std::string GetTempData();
88 
89     void PopTempData();
90 
91     void ParseClientCert(napi_value optionsValue);
92 
93     void CachePerformanceTimingItem(const std::string &key, double value);
94 
95     void StopAndCacheNapiPerformanceTiming(const char *key);
96 
97     void SetPerformanceTimingToResult(napi_value result);
98 
99     void SetMultipart(curl_mime *multipart);
100 
101 private:
102     bool usingCache_;
103     bool requestInStream_;
104     std::mutex dlLenLock_;
105     std::mutex ulLenLock_;
106     std::mutex tempDataLock_;
107     std::queue<std::string> tempData_;
108     HttpResponse cacheResponse_;
109     std::queue<LoadBytes> dlBytes_;
110     std::queue<LoadBytes> ulBytes_;
111     struct curl_slist *curlHeaderList_;
112     Timing::TimerMap timerMap_;
113     std::map<std::string, double> performanceTimingMap_;
114     curl_mime *multipart_;
115 
116     bool CheckParamsType(napi_value *params, size_t paramsCount);
117 
118     void ParseNumberOptions(napi_value optionsValue);
119 
120     void ParseHeader(napi_value optionsValue);
121 
122     bool ParseExtraData(napi_value optionsValue);
123 
124     void ParseUsingHttpProxy(napi_value optionsValue);
125 
126     void ParseCaPath(napi_value optionsValue);
127 
128     void ParseDnsServers(napi_value optionsValue);
129 
130     void ParseMultiFormData(napi_value optionsValue);
131 
132     void ParseDohUrl(napi_value optionsValue);
133 
134     void ParseResumeFromToNumber(napi_value optionsValue);
135 
136     bool GetRequestBody(napi_value extraData);
137 
138     void UrlAndOptions(napi_value urlValue, napi_value optionsValue);
139 
140     bool HandleMethodForGet(napi_value extraData);
141 
142     MultiFormData NapiValue2FormData(napi_value formDataValue);
143 
144     void SaveFormData(napi_env env, napi_value dataValue, MultiFormData &multiFormData);
145 };
146 } // namespace OHOS::NetStack::Http
147 
148 #endif /* COMMUNICATIONNETSTACK_REQUEST_CONTEXT_H */
149