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 
26 namespace OHOS::NetStack::Http {
27 enum class HttpProtocol {
28     HTTP1_1,
29     HTTP2,
30     HTTP3,
31     HTTP_NONE, // default choose by curl
32 };
33 
34 enum class UsingHttpProxyType {
35     NOT_USE,
36     USE_DEFAULT,
37     USE_SPECIFIED,
38 };
39 
40 struct MultiFormData {
41     MultiFormData() = default;
42     ~MultiFormData() = default;
43     std::string name;
44     std::string contentType;
45     std::string remoteFileName;
46     std::string data;
47     std::string filePath;
48 };
49 
50 enum class HashAlgorithm {
51     SHA256,
52     INVALID,
53 };
54 
55 struct CertificatePinning {
56     HashAlgorithm hashAlgorithm = HashAlgorithm::SHA256;
57     std::string publicKeyHash;
58 };
59 
60 class HttpRequestOptions final {
61 public:
62     HttpRequestOptions();
63 
64     void SetUrl(const std::string &url);
65 
66     void SetMethod(const std::string &method);
67 
68     void SetBody(const void *data, size_t length);
69 
70     void SetHeader(const std::string &key, const std::string &val);
71 
72     void SetReadTimeout(uint32_t readTimeout);
73 
74     void SetMaxLimit(uint32_t maxLimit);
75 
76     void SetConnectTimeout(uint32_t connectTimeout);
77 
78     void SetUsingProtocol(HttpProtocol httpProtocol);
79 
80     void SetHttpDataType(HttpDataType dataType);
81 
82     void SetUsingHttpProxyType(UsingHttpProxyType type);
83 
84     void SetSpecifiedHttpProxy(const std::string &host, int32_t port, const std::string &exclusionList);
85 
86     void SetCaPath(const std::string &SetCaPath);
87 
88     void SetDnsServers(const std::vector<std::string> &dnsServers);
89 
90     void SetDohUrl(const std::string &SetDohUrl);
91 
92     void SetRangeNumber(int64_t resumeFromNumber, int64_t resumeToNumber);
93 
94     void SetClientCert(std::string &cert, std::string &certType, std::string &key, Secure::SecureChar &keyPasswd);
95 
96     void AddMultiFormData(const MultiFormData &multiFormData);
97 
98     void SetCertificatePinning(std::string certPIN);
99 
100     [[nodiscard]] std::string GetCertificatePinning() const;
101 
102     [[nodiscard]] const std::string &GetUrl() const;
103 
104     [[nodiscard]] const std::string &GetMethod() const;
105 
106     [[nodiscard]] const std::string &GetBody() const;
107 
108     [[nodiscard]] const std::map<std::string, std::string> &GetHeader() const;
109 
110     [[nodiscard]] uint32_t GetReadTimeout() const;
111 
112     [[nodiscard]] uint32_t GetMaxLimit() const;
113 
114     [[nodiscard]] uint32_t GetConnectTimeout() const;
115 
116     [[nodiscard]] uint32_t GetHttpVersion() const;
117 
118     void SetRequestTime(const std::string &time);
119 
120     [[nodiscard]] const std::string &GetRequestTime() const;
121 
122     [[nodiscard]] HttpDataType GetHttpDataType() const;
123 
124     void SetPriority(uint32_t priority);
125 
126     [[nodiscard]] uint32_t GetPriority() const;
127 
128     [[nodiscard]] UsingHttpProxyType GetUsingHttpProxyType() const;
129 
130     void GetSpecifiedHttpProxy(std::string &host, int32_t &port, std::string &exclusionList);
131 
132     [[nodiscard]] const std::string &GetCaPath() const;
133 
134     [[nodiscard]] const std::string &GetDohUrl() const;
135 
136     [[nodiscard]] std::string GetRangeString() const;
137 
138     [[nodiscard]] const std::vector<std::string> &GetDnsServers() const;
139 
140     void GetClientCert(std::string &cert, std::string &certType, std::string &key, Secure::SecureChar &keyPasswd);
141 
142     std::vector<MultiFormData> GetMultiPartDataList();
143 private:
144     std::string url_;
145 
146     std::string body_;
147 
148     std::string method_;
149 
150     std::map<std::string, std::string> header_;
151 
152     uint32_t readTimeout_;
153 
154     uint32_t maxLimit_;
155 
156     uint32_t connectTimeout_;
157 
158     HttpProtocol usingProtocol_;
159 
160     std::string requestTime_;
161 
162     HttpDataType dataType_;
163 
164     uint32_t priority_;
165 
166     UsingHttpProxyType usingHttpProxyType_;
167 
168     std::string httpProxyHost_;
169 
170     int32_t httpProxyPort_;
171 
172     std::string httpProxyExclusions_;
173 
174     std::string caPath_;
175 
176     std::string dohUrl_;
177 
178     std::vector<std::string> dnsServers_;
179 
180     int64_t resumeFromNumber_;
181 
182     int64_t resumeToNumber_;
183 
184     std::string cert_;
185 
186     std::string certType_;
187 
188     std::string key_;
189 
190     Secure::SecureChar keyPasswd_;
191 
192     std::vector<MultiFormData> multiFormDataList_;
193 
194     std::string certificatePinning_;
195 };
196 } // namespace OHOS::NetStack::Http
197 
198 #endif /* COMMUNICATIONNETSTACK_HTTP_REQUEST_OPTIONS_H */
199