• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 NET_HTTP_REQUEST_CONTEXT_H
17 #define NET_HTTP_REQUEST_CONTEXT_H
18 
19 #include <queue>
20 #include <mutex>
21 #include <map>
22 #include <vector>
23 #include "curl/curl.h"
24 #include "ffi_remote_data.h"
25 #include "net_http_request.h"
26 #include "net_http_response.h"
27 #include "net_http_utils.h"
28 #include "ffi_structs.h"
29 
30 namespace OHOS::NetStack::Http {
31 
32 struct CertsPath {
33     CertsPath() = default;
34     ~CertsPath() = default;
35     std::vector<std::string> certPathList;
36     std::string certFile;
37 };
38 
39 struct LoadBytes {
LoadBytesLoadBytes40     LoadBytes() : nLen(0), tLen(0) {};
LoadBytesLoadBytes41     LoadBytes(curl_off_t nowLen, curl_off_t totalLen): nLen(nowLen), tLen(totalLen) {};
42 
43     ~LoadBytes() = default;
44     curl_off_t nLen;
45     curl_off_t tLen;
46 };
47 
48 struct RequestCallback {
49     std::vector<std::function<void(CArrString)>> headersReceive;
50     std::vector<std::function<void(CArrString)>> headersReceiveOnce;
51     std::vector<std::function<void(CArrUI8)>> dataReceive;
52     std::vector<std::function<void()>> dataEnd;
53     std::vector<std::function<void(CDataReceiveProgressInfo)>> dataReceiveProgress;
54     std::vector<std::function<void(CDataSendProgressInfo)>> dataSendProgress;
55 };
56 
57 class RequestContext {
58 public:
59     RequestContext();
60 
61     ~RequestContext();
62 
63     void StartTiming();
64 
65     void ParseParams(std::string url, CHttpRequestOptions *ops);
66 
67     HttpRequest options;
68 
69     HttpResponse response;
70 
71     [[nodiscard]] bool IsUsingCache() const;
72 
73     void SetCurlHeaderList(struct curl_slist *curlHeaderList);
74 
75     struct curl_slist *GetCurlHeaderList();
76 
77     void SetCacheResponse(const HttpResponse &cacheResponse);
78 
79     void SetResponseByCache();
80 
81     [[nodiscard]] int32_t GetErrorCode() const;
82 
83     [[nodiscard]] std::string GetErrorMessage() const;
84 
85     void SetErrorCode(int32_t code);
86 
87     void EnableRequestInStream();
88 
89     [[nodiscard]] bool IsRequestInStream() const;
90 
91     void SetDlLen(curl_off_t nowLen, curl_off_t totalLen);
92 
93     LoadBytes GetDlLen();
94 
95     void SetUlLen(curl_off_t nowLen, curl_off_t totalLen);
96 
97     LoadBytes GetUlLen();
98 
99     bool CompareWithLastElement(curl_off_t nowLen, curl_off_t totalLen);
100 
101     void SetTempData(const void *data, size_t size);
102 
103     std::string GetTempData();
104 
105     void PopTempData();
106 
107     void SetCertsPath(std::vector<std::string> &&certPathList, const std::string &certFile);
108 
109     const CertsPath &GetCertsPath();
110 
111     void CachePerformanceTimingItem(const std::string &key, double value);
112 
113     void StopAndCachePerformanceTiming(const char *key);
114 
115     void SetPerformanceTimingToResult(CHttpResponse &resp);
116 
117     void SetMultipart(curl_mime *multipart);
118 
119     void SetParseOK();
120 
121     bool IsParseOK() const;
122 
123     void SetExecOK(bool ok);
124 
125     bool IsExecOK() const;
126 
127     void SetPermissionDenied(bool deny);
128 
129     bool IsPermissionDenied() const;
130 
131     void Destroy();
132 
133     bool IsDestroyed() const;
134 
135     void SendResponse();
136 
137     bool IsRootCaVerified() const;
138 
139     void SetRootCaVerified();
140 
141     bool IsRootCaVerifiedOk() const;
142 
143     void SetRootCaVerifiedOk(bool ok);
144 
145     void SetPinnedPubkey(std::string &pubkey);
146 
147     std::string GetPinnedPubkey() const;
148 
149     std::function<void(CHttpResponse)> respCallback;
150 
151     std::shared_ptr<RequestCallback> streamingCallback{nullptr};
152 private:
153     bool usingCache_ = true;
154     bool requestInStream_ = false;
155     std::mutex dlLenLock_;
156     std::mutex ulLenLock_;
157     std::mutex tempDataLock_;
158     std::queue<std::string> tempData_;
159     HttpResponse cacheResponse_;
160     std::queue<LoadBytes> dlBytes_;
161     std::queue<LoadBytes> ulBytes_;
162     struct curl_slist *curlHeaderList_ = nullptr;
163     TimerMap timerMap_;
164     std::map<std::string, double> performanceTimingMap_;
165     curl_mime *multipart_ = nullptr;
166     int32_t errCode_ = 0;
167     std::string errMsg_;
168     CertsPath certsPath_;
169     bool parseok_ = false;
170     bool requestOK_ = false;
171     bool permissionDenied_ = false;
172     bool isDestroyed_ = false;
173     bool isRootCaVerified_ = false;
174     bool isRootCaVerifiedOk_ = false;
175     std::string pinnedPubkey_;
176 
177     bool GetRequestBody(CArrUI8 extraData);
178     void HandleMethodForGet(CArrUI8 extraData);
179     void ParseUsingHttpProxy(CHttpProxy* proxy, bool useDefault);
180     bool ParseExtraData(CArrUI8 data);
181     void ParseDnsServers(CArrString dns);
182     void ParseMultiFormData(CArrMultiFormData multi);
183     void ParseHeader(CArrString header);
184 };
185 
186 class HttpRequestProxy : public OHOS::FFI::FFIData {
187     DECL_TYPE(HttpRequestProxy, OHOS::FFI::FFIData);
188 public:
189     RequestContext* Request(std::string url, CHttpRequestOptions *ops, bool isInStream);
190     RequestContext* RequestInStream(std::string url, CHttpRequestOptions *ops);
191     void Destroy();
192 
193     std::shared_ptr<RequestCallback> callbacks = std::make_shared<RequestCallback>();
194 
195     bool isDestroyed = false;
196 };
197 
198 }
199 #endif