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 #ifndef COMMUNICATIONNETSTACK_HTTP_REQUEST_OPTIONS_H 17 #define COMMUNICATIONNETSTACK_HTTP_REQUEST_OPTIONS_H 18 19 #include <map> 20 #include <string> 21 #include <vector> 22 23 #include "constant.h" 24 #include "secure_char.h" 25 #include "http_tls_config.h" 26 #include "napi_utils.h" 27 28 namespace OHOS::NetStack::Http { 29 enum class HttpProtocol { 30 HTTP1_1, 31 HTTP2, 32 HTTP3, 33 HTTP_NONE, // default choose by curl 34 }; 35 36 enum class UsingHttpProxyType { 37 NOT_USE, 38 USE_DEFAULT, 39 USE_SPECIFIED, 40 }; 41 42 struct MultiFormData { 43 MultiFormData() = default; 44 ~MultiFormData() = default; 45 std::string name; 46 std::string contentType; 47 std::string remoteFileName; 48 std::string data; 49 std::string filePath; 50 }; 51 52 enum class HashAlgorithm { 53 SHA256, 54 INVALID, 55 }; 56 57 enum class AuthenticationType { 58 AUTO, 59 BASIC, 60 NTLM, 61 DIGEST, 62 }; 63 64 struct Credential { 65 NapiUtils::SecureData username; 66 NapiUtils::SecureData password; 67 }; 68 69 struct ServerAuthentication { 70 Credential credential; 71 AuthenticationType authenticationType = AuthenticationType::AUTO; 72 }; 73 74 struct TlsOption { 75 std::unordered_set<CipherSuite> cipherSuite; 76 TlsVersion tlsVersionMin = TlsVersion::DEFAULT; 77 TlsVersion tlsVersionMax = TlsVersion::DEFAULT; 78 }; 79 80 struct CertificatePinning { 81 HashAlgorithm hashAlgorithm = HashAlgorithm::SHA256; 82 std::string publicKeyHash; 83 }; 84 85 class HttpRequestOptions final { 86 public: 87 HttpRequestOptions(); 88 89 // https://man7.org/linux/man-pages/man7/tcp.7.html 90 struct TcpConfiguration { 91 friend class HttpRequestOptions; 92 /* 93 TCP_KEEPIDLE (since Linux 2.4) 94 The time (in seconds) the connection needs to remain idle 95 before TCP starts sending keepalive probes, if the socket 96 option SO_KEEPALIVE has been set on this socket. This 97 option should not be used in code intended to be portable. 98 For example, if the server will send FIN packet after 60 seconds, we suggest that set <keepIdle> to 61. 99 */ 100 int keepIdle_ = 60 * 5; // Default value according to <NetworkKit> Cloud Service. 101 /* 102 TCP_KEEPINTVL (since Linux 2.4) 103 The time (in seconds) between individual keepalive probes. 104 This option should not be used in code intended to be 105 portable. 106 <keepCnt> == 1 means that we just probe once, if it fails, we close the connection. 107 */ 108 int keepCnt_ = 1; 109 /* 110 TCP_KEEPINTVL (since Linux 2.4) 111 The time (in seconds) between individual keepalive probes. 112 This option should not be used in code intended to be 113 portable. 114 */ 115 int keepInterval_ = 1; 116 117 /* 118 TCP_USER_TIMEOUT (since Linux 2.6.37) 119 This option takes an unsigned int as an argument. When 120 the value is greater than 0, it specifies the maximum 121 amount of time in milliseconds that transmitted data may 122 remain unacknowledged, or buffered data may remain 123 untransmitted (due to zero window size) before TCP will 124 forcibly close the corresponding connection and return 125 ETIMEDOUT to the application. If the option value is 126 specified as 0, TCP will use the system default. 127 128 Increasing user timeouts allows a TCP connection to 129 survive extended periods without end-to-end connectivity. 130 Decreasing user timeouts allows applications to "fail 131 fast", if so desired. Otherwise, failure may take up to 132 20 minutes with the current system defaults in a normal 133 WAN environment. 134 135 This option can be set during any state of a TCP 136 connection, but is effective only during the synchronized 137 states of a connection (ESTABLISHED, FIN-WAIT-1, FIN- 138 WAIT-2, CLOSE-WAIT, CLOSING, and LAST-ACK). Moreover, 139 when used with the TCP keepalive (SO_KEEPALIVE) option, 140 TCP_USER_TIMEOUT will override keepalive to determine when 141 to close a connection due to keepalive failure. 142 143 The option has no effect on when TCP retransmits a packet, 144 nor when a keepalive probe is sent. 145 146 This option, like many others, will be inherited by the 147 socket returned by accept(2), if it was set on the 148 listening socket. 149 150 Further details on the user timeout feature can be found 151 in RFC 793 and RFC 5482 ("TCP User Timeout Option"). 152 */ 153 int userTimeout_ = HttpConstant::DEFAULT_READ_TIMEOUT; 154 155 bool SetOptionToSocket(int sock); 156 void SetTcpUserTimeout(const uint32_t &timeout); 157 }; 158 159 void SetUrl(const std::string &url); 160 161 void SetMethod(const std::string &method); 162 163 void SetBody(const void *data, size_t length); 164 165 void SetHeader(const std::string &key, const std::string &val); 166 167 void SetReadTimeout(uint32_t readTimeout); 168 169 void SetMaxLimit(uint32_t maxLimit); 170 171 void SetConnectTimeout(uint32_t connectTimeout); 172 173 void SetUsingProtocol(HttpProtocol httpProtocol); 174 175 void SetHttpDataType(HttpDataType dataType); 176 177 void SetUsingHttpProxyType(UsingHttpProxyType type); 178 179 void SetSpecifiedHttpProxy(const std::string &host, int32_t port, const std::string &exclusionList); 180 181 void SetCaPath(const std::string &SetCaPath); 182 183 void SetCaData(const std::string &caData); 184 185 void SetDnsServers(const std::vector<std::string> &dnsServers); 186 187 void SetDohUrl(const std::string &SetDohUrl); 188 189 void SetRangeNumber(int64_t resumeFromNumber, int64_t resumeToNumber); 190 191 void SetClientCert(std::string &cert, std::string &certType, std::string &key, Secure::SecureChar &keyPasswd); 192 193 void AddMultiFormData(const MultiFormData &multiFormData); 194 195 void SetTlsOption(const TlsOption &tlsOption); 196 197 void SetServerAuthentication(const ServerAuthentication &serverAuthentication); 198 199 void SetCertificatePinning(std::string certPIN); 200 201 void SetCanSkipCertVerifyFlag(bool canCertVerify); 202 203 [[nodiscard]] std::string GetCertificatePinning() const; 204 205 [[nodiscard]] const std::string &GetUrl() const; 206 207 [[nodiscard]] const std::string &GetMethod() const; 208 209 [[nodiscard]] const std::string &GetBody() const; 210 211 [[nodiscard]] const std::map<std::string, std::string> &GetHeader() const; 212 213 [[nodiscard]] uint32_t GetReadTimeout() const; 214 215 [[nodiscard]] uint32_t GetMaxLimit() const; 216 217 [[nodiscard]] uint32_t GetConnectTimeout() const; 218 219 [[nodiscard]] uint32_t GetHttpVersion() const; 220 221 void SetRequestTime(const std::string &time); 222 223 [[nodiscard]] const std::string &GetRequestTime() const; 224 225 [[nodiscard]] HttpDataType GetHttpDataType() const; 226 227 void SetPriority(uint32_t priority); 228 229 [[nodiscard]] uint32_t GetPriority() const; 230 231 [[nodiscard]] UsingHttpProxyType GetUsingHttpProxyType() const; 232 233 void GetSpecifiedHttpProxy(std::string &host, int32_t &port, std::string &exclusionList); 234 235 [[nodiscard]] const std::string &GetCaPath() const; 236 237 [[nodiscard]] const std::string &GetCaData() const; 238 239 [[nodiscard]] const std::string &GetDohUrl() const; 240 241 [[nodiscard]] std::string GetRangeString() const; 242 243 [[nodiscard]] const std::vector<std::string> &GetDnsServers() const; 244 245 [[nodiscard]] bool GetCanSkipCertVerifyFlag() const; 246 247 void GetClientCert(std::string &cert, std::string &certType, std::string &key, Secure::SecureChar &keyPasswd); 248 249 std::vector<MultiFormData> GetMultiPartDataList(); 250 251 [[nodiscard]] const TlsOption GetTlsOption() const; 252 [[nodiscard]] const TcpConfiguration GetTCPOption() const; 253 254 [[nodiscard]] const ServerAuthentication GetServerAuthentication() const; 255 256 void SetAddressFamily(std::string addressFamily); 257 258 [[nodiscard]] std::string GetAddressFamily() const; 259 private: 260 std::string url_; 261 262 std::string body_; 263 264 std::string method_; 265 266 std::map<std::string, std::string> header_; 267 268 uint32_t readTimeout_; 269 270 uint32_t maxLimit_; 271 272 uint32_t connectTimeout_; 273 274 HttpProtocol usingProtocol_; 275 276 std::string requestTime_; 277 278 HttpDataType dataType_; 279 280 uint32_t priority_; 281 282 UsingHttpProxyType usingHttpProxyType_; 283 284 std::string httpProxyHost_; 285 286 int32_t httpProxyPort_; 287 288 std::string httpProxyExclusions_; 289 290 std::string caPath_; 291 292 std::string caData_; 293 294 std::string dohUrl_; 295 296 std::vector<std::string> dnsServers_; 297 298 int64_t resumeFromNumber_; 299 300 int64_t resumeToNumber_; 301 302 std::string cert_; 303 304 std::string certType_; 305 306 std::string key_; 307 308 Secure::SecureChar keyPasswd_; 309 310 bool canSkipCertVerify_ = false; 311 312 std::vector<MultiFormData> multiFormDataList_; 313 314 std::string certificatePinning_; 315 316 TlsOption tlsOption_; 317 318 ServerAuthentication serverAuthentication_; 319 320 std::string addressFamily_; 321 322 TcpConfiguration tcpOption_; 323 }; 324 } // namespace OHOS::NetStack::Http 325 326 #endif /* COMMUNICATIONNETSTACK_HTTP_REQUEST_OPTIONS_H */ 327