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 #include "netstack_common_utils.h"
17 #include "netstack_log.h"
18 #include "curl/curl.h"
19 #include "net_http_request.h"
20 
21 namespace OHOS::NetStack::Http {
22 
HttpRequest()23 HttpRequest::HttpRequest()
24     : method_(HTTP_METHOD_GET),
25       readTimeout_(DEFAULT_READ_TIMEOUT),
26       maxLimit_(DEFAULT_MAX_LIMIT),
27       connectTimeout_(DEFAULT_CONNECT_TIMEOUT),
28       usingProtocol_(HttpProtocol::HTTP_NONE),
29       dataType_(HttpDataType::NO_DATA_TYPE),
30       priority_(MIN_PRIORITY),
31       usingHttpProxyType_(UsingHttpProxyType::USE_DEFAULT),
32       httpProxyPort_(0),
33       resumeFromNumber_(0),
34       resumeToNumber_(0)
35 {}
36 
SetUrl(const std::string & url)37 void HttpRequest::SetUrl(const std::string &url)
38 {
39     url_ = url;
40 }
41 
SetMethod(const std::string & method)42 void HttpRequest::SetMethod(const std::string &method)
43 {
44     method_ = method;
45 }
46 
SetBody(const void * data,size_t length)47 void HttpRequest::SetBody(const void *data, size_t length)
48 {
49     body_.append(static_cast<const char *>(data), length);
50 }
51 
SetHeader(const std::string & key,const std::string & val)52 void HttpRequest::SetHeader(const std::string &key, const std::string &val)
53 {
54     header_[key] = val;
55 }
56 
SetReadTimeout(uint32_t readTimeout)57 void HttpRequest::SetReadTimeout(uint32_t readTimeout)
58 {
59     readTimeout_ = readTimeout;
60 }
61 
SetMaxLimit(uint32_t maxLimit)62 void HttpRequest::SetMaxLimit(uint32_t maxLimit)
63 {
64     if (maxLimit > MAX_LIMIT) {
65         NETSTACK_LOGI("maxLimit setting exceeds the maximum limit, use max limit");
66         maxLimit_ = MAX_LIMIT;
67         return;
68     }
69     maxLimit_ = maxLimit;
70 }
71 
SetConnectTimeout(uint32_t connectTimeout)72 void HttpRequest::SetConnectTimeout(uint32_t connectTimeout)
73 {
74     connectTimeout_ = connectTimeout;
75 }
76 
GetUrl() const77 const std::string &HttpRequest::GetUrl() const
78 {
79     return url_;
80 }
81 
GetMethod() const82 const std::string &HttpRequest::GetMethod() const
83 {
84     return method_;
85 }
86 
GetBody() const87 const std::string &HttpRequest::GetBody() const
88 {
89     return body_;
90 }
91 
GetHeader() const92 const std::map<std::string, std::string> &HttpRequest::GetHeader() const
93 {
94     return header_;
95 }
96 
GetReadTimeout() const97 uint32_t HttpRequest::GetReadTimeout() const
98 {
99     return readTimeout_;
100 }
101 
GetMaxLimit() const102 uint32_t HttpRequest::GetMaxLimit() const
103 {
104     return maxLimit_;
105 }
106 
GetConnectTimeout() const107 uint32_t HttpRequest::GetConnectTimeout() const
108 {
109     return connectTimeout_;
110 }
111 
SetUsingProtocol(HttpProtocol httpProtocol)112 void HttpRequest::SetUsingProtocol(HttpProtocol httpProtocol)
113 {
114     usingProtocol_ = httpProtocol;
115 }
116 
GetHttpVersion() const117 uint32_t HttpRequest::GetHttpVersion() const
118 {
119     if (usingProtocol_ == HttpProtocol::HTTP2) {
120         NETSTACK_LOGI("CURL_HTTP_VERSION_2_0");
121         return CURL_HTTP_VERSION_2_0;
122     }
123     if (usingProtocol_ == HttpProtocol::HTTP1_1) {
124         NETSTACK_LOGI("CURL_HTTP_VERSION_1_1");
125         return CURL_HTTP_VERSION_1_1;
126     }
127     return CURL_HTTP_VERSION_NONE;
128 }
129 
SetRequestTime(const std::string & time)130 void HttpRequest::SetRequestTime(const std::string &time)
131 {
132     requestTime_ = time;
133 }
134 
GetRequestTime() const135 const std::string &HttpRequest::GetRequestTime() const
136 {
137     return requestTime_;
138 }
139 
SetHttpDataType(HttpDataType dataType)140 void HttpRequest::SetHttpDataType(HttpDataType dataType)
141 {
142     if (dataType != HttpDataType::STRING && dataType != HttpDataType::ARRAY_BUFFER &&
143         dataType != HttpDataType::OBJECT) {
144         return;
145     }
146     dataType_ = dataType;
147 }
GetHttpDataType() const148 HttpDataType HttpRequest::GetHttpDataType() const
149 {
150     return dataType_;
151 }
152 
SetPriority(uint32_t priority)153 void HttpRequest::SetPriority(uint32_t priority)
154 {
155     if (priority < MIN_PRIORITY || priority > MAX_PRIORITY) {
156         return;
157     }
158     priority_ = priority;
159 }
160 
GetPriority() const161 uint32_t HttpRequest::GetPriority() const
162 {
163     return priority_;
164 }
165 
SetUsingHttpProxyType(UsingHttpProxyType type)166 void HttpRequest::SetUsingHttpProxyType(UsingHttpProxyType type)
167 {
168     usingHttpProxyType_ = type;
169 }
170 
GetUsingHttpProxyType() const171 UsingHttpProxyType HttpRequest::GetUsingHttpProxyType() const
172 {
173     return usingHttpProxyType_;
174 }
175 
SetSpecifiedHttpProxy(const std::string & host,int32_t port,const std::string & exclusionList)176 void HttpRequest::SetSpecifiedHttpProxy(const std::string &host, int32_t port, const std::string &exclusionList)
177 {
178     httpProxyHost_ = host;
179     httpProxyPort_ = port;
180     httpProxyExclusions_ = exclusionList;
181 }
182 
GetSpecifiedHttpProxy(std::string & host,int32_t & port,std::string & exclusionList)183 void HttpRequest::GetSpecifiedHttpProxy(std::string &host, int32_t &port, std::string &exclusionList)
184 {
185     host = httpProxyHost_;
186     port = httpProxyPort_;
187     exclusionList = httpProxyExclusions_;
188 }
189 
190 
SetClientCert(std::string & cert,std::string & certType,std::string & key,SecureChar & keyPasswd)191 void HttpRequest::SetClientCert(
192     std::string &cert, std::string &certType, std::string &key, SecureChar &keyPasswd)
193 {
194     cert_ = cert;
195     certType_ = certType;
196     key_ = key;
197     keyPasswd_ = keyPasswd;
198 }
199 
AddMultiFormData(const MultiFormData & multiFormData)200 void HttpRequest::AddMultiFormData(const MultiFormData &multiFormData)
201 {
202     multiFormDataList_.push_back(multiFormData);
203 }
204 
GetClientCert(std::string & cert,std::string & certType,std::string & key,SecureChar & keyPasswd)205 void HttpRequest::GetClientCert(
206     std::string &cert, std::string &certType, std::string &key, SecureChar &keyPasswd)
207 {
208     cert = cert_;
209     certType = certType_;
210     key = key_;
211     keyPasswd = keyPasswd_;
212 }
213 
SetCaPath(const std::string & path)214 void HttpRequest::SetCaPath(const std::string &path)
215 {
216     if (path.empty()) {
217         return;
218     }
219 
220     caPath_ = path;
221 }
222 
GetCaPath() const223 const std::string &HttpRequest::GetCaPath() const
224 {
225     return caPath_;
226 }
227 
228 
SetDohUrl(const std::string & dohUrl)229 void HttpRequest::SetDohUrl(const std::string &dohUrl)
230 {
231     if (dohUrl.empty()) {
232         return;
233     }
234     dohUrl_ = dohUrl;
235 }
236 
GetDohUrl() const237 const std::string &HttpRequest::GetDohUrl() const
238 {
239     return dohUrl_;
240 }
241 
SetRangeNumber(int64_t resumeFromNumber,int64_t resumeToNumber)242 void HttpRequest::SetRangeNumber(int64_t resumeFromNumber, int64_t resumeToNumber)
243 {
244     if (resumeFromNumber >= MIN_RESUM_NUMBER && resumeFromNumber <= MAX_RESUM_NUMBER) {
245         resumeFromNumber_ = resumeFromNumber;
246     }
247     if (resumeToNumber >= MIN_RESUM_NUMBER && resumeToNumber <= MAX_RESUM_NUMBER) {
248         resumeToNumber_ = resumeToNumber;
249     }
250 }
251 
GetRangeString() const252 std::string HttpRequest::GetRangeString() const
253 {
254     bool isSetFrom = resumeFromNumber_ >= MIN_RESUM_NUMBER;
255     bool isSetTo = resumeToNumber_ >= MIN_RESUM_NUMBER;
256     if (!isSetTo && !isSetFrom) {
257         return "";
258     } else if (!isSetTo && isSetFrom) {
259         return std::to_string(resumeFromNumber_) + '-';
260     } else if (isSetTo && !isSetFrom) {
261         return '-' + std::to_string(resumeToNumber_);
262     } else if (resumeToNumber_ <= resumeFromNumber_) {
263         return "";
264     } else {
265         return std::to_string(resumeFromNumber_) + '-' + std::to_string(resumeToNumber_);
266     }
267 }
268 
GetDnsServers() const269 const std::vector<std::string> &HttpRequest::GetDnsServers() const
270 {
271     return dnsServers_;
272 }
273 
SetDnsServers(const std::vector<std::string> & dnsServers)274 void HttpRequest::SetDnsServers(const std::vector<std::string> &dnsServers)
275 {
276     dnsServers_ = dnsServers;
277 }
278 
GetMultiPartDataList()279 std::vector<MultiFormData> HttpRequest::GetMultiPartDataList()
280 {
281     return multiFormDataList_;
282 }
283 } // namespace OHOS::NetStack::Http