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
23 namespace OHOS::NetStack::Http {
24 static constexpr const uint32_t MIN_PRIORITY = 1;
25 static constexpr const uint32_t MAX_PRIORITY = 1000;
26
HttpRequestOptions()27 HttpRequestOptions::HttpRequestOptions()
28 : method_(HttpConstant::HTTP_METHOD_GET),
29 readTimeout_(HttpConstant::DEFAULT_READ_TIMEOUT),
30 connectTimeout_(HttpConstant::DEFAULT_CONNECT_TIMEOUT),
31 usingProtocol_(HttpProtocol::HTTP_NONE),
32 dataType_(HttpDataType::NO_DATA_TYPE),
33 priority_(MIN_PRIORITY),
34 usingHttpProxyType_(UsingHttpProxyType::NOT_USE),
35 httpProxyPort_(0)
36 {
37 #ifndef WINDOWS_PLATFORM
38 caPath_ = HttpConstant::HTTP_DEFAULT_CA_PATH;
39 #endif // WINDOWS_PLATFORM
40 }
41
SetUrl(const std::string & url)42 void HttpRequestOptions::SetUrl(const std::string &url)
43 {
44 url_ = url;
45 }
46
SetMethod(const std::string & method)47 void HttpRequestOptions::SetMethod(const std::string &method)
48 {
49 method_ = method;
50 }
51
SetBody(const void * data,size_t length)52 void HttpRequestOptions::SetBody(const void *data, size_t length)
53 {
54 body_.append(static_cast<const char *>(data), length);
55 }
56
SetHeader(const std::string & key,const std::string & val)57 void HttpRequestOptions::SetHeader(const std::string &key, const std::string &val)
58 {
59 header_[key] = val;
60 }
61
SetReadTimeout(uint32_t readTimeout)62 void HttpRequestOptions::SetReadTimeout(uint32_t readTimeout)
63 {
64 readTimeout_ = readTimeout;
65 }
66
SetConnectTimeout(uint32_t connectTimeout)67 void HttpRequestOptions::SetConnectTimeout(uint32_t connectTimeout)
68 {
69 connectTimeout_ = connectTimeout;
70 }
71
GetUrl() const72 const std::string &HttpRequestOptions::GetUrl() const
73 {
74 return url_;
75 }
76
GetMethod() const77 const std::string &HttpRequestOptions::GetMethod() const
78 {
79 return method_;
80 }
81
GetBody() const82 const std::string &HttpRequestOptions::GetBody() const
83 {
84 return body_;
85 }
86
GetHeader() const87 const std::map<std::string, std::string> &HttpRequestOptions::GetHeader() const
88 {
89 return header_;
90 }
91
GetReadTimeout() const92 uint32_t HttpRequestOptions::GetReadTimeout() const
93 {
94 return readTimeout_;
95 }
96
GetConnectTimeout() const97 uint32_t HttpRequestOptions::GetConnectTimeout() const
98 {
99 return connectTimeout_;
100 }
101
SetUsingProtocol(HttpProtocol httpProtocol)102 void HttpRequestOptions::SetUsingProtocol(HttpProtocol httpProtocol)
103 {
104 usingProtocol_ = httpProtocol;
105 }
106
GetHttpVersion() const107 uint32_t HttpRequestOptions::GetHttpVersion() const
108 {
109 if (usingProtocol_ == HttpProtocol::HTTP2) {
110 NETSTACK_LOGI("CURL_HTTP_VERSION_2_0");
111 return CURL_HTTP_VERSION_2_0;
112 }
113 if (usingProtocol_ == HttpProtocol::HTTP1_1) {
114 NETSTACK_LOGI("CURL_HTTP_VERSION_1_1");
115 return CURL_HTTP_VERSION_1_1;
116 }
117 return CURL_HTTP_VERSION_NONE;
118 }
119
SetRequestTime(const std::string & time)120 void HttpRequestOptions::SetRequestTime(const std::string &time)
121 {
122 requestTime_ = time;
123 }
124
GetRequestTime() const125 const std::string &HttpRequestOptions::GetRequestTime() const
126 {
127 return requestTime_;
128 }
129
SetHttpDataType(HttpDataType dataType)130 void HttpRequestOptions::SetHttpDataType(HttpDataType dataType)
131 {
132 if (dataType != HttpDataType::STRING && dataType != HttpDataType::ARRAY_BUFFER &&
133 dataType != HttpDataType::OBJECT) {
134 return;
135 }
136 dataType_ = dataType;
137 }
GetHttpDataType() const138 HttpDataType HttpRequestOptions::GetHttpDataType() const
139 {
140 return dataType_;
141 }
142
SetPriority(uint32_t priority)143 void HttpRequestOptions::SetPriority(uint32_t priority)
144 {
145 if (priority < MIN_PRIORITY || priority > MAX_PRIORITY) {
146 return;
147 }
148 priority_ = priority;
149 }
150
GetPriority() const151 uint32_t HttpRequestOptions::GetPriority() const
152 {
153 return priority_;
154 }
155
SetUsingHttpProxyType(UsingHttpProxyType type)156 void HttpRequestOptions::SetUsingHttpProxyType(UsingHttpProxyType type)
157 {
158 usingHttpProxyType_ = type;
159 }
160
GetUsingHttpProxyType() const161 UsingHttpProxyType HttpRequestOptions::GetUsingHttpProxyType() const
162 {
163 return usingHttpProxyType_;
164 }
165
SetSpecifiedHttpProxy(const std::string & host,int32_t port,const std::string & exclusionList)166 void HttpRequestOptions::SetSpecifiedHttpProxy(const std::string &host, int32_t port, const std::string &exclusionList)
167 {
168 httpProxyHost_ = host;
169 httpProxyPort_ = port;
170 httpProxyExclusions_ = exclusionList;
171 }
172
GetSpecifiedHttpProxy(std::string & host,int32_t & port,std::string & exclusionList)173 void HttpRequestOptions::GetSpecifiedHttpProxy(std::string &host, int32_t &port, std::string &exclusionList)
174 {
175 host = httpProxyHost_;
176 port = httpProxyPort_;
177 exclusionList = httpProxyExclusions_;
178 }
179
SetCaPath(const std::string & path)180 void HttpRequestOptions::SetCaPath(const std::string &path)
181 {
182 if (path.empty()) {
183 return;
184 }
185
186 caPath_ = path;
187 }
188
GetCaPath() const189 const std::string &HttpRequestOptions::GetCaPath() const
190 {
191 return caPath_;
192 }
193 } // namespace OHOS::NetStack::Http