1 /*
2 * Copyright (c) 2021-2022 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 {
24 static constexpr const uint32_t MIN_PRIORITY = 0;
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_(0),
34 usingHttpProxyType_(UsingHttpProxyType::NOT_USE),
35 httpProxyPort_(0)
36 {
37 header_[CommonUtils::ToLower(HttpConstant::HTTP_CONTENT_TYPE)] = HttpConstant::HTTP_CONTENT_TYPE_JSON; // default
38 }
39
SetUrl(const std::string & url)40 void HttpRequestOptions::SetUrl(const std::string &url)
41 {
42 url_ = url;
43 }
44
SetMethod(const std::string & method)45 void HttpRequestOptions::SetMethod(const std::string &method)
46 {
47 method_ = method;
48 }
49
SetBody(const void * data,size_t length)50 void HttpRequestOptions::SetBody(const void *data, size_t length)
51 {
52 body_.append(static_cast<const char *>(data), length);
53 }
54
SetHeader(const std::string & key,const std::string & val)55 void HttpRequestOptions::SetHeader(const std::string &key, const std::string &val)
56 {
57 header_[key] = val;
58 }
59
SetReadTimeout(uint32_t readTimeout)60 void HttpRequestOptions::SetReadTimeout(uint32_t readTimeout)
61 {
62 readTimeout_ = readTimeout;
63 }
64
SetConnectTimeout(uint32_t connectTimeout)65 void HttpRequestOptions::SetConnectTimeout(uint32_t connectTimeout)
66 {
67 connectTimeout_ = connectTimeout;
68 }
69
GetUrl() const70 const std::string &HttpRequestOptions::GetUrl() const
71 {
72 return url_;
73 }
74
GetMethod() const75 const std::string &HttpRequestOptions::GetMethod() const
76 {
77 return method_;
78 }
79
GetBody() const80 const std::string &HttpRequestOptions::GetBody() const
81 {
82 return body_;
83 }
84
GetHeader() const85 const std::map<std::string, std::string> &HttpRequestOptions::GetHeader() const
86 {
87 return header_;
88 }
89
GetReadTimeout() const90 uint32_t HttpRequestOptions::GetReadTimeout() const
91 {
92 return readTimeout_;
93 }
94
GetConnectTimeout() const95 uint32_t HttpRequestOptions::GetConnectTimeout() const
96 {
97 return connectTimeout_;
98 }
99
SetUsingProtocol(HttpProtocol httpProtocol)100 void HttpRequestOptions::SetUsingProtocol(HttpProtocol httpProtocol)
101 {
102 usingProtocol_ = httpProtocol;
103 }
104
GetHttpVersion() const105 uint32_t HttpRequestOptions::GetHttpVersion() const
106 {
107 if (usingProtocol_ == HttpProtocol::HTTP2) {
108 NETSTACK_LOGI("CURL_HTTP_VERSION_2_0");
109 return CURL_HTTP_VERSION_2_0;
110 }
111 if (usingProtocol_ == HttpProtocol::HTTP1_1) {
112 NETSTACK_LOGI("CURL_HTTP_VERSION_1_1");
113 return CURL_HTTP_VERSION_1_1;
114 }
115 return CURL_HTTP_VERSION_NONE;
116 }
117
SetRequestTime(const std::string & time)118 void HttpRequestOptions::SetRequestTime(const std::string &time)
119 {
120 requestTime_ = time;
121 }
122
GetRequestTime() const123 const std::string &HttpRequestOptions::GetRequestTime() const
124 {
125 return requestTime_;
126 }
127
SetHttpDataType(HttpDataType dataType)128 void HttpRequestOptions::SetHttpDataType(HttpDataType dataType)
129 {
130 if (dataType != HttpDataType::STRING && dataType != HttpDataType::ARRAY_BUFFER &&
131 dataType != HttpDataType::OBJECT) {
132 return;
133 }
134 dataType_ = dataType;
135 }
GetHttpDataType() const136 HttpDataType HttpRequestOptions::GetHttpDataType() const
137 {
138 return dataType_;
139 }
140
SetPriority(uint32_t priority)141 void HttpRequestOptions::SetPriority(uint32_t priority)
142 {
143 if (priority < MIN_PRIORITY || priority > MAX_PRIORITY) {
144 return;
145 }
146 priority_ = priority;
147 }
148
GetPriority() const149 uint32_t HttpRequestOptions::GetPriority() const
150 {
151 return priority_;
152 }
153
SetUsingHttpProxyType(UsingHttpProxyType type)154 void HttpRequestOptions::SetUsingHttpProxyType(UsingHttpProxyType type)
155 {
156 usingHttpProxyType_ = type;
157 }
158
GetUsingHttpProxyType() const159 UsingHttpProxyType HttpRequestOptions::GetUsingHttpProxyType() const
160 {
161 return usingHttpProxyType_;
162 }
163
SetSpecifiedHttpProxy(const std::string & host,int32_t port,const std::string & exclusionList)164 void HttpRequestOptions::SetSpecifiedHttpProxy(const std::string &host, int32_t port, const std::string &exclusionList)
165 {
166 httpProxyHost_ = host;
167 httpProxyPort_ = port;
168 httpProxyExclusions_ = exclusionList;
169 }
170
GetSpecifiedHttpProxy(std::string & host,int32_t & port,std::string & exclusionList)171 void HttpRequestOptions::GetSpecifiedHttpProxy(std::string &host, int32_t &port, std::string &exclusionList)
172 {
173 host = httpProxyHost_;
174 port = httpProxyPort_;
175 exclusionList = httpProxyExclusions_;
176 }
177 } // namespace OHOS::NetStack