1 /*
2  * Copyright (c) 2023-2024 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 <iostream>
17 #include <vector>
18 #include <algorithm>
19 #include <cctype>
20 
21 #include "http_client_request.h"
22 #include "http_client_constant.h"
23 #include "netstack_common_utils.h"
24 #include "netstack_log.h"
25 
26 namespace OHOS {
27 namespace NetStack {
28 namespace HttpClient {
29 static constexpr const uint32_t HTTP_MIN_PRIORITY = 1;
30 static constexpr const uint32_t HTTP_DEFAULT_PRIORITY = 500;
31 static constexpr const uint32_t HTTP_MAX_PRIORITY = 1000;
32 
HttpClientRequest()33 HttpClientRequest::HttpClientRequest()
34     : method_(HttpConstant::HTTP_METHOD_GET),
35       timeout_(HttpConstant::DEFAULT_READ_TIMEOUT),
36       connectTimeout_(HttpConstant::DEFAULT_CONNECT_TIMEOUT),
37       protocol_(HttpProtocol::HTTP_NONE),
38       proxyType_(HttpProxyType::NOT_USE),
39       priority_(HTTP_DEFAULT_PRIORITY)
40 {
41 }
42 
SetURL(const std::string & url)43 void HttpClientRequest::SetURL(const std::string &url)
44 {
45     url_ = url;
46 }
47 
SetHeader(const std::string & key,const std::string & val)48 void HttpClientRequest::SetHeader(const std::string &key, const std::string &val)
49 {
50     headers_[CommonUtils::ToLower(key)] = val;
51 }
52 
MethodForGet(const std::string & method)53 bool HttpClientRequest::MethodForGet(const std::string &method)
54 {
55     return (method == HttpConstant::HTTP_METHOD_HEAD || method == HttpConstant::HTTP_METHOD_OPTIONS ||
56             method == HttpConstant::HTTP_METHOD_DELETE || method == HttpConstant::HTTP_METHOD_TRACE ||
57             method == HttpConstant::HTTP_METHOD_GET || method == HttpConstant::HTTP_METHOD_CONNECT);
58 }
59 
MethodForPost(const std::string & method)60 bool HttpClientRequest::MethodForPost(const std::string &method)
61 {
62     return (method == HttpConstant::HTTP_METHOD_POST || method == HttpConstant::HTTP_METHOD_PUT);
63 }
64 
SetMethod(const std::string & method)65 void HttpClientRequest::SetMethod(const std::string &method)
66 {
67     if (!MethodForGet(method) && !MethodForPost(method) && !method.empty()) {
68         NETSTACK_LOGE("HttpClientRequest::SetMethod method %{public}s not supported", method.c_str());
69         return;
70     }
71 
72     method_ = method;
73 }
74 
SetBody(const void * data,size_t length)75 void HttpClientRequest::SetBody(const void *data, size_t length)
76 {
77     body_.append(static_cast<const char *>(data), length);
78 }
79 
SetTimeout(unsigned int timeout)80 void HttpClientRequest::SetTimeout(unsigned int timeout)
81 {
82     timeout_ = timeout;
83 }
84 
SetConnectTimeout(unsigned int timeout)85 void HttpClientRequest::SetConnectTimeout(unsigned int timeout)
86 {
87     connectTimeout_ = timeout;
88 }
89 
SetHttpProtocol(HttpProtocol protocol)90 void HttpClientRequest::SetHttpProtocol(HttpProtocol protocol)
91 {
92     protocol_ = protocol;
93 }
94 
SetHttpProxyType(HttpProxyType type)95 void HttpClientRequest::SetHttpProxyType(HttpProxyType type)
96 {
97     proxyType_ = type;
98 }
99 
SetCaPath(const std::string & path)100 void HttpClientRequest::SetCaPath(const std::string &path)
101 {
102     if (path.empty()) {
103         NETSTACK_LOGE("HttpClientRequest::SetCaPath path is empty");
104         return;
105     }
106     caPath_ = path;
107 }
108 
SetPriority(unsigned int priority)109 void HttpClientRequest::SetPriority(unsigned int priority)
110 {
111     if (priority < HTTP_MIN_PRIORITY || priority > HTTP_MAX_PRIORITY) {
112         NETSTACK_LOGE("HttpClientRequest::SetPriority priority %{public}d is invalid", priority);
113         return;
114     }
115     priority_ = priority;
116 }
117 
GetURL() const118 const std::string &HttpClientRequest::GetURL() const
119 {
120     return url_;
121 }
122 
GetMethod() const123 const std::string &HttpClientRequest::GetMethod() const
124 {
125     return method_;
126 }
127 
GetBody() const128 const std::string &HttpClientRequest::GetBody() const
129 {
130     return body_;
131 }
132 
GetHeaders() const133 const std::map<std::string, std::string> &HttpClientRequest::GetHeaders() const
134 {
135     return headers_;
136 }
137 
GetTimeout()138 unsigned int HttpClientRequest::GetTimeout()
139 {
140     return timeout_;
141 }
142 
GetConnectTimeout()143 unsigned int HttpClientRequest::GetConnectTimeout()
144 {
145     return connectTimeout_;
146 }
147 
GetHttpProtocol()148 HttpProtocol HttpClientRequest::GetHttpProtocol()
149 {
150     return protocol_;
151 }
152 
GetHttpProxyType()153 HttpProxyType HttpClientRequest::GetHttpProxyType()
154 {
155     return proxyType_;
156 }
157 
GetCaPath()158 const std::string &HttpClientRequest::GetCaPath()
159 {
160     return caPath_;
161 }
162 
GetPriority() const163 uint32_t HttpClientRequest::GetPriority() const
164 {
165     return priority_;
166 }
167 
SetHttpProxy(const HttpProxy & proxy)168 void HttpClientRequest::SetHttpProxy(const HttpProxy &proxy)
169 {
170     proxy_ = proxy;
171 }
172 
GetHttpProxy() const173 const HttpProxy &HttpClientRequest::GetHttpProxy() const
174 {
175     return proxy_;
176 }
177 
SetRequestTime(const std::string & time)178 void HttpClientRequest::SetRequestTime(const std::string &time)
179 {
180     requestTime_ = time;
181 }
182 
GetRequestTime() const183 const std::string &HttpClientRequest::GetRequestTime() const
184 {
185     return requestTime_;
186 }
187 } // namespace HttpClient
188 } // namespace NetStack
189 } // namespace OHOS
190