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 #ifndef WINDOWS_PLATFORM
45 caPath_ = HttpConstant::HTTP_DEFAULT_CA_PATH;
46 #endif // WINDOWS_PLATFORM
47 }
48
SetUrl(const std::string & url)49 void HttpRequestOptions::SetUrl(const std::string &url)
50 {
51 url_ = url;
52 }
53
SetMethod(const std::string & method)54 void HttpRequestOptions::SetMethod(const std::string &method)
55 {
56 method_ = method;
57 }
58
SetBody(const void * data,size_t length)59 void HttpRequestOptions::SetBody(const void *data, size_t length)
60 {
61 body_.append(static_cast<const char *>(data), length);
62 }
63
SetHeader(const std::string & key,const std::string & val)64 void HttpRequestOptions::SetHeader(const std::string &key, const std::string &val)
65 {
66 header_[key] = val;
67 }
68
SetReadTimeout(uint32_t readTimeout)69 void HttpRequestOptions::SetReadTimeout(uint32_t readTimeout)
70 {
71 readTimeout_ = readTimeout;
72 }
73
SetMaxLimit(uint32_t maxLimit)74 void HttpRequestOptions::SetMaxLimit(uint32_t maxLimit)
75 {
76 if (maxLimit > HttpConstant::MAX_LIMIT) {
77 NETSTACK_LOGD("maxLimit setting exceeds the maximum limit, use max limit");
78 maxLimit_ = HttpConstant::MAX_LIMIT;
79 return;
80 }
81 maxLimit_ = maxLimit;
82 }
83
SetConnectTimeout(uint32_t connectTimeout)84 void HttpRequestOptions::SetConnectTimeout(uint32_t connectTimeout)
85 {
86 connectTimeout_ = connectTimeout;
87 }
88
GetUrl() const89 const std::string &HttpRequestOptions::GetUrl() const
90 {
91 return url_;
92 }
93
GetMethod() const94 const std::string &HttpRequestOptions::GetMethod() const
95 {
96 return method_;
97 }
98
GetBody() const99 const std::string &HttpRequestOptions::GetBody() const
100 {
101 return body_;
102 }
103
GetHeader() const104 const std::map<std::string, std::string> &HttpRequestOptions::GetHeader() const
105 {
106 return header_;
107 }
108
GetReadTimeout() const109 uint32_t HttpRequestOptions::GetReadTimeout() const
110 {
111 return readTimeout_;
112 }
113
GetMaxLimit() const114 uint32_t HttpRequestOptions::GetMaxLimit() const
115 {
116 return maxLimit_;
117 }
118
GetConnectTimeout() const119 uint32_t HttpRequestOptions::GetConnectTimeout() const
120 {
121 return connectTimeout_;
122 }
123
SetUsingProtocol(HttpProtocol httpProtocol)124 void HttpRequestOptions::SetUsingProtocol(HttpProtocol httpProtocol)
125 {
126 usingProtocol_ = httpProtocol;
127 }
128
GetHttpVersion() const129 uint32_t HttpRequestOptions::GetHttpVersion() const
130 {
131 if (usingProtocol_ == HttpProtocol::HTTP3) {
132 NETSTACK_LOGD("CURL_HTTP_VERSION_3");
133 return CURL_HTTP_VERSION_3;
134 }
135 if (usingProtocol_ == HttpProtocol::HTTP2) {
136 NETSTACK_LOGD("CURL_HTTP_VERSION_2_0");
137 return CURL_HTTP_VERSION_2_0;
138 }
139 if (usingProtocol_ == HttpProtocol::HTTP1_1) {
140 NETSTACK_LOGD("CURL_HTTP_VERSION_1_1");
141 return CURL_HTTP_VERSION_1_1;
142 }
143 return CURL_HTTP_VERSION_NONE;
144 }
145
SetRequestTime(const std::string & time)146 void HttpRequestOptions::SetRequestTime(const std::string &time)
147 {
148 requestTime_ = time;
149 }
150
GetRequestTime() const151 const std::string &HttpRequestOptions::GetRequestTime() const
152 {
153 return requestTime_;
154 }
155
SetHttpDataType(HttpDataType dataType)156 void HttpRequestOptions::SetHttpDataType(HttpDataType dataType)
157 {
158 if (dataType != HttpDataType::STRING && dataType != HttpDataType::ARRAY_BUFFER &&
159 dataType != HttpDataType::OBJECT) {
160 return;
161 }
162 dataType_ = dataType;
163 }
164
GetHttpDataType() const165 HttpDataType HttpRequestOptions::GetHttpDataType() const
166 {
167 return dataType_;
168 }
169
SetPriority(uint32_t priority)170 void HttpRequestOptions::SetPriority(uint32_t priority)
171 {
172 if (priority < MIN_PRIORITY || priority > MAX_PRIORITY) {
173 return;
174 }
175 priority_ = priority;
176 }
177
GetPriority() const178 uint32_t HttpRequestOptions::GetPriority() const
179 {
180 return priority_;
181 }
182
SetUsingHttpProxyType(UsingHttpProxyType type)183 void HttpRequestOptions::SetUsingHttpProxyType(UsingHttpProxyType type)
184 {
185 usingHttpProxyType_ = type;
186 }
187
GetUsingHttpProxyType() const188 UsingHttpProxyType HttpRequestOptions::GetUsingHttpProxyType() const
189 {
190 return usingHttpProxyType_;
191 }
192
SetSpecifiedHttpProxy(const std::string & host,int32_t port,const std::string & exclusionList)193 void HttpRequestOptions::SetSpecifiedHttpProxy(const std::string &host, int32_t port, const std::string &exclusionList)
194 {
195 httpProxyHost_ = host;
196 httpProxyPort_ = port;
197 httpProxyExclusions_ = exclusionList;
198 }
199
GetSpecifiedHttpProxy(std::string & host,int32_t & port,std::string & exclusionList)200 void HttpRequestOptions::GetSpecifiedHttpProxy(std::string &host, int32_t &port, std::string &exclusionList)
201 {
202 host = httpProxyHost_;
203 port = httpProxyPort_;
204 exclusionList = httpProxyExclusions_;
205 }
206
SetClientCert(std::string & cert,std::string & certType,std::string & key,Secure::SecureChar & keyPasswd)207 void HttpRequestOptions::SetClientCert(
208 std::string &cert, std::string &certType, std::string &key, Secure::SecureChar &keyPasswd)
209 {
210 cert_ = cert;
211 certType_ = certType;
212 key_ = key;
213 keyPasswd_ = keyPasswd;
214 }
215
AddMultiFormData(const MultiFormData & multiFormData)216 void HttpRequestOptions::AddMultiFormData(const MultiFormData &multiFormData)
217 {
218 multiFormDataList_.push_back(multiFormData);
219 }
220
GetClientCert(std::string & cert,std::string & certType,std::string & key,Secure::SecureChar & keyPasswd)221 void HttpRequestOptions::GetClientCert(
222 std::string &cert, std::string &certType, std::string &key, Secure::SecureChar &keyPasswd)
223 {
224 cert = cert_;
225 certType = certType_;
226 key = key_;
227 keyPasswd = keyPasswd_;
228 }
229
SetCaPath(const std::string & path)230 void HttpRequestOptions::SetCaPath(const std::string &path)
231 {
232 if (path.empty()) {
233 return;
234 }
235
236 caPath_ = path;
237 }
238
GetCaPath() const239 const std::string &HttpRequestOptions::GetCaPath() const
240 {
241 return caPath_;
242 }
243
SetDohUrl(const std::string & dohUrl)244 void HttpRequestOptions::SetDohUrl(const std::string &dohUrl)
245 {
246 if (dohUrl.empty()) {
247 return;
248 }
249 dohUrl_ = dohUrl;
250 }
251
GetDohUrl() const252 const std::string &HttpRequestOptions::GetDohUrl() const
253 {
254 return dohUrl_;
255 }
256
SetRangeNumber(int64_t resumeFromNumber,int64_t resumeToNumber)257 void HttpRequestOptions::SetRangeNumber(int64_t resumeFromNumber, int64_t resumeToNumber)
258 {
259 if (resumeFromNumber >= MIN_RESUM_NUMBER && resumeFromNumber <= MAX_RESUM_NUMBER) {
260 resumeFromNumber_ = resumeFromNumber;
261 }
262 if (resumeToNumber >= MIN_RESUM_NUMBER && resumeToNumber <= MAX_RESUM_NUMBER) {
263 resumeToNumber_ = resumeToNumber;
264 }
265 }
266
GetRangeString() const267 std::string HttpRequestOptions::GetRangeString() const
268 {
269 bool isSetFrom = resumeFromNumber_ >= MIN_RESUM_NUMBER;
270 bool isSetTo = resumeToNumber_ >= MIN_RESUM_NUMBER;
271 if (!isSetTo && !isSetFrom) {
272 return "";
273 } else if (!isSetTo && isSetFrom) {
274 return std::to_string(resumeFromNumber_) + '-';
275 } else if (isSetTo && !isSetFrom) {
276 return '-' + std::to_string(resumeToNumber_);
277 } else if (resumeToNumber_ <= resumeFromNumber_) {
278 return "";
279 } else {
280 return std::to_string(resumeFromNumber_) + '-' + std::to_string(resumeToNumber_);
281 }
282 }
283
GetDnsServers() const284 const std::vector<std::string> &HttpRequestOptions::GetDnsServers() const
285 {
286 return dnsServers_;
287 }
288
SetDnsServers(const std::vector<std::string> & dnsServers)289 void HttpRequestOptions::SetDnsServers(const std::vector<std::string> &dnsServers)
290 {
291 dnsServers_ = dnsServers;
292 }
293
GetMultiPartDataList()294 std::vector<MultiFormData> HttpRequestOptions::GetMultiPartDataList()
295 {
296 return multiFormDataList_;
297 }
298 } // namespace OHOS::NetStack::Http