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 #ifndef COMMUNICATIONNETSTACK_HTTP_CLIENT_REQUEST_H 17 #define COMMUNICATIONNETSTACK_HTTP_CLIENT_REQUEST_H 18 19 #include <string> 20 #include <map> 21 #include <vector> 22 23 namespace OHOS { 24 namespace NetStack { 25 namespace HttpClient { 26 static constexpr const int64_t MIN_RESUM_NUMBER = 1; 27 static constexpr const int64_t MAX_RESUM_NUMBER = 4294967296; 28 enum HttpProxyType { 29 NOT_USE, 30 USE_SPECIFIED, 31 PROXY_TYPE_MAX, 32 }; 33 34 enum HttpProtocol { 35 HTTP_NONE, // default choose by curl 36 HTTP1_1, 37 HTTP2, 38 HTTP3, 39 HTTP_PROTOCOL_MAX, 40 }; 41 42 struct HttpProxy { 43 std::string host; 44 int32_t port; 45 std::string exclusions; 46 bool tunnel; 47 HttpProxyHttpProxy48 HttpProxy() : host(""), port(0), exclusions(""), tunnel(false) {} 49 }; 50 51 struct HttpClientCert { 52 std::string certPath; 53 std::string certType; 54 std::string keyPath; 55 std::string keyPassword; 56 }; 57 58 class HttpClientRequest { 59 public: 60 /** 61 * Default constructor for HttpClientRequest. 62 */ 63 HttpClientRequest(); 64 65 /** 66 * Set the URL for the HTTP request. 67 * @param url The URL to be set. 68 */ 69 void SetURL(const std::string &url); 70 71 /** 72 * Set the method for the HTTP request. 73 * @param method The method to be set. 74 */ 75 void SetMethod(const std::string &method); 76 77 /** 78 * Set the body data for the HTTP request. 79 * @param data Pointer to the data. 80 * @param length Length of the data. 81 */ 82 void SetBody(const void *data, size_t length); 83 84 /** 85 * Set a header field for the HTTP request. 86 * @param key The header field key. 87 * @param val The header field value. 88 */ 89 void SetHeader(const std::string &key, const std::string &val); 90 91 /** 92 * Set the timeout for the HTTP request. 93 * @param timeout The timeout value in seconds. 94 */ 95 void SetTimeout(unsigned int timeout); 96 97 /** 98 * Set the connect timeout for the HTTP request. 99 * @param timeout The connect timeout value in seconds. 100 */ 101 void SetConnectTimeout(unsigned int timeout); 102 103 /** 104 * Set the HTTP protocol for the request. 105 * @param protocol The HTTP protocol to be set. 106 */ 107 void SetHttpProtocol(HttpProtocol protocol); 108 109 /** 110 * Set the HTTP proxy for the request. 111 * @param proxy The HTTP proxy to be set. 112 */ 113 void SetHttpProxy(const HttpProxy &proxy); 114 115 /** 116 * Set the HTTP proxy type for the request. 117 * @param type The HTTP proxy type to be set. 118 */ 119 void SetHttpProxyType(HttpProxyType type); 120 121 /** 122 * Set the CA certificate path for the HTTPS request. 123 * @param path The CA certificate path to be set. 124 */ 125 void SetCaPath(const std::string &path); 126 127 /** 128 * Set the priority for the HTTP request. 129 * @param priority The priority value to be set. 130 */ 131 void SetPriority(unsigned int priority); 132 133 /** 134 * Set the download start position. Only used in GET method. 135 * @param resumeFrom The resumeFrom value to be set. 136 */ 137 void SetResumeFrom(int64_t resumeFrom); 138 139 /** 140 * Set the download end position. Only used in GET method. 141 * @param resumeTo The resumeTo value to be set. 142 */ 143 void SetResumeTo(int64_t resumeTo); 144 145 /** 146 * Set the ClientCert for the HTTP request. 147 * @param clientCert The clientCert value to be set. 148 */ 149 void SetClientCert(const HttpClientCert &clientCert); 150 151 /** 152 * Set the AddressFamily for the HTTP request. 153 * @param addressFamily The addressFamily value to be set. 154 */ 155 void SetAddressFamily(const std::string &addressFamily); 156 157 /** 158 * Get the URL of the HTTP request. 159 * @return The URL of the request. 160 */ 161 [[nodiscard]] const std::string &GetURL() const; 162 163 /** 164 * Get the method of the HTTP request. 165 * @return The method of the request. 166 */ 167 [[nodiscard]] const std::string &GetMethod() const; 168 169 /** 170 * Get the body data of the HTTP request. 171 * @return The body data of the request. 172 */ 173 [[nodiscard]] const std::string &GetBody() const; 174 175 /** 176 * Get the header fields of the HTTP request. 177 * @return A map of header field key-value pairs. 178 */ 179 [[nodiscard]] const std::map<std::string, std::string> &GetHeaders() const; 180 181 /** 182 * Get the timeout of the HTTP request. 183 * @return The timeout value in seconds. 184 */ 185 [[nodiscard]] unsigned int GetTimeout(); 186 187 /** 188 * Get the connect timeout of the HTTP request. 189 * @return The connect timeout value in seconds. 190 */ 191 [[nodiscard]] unsigned int GetConnectTimeout(); 192 193 /** 194 * Get the HTTP protocol of the request. 195 * @return The HTTP protocol of the request. 196 */ 197 [[nodiscard]] HttpProtocol GetHttpProtocol(); 198 199 /** 200 * Get the HTTP proxy of the request. 201 * @return The HTTP proxy of the request. 202 */ 203 [[nodiscard]] const HttpProxy &GetHttpProxy() const; 204 205 /** 206 * Get the HTTP proxy type of the request. 207 * @return The HTTP proxy type of the request. 208 */ 209 [[nodiscard]] HttpProxyType GetHttpProxyType(); 210 211 /** 212 * Get the CA certificate path of the HTTPS request. 213 * @return The CA certificate path of the request. 214 */ 215 [[nodiscard]] const std::string &GetCaPath(); 216 217 /** 218 * Get the priority of the HTTP request. 219 * @return The priority value of the request. 220 */ 221 [[nodiscard]] uint32_t GetPriority() const; 222 223 /** 224 * Get the download start position of the HTTP request. 225 * @return The download start position of the request. 226 */ 227 [[nodiscard]] int64_t GetResumeFrom() const; 228 229 /** 230 * Get the download end position of the HTTP request. 231 * @return The end start position of the request. 232 */ 233 [[nodiscard]] int64_t GetResumeTo() const; 234 235 /** 236 * Get the ClientCert for the HTTP request. 237 * @param clientCert The clientCert value to be set. 238 */ 239 [[nodiscard]] const HttpClientCert &GetClientCert() const; 240 241 /** 242 * Get the addressFamily of the HTTP request. 243 * @return The addressFamily of the request. 244 */ 245 [[nodiscard]] const std::string &GetAddressFamily() const; 246 247 /** 248 * Check if the specified method is suitable for a GET request. 249 * @param method The method to check. 250 * @return True if the method is suitable for a GET request, false otherwise. 251 */ 252 bool MethodForGet(const std::string &method); 253 254 /** 255 * Check if the specified method is suitable for a POST request. 256 * @param method The method to check. 257 * @return True if the method is suitable for a POST request, false otherwise. 258 */ 259 bool MethodForPost(const std::string &method); 260 261 /** 262 * Sets the request time for the object. 263 * @param time The request time to be set. 264 */ 265 void SetRequestTime(const std::string &time); 266 267 /** 268 * Retrieves the request time from the object. 269 * @return The request time. 270 */ 271 const std::string &GetRequestTime() const; 272 273 private: 274 std::string url_; 275 std::string method_; 276 std::string body_; 277 std::map<std::string, std::string> headers_; 278 unsigned int timeout_; 279 unsigned int connectTimeout_; 280 HttpProtocol protocol_; 281 HttpProxy proxy_; 282 HttpProxyType proxyType_; 283 std::string caPath_; 284 unsigned int priority_; 285 std::string requestTime_; 286 int64_t resumeFrom_; 287 int64_t resumeTo_; 288 HttpClientCert clientCert_; 289 std::string addressFamily_; 290 }; 291 } // namespace HttpClient 292 } // namespace NetStack 293 } // namespace OHOS 294 295 #endif // COMMUNICATIONNETSTACK_HTTP_CLIENT_REQUEST_H 296