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
SetUsingHttpProxyType(UsingHttpProxyType type)180 void HttpRequestOptions::SetUsingHttpProxyType(UsingHttpProxyType type)
181 {
182 usingHttpProxyType_ = type;
183 }
184
GetUsingHttpProxyType() const185 UsingHttpProxyType HttpRequestOptions::GetUsingHttpProxyType() const
186 {
187 return usingHttpProxyType_;
188 }
189
SetSpecifiedHttpProxy(const std::string & host,int32_t port,const std::string & exclusionList)190 void HttpRequestOptions::SetSpecifiedHttpProxy(const std::string &host, int32_t port, const std::string &exclusionList)
191 {
192 httpProxyHost_ = host;
193 httpProxyPort_ = port;
194 httpProxyExclusions_ = exclusionList;
195 }
196
GetSpecifiedHttpProxy(std::string & host,int32_t & port,std::string & exclusionList)197 void HttpRequestOptions::GetSpecifiedHttpProxy(std::string &host, int32_t &port, std::string &exclusionList)
198 {
199 host = httpProxyHost_;
200 port = httpProxyPort_;
201 exclusionList = httpProxyExclusions_;
202 }
203
SetClientCert(std::string & cert,std::string & certType,std::string & key,Secure::SecureChar & keyPasswd)204 void HttpRequestOptions::SetClientCert(
205 std::string &cert, std::string &certType, std::string &key, Secure::SecureChar &keyPasswd)
206 {
207 cert_ = cert;
208 certType_ = certType;
209 key_ = key;
210 keyPasswd_ = keyPasswd;
211 }
212
AddMultiFormData(const MultiFormData & multiFormData)213 void HttpRequestOptions::AddMultiFormData(const MultiFormData &multiFormData)
214 {
215 multiFormDataList_.push_back(multiFormData);
216 }
217
GetClientCert(std::string & cert,std::string & certType,std::string & key,Secure::SecureChar & keyPasswd)218 void HttpRequestOptions::GetClientCert(
219 std::string &cert, std::string &certType, std::string &key, Secure::SecureChar &keyPasswd)
220 {
221 cert = cert_;
222 certType = certType_;
223 key = key_;
224 keyPasswd = keyPasswd_;
225 }
226
SetCaPath(const std::string & path)227 void HttpRequestOptions::SetCaPath(const std::string &path)
228 {
229 if (path.empty()) {
230 return;
231 }
232
233 caPath_ = path;
234 }
235
GetCaPath() const236 const std::string &HttpRequestOptions::GetCaPath() const
237 {
238 return caPath_;
239 }
240
SetDohUrl(const std::string & dohUrl)241 void HttpRequestOptions::SetDohUrl(const std::string &dohUrl)
242 {
243 if (dohUrl.empty()) {
244 return;
245 }
246 dohUrl_ = dohUrl;
247 }
248
GetDohUrl() const249 const std::string &HttpRequestOptions::GetDohUrl() const
250 {
251 return dohUrl_;
252 }
253
SetRangeNumber(int64_t resumeFromNumber,int64_t resumeToNumber)254 void HttpRequestOptions::SetRangeNumber(int64_t resumeFromNumber, int64_t resumeToNumber)
255 {
256 if (resumeFromNumber >= MIN_RESUM_NUMBER && resumeFromNumber <= MAX_RESUM_NUMBER) {
257 resumeFromNumber_ = resumeFromNumber;
258 }
259 if (resumeToNumber >= MIN_RESUM_NUMBER && resumeToNumber <= MAX_RESUM_NUMBER) {
260 resumeToNumber_ = resumeToNumber;
261 }
262 }
263
GetRangeString() const264 std::string HttpRequestOptions::GetRangeString() const
265 {
266 bool isSetFrom = resumeFromNumber_ >= MIN_RESUM_NUMBER;
267 bool isSetTo = resumeToNumber_ >= MIN_RESUM_NUMBER;
268 if (!isSetTo && !isSetFrom) {
269 return "";
270 } else if (!isSetTo && isSetFrom) {
271 return std::to_string(resumeFromNumber_) + '-';
272 } else if (isSetTo && !isSetFrom) {
273 return '-' + std::to_string(resumeToNumber_);
274 } else if (resumeToNumber_ <= resumeFromNumber_) {
275 return "";
276 } else {
277 return std::to_string(resumeFromNumber_) + '-' + std::to_string(resumeToNumber_);
278 }
279 }
280
GetDnsServers() const281 const std::vector<std::string> &HttpRequestOptions::GetDnsServers() const
282 {
283 return dnsServers_;
284 }
285
SetDnsServers(const std::vector<std::string> & dnsServers)286 void HttpRequestOptions::SetDnsServers(const std::vector<std::string> &dnsServers)
287 {
288 dnsServers_ = dnsServers;
289 }
290
GetMultiPartDataList()291 std::vector<MultiFormData> HttpRequestOptions::GetMultiPartDataList()
292 {
293 return multiFormDataList_;
294 }
295
SetCertificatePinning(std::string certPIN)296 void HttpRequestOptions::SetCertificatePinning(std::string certPIN)
297 {
298 certificatePinning_ = std::move(certPIN);
299 }
300
GetCertificatePinning() const301 std::string HttpRequestOptions::GetCertificatePinning() const
302 {
303 return certificatePinning_;
304 }
305 } // namespace OHOS::NetStack::Http