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 enum HttpProxyType { 27 NOT_USE, 28 USE_SPECIFIED, 29 PROXY_TYPE_MAX, 30 }; 31 32 enum HttpProtocol { 33 HTTP_NONE, // default choose by curl 34 HTTP1_1, 35 HTTP2, 36 HTTP3, 37 HTTP_PROTOCOL_MAX, 38 }; 39 40 struct HttpProxy { 41 std::string host; 42 int32_t port; 43 std::string exclusions; 44 bool tunnel; 45 HttpProxyHttpProxy46 HttpProxy() : host(""), port(0), exclusions(""), tunnel(false) {} 47 }; 48 49 class HttpClientRequest { 50 public: 51 /** 52 * Default constructor for HttpClientRequest. 53 */ 54 HttpClientRequest(); 55 56 /** 57 * Set the URL for the HTTP request. 58 * @param url The URL to be set. 59 */ 60 void SetURL(const std::string &url); 61 62 /** 63 * Set the method for the HTTP request. 64 * @param method The method to be set. 65 */ 66 void SetMethod(const std::string &method); 67 68 /** 69 * Set the body data for the HTTP request. 70 * @param data Pointer to the data. 71 * @param length Length of the data. 72 */ 73 void SetBody(const void *data, size_t length); 74 75 /** 76 * Set a header field for the HTTP request. 77 * @param key The header field key. 78 * @param val The header field value. 79 */ 80 void SetHeader(const std::string &key, const std::string &val); 81 82 /** 83 * Set the timeout for the HTTP request. 84 * @param timeout The timeout value in seconds. 85 */ 86 void SetTimeout(unsigned int timeout); 87 88 /** 89 * Set the connect timeout for the HTTP request. 90 * @param timeout The connect timeout value in seconds. 91 */ 92 void SetConnectTimeout(unsigned int timeout); 93 94 /** 95 * Set the HTTP protocol for the request. 96 * @param protocol The HTTP protocol to be set. 97 */ 98 void SetHttpProtocol(HttpProtocol protocol); 99 100 /** 101 * Set the HTTP proxy for the request. 102 * @param proxy The HTTP proxy to be set. 103 */ 104 void SetHttpProxy(const HttpProxy &proxy); 105 106 /** 107 * Set the HTTP proxy type for the request. 108 * @param type The HTTP proxy type to be set. 109 */ 110 void SetHttpProxyType(HttpProxyType type); 111 112 /** 113 * Set the CA certificate path for the HTTPS request. 114 * @param path The CA certificate path to be set. 115 */ 116 void SetCaPath(const std::string &path); 117 118 /** 119 * Set the priority for the HTTP request. 120 * @param priority The priority value to be set. 121 */ 122 void SetPriority(unsigned int priority); 123 124 /** 125 * Get the URL of the HTTP request. 126 * @return The URL of the request. 127 */ 128 [[nodiscard]] const std::string &GetURL() const; 129 130 /** 131 * Get the method of the HTTP request. 132 * @return The method of the request. 133 */ 134 [[nodiscard]] const std::string &GetMethod() const; 135 136 /** 137 * Get the body data of the HTTP request. 138 * @return The body data of the request. 139 */ 140 [[nodiscard]] const std::string &GetBody() const; 141 142 /** 143 * Get the header fields of the HTTP request. 144 * @return A map of header field key-value pairs. 145 */ 146 [[nodiscard]] const std::map<std::string, std::string> &GetHeaders() const; 147 148 /** 149 * Get the timeout of the HTTP request. 150 * @return The timeout value in seconds. 151 */ 152 [[nodiscard]] unsigned int GetTimeout(); 153 154 /** 155 * Get the connect timeout of the HTTP request. 156 * @return The connect timeout value in seconds. 157 */ 158 [[nodiscard]] unsigned int GetConnectTimeout(); 159 160 /** 161 * Get the HTTP protocol of the request. 162 * @return The HTTP protocol of the request. 163 */ 164 [[nodiscard]] HttpProtocol GetHttpProtocol(); 165 166 /** 167 * Get the HTTP proxy of the request. 168 * @return The HTTP proxy of the request. 169 */ 170 [[nodiscard]] const HttpProxy &GetHttpProxy() const; 171 172 /** 173 * Get the HTTP proxy type of the request. 174 * @return The HTTP proxy type of the request. 175 */ 176 [[nodiscard]] HttpProxyType GetHttpProxyType(); 177 178 /** 179 * Get the CA certificate path of the HTTPS request. 180 * @return The CA certificate path of the request. 181 */ 182 [[nodiscard]] const std::string &GetCaPath(); 183 184 /** 185 * Get the priority of the HTTP request. 186 * @return The priority value of the request. 187 */ 188 [[nodiscard]] uint32_t GetPriority() const; 189 190 /** 191 * Check if the specified method is suitable for a GET request. 192 * @param method The method to check. 193 * @return True if the method is suitable for a GET request, false otherwise. 194 */ 195 bool MethodForGet(const std::string &method); 196 197 /** 198 * Check if the specified method is suitable for a POST request. 199 * @param method The method to check. 200 * @return True if the method is suitable for a POST request, false otherwise. 201 */ 202 bool MethodForPost(const std::string &method); 203 204 /** 205 * Sets the request time for the object. 206 * @param time The request time to be set. 207 */ 208 void SetRequestTime(const std::string &time); 209 210 /** 211 * Retrieves the request time from the object. 212 * @return The request time. 213 */ 214 const std::string &GetRequestTime() const; 215 216 private: 217 std::string url_; 218 std::string method_; 219 std::string body_; 220 std::map<std::string, std::string> headers_; 221 unsigned int timeout_; 222 unsigned int connectTimeout_; 223 HttpProtocol protocol_; 224 HttpProxy proxy_; 225 HttpProxyType proxyType_; 226 std::string caPath_; 227 unsigned int priority_; 228 std::string requestTime_; 229 }; 230 } // namespace HttpClient 231 } // namespace NetStack 232 } // namespace OHOS 233 234 #endif // COMMUNICATIONNETSTACK_HTTP_CLIENT_REQUEST_H 235