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 void SetUrl(const std::string &url); 90 91 void SetMethod(const std::string &method); 92 93 void SetBody(const void *data, size_t length); 94 95 void SetHeader(const std::string &key, const std::string &val); 96 97 void SetReadTimeout(uint32_t readTimeout); 98 99 void SetMaxLimit(uint32_t maxLimit); 100 101 void SetConnectTimeout(uint32_t connectTimeout); 102 103 void SetUsingProtocol(HttpProtocol httpProtocol); 104 105 void SetHttpDataType(HttpDataType dataType); 106 107 void SetUsingHttpProxyType(UsingHttpProxyType type); 108 109 void SetSpecifiedHttpProxy(const std::string &host, int32_t port, const std::string &exclusionList); 110 111 void SetCaPath(const std::string &SetCaPath); 112 113 void SetDnsServers(const std::vector<std::string> &dnsServers); 114 115 void SetDohUrl(const std::string &SetDohUrl); 116 117 void SetRangeNumber(int64_t resumeFromNumber, int64_t resumeToNumber); 118 119 void SetClientCert(std::string &cert, std::string &certType, std::string &key, Secure::SecureChar &keyPasswd); 120 121 void AddMultiFormData(const MultiFormData &multiFormData); 122 123 void SetTlsOption(const TlsOption &tlsOption); 124 125 void SetServerAuthentication(const ServerAuthentication &serverAuthentication); 126 127 void SetCertificatePinning(std::string certPIN); 128 129 void SetCanSkipCertVerifyFlag(bool canCertVerify); 130 131 [[nodiscard]] std::string GetCertificatePinning() const; 132 133 [[nodiscard]] const std::string &GetUrl() const; 134 135 [[nodiscard]] const std::string &GetMethod() const; 136 137 [[nodiscard]] const std::string &GetBody() const; 138 139 [[nodiscard]] const std::map<std::string, std::string> &GetHeader() const; 140 141 [[nodiscard]] uint32_t GetReadTimeout() const; 142 143 [[nodiscard]] uint32_t GetMaxLimit() const; 144 145 [[nodiscard]] uint32_t GetConnectTimeout() const; 146 147 [[nodiscard]] uint32_t GetHttpVersion() const; 148 149 void SetRequestTime(const std::string &time); 150 151 [[nodiscard]] const std::string &GetRequestTime() const; 152 153 [[nodiscard]] HttpDataType GetHttpDataType() const; 154 155 void SetPriority(uint32_t priority); 156 157 [[nodiscard]] uint32_t GetPriority() const; 158 159 [[nodiscard]] UsingHttpProxyType GetUsingHttpProxyType() const; 160 161 void GetSpecifiedHttpProxy(std::string &host, int32_t &port, std::string &exclusionList); 162 163 [[nodiscard]] const std::string &GetCaPath() const; 164 165 [[nodiscard]] const std::string &GetDohUrl() const; 166 167 [[nodiscard]] std::string GetRangeString() const; 168 169 [[nodiscard]] const std::vector<std::string> &GetDnsServers() const; 170 171 [[nodiscard]] bool GetCanSkipCertVerifyFlag() const; 172 173 void GetClientCert(std::string &cert, std::string &certType, std::string &key, Secure::SecureChar &keyPasswd); 174 175 std::vector<MultiFormData> GetMultiPartDataList(); 176 177 [[nodiscard]] const TlsOption GetTlsOption() const; 178 179 [[nodiscard]] const ServerAuthentication GetServerAuthentication() const; 180 181 void SetAddressFamily(std::string addressFamily); 182 183 [[nodiscard]] std::string GetAddressFamily() const; 184 private: 185 std::string url_; 186 187 std::string body_; 188 189 std::string method_; 190 191 std::map<std::string, std::string> header_; 192 193 uint32_t readTimeout_; 194 195 uint32_t maxLimit_; 196 197 uint32_t connectTimeout_; 198 199 HttpProtocol usingProtocol_; 200 201 std::string requestTime_; 202 203 HttpDataType dataType_; 204 205 uint32_t priority_; 206 207 UsingHttpProxyType usingHttpProxyType_; 208 209 std::string httpProxyHost_; 210 211 int32_t httpProxyPort_; 212 213 std::string httpProxyExclusions_; 214 215 std::string caPath_; 216 217 std::string dohUrl_; 218 219 std::vector<std::string> dnsServers_; 220 221 int64_t resumeFromNumber_; 222 223 int64_t resumeToNumber_; 224 225 std::string cert_; 226 227 std::string certType_; 228 229 std::string key_; 230 231 Secure::SecureChar keyPasswd_; 232 233 bool canSkipCertVerify_ = false; 234 235 std::vector<MultiFormData> multiFormDataList_; 236 237 std::string certificatePinning_; 238 239 TlsOption tlsOption_; 240 241 ServerAuthentication serverAuthentication_; 242 243 std::string addressFamily_; 244 }; 245 } // namespace OHOS::NetStack::Http 246 247 #endif /* COMMUNICATIONNETSTACK_HTTP_REQUEST_OPTIONS_H */ 248