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 #include "constant.h"
17 #include "curl/curl.h"
18 #include "netstack_common_utils.h"
19 #include "netstack_log.h"
20
21 #include "http_request_options.h"
22 #include "secure_char.h"
23
24 namespace OHOS::NetStack::Http {
25 static constexpr const uint32_t MIN_PRIORITY = 1;
26 static constexpr const uint32_t MAX_PRIORITY = 1000;
27
28 static constexpr const int64_t MIN_RESUM_NUMBER = 1;
29 static constexpr const int64_t MAX_RESUM_NUMBER = 4294967296;
30
HttpRequestOptions()31 HttpRequestOptions::HttpRequestOptions()
32 : method_(HttpConstant::HTTP_METHOD_GET),
33 readTimeout_(HttpConstant::DEFAULT_READ_TIMEOUT),
34 maxLimit_(HttpConstant::DEFAULT_MAX_LIMIT),
35 connectTimeout_(HttpConstant::DEFAULT_CONNECT_TIMEOUT),
36 usingProtocol_(HttpProtocol::HTTP_NONE),
37 dataType_(HttpDataType::NO_DATA_TYPE),
38 priority_(MIN_PRIORITY),
39 usingHttpProxyType_(UsingHttpProxyType::USE_DEFAULT),
40 httpProxyPort_(0),
41 resumeFromNumber_(0),
42 resumeToNumber_(0)
43 {
44 }
45
SetUrl(const std::string & url)46 void HttpRequestOptions::SetUrl(const std::string &url)
47 {
48 url_ = url;
49 }
50
SetMethod(const std::string & method)51 void HttpRequestOptions::SetMethod(const std::string &method)
52 {
53 method_ = method;
54 }
55
SetBody(const void * data,size_t length)56 void HttpRequestOptions::SetBody(const void *data, size_t length)
57 {
58 body_.append(static_cast<const char *>(data), length);
59 }
60
SetHeader(const std::string & key,const std::string & val)61 void HttpRequestOptions::SetHeader(const std::string &key, const std::string &val)
62 {
63 header_[key] = val;
64 }
65
SetReadTimeout(uint32_t readTimeout)66 void HttpRequestOptions::SetReadTimeout(uint32_t readTimeout)
67 {
68 readTimeout_ = readTimeout;
69 }
70
SetMaxLimit(uint32_t maxLimit)71 void HttpRequestOptions::SetMaxLimit(uint32_t maxLimit)
72 {
73 if (maxLimit > HttpConstant::MAX_LIMIT) {
74 NETSTACK_LOGD("maxLimit setting exceeds the maximum limit, use max limit");
75 maxLimit_ = HttpConstant::MAX_LIMIT;
76 return;
77 }
78 maxLimit_ = maxLimit;
79 }
80
SetConnectTimeout(uint32_t connectTimeout)81 void HttpRequestOptions::SetConnectTimeout(uint32_t connectTimeout)
82 {
83 connectTimeout_ = connectTimeout;
84 }
85
GetUrl() const86 const std::string &HttpRequestOptions::GetUrl() const
87 {
88 return url_;
89 }
90
GetMethod() const91 const std::string &HttpRequestOptions::GetMethod() const
92 {
93 return method_;
94 }
95
GetBody() const96 const std::string &HttpRequestOptions::GetBody() const
97 {
98 return body_;
99 }
100
GetHeader() const101 const std::map<std::string, std::string> &HttpRequestOptions::GetHeader() const
102 {
103 return header_;
104 }
105
GetReadTimeout() const106 uint32_t HttpRequestOptions::GetReadTimeout() const
107 {
108 return readTimeout_;
109 }
110
GetMaxLimit() const111 uint32_t HttpRequestOptions::GetMaxLimit() const
112 {
113 return maxLimit_;
114 }
115
GetConnectTimeout() const116 uint32_t HttpRequestOptions::GetConnectTimeout() const
117 {
118 return connectTimeout_;
119 }
120
SetUsingProtocol(HttpProtocol httpProtocol)121 void HttpRequestOptions::SetUsingProtocol(HttpProtocol httpProtocol)
122 {
123 usingProtocol_ = httpProtocol;
124 }
125
GetHttpVersion() const126 uint32_t HttpRequestOptions::GetHttpVersion() const
127 {
128 if (usingProtocol_ == HttpProtocol::HTTP3) {
129 NETSTACK_LOGD("CURL_HTTP_VERSION_3");
130 return CURL_HTTP_VERSION_3;
131 }
132 if (usingProtocol_ == HttpProtocol::HTTP2) {
133 NETSTACK_LOGD("CURL_HTTP_VERSION_2_0");
134 return CURL_HTTP_VERSION_2_0;
135 }
136 if (usingProtocol_ == HttpProtocol::HTTP1_1) {
137 NETSTACK_LOGD("CURL_HTTP_VERSION_1_1");
138 return CURL_HTTP_VERSION_1_1;
139 }
140 return CURL_HTTP_VERSION_NONE;
141 }
142
SetRequestTime(const std::string & time)143 void HttpRequestOptions::SetRequestTime(const std::string &time)
144 {
145 requestTime_ = time;
146 }
147
GetRequestTime() const148 const std::string &HttpRequestOptions::GetRequestTime() const
149 {
150 return requestTime_;
151 }
152
SetHttpDataType(HttpDataType dataType)153 void HttpRequestOptions::SetHttpDataType(HttpDataType dataType)
154 {
155 if (dataType != HttpDataType::STRING && dataType != HttpDataType::ARRAY_BUFFER &&
156 dataType != HttpDataType::OBJECT) {
157 return;
158 }
159 dataType_ = dataType;
160 }
161
GetHttpDataType() const162 HttpDataType HttpRequestOptions::GetHttpDataType() const
163 {
164 return dataType_;
165 }
166
SetPriority(uint32_t priority)167 void HttpRequestOptions::SetPriority(uint32_t priority)
168 {
169 if (priority < MIN_PRIORITY || priority > MAX_PRIORITY) {
170 return;
171 }
172 priority_ = priority;
173 }
174
GetPriority() const175 uint32_t HttpRequestOptions::GetPriority() const
176 {
177 return priority_;
178 }
179
SetCanSkipCertVerifyFlag(bool canCertVerify)180 void HttpRequestOptions::SetCanSkipCertVerifyFlag(bool canCertVerify)
181 {
182 canSkipCertVerify_ = canCertVerify;
183 }
184
GetCanSkipCertVerifyFlag() const185 bool HttpRequestOptions::GetCanSkipCertVerifyFlag() const
186 {
187 return canSkipCertVerify_;
188 }
SetUsingHttpProxyType(UsingHttpProxyType type)189 void HttpRequestOptions::SetUsingHttpProxyType(UsingHttpProxyType type)
190 {
191 usingHttpProxyType_ = type;
192 }
193
GetUsingHttpProxyType() const194 UsingHttpProxyType HttpRequestOptions::GetUsingHttpProxyType() const
195 {
196 return usingHttpProxyType_;
197 }
198
SetSpecifiedHttpProxy(const std::string & host,int32_t port,const std::string & exclusionList)199 void HttpRequestOptions::SetSpecifiedHttpProxy(const std::string &host, int32_t port, const std::string &exclusionList)
200 {
201 httpProxyHost_ = host;
202 httpProxyPort_ = port;
203 httpProxyExclusions_ = exclusionList;
204 }
205
GetSpecifiedHttpProxy(std::string & host,int32_t & port,std::string & exclusionList)206 void HttpRequestOptions::GetSpecifiedHttpProxy(std::string &host, int32_t &port, std::string &exclusionList)
207 {
208 host = httpProxyHost_;
209 port = httpProxyPort_;
210 exclusionList = httpProxyExclusions_;
211 }
212
SetClientCert(std::string & cert,std::string & certType,std::string & key,Secure::SecureChar & keyPasswd)213 void HttpRequestOptions::SetClientCert(
214 std::string &cert, std::string &certType, std::string &key, Secure::SecureChar &keyPasswd)
215 {
216 cert_ = cert;
217 certType_ = certType;
218 key_ = key;
219 keyPasswd_ = keyPasswd;
220 }
221
AddMultiFormData(const MultiFormData & multiFormData)222 void HttpRequestOptions::AddMultiFormData(const MultiFormData &multiFormData)
223 {
224 multiFormDataList_.push_back(multiFormData);
225 }
226
GetClientCert(std::string & cert,std::string & certType,std::string & key,Secure::SecureChar & keyPasswd)227 void HttpRequestOptions::GetClientCert(
228 std::string &cert, std::string &certType, std::string &key, Secure::SecureChar &keyPasswd)
229 {
230 cert = cert_;
231 certType = certType_;
232 key = key_;
233 keyPasswd = keyPasswd_;
234 }
235
SetCaPath(const std::string & path)236 void HttpRequestOptions::SetCaPath(const std::string &path)
237 {
238 if (path.empty()) {
239 return;
240 }
241
242 caPath_ = path;
243 }
244
GetCaPath() const245 const std::string &HttpRequestOptions::GetCaPath() const
246 {
247 return caPath_;
248 }
249
SetTlsOption(const TlsOption & tlsOption)250 void HttpRequestOptions::SetTlsOption(const TlsOption &tlsOption)
251 {
252 tlsOption_.tlsVersionMax = tlsOption.tlsVersionMax;
253 tlsOption_.tlsVersionMin = tlsOption.tlsVersionMin;
254 tlsOption_.cipherSuite = tlsOption.cipherSuite;
255 }
256
GetTlsOption() const257 const TlsOption HttpRequestOptions::GetTlsOption() const
258 {
259 return tlsOption_;
260 }
261
SetServerAuthentication(const ServerAuthentication & serverAuthentication)262 void HttpRequestOptions::SetServerAuthentication(const ServerAuthentication &serverAuthentication)
263 {
264 serverAuthentication_.authenticationType = serverAuthentication.authenticationType;
265 serverAuthentication_.credential.password = serverAuthentication.credential.password;
266 serverAuthentication_.credential.username = serverAuthentication.credential.username;
267 }
268
GetServerAuthentication() const269 const ServerAuthentication HttpRequestOptions::GetServerAuthentication() const
270 {
271 return serverAuthentication_;
272 }
273
SetDohUrl(const std::string & dohUrl)274 void HttpRequestOptions::SetDohUrl(const std::string &dohUrl)
275 {
276 if (dohUrl.empty()) {
277 return;
278 }
279 dohUrl_ = dohUrl;
280 }
281
GetDohUrl() const282 const std::string &HttpRequestOptions::GetDohUrl() const
283 {
284 return dohUrl_;
285 }
286
SetRangeNumber(int64_t resumeFromNumber,int64_t resumeToNumber)287 void HttpRequestOptions::SetRangeNumber(int64_t resumeFromNumber, int64_t resumeToNumber)
288 {
289 if (resumeFromNumber >= MIN_RESUM_NUMBER && resumeFromNumber <= MAX_RESUM_NUMBER) {
290 resumeFromNumber_ = resumeFromNumber;
291 }
292 if (resumeToNumber >= MIN_RESUM_NUMBER && resumeToNumber <= MAX_RESUM_NUMBER) {
293 resumeToNumber_ = resumeToNumber;
294 }
295 }
296
GetRangeString() const297 std::string HttpRequestOptions::GetRangeString() const
298 {
299 bool isSetFrom = resumeFromNumber_ >= MIN_RESUM_NUMBER;
300 bool isSetTo = resumeToNumber_ >= MIN_RESUM_NUMBER;
301 if (!isSetTo && !isSetFrom) {
302 return "";
303 } else if (!isSetTo && isSetFrom) {
304 return std::to_string(resumeFromNumber_) + '-';
305 } else if (isSetTo && !isSetFrom) {
306 return '-' + std::to_string(resumeToNumber_);
307 } else if (resumeToNumber_ <= resumeFromNumber_) {
308 return "";
309 } else {
310 return std::to_string(resumeFromNumber_) + '-' + std::to_string(resumeToNumber_);
311 }
312 }
313
GetDnsServers() const314 const std::vector<std::string> &HttpRequestOptions::GetDnsServers() const
315 {
316 return dnsServers_;
317 }
318
SetDnsServers(const std::vector<std::string> & dnsServers)319 void HttpRequestOptions::SetDnsServers(const std::vector<std::string> &dnsServers)
320 {
321 dnsServers_ = dnsServers;
322 }
323
GetMultiPartDataList()324 std::vector<MultiFormData> HttpRequestOptions::GetMultiPartDataList()
325 {
326 return multiFormDataList_;
327 }
328
SetCertificatePinning(std::string certPIN)329 void HttpRequestOptions::SetCertificatePinning(std::string certPIN)
330 {
331 certificatePinning_ = std::move(certPIN);
332 }
333
GetCertificatePinning() const334 std::string HttpRequestOptions::GetCertificatePinning() const
335 {
336 return certificatePinning_;
337 }
338
SetAddressFamily(std::string addressFamily)339 void HttpRequestOptions::SetAddressFamily(std::string addressFamily)
340 {
341 addressFamily_ = std::move(addressFamily);
342 }
343
GetAddressFamily() const344 std::string HttpRequestOptions::GetAddressFamily() const
345 {
346 return addressFamily_;
347 }
348 } // namespace OHOS::NetStack::Http