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     std::function<void(CHttpResponse)> respCallback;
138 
139     std::shared_ptr<RequestCallback> streamingCallback{nullptr};
140 private:
141     bool usingCache_ = true;
142     bool requestInStream_ = false;
143     std::mutex dlLenLock_;
144     std::mutex ulLenLock_;
145     std::mutex tempDataLock_;
146     std::queue<std::string> tempData_;
147     HttpResponse cacheResponse_;
148     std::queue<LoadBytes> dlBytes_;
149     std::queue<LoadBytes> ulBytes_;
150     struct curl_slist *curlHeaderList_ = nullptr;
151     TimerMap timerMap_;
152     std::map<std::string, double> performanceTimingMap_;
153     curl_mime *multipart_ = nullptr;
154     int32_t errCode_ = 0;
155     std::string errMsg_;
156     CertsPath certsPath_;
157     bool parseok_ = false;
158     bool requestOK_ = false;
159     bool permissionDenied_ = false;
160     bool isDestroyed_ = false;
161 
162     bool GetRequestBody(CArrUI8 extraData);
163     void HandleMethodForGet(CArrUI8 extraData);
164     void ParseUsingHttpProxy(CHttpProxy* proxy, bool useDefault);
165     bool ParseExtraData(CArrUI8 data);
166     void ParseDnsServers(CArrString dns);
167     void ParseMultiFormData(CArrMultiFormData multi);
168     void ParseHeader(CArrString header);
169 };
170 
171 class HttpRequestProxy : public OHOS::FFI::FFIData {
172     DECL_TYPE(HttpRequestProxy, OHOS::FFI::FFIData);
173 public:
174     RequestContext* Request(std::string url, CHttpRequestOptions *ops, bool isInStream);
175     RequestContext* RequestInStream(std::string url, CHttpRequestOptions *ops);
176     void Destroy();
177 
178     std::shared_ptr<RequestCallback> callbacks = std::make_shared<RequestCallback>();
179 
180     bool isDestroyed = false;
181 };
182 
183 }
184 #endif